SeminarSignUpController.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\ApiController;
  4. use App\Http\Services\Api\SeminarSignUpService;
  5. use App\Http\Requests\Api\SeminarSignUp\StoreRequest;
  6. use Log;
  7. class SeminarSignUpController extends ApiController
  8. {
  9. private $seminarSignUpSv;
  10. public function __construct()
  11. {
  12. $this->seminarSignUpSv = new SeminarSignUpService();
  13. }
  14. // save data to db
  15. public function insertData(StoreRequest $request)
  16. {
  17. $trackNo = $request->input('trackNo', '');
  18. $lang = substr($trackNo, 0, 2);
  19. $firstName_orig = $request->input('firstName', '');
  20. $firstName = $this->safeEncrypt($firstName_orig, 'arm');
  21. $lastName = $this->safeEncrypt($request->input('lastName', ''), 'arm');
  22. $companyName = $this->safeEncrypt($request->input('companyName', ''), 'arm');
  23. $companyEmail_orig = $request->input('companyEmail', '');
  24. $companyEmail = $this->safeEncrypt($companyEmail_orig, 'arm');
  25. $backupEmail = $request->input('backupEmail', '');
  26. if (!is_null($backupEmail)) {
  27. $backupEmail_orig = $request->input('backupEmail', '');
  28. $backupEmail = $this->safeEncrypt($backupEmail_orig, 'arm');
  29. } else {
  30. $backupEmail_orig = '';
  31. }
  32. $phoneNumber = $this->safeEncrypt($request->input('phoneNumber', ''), 'arm');
  33. $country = $request->input('country', '');
  34. $registeredSession = $request->input('registeredSession', '');
  35. $lunchOptions = $request->input('lunchOptions', '');
  36. $typeOfIndustry = $request->input('typeOfIndustry', '');
  37. $typeOfJob = $request->input('typeOfJob', '');
  38. $jobTitle = $request->input('jobTitle', '');
  39. $trackOfInterest = $request->input('trackOfInterest', '');
  40. $areaOfInterest = $request->input('areaOfInterest', '');
  41. $howToKnowAboutTheEvent = $request->input('howToKnowAboutTheEvent', '');
  42. $consentAcceptEmail = $request->input('consentAcceptEmail', '');
  43. $consentPrivacyPolicy = $request->input('consentPrivacyPolicy', '');
  44. $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackNo);
  45. if ($overOrNot) {
  46. $this->seminarSignUpSv->insertData(
  47. $firstName,
  48. $lastName,
  49. $companyName,
  50. $companyEmail,
  51. $backupEmail,
  52. $phoneNumber,
  53. $country,
  54. $trackNo,
  55. $registeredSession,
  56. $lunchOptions,
  57. $typeOfIndustry,
  58. $typeOfJob,
  59. $jobTitle,
  60. $trackOfInterest,
  61. $areaOfInterest,
  62. $howToKnowAboutTheEvent,
  63. $consentAcceptEmail,
  64. $consentPrivacyPolicy,
  65. );
  66. $res = '報名成功';
  67. if ($lang=='TP'||$lang=='HS') {
  68. $this->mailToUser_TW($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  69. } elseif ($lang=='JP') {
  70. $this->mailToUser_JP($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  71. } elseif ($lang=='KR') {
  72. $this->mailToUser_KR($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  73. } else {
  74. $this->mailToUser_EN($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  75. }
  76. } else {
  77. $res = '已達報名上限';
  78. }
  79. $data = [
  80. 'res' => $res,
  81. ];
  82. return $this->apiResponse($data);
  83. }
  84. public function getData()
  85. {
  86. $list = $this->seminarSignUpSv->getData();
  87. $data = [
  88. 'list' => $list
  89. ];
  90. return $this->apiResponse($data);
  91. }
  92. /**
  93. * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
  94. */
  95. public function safeEncrypt(string $message, string $skey): string
  96. {
  97. // if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
  98. // throw new RangeException('Key is not the correct size (must be 32 bytes).');
  99. // }
  100. // $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
  101. // $cipher = base64_encode(
  102. // $nonce .
  103. // sodium_crypto_secretbox(
  104. // $message,
  105. // $nonce,
  106. // $key
  107. // )
  108. // );
  109. // sodium_memzero($message);
  110. // sodium_memzero($key);
  111. // return $cipher;
  112. $strArr = str_split(base64_encode($message));
  113. $strCount = count($strArr);
  114. foreach (str_split($skey) as $key => $value)
  115. $key < $strCount && $strArr[$key].=$value;
  116. return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
  117. }
  118. /**
  119. * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
  120. */
  121. public function safeDecrypt(string $encrypted, string $skey): string
  122. {
  123. // $decoded = base64_decode($encrypted);
  124. // $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
  125. // $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
  126. // $plain = sodium_crypto_secretbox_open(
  127. // $ciphertext,
  128. // $nonce,
  129. // $key
  130. // );
  131. // if (!is_string($plain)) {
  132. // throw new Exception('Invalid MAC');
  133. // }
  134. // sodium_memzero($ciphertext);
  135. // sodium_memzero($key);
  136. // return $plain;
  137. $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
  138. $strCount = count($strArr);
  139. foreach (str_split($skey) as $key => $value)
  140. $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  141. return base64_decode(join('', $strArr));
  142. }
  143. public function mailToUser_TW(string $firstName='', string $companyEmail='', string $backupEmail='')
  144. {
  145. $data = array('name'=>$firstName);
  146. if ($backupEmail) {
  147. \Mail::send(['text'=>'mailTW'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  148. $message->to($companyEmail, $firstName)->subject('報名成功'); // Registration is complete
  149. $message->cc($backupEmail, $firstName)->subject('報名成功'); // Registration is complete
  150. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  151. });
  152. } else {
  153. \Mail::send(['text'=>'mailTW'], $data, function($message) use ($companyEmail, $firstName) {
  154. $message->to($companyEmail, $firstName)->subject('報名成功'); // Registration is complete
  155. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  156. });
  157. }
  158. }
  159. public function mailToUser_JP(string $firstName='', string $companyEmail='', string $backupEmail='')
  160. {
  161. $data = array('name'=>$firstName);
  162. if ($backupEmail) {
  163. \Mail::send(['text'=>'mailJP'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  164. $message->to($companyEmail, $firstName)->subject('登録完了');
  165. $message->cc($backupEmail, $firstName)->subject('登録完了');
  166. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  167. });
  168. } else {
  169. \Mail::send(['text'=>'mailJP'], $data, function($message) use ($companyEmail, $firstName) {
  170. $message->to($companyEmail, $firstName)->subject('登録完了');
  171. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  172. });
  173. }
  174. }
  175. public function mailToUser_KR(string $firstName='', string $companyEmail='', string $backupEmail='')
  176. {
  177. $data = array('name'=>$firstName);
  178. if ($backupEmail) {
  179. \Mail::send(['text'=>'mailKR'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  180. $message->to($companyEmail, $firstName)->subject('등록이 완료되었습니다');
  181. $message->cc($backupEmail, $firstName)->subject('등록이 완료되었습니다');
  182. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  183. });
  184. } else {
  185. \Mail::send(['text'=>'mailKR'], $data, function($message) use ($companyEmail, $firstName) {
  186. $message->to($companyEmail, $firstName)->subject('등록이 완료되었습니다');
  187. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  188. });
  189. }
  190. }
  191. public function mailToUser_EN(string $firstName='', string $companyEmail='', string $backupEmail='')
  192. {
  193. $data = array('name'=>$firstName);
  194. if ($backupEmail) {
  195. \Mail::send(['text'=>'mailEN'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  196. $message->to($companyEmail, $firstName)->subject('Registration is complete');
  197. $message->cc($backupEmail, $firstName)->subject('Registration is complete');
  198. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  199. });
  200. } else {
  201. \Mail::send(['text'=>'mailEN'], $data, function($message) use ($companyEmail, $firstName) {
  202. $message->to($companyEmail, $firstName)->subject('Registration is complete');
  203. $message->from('no-reply@erapr.com.tw','Arm Tech Symposia 2');
  204. });
  205. }
  206. }
  207. }