PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/pear/LiveUser/Admin/Storage/DB.php

http://punchcms.googlecode.com/
PHP | 384 lines | 137 code | 17 blank | 230 comment | 16 complexity | 9e5290a0ad248994770c600908db5afc MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A framework for authentication and authorization in PHP applications
  5. *
  6. * LiveUser_Admin is meant to be used with the LiveUser package.
  7. * It is composed of all the classes necessary to administrate
  8. * data used by LiveUser.
  9. *
  10. * You'll be able to add/edit/delete/get things like:
  11. * * Rights
  12. * * Users
  13. * * Groups
  14. * * Areas
  15. * * Applications
  16. * * Subgroups
  17. * * ImpliedRights
  18. *
  19. * And all other entities within LiveUser.
  20. *
  21. * At the moment we support the following storage containers:
  22. * * DB
  23. * * MDB
  24. * * MDB2
  25. *
  26. * But it takes no time to write up your own storage container,
  27. * so if you like to use native mysql functions straight, then it's possible
  28. * to do so in under a hour!
  29. *
  30. * PHP version 4 and 5
  31. *
  32. * LICENSE: This library is free software; you can redistribute it and/or
  33. * modify it under the terms of the GNU Lesser General Public
  34. * License as published by the Free Software Foundation; either
  35. * version 2.1 of the License, or (at your option) any later version.
  36. *
  37. * This library is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  40. * Lesser General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU Lesser General Public
  43. * License along with this library; if not, write to the Free Software
  44. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  45. * MA 02111-1307 USA
  46. *
  47. *
  48. * @category authentication
  49. * @package LiveUser_Admin
  50. * @author Markus Wolff <wolff@21st.de>
  51. * @author Helgi Ţormar Ţorbjörnsson <dufuz@php.net>
  52. * @author Lukas Smith <smith@pooteeweet.org>
  53. * @author Arnaud Limbourg <arnaud@php.net>
  54. * @author Christian Dickmann <dickmann@php.net>
  55. * @author Matt Scifo <mscifo@php.net>
  56. * @author Bjoern Kraus <krausbn@php.net>
  57. * @copyright 2002-2006 Markus Wolff
  58. * @license http://www.gnu.org/licenses/lgpl.txt
  59. * @version CVS: $Id: DB.php,v 1.26 2006/05/25 08:20:34 lsmith Exp $
  60. * @link http://pear.php.net/LiveUser_Admin
  61. */
  62. /**
  63. * Require parent class definition.
  64. */
  65. require_once 'LiveUser/Admin/Storage/SQL.php';
  66. require_once 'DB.php';
  67. /**
  68. * This is a PEAR::DB backend driver for the LiveUser class.
  69. * A PEAR::DB connection object can be passed to the constructor to reuse an
  70. * existing connection. Alternatively, a DSN can be passed to open a new one.
  71. *
  72. * Requirements:
  73. * - File "Liveuser/Admin.php" (contains the parent class "LiveUser_Admin")
  74. * - Array of connection options or a PEAR::DB connection object must be
  75. * passed to the init() method
  76. * Example: array('dsn' => 'mysql://user:pass@host/db_name')
  77. * OR
  78. * array('dbc' => &$conn) ($conn is a PEAR::DB connection object)
  79. *
  80. * @category authentication
  81. * @package LiveUser_Admin
  82. * @author Lukas Smith <smith@pooteeweet.org>
  83. * @author Bjoern Kraus <krausbn@php.net>
  84. * @copyright 2002-2006 Markus Wolff
  85. * @license http://www.gnu.org/licenses/lgpl.txt
  86. * @version Release: @package_version@
  87. * @link http://pear.php.net/LiveUser_Admin
  88. */
  89. class LiveUser_Admin_Storage_DB extends LiveUser_Admin_Storage_SQL
  90. {
  91. /**
  92. * Initializes database storage container.
  93. * Connects to database or uses existing database connection.
  94. *
  95. * @param array Storage Configuration
  96. * @param array containing the database structure (tables, fields, alias)
  97. * @return bool true on success and false on failure
  98. *
  99. * @access public
  100. */
  101. function init(&$storageConf, $structure)
  102. {
  103. parent::init($storageConf, $structure);
  104. if (!is_a($this->dbc, 'db_common') && !is_null($this->dsn)) {
  105. $this->options['portability'] = DB_PORTABILITY_ALL;
  106. $dbc =& DB::connect($this->dsn, $this->options);
  107. if (PEAR::isError($dbc)) {
  108. $this->stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error',
  109. array('container' => 'could not connect: '.$dbc->getMessage(),
  110. 'debug' => $dbc->getUserInfo()));
  111. return false;
  112. }
  113. $this->dbc =& $dbc;
  114. }
  115. if (!is_a($this->dbc, 'db_common')) {
  116. $this->stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error',
  117. array('container' => 'storage layer configuration missing'));
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Convert a text value into a DBMS specific format that is suitable to
  124. * compose query statements.
  125. *
  126. * @param string text string value that is intended to be converted.
  127. * @param string type to which the value should be converted to
  128. * @return stringtext string that represents the given argument value in
  129. * a DBMS specific format.
  130. *
  131. * @access public
  132. * @uses DB::quoteSmart
  133. */
  134. function quote($value, $type)
  135. {
  136. return $this->dbc->quoteSmart($value);
  137. }
  138. /**
  139. * Apply a type to all values of an array and return as a comma
  140. * seperated string useful for generating IN statements
  141. *
  142. * @param array data array
  143. * @param string determines type of the field
  144. * @return string comma seperated values
  145. *
  146. * @access public
  147. * @uses DB::quoteSmart
  148. */
  149. function implodeArray($array, $type)
  150. {
  151. if (!is_array($array) || empty($array)) {
  152. return 'NULL';
  153. }
  154. foreach ($array as $value) {
  155. $return[] = $this->dbc->quoteSmart($value);
  156. }
  157. return implode(', ', $return);
  158. }
  159. /**
  160. * This function is not implemented into DB so we
  161. * can't make use of it.
  162. *
  163. * @param string number of rows to select
  164. * @param string first row to select
  165. *
  166. * @return bool false This feature isn't supported by DB
  167. *
  168. * @access public
  169. */
  170. function setLimit($limit, $offset)
  171. {
  172. if ($limit || $offset) {
  173. $this->stack->push(
  174. LIVEUSER_ADMIN_ERROR_FILTER, 'exception',
  175. array('msg' => 'limit is not supported by this backend')
  176. );
  177. return false;
  178. }
  179. }
  180. /**
  181. * Execute DML query
  182. *
  183. * @param string DML query
  184. * @return bool|int of the affected rows
  185. *
  186. * @access public
  187. * @uses DB::query DB::affectedRows
  188. */
  189. function exec($query)
  190. {
  191. $result = $this->dbc->query($query);
  192. if (PEAR::isError($result)) {
  193. $this->stack->push(
  194. LIVEUSER_ADMIN_ERROR_QUERY_BUILDER, 'exception',
  195. array('reason' => $result->getMessage() . '-' . $result->getUserInfo())
  196. );
  197. return false;
  198. }
  199. return $this->dbc->affectedRows();
  200. }
  201. /**
  202. * Execute the specified query, fetch the value from the first column of
  203. * the first row of the result set and then frees the result set.
  204. *
  205. * @param string the SELECT query statement to be executed.
  206. * @param string argument that specifies the expected datatype of the
  207. * result set field, so that an eventual conversion may be performed.
  208. * The default datatype is text, meaning no conversion is performed.
  209. * @return bool|string
  210. *
  211. * @access public
  212. * @uses DB::getOne
  213. */
  214. function queryOne($query, $type)
  215. {
  216. $result = $this->dbc->getOne($query);
  217. if (PEAR::isError($result)) {
  218. $this->stack->push(
  219. LIVEUSER_ADMIN_ERROR_QUERY_BUILDER, 'exception',
  220. array('reason' => $result->getMessage() . '-' . $result->getUserInfo())
  221. );
  222. return false;
  223. }
  224. return $result;
  225. }
  226. /**
  227. * Execute the specified query, fetch the values from the first
  228. * row of the result set into an array and then frees
  229. * the result set.
  230. *
  231. * @param string the SELECT query statement to be executed.
  232. * @param array argument that specifies a list of expected datatypes
  233. * of theresult set columns, so that the conversions may be performed.
  234. * The default datatype is text, meaning no conversion is performed.
  235. * @return bool|array
  236. *
  237. * @access public
  238. * @uses DB::getRow
  239. */
  240. function queryRow($query, $type)
  241. {
  242. $result = $this->dbc->getRow($query, null, DB_FETCHMODE_ASSOC);
  243. if (PEAR::isError($result)) {
  244. $this->stack->push(
  245. LIVEUSER_ADMIN_ERROR_QUERY_BUILDER, 'exception',
  246. array('reason' => $result->getMessage() . '-' . $result->getUserInfo())
  247. );
  248. return false;
  249. }
  250. return $result;
  251. }
  252. /**
  253. * Execute the specified query, fetch the value from the first column of
  254. * each row of the result set into an array and then frees the result set.
  255. *
  256. * @param string the SELECT query statement to be executed.
  257. * @param string argument that specifies the expected datatype of the
  258. * result set field, so that an eventual conversion may be performed.
  259. * The default datatype is text, meaning no conversion is performed.
  260. * @return bool|array
  261. *
  262. * @access public
  263. * @uses DB::getCol
  264. */
  265. function queryCol($query, $type)
  266. {
  267. $result = $this->dbc->getCol($query);
  268. if (PEAR::isError($result)) {
  269. $this->stack->push(
  270. LIVEUSER_ADMIN_ERROR_QUERY_BUILDER, 'exception',
  271. array('reason' => $result->getMessage() . '-' . $result->getUserInfo())
  272. );
  273. return false;
  274. }
  275. return $result;
  276. }
  277. /**
  278. * Execute the specified query, fetch all the rows of the result set into
  279. * a two dimensional array and then frees the result set.
  280. *
  281. * @param string the SELECT query statement to be executed.
  282. * @param array argument that specifies a list of expected datatypes
  283. * of theresult set columns, so that the conversions may be performed.
  284. * The default datatype is text, meaning no conversion is performed.
  285. * @param bool if set to true, returned array will have the first
  286. * column as its first dimension
  287. * @param bool if set to true and $rekey is set to true, then
  288. * all values with the same first column will be wrapped in an array
  289. * @return bool|array
  290. *
  291. * @access public
  292. * @uses DB::getAll DB::getAssoc
  293. */
  294. function queryAll($query, $types, $rekey, $group)
  295. {
  296. if ($rekey) {
  297. $result = $this->dbc->getAssoc($query, false, array(), DB_FETCHMODE_ASSOC, $group);
  298. } else {
  299. $result = $this->dbc->getAll($query, array(), DB_FETCHMODE_ASSOC);
  300. }
  301. if (PEAR::isError($result)) {
  302. $this->stack->push(
  303. LIVEUSER_ADMIN_ERROR_QUERY_BUILDER, 'exception',
  304. array('reason' => $result->getMessage() . '-' . $result->getUserInfo())
  305. );
  306. return false;
  307. }
  308. return $result;
  309. }
  310. /**
  311. * returns the next free id of a sequence
  312. *
  313. * @param string name of the sequence
  314. * @param bool when true the seqence is
  315. * automatic created, if it not exists
  316. * @return bool|int false on failure or next id for the table
  317. *
  318. * @access public
  319. * @uses DB::nextId
  320. */
  321. function nextId($seqname, $ondemand = true)
  322. {
  323. $result = $this->dbc->nextId($seqname, $ondemand);
  324. if (PEAR::isError($result)) {
  325. $this->stack->push(
  326. LIVEUSER_ADMIN_ERROR_QUERY_BUILDER, 'exception',
  327. array('reason' => $result->getMessage() . '-' . $result->getUserInfo())
  328. );
  329. return false;
  330. }
  331. return $result;
  332. }
  333. /**
  334. * Since DB does not support determining if auto increment is supported,
  335. * the call is redirected to nextID()
  336. *
  337. * @param string name of the table into which a new row was inserted
  338. * @param string name of the field into which a new row was inserted
  339. * @param bool when true the seqence is automatic created, if it not exists
  340. * @return bool|int
  341. *
  342. * @access public
  343. * @uses MDB2::nextId MDB2_Extended::getBeforeId
  344. */
  345. function getBeforeId($table, $field, $ondemand = true)
  346. {
  347. $seq = $table.(empty($field) ? '' : '_'.$field);
  348. return $this->nextId($seq, $ondemand);
  349. }
  350. /**
  351. * Since DB does not support determining if auto increment is supported,
  352. * the call just returns the $id parameter
  353. *
  354. * @param string value as returned by getBeforeId()
  355. * @param string name of the table into which a new row was inserted
  356. * @param string name of the field into which a new row was inserted
  357. * @return bool|int returns the id that the users passed via params
  358. *
  359. * @access public
  360. * @uses MDB2_Extended::getAfterId
  361. */
  362. function getAfterId($id, $table, $field)
  363. {
  364. return $id;
  365. }
  366. }
  367. ?>