Fix: Mise au format unix du fichier DB.php

This commit is contained in:
Laurent Destailleur 2005-09-04 19:09:11 +00:00
parent b44faeb861
commit e02f80ea23

View File

@ -1,394 +1,394 @@
<?php <?php
// //
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP Version 4 | // | PHP Version 4 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | | // | |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, | // | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is | // | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at | // | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. | // | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to | // | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to | // | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. | // | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Authors: Martin Jansen <mj@php.net> | // | Authors: Martin Jansen <mj@php.net> |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// //
// $Id$ // $Id$
// //
//require_once 'Auth/Container.php'; //require_once 'Auth/Container.php';
require_once DOL_DOCUMENT_ROOT."/includes/pear/Auth/Container.php"; require_once DOL_DOCUMENT_ROOT."/includes/pear/Auth/Container.php";
//require_once 'DB.php'; //require_once 'DB.php';
require_once DOL_DOCUMENT_ROOT."/includes/pear/DB.php"; require_once DOL_DOCUMENT_ROOT."/includes/pear/DB.php";
/** /**
* Storage driver for fetching login data from a database * Storage driver for fetching login data from a database
* *
* This storage driver can use all databases which are supported * This storage driver can use all databases which are supported
* by the PEAR DB abstraction layer to fetch login data. * by the PEAR DB abstraction layer to fetch login data.
* *
* @author Martin Jansen <mj@php.net> * @author Martin Jansen <mj@php.net>
* @package Auth * @package Auth
* @version $Revision$ * @version $Revision$
*/ */
class Auth_Container_DB extends Auth_Container class Auth_Container_DB extends Auth_Container
{ {
/** /**
* Additional options for the storage container * Additional options for the storage container
* @var array * @var array
*/ */
var $options = array(); var $options = array();
/** /**
* DB object * DB object
* @var object * @var object
*/ */
var $db = null; var $db = null;
var $dsn = ''; var $dsn = '';
/** /**
* User that is currently selected from the DB. * User that is currently selected from the DB.
* @var string * @var string
*/ */
var $activeUser = ''; var $activeUser = '';
// {{{ Constructor // {{{ Constructor
/** /**
* Constructor of the container class * Constructor of the container class
* *
* Initate connection to the database via PEAR::DB * Initate connection to the database via PEAR::DB
* *
* @param string Connection data or DB object * @param string Connection data or DB object
* @return object Returns an error object if something went wrong * @return object Returns an error object if something went wrong
*/ */
function Auth_Container_DB($dsn) function Auth_Container_DB($dsn)
{ {
$this->_setDefaults(); $this->_setDefaults();
if (is_array($dsn)) { if (is_array($dsn)) {
$this->_parseOptions($dsn); $this->_parseOptions($dsn);
if (empty($this->options['dsn'])) { if (empty($this->options['dsn'])) {
PEAR::raiseError('No connection parameters specified!'); PEAR::raiseError('No connection parameters specified!');
} }
} else { } else {
$this->options['dsn'] = $dsn; $this->options['dsn'] = $dsn;
} }
} }
// }}} // }}}
// {{{ _connect() // {{{ _connect()
/** /**
* Connect to database by using the given DSN string * Connect to database by using the given DSN string
* *
* @access private * @access private
* @param string DSN string * @param string DSN string
* @return mixed Object on error, otherwise bool * @return mixed Object on error, otherwise bool
*/ */
function _connect($dsn) function _connect($dsn)
{ {
if (is_string($dsn) || is_array($dsn)) { if (is_string($dsn) || is_array($dsn)) {
$this->db = DB::Connect($dsn); $this->db = DB::Connect($dsn);
} elseif (get_parent_class($dsn) == "db_common") { } elseif (get_parent_class($dsn) == "db_common") {
$this->db = $dsn; $this->db = $dsn;
} elseif (DB::isError($dsn)) { } elseif (DB::isError($dsn)) {
return PEAR::raiseError($dsn->getMessage(), $dsn->getCode()); return PEAR::raiseError($dsn->getMessage(), $dsn->getCode());
} else { } else {
return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
41, 41,
PEAR_ERROR_RETURN, PEAR_ERROR_RETURN,
null, null,
null null
); );
} }
if (DB::isError($this->db) || DOLIPEAR::isError($this->db)) { if (DB::isError($this->db) || DOLIPEAR::isError($this->db)) {
return DOLIPEAR::raiseError($this->db->getMessage(), $this->db->getCode()); return DOLIPEAR::raiseError($this->db->getMessage(), $this->db->getCode());
} else { } else {
return true; return true;
} }
} }
// }}} // }}}
// {{{ _prepare() // {{{ _prepare()
/** /**
* Prepare database connection * Prepare database connection
* *
* This function checks if we have already opened a connection to * This function checks if we have already opened a connection to
* the database. If that's not the case, a new connection is opened. * the database. If that's not the case, a new connection is opened.
* *
* @access private * @access private
* @return mixed True or a DB error object. * @return mixed True or a DB error object.
*/ */
function _prepare() function _prepare()
{ {
if (!DB::isConnection($this->db)) { if (!DB::isConnection($this->db)) {
$res = $this->_connect($this->options['dsn']); $res = $this->_connect($this->options['dsn']);
if(DB::isError($res) || DOLIPEAR::isError($res)){ if(DB::isError($res) || DOLIPEAR::isError($res)){
return $res; return $res;
} }
} }
return true; return true;
} }
// }}} // }}}
// {{{ query() // {{{ query()
/** /**
* Prepare query to the database * Prepare query to the database
* *
* This function checks if we have already opened a connection to * This function checks if we have already opened a connection to
* the database. If that's not the case, a new connection is opened. * the database. If that's not the case, a new connection is opened.
* After that the query is passed to the database. * After that the query is passed to the database.
* *
* @access public * @access public
* @param string Query string * @param string Query string
* @return mixed a DB_result object or DB_OK on success, a DB * @return mixed a DB_result object or DB_OK on success, a DB
* or PEAR error on failure * or PEAR error on failure
*/ */
function query($query) function query($query)
{ {
$err = $this->_prepare(); $err = $this->_prepare();
if ($err !== true) { if ($err !== true) {
return $err; return $err;
} }
return $this->db->query($query); return $this->db->query($query);
} }
// }}} // }}}
// {{{ _setDefaults() // {{{ _setDefaults()
/** /**
* Set some default options * Set some default options
* *
* @access private * @access private
* @return void * @return void
*/ */
function _setDefaults() function _setDefaults()
{ {
$this->options['table'] = 'auth'; $this->options['table'] = 'auth';
$this->options['usernamecol'] = 'username'; $this->options['usernamecol'] = 'username';
$this->options['passwordcol'] = 'password'; $this->options['passwordcol'] = 'password';
$this->options['dsn'] = ''; $this->options['dsn'] = '';
$this->options['db_fields'] = ''; $this->options['db_fields'] = '';
$this->options['cryptType'] = 'md5'; $this->options['cryptType'] = 'md5';
} }
// }}} // }}}
// {{{ _parseOptions() // {{{ _parseOptions()
/** /**
* Parse options passed to the container class * Parse options passed to the container class
* *
* @access private * @access private
* @param array * @param array
*/ */
function _parseOptions($array) function _parseOptions($array)
{ {
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
if (isset($this->options[$key])) { if (isset($this->options[$key])) {
$this->options[$key] = $value; $this->options[$key] = $value;
} }
} }
/* Include additional fields if they exist */ /* Include additional fields if they exist */
if(!empty($this->options['db_fields'])){ if(!empty($this->options['db_fields'])){
if(is_array($this->options['db_fields'])){ if(is_array($this->options['db_fields'])){
$this->options['db_fields'] = join($this->options['db_fields'], ', '); $this->options['db_fields'] = join($this->options['db_fields'], ', ');
} }
$this->options['db_fields'] = ', '.$this->options['db_fields']; $this->options['db_fields'] = ', '.$this->options['db_fields'];
} }
} }
// }}} // }}}
// {{{ fetchData() // {{{ fetchData()
/** /**
* Get user information from database * Get user information from database
* *
* This function uses the given username to fetch * This function uses the given username to fetch
* the corresponding login data from the database * the corresponding login data from the database
* table. If an account that matches the passed username * table. If an account that matches the passed username
* and password is found, the function returns true. * and password is found, the function returns true.
* Otherwise it returns false. * Otherwise it returns false.
* *
* @param string Username * @param string Username
* @param string Password * @param string Password
* @return mixed Error object or boolean * @return mixed Error object or boolean
*/ */
function fetchData($username, $password) function fetchData($username, $password)
{ {
// Prepare for a database query // Prepare for a database query
$err = $this->_prepare(); $err = $this->_prepare();
if ($err !== true) { if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode()); return PEAR::raiseError($err->getMessage(), $err->getCode());
} }
// Find if db_fileds contains a *, i so assume all col are selected // Find if db_fileds contains a *, i so assume all col are selected
if(strstr($this->options['db_fields'], '*')){ if(strstr($this->options['db_fields'], '*')){
$sql_from = "*"; $sql_from = "*";
} }
else{ else{
$sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
} }
$query = "SELECT ! FROM ! WHERE ! = ?"; $query = "SELECT ! FROM ! WHERE ! = ?";
$query_params = array( $query_params = array(
$sql_from, $sql_from,
$this->options['table'], $this->options['table'],
$this->options['usernamecol'], $this->options['usernamecol'],
$username $username
); );
$res = $this->db->getRow($query, $query_params, DB_FETCHMODE_ASSOC); $res = $this->db->getRow($query, $query_params, DB_FETCHMODE_ASSOC);
if (DB::isError($res)) { if (DB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->getCode()); return PEAR::raiseError($res->getMessage(), $res->getCode());
} }
if (!is_array($res)) { if (!is_array($res)) {
$this->activeUser = ''; $this->activeUser = '';
return false; return false;
} }
if ($this->verifyPassword(trim($password), if ($this->verifyPassword(trim($password),
trim($res[$this->options['passwordcol']]), trim($res[$this->options['passwordcol']]),
$this->options['cryptType'])) { $this->options['cryptType'])) {
// Store additional field values in the session // Store additional field values in the session
foreach ($res as $key => $value) { foreach ($res as $key => $value) {
if ($key == $this->options['passwordcol'] || if ($key == $this->options['passwordcol'] ||
$key == $this->options['usernamecol']) { $key == $this->options['usernamecol']) {
continue; continue;
} }
Auth::setAuthData($key, $value); Auth::setAuthData($key, $value);
} }
return true; return true;
} }
$this->activeUser = $res[$this->options['usernamecol']]; $this->activeUser = $res[$this->options['usernamecol']];
return false; return false;
} }
// }}} // }}}
// {{{ listUsers() // {{{ listUsers()
function listUsers() function listUsers()
{ {
$err = $this->_prepare(); $err = $this->_prepare();
if ($err !== true) { if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode()); return PEAR::raiseError($err->getMessage(), $err->getCode());
} }
$retVal = array(); $retVal = array();
// Find if db_fileds contains a *, i so assume all col are selected // Find if db_fileds contains a *, i so assume all col are selected
if(strstr($this->options['db_fields'], '*') || empty($this->options['db_fields'])){ if(strstr($this->options['db_fields'], '*') || empty($this->options['db_fields'])){
$sql_from = "*"; $sql_from = "*";
} }
else{ else{
$sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
} }
$query = sprintf("SELECT %s FROM %s", $query = sprintf("SELECT %s FROM %s",
$sql_from, $sql_from,
$this->options['table'] $this->options['table']
); );
$res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC); $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
if (DB::isError($res)) { if (DB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->getCode()); return PEAR::raiseError($res->getMessage(), $res->getCode());
} else { } else {
foreach ($res as $user) { foreach ($res as $user) {
$user['username'] = $user[$this->options['usernamecol']]; $user['username'] = $user[$this->options['usernamecol']];
$retVal[] = $user; $retVal[] = $user;
} }
} }
return $retVal; return $retVal;
} }
// }}} // }}}
// {{{ addUser() // {{{ addUser()
/** /**
* Add user to the storage container * Add user to the storage container
* *
* @access public * @access public
* @param string Username * @param string Username
* @param string Password * @param string Password
* @param mixed Additional information that are stored in the DB * @param mixed Additional information that are stored in the DB
* *
* @return mixed True on success, otherwise error object * @return mixed True on success, otherwise error object
*/ */
function addUser($username, $password, $additional = "") function addUser($username, $password, $additional = "")
{ {
if (function_exists($this->options['cryptType'])) { if (function_exists($this->options['cryptType'])) {
$cryptfunction = $this->options['cryptType']; $cryptfunction = $this->options['cryptType'];
} else { } else {
$cryptfunction = 'md5'; $cryptfunction = 'md5';
} }
$additional_key = ''; $additional_key = '';
$additional_value = ''; $additional_value = '';
if (is_array($additional)) { if (is_array($additional)) {
foreach ($additional as $key => $value) { foreach ($additional as $key => $value) {
$additional_key .= ', ' . $key; $additional_key .= ', ' . $key;
$additional_value .= ", '" . $value . "'"; $additional_value .= ", '" . $value . "'";
} }
} }
$query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)", $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
$this->options['table'], $this->options['table'],
$this->options['usernamecol'], $this->options['usernamecol'],
$this->options['passwordcol'], $this->options['passwordcol'],
$additional_key, $additional_key,
$username, $username,
$cryptfunction($password), $cryptfunction($password),
$additional_value $additional_value
); );
$res = $this->query($query); $res = $this->query($query);
if (DB::isError($res)) { if (DB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->getCode()); return PEAR::raiseError($res->getMessage(), $res->getCode());
} else { } else {
return true; return true;
} }
} }
// }}} // }}}
// {{{ removeUser() // {{{ removeUser()
/** /**
* Remove user from the storage container * Remove user from the storage container
* *
* @access public * @access public
* @param string Username * @param string Username
* *
* @return mixed True on success, otherwise error object * @return mixed True on success, otherwise error object
*/ */
function removeUser($username) function removeUser($username)
{ {
$query = sprintf("DELETE FROM %s WHERE %s = '%s'", $query = sprintf("DELETE FROM %s WHERE %s = '%s'",
$this->options['table'], $this->options['table'],
$this->options['usernamecol'], $this->options['usernamecol'],
$username $username
); );
$res = $this->query($query); $res = $this->query($query);
if (DB::isError($res)) { if (DB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->getCode()); return PEAR::raiseError($res->getMessage(), $res->getCode());
} else { } else {
return true; return true;
} }
} }
// }}} // }}}
} }
?> ?>