AuthController.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. // 登录
  40. public function login(Request $request)
  41. {
  42. if ($request->isMethod('POST')) {
  43. $validator = Validator::make(
  44. $request->all(),
  45. [
  46. 'email' => 'required|email',
  47. 'password' => 'required',
  48. ],
  49. [
  50. 'email.required' => trans('auth.email_null'),
  51. 'password.required' => trans('auth.password_null'),
  52. ]
  53. );
  54. if ($validator->fails()) {
  55. return Redirect::back()->withInput()->withErrors(
  56. $validator->errors()
  57. );
  58. }
  59. $email = $request->input('email');
  60. $password = $request->input('password');
  61. $remember = $request->input('remember');
  62. // 是否校验验证码
  63. $captcha = $this->check_captcha($request);
  64. if ($captcha != false) {
  65. return $captcha;
  66. }
  67. // 验证账号并创建会话
  68. if ( ! Auth::attempt(
  69. ['email' => $email, 'password' => $password],
  70. $remember
  71. )) {
  72. return Redirect::back()->withInput()->withErrors(
  73. trans('auth.login_error')
  74. );
  75. }
  76. $user = Auth::getUser();
  77. if ( ! $user) {
  78. return Redirect::back()->withInput()->withErrors(
  79. trans('auth.login_error')
  80. );
  81. }
  82. // 校验普通用户账号状态
  83. if ( ! $user->is_admin) {
  84. if ($user->status < 0) {
  85. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  86. return Redirect::back()->withInput()->withErrors(
  87. trans(
  88. 'auth.login_ban',
  89. ['email' => sysConfig('webmaster_email')]
  90. )
  91. );
  92. }
  93. if ($user->status == 0 && sysConfig('is_activate_account')) {
  94. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  95. return Redirect::back()
  96. ->withInput()
  97. ->withErrors(
  98. trans(
  99. 'auth.active_tip'
  100. ) . '<a href="/activeUser?email=' . $email . '" target="_blank"><span style="color:#000">【' . trans(
  101. 'auth.active_account'
  102. ) . '】</span></a>'
  103. );
  104. }
  105. }
  106. // 写入登录日志
  107. $this->addUserLoginLog($user->id, getClientIp());
  108. // 更新登录信息
  109. Auth::getUser()->update(['last_login' => time()]);
  110. // 根据权限跳转
  111. if ($user->is_admin) {
  112. return Redirect::to('admin');
  113. }
  114. return Redirect::to('/');
  115. }
  116. if (Auth::check()) {
  117. if (Auth::getUser()->is_admin) {
  118. return Redirect::to('admin');
  119. }
  120. return Redirect::to('/');
  121. }
  122. return view('auth.login');
  123. }
  124. // 校验验证码
  125. private function check_captcha($request)
  126. {
  127. switch (sysConfig('is_captcha')) {
  128. case 1: // 默认图形验证码
  129. if ( ! Captcha::check($request->input('captcha'))) {
  130. return Redirect::back()->withInput()->withErrors(
  131. trans('auth.captcha_error')
  132. );
  133. }
  134. break;
  135. case 2: // Geetest
  136. $validator = Validator::make(
  137. $request->all(),
  138. [
  139. 'geetest_challenge' => 'required|geetest',
  140. ],
  141. [
  142. 'geetest' => trans('auth.captcha_fail'),
  143. ]
  144. );
  145. if ($validator->fails()) {
  146. return Redirect::back()->withInput()->withErrors(
  147. trans('auth.captcha_fail')
  148. );
  149. }
  150. break;
  151. case 3: // Google reCAPTCHA
  152. $validator = Validator::make(
  153. $request->all(),
  154. [
  155. 'g-recaptcha-response' => 'required|NoCaptcha',
  156. ]
  157. );
  158. if ($validator->fails()) {
  159. return Redirect::back()->withInput()->withErrors(
  160. trans('auth.captcha_fail')
  161. );
  162. }
  163. break;
  164. case 4: // hCaptcha
  165. $validator = Validator::make(
  166. $request->all(),
  167. [
  168. 'h-captcha-response' => 'required|HCaptcha',
  169. ]
  170. );
  171. if ($validator->fails()) {
  172. return Redirect::back()->withInput()->withErrors(
  173. trans('auth.captcha_fail')
  174. );
  175. }
  176. break;
  177. default: // 不启用验证码
  178. break;
  179. }
  180. return 0;
  181. }
  182. /**
  183. * 添加用户登录日志
  184. *
  185. * @param int $userId 用户ID
  186. * @param string $ip IP地址
  187. */
  188. private function addUserLoginLog(int $userId, string $ip): void
  189. {
  190. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  191. Log::info('识别到IPv6,尝试解析:' . $ip);
  192. $ipInfo = getIPInfo($ip);
  193. } else {
  194. $ipInfo = QQWry::ip($ip); // 通过纯真IP库解析IPv4信息
  195. if (isset($ipInfo['error'])) {
  196. Log::info('无法识别IPv4,尝试使用IPIP的IP库解析:' . $ip);
  197. $ipip = IPIP::ip($ip);
  198. $ipInfo = [
  199. 'country' => $ipip['country_name'],
  200. 'province' => $ipip['region_name'],
  201. 'city' => $ipip['city_name'],
  202. ];
  203. } else {
  204. // 判断纯真IP库获取的国家信息是否与IPIP的IP库获取的信息一致,不一致则用IPIP的(因为纯真IP库的非大陆IP准确率较低)
  205. $ipip = IPIP::ip($ip);
  206. if ($ipInfo['country'] != $ipip['country_name']) {
  207. $ipInfo['country'] = $ipip['country_name'];
  208. $ipInfo['province'] = $ipip['region_name'];
  209. $ipInfo['city'] = $ipip['city_name'];
  210. }
  211. }
  212. }
  213. if (empty($ipInfo) || empty($ipInfo['country'])) {
  214. Log::warning("获取IP信息异常:" . $ip);
  215. }
  216. $log = new UserLoginLog();
  217. $log->user_id = $userId;
  218. $log->ip = $ip;
  219. $log->country = $ipInfo['country'] ?? '';
  220. $log->province = $ipInfo['province'] ?? '';
  221. $log->city = $ipInfo['city'] ?? '';
  222. $log->county = $ipInfo['county'] ?? '';
  223. $log->isp = $ipInfo['isp'] ?? ($ipInfo['organization'] ?? '');
  224. $log->area = $ipInfo['area'] ?? '';
  225. $log->save();
  226. }
  227. // 退出
  228. public function logout(): RedirectResponse
  229. {
  230. Auth::logout();
  231. return Redirect::to('login');
  232. }
  233. // 注册
  234. public function register(Request $request)
  235. {
  236. $cacheKey = 'register_times_' . md5(getClientIp()); // 注册限制缓存key
  237. if ($request->isMethod('POST')) {
  238. $validator = Validator::make(
  239. $request->all(),
  240. [
  241. 'username' => 'required',
  242. 'email' => 'required|email|unique:user',
  243. 'password' => 'required|min:6',
  244. 'confirmPassword' => 'required|same:password',
  245. 'term' => 'accepted',
  246. ],
  247. [
  248. 'username.required' => trans('auth.email_null'),
  249. 'email.required' => trans('auth.email_null'),
  250. 'email.email' => trans(
  251. 'auth.email_legitimate'
  252. ),
  253. 'email.unique' => trans('auth.email_exist'),
  254. 'password.required' => trans('auth.password_null'),
  255. 'password.min' => trans('auth.password_limit'),
  256. 'confirmPassword.required' => trans(
  257. 'auth.confirm_password'
  258. ),
  259. 'confirmPassword.same' => trans('auth.password_same'),
  260. 'term.accepted' => trans('auth.unaccepted'),
  261. ]
  262. );
  263. if ($validator->fails()) {
  264. return Redirect::back()->withInput()->withErrors(
  265. $validator->errors()
  266. );
  267. }
  268. $username = $request->input('username');
  269. $email = $request->input('email');
  270. $password = $request->input('password');
  271. $register_token = $request->input('register_token');
  272. $code = $request->input('code');
  273. $verify_code = $request->input('verify_code');
  274. $aff = (int)$request->input('aff');
  275. // 防止重复提交
  276. if ($register_token !== Session::get('register_token')) {
  277. return Redirect::back()->withInput()->withErrors(
  278. trans('auth.repeat_request')
  279. );
  280. }
  281. Session::forget('register_token');
  282. // 是否开启注册
  283. if ( ! sysConfig('is_register')) {
  284. return Redirect::back()->withErrors(
  285. trans('auth.register_close')
  286. );
  287. }
  288. // 校验域名邮箱黑白名单
  289. if (sysConfig('is_email_filtering')) {
  290. $result = $this->emailChecker($email, 1);
  291. if ($result !== false) {
  292. return $result;
  293. }
  294. }
  295. // 如果需要邀请注册
  296. if (sysConfig('is_invite_register')) {
  297. // 校验邀请码合法性
  298. if ($code) {
  299. if (Invite::whereCode($code)->whereStatus(0)->doesntExist(
  300. )) {
  301. return Redirect::back()
  302. ->withInput($request->except(['code']))
  303. ->withErrors(trans('auth.code_error'));
  304. }
  305. } elseif (sysConfig('is_invite_register') == 2) { // 必须使用邀请码
  306. return Redirect::back()->withInput()->withErrors(
  307. trans('auth.code_null')
  308. );
  309. }
  310. }
  311. // 注册前发送激活码
  312. if (sysConfig('is_activate_account') == 1) {
  313. if ( ! $verify_code) {
  314. return Redirect::back()
  315. ->withInput(
  316. $request->except(['verify_code'])
  317. )
  318. ->withErrors(trans('auth.captcha_null'));
  319. }
  320. $verifyCode = VerifyCode::whereAddress($email)->whereCode(
  321. $verify_code
  322. )->whereStatus(0)->first();
  323. if ( ! $verifyCode) {
  324. return Redirect::back()
  325. ->withInput(
  326. $request->except(['verify_code'])
  327. )
  328. ->withErrors(trans('auth.captcha_overtime'));
  329. }
  330. $verifyCode->status = 1;
  331. $verifyCode->save();
  332. }
  333. // 是否校验验证码
  334. $captcha = $this->check_captcha($request);
  335. if ($captcha != false) {
  336. return $captcha;
  337. }
  338. // 24小时内同IP注册限制
  339. if (sysConfig('register_ip_limit') && Cache::has($cacheKey)) {
  340. $registerTimes = Cache::get($cacheKey);
  341. if ($registerTimes >= sysConfig('register_ip_limit')) {
  342. return Redirect::back()
  343. ->withInput($request->except(['code']))
  344. ->withErrors(trans('auth.register_anti'));
  345. }
  346. }
  347. // 获取可用端口
  348. $port = Helpers::getPort();
  349. if ($port > sysConfig('max_port')) {
  350. return Redirect::back()->withInput()->withErrors(
  351. trans('auth.register_close')
  352. );
  353. }
  354. // 获取aff
  355. $affArr = $this->getAff($code, $aff);
  356. $inviter_id = $affArr['inviter_id'];
  357. $transfer_enable = MB * ((int)sysConfig(
  358. 'default_traffic'
  359. ) + ($inviter_id ? (int)sysConfig('referral_traffic') : 0));
  360. // 创建新用户
  361. $uid = Helpers::addUser(
  362. $email,
  363. Hash::make($password),
  364. $transfer_enable,
  365. sysConfig('default_days'),
  366. $inviter_id
  367. );
  368. // 注册失败,抛出异常
  369. if ( ! $uid) {
  370. return Redirect::back()->withInput()->withErrors(
  371. trans('auth.register_fail')
  372. );
  373. }
  374. // 更新昵称
  375. User::find($uid)->update(['username' => $username]);
  376. // 注册次数+1
  377. if (Cache::has($cacheKey)) {
  378. Cache::increment($cacheKey);
  379. } else {
  380. Cache::put($cacheKey, 1, Day); // 24小时
  381. }
  382. // 更新邀请码
  383. if ($affArr['code_id'] && sysConfig('is_invite_register')) {
  384. Invite::find($affArr['code_id'])->update(
  385. ['invitee_id' => $uid, 'status' => 1]
  386. );
  387. }
  388. // 清除邀请人Cookie
  389. Cookie::unqueue('register_aff');
  390. // 注册后发送激活码
  391. if (sysConfig('is_activate_account') == 2) {
  392. // 生成激活账号的地址
  393. $token = $this->addVerifyUrl($uid, $email);
  394. $activeUserUrl = sysConfig('website_url') . '/active/' . $token;
  395. $logId = Helpers::addNotificationLog(
  396. '注册激活',
  397. '请求地址:' . $activeUserUrl,
  398. 1,
  399. $email
  400. );
  401. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  402. Session::flash(
  403. 'regSuccessMsg',
  404. trans('auth.register_active_tip')
  405. );
  406. } else {
  407. // 则直接给推荐人加流量
  408. if ($inviter_id) {
  409. $referralUser = User::find($inviter_id);
  410. if ($referralUser && $referralUser->expired_at >= date(
  411. 'Y-m-d'
  412. )) {
  413. (new UserService($referralUser))->incrementData(
  414. sysConfig('referral_traffic') * MB
  415. );
  416. }
  417. }
  418. if (sysConfig('is_activate_account') == 1) {
  419. User::find($uid)->update(['status' => 1]);
  420. }
  421. Session::flash('regSuccessMsg', trans('auth.register_success'));
  422. }
  423. return Redirect::to('login')->withInput();
  424. }
  425. $view['emailList'] = sysConfig(
  426. 'is_email_filtering'
  427. ) != 2 ? false : EmailFilter::whereType(2)->get();
  428. Session::put('register_token', Str::random());
  429. return view('auth.register', $view);
  430. }
  431. //邮箱检查
  432. private function emailChecker($email, $returnType = 0)
  433. {
  434. $emailFilterList = $this->emailFilterList(
  435. sysConfig('is_email_filtering')
  436. );
  437. $emailSuffix = explode('@', $email); // 提取邮箱后缀
  438. switch (sysConfig('is_email_filtering')) {
  439. // 黑名单
  440. case 1:
  441. if (in_array(
  442. strtolower($emailSuffix[1]),
  443. $emailFilterList,
  444. true
  445. )) {
  446. if ($returnType) {
  447. return Redirect::back()->withErrors(
  448. trans('auth.email_banned')
  449. );
  450. }
  451. return Response::json(
  452. [
  453. 'status' => 'fail',
  454. 'message' => trans('auth.email_banned'),
  455. ]
  456. );
  457. }
  458. break;
  459. //白名单
  460. case 2:
  461. if ( ! in_array(
  462. strtolower($emailSuffix[1]),
  463. $emailFilterList,
  464. true
  465. )) {
  466. if ($returnType) {
  467. return Redirect::back()->withErrors(
  468. trans('auth.email_invalid')
  469. );
  470. }
  471. return Response::json(
  472. [
  473. 'status' => 'fail',
  474. 'message' => trans('auth.email_invalid'),
  475. ]
  476. );
  477. }
  478. break;
  479. default:
  480. if ($returnType) {
  481. return Redirect::back()->withErrors(
  482. trans('auth.email_invalid')
  483. );
  484. }
  485. return Response::json(
  486. [
  487. 'status' => 'fail',
  488. 'message' => trans('auth.email_invalid'),
  489. ]
  490. );
  491. }
  492. return false;
  493. }
  494. /**
  495. * 获取AFF
  496. *
  497. * @param string|null $code 邀请码
  498. * @param int|null $aff URL中的aff参数
  499. *
  500. * @return array
  501. */
  502. private function getAff($code = null, $aff = null): array
  503. {
  504. $data = ['inviter_id' => null, 'code_id' => 0];// 邀请人ID 与 邀请码ID
  505. // 有邀请码先用邀请码,用谁的邀请码就给谁返利
  506. if ($code) {
  507. $inviteCode = Invite::whereCode($code)->whereStatus(0)->first();
  508. if ($inviteCode) {
  509. $data['inviter_id'] = $inviteCode->inviter_id;
  510. $data['code_id'] = $inviteCode->id;
  511. }
  512. }
  513. // 没有用邀请码或者邀请码是管理员生成的,则检查cookie或者url链接
  514. if ( ! $data['inviter_id']) {
  515. // 检查一下cookie里有没有aff
  516. $cookieAff = \Request::hasCookie('register_aff') ? \Request::cookie(
  517. 'register_aff'
  518. ) : 0;
  519. if ($cookieAff) {
  520. $data['inviter_id'] = User::find($cookieAff) ? $cookieAff : 0;
  521. } elseif ($aff) { // 如果cookie里没有aff,就再检查一下请求的url里有没有aff,因为有些人的浏览器会禁用了cookie,比如chrome开了隐私模式
  522. $data['inviter_id'] = User::find($aff) ? $aff : 0;
  523. }
  524. }
  525. return $data;
  526. }
  527. // 生成申请的请求地址
  528. private function addVerifyUrl($uid, $email)
  529. {
  530. $token = md5(
  531. sysConfig('website_name') . $email . microtime()
  532. );
  533. $verify = new Verify();
  534. $verify->type = 1;
  535. $verify->user_id = $uid;
  536. $verify->token = $token;
  537. $verify->status = 0;
  538. $verify->save();
  539. return $token;
  540. }
  541. // 重设密码页
  542. public function resetPassword(Request $request)
  543. {
  544. if ($request->isMethod('POST')) {
  545. // 校验请求
  546. $validator = Validator::make(
  547. $request->all(),
  548. [
  549. 'email' => 'required|email',
  550. ],
  551. [
  552. 'email.required' => trans('auth.email_null'),
  553. 'email.email' => trans('auth.email_legitimate'),
  554. ]
  555. );
  556. if ($validator->fails()) {
  557. return Redirect::back()->withInput()->withErrors(
  558. $validator->errors()
  559. );
  560. }
  561. $email = $request->input('email');
  562. // 是否开启重设密码
  563. if ( ! sysConfig('is_reset_password')) {
  564. return Redirect::back()->withErrors(
  565. trans(
  566. 'auth.reset_password_close',
  567. ['email' => sysConfig('webmaster_email')]
  568. )
  569. );
  570. }
  571. // 查找账号
  572. $user = User::whereEmail($email)->first();
  573. if ( ! $user) {
  574. return Redirect::back()->withErrors(
  575. trans('auth.email_notExist')
  576. );
  577. }
  578. // 24小时内重设密码次数限制
  579. $resetTimes = 0;
  580. if (Cache::has('resetPassword_' . md5($email))) {
  581. $resetTimes = Cache::get('resetPassword_' . md5($email));
  582. if ($resetTimes >= sysConfig('reset_password_times')) {
  583. return Redirect::back()->withErrors(
  584. trans(
  585. 'auth.reset_password_limit',
  586. ['time' => sysConfig('reset_password_times')]
  587. )
  588. );
  589. }
  590. }
  591. // 生成取回密码的地址
  592. $token = $this->addVerifyUrl($user->id, $email);
  593. // 发送邮件
  594. $resetPasswordUrl = sysConfig('website_url') . '/reset/' . $token;
  595. $logId = Helpers::addNotificationLog(
  596. '重置密码',
  597. '请求地址:' . $resetPasswordUrl,
  598. 1,
  599. $email
  600. );
  601. Mail::to($email)->send(
  602. new resetPassword($logId, $resetPasswordUrl)
  603. );
  604. Cache::put('resetPassword_' . md5($email), $resetTimes + 1, Day);
  605. return Redirect::back()->with(
  606. 'successMsg',
  607. trans(
  608. 'auth.reset_password_success_tip'
  609. )
  610. );
  611. }
  612. return view('auth.resetPassword');
  613. }
  614. // 重设密码
  615. public function reset(Request $request, $token)
  616. {
  617. if ( ! $token) {
  618. return Redirect::to('login');
  619. }
  620. if ($request->isMethod('POST')) {
  621. $validator = Validator::make(
  622. $request->all(),
  623. [
  624. 'password' => 'required|min:6',
  625. 'confirmPassword' => 'required|same:password',
  626. ],
  627. [
  628. 'password.required' => trans('auth.password_null'),
  629. 'password.min' => trans('auth.password_limit'),
  630. 'confirmPassword.required' => trans('auth.password_null'),
  631. 'confirmPassword.min' => trans('auth.password_limit'),
  632. 'confirmPassword.same' => trans('auth.password_same'),
  633. ]
  634. );
  635. if ($validator->fails()) {
  636. return Redirect::back()->withInput()->withErrors(
  637. $validator->errors()
  638. );
  639. }
  640. $password = $request->input('password');
  641. // 校验账号
  642. $verify = Verify::type(1)->whereToken($token)->first();
  643. $user = $verify->user;
  644. if ( ! $verify) {
  645. return Redirect::to('login');
  646. }
  647. if ($verify->status == 1) {
  648. return Redirect::back()->withErrors(trans('auth.overtime'));
  649. }
  650. if ($user->status < 0) {
  651. return Redirect::back()->withErrors(trans('auth.email_banned'));
  652. }
  653. if (Hash::check($password, $verify->user->password)) {
  654. return Redirect::back()->withErrors(
  655. trans('auth.reset_password_same_fail')
  656. );
  657. }
  658. // 更新密码
  659. if ( ! $user->update(['password' => Hash::make($password)])) {
  660. return Redirect::back()->withErrors(
  661. trans('auth.reset_password_fail')
  662. );
  663. }
  664. // 置为已使用
  665. $verify->status = 1;
  666. $verify->save();
  667. return Redirect::to('login')->with(
  668. 'successMsg',
  669. trans('auth.reset_password_new')
  670. );
  671. }
  672. $verify = Verify::type(1)->whereToken($token)->first();
  673. if ( ! $verify) {
  674. return Redirect::to('login');
  675. }
  676. if (time() - strtotime($verify->created_at) >= 1800) {
  677. // 置为已失效
  678. $verify->status = 2;
  679. $verify->save();
  680. }
  681. // 重新获取一遍verify
  682. $view['verify'] = Verify::type(1)->whereToken($token)->first();
  683. return view('auth.reset', $view);
  684. }
  685. // 激活账号页
  686. public function activeUser(Request $request)
  687. {
  688. if ($request->isMethod('POST')) {
  689. $validator = Validator::make(
  690. $request->all(),
  691. [
  692. 'email' => 'required|email|exists:user,email',
  693. ],
  694. [
  695. 'email.required' => trans('auth.email_null'),
  696. 'email.email' => trans('auth.email_legitimate'),
  697. 'email.exists' => trans('auth.email_notExist'),
  698. ]
  699. );
  700. if ($validator->fails()) {
  701. return Redirect::back()->withInput()->withErrors(
  702. $validator->errors()
  703. );
  704. }
  705. $email = $request->input('email');
  706. // 是否开启账号激活
  707. if (sysConfig('is_activate_account') != 2) {
  708. return Redirect::back()->withInput()->withErrors(
  709. trans(
  710. 'auth.active_close',
  711. ['email' => sysConfig('webmaster_email')]
  712. )
  713. );
  714. }
  715. // 查找账号
  716. $user = User::whereEmail($email)->firstOrFail();
  717. if ($user->status < 0) {
  718. return Redirect::back()->withErrors(
  719. trans(
  720. 'auth.login_ban',
  721. ['email' => sysConfig('webmaster_email')]
  722. )
  723. );
  724. }
  725. if ($user->status > 0) {
  726. return Redirect::back()->withErrors(trans('auth.email_normal'));
  727. }
  728. // 24小时内激活次数限制
  729. $activeTimes = 0;
  730. if (Cache::has('activeUser_' . md5($email))) {
  731. $activeTimes = Cache::get('activeUser_' . md5($email));
  732. if ($activeTimes >= sysConfig('active_times')) {
  733. return Redirect::back()->withErrors(
  734. trans(
  735. 'auth.active_limit',
  736. ['time' => sysConfig('webmaster_email')]
  737. )
  738. );
  739. }
  740. }
  741. // 生成激活账号的地址
  742. $token = $this->addVerifyUrl($user->id, $email);
  743. // 发送邮件
  744. $activeUserUrl = sysConfig('website_url') . '/active/' . $token;
  745. $logId = Helpers::addNotificationLog(
  746. '激活账号',
  747. '请求地址:' . $activeUserUrl,
  748. 1,
  749. $email
  750. );
  751. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  752. Cache::put('activeUser_' . md5($email), $activeTimes + 1, Day);
  753. return Redirect::back()->with(
  754. 'successMsg',
  755. trans('auth.register_active_tip')
  756. );
  757. }
  758. return view('auth.activeUser');
  759. }
  760. // 激活账号
  761. public function active($token)
  762. {
  763. if ( ! $token) {
  764. return Redirect::to('login');
  765. }
  766. $verify = Verify::type(1)->with('user')->whereToken($token)->first();
  767. $user = $verify->user;
  768. if ( ! $verify) {
  769. return Redirect::to('login');
  770. }
  771. if (empty($user)) {
  772. Session::flash('errorMsg', trans('auth.overtime'));
  773. return view('auth.active');
  774. }
  775. if ($verify->status > 0) {
  776. Session::flash('errorMsg', trans('auth.overtime'));
  777. return view('auth.active');
  778. }
  779. if ($user->status != 0) {
  780. Session::flash('errorMsg', trans('auth.email_normal'));
  781. return view('auth.active');
  782. }
  783. if (time() - strtotime($verify->created_at) >= 1800) {
  784. Session::flash('errorMsg', trans('auth.overtime'));
  785. // 置为已失效
  786. $verify->status = 2;
  787. $verify->save();
  788. return view('auth.active');
  789. }
  790. // 更新账号状态
  791. if ( ! $user->update(['status' => 1])) {
  792. Session::flash('errorMsg', trans('auth.active_fail'));
  793. return Redirect::back();
  794. }
  795. // 置为已使用
  796. $verify->status = 1;
  797. $verify->save();
  798. // 账号激活后给邀请人送流量
  799. $inviter = $user->inviter;
  800. if ($inviter) {
  801. (new UserService($inviter))->incrementData(
  802. sysConfig('referral_traffic') * MB
  803. );
  804. }
  805. Session::flash('successMsg', trans('auth.active_success'));
  806. return view('auth.active');
  807. }
  808. // 发送注册验证码
  809. public function sendCode(Request $request)
  810. {
  811. $validator = Validator::make(
  812. $request->all(),
  813. [
  814. 'email' => 'required|email|unique:user',
  815. ],
  816. [
  817. 'email.required' => trans('auth.email_null'),
  818. 'email.email' => trans('auth.email_legitimate'),
  819. 'email.unique' => trans('auth.email_exist'),
  820. ]
  821. );
  822. $email = $request->input('email');
  823. if ($validator->fails()) {
  824. return Response::json(
  825. [
  826. 'status' => 'fail',
  827. 'message' => $validator->getMessageBag()->first(),
  828. ]
  829. );
  830. }
  831. // 校验域名邮箱黑白名单
  832. if (sysConfig('is_email_filtering')) {
  833. $result = $this->emailChecker($email);
  834. if ($result !== false) {
  835. return $result;
  836. }
  837. }
  838. // 是否开启注册发送验证码
  839. if (sysConfig('is_activate_account') != 1) {
  840. return Response::json(
  841. ['status' => 'fail', 'message' => trans('auth.captcha_close')]
  842. );
  843. }
  844. // 防刷机制
  845. if (Cache::has('send_verify_code_' . md5(getClientIP()))) {
  846. return Response::json(
  847. ['status' => 'fail', 'message' => trans('auth.register_anti')]
  848. );
  849. }
  850. // 发送邮件
  851. $code = Str::random(6);
  852. $logId = Helpers::addNotificationLog(
  853. '发送注册验证码',
  854. '验证码:' . $code,
  855. 1,
  856. $email
  857. );
  858. Mail::to($email)->send(new sendVerifyCode($logId, $code));
  859. $this->addVerifyCode($email, $code);
  860. Cache::put(
  861. 'send_verify_code_' . md5(getClientIP()),
  862. getClientIP(),
  863. Minute
  864. );
  865. return Response::json(
  866. ['status' => 'success', 'message' => trans('auth.captcha_send')]
  867. );
  868. }
  869. // 生成注册验证码
  870. private function addVerifyCode($email, $code): void
  871. {
  872. $verify = new VerifyCode();
  873. $verify->address = $email;
  874. $verify->code = $code;
  875. $verify->status = 0;
  876. $verify->save();
  877. }
  878. // 公开的邀请码列表
  879. public function free()
  880. {
  881. $view['inviteList'] = Invite::whereInviterId(0)
  882. ->whereStatus(0)
  883. ->paginate();
  884. return view('auth.free', $view);
  885. }
  886. // 切换语言
  887. public function switchLang($locale): RedirectResponse
  888. {
  889. Session::put("locale", $locale);
  890. return Redirect::back();
  891. }
  892. }