PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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