AuthController.php 23 KB

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