AuthController.php 24 KB

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