PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/class/bwcookie.class.php

http://firmkernel.googlecode.com/
PHP | 268 lines | 188 code | 41 blank | 39 comment | 30 complexity | 0e13bb40f57774327e2df7ce289ae198 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------------+
  4. | $Id: bwcookie.class.php 2008-10-16 19:18:10Z Bleakwind $
  5. | System Cookies
  6. | Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
  7. | http://www.weaverdream.com/
  8. | Release under the GNU Lesser General Public License Version 3 (LGPLv3):
  9. | http://www.gnu.org/licenses/lgpl.html
  10. +-----------------------------------------------------------------------------+
  11. // Initialize
  12. $c = new bwcookie("weaverdream_sessid");
  13. $c->set_cookie_params(0,null,null);
  14. $c->set_session_name("weaverdream_sessid");
  15. $c->set_check_cookie(array("wd_id","wd_unm","wd_pwd"), array("sess_id","sess_username","sess_password"));
  16. $c->start();
  17. // Check if login
  18. if (!$c->iflogin()) {
  19. echo "no";
  20. } else {
  21. echo "yes";
  22. }
  23. // Login
  24. $c->set_lifetime('600');
  25. $c->set_cookie(array(
  26. "wd_id" => "1",
  27. "wd_unm" => "username",
  28. "wd_pwd" => "password",
  29. ));
  30. $c->set_session(array(
  31. "sess_id" => "1",
  32. "sess_username" => "username",
  33. "sess_password" => "password",
  34. ));
  35. // Logout
  36. $c->destroy();
  37. // Debug
  38. $c->debug();
  39. */
  40. class bwcookie {
  41. // Cookie Setting
  42. var $cookie_lifetime = 0;
  43. var $cookie_path = null;
  44. var $cookie_domain = null;
  45. var $session_name = null;
  46. var $session_id = null;
  47. var $alreadylogin = false;
  48. var $cookie_key = array();
  49. var $SESSION;
  50. function bwcookie(&$SESSION)
  51. {
  52. $this->SESSION = &$SESSION;
  53. $this->set_cookie_params();
  54. return;
  55. }
  56. function set_cookie_params($lifetime = null, $path = null, $domain = null)
  57. {
  58. if ( (preg_match("/^[0-9]{1,11}$/",$lifetime)) && ((int)$lifetime > 0) ) {
  59. $this->cookie_lifetime = (int)$lifetime;
  60. }
  61. if (!empty($path)) {
  62. $this->cookie_path = $path;
  63. } else {
  64. $this->cookie_path = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')+1);
  65. }
  66. if (!empty($domain)) {
  67. $this->cookie_domain = $domain;
  68. }
  69. session_set_cookie_params($this->cookie_lifetime, $this->cookie_path, $this->cookie_domain);
  70. return;
  71. }
  72. function set_session_name($name = null)
  73. {
  74. if (preg_match("/^[a-z0-9_]+$/i",$name) ) {
  75. $this->session_name = $name;
  76. }
  77. return $this->session_name;
  78. }
  79. function set_session_id($id = null)
  80. {
  81. if (preg_match("/^[a-z0-9_]+$/i",$id) ) {
  82. $this->session_id = $id;
  83. }
  84. return $this->session_id;
  85. }
  86. function set_check_cookie($cookie,$session)
  87. {
  88. $this->cookie_key = array();
  89. if (!is_array($cookie)) {
  90. if (!empty($cookie)) {
  91. $this->cookie_key['cookie'][] = $cookie;
  92. $this->cookie_key['session'][] = $session;
  93. }
  94. } else {
  95. reset($cookie);reset($session);
  96. $count = count($cookie);
  97. for($i=0;$i<$count;$i++) {
  98. $this->cookie_key['cookie'][] = $cookie[$i];
  99. $this->cookie_key['session'][] = $session[$i];
  100. }
  101. }
  102. return;
  103. }
  104. function start()
  105. {
  106. ob_start();
  107. session_cache_limiter("private, must-revalidate");
  108. $this->SESSION->session_name($this->session_name);
  109. $this->SESSION->session_id($this->session_id, $this->cookie_lifetime, $this->cookie_path, $this->cookie_domain);
  110. $this->SESSION->session_start();
  111. if ($this->check_cookie() === true) {
  112. $this->alreadylogin = true;
  113. } else {
  114. $this->session_reset();
  115. }
  116. return;
  117. }
  118. function check_cookie()
  119. {
  120. $count = count($this->cookie_key['cookie']);
  121. if ($count > 0) {
  122. for($i=0; $i<$count ;$i++) {
  123. if ( empty($_COOKIE[$this->cookie_key['cookie'][$i]]) ||
  124. ($_COOKIE[$this->cookie_key['cookie'][$i]] != sha1($this->SESSION->_[$this->cookie_key['session'][$i]])) ) {
  125. return false;
  126. }
  127. }
  128. } else {
  129. return false;
  130. }
  131. return true;
  132. }
  133. function session_reset()
  134. {
  135. $count = count($this->cookie_key['cookie']);
  136. if ($count > 0) {
  137. for($i=0; $i<$count ;$i++) {
  138. setcookie($this->cookie_key['cookie'][$i], '', time() - 1, $this->cookie_path, $this->cookie_domain);
  139. }
  140. }
  141. $count = count($this->cookie_key['session']);
  142. if ($count > 0) {
  143. for($i=0; $i<$count ;$i++) {
  144. $this->SESSION->del($this->cookie_key['session'][$i]);
  145. }
  146. }
  147. return;
  148. }
  149. function set_lifetime($lifetime)
  150. {
  151. $this->cookie_lifetime = $lifetime;
  152. $this->set_cookie_params($this->cookie_lifetime);
  153. return;
  154. }
  155. function set_path($path)
  156. {
  157. $this->cookie_path = $path;
  158. $this->set_cookie_params($this->cookie_lifetime, $path);
  159. return;
  160. }
  161. function set_domain($domain)
  162. {
  163. $this->cookie_domain = $domain;
  164. $this->set_cookie_params($this->cookie_lifetime, $this->cookie_path, $this->cookie_domain);
  165. return;
  166. }
  167. function set_cookie($varname, $value = null)
  168. {
  169. setcookie($this->SESSION->session_name, $this->SESSION->session_id, time() + $this->cookie_lifetime, $this->cookie_path, $this->cookie_domain);
  170. if (!is_array($varname)) {
  171. if (!empty($varname)) {
  172. setcookie($varname, sha1($value), time() + $this->cookie_lifetime, $this->cookie_path, $this->cookie_domain);
  173. }
  174. } else {
  175. reset($varname);
  176. while(list($k, $v) = each($varname)) {
  177. if (!empty($k)) {
  178. setcookie($k, sha1($v), time() + $this->cookie_lifetime, $this->cookie_path, $this->cookie_domain);
  179. }
  180. }
  181. }
  182. return;
  183. }
  184. function set_session($varname, $value = null)
  185. {
  186. $this->SESSION->set($this->SESSION->lifetime_name, $this->cookie_lifetime);
  187. if (!is_array($varname)) {
  188. if (!empty($varname)) {
  189. $this->SESSION->set($varname, $value);
  190. }
  191. } else {
  192. reset($varname);
  193. while(list($k, $v) = each($varname)) {
  194. if (!empty($k)) {
  195. $this->SESSION->set($k, $v);
  196. }
  197. }
  198. }
  199. return;
  200. }
  201. function iflogin()
  202. {
  203. return $this->alreadylogin;
  204. }
  205. function destroy()
  206. {
  207. $this->session_reset();
  208. return;
  209. }
  210. function debug()
  211. {
  212. echo "<fieldset style='width:300;font-family:tahoma;font-size:11px;background-color:#F2F7FB;'><legend>Debug</legend>";
  213. echo "<br>session_name => ".$this->SESSION->session_name;
  214. echo "<br><br>".$this->SESSION->session_name." => ".$this->SESSION->session_id;
  215. echo "<pre style='font-family:tahoma;font-size: 11px;'>session_get_cookie_params => ";
  216. print_r(session_get_cookie_params());
  217. echo "</pre>";
  218. echo "<pre style='font-family:tahoma;font-size: 11px;'>\$_COOKIE => ";
  219. print_r($_COOKIE);
  220. echo "</pre>";
  221. echo "<pre style='font-family:tahoma;font-size: 11px;'>\$SESSION->_ => ";
  222. print_r($this->SESSION->_);
  223. echo "</pre>";
  224. echo "</fieldset>";
  225. }
  226. }
  227. ?>