SeminarSignUpController.php 11KB

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