Ejemplo n.º 1
0
 public static function getInstance()
 {
     if (null === self::$_instance) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Ejemplo n.º 2
0
 public function loginAction()
 {
     $form = new Application_Form_Users();
     $this->view->form = $form;
     $modelConfig = new Application_Model_Configs();
     $tutorConfig = $modelConfig->getConfigDetail("dang-ky-tim-gia-su");
     $urlRedirect = '/news/detail/id/' . $tutorConfig['ConfigValue'];
     $this->view->urlRedirect = $urlRedirect;
     $getMail = false;
     /* Proccess data post*/
     if ($this->_request->isPost()) {
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             $data = $_POST;
             $data['Password'] = '';
             $data['LastName'] = '';
             $data['FirstName'] = '';
             $data['UserName'] = '******';
             $data['LastLogin'] = Zend_Date::now()->toString(DATE_FORMAT_DATABASE);
             if ($id = $this->_model->add($data)) {
                 setcookie('user_id', $id, time() + 7200, '/');
                 $this->_sendEmailToUser('', $data['Email'], $urlRedirect);
                 $this->_redirect($urlRedirect);
             }
         } else {
             $msgVN = array("is required and can't be empty" => 'Không được để trống', 'is no valid email address in the basic format local-part@hostname' => 'Email không hợp lệ', "record matching '" . $_POST['Email'] . "' was found" => "'" . $_POST['Email'] . "' đã tồn tại");
             $messageStatus = 'danger/Có lỗi xảy ra. Chú ý thông tin những ô sau đây:';
             $messages = array();
             foreach ($form->getMessages() as $fieldName => $message) {
                 $message = end($message);
                 $key = substr(strstr($message, " "), 1);
                 if (in_array($key, array_keys($msgVN))) {
                     $message = $msgVN[$key];
                 }
                 $messages[$fieldName] = $message;
             }
             $this->view->messages = $messages;
             $this->view->messageStatus = $messageStatus;
         }
     }
     // Here the response of the providers are registered
     if (null !== ($provider = $this->_request->getParam('provider', null))) {
         $auth = Tbs_Auth::getInstance();
         switch ($provider) {
             case "facebook":
                 if (null !== ($code = $this->_request->getParam('code', null))) {
                     $adapter = new Tbs_Auth_Adapter_Facebook($code);
                     $result = $auth->authenticate($adapter);
                 }
                 if (null !== ($error = $this->_request->getParam('error', null))) {
                     throw new Zend_Controller_Action_Exception('Facebook login failed, response is: ' . $error);
                 }
                 $getMail = true;
                 break;
             case "twitter":
                 if (null !== ($oauthToken = $this->_request->getParam('oauth_token', null))) {
                     $adapter = new Tbs_Auth_Adapter_Twitter($_GET);
                     $result = $auth->authenticate($adapter);
                 }
                 break;
             case "google":
                 if (null !== ($code = $this->_request->getParam('code', null))) {
                     $adapter = new Tbs_Auth_Adapter_Google($code);
                     $result = $auth->authenticate($adapter);
                 }
                 if (null !== ($error = $this->_request->getParam('error', null))) {
                     throw new Zend_Controller_Action_Exception('Google login failed, response is: ' . $error);
                 }
                 $getMail = true;
                 break;
         }
         // What to do when invalid
         if (isset($result) && !$result->isValid()) {
             $auth->clearIdentity($this->_request->getParam('provider'));
             throw new Zend_Controller_Action_Exception('Login failed');
         } else {
             $identity = Zend_Registry::get('MultipleIdentities');
             if (isset($identity) && !empty($identity)) {
                 $data = unserialize($identity->identityContainer);
                 if (count($data)) {
                     $info = $this->_registerUser($data);
                     $cinfo = count($info);
                     if ($getMail) {
                         $name = '';
                         $email = '';
                         if ($cinfo) {
                             $name = isset($info[0]) ? $info[0] : '';
                             $email = isset($info[1]) ? $info[1] : '';
                             $this->_sendEmailToUser($name, $email, $urlRedirect);
                         } else {
                             $this->view->messageStatus = 'danger/Tài khoản này đã có người đăng ký.';
                         }
                     } else {
                         $this->view->messageStatus = 'danger/Tài khoản này đã có người đăng ký.';
                     }
                     if ($cinfo) {
                         setcookie('user_id', isset($info[2]) ? $info[2] : $info[0], time() + 7200, '/');
                     }
                     $this->view->msg = 'success';
                 } else {
                     $this->view->msg = 'failed';
                 }
             }
         }
     } else {
         // Normal login page
         $this->view->googleAuthUrl = Tbs_Auth_Adapter_Google::getAuthorizationUrl();
         $this->view->googleAuthUrlOffline = Tbs_Auth_Adapter_Google::getAuthorizationUrl(true);
         $this->view->facebookAuthUrl = Tbs_Auth_Adapter_Facebook::getAuthorizationUrl();
         $this->view->twitterAuthUrl = Tbs_Auth_Adapter_Twitter::getAuthorizationUrl();
     }
 }