src/AppBundle/Controller/RegisterController.php line 137

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Common\Exception\AbstractException;
  4. use AppBundle\Common\SimpleValidator;
  5. use AppBundle\Common\SmsToolkit;
  6. use Biz\Common\CommonException;
  7. use Biz\Distributor\Util\DistributorCookieToolkit;
  8. use Biz\System\Service\LogService;
  9. use Biz\System\Service\SettingService;
  10. use Biz\User\Service\AuthService;
  11. use Biz\User\Service\MessageService;
  12. use Biz\User\Service\NotificationService;
  13. use Biz\User\Service\UserFieldService;
  14. use Biz\User\UserException;
  15. use Gregwar\Captcha\CaptchaBuilder;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. class RegisterController extends BaseController
  19. {
  20.     public function indexAction(Request $request)
  21.     {
  22.         $user $this->getCurrentUser();
  23.         if ($user->isLogin()) {
  24.             return $this->createMessageResponse('info''你已经登录了'null3000$this->getTargetPath($request));
  25.         }
  26.         $registerEnable $this->getAuthService()->isRegisterEnabled();
  27.         if (!$registerEnable) {
  28.             return $this->createMessageResponse('info''注册已关闭,请联系管理员'null3000$this->getTargetPath($request));
  29.         }
  30.         if ('POST' === $request->getMethod()) {
  31.             try {
  32.                 $registration $request->request->all();
  33.                 unset($registration['type']);
  34.                 if (isset($registration['emailOrMobile']) && SimpleValidator::mobile($registration['emailOrMobile'])) {
  35.                     $registration['verifiedMobile'] = $registration['emailOrMobile'];
  36.                 }
  37.                 $isUserTermsOpened 'opened' === $this->getSettingService()->node('auth.user_terms') ? true false;
  38.                 $isPrivacyPolicyOpened 'opened' === $this->getSettingService()->node('auth.privacy_policy') ? true false;
  39.                 if (($isUserTermsOpened || $isPrivacyPolicyOpened) && 'on' != $registration['agree_policy']) {
  40.                     return $this->createMessageResponse('info''请先勾选相关条款或政策');
  41.                 }
  42.                 $registration['mobile'] = isset($registration['verifiedMobile']) ? $registration['verifiedMobile'] : '';
  43.                 $registration['createdIp'] = $request->getClientIp();
  44.                 $authSettings $this->getSettingService()->get('auth', []);
  45.                 //拖动校验
  46.                 $this->dragCaptchaValidator($registration$authSettings);
  47.                 //手机校验码
  48.                 if ($this->smsCodeValidator($authSettings$registration)) {
  49.                     $request->request->add(array_merge($request->request->all(), ['mobile' => $registration['mobile']]));
  50.                     list($result$sessionField$requestField) = SmsToolkit::smsCheck($request$scenario 'sms_registration');
  51.                     if ($result) {
  52.                         $registration['verifiedMobile'] = $sessionField['to'];
  53.                     } else {
  54.                         return $this->createMessageResponse('info''手机号码和短信验证码不匹配,请重新注册');
  55.                     }
  56.                 }
  57.                 //获取POST参数验证码
  58.                 if (!isset($registration['invitedCode']) || empty($registration['invitedCode'])) {
  59.                     //获取GET地址的邀请码
  60.                     $registration['invitedCode'] = trim($request->get('inviteCode'''));
  61.                 }
  62.                 $registration['createdIp'] = $request->getClientIp();
  63.                 $registration['registeredWay'] = 'web';
  64.                 if ($this->isPluginInstalled('Drp')) {
  65.                     $registration DistributorCookieToolkit::setCookieTokenToFields($request$registrationDistributorCookieToolkit::USER);
  66.                 }
  67.                 $user $this->getAuthService()->register($registration);
  68.                 if (($authSettings
  69.                         && isset($authSettings['email_enabled'])
  70.                         && 'closed' === $authSettings['email_enabled'])
  71.                     || !$this->isEmptyVeryfyMobile($user)
  72.                 ) {
  73.                     $this->authenticateUser($user);
  74.                 }
  75.                 $goto $this->generateUrl('register_submited', [
  76.                     'id' => $user['id'],
  77.                     'hash' => $this->makeHash($user),
  78.                     'goto' => $this->getTargetPath($request),
  79.                 ]);
  80.                 if ($this->getAuthService()->hasPartnerAuth()) {
  81.                     $currentUser $this->getCurrentUser();
  82.                     if (!$currentUser->isLogin()) {
  83.                         $this->authenticateUser($user);
  84.                     }
  85.                     $goto $this->generateUrl('partner_login', ['goto' => $goto]);
  86.                 }
  87.                 $response $this->redirect($this->generateUrl('register_success', ['goto' => $goto]));
  88.                 $response DistributorCookieToolkit::clearCookieToken(
  89.                     $request,
  90.                     $response,
  91.                     ['checkedType' => DistributorCookieToolkit::USER]
  92.                 );
  93.                 return $response;
  94.             } catch (AbstractException $se) {
  95.                 $this->setFlashMessage('danger'$this->trans($se->getMessage()));
  96.             } catch (\Exception $e) {
  97.                 return $this->createMessageResponse('error'$this->trans($e->getMessage()));
  98.             }
  99.         }
  100.         // invitedCode这里为 被邀请码
  101.         $invitedCode $this->setInviteCode($request);
  102.         $inviteUser = empty($invitedCode) ? [] : $this->getUserService()->getUserByInviteCode($invitedCode);
  103.         if ($this->getWebExtension()->isWechatLoginBind()) {
  104.             return $this->redirect($this->generateUrl('login_bind', ['type' => 'weixinmob''_target_path' => $this->getTargetPath($request)]));
  105.         }
  106.         return $this->render('register/index.html.twig', [
  107.             'isRegisterEnabled' => $registerEnable,
  108.             'registerSort' => [],
  109.             'inviteUser' => $inviteUser,
  110.             '_target_path' => $this->getTargetPath($request),
  111.         ]);
  112.     }
  113.     private function setInviteCode($request)
  114.     {
  115.         // invitedCode这里为 被邀请码
  116.         $invitedCode $request->query->get('inviteCode''');
  117.         $inviteSetting $this->getSettingService()->get('invite');
  118.         if (empty($inviteSetting['invite_code_setting'])) {
  119.             $invitedCode '';
  120.         }
  121.         $this->get('session')->set('invitedCode'$invitedCode);
  122.         return $invitedCode;
  123.     }
  124.     public function successAction(Request $request)
  125.     {
  126.         $goto $request->query->get('goto');
  127.         if (empty($goto)) {
  128.             $goto $this->generateUrl('homepage');
  129.         }
  130.         return $this->createMessageResponse('info''正在跳转页面,请稍等......''注册成功'1$goto);
  131.     }
  132.     protected function isMobileRegister($registration)
  133.     {
  134.         if (isset($registration['emailOrMobile']) && !empty($registration['emailOrMobile'])) {
  135.             if (SimpleValidator::mobile($registration['emailOrMobile'])) {
  136.                 return true;
  137.             }
  138.         } elseif (isset($registration['mobile']) && !empty($registration['mobile'])) {
  139.             if (SimpleValidator::mobile($registration['mobile'])) {
  140.                 return true;
  141.             }
  142.         }
  143.         return false;
  144.     }
  145.     protected function isEmptyVeryfyMobile($user)
  146.     {
  147.         if (isset($user['verifiedMobile']) && !empty($user['verifiedMobile'])) {
  148.             return false;
  149.         }
  150.         return true;
  151.     }
  152.     public function userTermsAction(Request $request)
  153.     {
  154.         $setting $this->getSettingService()->get('auth', []);
  155.         return $this->render('register/user-terms.html.twig', [
  156.             'userTerms' => $setting['user_terms_body'],
  157.         ]);
  158.     }
  159.     public function privacyPolicyAction(Request $request)
  160.     {
  161.         $setting $this->getSettingService()->get('auth', []);
  162.         $privacyPolicyBody = empty($setting['privacy_policy_body']) ? '' $setting['privacy_policy_body'];
  163.         return $this->render('register/privacy-policy.html.twig', [
  164.             'privacyPolicy' => $privacyPolicyBody,
  165.         ]);
  166.     }
  167.     public function emailSendAction(Request $request$id$hash)
  168.     {
  169.         $user $this->checkHash($id$hash);
  170.         if (empty($user)) {
  171.             return $this->createJsonResponse(false);
  172.         }
  173.         $token $this->getUserService()->makeToken('email-verify'$user['id'], strtotime('+1 day'));
  174.         $this->sendVerifyEmail($token$user);
  175.         return $this->createJsonResponse(true);
  176.     }
  177.     public function submitedAction(Request $request$id$hash)
  178.     {
  179.         $user $this->checkHash($id$hash);
  180.         if (empty($user)) {
  181.             $this->createNewException(UserException::NOTFOUND_USER());
  182.         }
  183.         $auth $this->getSettingService()->get('auth');
  184.         if (!empty($user['verifiedMobile'])) {
  185.             return $this->redirect(htmlspecialchars($this->getTargetPath($request)));
  186.         }
  187.         if ($auth && 'mobile' !== $auth['register_mode']
  188.             && array_key_exists('email_enabled'$auth)
  189.             && ('opened' === $auth['email_enabled'])
  190.         ) {
  191.             return $this->render('register/email-verify.html.twig', [
  192.                 'user' => $user,
  193.                 'hash' => $hash,
  194.                 'emailLoginUrl' => $this->getEmailLoginUrl($user['email']),
  195.                 '_target_path' => htmlspecialchars($this->getTargetPath($request)),
  196.             ]);
  197.         }
  198.         $this->authenticateUser($user);
  199.         return $this->redirect(htmlspecialchars($this->getTargetPath($request)));
  200.     }
  201.     public function emailVerifyAction(Request $request$token)
  202.     {
  203.         $token $this->getUserService()->getToken('email-verify'$token);
  204.         if (empty($token)) {
  205.             $currentUser $this->getCurrentUser();
  206.             if (empty($currentUser) || == $currentUser['id']) {
  207.                 return $this->render('register/email-verify-error.html.twig');
  208.             }
  209.             return $this->redirect($this->generateUrl('settings'));
  210.         }
  211.         $user $this->getUserService()->getUser($token['userId']);
  212.         if (empty($user)) {
  213.             return $this->createNotFoundException();
  214.         }
  215.         $this->authenticateUser($user);
  216.         $this->getUserService()->setEmailVerified($user['id']);
  217.         if ('POST' === strtoupper($request->getMethod())) {
  218.             $this->getUserService()->deleteToken('email-verify'$token['token']);
  219.             return $this->createJsonResponse(true);
  220.         }
  221.         return $this->render('register/email-verify-success.html.twig', [
  222.             'token' => $token,
  223.         ]);
  224.     }
  225.     public function resetEmailAction(Request $request$id$hash)
  226.     {
  227.         $user $this->checkHash($id$hash);
  228.         if ($request->isMethod('post')) {
  229.             $password $request->request->get('password');
  230.             $email $request->request->get('email');
  231.             if ($user['email'] !== $email) {
  232.                 $this->createNewException(UserException::NOT_MATCH_BIND_EMAIL());
  233.             }
  234.             $user $this->getUserService()->getUserByEmail($email);
  235.             if (!$this->getUserService()->verifyPassword($user['id'], $password)) {
  236.                 $this->setFlashMessage('danger''site.incorrect.password');
  237.             } else {
  238.                 $token $this->getUserService()->makeToken('email-reset'$user['id'], strtotime('+10 minutes'), [
  239.                     'password' => $password,
  240.                 ]);
  241.                 return $this->render('register/reset-email-step2.html.twig', [
  242.                     'token' => $token,
  243.                 ]);
  244.             }
  245.         }
  246.         if (empty($user)) {
  247.             $this->createNewException(UserException::NOTFOUND_USER());
  248.         }
  249.         return $this->render('register/reset-email-step1.html.twig', [
  250.             'id' => $id,
  251.             'hash' => $hash,
  252.             'user' => $user,
  253.         ]);
  254.     }
  255.     public function resetEmailVerifyAction(Request $request)
  256.     {
  257.         $newEmail $request->request->get('email');
  258.         if (empty($newEmail)) {
  259.             $this->createNewException(CommonException::ERROR_PARAMETER_MISSING());
  260.         }
  261.         $token $request->request->get('token');
  262.         $token $this->getUserService()->getToken('email-reset'$token);
  263.         if (empty($token)) {
  264.             return $this->createNotFoundException('token已失效');
  265.         }
  266.         $user $this->getUserService()->getUser($token['userId']);
  267.         if (empty($user)) {
  268.             $this->createNewException(UserException::NOTFOUND_USER());
  269.         }
  270.         $authSettings $this->getSettingService()->get('auth', []);
  271.         $dragCaptchaToken $request->request->get('dragCaptchaToken');
  272.         //拖动校验
  273.         $this->dragCaptchaValidator(['dragCaptchaToken' => $dragCaptchaToken], $authSettings);
  274.         $this->getAuthService()->changeEmail($user['id'], $token['data']['password'], $newEmail);
  275.         $user $this->getUserService()->getUser($user['id']);
  276.         return $this->redirect($this->generateUrl('register_submited', [
  277.             'id' => $user['id'],
  278.             'hash' => $this->makeHash($user),
  279.             'goto' => $this->generateUrl('homepage'),
  280.         ]));
  281.     }
  282.     public function resetEmailCheckAction(Request $request)
  283.     {
  284.         $email $request->query->get('value');
  285.         $email str_replace('!''.'$email);
  286.         $user $this->getUserService()->getUserByEmail($email);
  287.         if (empty($user)) {
  288.             $response = ['success' => false'message' => '该Email不存在'];
  289.         } else {
  290.             $response = ['success' => true'message' => ''];
  291.         }
  292.         return $this->createJsonResponse($response);
  293.     }
  294.     protected function makeHash($user)
  295.     {
  296.         $string $user['id'].$user['email'].$this->container->getParameter('secret');
  297.         return md5($string);
  298.     }
  299.     protected function checkHash($userId$hash)
  300.     {
  301.         $user $this->getUserService()->getUser($userId);
  302.         if (empty($user)) {
  303.             return false;
  304.         }
  305.         if ($this->makeHash($user) !== $hash) {
  306.             return false;
  307.         }
  308.         return $user;
  309.     }
  310.     public function emailCheckAction(Request $request)
  311.     {
  312.         $email $request->query->get('value');
  313.         $email str_replace('!''.'$email);
  314.         list($result$message) = $this->getAuthService()->checkEmail($email);
  315.         return $this->validateResult($result$message);
  316.     }
  317.     public function mobileCheckAction(Request $request)
  318.     {
  319.         $mobile $request->query->get('value');
  320.         list($result$message) = $this->getAuthService()->checkMobile($mobile);
  321.         return $this->validateResult($result$message);
  322.     }
  323.     public function emailOrMobileCheckAction(Request $request)
  324.     {
  325.         $emailOrMobile $request->query->get('value');
  326.         $emailOrMobile str_replace('!''.'$emailOrMobile);
  327.         list($result$message) = $this->getAuthService()->checkEmailOrMobile($emailOrMobile);
  328.         return $this->validateResult($result$message);
  329.     }
  330.     protected function validateResult($result$message)
  331.     {
  332.         $response true;
  333.         if ('success' !== $result) {
  334.             $response $message;
  335.         }
  336.         return $this->createJsonResponse($response);
  337.     }
  338.     public function nicknameCheckAction(Request $request)
  339.     {
  340.         $nickname $request->query->get('value');
  341.         $randomName $request->query->get('randomName');
  342.         list($result$message) = $this->getAuthService()->checkUsername($nickname$randomName);
  343.         return $this->validateResult($result$message);
  344.     }
  345.     public function invitecodeCheckAction(Request $request)
  346.     {
  347.         $inviteCode $request->query->get('value');
  348.         $user $this->getUserService()->getUserByInviteCode($inviteCode);
  349.         if (empty($user)) {
  350.             return $this->validateResult('false''邀请码不正确');
  351.         }
  352.         return $this->validateResult('success''');
  353.     }
  354.     public function captchaModalAction()
  355.     {
  356.         return $this->render('register/captcha-modal.html.twig', []);
  357.     }
  358.     public function captchaCheckAction(Request $request)
  359.     {
  360.         $captchaFilledByUser strtolower($request->query->get('value'));
  361.         if ($request->getSession()->get('captcha_code') == $captchaFilledByUser) {
  362.             $response = ['success' => true'message' => '验证码正确'];
  363.         } else {
  364.             $request->getSession()->set('captcha_code'mt_rand(0999999999));
  365.             $response = ['success' => false'message' => '验证码错误'];
  366.         }
  367.         return $this->createJsonResponse($response);
  368.     }
  369.     public function getEmailLoginUrl($email)
  370.     {
  371.         $host substr($emailstrpos($email'@') + 1);
  372.         if ('hotmail.com' === $host) {
  373.             return 'http://www.'.$host;
  374.         }
  375.         if ('gmail.com' === $host) {
  376.             return 'http://mail.google.com';
  377.         }
  378.         return 'http://mail.'.$host;
  379.     }
  380.     public function analysisAction(Request $request)
  381.     {
  382.         return $this->render('register/analysis.html.twig', []);
  383.     }
  384.     public function agreementAction()
  385.     {
  386.         return $this->render('register/agreement.html.twig');
  387.     }
  388.     public function captchaAction(Request $request)
  389.     {
  390.         $imgBuilder = new CaptchaBuilder();
  391.         $imgBuilder->build($width 150$height 32$font null);
  392.         $request->getSession()->set('captcha_code'strtolower($imgBuilder->getPhrase()));
  393.         ob_start();
  394.         $imgBuilder->output();
  395.         $str ob_get_clean();
  396.         $imgBuilder null;
  397.         $headers = [
  398.             'Content-type' => 'image/jpeg',
  399.             'Content-Disposition' => 'inline; filename="'.'reg_captcha.jpg'.'"', ];
  400.         return new Response($str200$headers);
  401.     }
  402.     protected function sendRegisterMessage($user)
  403.     {
  404.         $senderUser = [];
  405.         $auth $this->getSettingService()->get('auth', []);
  406.         if (empty($auth['welcome_enabled'])) {
  407.             return false;
  408.         }
  409.         if ('opened' !== $auth['welcome_enabled']) {
  410.             return false;
  411.         }
  412.         if (empty($auth['welcome_sender'])) {
  413.             return false;
  414.         }
  415.         $senderUser $this->getUserService()->getUserByNickname($auth['welcome_sender']);
  416.         if (empty($senderUser)) {
  417.             return false;
  418.         }
  419.         $welcomeBody $this->getWelcomeBody($user);
  420.         if (empty($welcomeBody)) {
  421.             return true;
  422.         }
  423.         if (strlen($welcomeBody) >= 1000) {
  424.             $welcomeBody $this->getWebExtension()->plainTextFilter($welcomeBody1000);
  425.         }
  426.         $this->getMessageService()->sendMessage($senderUser['id'], $user['id'], $welcomeBody);
  427.         $conversation $this->getMessageService()->getConversationByFromIdAndToId($user['id'], $senderUser['id']);
  428.         $this->getMessageService()->deleteConversation($conversation['id']);
  429.     }
  430.     protected function getWelcomeBody($user)
  431.     {
  432.         $site $this->getSettingService()->get('site', []);
  433.         $valuesToBeReplace = ['{{nickname}}''{{sitename}}''{{siteurl}}'];
  434.         $valuesToReplace = [$user['nickname'], $site['name'], $site['url']];
  435.         $welcomeBody $this->setting('auth.welcome_body''注册欢迎的内容');
  436.         return str_replace($valuesToBeReplace$valuesToReplace$welcomeBody);
  437.     }
  438.     protected function sendVerifyEmail($token$user)
  439.     {
  440.         try {
  441.             $site $this->getSettingService()->get('site', []);
  442.             $verifyurl $this->getHttpHost().'/register/email/verify/'.$token;
  443.             $mailOptions = [
  444.                 'to' => $user['email'],
  445.                 'template' => 'email_registration',
  446.                 'params' => [
  447.                     'sitename' => $site['name'],
  448.                     'siteurl' => $site['url'],
  449.                     'verifyurl' => $verifyurl,
  450.                     'nickname' => $user['nickname'],
  451.                 ],
  452.             ];
  453.             $mailFactory $this->getBiz()->offsetGet('mail_factory');
  454.             $mail $mailFactory($mailOptions);
  455.             $mail->send();
  456.         } catch (\Exception $e) {
  457.             $this->getLogService()->error('user''register''注册激活邮件发送失败:'.$e->getMessage());
  458.         }
  459.     }
  460.     protected function getWebExtension()
  461.     {
  462.         return $this->container->get('web.twig.extension');
  463.     }
  464.     protected function dragCaptchaValidator($registration$authSettings)
  465.     {
  466.         if (array_key_exists('captcha_enabled'$authSettings) && (== $authSettings['captcha_enabled']) && empty($registration['mobile'])) {
  467.             $biz $this->getBiz();
  468.             $bizDragCaptcha $biz['biz_drag_captcha'];
  469.             $dragcaptchaToken = empty($registration['dragCaptchaToken']) ? '' $registration['dragCaptchaToken'];
  470.             $bizDragCaptcha->check($dragcaptchaToken);
  471.         }
  472.     }
  473.     //validate captcha
  474.     protected function captchaEnabledValidator($authSettings$registrationRequest $request)
  475.     {
  476.         if (array_key_exists('captcha_enabled'$authSettings) && (== $authSettings['captcha_enabled']) && !isset($registration['mobile'])) {
  477.             $captchaCodePostedByUser strtolower($registration['captcha_code']);
  478.             $captchaCode $request->getSession()->get('captcha_code');
  479.             if (!isset($captchaCodePostedByUser) || strlen($captchaCodePostedByUser) < 5) {
  480.                 $this->createNewException(CommonException::FORBIDDEN_DRAG_CAPTCHA_ERROR());
  481.             }
  482.             if (!isset($captchaCode) || strlen($captchaCode) < 5) {
  483.                 $this->createNewException(CommonException::FORBIDDEN_DRAG_CAPTCHA_ERROR());
  484.             }
  485.             if ($captchaCode != $captchaCodePostedByUser) {
  486.                 $request->getSession()->set('captcha_code'mt_rand(0999999999));
  487.                 $this->createNewException(CommonException::FORBIDDEN_DRAG_CAPTCHA_ERROR());
  488.             }
  489.             $request->getSession()->set('captcha_code'mt_rand(0999999999));
  490.         }
  491.     }
  492.     protected function smsCodeValidator($authSettings$registration)
  493.     {
  494.         if (
  495.             in_array($authSettings['register_mode'], ['mobile''email_or_mobile'])
  496.             && isset($registration['mobile']) && !empty($registration['mobile'])
  497.             && '1' == $this->setting('cloud_sms.sms_enabled')
  498.         ) {
  499.             return true;
  500.         }
  501.     }
  502.     /**
  503.      * @return UserFieldService
  504.      */
  505.     protected function getUserFieldService()
  506.     {
  507.         return $this->getBiz()->service('User:UserFieldService');
  508.     }
  509.     /**
  510.      * @return SettingService
  511.      */
  512.     protected function getSettingService()
  513.     {
  514.         return $this->getBiz()->service('System:SettingService');
  515.     }
  516.     /**
  517.      * @return MessageService
  518.      */
  519.     protected function getMessageService()
  520.     {
  521.         return $this->getBiz()->service('User:MessageService');
  522.     }
  523.     /**
  524.      * @return NotificationService
  525.      */
  526.     protected function getNotificationService()
  527.     {
  528.         return $this->getBiz()->service('User:NotificationService');
  529.     }
  530.     /**
  531.      * @return AuthService
  532.      */
  533.     protected function getAuthService()
  534.     {
  535.         return $this->getBiz()->service('User:AuthService');
  536.     }
  537.     /**
  538.      * @return LogService
  539.      */
  540.     protected function getLogService()
  541.     {
  542.         return $this->getBiz()->service('System:LogService');
  543.     }
  544.     /**
  545.      * @return DistributorService
  546.      */
  547.     protected function getDistributorService()
  548.     {
  549.         return $this->getBiz()->service('Distributor:DistributorService');
  550.     }
  551. }