DbalSessionHandler.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\HttpFoundation;
  11. use Doctrine\DBAL\Platforms\MySqlPlatform;
  12. use Doctrine\DBAL\Driver\Connection;
  13. /**
  14. * DBAL based session storage.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. class DbalSessionHandler implements \SessionHandlerInterface
  20. {
  21. /**
  22. * @var Connection
  23. */
  24. private $con;
  25. /**
  26. * @var string
  27. */
  28. private $tableName;
  29. /**
  30. * Constructor.
  31. *
  32. * @param Connection $con An instance of Connection.
  33. * @param string $tableName Table name.
  34. */
  35. public function __construct(Connection $con, $tableName = 'sessions')
  36. {
  37. $this->con = $con;
  38. $this->tableName = $tableName;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function open($path = null, $name = null)
  44. {
  45. return true;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function close()
  51. {
  52. // do nothing
  53. return true;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function destroy($id)
  59. {
  60. try {
  61. $this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_id = :id", array(
  62. 'id' => $id,
  63. ));
  64. } catch (\PDOException $e) {
  65. throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
  66. }
  67. return true;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function gc($lifetime)
  73. {
  74. try {
  75. $this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array(
  76. 'time' => time() - $lifetime,
  77. ));
  78. } catch (\PDOException $e) {
  79. throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
  80. }
  81. return true;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function read($id)
  87. {
  88. try {
  89. $data = $this->con->executeQuery("SELECT sess_data FROM {$this->tableName} WHERE sess_id = :id", array(
  90. 'id' => $id,
  91. ))->fetchColumn();
  92. if (false !== $data) {
  93. return base64_decode($data);
  94. }
  95. // session does not exist, create it
  96. $this->createNewSession($id);
  97. return '';
  98. } catch (\PDOException $e) {
  99. throw new \RuntimeException(sprintf('PDOException was thrown when trying to read the session data: %s', $e->getMessage()), 0, $e);
  100. }
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function write($id, $data)
  106. {
  107. $platform = $this->con->getDatabasePlatform();
  108. // this should maybe be abstracted in Doctrine DBAL
  109. if ($platform instanceof MySqlPlatform) {
  110. $sql = "INSERT INTO {$this->tableName} (sess_id, sess_data, sess_time) VALUES (%1\$s, %2\$s, %3\$d) "
  111. ."ON DUPLICATE KEY UPDATE sess_data = VALUES(sess_data), sess_time = CASE WHEN sess_time = %3\$d THEN (VALUES(sess_time) + 1) ELSE VALUES(sess_time) END";
  112. } else {
  113. $sql = "UPDATE {$this->tableName} SET sess_data = %2\$s, sess_time = %3\$d WHERE sess_id = %1\$s";
  114. }
  115. try {
  116. $rowCount = $this->con->exec(sprintf(
  117. $sql,
  118. $this->con->quote($id),
  119. //session data can contain non binary safe characters so we need to encode it
  120. $this->con->quote(base64_encode($data)),
  121. time()
  122. ));
  123. if (!$rowCount) {
  124. // No session exists in the database to update. This happens when we have called
  125. // session_regenerate_id()
  126. $this->createNewSession($id, $data);
  127. }
  128. } catch (\PDOException $e) {
  129. throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
  130. }
  131. return true;
  132. }
  133. /**
  134. * Creates a new session with the given $id and $data
  135. *
  136. * @param string $id
  137. * @param string $data
  138. *
  139. * @return Boolean
  140. */
  141. private function createNewSession($id, $data = '')
  142. {
  143. $this->con->exec(sprintf("INSERT INTO {$this->tableName} (sess_id, sess_data, sess_time) VALUES (%s, %s, %d)",
  144. $this->con->quote($id),
  145. //session data can contain non binary safe characters so we need to encode it
  146. $this->con->quote(base64_encode($data)),
  147. time()
  148. ));
  149. return true;
  150. }
  151. }