/**
 * Insertion (ajout) d'un nouvel utilisateur
 * NB: Provoque aussi l'ajout d'en enregistrement dans la table usercnx;
 * @param $username : username utilisateur
 * @param $password : password utilisateur
 * @param $firstname : firstname utilisateur
 * @param $lastname : lastname utilisateur
 * @param $email : email utilisateur
 * @return array|bool : Un array contgenant les deux id d'insertion (user et usercnx) en cas de succès, false en cas d'échec
 */
function user_add($username, $email, $age, $password, $sex, $orientation, $region, $buts, $description)
{
    global $pdo, $user_tb_cols;
    $resultat = false;
    // Mode défensif
    $queryStr = 'INSERT INTO ' . PHPAT_DB_TB_USER . '(' . get_tb_cols($user_tb_cols) . ') VALUES (' . get_tb_cols($user_tb_cols, COLON_CAR) . ')';
    $sth = $pdo->prepare($queryStr);
    $password_hash = passwd_encrypt($password);
    $params = array(COLON_CAR . USER_TB_COL_USERNAME => $username, COLON_CAR . USER_TB_COL_EMAIL => $email, COLON_CAR . USER_TB_COL_AGE => $age, COLON_CAR . USER_TB_COL_PASSWORD_HASH => $password_hash, COLON_CAR . USER_TB_COL_SEX => $sex, COLON_CAR . USER_TB_COL_ORIENTATION => $orientation, COLON_CAR . USER_TB_COL_REGION => $region, COLON_CAR . USER_TB_COL_BUTS => $buts, COLON_CAR . USER_TB_COL_DESCRIPTION => $description);
    $res = $sth->execute($params);
    //$sth->debugDumpParams();
    //var_dump($params);
    //var_dump($res);
    if (!$res || $sth->rowCount() == 0) {
        throw new Exception("Echec lors de la tentative d'ajout de l'utilisateur {$username} : (" . $sth->errorInfo()[0] . ")<br/>");
    }
    $inserted_user_id = $pdo->lastInsertId();
    if ($res) {
        $resultat = $inserted_user_id;
    }
    return $resultat;
}
Exemple #2
0
function db_insert_user($uname, $passwd, $email, $level)
{
    //get current user num
    $uid = execute_sql("select user_number from db_status;", 0)[0];
    //$email="*****@*****.**";//test
    $sql = "insert into user (uid,uname,passwd,salt,level,email,score,is_verified,verified_code) values (?,?,?,?,?,?,0,0,?);";
    $salt = "";
    $enc_passwd = "";
    $conn = db_startconn();
    if (!$conn) {
        // error handling
        return -1;
    }
    // make passwd
    $salt = generate_salt($GLOBALS["nsc_salt_len"]);
    $enc_passwd = passwd_encrypt($passwd, $salt);
    $verified_code = passwd_encrypt(mt_rand(0, 99999), $salt);
    // using prepared statement to prevent sql injection
    if (!($stmt = $conn->prepare($sql))) {
        // error handling
        db_close_conn($conn);
        return -2;
    }
    // bind parameters
    if (!$stmt->bind_param("isssiss", $uid, $uname, $enc_passwd, $salt, $level, $email, $verified_code)) {
        // error handling
        db_close_conn($conn);
        $stmt->close();
        return -4;
    }
    if (!$stmt->execute()) {
        // error handling
        db_close_conn($conn);
        $stmt->close();
        return -3;
    }
    //uid+1,并存入数据库
    $uid += 1;
    execute_sql("update db_status set user_number=" . $uid . ";", 0);
    //发送邮箱验证码
    $content = "hello! " . $uname . ":<br>your verified code is:<br> " . $verified_code . "<br>please <a href=http://127.0.0.1/nsc-website/test/verify_email.php?code=" . $verified_code . "&email=" . $email . ">verify your email</a> soon.";
    send_verified_code($email, $content);
    $stmt->close();
    db_close_conn($conn);
    return 1;
}