master
ブランチ master
LittleBeast / src / Site / Controller / Op.php
18 KB、482行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Site\Controller;
39
40use Site\Controller\Mods;
41use Std\Lib\Auth;
42use Std\Lib\Template;
43use Std\Lib\Openprovider;
44use Roles;
45
46class Op {
47 use Mods;
48
49 private Template $tmpl;
50 private Auth $auth;
51 private Openprovider $op;
52 private string $pagetit = 'OpenProvider管理 - ';
53 private string $description = '';
54 private ?\stdClass $user;
55
56 public function __construct() {
57 $this->tmpl = new Template('/openprovider/');
58
59 $this->auth = new Auth();
60 $this->user = $this->auth->getLoggedInUser();
61 if ($this->user && $this->user->role & (Roles::ADMIN)) {
62 $this->op = new Openprovider();
63 $this->op->login();
64 }
65
66 $this->tmpl->assign('user', $this->user);
67 $this->tmpl->assign('curPage', 'openprovider');
68 $this->tmpl->assign('custCss', false);
69 $this->tmpl->assign('menu', $this->getMenu());
70 $this->tmpl->assign('description', $this->description);
71 $this->tmpl->addCss('openprovider');
72 $this->tmpl->addCss('table');
73 }
74
75 public function index(array $params): void {
76 try {
77 $this->pagetit .= 'ホーム';
78 $this->tmpl->assign('pagetit', $this->pagetit);
79
80 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
81 $data = $this->op->getReseller(['with_statistics' => true]);
82 $domain = $this->op->listDomains(['order_by.renewal_date' => 'asc', 'status' => 'ACT']);
83 $this->tmpl->assign('data', $data->data);
84 $this->tmpl->assign('domain', $domain->data);
85 $this->tmpl->render('index');
86 exit();
87
88 noaccess:
89 $this->tmpl->render('nopermission');
90 } catch (\Exception $e) {
91 throw new \Exception($e->getMessage());
92 }
93 }
94
95 public function opListInvoices(array $params): void {
96 try {
97 $this->pagetit .= '請求書一覧';
98 $this->tmpl->assign('pagetit', $this->pagetit);
99 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
100 $data = $this->op->listInvoices();
101 $this->tmpl->assign('data', $data->data);
102 $this->tmpl->render('listinvoices');
103 exit();
104
105 noaccess:
106 $this->tmpl->render('nopermission');
107 } catch (\Exception $e) {
108 throw new \Exception($e->getMessage());
109 }
110 }
111
112 public function opListPayments(array $params): void {
113 try {
114 $this->pagetit .= '請求書一覧';
115 $this->tmpl->assign('pagetit', $this->pagetit);
116
117 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
118 $data = $this->op->listPayments();
119 $this->tmpl->assign('data', $data->data);
120 $this->tmpl->render('listpayments');
121 exit();
122
123 noaccess:
124 $this->tmpl->render('nopermission');
125 } catch (\Exception $e) {
126 throw new \Exception($e->getMessage());
127 }
128 }
129
130 public function opListTransactions(array $params): void {
131 try {
132 $this->pagetit .= '請求書一覧';
133 $this->tmpl->assign('pagetit', $this->pagetit);
134
135 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
136 $data = $this->op->listTransactions();
137 $this->tmpl->assign('data', $data->data);
138 $this->tmpl->render('listtransactions');
139 exit();
140
141 noaccess:
142 $this->tmpl->render('nopermission');
143 } catch (\Exception $e) {
144 throw new \Exception($e->getMessage());
145 }
146 }
147
148 public function opListCustomers(array $params): void {
149 try {
150 $this->pagetit .= '顧客様検索';
151 $this->tmpl->assign('pagetit', $this->pagetit);
152
153 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
154 $data = $this->op->listCustomers();
155 $this->tmpl->assign('data', $data->data);
156 $this->tmpl->render('listcustomers');
157 exit();
158
159 noaccess:
160 $this->tmpl->render('nopermission');
161 } catch (\Exception $e) {
162 throw new \Exception($e->getMessage());
163 }
164 }
165
166 public function opCreateCustomer(array $params): void {
167 try {
168 $payload = [
169 'additional_data' => [
170 'company_registration_number' => $_POST['company_registration_number'] ?? '',
171 'company_url' => $_POST['company_url'] ?? '',
172 'birth_date' => (isset($_POST['birth_year']) && $_POST['birth_year'] !== '')
173 && (isset($_POST['birth_month']) && $_POST['birth_month'] !== '')
174 && (isset($_POST['birth_day']) && $_POST['birth_day'] !== '')
175 ? $_POST['birth_year'].'-'.$_POST['birth_month'].'-'.$_POST['birth_day']
176 : '',
177 'trading_name' => $_POST['company_name'] ?? '',
178 ],
179 'address' => [
180 'city' => $_POST['address_city'] ?? '',
181 'country' => $_POST['address_country'] ?? 'JP',
182 'number' => $_POST['address_number'] ?? '',
183 'state' => $_POST['address_state'] ?? '',
184 'street' => $_POST['address_street'] ?? '',
185 'zipcode' => $_POST['address_zipcode'] ?? '',
186 ],
187 'birth' => [
188 'year' => $_POST['birth_year'] ?? '',
189 'month' => $_POST['birth_month'] ?? '',
190 'day' => $_POST['birth_day'] ?? '',
191 ],
192 'comments' => $_POST['comments'] ?? '',
193 'company_name' => $_POST['company_name'] ?? '',
194 'email' => $_POST['email'] ?? '',
195 'fax' => [
196 'area_code' => $_POST['fax_area_code'] ?? '',
197 'country_code' => $_POST['fax_country_code'] ?? '',
198 'subscriber_number' => $_POST['fax_subscriber_number'] ?? '',
199 ],
200 'locale' => 'ja_JP',
201 'name' => [
202 'first_name' => $_POST['first_name'] ?? '',
203 'full_name' => (isset($_POST['first_name']) && $_POST['first_name'] !== '')
204 && (isset($_POST['last_name']) && $_POST['last_name'] !== '')
205 ? $_POST['first_name'].' '.$_POST['last_name']
206 : '',
207 'last_name' => $_POST['last_name'] ?? '',
208 ],
209 'phone' => [
210 'area_code' => $_POST['phone_area_code'] ?? '',
211 'country_code' => $_POST['phone_country_code'] ?? '',
212 'subscriber_number' => $_POST['phone_subscriber_number'] ?? '',
213 ],
214 ];
215 $this->pagetit .= '顧客様の新規作成';
216 $this->tmpl->assign('pagetit', $this->pagetit);
217
218 $err = [];
219
220 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
221
222 if (!empty($_POST)) {
223 if (NULL === $_POST['last_name'] || $_POST['last_name'] === ''
224 || NULL === $_POST['first_name'] || $_POST['first_name'] === '') $err[] = '姓名をご入力下さい。';
225 if (NULL === $_POST['phone_country_code'] || $_POST['phone_country_code'] === ''
226 || NULL === $_POST['phone_area_code'] || $_POST['phone_area_code'] === ''
227 || NULL === $_POST['phone_subscriber_number'] || $_POST['phone_subscriber_number'] === '') $err[] = '電話番号をご入力下さい。';
228 if (NULL === $_POST['email'] || $_POST['email'] === '') $err[] = 'メールアドレスをご入力下さい。';
229 if (NULL === $_POST['address_zipcode'] || $_POST['address_zipcode'] === '') $err[] = '郵便番号をご入力下さい。';
230 if (NULL === $_POST['address_state'] || $_POST['address_state'] === '') $err[] = '都道府県をご入力下さい。';
231 if (NULL === $_POST['address_city'] || $_POST['address_city'] === '') $err[] = '市区町村をご入力下さい。';
232 if (NULL === $_POST['address_street'] || $_POST['address_street'] === '') $err[] = '町名をご入力下さい。';
233 if (NULL === $_POST['address_number'] || $_POST['address_number'] === '') $err[] = '番地をご入力下さい。';
234 if (!empty($err)) goto render;
235
236 if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $err[] = 'メールアドレスは不正です。';
237 if (!str_starts_with($_POST['phone_country_code'], '+')) $err[] = '電話番号の国コードは「+」ご始まり下さい。';
238 if ($_POST['fax_country_code'] !== '' && !str_starts_with($_POST['fax_country_code'], '+')) $err[] = 'FAXの国コードは「+」ご始まり下さい。';
239 if ((!filter_var($_POST['phone_area_code'], FILTER_VALIDATE_INT)) || (!filter_var($_POST['phone_subscriber_number'], FILTER_VALIDATE_INT))) $err[] = '電話番号は数字をご入力下さい。';
240 if (($_POST['fax_area_code'] !== '' || $_POST['fax_subscriber_number'] !== '') && (!filter_var($_POST['fax_area_code'], FILTER_VALIDATE_INT) || !filter_var($_POST['fax_subscriber_number'], FILTER_VALIDATE_INT))) $err[] = 'FAXは数字をご入力下さい。';
241 if (!empty($err)) goto render;
242
243 $data = $this->op->createCustomer($payload, true);
244 header("Location: /openprovider/getcustomer/{$data->data['data']['handle']}");
245 exit();
246 }
247
248 render:
249 $this->tmpl->assign('err', $err);
250 $this->tmpl->assign('data', $payload);
251 $this->tmpl->render('createcustomer');
252
253 exit();
254
255 noaccess:
256 $this->tmpl->render('nopermission');
257 } catch (\Exception $e) {
258 throw new \Exception($e->getMessage());
259 }
260 }
261
262 public function opGetCustomer(array $params): void {
263 try {
264 $handle = '';
265 if (isset($params['handle'])) $handle = $params['handle'];
266 if (!$this->isValidHandle($handle)) {
267 header('Location: /openprovider/listcustomers');
268 exit();
269 }
270 $this->pagetit .= "顧客様「{$handle}」の表示";
271 $this->tmpl->assign('pagetit', $this->pagetit);
272
273 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
274 $data = $this->op->getCustomer($handle, true);
275 $this->tmpl->assign('data', $data->data);
276 $this->tmpl->render('getcustomer');
277 exit();
278
279 noaccess:
280 $this->tmpl->render('nopermission');
281 } catch (\Exception $e) {
282 throw new \Exception($e->getMessage());
283 }
284 }
285
286 public function opDeleteCustomer(array $params): void {
287 try {
288 $handle = '';
289 if (isset($params['handle'])) $handle = $params['handle'];
290 if (!$this->isValidHandle($handle)) {
291 header('Location: /openprovider/listcustomers');
292 exit();
293 }
294 assert_not_null($handle);
295 $this->pagetit .= "顧客様「{$handle}」の表示";
296 $this->tmpl->assign('pagetit', $this->pagetit);
297
298 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
299 if (empty($_POST)) goto render_confirm;
300
301 if (isset($_POST['delete_reject'])) {
302 header("Location: /openprovider/getcustomer/{$handle}");
303 exit();
304 } else if (isset($_POST['delete_confirm'])) {
305 $this->op->deleteCustomer($handle);
306 }
307
308 header('Location: /openprovider/listcustomers');
309 exit();
310
311 render_confirm:
312 $this->tmpl->assign('handle', $handle);
313 $this->tmpl->render('deletecustomer');
314 exit();
315
316 noaccess:
317 $this->tmpl->render('nopermission');
318 } catch (\Exception $e) {
319 throw new \Exception($e->getMessage());
320 }
321 }
322
323 public function opListTlds(array $params): void {
324 try {
325 $this->pagetit .= 'TLD一覧';
326 $this->tmpl->assign('pagetit', $this->pagetit);
327
328 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
329 $data = $this->op->listTlds();
330 $this->tmpl->assign('data', $data->data);
331 $this->tmpl->render('listtlds');
332 exit();
333
334 noaccess:
335 $this->tmpl->render('nopermission');
336 } catch (\Exception $e) {
337 throw new \Exception($e->getMessage());
338 }
339 }
340
341 public function opGetTld(array $params): void {
342 try {
343 $tld = '';
344 if (isset($params['tld'])) $tld = $params['tld'];
345 if (!preg_match('/^[A-Za-z0-9-]{1,63}$/', $tld)) {
346 header('Location: /');
347 exit();
348 }
349 $this->pagetit .= "{$tld}の表示";
350 $this->tmpl->assign('pagetit', $this->pagetit);
351
352 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
353 $data = $this->op->getTld($tld);
354 $this->tmpl->assign('data', $data->data);
355 $this->tmpl->render('gettld');
356 exit();
357
358 noaccess:
359 $this->tmpl->render('nopermission');
360 } catch (\Exception $e) {
361 throw new \Exception($e->getMessage());
362 }
363 }
364
365 public function opListDomains(array $params): void {
366 try {
367 $this->pagetit .= 'ドメイン確認';
368 $this->tmpl->assign('pagetit', $this->pagetit);
369
370 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
371 $data = $this->op->listDomains($domains, true);
372 $this->tmpl->assign('data', $data->data);
373 $this->tmpl->render('checkdomain');
374 exit();
375
376 noaccess:
377 $this->tmpl->render('nopermission');
378 } catch (\Exception $e) {
379 throw new \Exception($e->getMessage());
380 }
381 }
382
383 public function opCheckDomainAvailable(array $params): void {
384 try {
385 $domains = isset($_GET['domains']) ? explode("\n", $_GET['domains']) : [];
386 $saved = '';
387 $this->pagetit .= 'ドメイン確認';
388 $this->tmpl->assign('pagetit', $this->pagetit);
389
390 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
391 if (!empty($domains)) {
392 $data = $this->op->checkDomainAvailable($domains, true);
393 $this->tmpl->assign('data', $data->data);
394 $saved = $_GET['domains'];
395 }
396
397 $this->tmpl->assign('saved', $saved);
398 $this->tmpl->render('checkdomain');
399 exit();
400
401 noaccess:
402 $this->tmpl->render('nopermission');
403 } catch (\Exception $e) {
404 throw new \Exception($e->getMessage());
405 }
406 }
407
408 public function opGetDomainPrices(array $params): void {
409 try {
410 $domainname = $_GET['domain_name'] ?? '';
411 $domainext = $_GET['domain_extension'] ?? '';
412 $this->pagetit .= 'ドメイン値段一覧';
413 $this->tmpl->assign('pagetit', $this->pagetit);
414
415 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
416 $data = $this->op->getDomainPrices(['domain.name' => $domainname, 'domain.extension' => $domainext]);
417 $this->tmpl->assign('data', $data->data);
418 $this->tmpl->render('getdomainprices');
419 exit();
420
421 noaccess:
422 $this->tmpl->render('nopermission');
423 } catch (\Exception $e) {
424 throw new \Exception($e->getMessage());
425 }
426 }
427
428 public function opListDns(array $params): void {
429 try {
430 $this->pagetit .= 'DNS一覧';
431 $this->tmpl->assign('pagetit', $this->pagetit);
432
433 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
434 $data = $this->op->listDnsZones();
435 $this->tmpl->assign('data', $data->data);
436 $this->tmpl->render('listdnszones');
437 exit();
438
439 noaccess:
440 $this->tmpl->render('nopermission');
441 } catch (\Exception $e) {
442 throw new \Exception($e->getMessage());
443 }
444 }
445
446 public function opGetDns(array $params): void {
447 try {
448 $domain = '';
449 if (isset($params['domain'])) $domain = $params['domain'];
450 if (!preg_match('/^(?=.{1,253}$)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9-]{2,63}$/', $domain)) {
451 header('Location: /');
452 exit();
453 }
454 $this->pagetit .= "{$domain}のDNS管理";
455 $this->tmpl->assign('pagetit', $this->pagetit);
456
457 if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess;
458 $data = ['rec' => $this->op->listZoneRecords($domain), 'zone' => $this->op->getDnsZone($domain)];
459 $this->tmpl->assign('data', $data->data);
460 $this->tmpl->render('getdns');
461 exit();
462
463 noaccess:
464 $this->tmpl->render('nopermission');
465 } catch (\Exception $e) {
466 throw new \Exception($e->getMessage());
467 }
468 }
469
470 //////////
471
472 /**
473 * 顧客ハンドルの形式を確認する(例:AB123456-XX)
474 * URLの値はAPIのパスとキャッシュのファイル名にそのまま入るので、先に確認する
475 *
476 * @param string $handle 顧客ハンドル
477 * @return bool 正しい形式ならtrue
478 */
479 private function isValidHandle(string $handle): bool {
480 return preg_match('/^[A-Za-z0-9-]{1,64}$/', $handle) === 1;
481 }
482}