| [ Index ] |
|
Code source de Dolibarr 2.0.1 |
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.4 2005/09/04 19:09:11 eldy Exp $ 20 // 21 22 //require_once 'Auth/Container.php'; 23 require_once DOL_DOCUMENT_ROOT."/includes/pear/Auth/Container.php"; 24 //require_once 'DB.php'; 25 require_once DOL_DOCUMENT_ROOT."/includes/pear/DB.php"; 26 27 /** 28 * Storage driver for fetching login data from a database 29 * 30 * This storage driver can use all databases which are supported 31 * by the PEAR DB abstraction layer to fetch login data. 32 * 33 * @author Martin Jansen <mj@php.net> 34 * @package Auth 35 * @version $Revision: 1.4 $ 36 */ 37 class Auth_Container_DB extends Auth_Container 38 { 39 40 /** 41 * Additional options for the storage container 42 * @var array 43 */ 44 var $options = array(); 45 46 /** 47 * DB object 48 * @var object 49 */ 50 var $db = null; 51 var $dsn = ''; 52 53 /** 54 * User that is currently selected from the DB. 55 * @var string 56 */ 57 var $activeUser = ''; 58 59 // {{{ Constructor 60 61 /** 62 * Constructor of the container class 63 * 64 * Initate connection to the database via PEAR::DB 65 * 66 * @param string Connection data or DB object 67 * @return object Returns an error object if something went wrong 68 */ 69 function Auth_Container_DB($dsn) 70 { 71 $this->_setDefaults(); 72 73 if (is_array($dsn)) { 74 $this->_parseOptions($dsn); 75 76 if (empty($this->options['dsn'])) { 77 PEAR::raiseError('No connection parameters specified!'); 78 } 79 } else { 80 $this->options['dsn'] = $dsn; 81 } 82 } 83 84 // }}} 85 // {{{ _connect() 86 87 /** 88 * Connect to database by using the given DSN string 89 * 90 * @access private 91 * @param string DSN string 92 * @return mixed Object on error, otherwise bool 93 */ 94 function _connect($dsn) 95 { 96 if (is_string($dsn) || is_array($dsn)) { 97 $this->db = DB::Connect($dsn); 98 } elseif (get_parent_class($dsn) == "db_common") { 99 $this->db = $dsn; 100 } elseif (DB::isError($dsn)) { 101 return PEAR::raiseError($dsn->getMessage(), $dsn->getCode()); 102 } else { 103 return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__, 104 41, 105 PEAR_ERROR_RETURN, 106 null, 107 null 108 ); 109 } 110 111 if (DB::isError($this->db) || DOLIPEAR::isError($this->db)) { 112 return DOLIPEAR::raiseError($this->db->getMessage(), $this->db->getCode()); 113 } else { 114 return true; 115 } 116 } 117 118 // }}} 119 // {{{ _prepare() 120 121 /** 122 * Prepare database connection 123 * 124 * This function checks if we have already opened a connection to 125 * the database. If that's not the case, a new connection is opened. 126 * 127 * @access private 128 * @return mixed True or a DB error object. 129 */ 130 function _prepare() 131 { 132 if (!DB::isConnection($this->db)) { 133 $res = $this->_connect($this->options['dsn']); 134 if(DB::isError($res) || DOLIPEAR::isError($res)){ 135 return $res; 136 } 137 } 138 return true; 139 } 140 141 // }}} 142 // {{{ query() 143 144 /** 145 * Prepare query to the database 146 * 147 * This function checks if we have already opened a connection to 148 * the database. If that's not the case, a new connection is opened. 149 * After that the query is passed to the database. 150 * 151 * @access public 152 * @param string Query string 153 * @return mixed a DB_result object or DB_OK on success, a DB 154 * or PEAR error on failure 155 */ 156 function query($query) 157 { 158 $err = $this->_prepare(); 159 if ($err !== true) { 160 return $err; 161 } 162 return $this->db->query($query); 163 } 164 165 // }}} 166 // {{{ _setDefaults() 167 168 /** 169 * Set some default options 170 * 171 * @access private 172 * @return void 173 */ 174 function _setDefaults() 175 { 176 $this->options['table'] = 'auth'; 177 $this->options['usernamecol'] = 'username'; 178 $this->options['passwordcol'] = 'password'; 179 $this->options['dsn'] = ''; 180 $this->options['db_fields'] = ''; 181 $this->options['cryptType'] = 'md5'; 182 } 183 184 // }}} 185 // {{{ _parseOptions() 186 187 /** 188 * Parse options passed to the container class 189 * 190 * @access private 191 * @param array 192 */ 193 function _parseOptions($array) 194 { 195 foreach ($array as $key => $value) { 196 if (isset($this->options[$key])) { 197 $this->options[$key] = $value; 198 } 199 } 200 201 /* Include additional fields if they exist */ 202 if(!empty($this->options['db_fields'])){ 203 if(is_array($this->options['db_fields'])){ 204 $this->options['db_fields'] = join($this->options['db_fields'], ', '); 205 } 206 $this->options['db_fields'] = ', '.$this->options['db_fields']; 207 } 208 } 209 210 // }}} 211 // {{{ fetchData() 212 213 /** 214 * Get user information from database 215 * 216 * This function uses the given username to fetch 217 * the corresponding login data from the database 218 * table. If an account that matches the passed username 219 * and password is found, the function returns true. 220 * Otherwise it returns false. 221 * 222 * @param string Username 223 * @param string Password 224 * @return mixed Error object or boolean 225 */ 226 function fetchData($username, $password) 227 { 228 // Prepare for a database query 229 $err = $this->_prepare(); 230 if ($err !== true) { 231 return PEAR::raiseError($err->getMessage(), $err->getCode()); 232 } 233 234 // Find if db_fileds contains a *, i so assume all col are selected 235 if(strstr($this->options['db_fields'], '*')){ 236 $sql_from = "*"; 237 } 238 else{ 239 $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; 240 } 241 242 $query = "SELECT ! FROM ! WHERE ! = ?"; 243 $query_params = array( 244 $sql_from, 245 $this->options['table'], 246 $this->options['usernamecol'], 247 $username 248 ); 249 $res = $this->db->getRow($query, $query_params, DB_FETCHMODE_ASSOC); 250 251 if (DB::isError($res)) { 252 return PEAR::raiseError($res->getMessage(), $res->getCode()); 253 } 254 if (!is_array($res)) { 255 $this->activeUser = ''; 256 return false; 257 } 258 if ($this->verifyPassword(trim($password), 259 trim($res[$this->options['passwordcol']]), 260 $this->options['cryptType'])) { 261 // Store additional field values in the session 262 foreach ($res as $key => $value) { 263 if ($key == $this->options['passwordcol'] || 264 $key == $this->options['usernamecol']) { 265 continue; 266 } 267 Auth::setAuthData($key, $value); 268 } 269 270 return true; 271 } 272 273 $this->activeUser = $res[$this->options['usernamecol']]; 274 return false; 275 } 276 277 // }}} 278 // {{{ listUsers() 279 280 function listUsers() 281 { 282 $err = $this->_prepare(); 283 if ($err !== true) { 284 return PEAR::raiseError($err->getMessage(), $err->getCode()); 285 } 286 287 $retVal = array(); 288 289 // Find if db_fileds contains a *, i so assume all col are selected 290 if(strstr($this->options['db_fields'], '*') || empty($this->options['db_fields'])){ 291 $sql_from = "*"; 292 } 293 else{ 294 $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields']; 295 } 296 297 $query = sprintf("SELECT %s FROM %s", 298 $sql_from, 299 $this->options['table'] 300 ); 301 $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC); 302 303 if (DB::isError($res)) { 304 return PEAR::raiseError($res->getMessage(), $res->getCode()); 305 } else { 306 foreach ($res as $user) { 307 $user['username'] = $user[$this->options['usernamecol']]; 308 $retVal[] = $user; 309 } 310 } 311 return $retVal; 312 } 313 314 // }}} 315 // {{{ addUser() 316 317 /** 318 * Add user to the storage container 319 * 320 * @access public 321 * @param string Username 322 * @param string Password 323 * @param mixed Additional information that are stored in the DB 324 * 325 * @return mixed True on success, otherwise error object 326 */ 327 function addUser($username, $password, $additional = "") 328 { 329 if (function_exists($this->options['cryptType'])) { 330 $cryptfunction = $this->options['cryptType']; 331 } else { 332 $cryptfunction = 'md5'; 333 } 334 335 $additional_key = ''; 336 $additional_value = ''; 337 338 if (is_array($additional)) { 339 foreach ($additional as $key => $value) { 340 $additional_key .= ', ' . $key; 341 $additional_value .= ", '" . $value . "'"; 342 } 343 } 344 345 $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)", 346 $this->options['table'], 347 $this->options['usernamecol'], 348 $this->options['passwordcol'], 349 $additional_key, 350 $username, 351 $cryptfunction($password), 352 $additional_value 353 ); 354 355 $res = $this->query($query); 356 357 if (DB::isError($res)) { 358 return PEAR::raiseError($res->getMessage(), $res->getCode()); 359 } else { 360 return true; 361 } 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 $username 381 ); 382 383 $res = $this->query($query); 384 385 if (DB::isError($res)) { 386 return PEAR::raiseError($res->getMessage(), $res->getCode()); 387 } else { 388 return true; 389 } 390 } 391 392 // }}} 393 } 394 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 12:29:37 2007 | par Balluche grâce à PHPXref 0.7 |
|