<?php

/**
 * FeedSync Upgrade 3.6 class.
 * 
 * This class is used for installation & upgradation of feedsync db tables.
 *
 *
 * @package FeedSync
 * @subpackage Tables
 * @since 3.4.5
 */

class UPGRADE_TO_3_6 {

	function __construct() {

                $this->upgrade();
	}

        public function rename_tables() {

                $compat_tables = array(
                        'listing'		=>	'feedsync',
                        'listing_meta'	        =>	'feedsync_listing_meta',
                        'agent'			=>	'feedsync_users',
                        'agent_meta'	        =>	'feedsync_users_meta',
                        'temp'			=>	'feedsync_temp',
                        'logs'			=>	'feedsync_logs',
                        'options'		=>	'feedsync_options'
                );

                foreach ($compat_tables as $key => $name) {

                        $check_exists = "SHOW TABLES LIKE '" . $name . "'";
                
                        $results = fsdb()->get_results($check_exists);
                
                        if ( !empty($results) ) {
                
                                $new_table_name = fsdb()->{$key};
                        
                                $check_new_exists = "SHOW TABLES LIKE '" . $new_table_name . "'";
                                $new_results = fsdb()->get_results($check_new_exists);
                        
                                if (empty($new_results)) {
                                        $sql = "ALTER TABLE `" . $name . "` RENAME TO `" . $new_table_name . "`";
                                        fsdb()->query($sql);
                                }
                        }
                }
        }

	/**
	 * Checks & inserts feedsync tables in database during installation & page load
	 */
	public function upgrade() {

                $this->rename_tables();

		$this->create_tables();

                $this->restore_sessions_from_session_path();
	}

	/**
	 * Create tables required for functioning of feedsync
	 * 
	 */
	public function create_tables() {

                $this->create_sessions_table();

	}

        /**
	 * Creates sessions table.
	 */
	public function create_sessions_table() {

                $fs_tables = new FEEDSYNC_TABLES();

		$sql = '
            CREATE TABLE IF NOT EXISTS `'.fsdb()->prefix.'sessions` (
                `id` varchar(32) NOT NULL,
                `access` int(10) unsigned,
                `data` text,
              PRIMARY KEY (`id`)
            ) '.$fs_tables->get_chartset().';

	    ';

	    fsdb()->query($sql);
	}

        /**
         * @since 3.7.1 fixed error Uncaught mysqli_sql_exception due to named params by converting them to MYSQLi compliant placeholders (?)
         */
        public function restore_sessions_from_session_path() {

                // Get all the current session data from the server's storage
                $session_path = ini_get('session.save_path');
        
                $db = new FS_Session_Database;
        
                $table = fsdb()->prefix.'sessions';

                if( empty( $_COOKIE['PHPSESSID'] ) ) {
                        return;
                }

                $file = 'sess_'.$_COOKIE['PHPSESSID'];

                if( file_exists( $session_path . '/' . $file ) ) {
                        
                        $session_data = file_get_contents($session_path . '/' . $file);
                        $session_id = substr($file, 5);
                        $access = time();
                
                        // Insert the session data into the database table
                        $db->query("INSERT IGNORE INTO ".$table." (id, access, data) VALUES (?, ?, ?)");
                        $db->bind('sss', $session_id, $access, $session_data);
                        $db->execute();
                }

                
        }
}

new UPGRADE_TO_3_6;