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

Source for file MDB2.php

Documentation is available at MDB2.php

  1. <?php
  2. //
  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. // | Author: Lorenzo Alberton <l.alberton@quipo.it>                       |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: MDB2.php,v 1.2 2004/04/26 19:50:31 toby Exp $
  20. //
  21.  
  22. require_once 'Auth/Container.php';
  23. require_once 'MDB2.php';
  24.  
  25. /**
  26.  * Storage driver for fetching login data from a database
  27.  *
  28.  * This storage driver can use all databases which are supported
  29.  * by the PEAR MDB2 abstraction layer to fetch login data.
  30.  *
  31.  * @author   Lorenzo Alberton <l.alberton@quipo.it>
  32.  * @package  Auth
  33.  * @version  $Revision: 1.2 $
  34.  */
  35. {
  36.  
  37.     /**
  38.      * Additional options for the storage container
  39.      * @var array 
  40.      */
  41.     var $options = array();
  42.  
  43.     /**
  44.      * MDB object
  45.      * @var object 
  46.      */
  47.     var $db = null;
  48.     var $dsn = '';
  49.  
  50.     /**
  51.      * User that is currently selected from the DB.
  52.      * @var string 
  53.      */
  54.     var $activeUser = '';
  55.  
  56.     // {{{ Constructor
  57.  
  58.     /**
  59.      * Constructor of the container class
  60.      *
  61.      * Initate connection to the database via PEAR::MDB2
  62.      *
  63.      * @param  string Connection data or MDB2 object
  64.      * @return object Returns an error object if something went wrong
  65.      */
  66.     function Auth_Container_MDB2($dsn)
  67.     {
  68.         $this->_setDefaults();
  69.  
  70.         if (is_array($dsn)) {
  71.             $this->_parseOptions($dsn);
  72.             if (empty($this->options['dsn'])) {
  73.                 PEAR::raiseError('No connection parameters specified!');
  74.             }
  75.         else {
  76.             $this->options['dsn'$dsn;
  77.         }
  78.     }
  79.  
  80.     // }}}
  81.     // {{{ _connect()
  82.  
  83.     /**
  84.      * Connect to database by using the given DSN string
  85.      *
  86.      * @access private
  87.      * @param  mixed DSN string | array | mdb object
  88.      * @return mixed  Object on error, otherwise bool
  89.      */
  90.     function _connect($dsn)
  91.     {
  92.         if (is_string($dsn|| is_array($dsn)) {
  93.             $this->db =MDB2::connect($dsn);
  94.         elseif (is_a($dsn'MDB2_Driver_Common')) {
  95.             $this->db = $dsn;
  96.         elseif (is_object($dsn&& MDB2::isError($dsn)) {
  97.             return PEAR::raiseError($dsn->getMessage()$dsn->code);
  98.         else {
  99.             return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
  100.                                     41,
  101.                                     PEAR_ERROR_RETURN,
  102.                                     null,
  103.                                     null
  104.                                     );
  105.  
  106.         }
  107.  
  108.         if (MDB2::isError($this->db|| PEAR::isError($this->db)) {
  109.             return PEAR::raiseError($this->db->getMessage()$this->db->code);
  110.         }
  111.         return true;
  112.     }
  113.  
  114.     // }}}
  115.     // {{{ _prepare()
  116.  
  117.     /**
  118.      * Prepare database connection
  119.      *
  120.      * This function checks if we have already opened a connection to
  121.      * the database. If that's not the case, a new connection is opened.
  122.      *
  123.      * @access private
  124.      * @return mixed True or a MDB error object.
  125.      */
  126.     function _prepare()
  127.     {
  128.         return $this->_connect($this->options['dsn']);
  129.     }
  130.  
  131.     // }}}
  132.     // {{{ query()
  133.  
  134.     /**
  135.      * Prepare query to the database
  136.      *
  137.      * This function checks if we have already opened a connection to
  138.      * the database. If that's not the case, a new connection is opened.
  139.      * After that the query is passed to the database.
  140.      *
  141.      * @access public
  142.      * @param  string Query string
  143.      * @return mixed  a MDB_result object or MDB_OK on success, a MDB
  144.      *                 or PEAR error on failure
  145.      */
  146.     function query($query)
  147.     {
  148.         $err $this->_prepare();
  149.         if ($err !== true{
  150.             return $err;
  151.         }
  152.         return $this->db->query($query);
  153.     }
  154.  
  155.     // }}}
  156.     // {{{ _setDefaults()
  157.  
  158.     /**
  159.      * Set some default options
  160.      *
  161.      * @access private
  162.      * @return void 
  163.      */
  164.     function _setDefaults()
  165.     {
  166.         $this->options['table']       'auth';
  167.         $this->options['usernamecol''username';
  168.         $this->options['passwordcol''password';
  169.         $this->options['dsn']         '';
  170.         $this->options['db_fields']   '';
  171.         $this->options['cryptType']   'md5';
  172.     }
  173.  
  174.     // }}}
  175.     // {{{ _parseOptions()
  176.  
  177.     /**
  178.      * Parse options passed to the container class
  179.      *
  180.      * @access private
  181.      * @param  array 
  182.      */
  183.     function _parseOptions($array)
  184.     {
  185.         foreach ($array as $key => $value{
  186.             if (isset($this->options[$key])) {
  187.                 $this->options[$key$value;
  188.             }
  189.         }
  190.  
  191.         // Include additional fields if they exist
  192.         if (!empty($this->options['db_fields'])) {
  193.             if (is_array($this->options['db_fields'])) {
  194.                 $this->options['db_fields'join($this->options['db_fields']', ');
  195.             }
  196.             $this->options['db_fields'', ' $this->options['db_fields'];
  197.         }
  198.     }
  199.  
  200.     // }}}
  201.     // {{{ fetchData()
  202.  
  203.     /**
  204.      * Get user information from database
  205.      *
  206.      * This function uses the given username to fetch
  207.      * the corresponding login data from the database
  208.      * table. If an account that matches the passed username
  209.      * and password is found, the function returns true.
  210.      * Otherwise it returns false.
  211.      *
  212.      * @param   string Username
  213.      * @param   string Password
  214.      * @return  mixed  Error object or boolean
  215.      */
  216.     function fetchData($username$password)
  217.     {
  218.         // Prepare for a database query
  219.         $err $this->_prepare();
  220.         if ($err !== true{
  221.             return PEAR::raiseError($err->getMessage()$err->getCode());
  222.         }
  223.  
  224.         // Find if db_fileds contains a *, i so assume all col are selected
  225.         if (strstr($this->options['db_fields']'*')) {
  226.             $sql_from '*';
  227.         else {
  228.             $sql_from $this->options['usernamecol'', '$this->options['passwordcol'$this->options['db_fields'];
  229.         }
  230.         $query sprintf("SELECT %s FROM %s WHERE %s = %s",
  231.                          $sql_from,
  232.                          $this->options['table'],
  233.                          $this->options['usernamecol'],
  234.                          $this->db->quote($username'text')
  235.                          );
  236.  
  237.         $res $this->db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  238.         if (MDB2::isError($res|| PEAR::isError($res)) {
  239.             return PEAR::raiseError($res->getMessage()$res->getCode());
  240.         }
  241.         if (!is_array($res)) {
  242.             $this->activeUser = '';
  243.             return false;
  244.         }
  245.         if ($this->verifyPassword(trim($password"\r\n"),
  246.                                   trim($res[$this->options['passwordcol']]"\r\n"),
  247.                                   $this->options['cryptType'])) {
  248.             // Store additional field values in the session
  249.             foreach ($res as $key => $value{
  250.                 if ($key == $this->options['passwordcol'||
  251.                     $key == $this->options['usernamecol']{
  252.                     continue;
  253.                 }
  254.                 // Use reference to the auth object if exists
  255.                 // This is because the auth session variable can change so a static call to setAuthData does not make sense
  256.                 if (isset($this->_auth_obj&& is_object($this->_auth_obj)) {
  257.                     $this->_auth_obj->setAuthData($key$value);
  258.                 else {
  259.                     Auth::setAuthData($key$value);
  260.                 }
  261.             }
  262.  
  263.             return true;
  264.         }
  265.  
  266.         $this->activeUser = $res[$this->options['usernamecol']];
  267.         return false;
  268.     }
  269.  
  270.     // }}}
  271.     // {{{ listUsers()
  272.  
  273.     /**
  274.      * @return array 
  275.      */
  276.     function listUsers()
  277.     {
  278.         $err $this->_prepare();
  279.         if ($err !== true{
  280.             return PEAR::raiseError($err->getMessage()$err->getCode());
  281.         }
  282.  
  283.         $retVal = array();
  284.  
  285.         // Find if db_fileds contains a *, i so assume all col are selected
  286.         if (strstr($this->options['db_fields']'*')) {
  287.             $sql_from '*';
  288.         else {
  289.             $sql_from $this->options['db_fields'];
  290.         }
  291.  
  292.         $query sprintf('SELECT %s FROM %s',
  293.                          $sql_from,
  294.                          $this->options['table']
  295.                          );
  296.  
  297.         $res $this->db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  298.         if (MDB2::isError($res)) {
  299.             return PEAR::raiseError($res->getMessage()$res->getCode());
  300.         else {
  301.             foreach ($res as $user{
  302.                 $user['username'$user[$this->options['usernamecol']];
  303.                 $retVal[$user;
  304.             }
  305.         }
  306.         return $retVal;
  307.     }
  308.  
  309.     // }}}
  310.     // {{{ addUser()
  311.  
  312.     /**
  313.      * Add user to the storage container
  314.      *
  315.      * @access public
  316.      * @param  string Username
  317.      * @param  string Password
  318.      * @param  mixed  Additional information that are stored in the DB
  319.      *
  320.      * @return mixed True on success, otherwise error object
  321.      */
  322.     function addUser($username$password$additional "")
  323.     {
  324.  
  325.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  326.             $cryptFunction 'strval';
  327.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  328.             $cryptFunction $this->options['cryptType'];
  329.         else {
  330.             $cryptFunction 'md5';
  331.         }
  332.  
  333.         $additional_key   '';
  334.         $additional_value '';
  335.  
  336.         if (is_array($additional)) {
  337.             foreach ($additional as $key => $value{
  338.                 $additional_key   .= ', ' $key;
  339.                 $additional_value .= ', ' $this->db->quote($value'text');
  340.             }
  341.         }
  342.  
  343.         $query sprintf("INSERT INTO %s (%s, %s%s) VALUES (%s, %s%s)",
  344.                          $this->options['table'],
  345.                          $this->options['usernamecol'],
  346.                          $this->options['passwordcol'],
  347.                          $additional_key,
  348.                          $this->db->quote($username'text'),
  349.                          $this->db->quote($cryptFunction($password)'text'),
  350.                          $additional_value
  351.                          );
  352.  
  353.         $res $this->query($query);
  354.  
  355.         if (MDB2::isError($res)) {
  356.             return PEAR::raiseError($res->getMessage()$res->code);
  357.         }
  358.         return true;
  359.     }
  360.  
  361.     // }}}
  362.     // {{{ removeUser()
  363.  
  364.     /**
  365.      * Remove user from the storage container
  366.      *
  367.      * @access public
  368.      * @param  string Username
  369.      *
  370.      * @return mixed True on success, otherwise error object
  371.      */
  372.     function removeUser($username)
  373.     {
  374.         $query sprintf("DELETE FROM %s WHERE %s = %s",
  375.                          $this->options['table'],
  376.                          $this->options['usernamecol'],
  377.                          $this->db->quote($username'text')
  378.                          );
  379.  
  380.         $res $this->query($query);
  381.  
  382.         if (MDB2::isError($res)) {
  383.             return PEAR::raiseError($res->getMessage()$res->code);
  384.         }
  385.         return true;
  386.     }
  387.  
  388.     // }}}
  389.     // {{{ changePassword()
  390.  
  391.     /**
  392.      * Change password for user in the storage container
  393.      *
  394.     * @param string Username
  395.      * @param string The new password
  396.      */
  397.     function changePassword($username$password)
  398.     {
  399.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  400.             $cryptFunction 'strval';
  401.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  402.             $cryptFunction $this->options['cryptType'];
  403.         else {
  404.             $cryptFunction 'md5';
  405.         }
  406.  
  407.         $query sprintf("UPDATE %s SET %s = %s WHERE %s = %s",
  408.                          $this->options['table'],
  409.                          $this->options['passwordcol'],
  410.                          $this->db->quote($password'text'),
  411.                          $this->options['usernamecol'],
  412.                          $this->db->quote($username'text')
  413.                          );
  414.  
  415.         $res $this->query($query);
  416.  
  417.         if (MDB2::isError($res)) {
  418.             return PEAR::raiseError($res->getMessage()$res->code);
  419.         }
  420.         return true;
  421.     }
  422.  
  423.     // }}}
  424.  
  425. }
  426. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.