AuthController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. 'username' => 'required',
  49. 'password' => 'required'
  50. ], [
  51. 'username.required' => trans('auth.email_null'),
  52. 'password.required' => trans('auth.password_null')
  53. ]);
  54. $username = $request->input('username');
  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(['username' => $username, '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_active_register'] || self::$systemConfig['is_verify_register'])){
  96. Auth::logout(); // 强制销毁会话,因为Auth::attempt的时候会产生会话
  97. return Redirect::back()->withInput()->withErrors(trans('auth.active_tip').'<a href="/activeUser?username='.$username.'" 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|email|unique:user',
  177. 'password' => 'required|min:6',
  178. 'confirmPassword' => 'required|same:password',
  179. 'term' => 'accepted'
  180. ], [
  181. 'username.required' => trans('auth.email_null'),
  182. 'username.email' => trans('auth.email_legitimate'),
  183. 'username.unique' => trans('auth.email_exist'),
  184. 'password.required' => trans('auth.password_null'),
  185. 'password.min' => trans('auth.password_limit'),
  186. 'confirmPassword.required' => trans('auth.confirm_password'),
  187. 'confirmPassword.same' => trans('auth.password_same'),
  188. 'term.accepted' => trans('auth.unaccepted')
  189. ]);
  190. $username = $request->input('username');
  191. $password = $request->input('password');
  192. $register_token = $request->input('register_token');
  193. $code = $request->input('code');
  194. $verify_code = $request->input('verify_code');
  195. $captcha = $request->input('captcha');
  196. $aff = intval($request->input('aff'));
  197. // 防止重复提交
  198. if($register_token != Session::get('register_token')){
  199. return Redirect::back()->withInput()->withErrors(trans('auth.repeat_request'));
  200. }else{
  201. Session::forget('register_token');
  202. }
  203. // 是否开启注册
  204. if(!self::$systemConfig['is_register']){
  205. return Redirect::back()->withErrors(trans('auth.register_close'));
  206. }
  207. // 校验域名邮箱黑白名单
  208. if(self::$systemConfig['sensitiveType']){
  209. // 校验域名邮箱是否在黑名单中
  210. $sensitiveWords = $this->sensitiveWords(1);
  211. $usernameSuffix = explode('@', $username); // 提取邮箱后缀
  212. if(in_array(strtolower($usernameSuffix[1]), $sensitiveWords)){
  213. return Redirect::back()->withInput()->withErrors(trans('auth.email_banned'));
  214. }
  215. }else{
  216. $sensitiveWords = $this->sensitiveWords(2);
  217. $usernameSuffix = explode('@', $username); // 提取邮箱后缀
  218. if(!in_array(strtolower($usernameSuffix[1]), $sensitiveWords)){
  219. return Redirect::back()->withInput()->withErrors(trans('auth.email_invalid'));
  220. }
  221. }
  222. // 如果需要邀请注册
  223. if(self::$systemConfig['is_invite_register']){
  224. // 必须使用邀请码
  225. if(self::$systemConfig['is_invite_register'] == 2 && !$code){
  226. return Redirect::back()->withInput()->withErrors(trans('auth.code_null'));
  227. }
  228. // 校验邀请码合法性
  229. if($code){
  230. $codeEnable = Invite::query()->where('code', $code)->where('status', 0)->doesntExist();
  231. if($codeEnable){
  232. return Redirect::back()->withInput($request->except(['code']))->withErrors(trans('auth.code_error'));
  233. }
  234. }
  235. }
  236. // 如果开启注册发送验证码
  237. if(self::$systemConfig['is_verify_register']){
  238. if(!$verify_code){
  239. return Redirect::back()->withInput($request->except(['verify_code']))->withErrors(trans('auth.captcha_null'));
  240. }else{
  241. $verifyCode = VerifyCode::query()->where('username', $username)->where('code', $verify_code)->where('status', 0)->first();
  242. if(!$verifyCode){
  243. return Redirect::back()->withInput($request->except(['verify_code']))->withErrors(trans('auth.captcha_overtime'));
  244. }
  245. $verifyCode->status = 1;
  246. $verifyCode->save();
  247. }
  248. }
  249. // 是否校验验证码
  250. switch(self::$systemConfig['is_captcha']){
  251. case 1: // 默认图形验证码
  252. if(!Captcha::check($captcha)){
  253. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_error'));
  254. }
  255. break;
  256. case 2: // Geetest
  257. $result = $this->validate($request, [
  258. 'geetest_challenge' => 'required|geetest'
  259. ], [
  260. 'geetest' => trans('auth.captcha_fail')
  261. ]);
  262. if(!$result){
  263. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  264. }
  265. break;
  266. case 3: // Google reCAPTCHA
  267. $result = $this->validate($request, [
  268. 'g-recaptcha-response' => 'required|NoCaptcha'
  269. ]);
  270. if(!$result){
  271. return Redirect::back()->withInput()->withErrors(trans('auth.captcha_fail'));
  272. }
  273. break;
  274. default: // 不启用验证码
  275. break;
  276. }
  277. // 24小时内同IP注册限制
  278. if(self::$systemConfig['register_ip_limit']){
  279. if(Cache::has($cacheKey)){
  280. $registerTimes = Cache::get($cacheKey);
  281. if($registerTimes >= self::$systemConfig['register_ip_limit']){
  282. return Redirect::back()->withInput($request->except(['code']))->withErrors(trans('auth.register_anti'));
  283. }
  284. }
  285. }
  286. // 获取可用端口
  287. $port = self::$systemConfig['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
  288. if($port > self::$systemConfig['max_port']){
  289. return Redirect::back()->withInput()->withErrors(trans('auth.register_close'));
  290. }
  291. // 获取aff
  292. $affArr = $this->getAff($code, $aff);
  293. $referral_uid = $affArr['referral_uid'];
  294. $transfer_enable = 1048576*(self::$systemConfig['default_traffic']+($referral_uid? self::$systemConfig['referral_traffic'] : 0));
  295. // 创建新用户
  296. $uid = Helpers::addUser($username, Hash::make($password), $transfer_enable, self::$systemConfig['default_days'], $referral_uid);
  297. // 注册失败,抛出异常
  298. if(!$uid){
  299. return Redirect::back()->withInput()->withErrors(trans('auth.register_fail'));
  300. }
  301. // 生成订阅码
  302. $subscribe = new UserSubscribe();
  303. $subscribe->user_id = $uid;
  304. $subscribe->code = Helpers::makeSubscribeCode();
  305. $subscribe->times = 0;
  306. $subscribe->save();
  307. // 注册次数+1
  308. if(Cache::has($cacheKey)){
  309. Cache::increment($cacheKey);
  310. }else{
  311. Cache::put($cacheKey, 1, 86400); // 24小时
  312. }
  313. // 初始化默认标签
  314. if(strlen(self::$systemConfig['initial_labels_for_user'])){
  315. $labels = explode(',', self::$systemConfig['initial_labels_for_user']);
  316. foreach($labels as $label){
  317. $userLabel = new UserLabel();
  318. $userLabel->user_id = $uid;
  319. $userLabel->label_id = $label;
  320. $userLabel->save();
  321. }
  322. }
  323. // 更新邀请码
  324. if(self::$systemConfig['is_invite_register'] && $affArr['code_id']){
  325. Invite::query()->where('id', $affArr['code_id'])->update(['fuid' => $uid, 'status' => 1]);
  326. }
  327. // 清除邀请人Cookie
  328. Cookie::unqueue('register_aff');
  329. // 邮箱验证码关闭情况下,发送激活邮件
  330. if(!self::$systemConfig['is_verify_register'] && self::$systemConfig['is_active_register']){
  331. // 生成激活账号的地址
  332. $token = $this->addVerifyUrl($uid, $username);
  333. $activeUserUrl = self::$systemConfig['website_url'].'/active/'.$token;
  334. $logId = Helpers::addEmailLog($username, '注册激活', '请求地址:'.$activeUserUrl);
  335. Mail::to($username)->send(new activeUser($logId, $activeUserUrl));
  336. Session::flash('regSuccessMsg', trans('auth.register_active_tip'));
  337. }else{
  338. // 则直接给推荐人加流量
  339. if($referral_uid){
  340. $referralUser = User::query()->where('id', $referral_uid)->first();
  341. if($referralUser){
  342. if($referralUser->expire_time >= date('Y-m-d')){
  343. User::query()->where('id', $referral_uid)->increment('transfer_enable', self::$systemConfig['referral_traffic']*1048576);
  344. }
  345. }
  346. }
  347. User::query()->where('id', $uid)->update(['status' => 1, 'enable' => 1]);
  348. Session::flash('regSuccessMsg', trans('auth.register_success'));
  349. }
  350. return Redirect::to('login')->withInput();
  351. }else{
  352. $view['emailList'] = self::$systemConfig['sensitiveType']? NULL : SensitiveWords::query()->where('type', 2)->get();
  353. Session::put('register_token', makeRandStr(16));
  354. return Response::view('auth.register', $view);
  355. }
  356. }
  357. /**
  358. * 获取AFF
  359. *
  360. * @param string $code 邀请码
  361. * @param int $aff URL中的aff参数
  362. *
  363. * @return array
  364. */
  365. private function getAff($code = '', $aff = NULL)
  366. {
  367. // 邀请人ID
  368. $referral_uid = 0;
  369. // 邀请码ID
  370. $code_id = 0;
  371. // 有邀请码先用邀请码,用谁的邀请码就给谁返利
  372. if($code){
  373. $inviteCode = Invite::query()->where('code', $code)->where('status', 0)->first();
  374. if($inviteCode){
  375. $referral_uid = $inviteCode->uid;
  376. $code_id = $inviteCode->id;
  377. }
  378. }
  379. // 没有用邀请码或者邀请码是管理员生成的,则检查cookie或者url链接
  380. if(!$referral_uid){
  381. // 检查一下cookie里有没有aff
  382. $cookieAff = \Request::hasCookie('register_aff')? \Request::cookie('register_aff') : 0;
  383. if($cookieAff){
  384. $affUser = User::query()->where('id', $cookieAff)->exists();
  385. $referral_uid = $affUser? $cookieAff : 0;
  386. }elseif($aff){ // 如果cookie里没有aff,就再检查一下请求的url里有没有aff,因为有些人的浏览器会禁用了cookie,比如chrome开了隐私模式
  387. $affUser = User::query()->where('id', $aff)->exists();
  388. $referral_uid = $affUser? $aff : 0;
  389. }
  390. }
  391. return [
  392. 'referral_uid' => $referral_uid,
  393. 'code_id' => $code_id
  394. ];
  395. }
  396. // 生成申请的请求地址
  397. private function addVerifyUrl($uid, $username)
  398. {
  399. $token = md5(self::$systemConfig['website_name'].$username.microtime());
  400. $verify = new Verify();
  401. $verify->type = 1;
  402. $verify->user_id = $uid;
  403. $verify->token = $token;
  404. $verify->status = 0;
  405. $verify->save();
  406. return $token;
  407. }
  408. // 重设密码页
  409. public function resetPassword(Request $request)
  410. {
  411. if($request->isMethod('POST')){
  412. // 校验请求
  413. $this->validate($request, [
  414. 'username' => 'required|email'
  415. ], [
  416. 'username.required' => trans('auth.email_null'),
  417. 'username.email' => trans('auth.email_legitimate')
  418. ]);
  419. $username = $request->input('username');
  420. // 是否开启重设密码
  421. if(!self::$systemConfig['is_reset_password']){
  422. return Redirect::back()->withErrors(trans('auth.reset_password_close', ['email' => self::$systemConfig['webmaster_email']]));
  423. }
  424. // 查找账号
  425. $user = User::query()->where('username', $username)->first();
  426. if(!$user){
  427. return Redirect::back()->withErrors(trans('auth.email_notExist'));
  428. }
  429. // 24小时内重设密码次数限制
  430. $resetTimes = 0;
  431. if(Cache::has('resetPassword_'.md5($username))){
  432. $resetTimes = Cache::get('resetPassword_'.md5($username));
  433. if($resetTimes >= self::$systemConfig['reset_password_times']){
  434. return Redirect::back()->withErrors(trans('auth.reset_password_limit', ['time' => self::$systemConfig['reset_password_times']]));
  435. }
  436. }
  437. // 生成取回密码的地址
  438. $token = $this->addVerifyUrl($user->id, $username);
  439. // 发送邮件
  440. $resetPasswordUrl = self::$systemConfig['website_url'].'/reset/'.$token;
  441. $logId = Helpers::addEmailLog($username, '重置密码', '请求地址:'.$resetPasswordUrl);
  442. Mail::to($username)->send(new resetPassword($logId, $resetPasswordUrl));
  443. Cache::put('resetPassword_'.md5($username), $resetTimes+1, 86400);
  444. return Redirect::back()->with('successMsg', trans('auth.reset_password_success_tip'));
  445. }else{
  446. return Response::view('auth.resetPassword');
  447. }
  448. }
  449. // 重设密码
  450. public function reset(Request $request, $token)
  451. {
  452. if(!$token){
  453. return Redirect::to('login');
  454. }
  455. if($request->isMethod('POST')){
  456. $this->validate($request, [
  457. 'password' => 'required|min:6',
  458. 'confirmPassword' => 'required|same:password'
  459. ], [
  460. 'password.required' => trans('auth.password_null'),
  461. 'password.min' => trans('auth.password_limit'),
  462. 'confirmPassword.required' => trans('auth.password_null'),
  463. 'confirmPassword.min' => trans('auth.password_limit'),
  464. 'confirmPassword.same' => trans('auth.password_same'),
  465. ]);
  466. $password = $request->input('password');
  467. // 校验账号
  468. $verify = Verify::type(1)->with('user')->where('token', $token)->first();
  469. if(!$verify){
  470. return Redirect::to('login');
  471. }elseif($verify->status == 1){
  472. return Redirect::back()->withErrors(trans('auth.overtime'));
  473. }elseif($verify->user->status < 0){
  474. return Redirect::back()->withErrors(trans('auth.email_banned'));
  475. }elseif(Hash::check($password, $verify->user->password)){
  476. return Redirect::back()->withErrors(trans('auth.reset_password_same_fail'));
  477. }
  478. // 更新密码
  479. $ret = User::query()->where('id', $verify->user_id)->update(['password' => Hash::make($password)]);
  480. if(!$ret){
  481. return Redirect::back()->withErrors(trans('auth.reset_password_fail'));
  482. }
  483. // 置为已使用
  484. $verify->status = 1;
  485. $verify->save();
  486. return Redirect::back()->with('successMsg', trans('auth.reset_password_new'));
  487. }else{
  488. $verify = Verify::type(1)->where('token', $token)->first();
  489. if(!$verify){
  490. return Redirect::to('login');
  491. }elseif(time()-strtotime($verify->created_at) >= 1800){
  492. // 置为已失效
  493. $verify->status = 2;
  494. $verify->save();
  495. }
  496. // 重新获取一遍verify
  497. $view['verify'] = Verify::type(1)->where('token', $token)->first();
  498. return Response::view('auth.reset', $view);
  499. }
  500. }
  501. // 激活账号页
  502. public function activeUser(Request $request)
  503. {
  504. if($request->isMethod('POST')){
  505. $this->validate($request, [
  506. 'username' => 'required|email|exists:user,username'
  507. ], [
  508. 'username.required' => trans('auth.email_null'),
  509. 'username.email' => trans('auth.email_legitimate'),
  510. 'username.exists' => trans('auth.email_notExist')
  511. ]);
  512. $username = $request->input('username');
  513. // 是否开启账号激活
  514. if(!self::$systemConfig['is_active_register']){
  515. return Redirect::back()->withInput()->withErrors(trans('auth.active_close', ['email' => self::$systemConfig['webmaster_email']]));
  516. }
  517. // 查找账号
  518. $user = User::query()->where('username', $username)->first();
  519. if($user->status < 0){
  520. return Redirect::back()->withErrors(trans('auth.login_ban', ['email' => self::$systemConfig['webmaster_email']]));
  521. }elseif($user->status > 0){
  522. return Redirect::back()->withErrors(trans('auth.email_normal'));
  523. }
  524. // 24小时内激活次数限制
  525. $activeTimes = 0;
  526. if(Cache::has('activeUser_'.md5($username))){
  527. $activeTimes = Cache::get('activeUser_'.md5($username));
  528. if($activeTimes >= self::$systemConfig['active_times']){
  529. return Redirect::back()->withErrors(trans('auth.active_limit', ['time' => self::$systemConfig['webmaster_email']]));
  530. }
  531. }
  532. // 生成激活账号的地址
  533. $token = $this->addVerifyUrl($user->id, $username);
  534. // 发送邮件
  535. $activeUserUrl = self::$systemConfig['website_url'].'/active/'.$token;
  536. $logId = Helpers::addEmailLog($username, '激活账号', '请求地址:'.$activeUserUrl);
  537. Mail::to($username)->send(new activeUser($logId, $activeUserUrl));
  538. Cache::put('activeUser_'.md5($username), $activeTimes+1, 86400);
  539. return Redirect::back()->with('successMsg', trans('auth.register_active_tip'));
  540. }else{
  541. return Response::view('auth.activeUser');
  542. }
  543. }
  544. // 激活账号
  545. public function active($token)
  546. {
  547. if(!$token){
  548. return Redirect::to('login');
  549. }
  550. $verify = Verify::type(1)->with('user')->where('token', $token)->first();
  551. if(!$verify){
  552. return Redirect::to('login');
  553. }elseif(empty($verify->user)){
  554. Session::flash('errorMsg', trans('auth.overtime'));
  555. return Response::view('auth.active');
  556. }elseif($verify->status > 0){
  557. Session::flash('errorMsg', trans('auth.overtime'));
  558. return Response::view('auth.active');
  559. }elseif($verify->user->status != 0){
  560. Session::flash('errorMsg', trans('auth.email_normal'));
  561. return Response::view('auth.active');
  562. }elseif(time()-strtotime($verify->created_at) >= 1800){
  563. Session::flash('errorMsg', trans('auth.overtime'));
  564. // 置为已失效
  565. $verify->status = 2;
  566. $verify->save();
  567. return Response::view('auth.active');
  568. }
  569. // 更新账号状态
  570. $ret = User::query()->where('id', $verify->user_id)->update(['status' => 1]);
  571. if(!$ret){
  572. Session::flash('errorMsg', trans('auth.active_fail'));
  573. return Redirect::back();
  574. }
  575. // 置为已使用
  576. $verify->status = 1;
  577. $verify->save();
  578. // 账号激活后给邀请人送流量
  579. if($verify->user->referral_uid){
  580. $transfer_enable = self::$systemConfig['referral_traffic']*1048576;
  581. User::query()->where('id', $verify->user->referral_uid)->increment('transfer_enable', $transfer_enable, ['enable' => 1]);
  582. }
  583. Session::flash('successMsg', trans('auth.active_success'));
  584. return Response::view('auth.active');
  585. }
  586. // 发送注册验证码
  587. public function sendCode(Request $request)
  588. {
  589. $validator = Validator::make($request->all(), [
  590. 'username' => 'required|email|unique:user'
  591. ], [
  592. 'username.required' => trans('auth.email_null'),
  593. 'username.email' => trans('auth.email_legitimate'),
  594. 'username.unique' => trans('auth.email_exist')
  595. ]);
  596. $username = $request->input('username');
  597. if($validator->fails()){
  598. return Response::json(['status' => 'fail', 'data' => '', 'message' => $validator->getMessageBag()->first()]);
  599. }
  600. // 校验域名邮箱黑白名单
  601. if(self::$systemConfig['sensitiveType']){
  602. // 校验域名邮箱是否在黑名单中
  603. $sensitiveWords = $this->sensitiveWords(1);
  604. $usernameSuffix = explode('@', $username); // 提取邮箱后缀
  605. if(in_array(strtolower($usernameSuffix[1]), $sensitiveWords)){
  606. return Response::json(['status' => 'fail', 'data' => '', 'message' => trans('auth.email_banned')]);
  607. }
  608. }else{
  609. $sensitiveWords = $this->sensitiveWords(2);
  610. $usernameSuffix = explode('@', $username); // 提取邮箱后缀
  611. if(!in_array(strtolower($usernameSuffix[1]), $sensitiveWords)){
  612. return Response::json(['status' => 'fail', 'data' => '', 'message' => trans('auth.email_invalid')]);
  613. }
  614. }
  615. // 是否开启注册发送验证码
  616. if(!self::$systemConfig['is_verify_register']){
  617. return Response::json(['status' => 'fail', 'data' => '', 'message' => trans('auth.captcha_close')]);
  618. }
  619. // 防刷机制
  620. if(Cache::has('send_verify_code_'.md5(getClientIP()))){
  621. return Response::json(['status' => 'fail', 'data' => '', 'message' => trans('auth.register_anti')]);
  622. }
  623. // 发送邮件
  624. $code = makeRandStr(6, TRUE);
  625. $logId = Helpers::addEmailLog($username, '发送注册验证码', '验证码:'.$code);
  626. Mail::to($username)->send(new sendVerifyCode($logId, $code));
  627. $this->addVerifyCode($username, $code);
  628. Cache::put('send_verify_code_'.md5(getClientIP()), getClientIP(), 60);
  629. return Response::json(['status' => 'success', 'data' => '', 'message' => trans('auth.captcha_send')]);
  630. }
  631. // 生成注册验证码
  632. private function addVerifyCode($username, $code)
  633. {
  634. $verify = new VerifyCode();
  635. $verify->username = $username;
  636. $verify->code = $code;
  637. $verify->status = 0;
  638. $verify->save();
  639. }
  640. // 公开的邀请码列表
  641. public function free()
  642. {
  643. $view['inviteList'] = Invite::query()->where('uid', 0)->where('status', 0)->paginate();
  644. return Response::view('auth.free', $view);
  645. }
  646. // 切换语言
  647. public function switchLang($locale)
  648. {
  649. Session::put("locale", $locale);
  650. return Redirect::back();
  651. }
  652. }