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

Source for file MDB2.php

Documentation is available at MDB2.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: MDB2.php,v 1.47 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::MDB2 class.
  49.  */
  50. require_once 'LiveUser/Auth/Common.php';
  51. require_once 'MDB2.php';
  52. MDB2::loadFile('Date');
  53.  
  54. /**
  55.  * MDB2 container for Authentication
  56.  *
  57.  * This is a PEAR::MDB2 backend driver for the LiveUser class.
  58.  * A PEAR::MDB2 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::MDB2 connection object must be
  64.  *   passed to the constructor.
  65.  *   Example: array('dsn' => 'mysql://user:pass@host/db_name',
  66.  *                  'dbc' => &$conn, # PEAR::MDB2 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.     /**
  110.      * Table prefix
  111.      * Prefix for all db tables the container has.
  112.      *
  113.      * @var    string 
  114.      * @access public
  115.      */
  116.     var $prefix = 'liveuser_';
  117.  
  118.     /**
  119.      * determines of the use of sequences should be forced
  120.      *
  121.      * @var bool 
  122.      * @access private
  123.      */
  124.     var $force_seq = true;
  125.  
  126.     /**
  127.      * Load the storage container
  128.      *
  129.      * @param   array  array containing the configuration.
  130.      * @param string  name of the container that should be used
  131.      * @return bool true on success or false on failure
  132.      *
  133.      * @access public
  134.      */
  135.     function init(&$conf$containerName)
  136.     {
  137.         parent::init($conf$containerName);
  138.  
  139.         if (!MDB2::isConnection($this->dbc&& !is_null($this->dsn)) {
  140.             $this->options['portability'= MDB2_PORTABILITY_ALL;
  141.             if ($this->function == 'singleton'{
  142.                 $dbc =MDB2::singleton($this->dsn$this->options);
  143.             else {
  144.                 $dbc =MDB2::connect($this->dsn$this->options);
  145.             }
  146.             if (PEAR::isError($dbc)) {
  147.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  148.                     array('container' => 'could not connect: '.$dbc->getMessage(),
  149.                     'debug' => $dbc->getUserInfo()));
  150.                 return false;
  151.             }
  152.             $this->dbc =$dbc;
  153.         }
  154.  
  155.         if (!MDB2::isConnection($this->dbc)) {
  156.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  157.                 array('container' => 'storage layer configuration missing'));
  158.             return false;
  159.         }
  160.  
  161.         return true;
  162.     }
  163.  
  164.     /**
  165.      * Writes current values for the user back to the database.
  166.      *
  167.      * @return bool true on success or false on failure
  168.      *
  169.      * @access private
  170.      */
  171.     function _updateUserData()
  172.     {
  173.         if (!array_key_exists('lastlogin'$this->tables['users']['fields'])) {
  174.             return true;
  175.         }
  176.  
  177.         $query  'UPDATE ' $this->prefix . $this->alias['users'].'
  178.                  SET '    $this->alias['lastlogin']
  179.                     .'='  $this->dbc->quote(MDB2_Date::unix2Mdbstamp($this->currentLogin)$this->fields['lastlogin']'
  180.                  WHERE '  $this->alias['auth_user_id']
  181.                     .'='  $this->dbc->quote($this->propertyValues['auth_user_id']$this->fields['auth_user_id']);
  182.  
  183.         $result $this->dbc->exec($query);
  184.  
  185.         if (PEAR::isError($result)) {
  186.             $this->stack->push(
  187.                 LIVEUSER_ERROR'exception',
  188.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  189.             );
  190.             return false;
  191.         }
  192.  
  193.         return true;
  194.     }
  195.  
  196.     /**
  197.      * Reads user data from the given data source
  198.      * If only $handle is given, it will read the data
  199.      * from the first user with that handle and return
  200.      * true on success.
  201.      * If $handle and $passwd are given, it will try to
  202.      * find the first user with both handle and password
  203.      * matching and return true on success (this allows
  204.      * multiple users having the same handle but different
  205.      * passwords - yep, some people want this).
  206.      * if only an auth_user_id is passed it will try to read the data based on the id
  207.      * If no match is found, false is being returned.
  208.      *
  209.      * @param  string user handle
  210.      * @param  string user password
  211.      * @param  bool|intif the user data should be read using the auth user id
  212.      * @return bool true on success or false on failure
  213.      *
  214.      * @access public
  215.      */
  216.     function readUserData($handle ''$passwd ''$auth_user_id = false)
  217.     {
  218.         $fields $types = array();
  219.         foreach ($this->tables['users']['fields'as $field => $req{
  220.             $fields[$this->alias[$field' AS ' $field;
  221.             $types[$this->fields[$field];
  222.         }
  223.  
  224.         // Setting the default query.
  225.         $query 'SELECT ' implode(','$fields'
  226.                    FROM '   $this->prefix . $this->alias['users''
  227.                    WHERE  ';
  228.         if ($auth_user_id{
  229.             $query .= $this->alias['auth_user_id''='
  230.                 . $this->dbc->quote($auth_user_id$this->fields['auth_user_id']);
  231.         else {
  232.             if (!is_array($this->handles|| empty($this->handles)) {
  233.                 $this->stack->push(
  234.                     LIVEUSER_ERROR_CONFIG'exception',
  235.                     array('reason' => 'No handle set in storage config.')
  236.                 );
  237.                 return false;
  238.             }
  239.             $handles = array();
  240.             foreach ($this->handles as $field{
  241.                 $handles[$this->alias[$field'=' .
  242.                     $this->dbc->quote($handle$this->fields[$field]);
  243.             }
  244.             $query .= '(' implode(' OR '$handles')';
  245.  
  246.             if (!is_null($this->tables['users']['fields']['passwd'])) {
  247.                 // If $passwd is set, try to find the first user with the given
  248.                 // handle and password.
  249.                 $query .= ' AND   ' $this->alias['passwd''='
  250.                     . $this->dbc->quote($this->encryptPW($passwd)$this->fields['passwd']);
  251.             }
  252.         }
  253.  
  254.         // Query database
  255.         $result $this->dbc->queryRow($query$typesMDB2_FETCHMODE_ASSOC);
  256.  
  257.         if (PEAR::isError($result)) {
  258.             $this->stack->push(
  259.                 LIVEUSER_ERROR'exception',
  260.                 array('reason' => $result->getMessage('-' $result->getUserInfo())
  261.             );
  262.             return false;
  263.         }
  264.  
  265.         if (!is_array($result)) {
  266.             return null;
  267.         }
  268.  
  269.         // User was found, read data into class variables and set return value to true
  270.         if (array_key_exists('lastlogin'$result&& !empty($result['lastlogin'])) {
  271.             $result['lastlogin'= MDB2_Date::mdbstamp2Unix($result['lastlogin']);
  272.         }
  273.         $this->propertyValues = $result;
  274.  
  275.         return true;
  276.     }
  277.  
  278.     /**
  279.      * properly disconnect from database
  280.      *
  281.      * @return bool true on success or false on failure
  282.      *
  283.      * @access public
  284.      */
  285.     function disconnect()
  286.     {
  287.         if ($this->dsn{
  288.             $result $this->dbc->disconnect();
  289.             if (PEAR::isError($result)) {
  290.                 $this->stack->push(
  291.                     LIVEUSER_ERROR'exception',
  292.                     array('reason' => $result->getMessage('-' $result->getUserInfo())
  293.                 );
  294.                 return false;
  295.             }
  296.             $this->dbc = false;
  297.         }
  298.         return true;
  299.     }
  300. }
  301. ?>

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