<?php

if( !defined( 'FEEDSYNC_ENV' ) ) {
        die('Unauthorised');
}

function get_config_content( $data ) {

        ob_start();

        ?>

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for FeedSync */
define('DB_NAME', '<?php echo $data['dbname']; ?>' );

/** MySQL database username */
define('DB_USER', '<?php echo $data['username']; ?>' );

/** MySQL database password */
define('DB_PASS', '<?php echo $data['password']; ?>' );

/** MySQL hostname */
define('DB_HOST', '<?php echo $data['host']; ?>' );

/** DB TABLE PREFIX */
define('DB_TABLE_PREFIX', '<?php echo $data['table_prefix']; ?>' );

// ** FeedSync Login Details - Set your username and password to prevent unauthorised access ** //
/** Username to access feedsync  */
define('FEEDSYNC_ADMIN', '<?php echo $data['fsusername']; ?>' );

/** Password to access feedsync  */
define('FEEDSYNC_PASS', '<?php echo $data['fspassword']; ?>' ); 

/** Define Site URL */
define('SITE_URL', '<?php echo $_COOKIE['fsurl']; ?>' ); <?php

        return ob_get_clean();
        
}

function compile_config(  $data ) {

        $config = get_config_content( $data );

        // get data from sample file :

        $sample_file = "./core/install/sample.php";
        $sample_data = file_get_contents(  $sample_file );
        $sample_data = str_replace( '{{config_details}}', $config, $sample_data );

        return $sample_data;
}

function write_config() {

        $location       = "config.php";
        $config         = fopen( $location, "w") or die("Unable to Create config file, please check file permissions!");
        $data           = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
        $txt            = compile_config( $data );
        return fwrite($config, $txt);
}

function config_exists() {
        
        return file_exists( "config.php" );
}

function redirect_to_fs() {

        $location = $_COOKIE['fsurl'];
        header( 'location: '.$location);
        exit;
}

function generate_password() {

        $uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $lowercase = 'abcdefghijklmnopqrstuvwxyz';
        $numbers = '0123456789';
        $specialChars = '!@#$%^&*_+-=,./<>?;:[]{}';
    
        $password = '';
        $password .= $uppercase[rand(0, strlen($uppercase) - 1)];
        $password .= $lowercase[rand(0, strlen($lowercase) - 1)];
        $password .= $numbers[rand(0, strlen($numbers) - 1)];
        $password .= $specialChars[rand(0, strlen($specialChars) - 1)];
    
        $length = 6;
        $password .= substr(str_shuffle($uppercase . $lowercase . $numbers . $specialChars), 0, $length);
    
        $password = str_shuffle($password);
    
        return $password;
}
    
function init_config() {
        
        if( config_exists() ) {
                redirect_to_fs();
        }

        if( isset( $_POST['config_submit'] ) && 'Submit' == $_POST['config_submit'] ) {
                
                write_config();
                redirect_to_fs();
        }
}

init_config();