CommController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Http\Controllers\Passport;
  3. use App\Http\Requests\Passport\CommSendEmailVerify;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Http\Exceptions\HttpResponseException;
  7. use Illuminate\Support\Facades\Mail;
  8. use App\Utils\Helper;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Jobs\SendEmailJob;
  11. use App\Models\InviteCode;
  12. use App\Utils\Dict;
  13. use App\Utils\CacheKey;
  14. use ReCaptcha\ReCaptcha;
  15. class CommController extends Controller
  16. {
  17. // TODO: remove on 1.5.5
  18. public function config()
  19. {
  20. return response([
  21. 'data' => [
  22. 'isEmailVerify' => (int)config('v2board.email_verify', 0) ? 1 : 0,
  23. 'isInviteForce' => (int)config('v2board.invite_force', 0) ? 1 : 0,
  24. 'emailWhitelistSuffix' => (int)config('v2board.email_whitelist_enable', 0)
  25. ? $this->getEmailSuffix()
  26. : 0,
  27. 'isRecaptcha' => (int)config('v2board.recaptcha_enable', 0) ? 1 : 0,
  28. 'recaptchaSiteKey' => config('v2board.recaptcha_site_key'),
  29. 'appDescription' => config('v2board.app_description'),
  30. 'appUrl' => config('v2board.app_url')
  31. ]
  32. ]);
  33. }
  34. private function isEmailVerify()
  35. {
  36. return response([
  37. 'data' => (int)config('v2board.email_verify', 0) ? 1 : 0
  38. ]);
  39. }
  40. public function sendEmailVerify(CommSendEmailVerify $request)
  41. {
  42. if ((int)config('v2board.recaptcha_enable', 0)) {
  43. $recaptcha = new ReCaptcha(config('v2board.recaptcha_key'));
  44. $recaptchaResp = $recaptcha->verify($request->input('recaptcha_data'));
  45. if (!$recaptchaResp->isSuccess()) {
  46. abort(500, __('Invalid code is incorrect'));
  47. }
  48. }
  49. $email = $request->input('email');
  50. if (Cache::get(CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP', $email))) {
  51. abort(500, __('Email verification code has been sent, please request again later'));
  52. }
  53. $code = rand(100000, 999999);
  54. $subject = config('v2board.app_name', 'V2Board') . __('Email verification code');
  55. SendEmailJob::dispatch([
  56. 'email' => $email,
  57. 'subject' => $subject,
  58. 'template_name' => 'verify',
  59. 'template_value' => [
  60. 'name' => config('v2board.app_name', 'V2Board'),
  61. 'code' => $code,
  62. 'url' => config('v2board.app_url')
  63. ]
  64. ]);
  65. Cache::put(CacheKey::get('EMAIL_VERIFY_CODE', $email), $code, 300);
  66. Cache::put(CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP', $email), time(), 60);
  67. return response([
  68. 'data' => true
  69. ]);
  70. }
  71. public function pv(Request $request)
  72. {
  73. $inviteCode = InviteCode::where('code', $request->input('invite_code'))->first();
  74. if ($inviteCode) {
  75. $inviteCode->pv = $inviteCode->pv + 1;
  76. $inviteCode->save();
  77. }
  78. return response([
  79. 'data' => true
  80. ]);
  81. }
  82. private function getEmailSuffix()
  83. {
  84. $suffix = config('v2board.email_whitelist_suffix', Dict::EMAIL_WHITELIST_SUFFIX_DEFAULT);
  85. if (!is_array($suffix)) {
  86. return preg_split('/,/', $suffix);
  87. }
  88. return $suffix;
  89. }
  90. }