<commit_before><?php

/**
 * sfCacheSessionStorage manages session storage via a signed cookie and cache backend.
 *
 * This class stores the session data in via sfCache instance and with an id issued in a
 * signed cookie. Useful when you don't want to store the session.
 *
 * @package    symfony
 * @subpackage storage
 * @author     <NAME> <<EMAIL>>
 */
class sfCacheSessionStorage extends sfStorage
{
  protected
    $context     = null,
    $request     = null,
    $response    = null,
    $cache       = null;
    $data        = array(),
    $dataChanged = false;

  /**
   * Initialize this Storage.
   *
   * @param array   An associative array of initialization parameters.
   *                session_name [required] name of session to use
   *                session_cookie_path [required] cookie path
   *                session_cookie_domain [required] cookie domain
   *                session_cookie_lifetime [required] liftime of cookie
   *                session_cookie_secure [required] send only if secure connection
   *                session_cookie_http_only [required] accessible only via http protocol
   *
   * @return bool true, if initialization completes successfully, otherwise false.
   *
   * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage.
   */
  public function initialize($options = array())
  {
    // initialize parent
    parent::initialize(array_merge(array('session_name' => 'sfproject',
                                         'session_cookie_lifetime' => '+30 days',
                                         'session_cookie_path' => '/',
                                         'session_cookie_domain' => null,
                                         'session_cookie_secure' => false,
                                         'session_cookie_http_only' => true,
                                         'session_cookie_secret' => 'sf$ecret'), $options));

    // create cache instance
    if (isset($this->options['cache']) && $this->options['cache']['class'])
    {
      $this->cache = new $this->options['cache']['class'](is_array($this->options['cache']['param']) ? $this->options['cache']['param'] : array());
    }
    else
    {
      throw new InvalidArgumentException('sfCacheSessionStorage requires cache option.');
    }

    $this->context     = sfContext::getInstance();

    $this->dispatcher  = $this->context->getEventDispatcher();
    $this->request     = $this->context->getRequest();
    $this->response    = $this->context->getResponse();

    $cookie = $this->request->getCookie($this->options['session_name']);
    
    if(strpos($cookie, ':') !== false)
    {
      // split cookie data id:signature(id+secret)
      list($id, $signature) = explode(':', $cookie, 2);

      if($signature == sha1($id.':'.$this->options['session_cookie_secret']))
      {
        // cookie is valid
        $this->id = $id;
      }
      else
      {
        // cookie signature broken
        $this->id = null;
      }
    }
    else
    {
      // cookie format wrong
      $this->id = null;
    }

    if(empty($this->id))
    {
       $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'localhost';
       $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'ua';

       // generate new id based on random # / ip / user agent / secret
       $this->id = md5(rand(0, 999999).$ip.$ua.$this->options['session_cookie_secret']);

       if(sfConfig::get('sf_logging_enabled'))
       {
         $this->dispatcher->notify(new sfEvent($this, 'application.log', array('New session created')));
       }

       // only send cookie when id is issued
       $this->response->setCookie($this->options['session_name'],
                                  $this->id.':'.sha1($this->id.':'.$this->options['session_cookie_secret']),
                                  $this->options['session_cookie_lifetime'],
                                  $this->options['session_cookie_path'],
                                  $this->options['session_cookie_domain'],
                                  $this->options['session_cookie_secure'],
                                  $this->options['session_cookie_http_only']);

       $this->data = array();
    }
    else
    {
      // load data from cache
      $this->data = $this->cache->get($this->id, array());

      if(sfConfig::get('sf_logging_enabled'))
      {
        $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Restored previous session')));
        // $this->dispatcher->notify(new sfEvent($this, 'application.log', array(var_export($this->data, true))));
      }
    }

    return true;
  }

