SeminarSignUpController.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. \Log::info(print_r($request->all(),true));
  18. $trackNo = $request->input('trackNo', '');
  19. $firstName = $this->safeEncrypt($request->input('firstName', ''), 'arm');
  20. $lastName = $this->safeEncrypt($request->input('lastName', ''), 'arm');
  21. $companyName = $this->safeEncrypt($request->input('companyName', ''), 'arm');
  22. // $companyEmail = $this->safeEncrypt($request->input('companyEmail', ''), 'arm');
  23. $companyEmail = $request->input('companyEmail', '');
  24. $backupEmail = $request->input('backupEmail', '');
  25. if (!is_null($backupEmail)) {
  26. $backupEmail = $this->safeEncrypt($backupEmail, 'arm');
  27. }
  28. $phoneNumber = $this->safeEncrypt($request->input('phoneNumber', ''), 'arm');
  29. $country = $request->input('country', '');
  30. $registeredSession = $request->input('registeredSession', '');
  31. $lunchOptions = $request->input('lunchOptions', '');
  32. $typeOfIndustry = $request->input('typeOfIndustry', '');
  33. $typeOfJob = $request->input('typeOfJob', '');
  34. $jobTitle = $request->input('jobTitle', '');
  35. $trackOfInterest = $request->input('trackOfInterest', '');
  36. $areaOfInterest = $request->input('areaOfInterest', '');
  37. $howToKnowAboutTheEvent = $request->input('howToKnowAboutTheEvent', '');
  38. $consentAcceptEmail = $request->input('consentAcceptEmail', '');
  39. $consentPrivacyPolicy = $request->input('consentPrivacyPolicy', '');
  40. $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackNo);
  41. \Log::info($overOrNot);
  42. if ($overOrNot) {
  43. $this->seminarSignUpSv->insertData(
  44. $firstName,
  45. $lastName,
  46. $companyName,
  47. $companyEmail,
  48. $backupEmail,
  49. $phoneNumber,
  50. $country,
  51. $trackNo,
  52. $registeredSession,
  53. $lunchOptions,
  54. $typeOfIndustry,
  55. $typeOfJob,
  56. $jobTitle,
  57. $trackOfInterest,
  58. $areaOfInterest,
  59. $howToKnowAboutTheEvent,
  60. $consentAcceptEmail,
  61. $consentPrivacyPolicy,
  62. );
  63. $res = '報名成功';
  64. } else {
  65. $res = '已達報名上限';
  66. }
  67. $data = [
  68. 'res' => $res,
  69. ];
  70. \Log::info(print_r($res,true));
  71. return $this->apiResponse($data);
  72. }
  73. public function getData()
  74. {
  75. $list = $this->seminarSignUpSv->getData();
  76. $data = [
  77. 'list' => $list
  78. ];
  79. return $this->apiResponse($data);
  80. }
  81. /**
  82. * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
  83. */
  84. public function safeEncrypt(string $message, string $skey): string
  85. {
  86. // if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
  87. // throw new RangeException('Key is not the correct size (must be 32 bytes).');
  88. // }
  89. // $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
  90. // $cipher = base64_encode(
  91. // $nonce .
  92. // sodium_crypto_secretbox(
  93. // $message,
  94. // $nonce,
  95. // $key
  96. // )
  97. // );
  98. // sodium_memzero($message);
  99. // sodium_memzero($key);
  100. // return $cipher;
  101. $strArr = str_split(base64_encode($message));
  102. $strCount = count($strArr);
  103. foreach (str_split($skey) as $key => $value)
  104. $key < $strCount && $strArr[$key].=$value;
  105. return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
  106. }
  107. /**
  108. * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
  109. */
  110. public function safeDecrypt(string $encrypted, string $skey): string
  111. {
  112. // $decoded = base64_decode($encrypted);
  113. // $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
  114. // $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
  115. // $plain = sodium_crypto_secretbox_open(
  116. // $ciphertext,
  117. // $nonce,
  118. // $key
  119. // );
  120. // if (!is_string($plain)) {
  121. // throw new Exception('Invalid MAC');
  122. // }
  123. // sodium_memzero($ciphertext);
  124. // sodium_memzero($key);
  125. // return $plain;
  126. $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
  127. $strCount = count($strArr);
  128. foreach (str_split($skey) as $key => $value)
  129. $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  130. return base64_decode(join('', $strArr));
  131. }
  132. }