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

Source for file PDO.php

Documentation is available at PDO.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.  * @category authentication
  33.  * @package LiveUser
  34.  * @author  Markus Wolff <wolff@21st.de>
  35.  * @author  Helgi Þormar Þorbjörnsson <dufuz@php.net>
  36.  * @author  Lukas Smith <smith@pooteeweet.org>
  37.  * @author  Arnaud Limbourg <arnaud@php.net>
  38.  * @author  Pierre-Alain Joye <pajoye@php.net>
  39.  * @author  Bjoern Kraus <krausbn@php.net>
  40.  * @copyright 2002-2006 Markus Wolff
  41.  * @license http://www.gnu.org/licenses/lgpl.txt
  42.  * @version CVS: $Id: PDO.php,v 1.23 2006/08/15 06:43:20 mahono Exp $
  43.  * @link http://pear.php.net/LiveUser
  44.  */
  45.  
  46. /**
  47.  * Require parent class definition
  48.  */
  49. require_once 'LiveUser/Auth/Common.php';
  50.  
  51. /**
  52.  * PDO container for Authentication. This is a PHP5 only driver.
  53.  *
  54.  * This is a PDO backend driver for the LiveUser class.
  55.  *
  56.  * Requirements:
  57.  * - PHP 5
  58.  * - File "LiveUser.php" (contains the parent class "LiveUser")
  59.  * - Array of connection options
  60.  *   passed to the constructor.
  61.  *   Example: array('dsn'     => 'mysql:host:localhost;dbname=db_name',
  62.  *                  'options' => array('username' => 'root', 'password' => 'secret', 'attr' => array()));
  63.  *
  64.  * @category  authentication
  65.  * @package   LiveUser
  66.  * @author    Arnaud Limbourg <arnaud@php.net>
  67.  * @copyright 2002-2006 Markus Wolff
  68.  * @license   http://www.gnu.org/licenses/lgpl.txt
  69.  * @version   Release: @package_version@
  70.  * @link      http://pear.php.net/LiveUser
  71.  */
  72. {
  73.     /**
  74.      * dsn that was connected to
  75.      *
  76.      * @var    string 
  77.      * @access private
  78.      */
  79.     var $dsn = false;
  80.  
  81.     /**
  82.      * Database connection object.
  83.      *
  84.      * @var    object 
  85.      * @access private
  86.      */
  87.     var $dbc = false;
  88.  
  89.     /**
  90.      * Database connection options.
  91.      *
  92.      * @var    object 
  93.      * @access private
  94.      */
  95.     var $options = array();
  96.  
  97.     /**
  98.      * Table prefix
  99.      * Prefix for all db tables the container has.
  100.      *
  101.      * @var    string 
  102.      * @access public
  103.      */
  104.     var $prefix = 'liveuser_';
  105.  
  106.     /**
  107.      * determines of the use of sequences should be forced
  108.      *
  109.      * @var bool 
  110.      * @access private
  111.      */
  112.     var $force_seq = false;
  113.  
  114.     /**
  115.      * Load the storage container
  116.      *
  117.      * @param   array  array containing the configuration.
  118.      * @param string  name of the container that should be used
  119.      * @return bool true on success or false on failure
  120.      *
  121.      * @access public
  122.      */
  123.     function init(&$conf$containerName)
  124.     {
  125.         parent::init($conf$containerName);
  126.  
  127.         if (!is_a($this->dbc'pdo'&& !is_null($this->dsn)) {
  128.             $login $password $extra = null;
  129.             if (!empty($this->options)) {
  130.                 if (array_key_exists('username'$this->options)) {
  131.                     $login $this->options['username'];
  132.                 }
  133.                 if (array_key_exists('password'$this->options)) {
  134.                     $password $this->options['password'];
  135.                 }
  136.                 if (array_key_exists('attr'$this->options)) {
  137.                     $extra $this->options['attr'];
  138.                 }
  139.             }
  140.             try {
  141.                 $dbc = new PDO($this->dsn$login$password$extra);
  142.             catch (PDOException $e{
  143.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  144.                     array(
  145.                         'container' => 'could not connect: ' $e->getMessage(),
  146.                         'debug'     => $e->getTrace()
  147.                     )
  148.                 );
  149.                 return false;
  150.             }
  151.             $this->dbc $dbc;
  152.         }
  153.  
  154.         if (!is_a($this->dbc'pdo')) {
  155.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  156.                 array('container' => 'storage layer configuration missing'));
  157.             return false;
  158.         }
  159.  
  160.         return true;
  161.     }
  162.  
  163.     /**
  164.      * Writes current values for the user back to the database.
  165.      *
  166.      * @return bool true on success or false on failure
  167.      *
  168.      * @access private
  169.      */
  170.     function _updateUserData()
  171.     {
  172.         if (!array_key_exists('lastlogin'$this->tables['users']['fields'])) {
  173.             return true;
  174.         }
  175.  
  176.         $query  'UPDATE ' $this->prefix . $this->alias['users'].'
  177.                  SET '    $this->alias['lastlogin']
  178.                     .'='  $this->dbc->quote(date('Y-m-d H:i:s'$this->currentLogin)) '
  179.                  WHERE '  $this->alias['auth_user_id']
  180.                     .'='  $this->dbc->quote($this->propertyValues['auth_user_id']);
  181.  
  182.         $rows_affected $this->dbc->exec($query);
  183.  
  184.         if ($rows_affected === false{
  185.             $error_info $this->dbc->errorInfo();
  186.             $this->stack->push(
  187.                 LIVEUSER_ERROR'exception',
  188.                 array(
  189.                     'reason' => $error_info[2],
  190.                     'debug'  => $query
  191.                 )
  192.             );
  193.             return false;
  194.         }
  195.  
  196.         return true;
  197.     }
  198.  
  199.     /**
  200.      * Reads user data from the given data source
  201.      * If only $handle is given, it will read the data
  202.      * from the first user with that handle and return
  203.      * true on success.
  204.      * If $handle and $passwd are given, it will try to
  205.      * find the first user with both handle and password
  206.      * matching and return true on success (this allows
  207.      * multiple users having the same handle but different
  208.      * passwords - yep, some people want this).
  209.      * if only an auth_user_id is passed it will try to read the data based on the id
  210.      * If no match is found, false is being returned.
  211.      *
  212.      * @param  string user handle
  213.      * @param  string user password
  214.      * @param  bool|intif the user data should be read using the auth user id
  215.      * @return bool true on success or false on failure
  216.      *
  217.      * @access public
  218.      */
  219.     function readUserData($handle ''$passwd ''$auth_user_id = false)
  220.     {
  221.         $fields = array();
  222.         foreach ($this->tables['users']['fields'as $field => $req{
  223.             $fields[$this->alias[$field' AS ' $field;
  224.         }
  225.  
  226.         // Setting the default query.
  227.         $query 'SELECT ' implode(','$fields'
  228.                    FROM '   $this->prefix . $this->alias['users''
  229.                    WHERE  ';
  230.         if ($auth_user_id{
  231.             $query .= $this->alias['auth_user_id''='
  232.                 . $this->dbc->quote($auth_user_id);
  233.         else {
  234.             if (!is_array($this->handles|| empty($this->handles)) {
  235.                 $this->stack->push(
  236.                     LIVEUSER_ERROR_CONFIG'exception',
  237.                     array('reason' => 'No handle set in storage config.')
  238.                 );
  239.                 return false;
  240.             }
  241.             $handles = array();
  242.             foreach ($this->handles as $field{
  243.                 $handles[$this->alias[$field'=' .
  244.                     $this->dbc->quote($handle);
  245.             }
  246.             $query .= '(' implode(' OR '$handles')';
  247.  
  248.             if (!is_null($this->tables['users']['fields']['passwd'])) {
  249.                 // If $passwd is set, try to find the first user with the given
  250.                 // handle and password.
  251.                 $query .= ' AND   ' $this->alias['passwd''='
  252.                     . $this->dbc->quote($this->encryptPW($passwd));
  253.             }
  254.         }
  255.  
  256.         // Query database
  257.         $res $this->dbc->query($query);
  258.  
  259.         if ($res === false{
  260.             $error_info $this->dbc->errorInfo();
  261.             $this->stack->push(
  262.                 LIVEUSER_ERROR'exception',
  263.                 array(
  264.                     'reason' => $error_info[2],
  265.                     'debug'  => $query
  266.                 )
  267.             );
  268.             return false;
  269.         }
  270.  
  271.         $result $res->fetch(PDO::FETCH_ASSOC);
  272.  
  273.         if ($result === false && $this->dbc->errorCode(!= '00000'{
  274.             $this->stack->push(
  275.                 LIVEUSER_ERROR'exception',
  276.                 array(
  277.                     'reason' => $this->dbc->errorCode(),
  278.                     'debug'  => $this->dbc->errorInfo()
  279.                 )
  280.             );
  281.             return false;
  282.         }
  283.  
  284.         if (!is_array($result)) {
  285.             return null;
  286.         }
  287.  
  288.         // User was found, read data into class variables and set return value to true
  289.         if (array_key_exists('lastlogin'$result&& !empty($result['lastlogin'])) {
  290.             $result['lastlogin'strtotime($result['lastlogin']);
  291.         }
  292.         $this->propertyValues = $result;
  293.  
  294.         return true;
  295.     }
  296.  
  297.     /**
  298.      * properly disconnect from database
  299.      *
  300.      * @return bool true on success or false on failure
  301.      *
  302.      * @access public
  303.      */
  304.     function disconnect()
  305.     {
  306.         if ($this->dsn{
  307.             $this->dbc = null;
  308.         }
  309.         return true;
  310.     }
  311. }
  312. ?>

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