PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ser-0.9.6/serweb-0.9.4/phplib/auth.inc

#
PHP | 306 lines | 203 code | 34 blank | 69 comment | 33 complexity | 850252d8749d59d671a3dace146800a6 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /*
  3. * Session Management for PHP3
  4. *
  5. * Copyright (c) 1998-2000 NetUSE AG
  6. * Boris Erdmann, Kristian Koehntopp
  7. * Copyright (c) 1999-2000 Internet Images srl
  8. * Massimiliano Masserelli
  9. *
  10. * $Id: auth.inc,v 1.2 2004/08/09 12:59:35 kozlik Exp $
  11. *
  12. */
  13. class Auth {
  14. var $classname = "Auth";
  15. var $persistent_slots = array("auth");
  16. var $lifetime = 15; ## Max allowed idle time before
  17. ## reauthentication is necessary.
  18. ## If set to 0, auth never expires.
  19. var $refresh = 0; ## Refresh interval in minutes.
  20. ## When expires auth data is refreshed
  21. ## from db using auth_refreshlogin()
  22. ## method. Set to 0 to disable refresh
  23. var $mode = "log"; ## "log" for login only systems,
  24. ## "reg" for user self registration
  25. var $magic = ""; ## Used in uniqid() generation
  26. var $nobody = false; ## If true, a default auth is created...
  27. var $cancel_login = "cancel_login"; ## The name of a button that can be
  28. ## used to cancel a login form
  29. ## End of user qualifiable settings.
  30. var $auth = array(); ## Data array
  31. var $in = false;
  32. var $db;
  33. ##
  34. ## Initialization
  35. ##
  36. function start() {
  37. $cl = $this->cancel_login;
  38. global $sess, $$cl;
  39. ## This is for performance, I guess but I'm not sure if it could
  40. ## be safely removed -- negro
  41. if (! $this->in) {
  42. $sess->register("auth");
  43. $this->in = true;
  44. }
  45. ## back compatibility: if d_c is set, create db object
  46. if(isset($this->database_class)) {
  47. $class = $this->database_class;
  48. $this->db = new $class;
  49. }
  50. # Check current auth state. Should be one of
  51. # 1) Not logged in (no valid auth info or auth expired)
  52. # 2) Logged in (valid auth info)
  53. # 3) Login in progress (if $$cl, revert to state 1)
  54. if ($this->is_authenticated()) {
  55. $uid = $this->auth["uid"];
  56. switch ($uid) {
  57. case "form":
  58. # Login in progress
  59. if ($$cl) {
  60. # If $$cl is set, delete all auth info
  61. # and set state to "Not logged in", so eventually
  62. # default or automatic authentication may take place
  63. $this->unauth();
  64. $state = 1;
  65. } else {
  66. # Set state to "Login in progress"
  67. $state = 3;
  68. }
  69. break;
  70. default:
  71. # User is authenticated and auth not expired
  72. $state = 2;
  73. break;
  74. }
  75. } else {
  76. # User is not (yet) authenticated
  77. $this->unauth();
  78. $state = 1;
  79. }
  80. switch ($state) {
  81. case 1:
  82. # No valid auth info or auth is expired
  83. # Check for user supplied automatic login procedure
  84. if ( $uid = $this->auth_preauth() ) {
  85. $this->auth["uid"] = $uid;
  86. $this->auth["exp"] = time() + (60 * $this->lifetime);
  87. $this->auth["refresh"] = time() + (60 * $this->refresh);
  88. return true;
  89. }
  90. # Check for "log" vs. "reg" mode
  91. switch ($this->mode) {
  92. case "yes":
  93. case "log":
  94. if ($this->nobody) {
  95. # Authenticate as nobody
  96. $this->auth["uid"] = "nobody";
  97. # $this->auth["uname"] = "nobody";
  98. $this->auth["exp"] = 0x7fffffff;
  99. $this->auth["refresh"] = 0x7fffffff;
  100. return true;
  101. } else {
  102. # Show the login form
  103. $this->auth_loginform();
  104. $this->auth["uid"] = "form";
  105. $this->auth["exp"] = 0x7fffffff;
  106. $this->auth["refresh"] = 0x7fffffff;
  107. $sess->freeze();
  108. exit;
  109. }
  110. break;
  111. case "reg":
  112. if ($this->nobody) {
  113. # Authenticate as nobody
  114. $this->auth["uid"] = "nobody";
  115. # $this->auth["uname"] = "nobody";
  116. $this->auth["exp"] = 0x7fffffff;
  117. $this->auth["refresh"] = 0x7fffffff;
  118. return true;
  119. } else {
  120. # Show the registration form
  121. $this->auth_registerform();
  122. $this->auth["uid"] = "form";
  123. $this->auth["exp"] = 0x7fffffff;
  124. $this->auth["refresh"] = 0x7fffffff;
  125. $sess->freeze();
  126. exit;
  127. }
  128. break;
  129. default:
  130. # This should never happen. Complain.
  131. echo "Error in auth handling: no valid mode specified.\n";
  132. $sess->freeze();
  133. exit;
  134. }
  135. break;
  136. case 2:
  137. # Valid auth info
  138. # Refresh expire info
  139. ## DEFAUTH handling: do not update exp for nobody.
  140. if ($uid != "nobody")
  141. $this->auth["exp"] = time() + (60 * $this->lifetime);
  142. break;
  143. case 3:
  144. # Login in progress, check results and act accordingly
  145. switch ($this->mode) {
  146. case "yes":
  147. case "log":
  148. if ( $uid = $this->auth_validatelogin() ) {
  149. $this->auth["uid"] = $uid;
  150. $this->auth["exp"] = time() + (60 * $this->lifetime);
  151. $this->auth["refresh"] = time() + (60 * $this->refresh);
  152. return true;
  153. } else {
  154. $this->auth_loginform();
  155. $this->auth["uid"] = "form";
  156. $this->auth["exp"] = 0x7fffffff;
  157. $this->auth["refresh"] = 0x7fffffff;
  158. $sess->freeze();
  159. exit;
  160. }
  161. break;
  162. case "reg":
  163. if ($uid = $this->auth_doregister()) {
  164. $this->auth["uid"] = $uid;
  165. $this->auth["exp"] = time() + (60 * $this->lifetime);
  166. $this->auth["refresh"] = time() + (60 * $this->refresh);
  167. return true;
  168. } else {
  169. $this->auth_registerform();
  170. $this->auth["uid"] = "form";
  171. $this->auth["exp"] = 0x7fffffff;
  172. $this->auth["refresh"] = 0x7fffffff;
  173. $sess->freeze();
  174. exit;
  175. }
  176. break;
  177. default:
  178. # This should never happen. Complain.
  179. echo "Error in auth handling: no valid mode specified.\n";
  180. $sess->freeze();
  181. exit;
  182. break;
  183. }
  184. break;
  185. default:
  186. # This should never happen. Complain.
  187. echo "Error in auth handling: invalid state reached.\n";
  188. $sess->freeze();
  189. exit;
  190. break;
  191. }
  192. }
  193. function login_if( $t ) {
  194. if ( $t ) {
  195. $this->unauth(); # We have to relogin, so clear current auth info
  196. $this->nobody = false; # We are forcing login, so default auth is
  197. # disabled
  198. $this->start(); # Call authentication code
  199. }
  200. }
  201. function unauth($nobody = false) {
  202. $this->auth["uid"] = "";
  203. $this->auth["perm"] = "";
  204. $this->auth["exp"] = 0;
  205. ## Back compatibility: passing $nobody to this method is
  206. ## deprecated
  207. if ($nobody) {
  208. $this->auth["uid"] = "nobody";
  209. $this->auth["perm"] = "";
  210. $this->auth["exp"] = 0x7fffffff;
  211. }
  212. }
  213. function logout($nobody = "") {
  214. global $sess;
  215. $sess->unregister("auth");
  216. unset($this->auth["uname"]);
  217. $this->unauth($nobody == "" ? $this->nobody : $nobody);
  218. }
  219. function is_authenticated() {
  220. if (
  221. isset($this->auth["uid"])
  222. &&
  223. $this->auth["uid"]
  224. &&
  225. (($this->lifetime <= 0) || (time() < $this->auth["exp"]))
  226. ) {
  227. # If more than $this->refresh minutes are passed since last check,
  228. # perform auth data refreshing. Refresh is only done when current
  229. # session is valid (registered, not expired).
  230. if (
  231. ($this->refresh > 0)
  232. &&
  233. ($this->auth["refresh"])
  234. &&
  235. ($this->auth["refresh"] < time())
  236. ) {
  237. if ( $this->auth_refreshlogin() ) {
  238. $this->auth["refresh"] = time() + (60 * $this->refresh);
  239. } else {
  240. return false;
  241. }
  242. }
  243. return $this->auth["uid"];
  244. } else {
  245. return false;
  246. }
  247. }
  248. ########################################################################
  249. ##
  250. ## Helper functions
  251. ##
  252. function url() {
  253. return $GLOBALS["sess"]->self_url();
  254. }
  255. function purl() {
  256. print $GLOBALS["sess"]->self_url();
  257. }
  258. ## This method can authenticate a user before the loginform
  259. ## is being displayed. If it does, it must set a valid uid
  260. ## (i.e. nobody IS NOT a valid uid) just like auth_validatelogin,
  261. ## else it shall return false.
  262. function auth_preauth() { return false; }
  263. ##
  264. ## Authentication dummies. Must be overridden by user.
  265. ##
  266. function auth_loginform() { ; }
  267. function auth_validatelogin() { ; }
  268. function auth_refreshlogin() { ; }
  269. function auth_registerform() { ; }
  270. function auth_doregister() { ; }
  271. }
  272. ?>