Account::createAccount PHP Method

createAccount() public static method

Used (currently) in the installation script. Note: this function relies on the settings file having been defined, along with an arbitrary encryption salt.
public static createAccount ( $accountInfo, boolean $isCurrentUser = false ) : integer
$accountInfo
$isCurrentUser boolean
return integer
    public static function createAccount($accountInfo, $isCurrentUser = false)
    {
        $accountInfo = Utils::sanitize($accountInfo);
        $encryptionSalt = Core::getEncryptionSalt();
        $accountType = $accountInfo["accountType"];
        $firstName = isset($accountInfo["firstName"]) && !empty($accountInfo["firstName"]) ? $accountInfo["firstName"] : "";
        $lastName = isset($accountInfo["lastName"]) && !empty($accountInfo["lastName"]) ? $accountInfo["lastName"] : "";
        $email = isset($accountInfo["email"]) && !empty($accountInfo["email"]) ? $accountInfo["email"] : "";
        $password = "";
        if (isset($accountInfo["password"]) && !empty($accountInfo["password"])) {
            $password = crypt($accountInfo["password"], $encryptionSalt);
        }
        // TODO - this is weird!
        $autoEmail = isset($accountInfo["accountType"]) ? $accountInfo["accountType"] : false;
        $L = Core::$language->getCurrentLanguageStrings();
        $now = Utils::getCurrentDatetime();
        $prefix = Core::getDbTablePrefix();
        $selectedDataTypes = Settings::getSetting("installedDataTypes");
        $selectedExportTypes = Settings::getSetting("installedExportTypes");
        $selectedCountries = Settings::getSetting("installedCountries");
        $result = Core::$db->query("\n\t\t\tINSERT INTO {$prefix}user_accounts (date_created, last_updated, date_expires, last_logged_in, account_type, \n\t\t\t\tfirst_name, last_name, email, password, selected_data_types, selected_export_types, selected_countries)\n\t\t\tVALUES ('{$now}', '{$now}', '{$now}', NULL, '{$accountType}', '{$firstName}', '{$lastName}', '{$email}', '{$password}',\n\t\t\t\t'{$selectedDataTypes}', '\${$selectedExportTypes}', '{$selectedCountries}')\n\t\t");
        $emailSent = false;
        // not used yet, but we should notify the user via the interface
        if ($autoEmail) {
            try {
                $content = $L["account_created_msg"] . "\n\n";
                if (isset($_SERVER["HTTP_REFERER"]) && !empty($_SERVER["HTTP_REFERER"])) {
                    $content .= "{$L["login_url_c"]} {$_SERVER["HTTP_REFERER"]}\n";
                }
                $content .= "{$L["email_c"]} {$email}\n{$L["password_c"]} {$accountInfo["password"]}\n";
                Emails::sendEmail(array("recipient" => $email, "subject" => $L["account_created"], "content" => $content));
                $emailSent = true;
            } catch (Exception $e) {
                $emailSent = false;
            }
        }
        $returnInfo = array("success" => $result["success"]);
        if ($result["success"]) {
            $accountID = mysqli_insert_id(Core::$db->getDBLink());
            if ($isCurrentUser) {
                Core::initSessions();
                $_SESSION["account_id"] = $accountID;
                Core::initUser(true);
            }
            $returnInfo["accountID"] = $accountID;
        }
        return $returnInfo;
    }

Usage Example

Beispiel #1
0
 public static function create()
 {
     $error_message = "";
     $studentId = "";
     $username = "";
     $password = "";
     $nickname = "";
     if (!empty($_POST)) {
         $studentId = $_POST["studentid"];
         $username = $_POST["username"];
         $password = $_POST["password"];
         $repeatPassword = $_POST["repeat_password"];
         $nickname = $_POST["nickname"];
         // 確認処理ー
         if (empty($studentId)) {
             $error_message .= "<li>学生番号を空白にしないでください。</li>";
         } else {
             if (preg_match("/^j/", $studentId)) {
                 $error_message .= "<li>英文字 j はいりません。数字のみです。</li>";
             } else {
                 if (!preg_match("/^[0-9]{7}\$/", $studentId)) {
                     $error_message .= "<li>正しい学籍番号を入力してください。</li>";
                 } else {
                     if (!in_array($studentId, getAllowableStudentIdList())) {
                         $error_message .= "<li>この学籍番号は使えません。</li>";
                     }
                 }
             }
         }
         if (empty($username)) {
             $error_message .= "<li>ログイン名を空白にしないでください。</li>";
         } else {
             if (!preg_match("/^[0-9a-zA-Z]+\$/", $username)) {
                 $error_message .= "<li>ログイン名は英数字以外の文字は受け付けません。[@_?,.]などの文字も使えません。</li>";
             } else {
                 if (Account::duplicateUsername($username)) {
                     $error_message .= "<li>ログイン名はすでに使われました。別のログイン名にしてください。</li>";
                 }
             }
         }
         if (empty($password)) {
             $error_message .= "<li>パスワードを空白にしないでください。</li>";
         }
         if (empty($repeatPassword)) {
             $error_message .= "<li>再確認パスワードを空白にしないでください。</li>";
         } else {
             if ($repeatPassword != $password) {
                 $error_message .= "<li>パスワードと再確認のパスワードは一致しません。</li>";
             }
         }
         if (empty($nickname)) {
             $error_message .= "<li>表示名を空白にしないでください。</li>";
         }
         // there is no error, and success to create a new account
         if (strlen($error_message) == 0) {
             $salt = Utils::generateSalt();
             $encrypted_password = Utils::encrpytPassword($password, $salt);
             // verify campus by
             if (preg_match("/^[481]/", $studentId)) {
                 $campus = "葛飾";
             } else {
                 if (preg_match("/^[67]/", $studentId)) {
                     $campus = "野田";
                 } else {
                     $campus = "謎";
                 }
             }
             $validate_code = Utils::generateValidationCode();
             $new_account_id = Account::createAccount($username, $encrypted_password, $nickname, $salt, $validate_code, $studentId, $campus);
             $mail_content = "下記のアカウントを作成しました。\n ログイン名: {$username}\n パスワード: {$password}\n次のリンクをクリックして認証が自動に行います。";
             self::sendMail($new_account_id, $studentId, $validate_code, $mail_content);
             header("Location: /account/verifyplease?accountid=" . $new_account_id);
             die;
         }
     }
     $content = "create.php";
     include VIEWS_PATH . "account/public.php";
 }
All Usage Examples Of Account::createAccount