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

Source for file MDB.php

Documentation is available at MDB.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: MDB.php,v 1.43 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::MDB class.
  49.  */
  50. require_once 'LiveUser/Auth/Common.php';
  51. require_once 'MDB.php';
  52. MDB::loadFile('Date');
  53.  
  54. /**
  55.  * MDB container for Authentication
  56.  *
  57.  * This is a PEAR::MDB backend driver for the LiveUser class.
  58.  * A PEAR::MDB connection object can be passed to the constructor to reuse an
  59.  * existing connection. Alternatively, a DSN can be passed to open a new one.
  60.  *
  61.  * Requirements:
  62.  * - File "LiveUser.php" (contains the parent class "LiveUser")
  63.  * - Array of connection options or a PEAR::MDB connection object must be
  64.  *   passed to the constructor.
  65.  *   Example: array('dsn' => 'mysql://user:pass@host/db_name',
  66.  *                  'dbc' => &$conn, # PEAR::MDB connection object);
  67.  *
  68.  * @category authentication
  69.  * @package LiveUser
  70.  * @author   Markus Wolff <wolff@21st.de>
  71.  * @copyright 2002-2006 Markus Wolff
  72.  * @license http://www.gnu.org/licenses/lgpl.txt
  73.  * @version Release: @package_version@
  74.  * @link http://pear.php.net/LiveUser
  75.  */
  76. {
  77.     /**
  78.      * dsn that was connected to
  79.      *
  80.      * @var    string 
  81.      * @access private
  82.      */
  83.     var $dsn = false;
  84.  
  85.     /**
  86.      * Database connection object.
  87.      *
  88.      * @var    object 
  89.      * @access private
  90.      */
  91.     var $dbc = false;
  92.  
  93.     /**
  94.      * Database connection options.
  95.      *
  96.      * @var    object 
  97.      * @access private
  98.      */
  99.     var $options = array();
  100.  
  101.     /**
  102.      * Database connection functions
  103.      *
  104.      * @var    object 
  105.      * @access private
  106.      */
  107.     var $function 'connect';
  108.     /**
  109.      * Table prefix
  110.      * Prefix for all db tables the container has.
  111.      *
  112.      * @var    string 
  113.      * @access public
  114.      */
  115.     var $prefix = 'liveuser_';
  116.  
  117.     /**
  118.      * Load the storage container
  119.      *
  120.      * @param   array  array containing the configuration.
  121.      * @param string  name of the container that should be used
  122.      * @return bool true on success or false on failure
  123.      *
  124.      * @access public
  125.      */
  126.     function init(&$conf$containerName)
  127.     {
  128.         parent::init($conf$containerName);
  129.  
  130.         if (!MDB::isConnection($this->dbc&& !is_null($this->dsn)) {
  131.             $this->options['optimize''portability';
  132.             if ($function == 'singleton'{
  133.                 $dbc =MDB::singleton($this->dsn$this->options);
  134.             else {
  135.                 $dbc =MDB::connect($this->dsn$this->options);
  136.             }
  137.             if (PEAR::isError($dbc)) {
  138.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  139.                     array('container' => 'could not connect: '.$dbc->getMessage(),
  140.                     'debug' => $dbc->getUserInfo()));
  141.                 return false;
  142.             }
  143.             $this->dbc =$dbc;
  144.         }
  145.  
  146.         if (!MDB::isConnection($this->dbc)) {
  147.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  148.                 array('container' => 'storage layer configuration missing'));
  149.             return false;
  150.         }
  151.  
  152.         return true;
  153.     }
  154.  
  155.     /**
  156.      * Writes current values for user back to the database.
  157.      *
  158.      * @return bool true on success or false on failure
  159.      *
  160.      * @access private
  161.      */
  162.     function _updateUserData()
  163.     {
  164.         if (!array_key_exists('lastlogin'$this->tables['users']['fields'])) {
  165.             return true;
  166.         }
  167.  
  168.         $query  'UPDATE ' $this->prefix . $this->alias['users'].'
  169.                  SET '    $this->alias['lastlogin']
  170.                     .'='  $this->dbc->getValue($this->fields['lastlogin']MDB_Date::unix2Mdbstamp($this->currentLogin)) '
  171.                  WHERE '  $this->alias['auth_user_id']
  172.                     .'='  $this->dbc->getValue($this->fields['auth_user_id']$this->propertyValues['auth_user_id']);
  173.  
  174.         $result $this->dbc->query($query);
  175.  
  176.         if (PEAR::isError($result)) {
  177.             $this->stack->push(
  178.                 LIVEUSER_ERROR'exception',
  179.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  180.             );
  181.             return false;
  182.         }
  183.  
  184.         return true;
  185.     }
  186.  
  187.     /**
  188.      * Reads user data from the given data source
  189.      * If only $handle is given, it will read the data
  190.      * from the first user with that handle and return
  191.      * true on success.
  192.      * If $handle and $passwd are given, it will try to
  193.      * find the first user with both handle and password
  194.      * matching and return true on success (this allows
  195.      * multiple users having the same handle but different
  196.      * passwords - yep, some people want this).
  197.      * if only an auth_user_id is passed it will try to read the data based on the id
  198.      * If no match is found, false is being returned.
  199.      *
  200.      * @param  string user handle
  201.      * @param  string user password
  202.      * @param  bool|intif the user data should be read using the auth user id
  203.      * @return bool true on success or false on failure
  204.      *
  205.      * @access public
  206.      */
  207.     function readUserData($handle ''$passwd ''$auth_user_id = false)
  208.     {
  209.         $fields $types = array();
  210.         foreach ($this->tables['users']['fields'as $field => $req{
  211.             $fields[$this->alias[$field' AS ' $field;
  212.             $types[$this->fields[$field];
  213.         }
  214.  
  215.         // Setting the default query.
  216.         $query 'SELECT ' implode(','$fields'
  217.                    FROM '   $this->prefix . $this->alias['users''
  218.                    WHERE  ';
  219.         if ($auth_user_id{
  220.             $query .= $this->alias['auth_user_id''='
  221.                 . $this->dbc->getValue($this->fields['auth_user_id']$auth_user_id);
  222.         else {
  223.             if (!is_array($this->handles|| empty($this->handles)) {
  224.                 $this->stack->push(
  225.                     LIVEUSER_ERROR_CONFIG'exception',
  226.                     array('reason' => 'No handle set in storage config.')
  227.                 );
  228.                 return false;
  229.             }
  230.             $handles = array();
  231.             foreach ($this->handles as $field{
  232.                 $handles[$this->alias[$field'=' .
  233.                     $this->dbc->getValue($this->fields[$field]$handle);
  234.             }
  235.             $query .= '(' implode(' OR '$handles')';
  236.  
  237.             if (!is_null($this->tables['users']['fields']['passwd'])) {
  238.                 // If $passwd is set, try to find the first user with the given
  239.                 // handle and password.
  240.                 $query .= ' AND   ' $this->alias['passwd''='
  241.                     . $this->dbc->getValue($this->fields['passwd']$this->encryptPW($passwd));
  242.             }
  243.         }
  244.  
  245.         // Query database
  246.         $result $this->dbc->queryRow($query$typesMDB_FETCHMODE_ASSOC);
  247.  
  248.         if (PEAR::isError($result)) {
  249.             $this->stack->push(
  250.                 LIVEUSER_ERROR'exception',
  251.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  252.             );
  253.             return false;
  254.         }
  255.  
  256.         if (!is_array($result)) {
  257.             return null;
  258.         }
  259.  
  260.         // User was found, read data into class variables and set return value to true
  261.         if (array_key_exists('lastlogin'$result&& !empty($result['lastlogin'])) {
  262.             $result['lastlogin'= MDB_Date::mdbstamp2Unix($result['lastlogin']);
  263.         }
  264.         $this->propertyValues = $result;
  265.  
  266.         return true;
  267.     }
  268.  
  269.     /**
  270.      * Properly disconnect from database
  271.      *
  272.      * @return bool true on success or false on failure
  273.      *
  274.      * @access public
  275.      */
  276.     function disconnect()
  277.     {
  278.         if ($this->dsn{
  279.             $result $this->dbc->disconnect();
  280.             if (PEAR::isError($result)) {
  281.                 $this->stack->push(
  282.                     LIVEUSER_ERROR'exception',
  283.                     array('reason' => $result->getMessage('-' $result->getUserInfo())
  284.                 );
  285.                 return false;
  286.             }
  287.             $this->dbc = false;
  288.         }
  289.         return true;
  290.     }
  291. }
  292. ?>

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