cookie.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /* Copyright (C) 2009-2015 Regis Houssin <regis.houssin@capnetworks.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/cookie.class.php
  19. * \ingroup core
  20. * \brief File of class to manage cookies
  21. */
  22. /**
  23. * Class to manage cookies.
  24. * This class is used by external module multicompany but will be removed soon only and must not be used by
  25. *
  26. * @deprecated PHP already provide function to read/store a cookie. No need to use a dedicated class. Also storing sensitive information into cookie is forbidden, so encryption is useless.
  27. * If a data is sensitive, it must be stored into database (if we need a long term retention) or into session.
  28. */
  29. class DolCookie
  30. {
  31. private $_myKey;
  32. private $_iv;
  33. var $myCookie;
  34. var $myValue;
  35. var $myExpire;
  36. var $myPath;
  37. var $myDomain;
  38. var $mySecure;
  39. var $cookie;
  40. /**
  41. * Constructor
  42. *
  43. * @param string $key Personnal key
  44. * @deprecated
  45. */
  46. function __construct($key = '')
  47. {
  48. $this->_myKey = hash('sha256', $key, TRUE);
  49. $this->_iv = md5(md5($this->_myKey));
  50. $this->cookie = "";
  51. $this->myCookie = "";
  52. $this->myValue = "";
  53. }
  54. /**
  55. * Encrypt en create the cookie
  56. *
  57. * @return void
  58. */
  59. private function _cryptCookie()
  60. {
  61. if (!empty($this->_myKey) && !empty($this->_iv))
  62. {
  63. $valuecrypt = base64_encode($this->myValue);
  64. $this->cookie = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->_myKey, $valuecrypt, MCRYPT_MODE_CBC, $this->_iv));
  65. }
  66. else
  67. {
  68. $this->cookie = $this->myValue;
  69. }
  70. setcookie($this->myCookie, $this->cookie, $this->myExpire, $this->myPath, $this->myDomain, $this->mySecure);
  71. }
  72. /**
  73. * Decrypt the cookie
  74. *
  75. * @return string
  76. */
  77. private function _decryptCookie()
  78. {
  79. if (!empty($this->_myKey) && !empty($this->_iv))
  80. {
  81. $this->cookie = $_COOKIE[$this->myCookie];
  82. $this->myValue = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->_myKey, base64_decode($this->cookie), MCRYPT_MODE_CBC, $this->_iv));
  83. return(base64_decode($this->myValue));
  84. }
  85. else
  86. {
  87. return($_COOKIE[$this->myCookie]);
  88. }
  89. }
  90. /**
  91. * Set and create the cookie
  92. *
  93. * @param string $cookie Cookie name
  94. * @param string $value Cookie value
  95. * @param integer $expire Expiration
  96. * @param string $path Path of cookie
  97. * @param string $domain Domain name
  98. * @param int $secure 0 or 1
  99. * @return void
  100. */
  101. public function setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0)
  102. {
  103. $this->myCookie = $cookie;
  104. $this->myValue = $value;
  105. $this->myExpire = $expire;
  106. $this->myPath = $path;
  107. $this->myDomain = $domain;
  108. $this->mySecure = $secure;
  109. //print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
  110. $this->_cryptCookie();
  111. }
  112. /**
  113. * Get the cookie
  114. *
  115. * @param string $cookie Cookie name
  116. * @return string Decrypted value
  117. */
  118. public function getCookie($cookie)
  119. {
  120. $this->myCookie = $cookie;
  121. $decryptValue = $this->_decryptCookie();
  122. return $decryptValue;
  123. }
  124. }