[PHPLIB-DEV] cvs commit From: kk (phplib-dev <email protected>)
Date: 10/29/99

From: kk
Date: Fri Oct 29 13:19:11 1999
Modified files:
      php-lib/CHANGES
      php-lib/php/auth.inc
      php-lib/php/local.inc
      php-lib/php/loginform.ihtml
      php-lib/php/registerform.ihtml

Log message:
You asked for reg mode, you got reg mode. With transparent switching.

Index: php-lib/CHANGES
diff -u php-lib/CHANGES:1.125 php-lib/CHANGES:1.126
--- php-lib/CHANGES:1.125 Fri Oct 29 12:18:25 1999
+++ php-lib/CHANGES Fri Oct 29 13:18:39 1999
@@ -1,4 +1,9 @@
-$Id: CHANGES,v 1.125 1999/10/29 10:18:25 athompso Exp $
+$Id: CHANGES,v 1.126 1999/10/29 11:18:39 kk Exp $
+
+29-Oct-1999 kk
+ - Ok, Massimiliano, try this one. A complete Example_Auth class
+ featuring reg and log mode and transparent switching between
+ both.
 
 29-Oct-1999 at
   - modified pages/admin/view_sessions.php3 to be much more useful
Index: php-lib/php/auth.inc
diff -u php-lib/php/auth.inc:1.22 php-lib/php/auth.inc:1.23
--- php-lib/php/auth.inc:1.22 Wed Oct 27 15:17:33 1999
+++ php-lib/php/auth.inc Fri Oct 29 13:18:40 1999
@@ -7,13 +7,13 @@
  * Copyright (c) 1999 Internet Images srl
  * Massimiliano Masserelli
  *
