Drupal investigation

MetadataBag.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Component\HttpFoundation\Session\Storage;
  11. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  12. /**
  13. * Metadata container.
  14. *
  15. * Adds metadata to the session.
  16. *
  17. * @author Drak <drak@zikula.org>
  18. */
  19. class MetadataBag implements SessionBagInterface
  20. {
  21. const CREATED = 'c';
  22. const UPDATED = 'u';
  23. const LIFETIME = 'l';
  24. /**
  25. * @var string
  26. */
  27. private $name = '__metadata';
  28. /**
  29. * @var string
  30. */
  31. private $storageKey;
  32. /**
  33. * @var array
  34. */
  35. protected $meta = array(self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0);
  36. /**
  37. * Unix timestamp.
  38. *
  39. * @var int
  40. */
  41. private $lastUsed;
  42. /**
  43. * @var int
  44. */
  45. private $updateThreshold;
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $storageKey The key used to store bag in the session
  50. * @param int $updateThreshold The time to wait between two UPDATED updates
  51. */
  52. public function __construct($storageKey = '_sf2_meta', $updateThreshold = 0)
  53. {
  54. $this->storageKey = $storageKey;
  55. $this->updateThreshold = $updateThreshold;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function initialize(array &$array)
  61. {
  62. $this->meta = &$array;
  63. if (isset($array[self::CREATED])) {
  64. $this->lastUsed = $this->meta[self::UPDATED];
  65. $timeStamp = time();
  66. if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
  67. $this->meta[self::UPDATED] = $timeStamp;
  68. }
  69. } else {
  70. $this->stampCreated();
  71. }
  72. }
  73. /**
  74. * Gets the lifetime that the session cookie was set with.
  75. *
  76. * @return int
  77. */
  78. public function getLifetime()
  79. {
  80. return $this->meta[self::LIFETIME];
  81. }
  82. /**
  83. * Stamps a new session's metadata.
  84. *
  85. * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
  86. * will leave the system settings unchanged, 0 sets the cookie
  87. * to expire with browser session. Time is in seconds, and is
  88. * not a Unix timestamp.
  89. */
  90. public function stampNew($lifetime = null)
  91. {
  92. $this->stampCreated($lifetime);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getStorageKey()
  98. {
  99. return $this->storageKey;
  100. }
  101. /**
  102. * Gets the created timestamp metadata.
  103. *
  104. * @return int Unix timestamp
  105. */
  106. public function getCreated()
  107. {
  108. return $this->meta[self::CREATED];
  109. }
  110. /**
  111. * Gets the last used metadata.
  112. *
  113. * @return int Unix timestamp
  114. */
  115. public function getLastUsed()
  116. {
  117. return $this->lastUsed;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function clear()
  123. {
  124. // nothing to do
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getName()
  130. {
  131. return $this->name;
  132. }
  133. /**
  134. * Sets name.
  135. *
  136. * @param string $name
  137. */
  138. public function setName($name)
  139. {
  140. $this->name = $name;
  141. }
  142. private function stampCreated($lifetime = null)
  143. {
  144. $timeStamp = time();
  145. $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
  146. $this->meta[self::LIFETIME] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime;
  147. }
  148. }