PageRenderTime 74ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/adodb.493a/pear/Auth/Container/ADOdb.php

https://github.com/infused/retrospect-gds
PHP | 413 lines | 213 code | 49 blank | 151 comment | 36 complexity | 50f80907419c441e57cd85b47963c31c MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.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. // | Richard Tango-Lowy <richtl@arscognita.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: ADOdb.php,v 1.3 2005/05/18 06:58:47 jlim Exp $
  21. //
  22. require_once 'Auth/Container.php';
  23. require_once 'adodb.inc.php';
  24. require_once 'adodb-pear.inc.php';
  25. require_once 'adodb-errorpear.inc.php';
  26. /**
  27. * Storage driver for fetching login data from a database using ADOdb-PHP.
  28. *
  29. * This storage driver can use all databases which are supported
  30. * by the ADBdb DB abstraction layer to fetch login data.
  31. * See http://php.weblogs.com/adodb for information on ADOdb.
  32. * NOTE: The ADOdb directory MUST be in your PHP include_path!
  33. *
  34. * @author Richard Tango-Lowy <richtl@arscognita.com>
  35. * @package Auth
  36. * @version $Revision: 1.3 $
  37. */
  38. class Auth_Container_ADOdb extends Auth_Container
  39. {
  40. /**
  41. * Additional options for the storage container
  42. * @var array
  43. */
  44. var $options = array();
  45. /**
  46. * DB object
  47. * @var object
  48. */
  49. var $db = null;
  50. var $dsn = '';
  51. /**
  52. * User that is currently selected from the DB.
  53. * @var string
  54. */
  55. var $activeUser = '';
  56. // {{{ Constructor
  57. /**
  58. * Constructor of the container class
  59. *
  60. * Initate connection to the database via PEAR::ADOdb
  61. *
  62. * @param string Connection data or DB object
  63. * @return object Returns an error object if something went wrong
  64. */
  65. function Auth_Container_ADOdb($dsn)
  66. {
  67. $this->_setDefaults();
  68. if (is_array($dsn)) {
  69. $this->_parseOptions($dsn);
  70. if (empty($this->options['dsn'])) {
  71. PEAR::raiseError('No connection parameters specified!');
  72. }
  73. } else {
  74. // Extract db_type from dsn string.
  75. $this->options['dsn'] = $dsn;
  76. }
  77. }
  78. // }}}
  79. // {{{ _connect()
  80. /**
  81. * Connect to database by using the given DSN string
  82. *
  83. * @access private
  84. * @param string DSN string
  85. * @return mixed Object on error, otherwise bool
  86. */
  87. function _connect($dsn)
  88. {
  89. if (is_string($dsn) || is_array($dsn)) {
  90. if(!$this->db) {
  91. $this->db = &ADONewConnection($dsn);
  92. if( $err = ADODB_Pear_error() ) {
  93. return PEAR::raiseError($err);
  94. }
  95. }
  96. } else {
  97. return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
  98. 41,
  99. PEAR_ERROR_RETURN,
  100. null,
  101. null
  102. );
  103. }
  104. if(!$this->db) {
  105. return PEAR::raiseError(ADODB_Pear_error());
  106. } else {
  107. return true;
  108. }
  109. }
  110. // }}}
  111. // {{{ _prepare()
  112. /**
  113. * Prepare database connection
  114. *
  115. * This function checks if we have already opened a connection to
  116. * the database. If that's not the case, a new connection is opened.
  117. *
  118. * @access private
  119. * @return mixed True or a DB error object.
  120. */
  121. function _prepare()
  122. {
  123. if(!$this->db) {
  124. $res = $this->_connect($this->options['dsn']);
  125. }
  126. return true;
  127. }
  128. // }}}
  129. // {{{ query()
  130. /**
  131. * Prepare query to the database
  132. *
  133. * This function checks if we have already opened a connection to
  134. * the database. If that's not the case, a new connection is opened.
  135. * After that the query is passed to the database.
  136. *
  137. * @access public
  138. * @param string Query string
  139. * @return mixed a DB_result object or DB_OK on success, a DB
  140. * or PEAR error on failure
  141. */
  142. function query($query)
  143. {
  144. $err = $this->_prepare();
  145. if ($err !== true) {
  146. return $err;
  147. }
  148. return $this->db->query($query);
  149. }
  150. // }}}
  151. // {{{ _setDefaults()
  152. /**
  153. * Set some default options
  154. *
  155. * @access private
  156. * @return void
  157. */
  158. function _setDefaults()
  159. {
  160. $this->options['db_type'] = 'mysql';
  161. $this->options['table'] = 'auth';
  162. $this->options['usernamecol'] = 'username';
  163. $this->options['passwordcol'] = 'password';
  164. $this->options['dsn'] = '';
  165. $this->options['db_fields'] = '';
  166. $this->options['cryptType'] = 'md5';
  167. }
  168. // }}}
  169. // {{{ _parseOptions()
  170. /**
  171. * Parse options passed to the container class
  172. *
  173. * @access private
  174. * @param array
  175. */
  176. function _parseOptions($array)
  177. {
  178. foreach ($array as $key => $value) {
  179. if (isset($this->options[$key])) {
  180. $this->options[$key] = $value;
  181. }
  182. }
  183. /* Include additional fields if they exist */
  184. if(!empty($this->options['db_fields'])){
  185. if(is_array($this->options['db_fields'])){
  186. $this->options['db_fields'] = join($this->options['db_fields'], ', ');
  187. }
  188. $this->options['db_fields'] = ', '.$this->options['db_fields'];
  189. }
  190. }
  191. // }}}
  192. // {{{ fetchData()
  193. /**
  194. * Get user information from database
  195. *
  196. * This function uses the given username to fetch
  197. * the corresponding login data from the database
  198. * table. If an account that matches the passed username
  199. * and password is found, the function returns true.
  200. * Otherwise it returns false.
  201. *
  202. * @param string Username
  203. * @param string Password
  204. * @return mixed Error object or boolean
  205. */
  206. function fetchData($username, $password)
  207. {
  208. // Prepare for a database query
  209. $err = $this->_prepare();
  210. if ($err !== true) {
  211. return PEAR::raiseError($err->getMessage(), $err->getCode());
  212. }
  213. // Find if db_fields contains a *, i so assume all col are selected
  214. if(strstr($this->options['db_fields'], '*')){
  215. $sql_from = "*";
  216. }
  217. else{
  218. $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
  219. }
  220. $query = "SELECT ".$sql_from.
  221. " FROM ".$this->options['table'].
  222. " WHERE ".$this->options['usernamecol']." = " . $this->db->Quote($username);
  223. $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  224. $rset = $this->db->Execute( $query );
  225. $res = $rset->fetchRow();
  226. if (DB::isError($res)) {
  227. return PEAR::raiseError($res->getMessage(), $res->getCode());
  228. }
  229. if (!is_array($res)) {
  230. $this->activeUser = '';
  231. return false;
  232. }
  233. if ($this->verifyPassword(trim($password, "\r\n"),
  234. trim($res[$this->options['passwordcol']], "\r\n"),
  235. $this->options['cryptType'])) {
  236. // Store additional field values in the session
  237. foreach ($res as $key => $value) {
  238. if ($key == $this->options['passwordcol'] ||
  239. $key == $this->options['usernamecol']) {
  240. continue;
  241. }
  242. // Use reference to the auth object if exists
  243. // This is because the auth session variable can change so a static call to setAuthData does not make sence
  244. if(is_object($this->_auth_obj)){
  245. $this->_auth_obj->setAuthData($key, $value);
  246. } else {
  247. Auth::setAuthData($key, $value);
  248. }
  249. }
  250. return true;
  251. }
  252. $this->activeUser = $res[$this->options['usernamecol']];
  253. return false;
  254. }
  255. // }}}
  256. // {{{ listUsers()
  257. function listUsers()
  258. {
  259. $err = $this->_prepare();
  260. if ($err !== true) {
  261. return PEAR::raiseError($err->getMessage(), $err->getCode());
  262. }
  263. $retVal = array();
  264. // Find if db_fileds contains a *, i so assume all col are selected
  265. if(strstr($this->options['db_fields'], '*')){
  266. $sql_from = "*";
  267. }
  268. else{
  269. $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
  270. }
  271. $query = sprintf("SELECT %s FROM %s",
  272. $sql_from,
  273. $this->options['table']
  274. );
  275. $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
  276. if (DB::isError($res)) {
  277. return PEAR::raiseError($res->getMessage(), $res->getCode());
  278. } else {
  279. foreach ($res as $user) {
  280. $user['username'] = $user[$this->options['usernamecol']];
  281. $retVal[] = $user;
  282. }
  283. }
  284. return $retVal;
  285. }
  286. // }}}
  287. // {{{ addUser()
  288. /**
  289. * Add user to the storage container
  290. *
  291. * @access public
  292. * @param string Username
  293. * @param string Password
  294. * @param mixed Additional information that are stored in the DB
  295. *
  296. * @return mixed True on success, otherwise error object
  297. */
  298. function addUser($username, $password, $additional = "")
  299. {
  300. if (function_exists($this->options['cryptType'])) {
  301. $cryptFunction = $this->options['cryptType'];
  302. } else {
  303. $cryptFunction = 'md5';
  304. }
  305. $additional_key = '';
  306. $additional_value = '';
  307. if (is_array($additional)) {
  308. foreach ($additional as $key => $value) {
  309. $additional_key .= ', ' . $key;
  310. $additional_value .= ", '" . $value . "'";
  311. }
  312. }
  313. $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
  314. $this->options['table'],
  315. $this->options['usernamecol'],
  316. $this->options['passwordcol'],
  317. $additional_key,
  318. $username,
  319. $cryptFunction($password),
  320. $additional_value
  321. );
  322. $res = $this->query($query);
  323. if (DB::isError($res)) {
  324. return PEAR::raiseError($res->getMessage(), $res->getCode());
  325. } else {
  326. return true;
  327. }
  328. }
  329. // }}}
  330. // {{{ removeUser()
  331. /**
  332. * Remove user from the storage container
  333. *
  334. * @access public
  335. * @param string Username
  336. *
  337. * @return mixed True on success, otherwise error object
  338. */
  339. function removeUser($username)
  340. {
  341. $query = sprintf("DELETE FROM %s WHERE %s = '%s'",
  342. $this->options['table'],
  343. $this->options['usernamecol'],
  344. $username
  345. );
  346. $res = $this->query($query);
  347. if (DB::isError($res)) {
  348. return PEAR::raiseError($res->getMessage(), $res->getCode());
  349. } else {
  350. return true;
  351. }
  352. }
  353. // }}}
  354. }
  355. function showDbg( $string ) {
  356. print "
  357. -- $string</P>";
  358. }
  359. function dump( $var, $str, $vardump = false ) {
  360. print "<H4>$str</H4><pre>";
  361. ( !$vardump ) ? ( print_r( $var )) : ( var_dump( $var ));
  362. print "</pre>";
  363. }
  364. ?>