SeminarSignUpController.php 3.6KB

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