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.44 2004/03/28 23:19:50 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.44 $
  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.     // {{{ fetchData()
  210.  
  211.     /**
  212.      * Get user information from database
  213.      *
  214.      * This function uses the given username to fetch
  215.      * the corresponding login data from the database
  216.      * table. If an account that matches the passed username
  217.      * and password is found, the function returns true.
  218.      * Otherwise it returns false.
  219.      *
  220.      * @param   string Username
  221.      * @param   string Password
  222.      * @return  mixed  Error object or boolean
  223.      */
  224.     function fetchData($username$password)
  225.     {
  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.  
  260.         if (DB::isError($res)) {
  261.             return PEAR::raiseError($res->getMessage()$res->getCode());
  262.         }
  263.         if (!is_array($res)) {
  264.             $this->activeUser = '';
  265.             return false;
  266.         }
  267.         if ($this->verifyPassword(trim($password"\r\n"),
  268.                                   trim($res[$this->options['passwordcol']]"\r\n"),
  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 sence
  278.                 if (is_object($this->_auth_obj)) {
  279.                     $this->_auth_obj->setAuthData($key$value);
  280.                 else {
  281.                     Auth::setAuthData($key$value);
  282.                 }
  283.             }
  284.  
  285.             return true;
  286.         }
  287.  
  288.         $this->activeUser = $res[$this->options['usernamecol']];
  289.         return false;
  290.     }
  291.  
  292.     // }}}
  293.     // {{{ listUsers()
  294.  
  295.     function listUsers()
  296.     {
  297.         $err $this->_prepare();
  298.         if ($err !== true{
  299.             return PEAR::raiseError($err->getMessage()$err->getCode());
  300.         }
  301.  
  302.         $retVal = array();
  303.  
  304.         // Find if db_fields contains a *, if so assume all col are selected
  305.         if (strstr($this->options['db_fields']'*')) {
  306.             $sql_from "*";
  307.         }
  308.         else{
  309.             $sql_from $this->options['usernamecol'", ".$this->options['passwordcol'].$this->options['db_fields'];
  310.         }
  311.  
  312.         $query sprintf("SELECT %s FROM %s",
  313.                          $sql_from,
  314.                          $this->options['table']
  315.                          );
  316.         $res $this->db->getAll($querynullDB_FETCHMODE_ASSOC);
  317.  
  318.         if (DB::isError($res)) {
  319.             return PEAR::raiseError($res->getMessage()$res->getCode());
  320.         else {
  321.             foreach ($res as $user{
  322.                 $user['username'$user[$this->options['usernamecol']];
  323.                 $retVal[$user;
  324.             }
  325.         }
  326.         return $retVal;
  327.     }
  328.  
  329.     // }}}
  330.     // {{{ addUser()
  331.  
  332.     /**
  333.      * Add user to the storage container
  334.      *
  335.      * @access public
  336.      * @param  string Username
  337.      * @param  string Password
  338.      * @param  mixed  Additional information that are stored in the DB
  339.      *
  340.      * @return mixed True on success, otherwise error object
  341.      */
  342.     function addUser($username$password$additional "")
  343.     {
  344.         if (isset($this->options['cryptType']&& $this->options['cryptType'== 'none'{
  345.             $cryptFunction 'strval';
  346.         elseif (isset($this->options['cryptType']&& function_exists($this->options['cryptType'])) {
  347.             $cryptFunction $this->options['cryptType'];
  348.         else {
  349.             $cryptFunction 'md5';
  350.         }
  351.         
  352.         $password $cryptFunction($password);
  353.  
  354.         $additional_key   '';
  355.         $additional_value '';
  356.  
  357.         if (is_array($additional)) {
  358.             foreach ($additional as $key => $value{
  359.                 $additional_key .= ', ' $key;
  360.                 $additional_value .= ", '" $value "'";
  361.             }
  362.         }
  363.  
  364.         $query sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
  365.                          $this->options['table'],
  366.                          $this->options['usernamecol'],
  367.                          $this->options['passwordcol'],
  368.                          $additional_key,
  369.                          $username,
  370.                          $password,
  371.                          $additional_value
  372.                          );
  373.  
  374.         $res $this->query($query);
  375.  
  376.         if (DB::isError($res)) {
  377.             return PEAR::raiseError($res->getMessage()$res->getCode());
  378.         else {
  379.             return true;
  380.         }
  381.     }
  382.  
  383.     // }}}
  384.     // {{{ removeUser()
  385.  
  386.     /**
  387.      * Remove user from the storage container
  388.      *
  389.      * @access public
  390.      * @param  string Username
  391.      *
  392.      * @return mixed True on success, otherwise error object
  393.      */
  394.     function removeUser($username)
  395.     {
  396.         $query sprintf("DELETE FROM %s WHERE %s = '%s'",
  397.                          $this->options['table'],
  398.                          $this->options['usernamecol'],
  399.                          $username
  400.                          );
  401.  
  402.         $res $this->query($query);
  403.  
  404.         if (DB::isError($res)) {
  405.            return PEAR::raiseError($res->getMessage()$res->getCode());
  406.         else {
  407.           return true;
  408.         }
  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.                          $password,
  436.                          $this->options['usernamecol'],
  437.                          $username
  438.                          );
  439.  
  440.         $res $this->query($query);
  441.  
  442.         if (DB::isError($res)) {
  443.             return PEAR::raiseError($res->getMessage()$res->getCode());
  444.         else {
  445.             return true;
  446.         }
  447.     }
  448.  
  449.     // }}}
  450. }
  451. ?>

Documentation generated on Mon, 11 Mar 2019 10:17:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.