<commit_before><?php
/**
 * SessionFile Driver
 *
 * Custom session uses file as storage
 *
 */

namespace Elips\Libraries\SessionDriver;

use Elips\Libraries\Cookie;
use Elips\Libraries\Encryption;

class SessionFile
{

    var $sessionId = null;
    var $sessionName = 'ELIPS_PHP_SESSID';
    var $sessionKey = 'SUPER_SECRET_KEY';
    var $sessionExpire = 7200;
    var $sessionMatchIP = false;
    var $sessionMaxSize = 1048576;
    var $sessionPath = 'storage/sessions/';
    var $sessionData = array();
    var $sessionDeleteId = null;

    /**
     * Initialize Config
     *
     * @param null $config
     */
    public function init($config = null)
    {
        if (isset($config['name'])) {
            $this->sessionName = $config['name'];
        }

        if (isset($config['encryption_key'])) {
            $this->sessionKey = $config['encryption_key'];
        }

        if (isset($config['expire'])) {
            $this->sessionExpire = $config['expire'];
        }

        if (isset($config['match_ip'])) {
            $this->sessionMatchIP = $config['match_ip'];
        }

        if (isset($config['max_size'])) {
            $this->sessionMaxSize = $config['max_size'];
        }

        if (time() - $this->get('SESS_CREATED') > $this->sessionExpire) {
            $this->destroy();
        }

        $this->generateSession();
    }

    /**
     * Generate Random String
     *
     * @param $length
     * @return string
     */
    private function generateRandomString($length)
    {
        return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
    }

    /**
     * Generate Session Id
     *
     * @return string
     */
    private function generateSessionId()
    {
        return sha1(uniqid('', true).$this->generateRandomString(25).microtime(true));
    }

    /**
     * Generate Session
     *
     * @return string
     */
    private function generateSession()
    {
        if (Cookie::get($this->sessionName) == null) {
            $this->sessionData['SESS_USER_AGENT'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
            $this->sessionData['SESS_IP_ADDR'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
            $this->sessionData['SESS_CREATED'] = time();
            $this->sessionData['SESS_EXPIRED'] = $this->sessionExpire;
            $this->sessionData['SESS_LAST_ACTIVITY'] = time();

            $this->sessionId = $this->generateSessionId();
            Cookie::set($this->sessionName, $this->sessionId);
            return $this->sessionId;
        } else {
            return Cookie::get($this->sessionName);
        }
    }

    /**
     * Get Session Id<commit_msg>fixing bug session file driver<commit_after><?php
/**
 * SessionFile Driver
 *
 * Custom session uses file as storage
 *
 */

namespace Elips\Libraries\SessionDriver;

use Elips\Libraries\Cookie;
use Elips\Libraries\Encryption;

class SessionFile
{

    var $sessionId = null;
    var $sessionName = 'ELIPS_PHP_SESSID';
    var $sessionKey = 'SUPER_SECRET_KEY';
    var $sessionExpire = 7200;
    var $sessionMatchIP = false;
    var $sessionMaxSize = 1048576;
    var $sessionPath = 'storage/sessions/';
    var $sessionData = array();
    var $sessionDeleteId = null;

    /**
     * Initialize Config
     *
     * @param null $config
     */
    public function init($config = null)
    {
        if (isset($config['name'])) {
            $this->sessionName = $config['name'];
        }

        if (isset($config['encryption_key'])) {
            $this->sessionKey = $config['encryption_key'];
        }

        if (isset($config['expire'])) {
            $this->sessionExpire = $config['expire'];
        }

        if (isset($config['match_ip'])) {
            $this->sessionMatchIP = $config['match_ip'];
        }

        if (isset($config['max_size'])) {
            $this->sessionMaxSize = $config['max_size'];
        }

        if (time() - $this->get('SESS_CREATED') > $this->sessionExpire) {
            $this->destroy();
        }

        $this->generateSession();
    }

    /**
     * Generate Random String
     *
     * @param $length
     * @return string
     */
    private function generateRandomString($length)
    {
        return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
    }

    /**
     * Generate Session Id
     *
     * @return string
     */
    private function generateSessionId()
    {
        return sha1(uniqid('', true).$this->generateRandomString(25).microtime(true));
    }

    /**
     * Generate Session
     *
     * @return string
     */
    private function generateSession()
    {
        if (Cookie::get($this->sessionName) == null) {
            $this->sessionData['SESS_USER_AGENT'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
            $this->sessionData['SESS_IP_ADDR'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
            $this->sessionData['SESS_CREATED'] = time();
            $this->sessionData['SESS_EXPIRED'] = $this->sessionExpire;
            $this->sessionData['SESS_LAST_ACTIVITY'] = time();

            $this->sessionId = $this->generateSessionId();
            Cookie::set($this->sessionName, $this->sessionId, $this->sessionExpire);
            return $this->sessionId;
        } else {
            return Cookie::get($this->sessionName);
        }
    }

    /**
     * Get Session Id
{'fraction_non_alphanumeric': 0.1530074504815555, 'fraction_numerical': 0.009085953116481919, 'mean_word_length': 2.403834260977118, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '3219612', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1662, 'n_words': 280}