TYPO3 CMS  TYPO3_7-6
ADOdb.php
Go to the documentation of this file.
1 <?php
2 /*
3 @version v5.20.3 01-Jan-2016
4 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6  Released under both BSD license and Lesser GPL library license.
7  Whenever there is any discrepancy between the two licenses,
8  the BSD license will take precedence. See License.txt.
9  Set tabs to 4 for best viewing.
10 
11  Latest version is available at http://adodb.sourceforge.net
12 
13  Original Authors: Martin Jansen <mj#php.net>
14  Richard Tango-Lowy <richtl#arscognita.com>
15 */
16 
17 require_once 'Auth/Container.php';
18 require_once 'adodb.inc.php';
19 require_once 'adodb-pear.inc.php';
20 require_once 'adodb-errorpear.inc.php';
21 
34 class Auth_Container_ADOdb extends Auth_Container
35 {
36 
41  var $options = array();
42 
47  var $db = null;
48  var $dsn = '';
49 
54  var $activeUser = '';
55 
56  // {{{ Constructor
57 
66  function __construct($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  // Extract db_type from dsn string.
78  $this->options['dsn'] = $dsn;
79  }
80  }
81 
82  // }}}
83  // {{{ _connect()
84 
92  function _connect($dsn)
93  {
94  if (is_string($dsn) || is_array($dsn)) {
95  if(!$this->db) {
96  $this->db = ADONewConnection($dsn);
97  if( $err = ADODB_Pear_error() ) {
98  return PEAR::raiseError($err);
99  }
100  }
101 
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(!$this->db) {
112  return PEAR::raiseError(ADODB_Pear_error());
113  } else {
114  return true;
115  }
116  }
117 
118  // }}}
119  // {{{ _prepare()
120 
130  function _prepare()
131  {
132  if(!$this->db) {
133  $res = $this->_connect($this->options['dsn']);
134  }
135  return true;
136  }
137 
138  // }}}
139  // {{{ query()
140 
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 
171  function _setDefaults()
172  {
173  $this->options['db_type'] = 'mysql';
174  $this->options['table'] = 'auth';
175  $this->options['usernamecol'] = 'username';
176  $this->options['passwordcol'] = 'password';
177  $this->options['dsn'] = '';
178  $this->options['db_fields'] = '';
179  $this->options['cryptType'] = 'md5';
180  }
181 
182  // }}}
183  // {{{ _parseOptions()
184 
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 
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 *, i 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  $query = "SELECT ".$sql_from.
241  " FROM ".$this->options['table'].
242  " WHERE ".$this->options['usernamecol']." = " . $this->db->Quote($username);
243 
244  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
245  $rset = $this->db->Execute( $query );
246  $res = $rset->fetchRow();
247 
248  if (DB::isError($res)) {
249  return PEAR::raiseError($res->getMessage(), $res->getCode());
250  }
251  if (!is_array($res)) {
252  $this->activeUser = '';
253  return false;
254  }
255  if ($this->verifyPassword(trim($password, "\r\n"),
256  trim($res[$this->options['passwordcol']], "\r\n"),
257  $this->options['cryptType'])) {
258  // Store additional field values in the session
259  foreach ($res as $key => $value) {
260  if ($key == $this->options['passwordcol'] ||
261  $key == $this->options['usernamecol']) {
262  continue;
263  }
264  // Use reference to the auth object if exists
265  // This is because the auth session variable can change so a static call to setAuthData does not make sence
266  if(is_object($this->_auth_obj)){
267  $this->_auth_obj->setAuthData($key, $value);
268  } else {
269  Auth::setAuthData($key, $value);
270  }
271  }
272 
273  return true;
274  }
275 
276  $this->activeUser = $res[$this->options['usernamecol']];
277  return false;
278  }
279 
280  // }}}
281  // {{{ listUsers()
282 
283  function listUsers()
284  {
285  $err = $this->_prepare();
286  if ($err !== true) {
287  return PEAR::raiseError($err->getMessage(), $err->getCode());
288  }
289 
290  $retVal = array();
291 
292  // Find if db_fileds contains a *, i so assume all col are selected
293  if(strstr($this->options['db_fields'], '*')){
294  $sql_from = "*";
295  }
296  else{
297  $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
298  }
299 
300  $query = sprintf("SELECT %s FROM %s",
301  $sql_from,
302  $this->options['table']
303  );
304  $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
305 
306  if (DB::isError($res)) {
307  return PEAR::raiseError($res->getMessage(), $res->getCode());
308  } else {
309  foreach ($res as $user) {
310  $user['username'] = $user[$this->options['usernamecol']];
311  $retVal[] = $user;
312  }
313  }
314  return $retVal;
315  }
316 
317  // }}}
318  // {{{ addUser()
319 
330  function addUser($username, $password, $additional = "")
331  {
332  if (function_exists($this->options['cryptType'])) {
333  $cryptFunction = $this->options['cryptType'];
334  } else {
335  $cryptFunction = 'md5';
336  }
337 
338  $additional_key = '';
339  $additional_value = '';
340 
341  if (is_array($additional)) {
342  foreach ($additional as $key => $value) {
343  $additional_key .= ', ' . $key;
344  $additional_value .= ", '" . $value . "'";
345  }
346  }
347 
348  $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
349  $this->options['table'],
350  $this->options['usernamecol'],
351  $this->options['passwordcol'],
352  $additional_key,
353  $username,
354  $cryptFunction($password),
355  $additional_value
356  );
357 
358  $res = $this->query($query);
359 
360  if (DB::isError($res)) {
361  return PEAR::raiseError($res->getMessage(), $res->getCode());
362  } else {
363  return true;
364  }
365  }
366 
367  // }}}
368  // {{{ removeUser()
369 
378  function removeUser($username)
379  {
380  $query = sprintf("DELETE FROM %s WHERE %s = '%s'",
381  $this->options['table'],
382  $this->options['usernamecol'],
383  $username
384  );
385 
386  $res = $this->query($query);
387 
388  if (DB::isError($res)) {
389  return PEAR::raiseError($res->getMessage(), $res->getCode());
390  } else {
391  return true;
392  }
393  }
394 
395  // }}}
396 }
397 
398 function showDbg( $string ) {
399  print "
400 -- $string</P>";
401 }
402 function dump( $var, $str, $vardump = false ) {
403  print "<H4>$str</H4><pre>";
404  ( !$vardump ) ? ( print_r( $var )) : ( var_dump( $var ));
405  print "</pre>";
406 }
addUser($username, $password, $additional="")
Definition: ADOdb.php:330
_parseOptions($array)
Definition: ADOdb.php:191
fetchData($username, $password)
Definition: ADOdb.php:224
dump( $var, $str, $vardump=false)
Definition: ADOdb.php:402
removeUser($username)
Definition: ADOdb.php:378
showDbg( $string)
Definition: ADOdb.php:398
isError($value)