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

Source for file MDB.php

Documentation is available at MDB.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: MDB.php,v 1.20 2006/01/08 11:54:11 lsmith Exp $
  20. //
  21.  
  22. require_once 'Auth/Container.php';
  23. require_once 'MDB.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 MDB abstraction layer to fetch login data.
  30.  *
  31.  * @author   Lorenzo Alberton <l.alberton@quipo.it>
  32.  * @package  Auth
  33.  * @version  $Revision: 1.20 $
  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::MDB
  62.      *
  63.      * @param  string Connection data or MDB object
  64.      * @return object Returns an error object if something went wrong
  65.      */
  66.     function Auth_Container_MDB($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 =MDB::connect($dsn$this->options['db_options']);
  94.         elseif (is_subclass_of($dsn'mdb_common')) {
  95.             $this->db = $dsn;
  96.         elseif (is_object($dsn&& MDB::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 (MDB::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.         if (is_subclass_of($this->db'mdb_common')) {
  129.             return true;
  130.         }
  131.         return $this->_connect($this->options['dsn']);
  132.     }
  133.  
  134.     // }}}
  135.     // {{{ query()
  136.  
  137.     /**
  138.      * Prepare query to the database
  139.      *
  140.      * This function checks if we have already opened a connection to
  141.      * the database. If that's not the case, a new connection is opened.
  142.      * After that the query is passed to the database.
  143.      *
  144.      * @access public
  145.      * @param  string Query string
  146.      * @return mixed  a MDB_result object or MDB_OK on success, a MDB
  147.      *                 or PEAR error on failure
  148.      */
  149.     function query($query)
  150.     {
  151.         $err $this->_prepare();
  152.         if ($err !== true{
  153.             return $err;
  154.         }
  155.         return $this->db->query($query);
  156.     }
  157.  
  158.     // }}}
  159.     // {{{ _setDefaults()
  160.  
  161.     /**
  162.      * Set some default options
  163.      *
  164.      * @access private
  165.      * @return void 
  166.      */
  167.     function _setDefaults()
  168.     {
  169.         $this->options['table']       'auth';
  170.         $this->options['usernamecol''username';
  171.         $this->options['passwordcol''password';
  172.         $this->options['dsn']         '';
  173.         $this->options['db_fields']   '';
  174.         $this->options['cryptType']   'md5';
  175.         $this->options['db_options']  = array();
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ _parseOptions()
  180.  
  181.     /**
  182.      * Parse options passed to the container class
  183.      *
  184.      * @access private
  185.      * @param  array 
  186.      */
  187.     function _parseOptions($array)
  188.     {
  189.         foreach ($array as $key => $value{
  190.             if (isset($this->options[$key])) {
  191.                 $this->options[$key$value;
  192.             }
  193.         }
  194.  
  195.         // Include additional fields if they exist
  196.         if (!empty($this->options['db_fields'])) {
  197.             if (is_array($this->options['db_fields'])) {
  198.                 $this->options['db_fields'join($this->options['db_fields']', ');
  199.             }
  200.             $this->options['db_fields'', ' $this->options['db_fields'];
  201.         }
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ fetchData()
  206.  
  207.     /**
  208.      * Get user information from database
  209.      *
  210.      * This function uses the given username to fetch
  211.      * the corresponding login data from the database
  212.      * table. If an account that matches the passed username
  213.      * and password is found, the function returns true.
  214.      * Otherwise it returns false.
  215.      *
  216.      * @param   string Username
  217.      * @param   string Password
  218.      * @param   boolean If true password is secured using a md5 hash
  219.      *                   the frontend and auth are responsible for making sure the container supports
  220.      *                   challenge response password authentication
  221.      * @return  mixed  Error object or boolean
  222.      */
  223.     function fetchData($username$password$isChallengeResponse=false)
  224.     {
  225.         // Prepare for a database query
  226.         $err $this->_prepare();
  227.         if ($err !== true{
  228.             return PEAR::raiseError($err->getMessage()$err->getCode());
  229.         }
  230.  
  231.         //Check if db_fields contains a *, if so assume all columns are selected
  232.         if (strstr($this->options['db_fields']'*')) {
  233.             $sql_from '*';
  234.         else {
  235.             $sql_from $this->options['usernamecol'', '$this->options['passwordcol'$this->options['db_fields'];
  236.         }
  237.  
  238.         $query sprintf("SELECT %s FROM %s WHERE %s = %s",
  239.                          $sql_from,
  240.                          $this->options['table'],
  241.                          $this->options['usernamecol'],
  242.                          $this->db->getTextValue($username)
  243.                          );
  244.  
  245.         $res $this->db->getRow($querynullnullnullMDB_FETCHMODE_ASSOC);
  246.  
  247.         if (MDB::isError($res|| PEAR::isError($res)) {
  248.             return PEAR::raiseError($res->getMessage()$res->getCode());
  249.         }
  250.         if (!is_array($res)) {
  251.             $this->activeUser = '';
  252.             return false;
  253.         }
  254.  
  255.         // Perform trimming here before the hashing
  256.         $password trim($password"\r\n");
  257.         $res[$this->options['passwordcol']] trim($res[$this->options['passwordcol']]"\r\n");
  258.         // If using Challenge Response md5 the pass with the secret
  259.         if ($isChallengeResponse{
  260.             $res[$this->options['passwordcol']] =
  261.                 md5($res[$this->options['passwordcol']].$this->_auth_obj->session['loginchallenege']);
  262.             // UGLY cannot avoid without modifying verifyPassword
  263.             if ($this->options['cryptType'== 'md5'{
  264.                 $res[$this->options['passwordcol']] md5($res[$this->options['passwordcol']]);
  265.             }
  266.         }
  267.         if ($this->verifyPassword($password,
  268.                                   $res[$this->options['passwordcol']],
  269.                                   $this->options['cryptType'])) {
  270.             // Store additional field values in the session
  271.             foreach ($res as $key => $value{
  272.                 if ($key == $this->options['passwordcol'||
  273.                     $key == $this->options['usernamecol']{
  274.                     continue;
  275.                 }
  276.                 // Use reference to the auth object if exists
  277.                 // This is because the auth session variable can change so a static call to setAuthData does not make sense
  278.                 $this->_auth_obj->setAuthData($key$value);
  279.             }
  280.             return true;
  281.         }
  282.  
  283.         $this->activeUser = $res[$this->options['usernamecol']];
  284.         return false;
  285.     }
  286.  
  287.     // }}}
  288.     // {{{ listUsers()
  289.  
  290.     /**
  291.      * Returns a list of users from the container
  292.      *
  293.      * @return mixed array|PEAR_Error
  294.      * @access public
  295.      */
  296.     function listUsers()
  297.     {
  298.         $err $this->_prepare();
  299.         if ($err !== true{
  300.             return PEAR::raiseError($err->getMessage()$err->getCode());
  301.         }
  302.  
  303.         $retVal = array();
  304.  
  305.         //Check if db_fields contains a *, if so assume all columns are selected
  306.         if (strstr($this->options['db_fields']'*')) {
  307.             $sql_from '*';
  308.         else {
  309.             $sql_from $this->options['db_fields'];
  310.         }
  311.  
  312.         $query sprintf('SELECT %s FROM %s',
  313.                          $sql_from,
  314.                          $this->options['table']
  315.                          );
  316.  
  317.         $res $this->db->getAll($querynullnullnullMDB_FETCHMODE_ASSOC);
  318.  
  319.         if (MDB::isError($res)) {
  320.             return PEAR::raiseError($res->getMessage()$res->getCode());
  321.         else {
  322.             foreach ($res as $user{
  323.                 $user['username'$user[$this->options['usernamecol']];
  324.                 $retVal[$user;
  325.             }
  326.         }
  327.         return $retVal;
  328.     }
  329.  
  330.     // }}}
  331.     // {{{ addUser()
  332.  
  333.     /**
  334.      * Add user to the storage container
  335.      *
  336.      * @access public
  337.      * @param  string Username
  338.      * @param  string Password
  339.      * @param  mixed  Additional information that are stored in the DB
  340.      *
  341.      * @return mixed True on success, otherwise error object
  342.      */
  343.     function addUser($username$password$additional "")
  344.     {
  345.  
  346.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  347.             $cryptFunction 'strval';
  348.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  349.             $cryptFunction $this->options['cryptType'];
  350.         else {
  351.             $cryptFunction 'md5';
  352.         }
  353.  
  354.         $password $cryptFunction($password);
  355.  
  356.         $additional_key   '';
  357.         $additional_value '';
  358.  
  359.         if (is_array($additional)) {
  360.             foreach ($additional as $key => $value{
  361.                 $additional_key   .= ', ' $key;
  362.                 $additional_value .= ', ' $this->db->getTextValue($value);
  363.             }
  364.         }
  365.  
  366.         $query sprintf("INSERT INTO %s (%s, %s%s) VALUES (%s, %s%s)",
  367.                          $this->options['table'],
  368.                          $this->options['usernamecol'],
  369.                          $this->options['passwordcol'],
  370.                          $additional_key,
  371.                          $this->db->getTextValue($username),
  372.                          $this->db->getTextValue($password),
  373.                          $additional_value
  374.                          );
  375.  
  376.         $res $this->query($query);
  377.  
  378.         if (MDB::isError($res)) {
  379.             return PEAR::raiseError($res->getMessage()$res->code);
  380.         }
  381.         return true;
  382.     }
  383.  
  384.     // }}}
  385.     // {{{ removeUser()
  386.  
  387.     /**
  388.      * Remove user from the storage container
  389.      *
  390.      * @access public
  391.      * @param  string Username
  392.      *
  393.      * @return mixed True on success, otherwise error object
  394.      */
  395.     function removeUser($username)
  396.     {
  397.         $query sprintf("DELETE FROM %s WHERE %s = %s",
  398.                          $this->options['table'],
  399.                          $this->options['usernamecol'],
  400.                          $this->db->getTextValue($username)
  401.                          );
  402.  
  403.         $res $this->query($query);
  404.  
  405.         if (MDB::isError($res)) {
  406.             return PEAR::raiseError($res->getMessage()$res->code);
  407.         }
  408.         return true;
  409.     }
  410.  
  411.     // }}}
  412.     // {{{ changePassword()
  413.  
  414.     /**
  415.      * Change password for user in the storage container
  416.      *
  417.      * @param string Username
  418.      * @param string The new password (plain text)
  419.      */
  420.     function changePassword($username$password)
  421.     {
  422.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  423.             $cryptFunction 'strval';
  424.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  425.             $cryptFunction $this->options['cryptType'];
  426.         else {
  427.             $cryptFunction 'md5';
  428.         }
  429.  
  430.         $password $cryptFunction($password);
  431.  
  432.         $query sprintf("UPDATE %s SET %s = %s WHERE %s = %s",
  433.                          $this->options['table'],
  434.                          $this->options['passwordcol'],
  435.                          $this->db->getTextValue($password),
  436.                          $this->options['usernamecol'],
  437.                          $this->db->getTextValue($username)
  438.                          );
  439.  
  440.         $res $this->query($query);
  441.  
  442.         if (MDB::isError($res)) {
  443.             return PEAR::raiseError($res->getMessage()$res->code);
  444.         }
  445.         return true;
  446.     }
  447.  
  448.     // }}}
  449.     // {{{ supportsChallengeResponse()
  450.  
  451.     /**
  452.      * Determine if this container supports
  453.      * password authentication with challenge response
  454.      *
  455.      * @return bool 
  456.      * @access public
  457.      */
  458.     function supportsChallengeResponse()
  459.     {
  460.         return in_array($this->options['cryptType']array('md5''none'''));
  461.     }
  462.  
  463.     // }}}
  464.     // {{{ getCryptType()
  465.  
  466.     /**
  467.      * Returns the selected crypt type for this container
  468.      *
  469.      * @return string Function used to crypt the password
  470.      */
  471.     function getCryptType()
  472.     {
  473.         return $this->options['cryptType'];
  474.     }
  475.  
  476.     // }}}
  477.  
  478. }
  479. ?>

Documentation generated on Mon, 11 Mar 2019 14:36:38 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.