AuthController.php 24 KB

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