<commit_before><?php

/**
 * Registers a new account
 */
require '../db.php';
header('Content-type: application/json');

$forename = $db->real_escape_string(isset($_GET['forename']) ? $_GET['forename'] : $_POST['forename']);
$surname = $db->real_escape_string(isset($_GET['surname']) ? $_GET['surname'] : $_POST['surname']);
$email = $db->real_escape_string(isset($_GET['email']) ? $_GET['email'] : $_POST['email']);
$current_password = md5(isset($_GET['current_password']) ? $_GET['current_password'] : $_POST['current_password']);
$new_password = md5(isset($_GET['new_password']) ? $_GET['new_password'] : $_POST['new_password']);
$new_password2 = <PASSWORD>(isset($_GET['new_password2']) ? $_GET['new_password2'] : $_POST['<PASSWORD>_password2']);
$description = $db->real_escape_string(isset($_GET['description']) ? $_GET['description'] : $_POST['description']);

$userID = isset($_COOKIE['userID']) ? $_COOKIE['userID'] : $_SESSION['userID'];

// validate that password fields have all been entered
if ($current_password && ($new_password != $<PASSWORD>)) {
    echo json_encode(array("success" => false, "msg" => "your passwords do not match"));
    return false;
}

// validate that the email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(array("success" => false, "msg" => "Please provide a valid email"));
    return false;
}

// validate that the email is not taken by someone else
$query = "SELECT userID FROM users WHERE email='$email' AND userID !='$userID'";
if ($result = $db->query($query)) {

    /* fetch object array */
    if ($row = $result->fetch_row()) {
        echo json_encode(array("success" => false, "msg" => "This email is already registered."));
        return false;
    }
    /* free result set */
    $result->close();
}

// Update the password if the 3 fields have been provided correctly
if ($current_password && ($new_password == $new_<PASSWORD>)) {
    $query = "SELECT password FROM users WHERE password='$<PASSWORD>' AND userID='$userID'";
    if ($result = $db->query($query)) {

        /* fetch object array */
        if ($row = $result->fetch_assoc()) {
            if ($row['password'] == $current_password) {
                // Update the password
                $query = "UPDATE users SET password = <PASSWORD>';";
                $result = $db->query($query);
                echo json_encode(array("success" => true, "msg" => "Password updated"));
            }
            else {
                echo json_encode(array("success" => false, "msg" => "The current password provided doesn't match your password"));
                return false;
            }
        }
        /* free result set */
        $result->close();
    }
}

// Update the details
$query = "UPDATE users SET forename='$forename', surname='$surname', email='$email', description='$description' WHERE userID='$userID'";
if ($result = $db->query($query)) {
    $db->commit();
    echo json_encode(array("success" => true, "msg" => "Details updated", "href" => "window.location"));
    return true;
} else {
    echo json_encode(array("success" => false, "msg" => "Failed to update details"));
    return false;
}
<commit_msg>Fixed defect in password update where non matching current password would throw up a server error<commit_after><?php

/**
 * Registers a new account
 */
require '../db.php';
header('Content-type: application/json');

$forename = $db->real_escape_string(isset($_GET['forename']) ? $_GET['forename'] : $_POST['forename']);
$surname = $db->real_escape_string(isset($_GET['surname']) ? $_GET['surname'] : $_POST['surname']);
$email = $db->real_escape_string(isset($_GET['email']) ? $_GET['email'] : $_POST['email']);
$current_password = md5(isset($_GET['current_password']) ? $_GET['current_password'] : $_POST['current_password']);
$new_password = md5(isset($_GET['new_password']) ? $_GET['new_password'] : $_POST['new_password']);
$new_password2 = md5(isset($_GET['new_password2']) ? $_GET['new_password2'] : $_POST['new_password2']);
$description = $db->real_escape_string(isset($_GET['description']) ? $_GET['description'] : $_POST['description']);

$userID = isset($_COOKIE['userID']) ? $_COOKIE['userID'] : $_SESSION['userID'];

// validate that password fields have all been entered
if ($current_password && ($new_password != $new_password2)) {
    echo json_encode(array("success" => false, "msg" => "The new passwords provided do not match"));
    return false;
}

// validate that the email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(array("success" => false, "msg" => "Please provide a valid email"));
    return false;
}

// validate that the email is not taken by someone else
$query = "SELECT userID FROM users WHERE email='$email' AND userID !='$userID'";
if ($result = $db->query($query)) {

    /* fetch object array */
    if ($row = $result->fetch_row()) {
        echo json_encode(array("success" => false, "msg" => "This email is already registered."));
        return false;
    }
    /* free result set */
    $result->close();
}

// Update the password if the 3 fields have been provided correctly
if ($current_password && ($new_password == $new_<PASSWORD>)) {
    $query = "SELECT password FROM users WHERE password='$<PASSWORD>' AND userID='$userID'";
    if ($result = $db->query($query)) {
        /* fetch object array */
        if ($row = $result->fetch_assoc()) {
            // Update the password
            $query = "UPDATE users SET password = '$<PASSWORD>';";
            $result = $db->query($query);
            echo json_encode(array("success" => true, "msg" => "Password updated"));
            /* free result set */
            $result->close();
        } else {
            echo json_encode(array("success" => false, "msg" => "The current password provided doesn't match your password"));
            return false;
        }
    }
}

// Update the details
$query = "UPDATE users SET forename='$forename', surname='$surname', email='$email', description='$description' WHERE userID='$userID'";
if ($result = $db->query($query)) {
    $db->commit();
    echo json_encode(array("success" => true, "msg" => "Details updated", "href" => "window.location"));
    return true;
} else {
    echo json_encode(array("success" => false, "msg" => "Failed to update details"));
    return false;
}

{'fraction_non_alphanumeric': 0.18514429900646587, 'fraction_numerical': 0.0025232613152499604, 'mean_word_length': 4.013438735177866, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1316223', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1851, 'n_words': 490}