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.12 2003/10/13 08:08:45 yavo 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.12 $
  34.  */
  35. {
  36.  
  37.     /**
  38.      * Additional options for the storage container
  39.      * @var array 
  40.      */
  41.     var $options = array();
  42.  
  43.     /**
  44.      * DB 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::DB
  62.      *
  63.      * @param  string Connection data or DB 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  string DSN string
  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.         else {
  111.             return true;
  112.         }
  113.     }
  114.  
  115.     // }}}
  116.     // {{{ _prepare()
  117.  
  118.     /**
  119.      * Prepare database connection
  120.      *
  121.      * This function checks if we have already opened a connection to
  122.      * the database. If that's not the case, a new connection is opened.
  123.      *
  124.      * @access private
  125.      * @return mixed True or a DB error object.
  126.      */
  127.     function _prepare()
  128.     {
  129.         return $this->_connect($this->options['dsn']);
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ query()
  134.  
  135.     /**
  136.      * Prepare query to the database
  137.      *
  138.      * This function checks if we have already opened a connection to
  139.      * the database. If that's not the case, a new connection is opened.
  140.      * After that the query is passed to the database.
  141.      *
  142.      * @access public
  143.      * @param  string Query string
  144.      * @return mixed  a MDB_result object or MDB_OK on success, a MDB
  145.      *                 or PEAR error on failure
  146.      */
  147.     function query($query)
  148.     {
  149.         $err $this->_prepare();
  150.         if ($err !== true{
  151.             return $err;
  152.         }
  153.         return $this->db->query($query);
  154.     }
  155.  
  156.     // }}}
  157.     // {{{ _setDefaults()
  158.  
  159.     /**
  160.      * Set some default options
  161.      *
  162.      * @access private
  163.      * @return void 
  164.      */
  165.     function _setDefaults()
  166.     {
  167.         $this->options['table']       'auth';
  168.         $this->options['usernamecol''username';
  169.         $this->options['passwordcol''password';
  170.         $this->options['dsn']         '';
  171.         $this->options['db_fields']   '';
  172.         $this->options['cryptType']   'md5';
  173.     }
  174.  
  175.     // }}}
  176.     // {{{ _parseOptions()
  177.  
  178.     /**
  179.      * Parse options passed to the container class
  180.      *
  181.      * @access private
  182.      * @param  array 
  183.      */
  184.     function _parseOptions($array)
  185.     {
  186.         foreach ($array as $key => $value{
  187.             if (isset($this->options[$key])) {
  188.                 $this->options[$key$value;
  189.             }
  190.         }
  191.  
  192.         // Include additional fields if they exist
  193.         if (!empty($this->options['db_fields'])) {
  194.             if (is_array($this->options['db_fields'])) {
  195.                 $this->options['db_fields'join($this->options['db_fields']', ');
  196.             }
  197.             $this->options['db_fields'', ' $this->options['db_fields'];
  198.         }
  199.  
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ fetchData()
  204.  
  205.     /**
  206.      * Get user information from database
  207.      *
  208.      * This function uses the given username to fetch
  209.      * the corresponding login data from the database
  210.      * table. If an account that matches the passed username
  211.      * and password is found, the function returns true.
  212.      * Otherwise it returns false.
  213.      *
  214.      * @param   string Username
  215.      * @param   string Password
  216.      * @return  mixed  Error object or boolean
  217.      */
  218.     function fetchData($username$password)
  219.     {
  220.         // Prepare for a database query
  221.         $err $this->_prepare();
  222.         if ($err !== true{
  223.             return PEAR::raiseError($err->getMessage()$err->getCode());
  224.         }
  225.  
  226.         // Find if db_fileds contains a *, i so assume all col are selected
  227.         if (strstr($this->options['db_fields']'*')) {
  228.             $sql_from '*';
  229.         else{
  230.             $sql_from $this->options['usernamecol'', '$this->options['passwordcol'$this->options['db_fields'];
  231.         }
  232.  
  233.         $query sprintf("SELECT %s FROM %s WHERE %s = %s",
  234.                          $sql_from,
  235.                          $this->options['table'],
  236.                          $this->options['usernamecol'],
  237.                          $this->db->getTextValue($username)
  238.                          );
  239.  
  240.         $res $this->db->getRow($querynullnullnullMDB_FETCHMODE_ASSOC);
  241.  
  242.         if (MDB::isError($res|| PEAR::isError($res)) {
  243.             return PEAR::raiseError($res->getMessage()$res->getCode());
  244.         }
  245.         if (!is_array($res)) {
  246.             $this->activeUser = '';
  247.             return false;
  248.         }
  249.         if ($this->verifyPassword(trim($password"\r\n"),
  250.                                   trim($res[$this->options['passwordcol']]"\r\n"),
  251.                                   $this->options['cryptType'])) {
  252.             // Store additional field values in the session
  253.             foreach ($res as $key => $value{
  254.                 if ($key == $this->options['passwordcol'||
  255.                     $key == $this->options['usernamecol']{
  256.                     continue;
  257.                 }
  258.                 // Use reference to the auth object if exists
  259.                 // This is because the auth session variable can change so a static call to setAuthData does not make sence
  260.                 if(is_object($this->_auth_obj)){
  261.                     $this->_auth_obj->setAuthData($key$value);
  262.                 else {
  263.                     Auth::setAuthData($key$value);
  264.                 }
  265.             }
  266.  
  267.             return true;
  268.         }
  269.  
  270.         $this->activeUser = $res[$this->options['usernamecol']];
  271.         return false;
  272.     }
  273.  
  274.     // }}}
  275.     // {{{ listUsers()
  276.  
  277.     function listUsers()
  278.     {
  279.         $err $this->_prepare();
  280.         if ($err !== true{
  281.             return PEAR::raiseError($err->getMessage()$err->getCode());
  282.         }
  283.  
  284.         $retVal = array();
  285.  
  286.         // Find if db_fileds contains a *, i so assume all col are selected
  287.         if (strstr($this->options['db_fields']'*')) {
  288.             $sql_from '*';
  289.         else{
  290.             $sql_from $this->options['db_fields'];
  291.         }
  292.  
  293.         $query sprintf('SELECT %s FROM %s',
  294.                          $sql_from,
  295.                          $this->options['table']
  296.                          );
  297.  
  298.         $res $this->db->getAll($querynullnullnullMDB_FETCHMODE_ASSOC);
  299.  
  300.         if (MDB::isError($res)) {
  301.             return PEAR::raiseError($res->getMessage()$res->getCode());
  302.         else {
  303.             foreach ($res as $user{
  304.                 $user['username'$user[$this->options['usernamecol']];
  305.                 $retVal[$user;
  306.             }
  307.         }
  308.         return $retVal;
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ addUser()
  313.  
  314.     /**
  315.      * Add user to the storage container
  316.      *
  317.      * @access public
  318.      * @param  string Username
  319.      * @param  string Password
  320.      * @param  mixed  Additional information that are stored in the DB
  321.      *
  322.      * @return mixed True on success, otherwise error object
  323.      */
  324.     function addUser($username$password$additional "")
  325.     {
  326.         if (function_exists($this->options['cryptType'])) {
  327.             $cryptFunction $this->options['cryptType'];
  328.         else {
  329.             $cryptFunction 'md5';
  330.         }
  331.  
  332.         $additional_key   '';
  333.         $additional_value '';
  334.  
  335.         if (is_array($additional)) {
  336.             foreach ($additional as $key => $value{
  337.                 $additional_key   .= ', ' $key;
  338.                 $additional_value .= ', ' $this->db->getTextValue($value);
  339.             }
  340.         }
  341.  
  342.         $query sprintf("INSERT INTO %s (%s, %s%s) VALUES (%s, %s%s)",
  343.                          $this->options['table'],
  344.                          $this->options['usernamecol'],
  345.                          $this->options['passwordcol'],
  346.                          $additional_key,
  347.                          $this->db->getTextValue($username),
  348.                          $this->db->getTextValue($cryptFunction($password)),
  349.                          $additional_value
  350.                          );
  351.  
  352.         $res $this->query($query);
  353.  
  354.         if (MDB::isError($res)) {
  355.             return PEAR::raiseError($res->getMessage()$res->code);
  356.         else {
  357.             return true;
  358.         }
  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->getTextValue($username)
  378.                          );
  379.  
  380.         $res $this->query($query);
  381.  
  382.         if (MDB::isError($res)) {
  383.            return PEAR::raiseError($res->getMessage()$res->code);
  384.         else {
  385.           return true;
  386.         }
  387.     }
  388.  
  389.     // }}}
  390. }
  391. ?>

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