<commit_before><?php

/**
 * @file Session.php
 * This file is part of MOVIM.
 *
 * @brief Class that manages session variables with minimal lock time.
 *
 * @author <NAME> <<EMAIL>>
 *
 * Yes, this class is again a singleton. But this is justified by the fact that
 * there can only be one active session that is locked down.
 *
 * @version 1.0
 * @date 26 April 2011
 *
 * Copyright (C)2011 MOVIM
 *
 * See COPYING for licensing information.
 */

if(!class_exists('Session')):

class SessionVar extends DatajarBase
{
    protected $name;
    protected $value;
    protected $session;
    protected $container;
    protected $timestamp;

    protected function type_init()
    {
        $this->name      = DatajarType::varchar(128);
        $this->value     = DatajarType::text();
        $this->session   = DatajarType::varchar(128);
        $this->container = DatajarType::varchar(128);
        $this->timestamp = DatajarType::int();
    }
}

class Session
{
    protected $db;
    protected static $instances = array();
    protected static $sid = null;
    protected $container;
    protected $max_age = 86400; // 24hours

    /**
     * Loads and immediately closes the session variables for the namespace
     * $name.
     */
    protected function __construct($name)
    {
        // Does the database exist?
        $var = new SessionVar();

        if(self::$sid == null) {
            if(isset($_COOKIE['PHPFASTSESSID'])) {
                self::$sid = $_COOKIE['PHPFASTSESSID'];
            } else {
                $this->regenerate();
            }
        }

        $this->container = $name;
        Logger::log(1, "Session: Starting session ".self::$sid);
    }

    protected function regenerate()
    {
        // Generating the session cookie's hash.
        $hash_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $hash = "";

        for($i = 0; $i < 64; $i++) {
            $r = mt_rand(0, strlen($hash_chars) - 1);
            $hash.= $hash_chars[$r];
        }

        self::$sid = $hash;
        setcookie('PHPFASTSESSID', self::$sid, time() + $this->max_age);
    }

    /**
     * Gets a session handle.
     */
    public static function start($name)
    {
        if(!isset(self::$instances[$name])) {
            self::$instances[$name] = new self($name);
        }

        return self::$instances[$name];
    }

    /**
     * Gets a session variable. Returns false if doesn't exist.
     */
    public function get($varname)
    {
        $query = SessionVar::query()->select()
                                   ->where(array(
                                           'session' => self::$sid,
                                           'container' => $this->container,
                                           'name' => $varname));
        $data = SessionVar::run_query($query);

        if($data) {
            return unserialize(base64_decode($data[0]->value));
        } else {
            return false;
        }
    }

    /**
     * Sets a session variable. Returns $value.
     */
    public function set($varname, $value)
    {
        // Does the variable exist?
        $var = new SessionVar();
                                   
        $query = SessionVar::query()->select()
                                   ->where(array(
                                           'session' => self::$sid,
                                           'container' => $this->container,
                                           'name' => $varname))
                                   ->limit(0, 1);
        $data = SessionVar::run_query($query);


        Logger::log(1, "Session: Setting variable $varname");

        if($data) {
            $var = $data[0];
        }

            $var->session = self::$sid;
            $var->container = $this->container;
            $var->name = $varname;

        
        $var->value = base64_encode(serialize($value));
        $var->timestamp = time();
        
        $var->run_query($var->query()->save($var));

        return $var->value;
    }

    /**
     * Deletes a variable from the session.
     */
    public function remove($varname)
    {
        $var = new SessionVar();
        $var->load(array(
                            'session' => self::$sid,
                            'container' => $this->container,
                            'name' => $varname));

        $query = $var->query()->delete($var);
        $var->run_query($query);
    }

    public function delete_container()
    {
        $query = SessionVar::query()->select()
                           ->where(array(
                                   'session' => self::$sid,
                                   'container' => $this->container));
        $vars = SessionVar::run_query($query);
        
        foreach($vars as $var)
        {
            $query = SessionVar::query()->delete($var);
            SessionVar::run_query($query);
        }
    }

