Development

Changeset 10426

You must first sign up to be able to contribute.

Changeset 10426

Show
Ignore:
Timestamp:
07/22/08 17:03:31 (4 months ago)
Author:
nicolas
Message:

[1.2] closes #4053 - fixed regenerated session id wasn't reflected in database based session storage handlers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/lib/storage/sfDatabaseSessionStorage.class.php

    r9663 r10426  
    103103    $this->db = $database->getResource(); 
    104104    $this->con = $database->getConnection(); 
     105     
    105106    if (is_null($this->db) && is_null($this->con)) 
    106107    { 
     
    157158 
    158159  /** 
     160   * Regenerates id that represents this storage. 
     161   * 
     162   * @param  boolean $destroy Destroy session when regenerating? 
     163   * 
     164   * @return boolean True if session regenerated, false if error 
     165   * 
     166   */ 
     167  public function regenerate($destroy = false) 
     168  { 
     169    if (self::$sessionIdRegenerated) 
     170    { 
     171      return; 
     172    } 
     173 
     174    $currentId = session_id(); 
     175 
     176    parent::regenerate($destroy); 
     177 
     178    $newId = session_id(); 
     179 
     180    $this->updateSessionId($currentId, $newId); 
     181  } 
     182 
     183  /** 
     184   * Updates the session id. 
     185   * 
     186   * @param  string   $currentId The current session id 
     187   * @param  string   $newId     The new current id 
     188   *  
     189   * @return Boolean  True if the session id was successfully regenerated 
     190   * @throws sfDatabaseException if an error occured while regenrating the session id 
     191   */ 
     192  abstract protected function updateSessionId($currentId, $newId); 
     193 
     194  /** 
    159195   * Executes the shutdown procedure. 
    160196   * 
  • branches/1.2/lib/storage/sfMySQLSessionStorage.class.php

    r10116 r10426  
    8686   * @param  string $id  A session ID 
    8787   * 
    88    * @return bool true, if the session was read, otherwise an exception is thrown 
     88   * @return string      The session data if the session was read or created, otherwise an exception is thrown 
    8989   * 
    9090   * @throws <b>sfDatabaseException</b> If the session cannot be read 
     
    161161  } 
    162162 
    163   /*! 
    164    * Execute an SQL Query 
     163  /** 
     164   * Updates the session id. 
     165   * 
     166   * @param  string   $currentId The current session id 
     167   * @param  string   $newId     The new current id 
     168   *  
     169   * @return Boolean  True if the session id was successfully regenerated 
     170   * @throws sfDatabaseException if an error occured while regenrating the session id 
     171   */ 
     172  protected function updateSessionId($currentId, $newId) 
     173  { 
     174    // get table/column 
     175    $db_table    = $this->options['db_table']; 
     176    $db_id_col   = $this->options['db_id_col']; 
     177 
     178    // cleanup the session ids, just in case 
     179    $newId     = $this->db_escape($newId); 
     180    $currentId = $this->db_escape($currentId); 
     181 
     182    // update the session id 
     183    $sql = "UPDATE $db_table SET $db_id_col='$newId' WHERE $db_id_col='$currentId'"; 
     184 
     185    if ($this->db_query($sql)) 
     186    { 
     187      return true; 
     188    } 
     189 
     190    // failed to write session data 
     191    throw new sfDatabaseException(sprintf('%s cannot update session id from "%s" to "%s" (%s).', get_class($this), $currentId, $newId, mysql_error())); 
     192  } 
     193 
     194  /** 
     195   * Executes an SQL Query 
    165196   * 
    166197   * @param  string $query  The query to execute 
     
    172203  } 
    173204 
    174   /*! 
    175    * Escape a string before using it in a query statement 
     205  /** 
     206   * Escapes a string before using it in a query statement 
    176207   * 
    177208   * @param  string $string  The string to escape 
     
    183214  } 
    184215 
    185   /*! 
    186    * Count the rows in a query result 
     216  /** 
     217   * Counts the rows in a query result 
    187218   * 
    188219   * @param  resource $result  Result of a query 
     
    194225  } 
    195226 
    196   /*! 
    197    * Extract a row from a query result set 
     227  /** 
     228   * Extracts a row from a query result set 
    198229   * 
    199230   * @param  resource $result  Result of a query 
  • branches/1.2/lib/storage/sfPDOSessionStorage.class.php

    r9089 r10426  
    5252      throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); 
    5353    } 
     54     
     55    return true; 
    5456  } 
    5557 
     
    8991   * @param  string $id  A session ID 
    9092   * 
    91    * @return bool true, if the session was read, otherwise an exception is thrown 
     93   * @return string      The session data if the session was read or created, otherwise an exception is thrown 
    9294   * 
    9395   * @throws <b>DatabaseException</b> If the session cannot be read 
     
    165167    } 
    166168 
    167     return false; 
     169    return true; 
     170  } 
     171 
     172  /** 
     173   * Updates the session id. 
     174   * 
     175   * @param  string   $currentId The current session id 
     176   * @param  string   $newId     The new current id 
     177   *  
     178   * @return Boolean  True if the session id was successfully regenerated 
     179   * @throws sfDatabaseException if an error occured while regenrating the session id 
     180   */ 
     181  protected function updateSessionId($currentId, $newId) 
     182  { 
     183    // get table/column 
     184    $db_table    = $this->options['db_table']; 
     185    $db_id_col   = $this->options['db_id_col']; 
     186 
     187    // update the session id 
     188    $sql = "UPDATE $db_table SET $db_id_col=? WHERE $db_id_col=?"; 
     189 
     190    try 
     191    { 
     192      $stmt = $this->db->prepare($sql); 
     193      $stmt->bindParam(1, $currentId, PDO::PARAM_STR); 
     194      $stmt->bindParam(2, $newId, PDO::PARAM_STR); 
     195      $stmt->execute(); 
     196    } 
     197    catch (PDOException $e) 
     198    { 
     199      throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); 
     200    } 
     201 
     202    return true; 
    168203  } 
    169204} 
  • branches/1.2/lib/storage/sfPostgreSQLSessionStorage.class.php

    r9089 r10426  
    8484   * @param  string $id  A session ID 
    8585   * 
    86    * @return bool true, if the session was read, otherwise an exception is thrown 
     86   * @return string      The session data if the session was read or created, otherwise an exception is thrown 
    8787   * 
    8888   * @throws <b>sfDatabaseException</b> If the session cannot be read 
     
    159159    throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot write session data for id "%s".', $id)); 
    160160  } 
     161 
     162  /** 
     163   * Updates the session id. 
     164   * 
     165   * @param  string   $currentId The current session id 
     166   * @param  string   $newId     The new current id 
     167   *  
     168   * @return Boolean  True if the session id was successfully regenerated 
     169   * @throws sfDatabaseException if an error occured while regenrating the session id 
     170   */ 
     171  protected function updateSessionId($currentId, $newId) 
     172  { 
     173    // get table/column 
     174    $db_table    = $this->options['db_table']; 
     175    $db_id_col   = $this->options['db_id_col']; 
     176 
     177    // cleanup the session id and data, just in case 
     178    $currentId = addslashes($currentId); 
     179    $newId     = addslashes($newId); 
     180 
     181    // update the session id 
     182    $sql = "UPDATE $db_table SET $db_id_col=? WHERE $db_id_col=?"; 
     183 
     184    if (@pg_query($this->db, $sql)) 
     185    { 
     186      return true; 
     187    } 
     188 
     189    // failed to write session data 
     190    throw new sfDatabaseException(sprintf('% cannot update session id from "%s" to "%s".', get_class($this), $currentId, $newId)); 
     191  } 
    161192} 
  • branches/1.2/lib/storage/sfSessionStorage.class.php

    r9942 r10426  
    2727{ 
    2828  static protected 
    29     $sessionStarted = false; 
     29    $sessionIdRegenerated = false, 
     30    $sessionStarted       = false; 
    3031 
    3132  /** 
     
    163164  public function regenerate($destroy = false) 
    164165  { 
    165     // regenerate a new session id 
     166    if (self::$sessionIdRegenerated) 
     167    { 
     168      return; 
     169    } 
     170 
     171    // regenerate a new session id once per object 
    166172    session_regenerate_id($destroy); 
     173 
     174    self::$sessionIdRegenerated = true; 
    167175  } 
    168176