- * $Id: auth.inc,v 1.22 1999/10/27 13:17:33 kk Exp $
+ * $Id: auth.inc,v 1.23 1999/10/29 11:18:40 kk Exp $
  *
  */
 
 class Auth {
   var $classname = "Auth";
- var $persistent_slots = array("auth");
+ var $persistent_slots = array("auth", "mode");
   
   var $lifetime = 15; ## Max allowed idle time before
                                   ## reauthentication is necessary.
@@ -158,7 +158,10 @@
               $this->auth["refresh"] = time() + (60 * $this->refresh);
               return true;
             } else {
- $this->auth_loginform();
+ if ($this->mode == "reg")
+ $this->auth_registerform();
+ else
+ $this->auth_loginform();
               $this->auth["uid"] = "form";
               $this->auth["exp"] = 0x7fffffff;
               $this->auth["refresh"] = 0x7fffffff;
@@ -173,7 +176,10 @@
               $this->auth["refresh"] = time() + (60 * $this->refresh);
               return true;
             } else {
- $this->auth_registerform();
+ if ($this->mode == "reg")
+ $this->auth_registerform();
+ else
+ $this->auth_loginform();
               $this->auth["uid"] = "form";
               $this->auth["exp"] = 0x7fffffff;
               $this->auth["refresh"] = 0x7fffffff;
Index: php-lib/php/local.inc
diff -u php-lib/php/local.inc:1.28 php-lib/php/local.inc:1.29
--- php-lib/php/local.inc:1.28 Fri Oct 29 12:32:16 1999
+++ php-lib/php/local.inc Fri Oct 29 13:18:40 1999
@@ -5,7 +5,7 @@
  * Copyright (c) 1998,1999 NetUSE GmbH
  * Boris Erdmann, Kristian Koehntopp
  *
- * $Id: local.inc,v 1.28 1999/10/29 10:32:16 kk Exp $
+ * $Id: local.inc,v 1.29 1999/10/29 11:18:40 kk Exp $
  *
  * All functions in this file are example classes, which can be used
  * by your application to get you going. Once you get the hang of it,
@@ -94,16 +94,30 @@
 
   var $database_class = "DB_Example";
   var $database_table = "auth_user";
-
+
+ ## show login form
+ ## this is a CI-less generic login form. Feel free to
+ ## customize.
   function auth_loginform() {
- global $sess;
- global $_PHPLIB;
+ global $sess, $auth, $_PHPLIB, $PHP_SELF;
 
     include($_PHPLIB["libdir"] . "loginform.ihtml");
   }
   
+ ## validate login information.
+ ## please remember to adapt the global statements here to match the
+ ## variables used in your loginform.ihtml.
+ ## this function has to return false, if the login fails, or
+ ## a valid user_id.
   function auth_validatelogin() {
- global $username, $password;
+ global $username, $password, $mode;
+
+ if (isset($mode) && $mode == "reg") {
+ $this->mode = "reg";
+ $this->auth["uname"] = $username;
+ $this->auth["error"] = "Please fill in the required registration information. Thank you.";
+ return false;
+ }
 
     if(isset($username)) {
       $this->auth["uname"]=$username; ## This provides access for "loginform.ihtml"
@@ -124,27 +138,46 @@
       $uid = $this->db->f("user_id");
       $this->auth["perm"] = $this->db->f("perms");
     }
+ if ($uid == false)
+ $this->auth["error"] = "Either your username or password are invalid.<br>Please try again.";
+
     return $uid;
   }
+
+ function auth_preauth() {
+ global $HTTP_COOKIE_VARS;
+
+ if ($HTTP_COOKIE_VARS["username"]) {
+ $this->auth["uname"] = $HTTP_COOKIE_VARS["username"];
+ $this->mode = "log";
+ }
+ return false;
+ }
   
   ## show registration form.
   ## this is a very basic one, you certainly want to
   ## add columns to that table and this form.
   function auth_registerform() {
- global $sess, $auth;
+ global $sess, $auth, $_PHPLIB, $PHP_SELF;
 
     include("registerform.ihtml");
   }
   
   function auth_doregister() {
     ## Import form variables
- global $username, $pass1, $pass2;
+ global $username, $pass1, $pass2, $mode;
     
     ## Save the username for use in registerform.ihtml,
     ## should registration fail. If you add more fields,
     ## you will want to save them here, too.
     $this->auth["uname"] = $username;
 
+ if (isset($mode) && $mode == "log") {
+ $this->mode = "log";
+ $this->auth["error"] = "Please enter your username and password. Thank you.";
+ return false;
+ }
+
     ## Check the passwords for validity.
     if ($pass1 != $pass2) {
       $this->auth["error"] = "Password and repeated password do not match. Please try again.";
@@ -163,7 +196,7 @@
         $this->auth["perm"] = $this->db->f("perms");
         return $this->db->f("user_id");
       }
-
+
       ## If user is present and password does not match,
       ## complain and fail.
       $this->auth["error"] = "This username is already taken. Please choose a different one.";
@@ -180,7 +213,10 @@
       $username,
       $pass1);
     $this->db->query($query);
-
+
+ ## Set a cookie to remember the username.
+ SetCookie("username", $username, pow(2, 31)-1, "/");
+
     ## log in that new user. The new user has no permissions.
     ## If the user should have permissions, you put them
     ## into $auth->auth["perm"].
@@ -362,42 +398,42 @@
 ## See /pages/menu for an example application of Example_Menu.
 ##
 
-# class Example_Menu extends Menu {
-# # Map of PHP_SELF URL strings to menu positions
-# var $urlmap = array(
-# "/menu/index.php3" => "",
-# "/menu/item1.php3" => "/1",
-# "/menu/item11.php3" => "/1/1",
-# "/menu/item12.php3" => "/1/2",
-# "/menu/item13.php3" => "/1/3",
-# "/menu/item2.php3" => "/2",
-# "/menu/item21.php3" => "/2/1",
-# "/menu/item22.php3" => "/2/2",
-# "/menu/item221.php3" => "/2/2/1",
-# "/menu/item222.php3" => "/2/2/2",
-# "/menu/item23.php3" => "/2/3",
-# "/menu/item24.php3" => "/2/4"
-# );
-#
-# # Information about each menu item
-# var $item = array(
-# "" => array("title" => "Main"),
-# "/1" => array("title" => "Text 1"),
-# "/1/1" => array("title" => "Text 1.1"),
-# "/1/2" => array("title" => "Text 1.2"),
-# "/1/3" => array("title" => "Text 1.3"),
-# "/2" => array("title" => "Text 2"),
-# "/2/1" => array("title" => "Text 2.1"),
-# "/2/2" => array("title" => "Text 2.2"),
-# "/2/2/1"=> array("title" => "Text 2.2.1"),
-# "/2/2/2"=> array("title" => "Text 2.2.2"),
-# "/2/3" => array("title" => "Text 2.3"),
-# "/2/4" => array("title" => "Text 2.4")
-# );
-#
-# function Example_Menu() {
-# $this->setup();
-# }
-# }
+class Example_Menu extends Menu {
+ # Map of PHP_SELF URL strings to menu positions
+ var $urlmap = array(
+ "/menu/index.php3" => "",
+ "/menu/item1.php3" => "/1",
+ "/menu/item11.php3" => "/1/1",
+ "/menu/item12.php3" => "/1/2",
+ "/menu/item13.php3" => "/1/3",
+ "/menu/item2.php3" => "/2",
+ "/menu/item21.php3" => "/2/1",
+ "/menu/item22.php3" => "/2/2",
+ "/menu/item221.php3" => "/2/2/1",
+ "/menu/item222.php3" => "/2/2/2",
+ "/menu/item23.php3" => "/2/3",
+ "/menu/item24.php3" => "/2/4"
+ );
+
+ # Information about each menu item
+ var $item = array(
+ "" => array("title" => "Main"),
+ "/1" => array("title" => "Text 1"),
+ "/1/1" => array("title" => "Text 1.1"),
+ "/1/2" => array("title" => "Text 1.2"),
+ "/1/3" => array("title" => "Text 1.3"),
+ "/2" => array("title" => "Text 2"),
+ "/2/1" => array("title" => "Text 2.1"),
+ "/2/2" => array("title" => "Text 2.2"),
+ "/2/2/1"=> array("title" => "Text 2.2.1"),
+ "/2/2/2"=> array("title" => "Text 2.2.2"),
+ "/2/3" => array("title" => "Text 2.3"),
+ "/2/4" => array("title" => "Text 2.4")
+ );
+
+ function Example_Menu() {
+ $this->setup();
+ }
+}
 
 ?>
Index: php-lib/php/loginform.ihtml
diff -u php-lib/php/loginform.ihtml:1.2 php-lib/php/loginform.ihtml:1.3
--- php-lib/php/loginform.ihtml:1.2 Wed Oct 14 15:00:38 1998
+++ php-lib/php/loginform.ihtml Fri Oct 29 13:18:40 1999
@@ -14,7 +14,8 @@
 
 Welcome!
 
-Please identify yourself with a username and a password:<br>
+Please identify yourself with a username and a password. If
+you are a new user, please use our <a href="<?php print $PHP_SELF ?>?mode=reg">registration form.</a><br>
 
 <form action="<?php print $this->url() ?>" method=post>
 <table border=0 bgcolor="#eeeeee" align="center" cellspacing=0 cellpadding=4>
@@ -35,19 +36,20 @@
  </tr>
 </table>
 
- <?php global $username; if ( isset($username) ): ?>
+ <?php if ($this->auth["error"]): ?>
  <!-- failed login code -->
 
  <p>
  <table>
   <tr>
- <td colspan=2><font color=red><b>Either your username or your password
- are invalid.<br>
- Please try again!</b></font></td>
+ <td colspan=2><font color=red><b><?php print $this->auth["error"] ?></b></font></td>
   </tr>
  </table>
 
- <?php endif ?>
+ <?php
+ $this->auth["error"] = "";
+ endif;
+ ?>
 
 </table>
 </form>
@@ -62,4 +64,4 @@
 // -->
 </script>
 </html>
-<!-- $Id: loginform.ihtml,v 1.2 1998/10/14 13:00:38 kk Exp $ -->
+<!-- $Id: loginform.ihtml,v 1.3 1999/10/29 11:18:40 kk Exp $ -->
Index: php-lib/php/registerform.ihtml
diff -u php-lib/php/registerform.ihtml:1.1 php-lib/php/registerform.ihtml:1.2
--- php-lib/php/registerform.ihtml:1.1 Thu Oct 28 20:22:03 1999
+++ php-lib/php/registerform.ihtml Fri Oct 29 13:18:40 1999
@@ -14,9 +14,11 @@
 
 Welcome!
 
-Please choose a username and a password:<br>
+Please choose a username and a password or use our <a href="<?php
+print $PHP_SELF ?>?mode=log">login form</a>,
+if you believe that we already know you:<br>
 
-<form action="<?php print $this->url() ?>" method=post>
+<form action="<?php $this->purl() ?>" method=post>
 <table border=0 bgcolor="#eeeeee" align="center" cellspacing=0 cellpadding=4>
  <tr valign=top align=left>
   <td>Username:</td>
@@ -47,7 +49,10 @@
   </tr>
  </table>
 
- <?php endif ?>
+ <?php
+ $this->auth["error"] = "";
+ endif;
+ ?>
 
 </table>
 </form>
@@ -62,7 +67,7 @@
 // -->
 </script>
 </html>
-<!-- $Id: registerform.ihtml,v 1.1 1999/10/28 18:22:03 kk Exp $ -->
+<!-- $Id: registerform.ihtml,v 1.2 1999/10/29 11:18:40 kk Exp $ -->
 
  </body>
 </html>

-
PHPLIB Developers Mailing List. Send messages to <phplib-dev <email protected>>.
To unsubscribe, send "unsubscribe" to <phplib-dev-request <email protected>> in
the body, not the subject, of your message.