/lib/Ld/Auth/Storage/Cookie.php

https://github.com/ladistribution/ladistribution · PHP · 152 lines · 84 code · 22 blank · 46 comment · 8 complexity · 00514a587b4864f53a6a13c7fff48969 MD5 · raw file

  1. <?php
  2. /**
  3. * La Distribution PHP libraries
  4. *
  5. * @category Ld
  6. * @package Ld_Auth
  7. * @subpackage Ld_Auth_Storage
  8. * @author Matthieu Huguet (http://bigornot.blogspot.com/)
  9. * @copyright Copyright (c) 2009 h6e / François Hodierne (http://h6e.net)
  10. * @license Dual licensed under the MIT and GPL licenses.
  11. * @version $Id$
  12. */
  13. class Ld_Auth_Storage_Cookie implements Zend_Auth_Storage_Interface
  14. {
  15. /* Instance of Ld_Cookie */
  16. protected $_cookieManager = null;
  17. /* Cookie configuration (see setcookie() for details) */
  18. protected $_cookieName = 'auth';
  19. protected $_cookieExpire = 0;
  20. protected $_cookiePath = '/';
  21. protected $_cookieDomain = '';
  22. protected $_cookieSecure = false;
  23. protected $_cookieHttpOnly = false;
  24. /* Local "cache" to store cookie value, in order to not waste
  25. time in cryptographic functions of cookieManager->getCookieValue() */
  26. protected $_cache = null;
  27. /**
  28. * Constructor
  29. *
  30. * Available configuration options are :
  31. * cookieName, cookieExpire, cookiePath, cookieDomain, cookieSecure, cookieHttpOnly
  32. * (see native setcookie() documentation for more details)
  33. *
  34. * @param Ld_Cookie $cookieManager the secure cookie manager
  35. * @param array|Zend_Config $config configuration
  36. */
  37. public function __construct($cookieManager, $config = array())
  38. {
  39. $this->_cookieManager = $cookieManager;
  40. if ($config instanceof Zend_Config) {
  41. $config = $config->toArray();
  42. }
  43. $this->setOptions($config);
  44. }
  45. public function setOptions($options)
  46. {
  47. if (is_array($options)) {
  48. $keys = array('cookieName', 'cookieExpire', 'cookiePath',
  49. 'cookieDomain', 'cookieSecure', 'cookieHttpOnly');
  50. foreach ($keys as $key) {
  51. if (array_key_exists($key, $options)) {
  52. $property = "_$key";
  53. $this->$property = $options[$key];
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * Defined by Zend_Auth_Storage_Interface
  60. *
  61. * @return boolean
  62. */
  63. public function isEmpty()
  64. {
  65. if ($this->_cache) {
  66. return false;
  67. } elseif ($content = $this->_cookieManager->getCookieValue($this->_cookieName)) {
  68. $this->_cache = unserialize($content);
  69. return false;
  70. } else {
  71. return true;
  72. }
  73. }
  74. /**
  75. * Defined by Zend_Auth_Storage_Interface
  76. *
  77. * @return mixed
  78. */
  79. public function read()
  80. {
  81. if ($this->_cache) {
  82. return $this->_cache;
  83. } elseif ($this->_cookieManager->cookieExists($this->_cookieName)) {
  84. if ($content = $this->_cookieManager->getCookieValue($this->_cookieName)) {
  85. $this->_cache = unserialize($content);
  86. return $this->_cache;
  87. }
  88. } else {
  89. return null;
  90. }
  91. }
  92. /**
  93. * Defined by Zend_Auth_Storage_Interface
  94. *
  95. * @return void
  96. * @param mixed $contents
  97. */
  98. public function write($contents)
  99. {
  100. $this->_cache = $contents;
  101. $serializedContent = serialize($contents);
  102. $userIdentifier = substr(md5($serializedContent), 0, 10);
  103. $this->_cookieManager->setCookie(
  104. $this->_cookieName,
  105. $serializedContent,
  106. $userIdentifier,
  107. $this->_cookieExpire,
  108. $this->_cookiePath,
  109. $this->_cookieDomain,
  110. $this->_cookieSecure,
  111. $this->_cookieHttpOnly
  112. );
  113. }
  114. /**
  115. * Defined by Zend_Auth_Storage_Interface
  116. *
  117. * @return void
  118. */
  119. public function clear()
  120. {
  121. $this->_cache = null;
  122. $this->_cookieManager->deleteCookie(
  123. $this->_cookieName,
  124. $this->_cookiePath,
  125. $this->_cookieDomain,
  126. $this->_cookieSecure,
  127. $this->_cookieHttpOnly
  128. );
  129. }
  130. }