    /**
     * Deletes all this session container (not the session!)
     */
    public static function dispose($name)
    {
        if(isset(self::$instances[$name])) {
            self::$instances[$name]->delete_container();
            unset(self::$instances[$name]);
            return true;
        } else {
            return false;
        }
    }
    
    public static function clear()
    {
    
    }
}

endif;

?><commit_msg>- Portage of Movim Session on Modl<commit_after><?php

/**
 * @file Session.php
 * This file is part of MOVIM.
 *
 * @brief Class that manages session variables with minimal lock time.
 *
 * @author <NAME> <<EMAIL>>
 *
 * Yes, this class is again a singleton. But this is justified by the fact that
 * there can only be one active session that is locked down.
 *
 * @version 1.0
 * @date 26 April 2011
 *
 * Copyright (C)2011 MOVIM
 *
 * See COPYING for licensing information.
 */

if(!class_exists('Session')):

class SessionVar extends DatajarBase
{
    protected $name;
    protected $value;
    protected $session;
    protected $container;
    protected $timestamp;

    protected function type_init()
    {
        $this->name      = DatajarType::varchar(128);
        $this->value     = DatajarType::text();
        $this->session   = DatajarType::varchar(128);
        $this->container = DatajarType::varchar(128);
        $this->timestamp = DatajarType::int();
    }
}

class Session
{
    protected $db;
    protected static $instances = array();
    protected static $sid = null;
    protected $container;
    protected $max_age = 86400; // 24hours

    /**
     * Loads and immediately closes the session variables for the namespace
     * $name.
     */
    protected function __construct($name)
    {
        // Does the database exist?
        $var = new SessionVar();

        if(self::$sid == null) {
            if(isset($_COOKIE['PHPFASTSESSID'])) {
                self::$sid = $_COOKIE['PHPFASTSESSID'];
            } else {
                $this->regenerate();
            }
        }

        $this->container = $name;
        Logger::log(1, "Session: Starting session ".self::$sid);
    }

    protected function regenerate()
    {
        // Generating the session cookie's hash.
        $hash_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $hash = "";

        for($i = 0; $i < 64; $i++) {
            $r = mt_rand(0, strlen($hash_chars) - 1);
            $hash.= $hash_chars[$r];
        }

        self::$sid = $hash;
        setcookie('PHPFASTSESSID', self::$sid, time() + $this->max_age);
    }

    /**
     * Gets a session handle.
     */
    public static function start($name)
    {
        if(!isset(self::$instances[$name])) {
            self::$instances[$name] = new self($name);
        }

        return self::$instances[$name];
    }

    /**
     * Gets a session variable. Returns false if doesn't exist.
     */
    public function get($varname)
    {
        $sd = new modl\SessionDAO();
        $data = $sd->get(self::$sid, $this->container, $varname);

        if($data) {
            return unserialize(base64_decode($data->value));
        } else {
            return false;
        }
    }

    /**
     * Sets a session variable. Returns $value.
     */
    public function set($varname, $value)
    {
        $value = base64_encode(serialize($value));
        
        $sd = new modl\SessionDAO();
        $sd->set(self::$sid, $this->container, $varname, $value, time());

        return $value;
    }

    /**
     * Deletes a variable from the session.
     */
    public function remove($varname)
    {
        $var = new SessionVar();
        $var->load(array(
                            'session' => self::$sid,
                            'container' => $this->container,
                            'name' => $varname));

        $query = $var->query()->delete($var);
        $var->run_query($query);
    }

    public function delete_container()
    {
        $sd = new modl\SessionDAO();
        $sd->deleteContainer(self::$sid, $this->container);
    }

    /**
     * Deletes all this session container (not the session!)
     */
    public static function dispose($name)
    {
        if(isset(self::$instances[$name])) {
            self::$instances[$name]->delete_container();
            unset(self::$instances[$name]);
            return true;
        } else {
            return false;
        }
    }
    
    public static function clear()
    {
    
    }
}

endif;

?>
{'fraction_non_alphanumeric': 0.14071413479822523, 'fraction_numerical': 0.010669765476442003, 'mean_word_length': 1.7313906520484708, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2713261', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2663, 'n_words': 623}