public function run()
 {
     $uuid = '';
     if (!isset($this->application->parameters['uuid'])) {
         $uuid = '';
     } else {
         $uuid = $this->application->parameters['uuid'];
     }
     if (empty($uuid) || !ApplicationModel_User::validateUuid($uuid)) {
         // unvalid uuid
         throw new ApplicationException('Invalid client uuid.', 400);
     }
     $login = '';
     $loginBad = false;
     $passwordBad = false;
     if (!isset($this->application->parameters['login'])) {
         $loginBad = true;
     } else {
         $login = $this->application->parameters['login'];
         if (!ApplicationModel_User::validateLogin($login)) {
             $loginBad = true;
         }
     }
     if (!isset($this->application->parameters['password'])) {
         $passwordBad = true;
     } else {
         $password = $this->application->parameters['password'];
         if (!ApplicationModel_User::validatePassword($password)) {
             $passwordBad = true;
         }
     }
     // login must be valid
     $success = !$loginBad && !$passwordBad;
     $passwordWrong = false;
     $registerUser = false;
     if ($success) {
         $user = new ApplicationModel_User($this->application);
         try {
             // try to load user with selected login
             $user->setLogin($login);
             $user->load();
         } catch (ApplicationModelException_User $e) {
             // selected login does not exist - create a new user
             $registerUser = true;
         }
         // register a new user
         if ($registerUser) {
             $user->setPasswordHash($user->makePasswordHash($password));
             $user->save();
         } else {
             if ($user->makePasswordHash($password) != $user->getPasswordHash()) {
                 $passwordWrong = true;
             }
             $success = !$passwordWrong;
         }
     }
     // if everything is ok (user has the correct password, etc)...
     $attachUser = false;
     $uuidTaken = false;
     if ($success) {
         try {
             // authorize user
             $_SESSION['authorized_user_id'] = $user->getId();
             $_SESSION['authorized_user_login'] = $user->getLogin();
             // attach uuid if we have to
             if (!empty($uuid)) {
                 $attachUser = true;
                 $user->addUuid($uuid, time());
                 $user->save();
             }
         } catch (ApplicationModelException_User $e) {
             if ($e->getCode() == ApplicationModel_User::ERROR_TAKEN_UUID) {
                 $uuidTaken = true;
             }
             $success = !$uuidTaken;
         }
     }
     // if we have only authorized the user
     if ($success && !$registerUser && !$attachUser) {
         $this->application->outputHeaders[] = 'HTTP/1.1 302 Found';
         $this->application->outputHeaders[] = 'Location: /account.php';
         $this->application->outputContent = '';
     }
     $view = new ApplicationView($this->application, $this->application->path . '/views/user_register_handler.php');
     $view->success = $success;
     $view->registered = $registerUser;
     $view->uuid = $uuid;
     $view->uuidTaken = $uuidTaken;
     $view->login = $login;
     $view->loginBad = $loginBad;
     $view->passwordBad = $passwordBad;
     $view->passwordWrong = $passwordWrong;
     $view->render();
 }
 public function run()
 {
     $success = true;
     $use_uuid = false;
     $register_redirect = false;
     $uuid = '';
     $login = '';
     $password = '';
     if (!isset($this->application->parameters['login'])) {
         $success = false;
     } else {
         $login = $this->application->parameters['login'];
         if (!ApplicationModel_User::validateLogin($login)) {
             $success = false;
         }
     }
     if (!isset($this->application->parameters['password'])) {
         $success = false;
     } else {
         $password = $this->application->parameters['password'];
     }
     if (!$success) {
         if (isset($this->application->parameters['uuid'])) {
             $uuid = $this->application->parameters['uuid'];
             if (ApplicationModel_User::validateUuid($uuid)) {
                 $use_uuid = true;
             }
         }
     }
     if ($success) {
         $user = new ApplicationModel_User($this->application);
         try {
             $user->setLogin($login);
             $user->load();
         } catch (ApplicationModelException_User $e) {
             $success = false;
         }
     }
     if ($use_uuid) {
         $success = true;
         $user = new ApplicationModel_User($this->application);
         try {
             $user->setUuid($uuid);
             $user->load();
         } catch (ApplicationModelException_User $e) {
             $success = false;
             $register_redirect = true;
         }
     }
     if ($success) {
         if (!$use_uuid && $user->makePasswordHash($password) != $user->getPasswordHash()) {
             $success = false;
         }
     }
     if ($success) {
         $_SESSION['authorized_user_id'] = $user->getId();
         $_SESSION['authorized_user_login'] = $user->getLogin();
         $this->application->outputHeaders[] = 'HTTP/1.1 302 Found';
         $this->application->outputHeaders[] = 'Location: /account.php';
         $this->application->outputContent = '';
     } elseif ($register_redirect) {
         $this->application->outputHeaders[] = 'HTTP/1.1 302 Found';
         $this->application->outputHeaders[] = 'Location: /register.php?uuid=' . $uuid;
         $this->application->outputContent = '';
     } else {
         $this->application->outputHeaders[] = 'HTTP/1.1 302 Found';
         $this->application->outputHeaders[] = 'Location: /login.php?unsuccessful&login='******'';
     }
 }