Source for file Cookie.php

Documentation is available at Cookie.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23. import('sylar.common.system.Logger');
  24. import('sylar.common.system.ConfigBox');
  25.  
  26. /**
  27. * Cookie
  28. *
  29. * Class to use cookie
  30. *
  31. * @package Sylar
  32. * @version 1.0
  33. * @since 18/apr/08
  34. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  35. * @copyright Sylar Development Team
  36. */
  37. class Sylar_Cookie{
  38. /**
  39. * web server path where the cookie is valid
  40. * @var string
  41. */
  42. private $sPathOnServer;
  43. /**
  44. * Eventually prefix for cookie name
  45. * @var string
  46. */
  47. private $sKeyPrefix;
  48. /**
  49. * expiration time for cookie
  50. * @var int
  51. */
  52. private $iExpireTime;
  53. /**
  54. * Domain for example .giano-solutions.com
  55. * @var string
  56. */
  57. private $sDomain;
  58. /**
  59. * Class constructor
  60. *
  61. * @return void
  62. *
  63. * @param string $domain
  64. * @param string $directory
  65. * @param string $keyPrefix
  66. */
  67. function __construct($domain=null, $directory=null, $keyPrefix=null){
  68. // set eventually Key Prefix
  69. if(!is_null($keyPrefix))
  70. $this->setKeyPrefix($keyPrefix);
  71. // set an eventually directory
  72. if(!is_null($directory))
  73. $this->setPathOnServer($directory);
  74. // set an eventually domain
  75. if(!is_null($domain))
  76. $this->setDomain($domain);
  77. }
  78.  
  79. function __destruct() {
  80. # nothing to do
  81. }
  82.  
  83. // Getter and Setter Methods
  84. //__________________________________________________________________________
  85. /**
  86. * @param string $sPathOnServer
  87. * @return void
  88. */
  89. public function setPathOnServer($sPathOnServer){
  90. $this->sPathOnServer = $sPathOnServer;
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function getPathOnServer(){
  96. return $this->sPathOnServer;
  97. }
  98. /**
  99. * @param string $sKeyPrefix
  100. * @return void
  101. */
  102. public function setKeyPrefix($sKeyPrefix){
  103. $this->sKeyPrefix = $sKeyPrefix;
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getKeyPrefix(){
  109. return $this->sKeyPrefix;
  110. }
  111.  
  112. /**
  113. * @param int $iExpireTime
  114. * @return void
  115. */
  116. public function setExpireTime($iExpireTime){
  117. $this->iExpireTime = $iExpireTime;
  118. }
  119. /**
  120. * @return int
  121. */
  122. public function getExpireTime(){
  123. return $this->iExpireTime;
  124. }
  125.  
  126. /**
  127. * @param string $sDomain
  128. * @return void
  129. */
  130. public function setDomain($sDomain){
  131. $this->sDomain = $sDomain;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function getDomain(){
  137. return $this->sDomain;
  138. }
  139. // Public Methods
  140. //__________________________________________________________________________
  141. /**
  142. * Set and create cookie
  143. * It set cookie with all parameters managed by this object.
  144. *
  145. * @param string $sKey
  146. * @param mixed $values
  147. * @param int $expireTime
  148. * @param boolean $fIngorePrefix if true ignore eventually key prefix
  149. * @return boolean
  150. */
  151. // bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] )
  152. public function set($sKey, $values, $expireTime=null, $fIngorePrefix=false){
  153. try{
  154. if( setcookie ($this->prepareKey($sKey, $fIngorePrefix), $values, $this->prepareExpireTime($expireTime), $this->getPathOnServer(), $this->getDomain()) ){
  155. return true;
  156. }else{
  157. throw new ExceptionInSylar("Error! Cookie not set", 10013 );
  158. return false;
  159. }
  160. }catch (ExceptionInSylar $ex){
  161. throw $ex;
  162. return false;
  163. }
  164. }
  165. /**
  166. * Get cookie value
  167. * It returns the value of specified cookie
  168. *
  169. * @param string $sKey
  170. * @param boolean $fIngorePrefix if true ignore eventually key prefix
  171. * @return mixed
  172. */
  173. public function get($sKey, $fIngorePrefix=false){
  174. if(array_key_exists($this->prepareKey($sKey, $fIngorePrefix), $_COOKIE) ){
  175. return $_COOKIE[$this->prepareKey($sKey, $fIngorePrefix)];
  176. }else{
  177. return null;
  178. }
  179. }
  180. /**
  181. * Check if cookie exists
  182. * return true if the cookie exists, false otherwise
  183. *
  184. * @param string $sKey
  185. * @param boolean $fIngorePrefix if true ignore eventually key prefix
  186. * @return boolean
  187. */
  188. public function exists($sKey, $fIngorePrefix=false){
  189. if(array_key_exists($this->prepareKey($sKey, $fIngorePrefix), $_COOKIE) ){
  190. return true;
  191. }else{
  192. return false;
  193. }
  194. }
  195. /**
  196. * Delete specified cookie
  197. *
  198. * @return void
  199. * @param string $sKey
  200. * @param boolean $fIngorePrefix if true ignore eventually key prefix
  201. */
  202. public function delete($sKey, $fIngorePrefix=false){
  203. $this->set($sKey, "", -1, $fIngorePrefix);
  204. }
  205. // Private Methods
  206. //__________________________________________________________________________
  207.  
  208.  
  209. /**
  210. * Control and prepare the key of cookie
  211. * it also manage key name prefix
  212. *
  213. * @param string $sKey
  214. * @param boolean $fIngorePrefix if true ignore eventually key prefix
  215. * @return string
  216. */
  217. private function prepareKey($sKey, $fIngorePrefix=false){
  218. if(!$fIngorePrefix && !is_null($this->getKeyPrefix()) ){
  219. return $this->getKeyPrefix().$sKey;
  220. }else{
  221. return $sKey;
  222. }
  223. }
  224. /**
  225. * Return expire time
  226. * it also manage default expiration time
  227. *
  228. * @param int $expireTime
  229. * @return int
  230. */
  231. private function prepareExpireTime($expireTime){
  232. if(!is_null($expireTime)){
  233. return $expireTime;
  234. }else{
  235. if(!is_null($this->getExpireTime())){
  236. return $this->getExpireTime();
  237. }else{
  238. // Return 0 as default that corresponds to magic cookie
  239. return 0;
  240. }
  241. }
  242. }
  243. }
  244. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:13 +0200 by phpDocumentor 1.3.0RC3