<commit_before><?php
/**
 * Controls the COOKIES of the application.
 * 
 * This class is part of the Lalaz Framework
 * Lalaz it's a open source MVA (Model View Action) Framework writen in PHP
 * 
 * @author <NAME> (<EMAIL>)
 * @copyright Copyright 2010, WMD (http://www.wmdweb.com.br)
 */
class Cookie extends Singleton {
    
    public $expires;
    public $path = "/";
    public $domain = "";
    public $secure = false;
    public $key = "#@lalaz@#!";
    public $name = "LalazCookie";
    
    protected function __construct() {}
    
    public static function set($key, $value) {
        $self = self::getInstance("Cookie");
        
        if(isset($self->$key)) {
            $self->$key = $value;
            return true;
        }
        
        return false;
    }

    public static function get($key) {
        $self = self::getInstance("Cookie");
        
        if(isset($self->$key)) {
            return $self->$key;
        }
        
        return null;
    }

    public static function delete($name) {
        $self = self::getInstance("Cookie");
        $path = Mapper::normalize(Mapper::base() . $self->path);
        return setcookie("{$self->name}[{$name}]", "", time() - 42000, $path, $self->domain, $self->secure);
    }
    
    public static function read($name) {
        $self = self::getInstance("Cookie");
        return self::decrypt($_COOKIE[$self->name][$name]);
    }

    public static function write($name, $value, $expires = null) {
        $self = self::getInstance("Cookie");
        $expires = $self->expire($expires);
        $path = Mapper::normalize(Mapper::base() . $self->path);
        return setcookie("{$self->name}[{$name}]", self::encrypt($value), $expires, $path, $self->domain, $self->secure);
    }

    public static function encrypt($value) {
        $self = self::getInstance("Cookie");
        $encripted = base64_encode(Security::cipher($value, $self->key));
        return "U3BhZ2hldHRp.{$encripted}";
    }

    public static function decrypt($value) {
        $self = self::getInstance("Cookie");
        $prefix = strpos($value, "U3BhZ2hldHRp.");
        
        if($prefix !== false) {
            $encrypted = base64_decode(substr($value, $prefix + 13));
            return Security::cipher($encrypted, $self->key);
        }
        
        return false;
    }
    
    public function expire($expires) {
        if(is_null($expires)) {
            $expires = $this->expires;
        }
        
        $now = time();
        
        if(is_numeric($expires)) {
            return $this->expires = $now + $expires;
        } else {
            return $this->expires = strtotime($expires, $now);
        }
    }
}
?><commit_msg>Fix read method from cookie class<commit_after><?php
/**
 * Controls the COOKIES of the application.
 * 
 * This class is part of the Lalaz Framework
 * Lalaz it's a open source MVA (Model View Action) Framework writen in PHP
 * 
 * @author <NAME> (<EMAIL>)
 * @copyright Copyright 2010, WMD (http://www.wmdweb.com.br)
 */
class Cookie extends Singleton {
    
    public $expires;
    public $path = "/";
    public $domain = "";
    public $secure = false;
    public $key = "#@lalaz@#!";
    public $name = "LalazCookie";
    
    protected function __construct() {}
    
    public static function set($key, $value) {
        $self = self::getInstance("Cookie");
        
        if(isset($self->$key)) {
            $self->$key = $value;
            return true;
        }
        
        return false;
    }

    public static function get($key) {
        $self = self::getInstance("Cookie");
        
        if(isset($self->$key)) {
            return $self->$key;
        }
        
        return null;
    }

    public static function delete($name) {
        $self = self::getInstance("Cookie");
        $path = Mapper::normalize(Mapper::base() . $self->path);
        return setcookie("{$self->name}[{$name}]", "", time() - 42000, $path, $self->domain, $self->secure);
    }
    
    public static function read($name) {
        $self = self::getInstance("Cookie");
        return self::decrypt($_COOKIE[$self->name][$name]);
    }

    public static function write($name, $value, $expires = null) {
        $self = self::getInstance("Cookie");
        $expires = $self->expire($expires);
        $path = UrlRouter::normalize(UrlRouter::base() . $self->path);
        return setcookie("{$self->name}[{$name}]", self::encrypt($value), $expires, $path, $self->domain, $self->secure);
    }

    public static function encrypt($value) {
        $self = self::getInstance("Cookie");
        $encripted = base64_encode(Security::cipher($value, $self->key));
        return "U3BhZ2hldHRp.{$encripted}";
    }

    public static function decrypt($value) {
        $self = self::getInstance("Cookie");
        $prefix = strpos($value, "U3BhZ2hldHRp.");
        
        if($prefix !== false) {
            $encrypted = base64_decode(substr($value, $prefix + 13));
            return Security::cipher($encrypted, $self->key);
        }
        
        return false;
    }
    
    public function expire($expires) {
        if(is_null($expires)) {
            $expires = $this->expires;
        }
        
        $now = time();
        
        if(is_numeric($expires)) {
            return $this->expires = $now + $expires;
        } else {
            return $this->expires = strtotime($expires, $now);
        }
    }
}
?>
{'fraction_non_alphanumeric': 0.16403089371092314, 'fraction_numerical': 0.006987863184994484, 'mean_word_length': 2.2607913669064748, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4598046', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1631, 'n_words': 359}