public static function register()
 {
     $email = Core::validate(self::getVar('email'));
     $pass = Core::validate(self::getVar('password'));
     $captcha = Core::validate(self::getVar('captcha'));
     if ($email == null || $pass == null || $captcha == null) {
         Core::printErrorJson('Incorrect data input');
         return;
     }
     $right_code = Session::getSessionVariable('security_code');
     Session::unsetSessionVariable('security_code');
     if ($captcha != $right_code) {
         Core::printErrorJson('Incorrect captcha');
         return;
     }
     if (!Core::isEmailAddress($email)) {
         Core::printErrorJson('Incorrect email');
         return;
     }
     if (User::isExist($email, $email)) {
         Core::printErrorJson('User ' . $email . ' is already registered.');
         return;
     }
     $usr = new User();
     $usr->setLogin($email);
     $usr->setEmail($email);
     $usr->setDate(date("Y-m-d H:i:s"));
     $usr->setActivation(0);
     $usr->setPassHash(Core::calculateHash($pass));
     $usr->insert();
     $activationCode = self::calcActivationCode($usr);
     $activationUrl = "http://" . $_SERVER['SERVER_NAME'] . "/usr/activation?login="******"&code=" . $activationCode;
     $subject = Core::translateToCurrentLocale("Registration confirmation") . ".";
     $header = '<h1>' . Core::translateToCurrentLocale("Hello") . ', </h1>
     <p class="lead">' . Core::translateToCurrentLocale("you have registered on the Bitmonex website") . '.</p>' . '<p>' . Core::translateToCurrentLocale("Your login is") . ': ' . $email . '</p><p>' . Core::translateToCurrentLocale("Your password is") . ': ' . $pass . '</p>';
     $body = '<p>' . Core::translateToCurrentLocale("To confirm your registration, please click on this link") . '. <a href="' . $activationUrl . '">' . Core::translateToCurrentLocale("Activate") . '!</a></p>';
     $message = self::getMessage($header, $body);
     if (!Core::send_mail($email, $subject, $message)) {
         $usr->delete();
         Core::printErrorJson('Notification email is not send.');
         return;
     }
     $result['success'] = 1;
     print json_encode($result);
 }
 /**
  * Run method with main page logic
  * 
  * Populate template and display form for registration. For POST requests, check if the user
  * already exists. If not, create new User and AuthToken entries and send an email notification to the user
  * @access public
  */
 public function run()
 {
     $form_errors = array();
     $form_values = array("username" => "", "password" => "", "password2" => "", "ulid" => "");
     $session = Session::getInstance();
     $user = $session->getUser();
     // Session should not have a defined user
     if ($user != null) {
         $session->setMessage("You are already a user", Session::MESSAGE_ERROR);
         header("Location: " . BASE_URL);
         return;
     }
     if (!empty($_POST)) {
         $form_values["username"] = isset($_POST["username"]) ? trim($_POST["username"]) : "";
         $form_values["password"] = isset($_POST["password"]) ? trim($_POST["password"]) : "";
         $form_values["password2"] = isset($_POST["password2"]) ? trim($_POST["password2"]) : "";
         $form_values["ulid"] = isset($_POST["ulid"]) ? trim($_POST["ulid"]) : "";
         if (empty($form_values["username"])) {
             $form_errors["username"] = "******";
         }
         if (empty($form_values["password"])) {
             $form_errors["password"] = "******";
         }
         if (empty($form_values["password2"])) {
             $form_errors["password"] = "******";
         }
         if (empty($form_values["ulid"])) {
             $form_errors["ulid"] = "No ulid specified";
         } else {
             if (!preg_match("/[a-z]{5,7}/", $form_values["ulid"])) {
                 $form_errors["ulid"] = "Ulid is not in the proper format.";
             }
         }
         $userDAO = UserDAO::getInstance();
         $user = $userDAO->loadByUsername($form_values["username"]);
         // User already exists
         if ($user != null) {
             $form_errors["username"] = "******";
         }
         if (strcmp($form_values["password"], $form_values["password2"]) != 0) {
             $form_errors["password"] = "******";
         }
         $user = $userDAO->loadByUlid($form_values["ulid"]);
         // User already exists
         if ($user != null) {
             $form_errors["ulid"] = "Ulid is already registered";
         }
         if (empty($form_errors)) {
             $user = new User();
             $user->setUsername($form_values["username"]);
             $user->setPassHash(sha1($form_values["password"]));
             $user->setUlid($form_values["ulid"]);
             $status = $userDAO->insert($user);
             if ($status) {
                 $token = new AuthToken();
                 $token->setUser($user);
                 $tokenDAO = AuthTokenDAO::getInstance();
                 $status = $tokenDAO->insert($token);
                 if ($status) {
                     $session->setMessage("Registration started. Check your email for a message to continue");
                     if (defined("SMTP_HOST") && strcmp(SMTP_HOST, "") != 0) {
                         $from_addr = EMAIL_ADDRESS;
                         //$to = "*****@*****.**";
                         $to = "{$form_values["ulid"]}@" . User::ISU_EMAIL_DOMAIN;
                         $subject = "Verify registration with " . SITE_NAME;
                         $body = "To start the next step of the registration process, click the verify link below and enter the requested information. If the URL does not appear as a link, copy the URL, paste it into your browser's address bar and proceed to the web page.\n\n" . joinPath(BASE_URL, "verify.php") . "?token={$token->getToken()}\n";
                         $headers = array("From" => $from_addr, "To" => $to, "Subject" => $subject);
                         $stmp = Mail::factory("smtp", array("host" => SMTP_HOST, "auth" => true, "username" => SMTP_USERNAME, "password" => SMTP_PASSWORD));
                         $mail = $stmp->send($to, $headers, $body);
                     }
                     header("Location: " . BASE_URL);
                     return;
                 }
             }
         }
     }
     $user = $session->getUser();
     $this->template->render(array("title" => "Register", "main_page" => "register_tpl.php", "user" => $user, "session" => $session, "form_errors" => $form_errors, "form_values" => $form_values));
 }