Exemple #1
0
/**
 * Updates the password of the currently logged in user.
 * @param (mixed) $new_password The new password for the user.
 * @param (mixed) $new_password_again Confirmation of the new password for the user.
 * @return (array) An array with indices `status`, `message`, and `reasons`
 *                 containing the status code and description of the result of the operation,
 *                 and an array of reasons for that status.
 */
function update_current_user_password($new_password, $new_password_again)
{
    try {
        $cif_user = new CifUser($_SERVER['REDIRECT_WEBAUTH_USER']);
        if ($new_password !== $new_password_again) {
            throw new Exception("The passwords you entered didn't match.");
        }
        // Validate the password
        $errors = is_secure_password($new_password, 'Sorry, but your password needs');
        if ($errors) {
            throw new Exception($errors);
        }
        // Change the user's password
        $cif_user->change_password($new_password);
        return array('status' => STATUS_OK, 'message' => "Your password was changed.", 'reasons' => $errors);
    } catch (Exception $e) {
        $error_message = $e->getMessage();
    }
    // If execution reaches this point, something went wrong
    // If the password was valid, log the results of this failed operation
    if (isset($errors) && !$errors) {
        file_put_contents(LOG_DIR . $cifid, $log, FILE_APPEND);
    }
    return array('status' => STATUS_ERROR, 'message' => $error_message, 'reasons' => $errors);
}