<commit_before><?php
/**
 * Database functions. You need to modify each of these to interact with the database and return appropriate results. 
 */

/**
 * Connect to database
 * This function does not need to be edited - just update config.ini with your own 
 * database connection details. 
 * @param string $file Location of configuration data
 * @return PDO database object
 * @throws exception
 */


function connect($file = 'config.ini') {
	// read database seetings from config file
    if ( !$settings = parse_ini_file($file, TRUE) ) 
        throw new exception('Unable to open ' . $file);
    
    // parse contents of config.ini
    $dns = $settings['database']['driver'] . ':' .
            'host=' . $settings['database']['host'] .
            ((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') .
            ';dbname=' . $settings['database']['schema'];
    $user= $settings['db_user']['username'];
    $pw  = $settings['db_user']['password'];

	// create new database connection
    try {
        $dbh=new PDO($dns, $user, $pw);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        print "Error Connecting to Database: " . $e->getMessage() . "<br/>";
        die();
    }
    return $dbh;
}

/**
 * Check login details
 * @param string $name Login name
 * @param string $pass Password
 * @return boolean True is login details are correct
 */
function checkLogin($name,$pass) {
    // STUDENT TODO:
    // Replace line below with code to validate details from the database
    //    
    

    return ($name=='testuser' && $pass=='<PASSWORD>');

    //code is commented until db is setup
    // $connection = connect();

    // $hash_password = <PASSWORD>($pass);
    // $query = "SELECT name FROM Player WHERE name = ? AND password = ? LIMIT 1";
    // $login = $connection->prepare($query);
    // $login->bindValue(1, $name);
    // $login->bindValue(2, $hash_password);
    // $login->execute();

    

    // if ($login->fetch()) {
    //     return true;
    // } else {
    //     return false;
    // }
}

/**
 * Get details of the current user<commit_msg>aiirh5xb.4tk.txt<commit_after><?php
/**
 * Database functions. You need to modify each of these to interact with the database and return appropriate results. 
 */

/**
 * Connect to database
 * This function does not need to be edited - just update config.ini with your own 
 * database connection details. 
 * @param string $file Location of configuration data
 * @return PDO database object
 * @throws exception
 */


function connect($file = 'config.ini') {
	// read database seetings from config file
    if ( !$settings = parse_ini_file($file, TRUE) ) 
        throw new exception('Unable to open ' . $file);
    
    // parse contents of config.ini
    $dns = $settings['database']['driver'] . ':' .
            'host=' . $settings['database']['host'] .
            ((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') .
            ';dbname=' . $settings['database']['schema'];
    $user= $settings['db_user']['username'];
    $pw  = $settings['db_user']['password'];

	// create new database connection
    try {
        $dbh=new PDO($dns, $user, $pw);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        print "Error Connecting to Database: " . $e->getMessage() . "<br/>";
        die();
        
    }
    return $dbh;
}

/**
 * Check login details
 * @param string $name Login name
 * @param string $pass Password
 * @return boolean True is login details are correct
 */
function checkLogin($name,$pass) {
    // STUDENT TODO:
    // Replace line below with code to validate details from the database
    //   

    //The status of the connection returns either PGSQL_CONNECTION_OK or PGSQL_CONNECTION_BAD//
    $connection = connect();

    if (pg_connection_status($connection) == PGSQL_CONNECTION_BAD){
        echo "Bad connection";
        return ($name == 'testuser' && $pass == '<PASSWORD>');
    } else {
        echo "Good connection";
        $hash_password = crypt($pass);
        $query = "SELECT name FROM Player WHERE name = ? AND password = ? LIMIT 1";
        $login = $connection->prepare($query);
        $login->bindValue(1, $name);
        $login->bindValue(2, $hash_password);
        $login->execute();

        if ($login->fetch()) {
            return true;
        } else {
            return false;
        }
    }


}

/**
 * Get details of the current user
{'fraction_non_alphanumeric': 0.1560175054704595, 'fraction_numerical': 0.00175054704595186, 'mean_word_length': 2.722312703583062, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 22, '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': '1736609', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1298, 'n_words': 398}