AuthController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Components\IPIP;
  5. use App\Components\QQWry;
  6. use App\Mail\activeUser;
  7. use App\Mail\resetPassword;
  8. use App\Mail\sendVerifyCode;
  9. use App\Models\EmailFilter;
  10. use App\Models\Invite;
  11. use App\Models\User;
  12. use App\Models\UserLoginLog;
  13. use App\Models\Verify;
  14. use App\Models\VerifyCode;
  15. use App\Services\UserService;
  16. use Auth;
  17. use Cache;
  18. use Captcha;
  19. use Cookie;
  20. use Hash;
  21. use Illuminate\Http\RedirectResponse;
  22. use Illuminate\Http\Request;
  23. use Log;
  24. use Mail;
  25. use Redirect;
  26. use Response;
  27. use Session;
  28. use Str;
  29. use Validator;
  30. /**
  31. * 认证控制器
  32. *
  33. * Class AuthController
  34. *
  35. * @package App\Http\Controllers
  36. */
  37. class AuthController extends Controller {
  38. // 登录
  39. public function login(Request $request) {
  40. if($request->isMethod('POST')){
  41. $validator = Validator::make($request->all(), [
  42. 'email' => 'required|email',
  43. 'password' => 'required'
  44. ], [
  45. 'email.required' => trans('auth.email_null'),
  46. 'password.required' => trans('auth.password_null')
  47. ]);
  48. if($validator->fails()){
  49. return Redirect::back()->withInput()->withErrors($validator->errors());
  50. }
  51. $email = $request->input('email');
  52. $password = $request->input('password');
  53. $remember = $request->input('remember');
  54. // 是否校验验证码
  55. $captcha = $this->check_captcha($request);
  56. if($captcha != false){
  57. return $captcha;
  58. }
  59. // 验证账号并创建会话
  60. if(!Auth::attempt(['email' => $email, 'password' => $password], $remember)){
  61. return Redirect::back()->withInput()->withErrors(trans('auth.login_error'));
  62. }
  63. $user = Auth::getUser();
  64. if(!$user){
  65. return Redirect::back()->withInput()->withErrors(trans('auth.login_error'));
  66. }
  67. // 校验普通用户账号状态
  68. if(!$user->is_admin){
  69. if($user->status < 0){
  70. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  71. return Redirect::back()->withInput()->withErrors(trans('auth.login_ban',
  72. ['email' => sysConfig('webmaster_email')]));
  73. }
  74. if($user->status == 0 && sysConfig('is_activate_account')){
  75. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  76. return Redirect::back()
  77. ->withInput()
  78. ->withErrors(trans('auth.active_tip').'<a href="/activeUser?email='.$email.'" target="_blank"><span style="color:#000">【'.trans('auth.active_account').'】</span></a>');
  79. }
  80. }
  81. // 写入登录日志
  82. $this->addUserLoginLog($user->id, getClientIp());
  83. // 更新登录信息
  84. Auth::getUser()->update(['last_login' => time()]);
  85. // 根据权限跳转
  86. if($user->is_admin){
  87. return Redirect::to('admin');
  88. }
  89. return Redirect::to('/');
  90. }
  91. if(Auth::check()){
  92. if(Auth::getUser()->is_admin){
  93. return Redirect::to('admin');
  94. }
  95. return Redirect::to('/');
  96. }
  97. return view('auth.login');
  98. }
  99. // 校验验证码
  100. private function check_captcha($request) {
  101. switch(sysConfig('is_captcha')){
  102. case 1: // 默认图形验证码
  103. if(!Captcha::check($request->input('captcha'))){
  104. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_error'));
  105. }
  106. break;
  107. case 2: // Geetest
  108. $validator = Validator::make($request->all(), [
  109. 'geetest_challenge' => 'required|geetest'
  110. ], [
  111. 'geetest' => trans('auth.captcha_fail')
  112. ]);
  113. if($validator->fails()){
  114. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  115. }
  116. break;
  117. case 3: // Google reCAPTCHA
  118. $validator = Validator::make($request->all(), [
  119. 'g-recaptcha-response' => 'required|NoCaptcha'
  120. ]);
  121. if($validator->fails()){
  122. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  123. }
  124. break;
  125. case 4: // hCaptcha
  126. $validator = Validator::make($request->all(), [
  127. 'h-captcha-response' => 'required|HCaptcha'
  128. ]);
  129. if($validator->fails()){
  130. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  131. }
  132. break;
  133. default: // 不启用验证码
  134. break;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * 添加用户登录日志
  140. *
  141. * @param integer $userId 用户ID
  142. * @param string $ip IP地址
  143. */
  144. private function addUserLoginLog($userId, $ip): void {
  145. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){
  146. Log::info('识别到IPv6,尝试解析:'.$ip);
  147. $ipInfo = getIPInfo($ip);
  148. }else{
  149. $ipInfo = QQWry::ip($ip); // 通过纯真IP库解析IPv4信息
  150. if(isset($ipInfo['error'])){
  151. Log::info('无法识别IPv4,尝试使用IPIP的IP库解析:'.$ip);
  152. $ipip = IPIP::ip($ip);
  153. $ipInfo = [
  154. 'country' => $ipip['country_name'],
  155. 'province' => $ipip['region_name'],
  156. 'city' => $ipip['city_name']
  157. ];
  158. }else{
  159. // 判断纯真IP库获取的国家信息是否与IPIP的IP库获取的信息一致,不一致则用IPIP的(因为纯真IP库的非大陆IP准确率较低)
  160. $ipip = IPIP::ip($ip);
  161. if($ipInfo['country'] != $ipip['country_name']){
  162. $ipInfo['country'] = $ipip['country_name'];
  163. $ipInfo['province'] = $ipip['region_name'];
  164. $ipInfo['city'] = $ipip['city_name'];
  165. }
  166. }
  167. }
  168. if(empty($ipInfo) || empty($ipInfo['country'])){
  169. Log::warning("获取IP信息异常:".$ip);
  170. }
  171. $log = new UserLoginLog();
  172. $log->user_id = $userId;
  173. $log->ip = $ip;
  174. $log->country = $ipInfo['country'] ?? '';
  175. $log->province = $ipInfo['province'] ?? '';
  176. $log->city = $ipInfo['city'] ?? '';
  177. $log->county = $ipInfo['county'] ?? '';
  178. $log->isp = $ipInfo['isp'] ?? ($ipInfo['organization'] ?? '');
  179. $log->area = $ipInfo['area'] ?? '';
  180. $log->save();
  181. }
  182. // 退出
  183. public function logout(): RedirectResponse {
  184. Auth::logout();
  185. return Redirect::to('login');
  186. }
  187. // 注册
  188. public function register(Request $request) {
  189. $cacheKey = 'register_times_'.md5(getClientIp()); // 注册限制缓存key
  190. if($request->isMethod('POST')){
  191. $validator = Validator::make($request->all(), [
  192. 'username' => 'required',
  193. 'email' => 'required|email|unique:user',
  194. 'password' => 'required|min:6',
  195. 'confirmPassword' => 'required|same:password',
  196. 'term' => 'accepted'
  197. ], [
  198. 'username.required' => trans('auth.email_null'),
  199. 'email.required' => trans('auth.email_null'),
  200. 'email.email' => trans('auth.email_legitimate'),
  201. 'email.unique' => trans('auth.email_exist'),
  202. 'password.required' => trans('auth.password_null'),
  203. 'password.min' => trans('auth.password_limit'),
  204. 'confirmPassword.required' => trans('auth.confirm_password'),
  205. 'confirmPassword.same' => trans('auth.password_same'),
  206. 'term.accepted' => trans('auth.unaccepted')
  207. ]);
  208. if($validator->fails()){
  209. return Redirect::back()->withInput()->withErrors($validator->errors());
  210. }
  211. $username = $request->input('username');
  212. $email = $request->input('email');
  213. $password = $request->input('password');
  214. $register_token = $request->input('register_token');
  215. $code = $request->input('code');
  216. $verify_code = $request->input('verify_code');
  217. $aff = (int) $request->input('aff');
  218. // 防止重复提交
  219. if($register_token !== Session::get('register_token')){
  220. return Redirect::back()->withInput()->withErrors(trans('auth.repeat_request'));
  221. }
  222. Session::forget('register_token');
  223. // 是否开启注册
  224. if(!sysConfig('is_register')){
  225. return Redirect::back()->withErrors(trans('auth.register_close'));
  226. }
  227. // 校验域名邮箱黑白名单
  228. if(sysConfig('is_email_filtering')){
  229. $result = $this->emailChecker($email, 1);
  230. if($result !== false){
  231. return $result;
  232. }
  233. }
  234. // 如果需要邀请注册
  235. if(sysConfig('is_invite_register')){
  236. // 校验邀请码合法性
  237. if($code){
  238. if(Invite::whereCode($code)->whereStatus(0)->doesntExist()){
  239. return Redirect::back()
  240. ->withInput($request->except(['code']))
  241. ->withErrors(trans('auth.code_error'));
  242. }
  243. }elseif(sysConfig('is_invite_register') == 2){ // 必须使用邀请码
  244. return Redirect::back()->withInput()->withErrors(trans('auth.code_null'));
  245. }
  246. }
  247. // 注册前发送激活码
  248. if(sysConfig('is_activate_account') == 1){
  249. if(!$verify_code){
  250. return Redirect::back()
  251. ->withInput($request->except(['verify_code']))
  252. ->withErrors(trans('auth.captcha_null'));
  253. }
  254. $verifyCode = VerifyCode::whereAddress($email)->whereCode($verify_code)->whereStatus(0)->first();
  255. if(!$verifyCode){
  256. return Redirect::back()
  257. ->withInput($request->except(['verify_code']))
  258. ->withErrors(trans('auth.captcha_overtime'));
  259. }
  260. $verifyCode->status = 1;
  261. $verifyCode->save();
  262. }
  263. // 是否校验验证码
  264. $captcha = $this->check_captcha($request);
  265. if($captcha != false){
  266. return $captcha;
  267. }
  268. // 24小时内同IP注册限制
  269. if(sysConfig('register_ip_limit') && Cache::has($cacheKey)){
  270. $registerTimes = Cache::get($cacheKey);
  271. if($registerTimes >= sysConfig('register_ip_limit')){
  272. return Redirect::back()
  273. ->withInput($request->except(['code']))
  274. ->withErrors(trans('auth.register_anti'));
  275. }
  276. }
  277. // 获取可用端口
  278. $port = Helpers::getPort();
  279. if($port > sysConfig('max_port')){
  280. return Redirect::back()->withInput()->withErrors(trans('auth.register_close'));
  281. }
  282. // 获取aff
  283. $affArr = $this->getAff($code, $aff);
  284. $inviter_id = $affArr['inviter_id'];
  285. $transfer_enable = MB * ((int) sysConfig('default_traffic') + ($inviter_id? (int) sysConfig('referral_traffic') : 0));
  286. // 创建新用户
  287. $uid = Helpers::addUser($email, Hash::make($password), $transfer_enable, sysConfig('default_days'),
  288. $inviter_id);
  289. // 注册失败,抛出异常
  290. if(!$uid){
  291. return Redirect::back()->withInput()->withErrors(trans('auth.register_fail'));
  292. }
  293. // 更新昵称
  294. User::find($uid)->update(['username' => $username]);
  295. // 注册次数+1
  296. if(Cache::has($cacheKey)){
  297. Cache::increment($cacheKey);
  298. }else{
  299. Cache::put($cacheKey, 1, Day); // 24小时
  300. }
  301. // 更新邀请码
  302. if($affArr['code_id'] && sysConfig('is_invite_register')){
  303. Invite::find($affArr['code_id'])->update(['invitee_id' => $uid, 'status' => 1]);
  304. }
  305. // 清除邀请人Cookie
  306. Cookie::unqueue('register_aff');
  307. // 注册后发送激活码
  308. if(sysConfig('is_activate_account') == 2){
  309. // 生成激活账号的地址
  310. $token = $this->addVerifyUrl($uid, $email);
  311. $activeUserUrl = sysConfig('website_url').'/active/'.$token;
  312. $logId = Helpers::addNotificationLog('注册激活', '请求地址:'.$activeUserUrl, 1, $email);
  313. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  314. Session::flash('regSuccessMsg', trans('auth.register_active_tip'));
  315. }else{
  316. // 则直接给推荐人加流量
  317. if($inviter_id){
  318. $referralUser = User::find($inviter_id);
  319. if($referralUser && $referralUser->expired_at >= date('Y-m-d')){
  320. (new UserService($referralUser))->incrementData(sysConfig('referral_traffic') * MB);
  321. }
  322. }
  323. if(sysConfig('is_activate_account') == 1){
  324. User::find($uid)->update(['status' => 1]);
  325. }
  326. Session::flash('regSuccessMsg', trans('auth.register_success'));
  327. }
  328. return Redirect::to('login')->withInput();
  329. }
  330. $view['emailList'] = sysConfig('is_email_filtering') != 2? false : EmailFilter::whereType(2)->get();
  331. Session::put('register_token', Str::random());
  332. return view('auth.register', $view);
  333. }
  334. //邮箱检查
  335. private function emailChecker($email, $returnType = 0) {
  336. $emailFilterList = $this->emailFilterList(sysConfig('is_email_filtering'));
  337. $emailSuffix = explode('@', $email); // 提取邮箱后缀
  338. switch(sysConfig('is_email_filtering')){
  339. // 黑名单
  340. case 1:
  341. if(in_array(strtolower($emailSuffix[1]), $emailFilterList, true)){
  342. if($returnType){
  343. return Redirect::back()->withErrors(trans('auth.email_banned'));
  344. }
  345. return Response::json(['status' => 'fail', 'message' => trans('auth.email_banned')]);
  346. }
  347. break;
  348. //白名单
  349. case 2:
  350. if(!in_array(strtolower($emailSuffix[1]), $emailFilterList, true)){
  351. if($returnType){
  352. return Redirect::back()->withErrors(trans('auth.email_invalid'));
  353. }
  354. return Response::json(['status' => 'fail', 'message' => trans('auth.email_invalid')]);
  355. }
  356. break;
  357. default:
  358. if($returnType){
  359. return Redirect::back()->withErrors(trans('auth.email_invalid'));
  360. }
  361. return Response::json(['status' => 'fail', 'message' => trans('auth.email_invalid')]);
  362. }
  363. return false;
  364. }
  365. /**
  366. * 获取AFF
  367. *
  368. * @param string|null $code 邀请码
  369. * @param int|null $aff URL中的aff参数
  370. *
  371. * @return array
  372. */
  373. private function getAff($code = null, $aff = null): array {
  374. $data = ['inviter_id' => null, 'code_id' => 0];// 邀请人ID 与 邀请码ID
  375. // 有邀请码先用邀请码,用谁的邀请码就给谁返利
  376. if($code){
  377. $inviteCode = Invite::whereCode($code)->whereStatus(0)->first();
  378. if($inviteCode){
  379. $data['inviter_id'] = $inviteCode->inviter_id;
  380. $data['code_id'] = $inviteCode->id;
  381. }
  382. }
  383. // 没有用邀请码或者邀请码是管理员生成的,则检查cookie或者url链接
  384. if(!$data['inviter_id']){
  385. // 检查一下cookie里有没有aff
  386. $cookieAff = \Request::hasCookie('register_aff')? \Request::cookie('register_aff') : 0;
  387. if($cookieAff){
  388. $data['inviter_id'] = User::find($cookieAff)? $cookieAff : 0;
  389. }elseif($aff){ // 如果cookie里没有aff,就再检查一下请求的url里有没有aff,因为有些人的浏览器会禁用了cookie,比如chrome开了隐私模式
  390. $data['inviter_id'] = User::find($aff)? $aff : 0;
  391. }
  392. }
  393. return $data;
  394. }
  395. // 生成申请的请求地址
  396. private function addVerifyUrl($uid, $email) {
  397. $token = md5(sysConfig('website_name').$email.microtime());
  398. $verify = new Verify();
  399. $verify->type = 1;
  400. $verify->user_id = $uid;
  401. $verify->token = $token;
  402. $verify->status = 0;
  403. $verify->save();
  404. return $token;
  405. }
  406. // 重设密码页
  407. public function resetPassword(Request $request) {
  408. if($request->isMethod('POST')){
  409. // 校验请求
  410. $validator = Validator::make($request->all(), [
  411. 'email' => 'required|email'
  412. ], [
  413. 'email.required' => trans('auth.email_null'),
  414. 'email.email' => trans('auth.email_legitimate')
  415. ]);
  416. if($validator->fails()){
  417. return Redirect::back()->withInput()->withErrors($validator->errors());
  418. }
  419. $email = $request->input('email');
  420. // 是否开启重设密码
  421. if(!sysConfig('is_reset_password')){
  422. return Redirect::back()->withErrors(trans('auth.reset_password_close',
  423. ['email' => sysConfig('webmaster_email')]));
  424. }
  425. // 查找账号
  426. $user = User::whereEmail($email)->first();
  427. if(!$user){
  428. return Redirect::back()->withErrors(trans('auth.email_notExist'));
  429. }
  430. // 24小时内重设密码次数限制
  431. $resetTimes = 0;
  432. if(Cache::has('resetPassword_'.md5($email))){
  433. $resetTimes = Cache::get('resetPassword_'.md5($email));
  434. if($resetTimes >= sysConfig('reset_password_times')){
  435. return Redirect::back()->withErrors(trans('auth.reset_password_limit',
  436. ['time' => sysConfig('reset_password_times')]));
  437. }
  438. }
  439. // 生成取回密码的地址
  440. $token = $this->addVerifyUrl($user->id, $email);
  441. // 发送邮件
  442. $resetPasswordUrl = sysConfig('website_url').'/reset/'.$token;
  443. $logId = Helpers::addNotificationLog('重置密码', '请求地址:'.$resetPasswordUrl, 1, $email);
  444. Mail::to($email)->send(new resetPassword($logId, $resetPasswordUrl));
  445. Cache::put('resetPassword_'.md5($email), $resetTimes + 1, Day);
  446. return Redirect::back()->with('successMsg', trans('auth.reset_password_success_tip'));
  447. }
  448. return view('auth.resetPassword');
  449. }
  450. // 重设密码
  451. public function reset(Request $request, $token) {
  452. if(!$token){
  453. return Redirect::to('login');
  454. }
  455. if($request->isMethod('POST')){
  456. $validator = Validator::make($request->all(), [
  457. 'password' => 'required|min:6',
  458. 'confirmPassword' => 'required|same:password'
  459. ], [
  460. 'password.required' => trans('auth.password_null'),
  461. 'password.min' => trans('auth.password_limit'),
  462. 'confirmPassword.required' => trans('auth.password_null'),
  463. 'confirmPassword.min' => trans('auth.password_limit'),
  464. 'confirmPassword.same' => trans('auth.password_same'),
  465. ]);
  466. if($validator->fails()){
  467. return Redirect::back()->withInput()->withErrors($validator->errors());
  468. }
  469. $password = $request->input('password');
  470. // 校验账号
  471. $verify = Verify::type(1)->whereToken($token)->first();
  472. $user = $verify->user;
  473. if(!$verify){
  474. return Redirect::to('login');
  475. }
  476. if($verify->status == 1){
  477. return Redirect::back()->withErrors(trans('auth.overtime'));
  478. }
  479. if($user->status < 0){
  480. return Redirect::back()->withErrors(trans('auth.email_banned'));
  481. }
  482. if(Hash::check($password, $verify->user->password)){
  483. return Redirect::back()->withErrors(trans('auth.reset_password_same_fail'));
  484. }
  485. // 更新密码
  486. if(!$user->update(['password' => Hash::make($password)])){
  487. return Redirect::back()->withErrors(trans('auth.reset_password_fail'));
  488. }
  489. // 置为已使用
  490. $verify->status = 1;
  491. $verify->save();
  492. return Redirect::to('login')->with('successMsg', trans('auth.reset_password_new'));
  493. }
  494. $verify = Verify::type(1)->whereToken($token)->first();
  495. if(!$verify){
  496. return Redirect::to('login');
  497. }
  498. if(time() - strtotime($verify->created_at) >= 1800){
  499. // 置为已失效
  500. $verify->status = 2;
  501. $verify->save();
  502. }
  503. // 重新获取一遍verify
  504. $view['verify'] = Verify::type(1)->whereToken($token)->first();
  505. return view('auth.reset', $view);
  506. }
  507. // 激活账号页
  508. public function activeUser(Request $request) {
  509. if($request->isMethod('POST')){
  510. $validator = Validator::make($request->all(), [
  511. 'email' => 'required|email|exists:user,email'
  512. ], [
  513. 'email.required' => trans('auth.email_null'),
  514. 'email.email' => trans('auth.email_legitimate'),
  515. 'email.exists' => trans('auth.email_notExist')
  516. ]);
  517. if($validator->fails()){
  518. return Redirect::back()->withInput()->withErrors($validator->errors());
  519. }
  520. $email = $request->input('email');
  521. // 是否开启账号激活
  522. if(sysConfig('is_activate_account') != 2){
  523. return Redirect::back()->withInput()->withErrors(trans('auth.active_close',
  524. ['email' => sysConfig('webmaster_email')]));
  525. }
  526. // 查找账号
  527. $user = User::whereEmail($email)->firstOrFail();
  528. if($user->status < 0){
  529. return Redirect::back()->withErrors(trans('auth.login_ban', ['email' => sysConfig('webmaster_email')]));
  530. }
  531. if($user->status > 0){
  532. return Redirect::back()->withErrors(trans('auth.email_normal'));
  533. }
  534. // 24小时内激活次数限制
  535. $activeTimes = 0;
  536. if(Cache::has('activeUser_'.md5($email))){
  537. $activeTimes = Cache::get('activeUser_'.md5($email));
  538. if($activeTimes >= sysConfig('active_times')){
  539. return Redirect::back()->withErrors(trans('auth.active_limit',
  540. ['time' => sysConfig('webmaster_email')]));
  541. }
  542. }
  543. // 生成激活账号的地址
  544. $token = $this->addVerifyUrl($user->id, $email);
  545. // 发送邮件
  546. $activeUserUrl = sysConfig('website_url').'/active/'.$token;
  547. $logId = Helpers::addNotificationLog('激活账号', '请求地址:'.$activeUserUrl, 1, $email);
  548. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  549. Cache::put('activeUser_'.md5($email), $activeTimes + 1, Day);
  550. return Redirect::back()->with('successMsg', trans('auth.register_active_tip'));
  551. }
  552. return view('auth.activeUser');
  553. }
  554. // 激活账号
  555. public function active($token) {
  556. if(!$token){
  557. return Redirect::to('login');
  558. }
  559. $verify = Verify::type(1)->with('user')->whereToken($token)->first();
  560. $user = $verify->user;
  561. if(!$verify){
  562. return Redirect::to('login');
  563. }
  564. if(empty($user)){
  565. Session::flash('errorMsg', trans('auth.overtime'));
  566. return view('auth.active');
  567. }
  568. if($verify->status > 0){
  569. Session::flash('errorMsg', trans('auth.overtime'));
  570. return view('auth.active');
  571. }
  572. if($user->status != 0){
  573. Session::flash('errorMsg', trans('auth.email_normal'));
  574. return view('auth.active');
  575. }
  576. if(time() - strtotime($verify->created_at) >= 1800){
  577. Session::flash('errorMsg', trans('auth.overtime'));
  578. // 置为已失效
  579. $verify->status = 2;
  580. $verify->save();
  581. return view('auth.active');
  582. }
  583. // 更新账号状态
  584. if(!$user->update(['status' => 1])){
  585. Session::flash('errorMsg', trans('auth.active_fail'));
  586. return Redirect::back();
  587. }
  588. // 置为已使用
  589. $verify->status = 1;
  590. $verify->save();
  591. // 账号激活后给邀请人送流量
  592. $inviter = $user->inviter;
  593. if($inviter){
  594. (new UserService($inviter))->incrementData(sysConfig('referral_traffic') * MB);
  595. }
  596. Session::flash('successMsg', trans('auth.active_success'));
  597. return view('auth.active');
  598. }
  599. // 发送注册验证码
  600. public function sendCode(Request $request) {
  601. $validator = Validator::make($request->all(), [
  602. 'email' => 'required|email|unique:user'
  603. ], [
  604. 'email.required' => trans('auth.email_null'),
  605. 'email.email' => trans('auth.email_legitimate'),
  606. 'email.unique' => trans('auth.email_exist')
  607. ]);
  608. $email = $request->input('email');
  609. if($validator->fails()){
  610. return Response::json(['status' => 'fail', 'message' => $validator->getMessageBag()->first()]);
  611. }
  612. // 校验域名邮箱黑白名单
  613. if(sysConfig('is_email_filtering')){
  614. $result = $this->emailChecker($email);
  615. if($result !== false){
  616. return $result;
  617. }
  618. }
  619. // 是否开启注册发送验证码
  620. if(sysConfig('is_activate_account') != 1){
  621. return Response::json(['status' => 'fail', 'message' => trans('auth.captcha_close')]);
  622. }
  623. // 防刷机制
  624. if(Cache::has('send_verify_code_'.md5(getClientIP()))){
  625. return Response::json(['status' => 'fail', 'message' => trans('auth.register_anti')]);
  626. }
  627. // 发送邮件
  628. $code = Str::random(6);
  629. $logId = Helpers::addNotificationLog('发送注册验证码', '验证码:'.$code, 1, $email);
  630. Mail::to($email)->send(new sendVerifyCode($logId, $code));
  631. $this->addVerifyCode($email, $code);
  632. Cache::put('send_verify_code_'.md5(getClientIP()), getClientIP(), Minute);
  633. return Response::json(['status' => 'success', 'message' => trans('auth.captcha_send')]);
  634. }
  635. // 生成注册验证码
  636. private function addVerifyCode($email, $code): void {
  637. $verify = new VerifyCode();
  638. $verify->address = $email;
  639. $verify->code = $code;
  640. $verify->status = 0;
  641. $verify->save();
  642. }
  643. // 公开的邀请码列表
  644. public function free() {
  645. $view['inviteList'] = Invite::whereInviterId(0)->whereStatus(0)->paginate();
  646. return view('auth.free', $view);
  647. }
  648. // 切换语言
  649. public function switchLang($locale): RedirectResponse {
  650. Session::put("locale", $locale);
  651. return Redirect::back();
  652. }
  653. }