123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace Symfony\Component\HttpFoundation\SessionStorage;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * PdoSessionStorage.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class PdoSessionStorage extends NativeSessionStorage
- {
- protected $db;
- /**
- * @throws \InvalidArgumentException When "db_table" option is not provided
- */
- public function __construct(\PDO $db, $options = null)
- {
- $this->db = $db;
- $options = array_merge(array(
- 'db_id_col' => 'sess_id',
- 'db_data_col' => 'sess_data',
- 'db_time_col' => 'sess_time',
- ), $options);
- if (!array_key_exists('db_table', $options)) {
- throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
- }
- parent::__construct($options);
- }
- /**
- * Starts the session.
- */
- public function start()
- {
- if (self::$sessionStarted) {
- return;
- }
- // use this object as the session handler
- session_set_save_handler(
- array($this, 'sessionOpen'),
- array($this, 'sessionClose'),
- array($this, 'sessionRead'),
- array($this, 'sessionWrite'),
- array($this, 'sessionDestroy'),
- array($this, 'sessionGC')
- );
- parent::start();
- }
- /**
- * Opens a session.
- *
- * @param string $path (ignored)
- * @param string $name (ignored)
- *
- * @return boolean true, if the session was opened, otherwise an exception is thrown
- */
- public function sessionOpen($path = null, $name = null)
- {
- return true;
- }
- /**
- * Closes a session.
- *
- * @return boolean true, if the session was closed, otherwise false
- */
- public function sessionClose()
- {
- // do nothing
- return true;
- }
- /**
- * Destroys a session.
- *
- * @param string $id A session ID
- *
- * @return bool true, if the session was destroyed, otherwise an exception is thrown
- *
- * @throws \RuntimeException If the session cannot be destroyed
- */
- public function sessionDestroy($id)
- {
- // get table/column
- $db_table = $this->options['db_table'];
- $db_id_col = $this->options['db_id_col'];
- // delete the record associated with this id
- $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
- try {
- $stmt = $this->db->prepare($sql);
- $stmt->bindParam(1, $id, \PDO::PARAM_STR);
- $stmt->execute();
- } catch (\PDOException $e) {
- throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
- }
- return true;
- }
- /**
- * Cleans up old sessions.
- *
- * @param int $lifetime The lifetime of a session
- *
- * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
- *
- * @throws \RuntimeException If any old sessions cannot be cleaned
- */
- public function sessionGC($lifetime)
- {
- // get table/column
- $db_table = $this->options['db_table'];
- $db_time_col = $this->options['db_time_col'];
- // delete the record associated with this id
- $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
- try {
- $this->db->query($sql);
- } catch (\PDOException $e) {
- throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
- }
- return true;
- }
- /**
- * Reads a session.
- *
- * @param string $id A session ID
- *
- * @return string The session data if the session was read or created, otherwise an exception is thrown
- *
- * @throws \RuntimeException If the session cannot be read
- */
- public function sessionRead($id)
- {
- // get table/columns
- $db_table = $this->options['db_table'];
- $db_data_col = $this->options['db_data_col'];
- $db_id_col = $this->options['db_id_col'];
- $db_time_col = $this->options['db_time_col'];
- try {
- $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
- $stmt = $this->db->prepare($sql);
- $stmt->bindParam(1, $id, \PDO::PARAM_STR, 255);
- $stmt->execute();
- // it is recommended to use fetchAll so that PDO can close the DB cursor
- // we anyway expect either no rows, or one row with one column. fetchColumn, seems to be buggy #4777
- $sessionRows = $stmt->fetchAll(\PDO::FETCH_NUM);
- if (count($sessionRows) == 1) {
- return $sessionRows[0][0];
- } else {
- // session does not exist, create it
- $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
- $stmt = $this->db->prepare($sql);
- $stmt->bindParam(1, $id, \PDO::PARAM_STR);
- $stmt->bindValue(2, '', \PDO::PARAM_STR);
- $stmt->bindValue(3, time(), \PDO::PARAM_INT);
- $stmt->execute();
- return '';
- }
- } catch (\PDOException $e) {
- throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
- }
- }
- /**
- * Writes session data.
- *
- * @param string $id A session ID
- * @param string $data A serialized chunk of session data
- *
- * @return bool true, if the session was written, otherwise an exception is thrown
- *
- * @throws \RuntimeException If the session data cannot be written
- */
- public function sessionWrite($id, $data)
- {
- // get table/column
- $db_table = $this->options['db_table'];
- $db_data_col = $this->options['db_data_col'];
- $db_id_col = $this->options['db_id_col'];
- $db_time_col = $this->options['db_time_col'];
- $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
- try {
- $stmt = $this->db->prepare($sql);
- $stmt->bindParam(1, $data, \PDO::PARAM_STR);
- $stmt->bindParam(2, $id, \PDO::PARAM_STR);
- $stmt->execute();
- } catch (\PDOException $e) {
- throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
- }
- return true;
- }
- }
|