AuthController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. protected static $systemConfig;
  38. function __construct() {
  39. self::$systemConfig = Helpers::systemConfig();
  40. }
  41. // 登录
  42. public function login(Request $request) {
  43. if($request->isMethod('POST')){
  44. $this->validate($request, [
  45. 'email' => 'required',
  46. 'password' => 'required'
  47. ], [
  48. 'email.required' => trans('auth.email_null'),
  49. 'password.required' => trans('auth.password_null')
  50. ]);
  51. $email = $request->input('email');
  52. $password = $request->input('password');
  53. $remember = $request->input('remember');
  54. // 是否校验验证码
  55. $captcha = $this->check_captcha($request);
  56. if($captcha != false){
  57. return $captcha;
  58. }
  59. // 验证账号并创建会话
  60. if(!Auth::attempt(['email' => $email, 'password' => $password], $remember)){
  61. return Redirect::back()->withInput()->withErrors(trans('auth.login_error'));
  62. }
  63. // 校验普通用户账号状态
  64. if(!Auth::user()->is_admin){
  65. if(Auth::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(Auth::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(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. 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. $result = $this->validate($request, [
  105. 'geetest_challenge' => 'required|geetest'
  106. ], [
  107. 'geetest' => trans('auth.captcha_fail')
  108. ]);
  109. if(!$result){
  110. return Redirect::back()->withInput()->withErrors(trans('auth.fail_captcha'));
  111. }
  112. break;
  113. case 3: // Google reCAPTCHA
  114. $result = $this->validate($request, [
  115. 'g-recaptcha-response' => 'required|NoCaptcha'
  116. ]);
  117. if(!$result){
  118. return Redirect::back()->withInput()->withErrors(trans('auth.fail_captcha'));
  119. }
  120. break;
  121. case 4: // hCaptcha
  122. $result = $this->validate($request, [
  123. 'h-captcha-response' => 'required|HCaptcha'
  124. ]);
  125. if(!$result){
  126. return Redirect::back()->withInput()->withErrors(trans('auth.fail_captcha'));
  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. $this->validate($request, [
  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 = 1048576 * (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, 86400); // 24小时
  310. }
  311. // 初始化默认标签
  312. if(strlen(self::$systemConfig['initial_labels_for_user'])){
  313. $labels = explode(',', self::$systemConfig['initial_labels_for_user']);
  314. foreach($labels as $label){
  315. $userLabel = new UserLabel();
  316. $userLabel->user_id = $uid;
  317. $userLabel->label_id = $label;
  318. $userLabel->save();
  319. }
  320. }
  321. // 更新邀请码
  322. if(self::$systemConfig['is_invite_register'] && $affArr['code_id']){
  323. Invite::query()->whereId($affArr['code_id'])->update(['fuid' => $uid, 'status' => 1]);
  324. }
  325. // 清除邀请人Cookie
  326. Cookie::unqueue('register_aff');
  327. // 注册后发送激活码
  328. if(self::$systemConfig['is_activate_account'] == 2){
  329. // 生成激活账号的地址
  330. $token = $this->addVerifyUrl($uid, $email);
  331. $activeUserUrl = self::$systemConfig['website_url'].'/active/'.$token;
  332. $logId = Helpers::addNotificationLog('注册激活', '请求地址:'.$activeUserUrl, 1, $email);
  333. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  334. Session::flash('regSuccessMsg', trans('auth.register_active_tip'));
  335. }else{
  336. // 则直接给推荐人加流量
  337. if($referral_uid){
  338. $referralUser = User::query()->whereId($referral_uid)->first();
  339. if($referralUser){
  340. if($referralUser->expire_time >= date('Y-m-d')){
  341. User::query()
  342. ->whereId($referral_uid)
  343. ->increment('transfer_enable', self::$systemConfig['referral_traffic'] * 1048576);
  344. }
  345. }
  346. }
  347. if(self::$systemConfig['is_activate_account'] == 1){
  348. User::query()->whereId($uid)->update(['status' => 1]);
  349. }
  350. Session::flash('regSuccessMsg', trans('auth.register_success'));
  351. }
  352. return Redirect::to('login')->withInput();
  353. }else{
  354. $view['emailList'] = self::$systemConfig['is_email_filtering'] != 2? false : SensitiveWords::query()
  355. ->whereType(2)
  356. ->get();
  357. Session::put('register_token', makeRandStr(16));
  358. return Response::view('auth.register', $view);
  359. }
  360. }
  361. //邮箱检查
  362. private function emailChecker($email) {
  363. $sensitiveWords = $this->sensitiveWords(self::$systemConfig['is_email_filtering']);
  364. $emailSuffix = explode('@', $email); // 提取邮箱后缀
  365. switch(self::$systemConfig['is_email_filtering']){
  366. // 黑名单
  367. case 1:
  368. if(in_array(strtolower($emailSuffix[1]), $sensitiveWords)){
  369. return Response::json(['status' => 'fail', 'message' => trans('auth.email_banned')]);
  370. }
  371. break;
  372. //白名单
  373. case 2:
  374. if(!in_array(strtolower($emailSuffix[1]), $sensitiveWords)){
  375. return Response::json(['status' => 'fail', 'message' => trans('auth.email_invalid')]);
  376. }
  377. break;
  378. default:
  379. return Response::json(['status' => 'fail', 'message' => trans('auth.email_invalid')]);
  380. }
  381. return false;
  382. }
  383. /**
  384. * 获取AFF
  385. *
  386. * @param string $code 邀请码
  387. * @param int $aff URL中的aff参数
  388. *
  389. * @return array
  390. */
  391. private function getAff($code = '', $aff = null) {
  392. // 邀请人ID
  393. $referral_uid = 0;
  394. // 邀请码ID
  395. $code_id = 0;
  396. // 有邀请码先用邀请码,用谁的邀请码就给谁返利
  397. if($code){
  398. $inviteCode = Invite::query()->whereCode($code)->whereStatus(0)->first();
  399. if($inviteCode){
  400. $referral_uid = $inviteCode->uid;
  401. $code_id = $inviteCode->id;
  402. }
  403. }
  404. // 没有用邀请码或者邀请码是管理员生成的,则检查cookie或者url链接
  405. if(!$referral_uid){
  406. // 检查一下cookie里有没有aff
  407. $cookieAff = \Request::hasCookie('register_aff')? \Request::cookie('register_aff') : 0;
  408. if($cookieAff){
  409. $affUser = User::query()->whereId($cookieAff)->exists();
  410. $referral_uid = $affUser? $cookieAff : 0;
  411. }elseif($aff){ // 如果cookie里没有aff,就再检查一下请求的url里有没有aff,因为有些人的浏览器会禁用了cookie,比如chrome开了隐私模式
  412. $affUser = User::query()->whereId($aff)->exists();
  413. $referral_uid = $affUser? $aff : 0;
  414. }
  415. }
  416. return [
  417. 'referral_uid' => $referral_uid,
  418. 'code_id' => $code_id
  419. ];
  420. }
  421. // 生成申请的请求地址
  422. private function addVerifyUrl($uid, $email) {
  423. $token = md5(self::$systemConfig['website_name'].$email.microtime());
  424. $verify = new Verify();
  425. $verify->type = 1;
  426. $verify->user_id = $uid;
  427. $verify->token = $token;
  428. $verify->status = 0;
  429. $verify->save();
  430. return $token;
  431. }
  432. // 重设密码页
  433. public function resetPassword(Request $request) {
  434. if($request->isMethod('POST')){
  435. // 校验请求
  436. $this->validate($request, [
  437. 'email' => 'required|email'
  438. ], [
  439. 'email.required' => trans('auth.email_null'),
  440. 'email.email' => trans('auth.email_legitimate')
  441. ]);
  442. $email = $request->input('email');
  443. // 是否开启重设密码
  444. if(!self::$systemConfig['is_reset_password']){
  445. return Redirect::back()->withErrors(trans('auth.reset_password_close',
  446. ['email' => self::$systemConfig['webmaster_email']]));
  447. }
  448. // 查找账号
  449. $user = User::query()->whereEmail($email)->first();
  450. if(!$user){
  451. return Redirect::back()->withErrors(trans('auth.email_notExist'));
  452. }
  453. // 24小时内重设密码次数限制
  454. $resetTimes = 0;
  455. if(Cache::has('resetPassword_'.md5($email))){
  456. $resetTimes = Cache::get('resetPassword_'.md5($email));
  457. if($resetTimes >= self::$systemConfig['reset_password_times']){
  458. return Redirect::back()->withErrors(trans('auth.reset_password_limit',
  459. ['time' => self::$systemConfig['reset_password_times']]));
  460. }
  461. }
  462. // 生成取回密码的地址
  463. $token = $this->addVerifyUrl($user->id, $email);
  464. // 发送邮件
  465. $resetPasswordUrl = self::$systemConfig['website_url'].'/reset/'.$token;
  466. $logId = Helpers::addNotificationLog('重置密码', '请求地址:'.$resetPasswordUrl, 1, $email);
  467. Mail::to($email)->send(new resetPassword($logId, $resetPasswordUrl));
  468. Cache::put('resetPassword_'.md5($email), $resetTimes + 1, 86400);
  469. return Redirect::back()->with('successMsg', trans('auth.reset_password_success_tip'));
  470. }else{
  471. return Response::view('auth.resetPassword');
  472. }
  473. }
  474. // 重设密码
  475. public function reset(Request $request, $token) {
  476. if(!$token){
  477. return Redirect::to('login');
  478. }
  479. if($request->isMethod('POST')){
  480. $this->validate($request, [
  481. 'password' => 'required|min:6',
  482. 'confirmPassword' => 'required|same:password'
  483. ], [
  484. 'password.required' => trans('auth.password_null'),
  485. 'password.min' => trans('auth.password_limit'),
  486. 'confirmPassword.required' => trans('auth.password_null'),
  487. 'confirmPassword.min' => trans('auth.password_limit'),
  488. 'confirmPassword.same' => trans('auth.password_same'),
  489. ]);
  490. $password = $request->input('password');
  491. // 校验账号
  492. $verify = Verify::type(1)->with('user')->whereToken($token)->first();
  493. if(!$verify){
  494. return Redirect::to('login');
  495. }elseif($verify->status == 1){
  496. return Redirect::back()->withErrors(trans('auth.overtime'));
  497. }elseif($verify->user->status < 0){
  498. return Redirect::back()->withErrors(trans('auth.email_banned'));
  499. }elseif(Hash::check($password, $verify->user->password)){
  500. return Redirect::back()->withErrors(trans('auth.reset_password_same_fail'));
  501. }
  502. // 更新密码
  503. $ret = User::query()->whereId($verify->user_id)->update(['password' => Hash::make($password)]);
  504. if(!$ret){
  505. return Redirect::back()->withErrors(trans('auth.reset_password_fail'));
  506. }
  507. // 置为已使用
  508. $verify->status = 1;
  509. $verify->save();
  510. return Redirect::back()->with('successMsg', trans('auth.reset_password_new'));
  511. }else{
  512. $verify = Verify::type(1)->whereToken($token)->first();
  513. if(!$verify){
  514. return Redirect::to('login');
  515. }elseif(time() - strtotime($verify->created_at) >= 1800){
  516. // 置为已失效
  517. $verify->status = 2;
  518. $verify->save();
  519. }
  520. // 重新获取一遍verify
  521. $view['verify'] = Verify::type(1)->whereToken($token)->first();
  522. return Response::view('auth.reset', $view);
  523. }
  524. }
  525. // 激活账号页
  526. public function activeUser(Request $request) {
  527. if($request->isMethod('POST')){
  528. $this->validate($request, [
  529. 'email' => 'required|email|exists:user,email'
  530. ], [
  531. 'email.required' => trans('auth.email_null'),
  532. 'email.email' => trans('auth.email_legitimate'),
  533. 'email.exists' => trans('auth.email_notExist')
  534. ]);
  535. $email = $request->input('email');
  536. // 是否开启账号激活
  537. if(self::$systemConfig['is_activate_account'] != 2){
  538. return Redirect::back()->withInput()->withErrors(trans('auth.active_close',
  539. ['email' => self::$systemConfig['webmaster_email']]));
  540. }
  541. // 查找账号
  542. $user = User::query()->whereEmail($email)->first();
  543. if($user->status < 0){
  544. return Redirect::back()->withErrors(trans('auth.login_ban',
  545. ['email' => self::$systemConfig['webmaster_email']]));
  546. }elseif($user->status > 0){
  547. return Redirect::back()->withErrors(trans('auth.email_normal'));
  548. }
  549. // 24小时内激活次数限制
  550. $activeTimes = 0;
  551. if(Cache::has('activeUser_'.md5($email))){
  552. $activeTimes = Cache::get('activeUser_'.md5($email));
  553. if($activeTimes >= self::$systemConfig['active_times']){
  554. return Redirect::back()->withErrors(trans('auth.active_limit',
  555. ['time' => self::$systemConfig['webmaster_email']]));
  556. }
  557. }
  558. // 生成激活账号的地址
  559. $token = $this->addVerifyUrl($user->id, $email);
  560. // 发送邮件
  561. $activeUserUrl = self::$systemConfig['website_url'].'/active/'.$token;
  562. $logId = Helpers::addNotificationLog('激活账号', '请求地址:'.$activeUserUrl, 1, $email);
  563. Mail::to($email)->send(new activeUser($logId, $activeUserUrl));
  564. Cache::put('activeUser_'.md5($email), $activeTimes + 1, 86400);
  565. return Redirect::back()->with('successMsg', trans('auth.register_active_tip'));
  566. }else{
  567. return Response::view('auth.activeUser');
  568. }
  569. }
  570. // 激活账号
  571. public function active($token) {
  572. if(!$token){
  573. return Redirect::to('login');
  574. }
  575. $verify = Verify::type(1)->with('user')->whereToken($token)->first();
  576. if(!$verify){
  577. return Redirect::to('login');
  578. }elseif(empty($verify->user)){
  579. Session::flash('errorMsg', trans('auth.overtime'));
  580. return Response::view('auth.active');
  581. }elseif($verify->status > 0){
  582. Session::flash('errorMsg', trans('auth.overtime'));
  583. return Response::view('auth.active');
  584. }elseif($verify->user->status != 0){
  585. Session::flash('errorMsg', trans('auth.email_normal'));
  586. return Response::view('auth.active');
  587. }elseif(time() - strtotime($verify->created_at) >= 1800){
  588. Session::flash('errorMsg', trans('auth.overtime'));
  589. // 置为已失效
  590. $verify->status = 2;
  591. $verify->save();
  592. return Response::view('auth.active');
  593. }
  594. // 更新账号状态
  595. $ret = User::query()->whereId($verify->user_id)->update(['status' => 1]);
  596. if(!$ret){
  597. Session::flash('errorMsg', trans('auth.active_fail'));
  598. return Redirect::back();
  599. }
  600. // 置为已使用
  601. $verify->status = 1;
  602. $verify->save();
  603. // 账号激活后给邀请人送流量
  604. if($verify->user->referral_uid){
  605. $transfer_enable = self::$systemConfig['referral_traffic'] * 1048576;
  606. User::query()
  607. ->whereId($verify->user->referral_uid)
  608. ->increment('transfer_enable', $transfer_enable, ['enable' => 1]);
  609. }
  610. Session::flash('successMsg', trans('auth.active_success'));
  611. return Response::view('auth.active');
  612. }
  613. // 发送注册验证码
  614. public function sendCode(Request $request) {
  615. $validator = Validator::make($request->all(), [
  616. 'email' => 'required|email|unique:user'
  617. ], [
  618. 'email.required' => trans('auth.email_null'),
  619. 'email.email' => trans('auth.email_legitimate'),
  620. 'email.unique' => trans('auth.email_exist')
  621. ]);
  622. $email = $request->input('email');
  623. if($validator->fails()){
  624. return Response::json(['status' => 'fail', 'message' => $validator->getMessageBag()->first()]);
  625. }
  626. // 校验域名邮箱黑白名单
  627. if(self::$systemConfig['is_email_filtering']){
  628. $result = $this->emailChecker($email);
  629. if($result != false){
  630. return $result;
  631. }
  632. }
  633. // 是否开启注册发送验证码
  634. if(self::$systemConfig['is_activate_account'] != 1){
  635. return Response::json(['status' => 'fail', 'message' => trans('auth.captcha_close')]);
  636. }
  637. // 防刷机制
  638. if(Cache::has('send_verify_code_'.md5(getClientIP()))){
  639. return Response::json(['status' => 'fail', 'message' => trans('auth.register_anti')]);
  640. }
  641. // 发送邮件
  642. $code = makeRandStr(6, true);
  643. $logId = Helpers::addNotificationLog('发送注册验证码', '验证码:'.$code, 1, $email);
  644. Mail::to($email)->send(new sendVerifyCode($logId, $code));
  645. $this->addVerifyCode($email, $code);
  646. Cache::put('send_verify_code_'.md5(getClientIP()), getClientIP(), 60);
  647. return Response::json(['status' => 'success', 'message' => trans('auth.captcha_send')]);
  648. }
  649. // 生成注册验证码
  650. private function addVerifyCode($email, $code) {
  651. $verify = new VerifyCode();
  652. $verify->address = $email;
  653. $verify->code = $code;
  654. $verify->status = 0;
  655. $verify->save();
  656. }
  657. // 公开的邀请码列表
  658. public function free() {
  659. $view['inviteList'] = Invite::query()->whereUid(0)->whereStatus(0)->paginate();
  660. return Response::view('auth.free', $view);
  661. }
  662. // 切换语言
  663. public function switchLang($locale) {
  664. Session::put("locale", $locale);
  665. return Redirect::back();
  666. }
  667. }