SeminarSignUpController.php 8.0KB

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