SeminarSignUpController.php 3.6KB

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