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