Auth
[ class tree: Auth ] [ index: Auth ] [ all elements ]

Source for file DBLite.php

Documentation is available at DBLite.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // |                                                                      |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: DBLite.php,v 1.2 2004/07/02 22:42:00 yavo Exp $
  20. //
  21.  
  22. require_once 'Auth/Container.php';
  23. require_once 'DB.php';
  24.  
  25. /**
  26.  * A lighter storage driver for fetching login data from a database
  27.  *
  28.  * This driver is derived from the DB storage container but
  29.  * with the user manipulation function removed for smaller file size
  30.  * by the PEAR DB abstraction layer to fetch login data.
  31.  *
  32.  * @author   Martin Jansen <mj@php.net>
  33.  * @package  Auth
  34.  * @version  $Revision: 1.2 $
  35.  */
  36. {
  37.  
  38.     /**
  39.      * Additional options for the storage container
  40.      * @var array 
  41.      */
  42.     var $options = array();
  43.  
  44.     /**
  45.      * DB object
  46.      * @var object 
  47.      */
  48.     var $db = null;
  49.     var $dsn = '';
  50.  
  51.     /**
  52.      * User that is currently selected from the DB.
  53.      * @var string 
  54.      */
  55.     var $activeUser = '';
  56.  
  57.     // {{{ Constructor
  58.  
  59.     /**
  60.      * Constructor of the container class
  61.      *
  62.      * Initate connection to the database via PEAR::DB
  63.      *
  64.      * @param  string Connection data or DB object
  65.      * @return object Returns an error object if something went wrong
  66.      */
  67.     function Auth_Container_DB($dsn)
  68.     {
  69.         $this->options['table']       'auth';
  70.         $this->options['usernamecol''username';
  71.         $this->options['passwordcol''password';
  72.         $this->options['dsn']         '';
  73.         $this->options['db_fields']   '';
  74.         $this->options['cryptType']   'md5';
  75.         $this->options['db_options']  = array();
  76.  
  77.         if (is_array($dsn)) {
  78.             $this->_parseOptions($dsn);
  79.             if (empty($this->options['dsn'])) {
  80.                 PEAR::raiseError('No connection parameters specified!');
  81.             }
  82.         else {
  83.             $this->options['dsn'$dsn;
  84.         }
  85.     }
  86.  
  87.     /**
  88.      * Connect to database by using the given DSN string
  89.      *
  90.      * @access private
  91.      * @param  string DSN string
  92.      * @return mixed  Object on error, otherwise bool
  93.      */
  94.     function _connect(&$dsn)
  95.     {
  96.         if (is_string($dsn|| is_array($dsn)) {
  97.             $this->db =DB::connect($dsn$this->options['db_options']);
  98.         elseif (get_parent_class($dsn== "db_common"{
  99.             $this->db =$dsn;
  100.         else {
  101.             return PEAR::raiseError("Invalid dsn or db object given");
  102.         }
  103.  
  104.         if (DB::isError($this->db|| PEAR::isError($this->db)) {
  105.             return PEAR::raiseError($this->db->getMessage()$this->db->getCode());
  106.         else {
  107.             return true;
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * Prepare database connection
  113.      *
  114.      * This function checks if we have already opened a connection to
  115.      * the database. If that's not the case, a new connection is opened.
  116.      *
  117.      * @access private
  118.      * @return mixed True or a DB error object.
  119.      */
  120.     function _prepare({
  121.         if (!DB::isConnection($this->db)) {
  122.             $res $this->_connect($this->options['dsn']);
  123.             if (DB::isError($res|| PEAR::isError($res)) {
  124.                 return $res;
  125.             }
  126.         }
  127.         return true;
  128.     }
  129.  
  130.     /**
  131.      * Parse options passed to the container class
  132.      *
  133.      * @access private
  134.      * @param  array 
  135.      */
  136.     function _parseOptions($array{
  137.         foreach ($array as $key => $value{
  138.             if (isset($this->options[$key])) {
  139.                 $this->options[$key$value;
  140.             }
  141.         }
  142.  
  143.         /* Include additional fields if they exist */
  144.         if (!empty($this->options['db_fields'])) {
  145.             if (is_array($this->options['db_fields'])) {
  146.                 $this->options['db_fields'join($this->options['db_fields']', ');
  147.             }
  148.             $this->options['db_fields'', '.$this->options['db_fields'];
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Get user information from database
  154.      *
  155.      * This function uses the given username to fetch
  156.      * the corresponding login data from the database
  157.      * table. If an account that matches the passed username
  158.      * and password is found, the function returns true.
  159.      * Otherwise it returns false.
  160.      *
  161.      * @param   string Username
  162.      * @param   string Password
  163.      * @return  mixed  Error object or boolean
  164.      */
  165.     function fetchData($username$password)
  166.     {
  167.         // Prepare for a database query
  168.         $err $this->_prepare();
  169.         if ($err !== true{
  170.             return PEAR::raiseError($err->getMessage()$err->getCode());
  171.         }
  172.  
  173.         // Find if db_fields contains a *, if so assume all col are selected
  174.         if (strstr($this->options['db_fields']'*')) {
  175.             $sql_from "*";
  176.         }
  177.         else{
  178.             $sql_from $this->options['usernamecol'", ".$this->options['passwordcol'].$this->options['db_fields'];
  179.         }
  180.         
  181.         $query "SELECT ".$sql_from.
  182.                 " FROM ".$this->options['table'].
  183.                 " WHERE ".$this->options['usernamecol']." = '".$this->db->quoteString($username)."'";
  184.         $res $this->db->getRow($querynullDB_FETCHMODE_ASSOC);
  185.  
  186.         if (DB::isError($res)) {
  187.             return PEAR::raiseError($res->getMessage()$res->getCode());
  188.         }
  189.         if (!is_array($res)) {
  190.             $this->activeUser = '';
  191.             return false;
  192.         }
  193.         if ($this->verifyPassword(trim($password"\r\n"),
  194.                                   trim($res[$this->options['passwordcol']]"\r\n"),
  195.                                   $this->options['cryptType'])) {
  196.             // Store additional field values in the session
  197.             foreach ($res as $key => $value{
  198.                 if ($key == $this->options['passwordcol'||
  199.                     $key == $this->options['usernamecol']{
  200.                     continue;
  201.                 }
  202.                 // Use reference to the auth object if exists
  203.                 // This is because the auth session variable can change so a static call to setAuthData does not make sence
  204.                 if (is_object($this->_auth_obj)) {
  205.                     $this->_auth_obj->setAuthData($key$value);
  206.                 else {
  207.                     Auth::setAuthData($key$value);
  208.                 }
  209.             }
  210.             $this->activeUser = $res[$this->options['usernamecol']];
  211.             return true;
  212.         }
  213.         $this->activeUser = $res[$this->options['usernamecol']];
  214.         return false;
  215.     }
  216. }
  217. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.