SeminarSignUpController.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $name = $this->safeEncrypt($request->input('name', 'name'), 'arm');
  19. $email = $this->safeEncrypt($request->input('email', 'email'), 'arm');
  20. $mobile = $this->safeEncrypt($request->input('mobile', 'mobile'), 'arm');
  21. $trackNo = $request->input('trackNo', '');
  22. $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackNo);
  23. \Log::info($overOrNot);
  24. if ($overOrNot) {
  25. $this->seminarSignUpSv->insertData($name, $email, $mobile, $trackNo);
  26. $res = '報名成功';
  27. } else {
  28. $res = '已達報名上限';
  29. }
  30. $data = [
  31. 'res' => $res,
  32. ];
  33. \Log::info(print_r($res,true));
  34. return $this->apiResponse($data);
  35. }
  36. public function getData()
  37. {
  38. $list = $this->seminarSignUpSv->getData();
  39. $data = [
  40. 'list' => $list
  41. ];
  42. return $this->apiResponse($data);
  43. }
  44. /**
  45. * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
  46. */
  47. public function safeEncrypt(string $message, string $skey): string
  48. {
  49. // if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
  50. // throw new RangeException('Key is not the correct size (must be 32 bytes).');
  51. // }
  52. // $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
  53. // $cipher = base64_encode(
  54. // $nonce .
  55. // sodium_crypto_secretbox(
  56. // $message,
  57. // $nonce,
  58. // $key
  59. // )
  60. // );
  61. // sodium_memzero($message);
  62. // sodium_memzero($key);
  63. // return $cipher;
  64. $strArr = str_split(base64_encode($message));
  65. $strCount = count($strArr);
  66. foreach (str_split($skey) as $key => $value)
  67. $key < $strCount && $strArr[$key].=$value;
  68. return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
  69. }
  70. /**
  71. * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
  72. */
  73. public function safeDecrypt(string $encrypted, string $skey): string
  74. {
  75. // $decoded = base64_decode($encrypted);
  76. // $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
  77. // $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
  78. // $plain = sodium_crypto_secretbox_open(
  79. // $ciphertext,
  80. // $nonce,
  81. // $key
  82. // );
  83. // if (!is_string($plain)) {
  84. // throw new Exception('Invalid MAC');
  85. // }
  86. // sodium_memzero($ciphertext);
  87. // sodium_memzero($key);
  88. // return $plain;
  89. $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
  90. $strCount = count($strArr);
  91. foreach (str_split($skey) as $key => $value)
  92. $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  93. return base64_decode(join('', $strArr));
  94. }
  95. }