  /**
   * Write data to this storage.
   *
   * The preferred format for a key is directory style so naming conflicts can be avoided.
   *
   * @param string A unique key identifying your data.
   * @param mixed  Data associated with your key.
   *
   * @return void<commit_msg>[1.2] removed old code<commit_after><?php

/**
 * sfCacheSessionStorage manages session storage via a signed cookie and cache backend.
 *
 * This class stores the session data in via sfCache instance and with an id issued in a
 * signed cookie. Useful when you don't want to store the session.
 *
 * @package    symfony
 * @subpackage storage
 * @author     <NAME> <<EMAIL>>
 */
class sfCacheSessionStorage extends sfStorage
{
  protected
    $context     = null,
    $request     = null,
    $response    = null,
    $cache       = null;
    $data        = array(),
    $dataChanged = false;

  /**
   * Initialize this Storage.
   *
   * @param array   An associative array of initialization parameters.
   *                session_name [required] name of session to use
   *                session_cookie_path [required] cookie path
   *                session_cookie_domain [required] cookie domain
   *                session_cookie_lifetime [required] liftime of cookie
   *                session_cookie_secure [required] send only if secure connection
   *                session_cookie_http_only [required] accessible only via http protocol
   *
   * @return bool true, if initialization completes successfully, otherwise false.
   *
   * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage.
   */
  public function initialize($options = array())
  {
    // initialize parent
    parent::initialize(array_merge(array('session_name' => 'sfproject',
                                         'session_cookie_lifetime' => '+30 days',
                                         'session_cookie_path' => '/',
                                         'session_cookie_domain' => null,
                                         'session_cookie_secure' => false,
                                         'session_cookie_http_only' => true,
                                         'session_cookie_secret' => 'sf$ecret'), $options));

    // create cache instance
    if (isset($this->options['cache']) && $this->options['cache']['class'])
    {
      $this->cache = new $this->options['cache']['class'](is_array($this->options['cache']['param']) ? $this->options['cache']['param'] : array());
    }
    else
    {
      throw new InvalidArgumentException('sfCacheSessionStorage requires cache option.');
    }

    $this->context     = sfContext::getInstance();

    $this->dispatcher  = $this->context->getEventDispatcher();
    $this->request     = $this->context->getRequest();
    $this->response    = $this->context->getResponse();

    $cookie = $this->request->getCookie($this->options['session_name']);
    
    if(strpos($cookie, ':') !== false)
    {
      // split cookie data id:signature(id+secret)
      list($id, $signature) = explode(':', $cookie, 2);

      if($signature == sha1($id.':'.$this->options['session_cookie_secret']))
      {
        // cookie is valid
        $this->id = $id;
      }
      else
      {
        // cookie signature broken
        $this->id = null;
      }
    }
    else
    {
      // cookie format wrong
      $this->id = null;
    }

    if(empty($this->id))
    {
       $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'localhost';
       $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'ua';

       // generate new id based on random # / ip / user agent / secret
       $this->id = md5(rand(0, 999999).$ip.$ua.$this->options['session_cookie_secret']);

       if(sfConfig::get('sf_logging_enabled'))
       {
         $this->dispatcher->notify(new sfEvent($this, 'application.log', array('New session created')));
       }

       // only send cookie when id is issued
       $this->response->setCookie($this->options['session_name'],
                                  $this->id.':'.sha1($this->id.':'.$this->options['session_cookie_secret']),
                                  $this->options['session_cookie_lifetime'],
                                  $this->options['session_cookie_path'],
                                  $this->options['session_cookie_domain'],
                                  $this->options['session_cookie_secure'],
                                  $this->options['session_cookie_http_only']);

       $this->data = array();
    }
    else
    {
      // load data from cache
      $this->data = $this->cache->get($this->id, array());

      if(sfConfig::get('sf_logging_enabled'))
      {
        $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Restored previous session')));
      }
    }

    return true;
  }

  /**
   * Write data to this storage.
   *
   * The preferred format for a key is directory style so naming conflicts can be avoided.
   *
   * @param string A unique key identifying your data.
   * @param mixed  Data associated with your key.
   *
   * @return void
{'fraction_non_alphanumeric': 0.13616026032133416, 'fraction_numerical': 0.002847264592231035, 'mean_word_length': 2.131168417701369, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '359235', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2573, 'n_words': 619}