PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/cookie.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 147 lines | 73 code | 17 blank | 57 comment | 4 complexity | 444234c5c2d8fd0d5eaf1a998f91ca4f MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2009 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 DolCookie
  24. * \brief Class to manage cookies
  25. */
  26. class DolCookie
  27. {
  28. var $myKey;
  29. var $myCookie;
  30. var $myValue;
  31. var $myExpire;
  32. var $myPath;
  33. var $myDomain;
  34. var $mySsecure;
  35. var $cookiearray;
  36. var $cookie;
  37. /**
  38. * Constructor
  39. *
  40. * @param string $key Personnal key
  41. */
  42. function __construct($key = '')
  43. {
  44. $this->myKey = $key;
  45. $this->cookiearray = array();
  46. $this->cookie = "";
  47. $this->myCookie = "";
  48. $this->myValue = "";
  49. }
  50. /**
  51. * Encrypt en create the cookie
  52. *
  53. * @return void
  54. */
  55. function cryptCookie()
  56. {
  57. if (!empty($this->myKey))
  58. {
  59. $valuecrypt = base64_encode($this->myValue);
  60. $max=dol_strlen($valuecrypt)-1;
  61. for ($f=0 ; $f <= $max; $f++)
  62. {
  63. $this->cookie .= intval(ord($valuecrypt[$f]))*$this->myKey."|";
  64. }
  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 void
  76. */
  77. function decryptCookie()
  78. {
  79. if (!empty($this->myKey))
  80. {
  81. $this->cookiearray = explode("|",$_COOKIE[$this->myCookie]);
  82. $this->myValue = "" ;
  83. $num = (count($this->cookiearray) - 2);
  84. for ($f = 0; $f <= $num; $f++)
  85. {
  86. $this->myValue .= strval(chr($this->cookiearray[$f]/$this->myKey));
  87. }
  88. return(base64_decode($this->myValue));
  89. }
  90. else
  91. {
  92. return($_COOKIE[$this->myCookie]);
  93. }
  94. }
  95. /**
  96. * Set and create the cookie
  97. *
  98. * @param string $cookie Cookie name
  99. * @param string $value Cookie value
  100. * @param string $expire Expiration
  101. * @param string $path Path of cookie
  102. * @param string $domain Domain name
  103. * @param int $secure 0 or 1
  104. * @return void
  105. */
  106. function _setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0)
  107. {
  108. $this->myCookie = $cookie;
  109. $this->myValue = $value;
  110. $this->myExpire = $expire;
  111. $this->myPath = $path;
  112. $this->myDomain = $domain;
  113. $this->mySecure = $secure;
  114. //print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
  115. $this->cryptCookie();
  116. }
  117. /**
  118. * Get the cookie
  119. *
  120. * @param string $cookie Cookie name
  121. * @return string Decrypted value
  122. */
  123. function _getCookie($cookie)
  124. {
  125. $this->myCookie = $cookie;
  126. $decryptValue = $this->decryptCookie();
  127. return $decryptValue;
  128. }
  129. }
  130. ?>