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.15 2004/03/30 17:39:04 quipo 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.15 $
  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);
  94.         elseif (get_parent_class($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.         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.  
  231.         $query sprintf("SELECT %s FROM %s WHERE %s = %s",
  232.                          $sql_from,
  233.                          $this->options['table'],
  234.                          $this->options['usernamecol'],
  235.                          $this->db->getTextValue($username)
  236.                          );
  237.  
  238.         $res $this->db->getRow($querynullnullnullMDB_FETCHMODE_ASSOC);
  239.  
  240.         if (MDB::isError($res|| PEAR::isError($res)) {
  241.             return PEAR::raiseError($res->getMessage()$res->getCode());
  242.         }
  243.         if (!is_array($res)) {
  244.             $this->activeUser = '';
  245.             return false;
  246.         }
  247.         if ($this->verifyPassword(trim($password"\r\n"),
  248.                                   trim($res[$this->options['passwordcol']]"\r\n"),
  249.                                   $this->options['cryptType'])) {
  250.             // Store additional field values in the session
  251.             foreach ($res as $key => $value{
  252.                 if ($key == $this->options['passwordcol'||
  253.                     $key == $this->options['usernamecol']{
  254.                     continue;
  255.                 }
  256.                 // Use reference to the auth object if exists
  257.                 // This is because the auth session variable can change so a static call to setAuthData does not make sence
  258.                 if (is_object($this->_auth_obj)) {
  259.                     $this->_auth_obj->setAuthData($key$value);
  260.                 else {
  261.                     Auth::setAuthData($key$value);
  262.                 }
  263.             }
  264.  
  265.             return true;
  266.         }
  267.  
  268.         $this->activeUser = $res[$this->options['usernamecol']];
  269.         return false;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ listUsers()
  274.  
  275.     /**
  276.      * @return array 
  277.      */
  278.     function listUsers()
  279.     {
  280.         $err $this->_prepare();
  281.         if ($err !== true{
  282.             return PEAR::raiseError($err->getMessage()$err->getCode());
  283.         }
  284.  
  285.         $retVal = array();
  286.  
  287.         // Find if db_fileds contains a *, i so assume all col are selected
  288.         if (strstr($this->options['db_fields']'*')) {
  289.             $sql_from '*';
  290.         else {
  291.             $sql_from $this->options['db_fields'];
  292.         }
  293.  
  294.         $query sprintf('SELECT %s FROM %s',
  295.                          $sql_from,
  296.                          $this->options['table']
  297.                          );
  298.  
  299.         $res $this->db->getAll($querynullnullnullMDB_FETCHMODE_ASSOC);
  300.  
  301.         if (MDB::isError($res)) {
  302.             return PEAR::raiseError($res->getMessage()$res->getCode());
  303.         else {
  304.             foreach ($res as $user{
  305.                 $user['username'$user[$this->options['usernamecol']];
  306.                 $retVal[$user;
  307.             }
  308.         }
  309.         return $retVal;
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ addUser()
  314.  
  315.     /**
  316.      * Add user to the storage container
  317.      *
  318.      * @access public
  319.      * @param  string Username
  320.      * @param  string Password
  321.      * @param  mixed  Additional information that are stored in the DB
  322.      *
  323.      * @return mixed True on success, otherwise error object
  324.      */
  325.     function addUser($username$password$additional "")
  326.     {
  327.  
  328.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  329.             $cryptFunction 'strval';
  330.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  331.             $cryptFunction $this->options['cryptType'];
  332.         else {
  333.             $cryptFunction 'md5';
  334.         }
  335.  
  336.         $additional_key   '';
  337.         $additional_value '';
  338.  
  339.         if (is_array($additional)) {
  340.             foreach ($additional as $key => $value{
  341.                 $additional_key   .= ', ' $key;
  342.                 $additional_value .= ', ' $this->db->getTextValue($value);
  343.             }
  344.         }
  345.  
  346.         $query sprintf("INSERT INTO %s (%s, %s%s) VALUES (%s, %s%s)",
  347.                          $this->options['table'],
  348.                          $this->options['usernamecol'],
  349.                          $this->options['passwordcol'],
  350.                          $additional_key,
  351.                          $this->db->getTextValue($username),
  352.                          $this->db->getTextValue($cryptFunction($password)),
  353.                          $additional_value
  354.                          );
  355.  
  356.         $res $this->query($query);
  357.  
  358.         if (MDB::isError($res)) {
  359.             return PEAR::raiseError($res->getMessage()$res->code);
  360.         }
  361.         return true;
  362.     }
  363.  
  364.     // }}}
  365.     // {{{ removeUser()
  366.  
  367.     /**
  368.      * Remove user from the storage container
  369.      *
  370.      * @access public
  371.      * @param  string Username
  372.      *
  373.      * @return mixed True on success, otherwise error object
  374.      */
  375.     function removeUser($username)
  376.     {
  377.         $query sprintf("DELETE FROM %s WHERE %s = %s",
  378.                          $this->options['table'],
  379.                          $this->options['usernamecol'],
  380.                          $this->db->getTextValue($username)
  381.                          );
  382.  
  383.         $res $this->query($query);
  384.  
  385.         if (MDB::isError($res)) {
  386.             return PEAR::raiseError($res->getMessage()$res->code);
  387.         }
  388.         return true;
  389.     }
  390.  
  391.     // }}}
  392.     // {{{ changePassword()
  393.  
  394.     /**
  395.      * Change password for user in the storage container
  396.      *
  397.     * @param string Username
  398.      * @param string The new password
  399.      */
  400.     function changePassword($username$password)
  401.     {
  402.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  403.             $cryptFunction 'strval';
  404.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  405.             $cryptFunction $this->options['cryptType'];
  406.         else {
  407.             $cryptFunction 'md5';
  408.         }
  409.  
  410.         $query sprintf("UPDATE %s SET %s = %s WHERE %s = %s",
  411.                          $this->options['table'],
  412.                          $this->options['passwordcol'],
  413.                          $this->db->getTextValue($password),
  414.                          $this->options['usernamecol'],
  415.                          $this->db->getTextValue($username)
  416.                          );
  417.  
  418.         $res $this->query($query);
  419.  
  420.         if (MDB::isError($res)) {
  421.             return PEAR::raiseError($res->getMessage()$res->code);
  422.         }
  423.         return true;
  424.     }
  425.  
  426.     // }}}
  427.  
  428. }
  429. ?>

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