flag.cookie_storage.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @file
  4. * Contains the FlagCookieStorage class.
  5. */
  6. /**
  7. * Utility class to handle cookies.
  8. *
  9. * Cookies are used to record flaggings for anonymous users on cached pages.
  10. *
  11. * This class contains only two instance methods. Usage example:
  12. * @code
  13. * $storage = FlagCookieStorage::factory($flag);
  14. * $storage->flag(145);
  15. * $storage->unflag(17);
  16. * @endcode
  17. *
  18. * You may delete all the cookies with <code>FlagCookieStorage::drop()</code>.
  19. */
  20. abstract class FlagCookieStorage {
  21. /**
  22. * Returns the actual storage object compatible with the flag.
  23. */
  24. static function factory($flag) {
  25. if ($flag->global) {
  26. return new FlagGlobalCookieStorage($flag);
  27. }
  28. else {
  29. return new FlagNonGlobalCookieStorage($flag);
  30. }
  31. }
  32. function __construct($flag) {
  33. $this->flag = $flag;
  34. }
  35. /**
  36. * "Flags" an item.
  37. *
  38. * It just records this fact in a cookie.
  39. */
  40. abstract function flag($entity_id);
  41. /**
  42. * "Unflags" an item.
  43. *
  44. * It just records this fact in a cookie.
  45. */
  46. abstract function unflag($entity_id);
  47. /**
  48. * Deletes all the cookies.
  49. *
  50. * (Etymology: "drop" as in "drop database".)
  51. */
  52. static function drop() {
  53. FlagGlobalCookieStorage::drop();
  54. FlagNonGlobalCookieStorage::drop();
  55. }
  56. }
  57. /**
  58. * Storage handler for global flags.
  59. */
  60. class FlagGlobalCookieStorage extends FlagCookieStorage {
  61. function flag($entity_id) {
  62. $cookie_key = $this->cookie_key($entity_id);
  63. setcookie($cookie_key, 1, REQUEST_TIME + $this->get_lifetime(), base_path());
  64. $_COOKIE[$cookie_key] = 1;
  65. }
  66. function unflag($entity_id) {
  67. $cookie_key = $this->cookie_key($entity_id);
  68. setcookie($cookie_key, 0, REQUEST_TIME + $this->get_lifetime(), base_path());
  69. $_COOKIE[$cookie_key] = 0;
  70. }
  71. // Global flags persist for the length of the minimum cache lifetime.
  72. protected function get_lifetime() {
  73. $cookie_lifetime = variable_get('cache', 0) ? variable_get('cache_lifetime', 0) : -1;
  74. // Do not let the cookie lifetime be 0 (which is the no cache limit on
  75. // anonymous page caching), since it would expire immediately. Usually
  76. // the no cache limit means caches are cleared on cron, which usually runs
  77. // at least once an hour.
  78. if ($cookie_lifetime == 0) {
  79. $cookie_lifetime = 3600;
  80. }
  81. return $cookie_lifetime;
  82. }
  83. protected function cookie_key($entity_id) {
  84. return 'flag_global_' . $this->flag->name . '_' . $entity_id;
  85. }
  86. /**
  87. * Deletes all the global cookies.
  88. */
  89. static function drop() {
  90. foreach ($_COOKIE as $key => $value) {
  91. if (strpos($key, 'flag_global_') === 0) {
  92. setcookie($key, FALSE, 0, base_path());
  93. unset($_COOKIE[$key]);
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Storage handler for non-global flags.
  100. */
  101. class FlagNonGlobalCookieStorage extends FlagCookieStorage {
  102. // The anonymous per-user flaggings are stored in a single cookie, so that
  103. // all of them persist as long as the Drupal cookie lifetime.
  104. function __construct($flag) {
  105. parent::__construct($flag);
  106. $this->flaggings = isset($_COOKIE['flags']) ? explode(' ', $_COOKIE['flags']) : array();
  107. }
  108. function flag($entity_id) {
  109. if (!$this->is_flagged($entity_id)) {
  110. $this->flaggings[] = $this->cookie_key($entity_id);
  111. $this->write();
  112. }
  113. }
  114. function unflag($entity_id) {
  115. if (($index = $this->index_of($entity_id)) !== FALSE) {
  116. unset($this->flaggings[$index]);
  117. $this->write();
  118. }
  119. }
  120. protected function get_lifetime() {
  121. return min((int) ini_get('session.cookie_lifetime'), (int) ini_get('session.gc_maxlifetime'));
  122. }
  123. protected function cookie_key($entity_id) {
  124. return $this->flag->name . '_' . $entity_id;
  125. }
  126. protected function write() {
  127. $serialized = implode(' ', array_filter($this->flaggings));
  128. setcookie('flags', $serialized, REQUEST_TIME + $this->get_lifetime(), base_path());
  129. $_COOKIE['flags'] = $serialized;
  130. }
  131. protected function is_flagged($entity_id) {
  132. return $this->index_of($entity_id) !== FALSE;
  133. }
  134. protected function index_of($entity_id) {
  135. return array_search($this->cookie_key($entity_id), $this->flaggings);
  136. }
  137. /**
  138. * Deletes the cookie.
  139. */
  140. static function drop() {
  141. if (isset($_COOKIE['flags'])) {
  142. setcookie('flags', FALSE, 0, base_path());
  143. unset($_COOKIE['flags']);
  144. }
  145. }
  146. }