AuthController.php 23 KB

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