SeminarSignUpController.php 5.3KB

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