"Fossies" - the Fresh Open Source Software Archive

Member "openmailadmin-1.0.1/inc/lib/User.php" (30 Jul 2007, 2744 Bytes) of package /linux/privat/old/openmailadmin-1.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "User.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * Does administer all the properties, permissions and any relationship
    4  * methods, such as "is_descendant" etc.
    5  * Use instances of this class whenever data about any user is needed.
    6  */
    7 class User
    8 {
    9     public static       $db;
   10     public static       $tablenames;
   11 
   12     public      $password;
   13     private     $data       = array();
   14 
   15     /**
   16      * @param   username    User must exist.
   17      * @throws  Exception   if user does not exist.
   18      */
   19     public function __construct($username) {
   20         global $cfg;
   21         $data = self::$db->GetRow('SELECT * FROM '.self::$tablenames['user'].' WHERE mbox='.self::$db->qstr($username));
   22         if($data === false) {
   23             throw new Exception(txt(2));
   24         }
   25         $this->password = new Password($this, $data['password'], new $cfg['passwd']['strategy']());
   26         unset($data['password']);
   27         $this->data = $data;
   28     }
   29 
   30     /**
   31      * This is from Openmaillist's DataCarrier.
   32      *
   33      * @throw       If no value for $key has yet been set.
   34      */
   35     protected function __get($key) {
   36         if(array_key_exists($key, $this->data)) {
   37             return $this->data[$key];
   38         } else {
   39             throw new Exception('Variable does not exist or has not been set.');
   40         }
   41     }
   42 
   43     protected function __set($key, $value) {
   44         if(is_null($value)) {
   45             if(array_key_exists($key, $this->data)) {
   46                 unset($this->data[$key]);
   47             }
   48         } else {
   49             $this->data[$key] = $value;
   50         }
   51         return true;
   52     }
   53 
   54     /**
   55      * Immediately set given column in database to the given value.
   56      *
   57      * @param   attribute   Name of attribute/SQL column to be set.
   58      * @param   value       The value the field shall be assigned.
   59      * @return  boolean     True if column has been changed successfully.
   60      */
   61     public function immediate_set($attribute, $value) {
   62         self::$db->Execute('UPDATE '.self::$tablenames['user']
   63                 .' SET '.$attribute.'='.self::$db->qstr($value)
   64                 .' WHERE mbox='.self::$db->qstr($this->mbox));
   65         if($attribute != 'password')
   66             $this->{$attribute} = $value;
   67         if(self::$db->ErrorNo() != 0)
   68             throw new RuntimeException('Cannot set "'.$attribute.'" to "'.$value.'".');
   69         return true;
   70     }
   71 
   72     public function update_last_login() {
   73         return self::$db->Execute('UPDATE '.self::$tablenames['user'].' SET last_login='.time().' WHERE mbox='.self::$db->qstr($this->mbox));
   74     }
   75 
   76     /**
   77      * Use this to get a new user only if given plaintext password matches.
   78      *
   79      * @param   username    User must exist.
   80      * @param   password    Plaintext password
   81      * @return          User
   82      * @throws  Exception   if user does not exist or password didn't match.
   83      */
   84     public static function authenticate($username, $password) {
   85         $usr    = new User($username);
   86         if($usr->password->equals($password)) {
   87             $usr->update_last_login();
   88             $usr->password->store_plaintext($password);
   89             return $usr;
   90         }
   91         throw new Exception(txt(0));
   92     }
   93 
   94     public function is_superuser() {
   95         return $this->a_super >= 1;
   96     }
   97 
   98 }
   99 ?>