2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Passport;
|
|
|
|
|
|
|
|
use App\Http\Requests\Passport\CommSendEmailVerify;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2020-01-12 00:49:13 +08:00
|
|
|
use App\Utils\Helper;
|
2020-01-12 01:27:36 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2020-03-18 18:12:19 +08:00
|
|
|
use App\Jobs\SendEmailJob;
|
2020-01-29 17:08:09 +08:00
|
|
|
use App\Models\InviteCode;
|
2020-02-09 18:01:06 +08:00
|
|
|
use App\Utils\Dict;
|
2020-03-13 14:32:36 +08:00
|
|
|
use App\Utils\CacheKey;
|
2020-10-09 23:27:36 +08:00
|
|
|
use ReCaptcha\ReCaptcha;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class CommController extends Controller
|
|
|
|
{
|
2021-07-19 17:44:40 +08:00
|
|
|
// TODO: remove on 1.5.5
|
2020-01-11 13:36:52 +08:00
|
|
|
public function config()
|
|
|
|
{
|
2019-11-25 18:14:49 +08:00
|
|
|
return response([
|
2019-11-25 18:22:38 +08:00
|
|
|
'data' => [
|
|
|
|
'isEmailVerify' => (int)config('v2board.email_verify', 0) ? 1 : 0,
|
2020-02-09 18:02:13 +08:00
|
|
|
'isInviteForce' => (int)config('v2board.invite_force', 0) ? 1 : 0,
|
2020-02-09 18:09:48 +08:00
|
|
|
'emailWhitelistSuffix' => (int)config('v2board.email_whitelist_enable', 0)
|
2020-02-09 18:24:24 +08:00
|
|
|
? $this->getEmailSuffix()
|
2020-10-09 23:27:36 +08:00
|
|
|
: 0,
|
|
|
|
'isRecaptcha' => (int)config('v2board.recaptcha_enable', 0) ? 1 : 0,
|
2020-10-25 03:56:43 +08:00
|
|
|
'recaptchaSiteKey' => config('v2board.recaptcha_site_key'),
|
2020-11-11 17:15:07 +08:00
|
|
|
'appDescription' => config('v2board.app_description'),
|
|
|
|
'appUrl' => config('v2board.app_url')
|
2019-11-25 18:22:38 +08:00
|
|
|
]
|
2019-11-25 18:14:49 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
private function isEmailVerify()
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
2019-11-18 19:52:30 +08:00
|
|
|
'data' => (int)config('v2board.email_verify', 0) ? 1 : 0
|
2019-10-29 15:33:36 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function sendEmailVerify(CommSendEmailVerify $request)
|
|
|
|
{
|
2020-10-09 23:27:36 +08:00
|
|
|
if ((int)config('v2board.recaptcha_enable', 0)) {
|
|
|
|
$recaptcha = new ReCaptcha(config('v2board.recaptcha_key'));
|
|
|
|
$recaptchaResp = $recaptcha->verify($request->input('recaptcha_data'));
|
|
|
|
if (!$recaptchaResp->isSuccess()) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('Invalid code is incorrect'));
|
2020-10-09 23:27:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
$email = $request->input('email');
|
2020-03-13 14:32:36 +08:00
|
|
|
if (Cache::get(CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP', $email))) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('Email verification code has been sent, please request again later'));
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2020-03-13 14:32:36 +08:00
|
|
|
$code = rand(100000, 999999);
|
2021-06-12 00:56:39 +08:00
|
|
|
$subject = config('v2board.app_name', 'V2Board') . __('Email verification code');
|
2020-01-20 13:57:30 +08:00
|
|
|
|
2020-03-18 18:12:19 +08:00
|
|
|
SendEmailJob::dispatch([
|
2020-01-20 14:04:03 +08:00
|
|
|
'email' => $email,
|
2020-01-20 13:57:30 +08:00
|
|
|
'subject' => $subject,
|
2020-03-18 18:12:19 +08:00
|
|
|
'template_name' => 'verify',
|
2020-01-20 13:57:30 +08:00
|
|
|
'template_value' => [
|
2019-12-30 21:05:48 +08:00
|
|
|
'name' => config('v2board.app_name', 'V2Board'),
|
2020-01-20 13:57:30 +08:00
|
|
|
'code' => $code,
|
2019-12-30 21:05:48 +08:00
|
|
|
'url' => config('v2board.app_url')
|
2020-01-20 13:57:30 +08:00
|
|
|
]
|
2020-03-18 18:16:33 +08:00
|
|
|
]);
|
2019-12-26 23:47:01 +08:00
|
|
|
|
2020-03-13 14:32:36 +08:00
|
|
|
Cache::put(CacheKey::get('EMAIL_VERIFY_CODE', $email), $code, 300);
|
|
|
|
Cache::put(CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP', $email), time(), 60);
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-01-29 17:08:09 +08:00
|
|
|
|
|
|
|
public function pv(Request $request)
|
|
|
|
{
|
|
|
|
$inviteCode = InviteCode::where('code', $request->input('invite_code'))->first();
|
|
|
|
if ($inviteCode) {
|
|
|
|
$inviteCode->pv = $inviteCode->pv + 1;
|
|
|
|
$inviteCode->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-02-09 18:24:24 +08:00
|
|
|
|
|
|
|
private function getEmailSuffix()
|
|
|
|
{
|
|
|
|
$suffix = config('v2board.email_whitelist_suffix', Dict::EMAIL_WHITELIST_SUFFIX_DEFAULT);
|
|
|
|
if (!is_array($suffix)) {
|
|
|
|
return preg_split('/,/', $suffix);
|
|
|
|
}
|
|
|
|
return $suffix;
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|