PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/adodb/pear/Auth/Container/ADOdb.php

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