<commit_before><?php
/**
 * Name:       Settings updater.
 * Programmer: <NAME>
 * Date:       5/21/13
 */

//includes
require_once('../path.php');
require_once(ABSPATH.'includes/config/settings.php');
require_once(ABSPATH.'includes/data.php');
require_once(ABSPATH.'includes/config/users.php');
require_once(ABSPATH.'admin/generate_reset_codes.php');

//Start the users session if necessary
if(!(isset($_SESSION))){
    session_start();
}

//fetch the settings
$set = new settings;
$settings = $set->fetch();

//flags
$fail = TRUE;
$save = TRUE;

//make sure the user is actually logged in.
if(isset($_SESSION['userid'])){

//Begin administrator ONLY section
if($_SESSION['admin'] >= '1' && !(isset($_REQUEST['userid']))){

//output debugging info on request
if(isset($_REQUEST['dump'])){
?>
    <h1>Settings Debugging information</h1>
    <hr />
    <b>JSON:</b>
    <pre>
<?php
    echo file_get_contents($set->location);
?>
   </pre>
   <b>Associative Array:</b>
   <pre>
<?php
    var_dump($set->fetch());
?>
   </pre>
   <hr />
   <a href="./?p=admin">Go back</a>
<?php

    $save = FALSE; //prevent redirection
}

//Rest the settings file to defaults on request
if(isset($_REQUEST['rebuild'])){

    $set->create();

    header('location: ../?p=admin');

    $save = FALSE;
}

if(isset($_REQUEST['download'])){

    header('location: ../includes/config/settings.json');
    $save = FALSE;
}

//update each of the settings
foreach($_REQUEST as $key => $value){

    if(isset($settings[$key])){

        switch($value){

            CASE 'TRUE':

                $settings[$key] = TRUE; //this ensures a boolean is saved

            break;

            CASE 'FALSE':

                $settings[$key] = FALSE; //this ensures a boolean is saved

            break;

            DEFAULT:

                $settings[$key] = $value; //normal method (for strings)

            break;
        }

    }

}

//Debug mode enable/disable
if(isset($_REQUEST['d'])){

    if($_REQUEST['d'] == '1'){

        //Enable debug mode
        $settings['debug'] = TRUE;
        $set->update($settings);

    }

    if($_REQUEST['d'] == '0'){

        //Disable debug mode
        $settings['debug'] = FALSE;
        $set->update($settings);


    }
}

    //Administrator user tools
    echo "<br />Entered user mode<br />";
    $users = new users();

    if(isset($_REQUEST['Add'])){

        $users->change('firstname', $_REQUEST['firstname']);
        $users->change('lastname', $_REQUEST['lastname']);

        $users->change('name', $_REQUEST['email']);
        $users->change('name', sha1($_REQUEST['password']));

        $users->change('name', $_REQUEST['type']);
        $users->change('name', $_REQUEST['admin']);

        var_dump($users);
        //$users->create();

        $save =false;

    }

if($save == TRUE){ //prevent unnecessary updates

    //update the settings
    $fail = $set->update($settings);

    //Redirect the user back to the settings menu
    if($fail == TRUE){
        header('location: ../?p=admin');
    }else{
        header('location: ../?p=admin');
    }
}


//end administrator only section
}


//Begin user section

//User profile updates
if(isset($_REQUEST['userid'])){
    echo "<br />Entered user mode<br />";

    $users = new users();

    $status = $users->login($_REQUEST['email'], $_REQUEST['password']);



    //make sure the users info checked out
    if(!($status == false)){

    //password change
    if(isset($_REQUEST['new_pass']) && !(empty($_REQUEST['new_pass']))){
        if(isset($_REQUEST['new_pass_II'])  && !(empty($_REQUEST['new_pass']))){
            if($_REQUEST['new_pass'] == $_REQUEST['new_pass_II']){
                echo '<br />Changeing password <br />';
                $users->change('password', hash('SHA512', $_REQUEST['new_pass'].$settings['salt']));
                $users->update();
            }
        }
    }

    //name change
    if(isset($_REQUEST['firstname']) && isset($_REQUEST['lastname'])){
        echo '<br />Changing name <br />';
        $users->change('firstname', $_REQUEST['firstname']);
        $users->change('lastname', $_REQUEST['lastname']);
        $users->change('colorization', $_REQUEST['month_colors']);
        $users->update();
    }


    }else{
        echo "<br />Bad user info supplied (probably password).<br />";
    }

    header('location: ../?p=user');
}



//end of login check
}else{
    ?><span class="error">You must be logged in to view this page.</span><?php
}

