Location: PHPKode > scripts > CAuth > cauth/CAuth.php
<?
/*********************************************************************************
**********************************************************************************

Class CGroup{} //exaktes Abbild des Datenbankobkjekts groups
Class CUser{}  //exaktes Abbild des Datenbankobkjekts user
Class CAuth{}

Klassen zur Authentifizierung mit Sessionverwaltung (siehe CSession.php) an einer
MySQL DB. Die Klasse CAuth beerbt CSession und erzuegt ein Objekt CUser, anhand
von Username und Passwort. Wird ein entsprechender Eintrag gefunden, wird die
komplette Klasse in die Session geschrieben.


Autoren : Sven Zurnieden, Frank Cawein, Marcel Weilacher
Datum :   22.06.2002

!!!!!!!!!!!!!!!V I S I T!!!!!!!!!!!!!!!!!!!!!!
http://berl.dyndns.org/
!!!!!!!!!!!!!!!V I S I T!!!!!!!!!!!!!!!!!!!!!!

**********************************************************************************
*********************************************************************************/
include("CSession.php");

class CGroup{

      var $id = "Not authenticated";
      var $name = "Not authenticated";
      var $description = "Not authenticated";
      var $admin = "Not authenticated";

      var $connect = "Not connected to db";
      var $debug;

      function CGroup($group_id, $debug=0){
               $this->debug = $debug;
               $this->connect = mysql_connect("localhost","username","password")OR die ("<font size=5 color=#FF0000>!!!!!An Error occured while contacting the database!!!!!</font>");
               mysql_select_db("thehell",$this->connect);
               $sql = "SELECT * FROM groups WHERE id = '$group_id'";
               $query = mysql_query($sql,$this->connect);
               $row = mysql_fetch_object($query,MYSQL_ASSOC);
               $this->id = $row->id;
               $this->name = $row->name;
               $this->description = $row->description;
               $this->admin = $row->admin;

               if ($this->debug==1){
                   $this->show_debug();
               }
      }
      function show_debug(){
               echo "<font size=3 color=#222222><br> OBJEKT CGroup<br></font>";
               while(list($key,$val)=each($this)){
                     echo "<font size=3 color=#222222>$key ---> $val</font><br>";
               }
      }
}

class CUser{

      var $id = "Not authenticated";
      var $group_id = "Not authenticated";
      var $username = "Not authenticated";
      var $password = "Not authenticated";
      var $email = "Not authenticated";
      var $group;

      var $authenticated = false;
      var $connect = "Not connected to db";
      var $debug = 0;


      function CUser($username,$password, $debug=0){
               $this->debug = $debug;
               $this->connect = mysql_connect("localhost","root","heiner")OR die ("<font size=5 color=#FF0000>!!!!!An Error occured while contacting the database!!!!!</font>");
               mysql_select_db("thehell",$this->connect);
               $sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";
               $query = mysql_query($sql,$this->connect);
               $row = mysql_fetch_object($query,MYSQL_ASSOC);
               if ($row->id==""){
                   $this->authenticated = false;
               }
               else{
                    $this->authenticated = true;
                    $this->id = $row->id;
                    $this->group_id = $row->group_id;
                    $this->username = $row->username;
                    $this->password = $row->password;
                    $this->email = $row->email;
                    $this->group = new CGroup($this->group_id);
               }

               if ($this->debug==1){
                   $this->show_debug();
               }
      }
      function show_debug(){
               $this->group->show_debug();
               echo "<font size=3 color=#222222><br> OBJEKT CUser<br></font>";
               while(list($key,$val)=each($this)){
                     echo "<font size=3 color=#222222>$key ---> $val</font><br>";
               }
      }
}

class CAuth extends CSession{

      var $user; //Das Objekt Benutzer

      var $debug = 0;

      function CAuth($username, $password, $debug=0){
               CSession::CSession();
               $this->debug = $debug;
               if(!session_is_registered("ses_user")){
                  session_register("ses_user");
                  $this->user = new CUser ($username, $password);
               }
               elseif($GLOBALS["ses_user"]->authenticated==1){
                    $this->user = $GLOBALS["ses_user"];
               }
               else{
                    $this->user = new CUser ($username, $password);
               }
               if ($this->debug==1){
                   $this->show_debug();
               }
      }

      function write_to_session(){
               CSession::write_to_session();
               $GLOBALS["ses_user"]=$this->user;
      }

      function show_debug(){
               $this->user->show_debug();
               echo "<font size=3 color=#222222><br> OBJEKT CAuth<br></font>";
               while(list($key,$val)=each($this)){
                     echo "<font size=3 color=#222222>$key ---> $val</font><br>";
               }
      }
}
?>
Return current item: CAuth