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

Source for file DBLite.php

Documentation is available at DBLite.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  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.5 2006/02/28 02:19:22 aashley 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.  * @author   Adam Ashley <aashley@php.net>
  34.  * @package  Auth
  35.  * @version  $Revision: 1.5 $
  36.  */
  37. {
  38.  
  39.     // {{{ properties
  40.  
  41.     /**
  42.      * Additional options for the storage container
  43.      * @var array 
  44.      */
  45.     var $options = array();
  46.  
  47.     /**
  48.      * DB object
  49.      * @var object 
  50.      */
  51.     var $db = null;
  52.     var $dsn = '';
  53.  
  54.     /**
  55.      * User that is currently selected from the DB.
  56.      * @var string 
  57.      */
  58.     var $activeUser = '';
  59.  
  60.     // }}}
  61.  
  62.     // {{{ Auth_Container_DBLite() [constructor]
  63.  
  64.     /**
  65.      * Constructor of the container class
  66.      *
  67.      * Initate connection to the database via PEAR::DB
  68.      *
  69.      * @param  string Connection data or DB object
  70.      * @return object Returns an error object if something went wrong
  71.      */
  72.     function Auth_Container_DBLite($dsn)
  73.     {
  74.         $this->options['table']       'auth';
  75.         $this->options['usernamecol''username';
  76.         $this->options['passwordcol''password';
  77.         $this->options['dsn']         '';
  78.         $this->options['db_fields']   '';
  79.         $this->options['cryptType']   'md5';
  80.         $this->options['db_options']  = array();
  81.  
  82.         if (is_array($dsn)) {
  83.             $this->_parseOptions($dsn);
  84.             if (empty($this->options['dsn'])) {
  85.                 PEAR::raiseError('No connection parameters specified!');
  86.             }
  87.         else {
  88.             $this->options['dsn'$dsn;
  89.         }
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ _connect()
  94.  
  95.     /**
  96.      * Connect to database by using the given DSN string
  97.      *
  98.      * @access private
  99.      * @param  string DSN string
  100.      * @return mixed  Object on error, otherwise bool
  101.      */
  102.     function _connect(&$dsn)
  103.     {
  104.         if (is_string($dsn|| is_array($dsn)) {
  105.             $this->db =DB::connect($dsn$this->options['db_options']);
  106.         elseif (is_subclass_of($dsn"db_common")) {
  107.             $this->db =$dsn;
  108.         else {
  109.             return PEAR::raiseError("Invalid dsn or db object given");
  110.         }
  111.  
  112.         if (DB::isError($this->db|| PEAR::isError($this->db)) {
  113.             return PEAR::raiseError($this->db->getMessage()$this->db->getCode());
  114.         else {
  115.             return true;
  116.         }
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ _prepare()
  121.  
  122.     /**
  123.      * Prepare database connection
  124.      *
  125.      * This function checks if we have already opened a connection to
  126.      * the database. If that's not the case, a new connection is opened.
  127.      *
  128.      * @access private
  129.      * @return mixed True or a DB error object.
  130.      */
  131.     function _prepare()
  132.     {
  133.         if (!DB::isConnection($this->db)) {
  134.             $res $this->_connect($this->options['dsn']);
  135.             if (DB::isError($res|| PEAR::isError($res)) {
  136.                 return $res;
  137.             }
  138.         }
  139.         return true;
  140.     }
  141.  
  142.     // }}}
  143.     // {{{ _parseOptions()
  144.  
  145.     /**
  146.      * Parse options passed to the container class
  147.      *
  148.      * @access private
  149.      * @param  array 
  150.      */
  151.     function _parseOptions($array)
  152.     {
  153.         foreach ($array as $key => $value{
  154.             if (isset($this->options[$key])) {
  155.                 $this->options[$key$value;
  156.             }
  157.         }
  158.  
  159.         /* Include additional fields if they exist */
  160.         if (!empty($this->options['db_fields'])) {
  161.             if (is_array($this->options['db_fields'])) {
  162.                 $this->options['db_fields'join($this->options['db_fields']', ');
  163.             }
  164.             $this->options['db_fields'', '.$this->options['db_fields'];
  165.         }
  166.     }
  167.  
  168.     // }}}
  169.     // {{{ fetchData()
  170.  
  171.     /**
  172.      * Get user information from database
  173.      *
  174.      * This function uses the given username to fetch
  175.      * the corresponding login data from the database
  176.      * table. If an account that matches the passed username
  177.      * and password is found, the function returns true.
  178.      * Otherwise it returns false.
  179.      *
  180.      * @param   string Username
  181.      * @param   string Password
  182.      * @return  mixed  Error object or boolean
  183.      */
  184.     function fetchData($username$password)
  185.     {
  186.         // Prepare for a database query
  187.         $err $this->_prepare();
  188.         if ($err !== true{
  189.             return PEAR::raiseError($err->getMessage()$err->getCode());
  190.         }
  191.  
  192.         // Find if db_fields contains a *, if so assume all col are selected
  193.         if (strstr($this->options['db_fields']'*')) {
  194.             $sql_from "*";
  195.         else {
  196.             $sql_from $this->options['usernamecol'", ".$this->options['passwordcol'].$this->options['db_fields'];
  197.         }
  198.         
  199.         $query "SELECT ".$sql_from.
  200.                 " FROM ".$this->options['table'].
  201.                 " WHERE ".$this->options['usernamecol']." = ".$this->db->quoteSmart($username);
  202.         $res $this->db->getRow($querynullDB_FETCHMODE_ASSOC);
  203.  
  204.         if (DB::isError($res)) {
  205.             return PEAR::raiseError($res->getMessage()$res->getCode());
  206.         }
  207.         if (!is_array($res)) {
  208.             $this->activeUser = '';
  209.             return false;
  210.         }
  211.         if ($this->verifyPassword(trim($password"\r\n"),
  212.                                   trim($res[$this->options['passwordcol']]"\r\n"),
  213.                                   $this->options['cryptType'])) {
  214.             // Store additional field values in the session
  215.             foreach ($res as $key => $value{
  216.                 if ($key == $this->options['passwordcol'||
  217.                     $key == $this->options['usernamecol']{
  218.                     continue;
  219.                 }
  220.                 // Use reference to the auth object if exists
  221.                 // This is because the auth session variable can change so a static call to setAuthData does not make sence
  222.                 if (is_object($this->_auth_obj)) {
  223.                     $this->_auth_obj->setAuthData($key$value);
  224.                 else {
  225.                     Auth::setAuthData($key$value);
  226.                 }
  227.             }
  228.             $this->activeUser = $res[$this->options['usernamecol']];
  229.             return true;
  230.         }
  231.         $this->activeUser = $res[$this->options['usernamecol']];
  232.         return false;
  233.     }
  234.  
  235.     // }}}
  236.  
  237. }
  238. ?>

Documentation generated on Mon, 11 Mar 2019 14:37:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.