PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/symphony/lib/core/class.cookie.php

https://github.com/bauhouse/sym-designprojectx
PHP | 202 lines | 62 code | 24 blank | 116 comment | 13 complexity | bb9e0d931800d04216a13369b424f979 MD5 | raw file
  1. <?php
  2. /**
  3. * @package core
  4. */
  5. if (!defined('__IN_SYMPHONY__')) {
  6. die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
  7. }
  8. /**
  9. * The Cookie class is a wrapper to save Symphony cookies. Typically this
  10. * is used to maintain if an Author is logged into Symphony, or by extensions
  11. * to determine similar things. The Cookie class is tightly integrated with
  12. * PHP's `$_SESSION` global and it's related functions.
  13. */
  14. class Cookie
  15. {
  16. /**
  17. * Used to prevent Symphony cookies from completely polluting the
  18. * `$_SESSION` array. This will act as a key and all
  19. * cookies will live under that key. By default, the index is read from
  20. * the Symphony configuration, and unless changed, is `sym-`
  21. *
  22. * @var string
  23. */
  24. private $_index;
  25. /**
  26. * This variable determines if the Cookie was set by the Symphony Session
  27. * class, or if it was set directly. By default, this is false as the Symphony cookie
  28. * created directly in the Symphony constructor, otherwise it will be an instance
  29. * of the Session class
  30. *
  31. * @see core.Symphony#__construct()
  32. * @var Session|boolean
  33. */
  34. private $_session = false;
  35. /**
  36. * How long this cookie is valid for. By default, this is 0 if used by an extension,
  37. * but it is usually set for 2 weeks in the Symphony context.
  38. *
  39. * @var integer
  40. */
  41. private $_timeout = 0;
  42. /**
  43. * The path that this cookie is valid for, by default Symphony makes this the whole
  44. * domain using /
  45. *
  46. * @var string
  47. */
  48. private $_path;
  49. /**
  50. * The domain that this cookie is valid for. This is null by default which implies
  51. * the entire domain and all subdomains created will have access to this cookie.
  52. *
  53. * @var string
  54. */
  55. private $_domain;
  56. /**
  57. * Determines whether this cookie can be read by Javascript or not, by default
  58. * this is set to true, meaning cookies written by Symphony cannot be read by Javascript
  59. *
  60. * @var boolean
  61. */
  62. private $_httpOnly = true;
  63. /**
  64. * Determines whether this cookie will be sent over a secure connection or not. If
  65. * true, this cookie will only be sent on a secure connection. Defaults to false
  66. * but will automatically be set if `__SECURE__` is true
  67. *
  68. * @since Symphony 2.3.3
  69. * @see boot
  70. * @var boolean
  71. */
  72. private $_secure = false;
  73. /**
  74. * Constructor for the Cookie class intialises all class variables with the
  75. * given parameters. Most of the parameters map to PHP's setcookie
  76. * function. It creates a new Session object via the `$this->__init()`
  77. *
  78. * @see __init()
  79. * @link http://php.net/manual/en/function.setcookie.php
  80. * @param string $index
  81. * The prefix to used to namespace all Symphony cookies
  82. * @param integer $timeout
  83. * The Time to Live for a cookie, by default this is zero, meaning the
  84. * cookie never expires
  85. * @param string $path
  86. * The path the cookie is valid for on the domain
  87. * @param string $domain
  88. * The domain this cookie is valid for
  89. * @param boolean $httpOnly
  90. * Whether this cookie can be read by Javascript. By default the cookie
  91. * cannot be read by Javascript
  92. * @throws Exception
  93. */
  94. public function __construct($index, $timeout = 0, $path = '/', $domain = null, $httpOnly = true)
  95. {
  96. $this->_index = $index;
  97. $this->_timeout = $timeout;
  98. $this->_path = $path;
  99. $this->_domain = $domain;
  100. $this->_httpOnly = $httpOnly;
  101. if (defined('__SECURE__')) {
  102. $this->_secure = __SECURE__;
  103. }
  104. $this->_session = $this->__init();
  105. }
  106. /**
  107. * Initialises a new Session instance using this cookie's params
  108. *
  109. * @throws Throwable
  110. * @return string|boolean
  111. */
  112. private function __init()
  113. {
  114. $this->_session = Session::start($this->_timeout, $this->_path, $this->_domain, $this->_httpOnly, $this->_secure);
  115. if (!$this->_session) {
  116. return false;
  117. }
  118. if (!isset($_SESSION[$this->_index])) {
  119. $_SESSION[$this->_index] = array();
  120. }
  121. // Class FrontendPage uses $_COOKIE directly (inside it's __buildPage() function), so try to emulate it.
  122. // @deprecated will be removed in Symphony 3.0.0
  123. $_COOKIE[$this->_index] = &$_SESSION[$this->_index];
  124. return $this->_session;
  125. }
  126. /**
  127. * A basic setter, which will set a value to a given property in the
  128. * `$_SESSION` array, stored in the key of `$this->_index`
  129. *
  130. * @param string $name
  131. * The name of the property
  132. * @param string $value
  133. * The value of the property
  134. */
  135. public function set($name, $value)
  136. {
  137. $_SESSION[$this->_index][$name] = $value;
  138. }
  139. /**
  140. * Accessor function for properties in the `$_SESSION` array
  141. *
  142. * @param string $name
  143. * The name of the property to retrieve (optional)
  144. * @return string|null
  145. * The value of the property, or null if it does not exist. If
  146. * no `$name` is provided, return the entire Cookie.
  147. */
  148. public function get($name = null)
  149. {
  150. if (is_null($name) && isset($_SESSION[$this->_index])) {
  151. return $_SESSION[$this->_index];
  152. }
  153. if (isset($_SESSION[$this->_index]) && is_array($_SESSION[$this->_index]) && array_key_exists($name, $_SESSION[$this->_index])) {
  154. return $_SESSION[$this->_index][$name];
  155. }
  156. return null;
  157. }
  158. /**
  159. * Expires the current `$_SESSION` by unsetting the Symphony
  160. * namespace (`$this->_index`). If the `$_SESSION`
  161. * is empty, the function will destroy the entire `$_SESSION`
  162. *
  163. * @link http://au2.php.net/manual/en/function.session-destroy.php
  164. */
  165. public function expire()
  166. {
  167. if (!isset($_SESSION[$this->_index]) || !is_array($_SESSION[$this->_index]) || empty($_SESSION[$this->_index])) {
  168. return;
  169. }
  170. unset($_SESSION[$this->_index]);
  171. // Calling session_destroy triggers the Session::destroy function which removes the entire session
  172. // from the database. To prevent logout issues between functionality that relies on $_SESSION, such
  173. // as Symphony authentication or the Members extension, only delete the $_SESSION if it empty!
  174. if (empty($_SESSION)) {
  175. session_destroy();
  176. }
  177. }
  178. }