PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/Cookie.php

http://marocmall.googlecode.com/
PHP | 354 lines | 204 code | 36 blank | 114 comment | 30 complexity | d7c05793197d8b830aa3e6a717ae0ff6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2011 PrestaShop SA
  23. * @version Release: $Revision: 6851 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class CookieCore
  28. {
  29. /** @var array Contain cookie content in a key => value format */
  30. protected $_content;
  31. /** @var array Crypted cookie name for setcookie() */
  32. protected $_name;
  33. /** @var array expiration date for setcookie() */
  34. protected $_expire;
  35. /** @var array Website domain for setcookie() */
  36. protected $_domain;
  37. /** @var array Path for setcookie() */
  38. protected $_path;
  39. /** @var array cipher tool instance */
  40. protected $_cipherTool;
  41. /** @var array cipher tool initialization key */
  42. protected $_key;
  43. /** @var array cipher tool initilization vector */
  44. protected $_iv;
  45. protected $_modified = false;
  46. /**
  47. * Get data if the cookie exists and else initialize an new one
  48. *
  49. * @param $name Cookie name before encrypting
  50. * @param $path
  51. */
  52. public function __construct($name, $path = '', $expire = NULL)
  53. {
  54. $this->_content = array();
  55. $this->_expire = isset($expire) ? (int)($expire) : (time() + 1728000);
  56. $this->_name = md5($name);
  57. $this->_path = trim(__PS_BASE_URI__.$path, '/\\').'/';
  58. if ($this->_path{0} != '/') $this->_path = '/'.$this->_path;
  59. $this->_path = rawurlencode($this->_path);
  60. $this->_path = str_replace('%2F', '/', $this->_path);
  61. $this->_path = str_replace('%7E', '~', $this->_path);
  62. $this->_key = _COOKIE_KEY_;
  63. $this->_iv = _COOKIE_IV_;
  64. $this->_domain = $this->getDomain();
  65. if (Configuration::get('PS_CIPHER_ALGORITHM'))
  66. $this->_cipherTool = new Rijndael(_RIJNDAEL_KEY_, _RIJNDAEL_IV_);
  67. else
  68. $this->_cipherTool = new Blowfish($this->_key, $this->_iv);
  69. $this->update();
  70. }
  71. protected function getDomain()
  72. {
  73. $r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
  74. preg_match ($r, Tools::getHttpHost(false, false), $out);
  75. if (preg_match('/^(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]|[1-9]).)'.
  76. '{1}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]).)'.
  77. '{2}((25[0-5]|2[0-4][0-9]|[1]{1}[0-9]{2}|[1-9]{1}[0-9]|[0-9]){1}))$/', $out[4]))
  78. return false;
  79. if (!strstr(Tools::getHttpHost(false, false), '.'))
  80. return false;
  81. $domain = $out[4];
  82. $subDomains = SubDomain::getSubDomains();
  83. if ($subDomains === false)
  84. die(Tools::displayError('Bad SubDomain SQL query.'));
  85. foreach ($subDomains AS $subDomain)
  86. {
  87. $subDomainLength = strlen($subDomain) + 1;
  88. if (strncmp($subDomain.'.', $domain, $subDomainLength) == 0)
  89. $domain = substr($domain, $subDomainLength);
  90. }
  91. return $domain;
  92. }
  93. /**
  94. * Set expiration date
  95. *
  96. * @param integer $expire Expiration time from now
  97. */
  98. function setExpire($expire)
  99. {
  100. $this->_expire = (int)($expire);
  101. }
  102. /**
  103. * Magic method wich return cookie data from _content array
  104. *
  105. * @param $key key wanted
  106. * @return string value corresponding to the key
  107. */
  108. public function __get($key)
  109. {
  110. return isset($this->_content[$key]) ? $this->_content[$key] : false;
  111. }
  112. /**
  113. * Magic method which check if key exists in the cookie
  114. *
  115. * @param $key key wanted
  116. * @return boolean key existence
  117. */
  118. public function __isset($key)
  119. {
  120. return isset($this->_content[$key]);
  121. }
  122. /**
  123. * Magic method wich add data into _content array
  124. *
  125. * @param $key key desired
  126. * @param $value value corresponding to the key
  127. */
  128. public function __set($key, $value)
  129. {
  130. if (is_array($value))
  131. die(Tools::displayError());
  132. if (preg_match('/?|\|/', $key.$value))
  133. throw new Exception('Forbidden chars in cookie');
  134. if (!$this->_modified AND (!isset($this->_content[$key]) OR (isset($this->_content[$key]) AND $this->_content[$key] != $value)))
  135. $this->_modified = true;
  136. $this->_content[$key] = $value;
  137. $this->write();
  138. }
  139. /**
  140. * Magic method wich delete data into _content array
  141. *
  142. * @param $key key wanted
  143. */
  144. public function __unset($key)
  145. {
  146. if (isset($this->_content[$key]))
  147. $this->_modified = true;
  148. unset($this->_content[$key]);
  149. $this->write();
  150. }
  151. /**
  152. * Check customer informations saved into cookie and return customer validity
  153. *
  154. * @return boolean customer validity
  155. */
  156. public function isLogged($withGuest = false)
  157. {
  158. if (!$withGuest AND $this->is_guest == 1)
  159. return false;
  160. /* Customer is valid only if it can be load and if cookie password is the same as database one */
  161. if ($this->logged == 1 AND $this->id_customer AND Validate::isUnsignedId($this->id_customer) AND Customer::checkPassword((int)($this->id_customer), $this->passwd))
  162. return true;
  163. return false;
  164. }
  165. /**
  166. * Check employee informations saved into cookie and return employee validity
  167. *
  168. * @return boolean employee validity
  169. */
  170. public function isLoggedBack()
  171. {
  172. /* Employee is valid only if it can be load and if cookie password is the same as database one */
  173. return ($this->id_employee
  174. AND Validate::isUnsignedId($this->id_employee)
  175. AND Employee::checkPassword((int)$this->id_employee, $this->passwd)
  176. AND (!isset($this->_content['remote_addr']) OR $this->_content['remote_addr'] == ip2long(Tools::getRemoteAddr()) OR !Configuration::get('PS_COOKIE_CHECKIP'))
  177. );
  178. }
  179. /**
  180. * Delete cookie
  181. */
  182. public function logout()
  183. {
  184. $this->_content = array();
  185. $this->_setcookie();
  186. unset($_COOKIE[$this->_name]);
  187. $this->_modified = true;
  188. $this->write();
  189. }
  190. /**
  191. * Soft logout, delete everything links to the customer
  192. * but leave there affiliate's informations
  193. */
  194. public function mylogout()
  195. {
  196. unset($this->_content['id_customer']);
  197. unset($this->_content['id_guest']);
  198. unset($this->_content['is_guest']);
  199. unset($this->_content['id_connections']);
  200. unset($this->_content['customer_lastname']);
  201. unset($this->_content['customer_firstname']);
  202. unset($this->_content['passwd']);
  203. unset($this->_content['logged']);
  204. unset($this->_content['email']);
  205. unset($this->_content['id_cart']);
  206. unset($this->_content['id_address_invoice']);
  207. unset($this->_content['id_address_delivery']);
  208. $this->_modified = true;
  209. $this->write();
  210. }
  211. function makeNewLog()
  212. {
  213. unset($this->_content['id_customer']);
  214. unset($this->_content['id_guest']);
  215. Guest::setNewGuest($this);
  216. $this->_modified = true;
  217. }
  218. /**
  219. * Get cookie content
  220. */
  221. function update($nullValues = false)
  222. {
  223. if (isset($_COOKIE[$this->_name]))
  224. {
  225. /* Decrypt cookie content */
  226. $content = $this->_cipherTool->decrypt($_COOKIE[$this->_name]);
  227. /* Get cookie checksum */
  228. $checksum = crc32($this->_iv.substr($content, 0, strrpos($content, '?') + 2));
  229. /* Unserialize cookie content */
  230. $tmpTab = explode('?', $content);
  231. foreach ($tmpTab AS $keyAndValue)
  232. {
  233. $tmpTab2 = explode('|', $keyAndValue);
  234. if (sizeof($tmpTab2) == 2)
  235. $this->_content[$tmpTab2[0]] = $tmpTab2[1];
  236. }
  237. /* Blowfish fix */
  238. if (isset($this->_content['checksum']))
  239. $this->_content['checksum'] = (int)($this->_content['checksum']);
  240. /* Check if cookie has not been modified */
  241. if (!isset($this->_content['checksum']) OR $this->_content['checksum'] != $checksum)
  242. $this->logout();
  243. if (!isset($this->_content['date_add']))
  244. $this->_content['date_add'] = date('Y-m-d H:i:s');
  245. }
  246. else
  247. $this->_content['date_add'] = date('Y-m-d H:i:s');
  248. //checks if the language exists, if not choose the default language
  249. if (!Language::getLanguage((int)$this->id_lang))
  250. $this->id_lang = Configuration::get('PS_LANG_DEFAULT');
  251. }
  252. /**
  253. * Setcookie according to php version
  254. */
  255. protected function _setcookie($cookie = NULL)
  256. {
  257. if ($cookie)
  258. {
  259. $content = $this->_cipherTool->encrypt($cookie);
  260. $time = $this->_expire;
  261. }
  262. else
  263. {
  264. $content = 0;
  265. $time = time() - 1;
  266. }
  267. if (PHP_VERSION_ID <= 50200) /* PHP version > 5.2.0 */
  268. return setcookie($this->_name, $content, $time, $this->_path, $this->_domain, 0);
  269. else
  270. return setcookie($this->_name, $content, $time, $this->_path, $this->_domain, 0, true);
  271. }
  272. /**
  273. * Save cookie with setcookie()
  274. */
  275. public function write()
  276. {
  277. $cookie = '';
  278. /* Serialize cookie content */
  279. if (isset($this->_content['checksum'])) unset($this->_content['checksum']);
  280. foreach ($this->_content AS $key => $value)
  281. $cookie .= $key.'|'.$value.'?';
  282. /* Add checksum to cookie */
  283. $cookie .= 'checksum|'.crc32($this->_iv.$cookie);
  284. /* Cookies are encrypted for evident security reasons */
  285. return $this->_setcookie($cookie);
  286. }
  287. /**
  288. * Get a family of variables (e.g. "filter_")
  289. */
  290. public function getFamily($origin)
  291. {
  292. $result = array();
  293. if (count($this->_content) == 0)
  294. return $result;
  295. foreach ($this->_content AS $key => $value)
  296. if (strncmp($key, $origin, strlen($origin)) == 0)
  297. $result[$key] = $value;
  298. return $result;
  299. }
  300. /**
  301. *
  302. */
  303. public function unsetFamily($origin)
  304. {
  305. $family = $this->getFamily($origin);
  306. foreach ($family AS $member => $value)
  307. unset($this->$member);
  308. }
  309. /**
  310. *
  311. * @return String name of cookie
  312. */
  313. public function getName()
  314. {
  315. return $this->_name;
  316. }
  317. }