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

Source for file DB.php

Documentation is available at DB.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. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: DB.php,v 1.45 2004/07/04 16:52:20 yavo Exp $
  20. //
  21.  
  22. require_once 'Auth/Container.php';
  23. require_once 'DB.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 DB abstraction layer to fetch login data.
  30.  *
  31.  * @author   Martin Jansen <mj@php.net>
  32.  * @package  Auth
  33.  * @version  $Revision: 1.45 $
  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_DB($dsn)
  67.     {
  68.         $this->_setDefaults();
  69.  
  70.         if (is_array($dsn)) {
  71.             $this->_parseOptions($dsn);
  72.  
  73.             if (empty($this->options['dsn'])) {
  74.                 PEAR::raiseError('No connection parameters specified!');
  75.             }
  76.         else {
  77.             $this->options['dsn'$dsn;
  78.         }
  79.     }
  80.  
  81.     // }}}
  82.     // {{{ _connect()
  83.  
  84.     /**
  85.      * Connect to database by using the given DSN string
  86.      *
  87.      * @access private
  88.      * @param  string DSN string
  89.      * @return mixed  Object on error, otherwise bool
  90.      */
  91.     function _connect($dsn)
  92.     {
  93.         if (is_string($dsn|| is_array($dsn)) {
  94.             $this->db = DB::Connect($dsn$this->options['db_options']);
  95.         elseif (get_parent_class($dsn== "db_common"{
  96.             $this->db = $dsn;
  97.         elseif (DB::isError($dsn)) {
  98.             return PEAR::raiseError($dsn->getMessage()$dsn->getCode());
  99.         else {
  100.             return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
  101.                                     41,
  102.                                     PEAR_ERROR_RETURN,
  103.                                     null,
  104.                                     null
  105.                                     );
  106.         }
  107.  
  108.         if (DB::isError($this->db|| PEAR::isError($this->db)) {
  109.             return PEAR::raiseError($this->db->getMessage()$this->db->getCode());
  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.         if (!DB::isConnection($this->db)) {
  130.             $res $this->_connect($this->options['dsn']);
  131.             if (DB::isError($res|| PEAR::isError($res)) {
  132.                 return $res;
  133.             }
  134.         }
  135.         return true;
  136.     }
  137.  
  138.     // }}}
  139.     // {{{ query()
  140.  
  141.     /**
  142.      * Prepare query to the database
  143.      *
  144.      * This function checks if we have already opened a connection to
  145.      * the database. If that's not the case, a new connection is opened.
  146.      * After that the query is passed to the database.
  147.      *
  148.      * @access public
  149.      * @param  string Query string
  150.      * @return mixed  a DB_result object or DB_OK on success, a DB
  151.      *                 or PEAR error on failure
  152.      */
  153.     function query($query)
  154.     {
  155.         $err $this->_prepare();
  156.         if ($err !== true{
  157.             return $err;
  158.         }
  159.         return $this->db->query($query);
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ _setDefaults()
  164.  
  165.     /**
  166.      * Set some default options
  167.      *
  168.      * @access private
  169.      * @return void 
  170.      */
  171.     function _setDefaults()
  172.     {
  173.         $this->options['table']       'auth';
  174.         $this->options['usernamecol''username';
  175.         $this->options['passwordcol''password';
  176.         $this->options['dsn']         '';
  177.         $this->options['db_fields']   '';
  178.         $this->options['cryptType']   'md5';
  179.         $this->options['db_options']  = array();
  180.     }
  181.  
  182.     // }}}
  183.     // {{{ _parseOptions()
  184.  
  185.     /**
  186.      * Parse options passed to the container class
  187.      *
  188.      * @access private
  189.      * @param  array 
  190.      */
  191.     function _parseOptions($array)
  192.     {
  193.         foreach ($array as $key => $value{
  194.             if (isset($this->options[$key])) {
  195.                 $this->options[$key$value;
  196.             }
  197.         }
  198.  
  199.         /* Include additional fields if they exist */
  200.         if (!empty($this->options['db_fields'])) {
  201.             if (is_array($this->options['db_fields'])) {
  202.                 $this->options['db_fields'join($this->options['db_fields']', ');
  203.             }
  204.             $this->options['db_fields'', '.$this->options['db_fields'];
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * Get user information from database
  210.      *
  211.      * This function uses the given username to fetch
  212.      * the corresponding login data from the database
  213.      * table. If an account that matches the passed username
  214.      * and password is found, the function returns true.
  215.      * Otherwise it returns false.
  216.      *
  217.      * @param   string Username
  218.      * @param   string Password
  219.      * @param   boolean If true password is secured using an md5 hash
  220.      *                   the frontend and auth are responsible for making sure the container supports
  221.      *                   challenge responce password authenthication
  222.      * @return  mixed  Error object or boolean
  223.      */
  224.     function fetchData($username$password$isChallengeResponce=false{
  225.         //print "Container_DB::fetchData($username, $password, $isChallengeResponce) <br/>\n";
  226.         // Prepare for a database query
  227.         $err $this->_prepare();
  228.         if ($err !== true{
  229.             return PEAR::raiseError($err->getMessage()$err->getCode());
  230.         }
  231.  
  232.         // Find if db_fields contains a *, if so assume all col are selected
  233.         if (strstr($this->options['db_fields']'*')) {
  234.             $sql_from "*";
  235.         }
  236.         else{
  237.             $sql_from $this->options['usernamecol'", ".$this->options['passwordcol'].$this->options['db_fields'];
  238.         }
  239.         /**
  240.          Old Style, removed to go around the oci8 
  241.          problem 
  242.          See bug 206
  243.          http://pear.php.net/bugs/bug.php?id=206
  244.          
  245.         $query = "SELECT ! FROM ! WHERE ! = ?";
  246.         $query_params = array(
  247.                          $sql_from,
  248.                          $this->options['table'],
  249.                          $this->options['usernamecol'],
  250.                          $username
  251.                          );
  252.         */
  253.         
  254.         $query "SELECT ".$sql_from.
  255.                 " FROM ".$this->options['table'].
  256.                 " WHERE ".$this->options['usernamecol']." = '".$this->db->quoteString($username)."'";
  257.         
  258.         $res $this->db->getRow($querynullDB_FETCHMODE_ASSOC);
  259.         #print "SQL: $query <br/>\n";
  260.         #print_r($res);
  261.  
  262.         if (DB::isError($res)) {
  263.             return PEAR::raiseError($res->getMessage()$res->getCode());
  264.         }
  265.         if (!is_array($res)) {
  266.             $this->activeUser = '';
  267.             return false;
  268.         }
  269.         
  270.         // Perform trimming here before the hashihg
  271.         $password trim($password"\r\n");
  272.         $res[$this->options['passwordcol']] trim($res[$this->options['passwordcol']]"\r\n");
  273.         // If using Challeneg Responce md5 the pass with the secret
  274.         if($isChallengeResponce{
  275.             //print " Orig Password [{$res[$this->options['passwordcol']]}]<br/>\n";
  276.             //print " Challenge [{$this->_auth_obj->session['loginchallenege']}]<br/>\n";
  277.             $res[$this->options['passwordcol']] md5($res[$this->options['passwordcol']].$this->_auth_obj->session['loginchallenege']);
  278.             // UGLY cannot avoid without modifying verifyPassword
  279.             if($this->options['cryptType'== 'md5'{
  280.                 $res[$this->options['passwordcol']] md5($res[$this->options['passwordcol']]);
  281.             }
  282.             //print " Hashed Password [{$res[$this->options['passwordcol']]}]<br/>\n";
  283.         }
  284.         if ($this->verifyPassword($password,
  285.                                   $res[$this->options['passwordcol']],
  286.                                   $this->options['cryptType'])) {
  287.             // Store additional field values in the session
  288.             foreach ($res as $key => $value{
  289.                 if ($key == $this->options['passwordcol'||
  290.                     $key == $this->options['usernamecol']{
  291.                     continue;
  292.                 }
  293.                 // Use reference to the auth object if exists
  294.                 // This is because the auth session variable can change so a static call to setAuthData does not make sence
  295.                 if (is_object($this->_auth_obj)) {
  296.                     $this->_auth_obj->setAuthData($key$value);
  297.                 else {
  298.                     Auth::setAuthData($key$value);
  299.                 }
  300.             }
  301.  
  302.             return true;
  303.         }
  304.  
  305.         $this->activeUser = $res[$this->options['usernamecol']];
  306.         return false;
  307.     }
  308.  
  309.     // }}}
  310.     // {{{ listUsers()
  311.  
  312.     function listUsers()
  313.     {
  314.         $err $this->_prepare();
  315.         if ($err !== true{
  316.             return PEAR::raiseError($err->getMessage()$err->getCode());
  317.         }
  318.  
  319.         $retVal = array();
  320.  
  321.         // Find if db_fields contains a *, if so assume all col are selected
  322.         if (strstr($this->options['db_fields']'*')) {
  323.             $sql_from "*";
  324.         }
  325.         else{
  326.             $sql_from $this->options['usernamecol'", ".$this->options['passwordcol'].$this->options['db_fields'];
  327.         }
  328.  
  329.         $query sprintf("SELECT %s FROM %s",
  330.                          $sql_from,
  331.                          $this->options['table']
  332.                          );
  333.         $res $this->db->getAll($querynullDB_FETCHMODE_ASSOC);
  334.  
  335.         if (DB::isError($res)) {
  336.             return PEAR::raiseError($res->getMessage()$res->getCode());
  337.         else {
  338.             foreach ($res as $user{
  339.                 $user['username'$user[$this->options['usernamecol']];
  340.                 $retVal[$user;
  341.             }
  342.         }
  343.         return $retVal;
  344.     }
  345.  
  346.     // }}}
  347.     // {{{ addUser()
  348.  
  349.     /**
  350.      * Add user to the storage container
  351.      *
  352.      * @access public
  353.      * @param  string Username
  354.      * @param  string Password
  355.      * @param  mixed  Additional information that are stored in the DB
  356.      *
  357.      * @return mixed True on success, otherwise error object
  358.      */
  359.     function addUser($username$password$additional "")
  360.     {
  361.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  362.             $cryptFunction 'strval';
  363.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  364.             $cryptFunction $this->options['cryptType'];
  365.         else {
  366.             $cryptFunction 'md5';
  367.         }
  368.         
  369.         $password $cryptFunction($password);
  370.  
  371.         $additional_key   '';
  372.         $additional_value '';
  373.  
  374.         if (is_array($additional)) {
  375.             foreach ($additional as $key => $value{
  376.                 $additional_key .= ', ' $key;
  377.                 $additional_value .= ", '" $value "'";
  378.             }
  379.         }
  380.  
  381.         $query sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
  382.                          $this->options['table'],
  383.                          $this->options['usernamecol'],
  384.                          $this->options['passwordcol'],
  385.                          $additional_key,
  386.                          $username,
  387.                          $password,
  388.                          $additional_value
  389.                          );
  390.  
  391.         $res $this->query($query);
  392.  
  393.         if (DB::isError($res)) {
  394.             return PEAR::raiseError($res->getMessage()$res->getCode());
  395.         else {
  396.             return true;
  397.         }
  398.     }
  399.  
  400.     // }}}
  401.     // {{{ removeUser()
  402.  
  403.     /**
  404.      * Remove user from the storage container
  405.      *
  406.      * @access public
  407.      * @param  string Username
  408.      *
  409.      * @return mixed True on success, otherwise error object
  410.      */
  411.     function removeUser($username)
  412.     {
  413.         $query sprintf("DELETE FROM %s WHERE %s = '%s'",
  414.                          $this->options['table'],
  415.                          $this->options['usernamecol'],
  416.                          $username
  417.                          );
  418.  
  419.         $res $this->query($query);
  420.  
  421.         if (DB::isError($res)) {
  422.            return PEAR::raiseError($res->getMessage()$res->getCode());
  423.         else {
  424.           return true;
  425.         }
  426.     }
  427.  
  428.     // }}}
  429.     // {{{ changePassword()
  430.  
  431.     /**
  432.      * Change password for user in the storage container
  433.      *
  434.      * @param string Username
  435.      * @param string The new password (plain text)
  436.      */
  437.     function changePassword($username$password)
  438.     {
  439.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  440.             $cryptFunction 'strval';
  441.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  442.             $cryptFunction $this->options['cryptType'];
  443.         else {
  444.             $cryptFunction 'md5';
  445.         }
  446.         
  447.         $password $cryptFunction($password);
  448.  
  449.         $query sprintf("UPDATE %s SET %s = '%s' WHERE %s = '%s'",
  450.                          $this->options['table'],
  451.                          $this->options['passwordcol'],
  452.                          $password,
  453.                          $this->options['usernamecol'],
  454.                          $username
  455.                          );
  456.  
  457.         $res $this->query($query);
  458.  
  459.         if (DB::isError($res)) {
  460.             return PEAR::raiseError($res->getMessage()$res->getCode());
  461.         else {
  462.             return true;
  463.         }
  464.     }
  465.  
  466.     function supportsChallengeResponce({
  467.         if$this->options['cryptType'== 'md5' || $this->options['cryptType'== 'none' || $this->options['cryptType'== '' {
  468.             return(true);
  469.         }
  470.         return(false);
  471.     }
  472.     
  473.     function getCryptType({
  474.         return($this->options['cryptType']);
  475.     }
  476.     
  477.     // }}}
  478. }
  479. ?>

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