|
@@ -0,0 +1,114 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App\Http\Controllers\Api;
|
|
4
|
+
|
|
5
|
+use App\Http\Controllers\Api\ApiController;
|
|
6
|
+use App\Http\Services\Web\SeminarSignUpService;
|
|
7
|
+use App\Http\Requests\Api\SeminarSignUp\StoreRequest;
|
|
8
|
+
|
|
9
|
+use Log;
|
|
10
|
+
|
|
11
|
+class SeminarSignUpController extends ApiController
|
|
12
|
+{
|
|
13
|
+
|
|
14
|
+ private $seminarSignUpSv;
|
|
15
|
+
|
|
16
|
+ public function __construct()
|
|
17
|
+ {
|
|
18
|
+ $this->seminarSignUpSv = new SeminarSignUpService();
|
|
19
|
+
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ // save data to db
|
|
23
|
+ public function insertData(StoreRequest $request)
|
|
24
|
+ {
|
|
25
|
+ $name = $this->safeEncrypt($request->input('name', 'name'), 'arm');
|
|
26
|
+ $email = $this->safeEncrypt($request->input('email', 'email'), 'arm');
|
|
27
|
+ $mobile = $this->safeEncrypt($request->input('mobile', 'mobile'), 'arm');
|
|
28
|
+ $trackId = $request->input('trackId', 'T0052');
|
|
29
|
+
|
|
30
|
+ $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackId);
|
|
31
|
+ if ($overOrNot) {
|
|
32
|
+
|
|
33
|
+ $this->seminarSignUpSv->insertData($name, $email, $mobile, $trackId);
|
|
34
|
+ $res = '報名成功';
|
|
35
|
+ } else {
|
|
36
|
+
|
|
37
|
+ $res = '已達報名上限';
|
|
38
|
+ }
|
|
39
|
+ $data = [
|
|
40
|
+ 'res' => $res,
|
|
41
|
+ ];
|
|
42
|
+
|
|
43
|
+ return $this->apiResponse($data);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public function getData()
|
|
47
|
+ {
|
|
48
|
+
|
|
49
|
+ $Data = $this->seminarSignUpSv->getData();
|
|
50
|
+
|
|
51
|
+ $data = [
|
|
52
|
+ 'list' => $Data
|
|
53
|
+ ];
|
|
54
|
+
|
|
55
|
+ return $this->apiResponse($data);
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ /**
|
|
59
|
+ * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
|
|
60
|
+ */
|
|
61
|
+ public function safeEncrypt(string $message, string $skey): string
|
|
62
|
+ {
|
|
63
|
+ // if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
|
|
64
|
+ // throw new RangeException('Key is not the correct size (must be 32 bytes).');
|
|
65
|
+ // }
|
|
66
|
+ // $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
|
67
|
+
|
|
68
|
+ // $cipher = base64_encode(
|
|
69
|
+ // $nonce .
|
|
70
|
+ // sodium_crypto_secretbox(
|
|
71
|
+ // $message,
|
|
72
|
+ // $nonce,
|
|
73
|
+ // $key
|
|
74
|
+ // )
|
|
75
|
+ // );
|
|
76
|
+ // sodium_memzero($message);
|
|
77
|
+ // sodium_memzero($key);
|
|
78
|
+ // return $cipher;
|
|
79
|
+ $strArr = str_split(base64_encode($message));
|
|
80
|
+ $strCount = count($strArr);
|
|
81
|
+ foreach (str_split($skey) as $key => $value)
|
|
82
|
+ $key < $strCount && $strArr[$key].=$value;
|
|
83
|
+ return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ /**
|
|
87
|
+ * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
|
|
88
|
+ */
|
|
89
|
+ public function safeDecrypt(string $encrypted, string $skey): string
|
|
90
|
+ {
|
|
91
|
+ // $decoded = base64_decode($encrypted);
|
|
92
|
+ // $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
|
|
93
|
+ // $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
|
|
94
|
+
|
|
95
|
+ // $plain = sodium_crypto_secretbox_open(
|
|
96
|
+ // $ciphertext,
|
|
97
|
+ // $nonce,
|
|
98
|
+ // $key
|
|
99
|
+ // );
|
|
100
|
+ // if (!is_string($plain)) {
|
|
101
|
+ // throw new Exception('Invalid MAC');
|
|
102
|
+ // }
|
|
103
|
+ // sodium_memzero($ciphertext);
|
|
104
|
+ // sodium_memzero($key);
|
|
105
|
+
|
|
106
|
+ // return $plain;
|
|
107
|
+ $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
|
|
108
|
+ $strCount = count($strArr);
|
|
109
|
+ foreach (str_split($skey) as $key => $value)
|
|
110
|
+ $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
|
|
111
|
+ return base64_decode(join('', $strArr));
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+}
|