//Password reset
if(isset($_REQUEST['reset_code'])){

    $users = new users();
    $users->reset_code($_REQUEST['reset_code']);

    //password change
    if(isset($_REQUEST['new_pass']) && !(empty($_REQUEST['new_pass']))){
        if(isset($_REQUEST['new_pass_II'])  && !(empty($_REQUEST['new_pass']))){
            if($_REQUEST['new_pass'] == $_REQUEST['new_pass_II']){
                echo '<br />Changeing password <br />';
                $users->change('password', hash('SHA512', $_REQUEST['new_pass'].$settings['salt']));
                $users->change('reset_code', '');
                var_dump($users);
                $users->update();
            }
        }
    }

    header('location: ../?p=login');
}

//Change the salt
if(isset($_REQUEST['salt'])){

    //Change the salt
    $fp = fopen('/dev/urandom', 'r');
    $set = new settings;
    $settings = $set->fetch();
    $settings['salt'] = hash('SHA512', fread($fp, 256));
    $set->update($settings);

    //Reset everyone in the database's password
    $dbc = new db;
    $dbc->connect();
    $people = $dbc->query('SELECT * FROM people');
    $dbc->close();

    foreach($people as $person){

        $code = insert_reset_code($person['index']);

        if($person['index'] == $_SESSION['userid']){
            $_SESSION['reset_code'] = $code;
        }

    }

    //Redirect the current user to the password reset script
    header('location: ../?p=reset');

    //Prevent anything from being save on accident
    $save = FALSE;
}
<commit_msg>Fixed bug in profile.php that delete the users phone number upon edit.<commit_after><?php
/**
 * Name:       Settings updater.
 * Programmer: <NAME>
 * Date:       5/21/13
 */

//includes
require_once('../path.php');
require_once(ABSPATH.'includes/config/settings.php');
require_once(ABSPATH.'includes/data.php');
require_once(ABSPATH.'includes/config/users.php');
require_once(ABSPATH.'admin/generate_reset_codes.php');

//Start the users session if necessary
if(!(isset($_SESSION))){
    session_start();
}

//fetch the settings
$set = new settings;
$settings = $set->fetch();

//flags
$fail = TRUE;
$save = TRUE;

