auth.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:9k
源码类别:

WEB邮件程序

开发平台:

PHP

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