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

Source for file DB.php

Documentation is available at DB.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A framework for authentication and authorization in PHP applications
  6.  *
  7.  * LiveUser is an authentication/permission framework designed
  8.  * to be flexible and easily extendable.
  9.  *
  10.  * Since it is impossible to have a
  11.  * "one size fits all" it takes a container
  12.  * approach which should enable it to
  13.  * be versatile enough to meet most needs.
  14.  *
  15.  * PHP version 4 and 5
  16.  *
  17.  * LICENSE: This library is free software; you can redistribute it and/or
  18.  * modify it under the terms of the GNU Lesser General Public
  19.  * License as published by the Free Software Foundation; either
  20.  * version 2.1 of the License, or (at your option) any later version.
  21.  *
  22.  * This library is distributed in the hope that it will be useful,
  23.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25.  * Lesser General Public License for more details.
  26.  *
  27.  * You should have received a copy of the GNU Lesser General Public
  28.  * License along with this library; if not, write to the Free Software
  29.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30.  * MA  02111-1307  USA
  31.  *
  32.  *
  33.  * @category authentication
  34.  * @package LiveUser
  35.  * @author  Markus Wolff <wolff@21st.de>
  36.  * @author  Helgi Þormar Þorbjörnsson <dufuz@php.net>
  37.  * @author  Lukas Smith <smith@pooteeweet.org>
  38.  * @author  Arnaud Limbourg <arnaud@php.net>
  39.  * @author  Pierre-Alain Joye <pajoye@php.net>
  40.  * @author  Bjoern Kraus <krausbn@php.net>
  41.  * @copyright 2002-2006 Markus Wolff
  42.  * @license http://www.gnu.org/licenses/lgpl.txt
  43.  * @version CVS: $Id: DB.php,v 1.41 2006/08/15 06:43:20 mahono Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  * Require parent class definition and PEAR::DB class.
  49.  */
  50. require_once 'LiveUser/Auth/Common.php';
  51. require_once 'DB.php';
  52.  
  53. /**
  54.  *  DB container for Authentication
  55.  *
  56.  * This is a PEAR::DB backend driver for the LiveUser class.
  57.  * A PEAR::DB connection object can be passed to the constructor to reuse an
  58.  * existing connection. Alternatively, a DSN can be passed to open a new one.
  59.  *
  60.  * Requirements:
  61.  * - File "LiveUser.php" (contains the parent class "LiveUser")
  62.  * - Array of connection options or a PEAR::DB connection object must be
  63.  *   passed to the constructor.
  64.  *   Example: array('dsn' => 'mysql://user:pass@host/db_name',
  65.  *                  'dbc' => &$conn, # PEAR::DB connection object);
  66.  *
  67.  * @category authentication
  68.  * @package LiveUser
  69.  * @author   Markus Wolff <wolff@21st.de>
  70.  * @copyright 2002-2006 Markus Wolff
  71.  * @license http://www.gnu.org/licenses/lgpl.txt
  72.  * @version Release: @package_version@
  73.  * @link http://pear.php.net/LiveUser
  74.  */
  75. {
  76.     /**
  77.      * dsn that was connected to
  78.      *
  79.      * @var    string 
  80.      * @access private
  81.      */
  82.     var $dsn = false;
  83.  
  84.     /**
  85.      * Database connection object.
  86.      *
  87.      * @var    object 
  88.      * @access private
  89.      */
  90.     var $dbc = false;
  91.  
  92.     /**
  93.      * Database connection options.
  94.      *
  95.      * @var    object 
  96.      * @access private
  97.      */
  98.     var $options = array();
  99.  
  100.     /**
  101.      * Table prefix
  102.      * Prefix for all db tables the container has.
  103.      *
  104.      * @var    string 
  105.      * @access public
  106.      */
  107.     var $prefix = 'liveuser_';
  108.  
  109.     /**
  110.      * Load the storage container
  111.      *
  112.      * @param   array  array containing the configuration.
  113.      * @param string  name of the container that should be used
  114.      * @return bool true on success or false on failure
  115.      *
  116.      * @access public
  117.      */
  118.     function init(&$conf$containerName)
  119.     {
  120.         parent::init($conf$containerName);
  121.  
  122.         if (!is_a($this->dbc'db_common'&& !is_null($this->dsn)) {
  123.             $this->options['portability'= DB_PORTABILITY_ALL;
  124.             $dbc =DB::connect($this->dsn$this->options);
  125.             if (PEAR::isError($dbc)) {
  126.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  127.                     array('container' => 'could not connect: '.$dbc->getMessage(),
  128.                     'debug' => $dbc->getUserInfo()));
  129.                 return false;
  130.             }
  131.             $this->dbc =$dbc;
  132.         }
  133.  
  134.         if (!is_a($this->dbc'db_common')) {
  135.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  136.                 array('container' => 'storage layer configuration missing'));
  137.             return false;
  138.         }
  139.  
  140.         return true;
  141.     }
  142.  
  143.     /**
  144.      * Writes current values for user back to the database.
  145.      *
  146.      * @return bool true on success or false on failure
  147.      *
  148.      * @access private
  149.      */
  150.     function _updateUserData()
  151.     {
  152.         if (!array_key_exists('lastlogin'$this->tables['users']['fields'])) {
  153.             return true;
  154.         }
  155.  
  156.         $query  'UPDATE ' $this->prefix . $this->alias['users'].'
  157.                  SET '    $this->alias['lastlogin''=' .
  158.                     $this->dbc->quoteSmart(date('Y-m-d H:i:s'$this->currentLogin)) '
  159.                  WHERE '  $this->alias['auth_user_id']   '=' .
  160.                     $this->dbc->quoteSmart($this->propertyValues['auth_user_id']);
  161.  
  162.         $result $this->dbc->query($query);
  163.  
  164.         if (PEAR::isError($result)) {
  165.             $this->stack->push(
  166.                 LIVEUSER_ERROR'exception',
  167.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  168.             );
  169.             return false;
  170.         }
  171.  
  172.         return true;
  173.     }
  174.  
  175.     /**
  176.      * Reads user data from the given data source
  177.      * If only $handle is given, it will read the data
  178.      * from the first user with that handle and return
  179.      * true on success.
  180.      * If $handle and $passwd are given, it will try to
  181.      * find the first user with both handle and password
  182.      * matching and return true on success (this allows
  183.      * multiple users having the same handle but different
  184.      * passwords - yep, some people want this).
  185.      * if only an auth_user_id is passed it will try to read the data based on the id
  186.      * If no match is found, false is being returned.
  187.      *
  188.      * @param  string user handle
  189.      * @param  string user password
  190.      * @param  bool|intif the user data should be read using the auth user id
  191.      * @return bool true on success or false on failure
  192.      *
  193.      * @access public
  194.      */
  195.     function readUserData($handle ''$passwd ''$auth_user_id = false)
  196.     {
  197.         $fields = array();
  198.         foreach ($this->tables['users']['fields'as $field => $req{
  199.             $fields[$this->alias[$field' AS ' $field;
  200.         }
  201.  
  202.         // Setting the default sql query.
  203.         $query 'SELECT ' implode(','$fields'
  204.                    FROM   ' $this->prefix . $this->alias['users'].'
  205.                    WHERE  ';
  206.         if ($auth_user_id{
  207.             $query .= $this->alias['auth_user_id''='
  208.                 . $this->dbc->quoteSmart($auth_user_id);
  209.         else {
  210.             if (!is_array($this->handles|| empty($this->handles)) {
  211.                 $this->stack->push(
  212.                     LIVEUSER_ERROR_CONFIG'exception',
  213.                     array('reason' => 'No handle set in storage config.')
  214.                 );
  215.                 return false;
  216.             }
  217.             $handles = array();
  218.             foreach ($this->handles as $field{
  219.                 $handles[$this->alias[$field'=' .
  220.                     $this->dbc->quoteSmart($handle);
  221.             }
  222.             $query .= '(' implode(' OR '$handles')';
  223.  
  224.             if (!is_null($this->tables['users']['fields']['passwd'])) {
  225.                 // If $passwd is set, try to find the first user with the given
  226.                 // handle and password.
  227.                 $query .= ' AND   ' $this->alias['passwd''='
  228.                     . $this->dbc->quoteSmart($this->encryptPW($passwd));
  229.             }
  230.         }
  231.  
  232.         // Query database
  233.         $result $this->dbc->getRow($querynullDB_FETCHMODE_ASSOC);
  234.  
  235.         if (PEAR::isError($result)) {
  236.             $this->stack->push(
  237.                 LIVEUSER_ERROR'exception',
  238.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  239.             );
  240.             return false;
  241.         }
  242.  
  243.         if (!is_array($result)) {
  244.             return null;
  245.         }
  246.  
  247.         // User was found, read data into class variables and set return value to true
  248.         if (array_key_exists('lastlogin'$result&& !empty($result['lastlogin'])) {
  249.             $result['lastlogin'strtotime($result['lastlogin']);
  250.         }
  251.         $this->propertyValues = $result;
  252.  
  253.         return true;
  254.     }
  255.  
  256.     /**
  257.      * Properly disconnect from database
  258.      *
  259.      * @return bool true on success or false on failure
  260.      *
  261.      * @access public
  262.      */
  263.     function disconnect()
  264.     {
  265.         if ($this->dsn{
  266.             $result $this->dbc->disconnect();
  267.             if (PEAR::isError($result)) {
  268.                 $this->stack->push(
  269.                     LIVEUSER_ERROR'exception',
  270.                     array('reason' => $result->getMessage('-' $result->getUserInfo())
  271.                 );
  272.                 return false;
  273.             }
  274.             $this->dbc = false;
  275.         }
  276.         return true;
  277.     }
  278. }
  279. ?>

Documentation generated on Mon, 28 Jan 2008 03:30:11 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.