//make sure the user is actually logged in.
if(isset($_SESSION['userid'])){

//Begin administrator ONLY section
if($_SESSION['admin'] >= '1' && !(isset($_REQUEST['userid']))){

//output debugging info on request
if(isset($_REQUEST['dump'])){
?>
    <h1>Settings Debugging information</h1>
    <hr />
    <b>JSON:</b>
    <pre>
<?php
    echo file_get_contents($set->location);
?>
   </pre>
   <b>Associative Array:</b>
   <pre>
<?php
    var_dump($set->fetch());
?>
   </pre>
   <hr />
   <a href="./?p=admin">Go back</a>
<?php

    $save = FALSE; //prevent redirection
}

//Rest the settings file to defaults on request
if(isset($_REQUEST['rebuild'])){

    $set->create();

    header('location: ../?p=admin');

    $save = FALSE;
}

if(isset($_REQUEST['download'])){

    header('location: ../includes/config/settings.json');
    $save = FALSE;
}

//update each of the settings
foreach($_REQUEST as $key => $value){

    if(isset($settings[$key])){

        switch($value){

            CASE 'TRUE':

                $settings[$key] = TRUE; //this ensures a boolean is saved

            break;

            CASE 'FALSE':

                $settings[$key] = FALSE; //this ensures a boolean is saved

            break;

            DEFAULT:

                $settings[$key] = $value; //normal method (for strings)

            break;
        }

    }

}

//Debug mode enable/disable
if(isset($_REQUEST['d'])){

    if($_REQUEST['d'] == '1'){

        //Enable debug mode
        $settings['debug'] = TRUE;
        $set->update($settings);

    }

    if($_REQUEST['d'] == '0'){

        //Disable debug mode
        $settings['debug'] = FALSE;
        $set->update($settings);


    }
}

    //Administrator user tools
    echo "<br />Entered user mode<br />";
    $users = new users();

    if(isset($_REQUEST['Add'])){

        $users->change('firstname', $_REQUEST['firstname']);
        $users->change('lastname', $_REQUEST['lastname']);

        $users->change('name', $_REQUEST['email']);
        $users->change('name', sha1($_REQUEST['password']));

        $users->change('name', $_REQUEST['type']);
        $users->change('name', $_REQUEST['admin']);

        var_dump($users);
        //$users->create();

        $save =false;

    }

if($save == TRUE){ //prevent unnecessary updates

    //update the settings
    $fail = $set->update($settings);

    //Redirect the user back to the settings menu
    if($fail == TRUE){
        header('location: ../?p=admin');
    }else{
        header('location: ../?p=admin');
    }
}


//end administrator only section
}


//Begin user section

//User profile updates
if(isset($_REQUEST['userid'])){
    echo "<br />Entered user mode<br />";

    $users = new users();

    $status = $users->login($_REQUEST['email'], $_REQUEST['password']);



    //make sure the users info checked out
    if(!($status == false)){

    //password change
    if(isset($_REQUEST['new_pass']) && !(empty($_REQUEST['new_pass']))){
        if(isset($_REQUEST['new_pass_II'])  && !(empty($_REQUEST['new_pass']))){
            if($_REQUEST['new_pass'] == $_REQUEST['new_pass_II']){
                echo '<br />Changeing password <br />';
                $users->change('password', hash('SHA512', $_REQUEST['new_pass'].$settings['salt']));
                $users->update();
            }
        }
    }

    //name change
    if(isset($_REQUEST['firstname']) && isset($_REQUEST['lastname'])){
        echo '<br />Changing name <br />';
        $users->change('firstname', $_REQUEST['firstname']);
        $users->change('lastname', $_REQUEST['lastname']);
        $users->change('colorization', $_REQUEST['month_colors']);
        $users->change('phone_number', $_REQUEST['phone_number']);
        $users->update();
    }


    }else{
        echo "<br />Bad user info supplied (probably password).<br />";
    }

    header('location: ../?p=user');
}



//end of login check
}else{
    ?><span class="error">You must be logged in to view this page.</span><?php
}

//Password reset
if(isset($_REQUEST['reset_code'])){

    $users = new users();
    $users->reset_code($_REQUEST['reset_code']);

    //password change
    if(isset($_REQUEST['new_pass']) && !(empty($_REQUEST['new_pass']))){
        if(isset($_REQUEST['new_pass_II'])  && !(empty($_REQUEST['new_pass']))){
            if($_REQUEST['new_pass'] == $_REQUEST['new_pass_II']){
                echo '<br />Changeing password <br />';
                $users->change('password', hash('SHA<PASSWORD>', $_REQUEST['new_pass'].$settings['salt']));
                $users->change('reset_code', '');
                var_dump($users);
                $users->update();
            }
        }
    }

    header('location: ../?p=login');
}

//Change the salt
if(isset($_REQUEST['salt'])){

    //Change the salt
    $fp = fopen('/dev/urandom', 'r');
    $set = new settings;
    $settings = $set->fetch();
    $settings['salt'] = hash('SHA512', fread($fp, 256));
    $set->update($settings);

    //Reset everyone in the database's password
    $dbc = new db;
    $dbc->connect();
    $people = $dbc->query('SELECT * FROM people');
    $dbc->close();

    foreach($people as $person){

        $code = insert_reset_code($person['index']);

        if($person['index'] == $_SESSION['userid']){
            $_SESSION['reset_code'] = $code;
        }

    }

    //Redirect the current user to the password reset script
    header('location: ../?p=reset');

    //Prevent anything from being save on accident
    $save = FALSE;
}

{'fraction_non_alphanumeric': 0.19968644277580658, 'fraction_numerical': 0.003548147536925489, 'mean_word_length': 2.728083666564134, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6667134', 'n_tokens_mistral': 3786, 'n_tokens_neox': 3602, 'n_words': 807}