PageRenderTime 20ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/_crust/core.php

https://github.com/mattkosoy/Crust-CMS
PHP | 442 lines | 245 code | 98 blank | 99 comment | 28 complexity | e90059970fb7982a6944da6d225310df MD5 | raw file
  1. <?php
  2. /**********************************************************************
  3. * Author: Justin Vincent (justin@visunet.ie)
  4. * Web...: http://php.justinvincent.com
  5. * Name..: ezSQL
  6. * Desc..: ezSQL Core module - database abstraction library to make
  7. * it very easy to deal with databases.
  8. *
  9. */
  10. /**********************************************************************
  11. * ezSQL Constants
  12. */
  13. define('EZSQL_VERSION','2.0');
  14. define('OBJECT','OBJECT',true);
  15. define('ARRAY_A','ARRAY_A',true);
  16. define('ARRAY_N','ARRAY_N',true);
  17. define('EZSQL_CORE_ERROR','ezSQLcore can not be used by itself (it is designed for use by database specific modules).');
  18. /**********************************************************************
  19. * Core class containg common functions to manipulate query result
  20. * sets once returned
  21. */
  22. class ezSQLcore
  23. {
  24. var $trace = false; // same as $debug_all
  25. var $debug_all = false; // same as $trace
  26. var $debug_called = false;
  27. var $vardump_called = false;
  28. var $show_errors = true;
  29. var $num_queries = 0;
  30. var $last_query = null;
  31. var $last_error = null;
  32. var $col_info = null;
  33. var $captured_errors = array();
  34. /**********************************************************************
  35. * Constructor
  36. */
  37. function ezSQLcore()
  38. {
  39. }
  40. /**********************************************************************
  41. * Connect to DB - over-ridden by specific DB class
  42. */
  43. function connect()
  44. {
  45. die(EZSQL_CORE_ERROR);
  46. }
  47. /**********************************************************************
  48. * Select DB - over-ridden by specific DB class
  49. */
  50. function select()
  51. {
  52. die(EZSQL_CORE_ERROR);
  53. }
  54. /**********************************************************************
  55. * Basic Query - over-ridden by specific DB class
  56. */
  57. function query()
  58. {
  59. die(EZSQL_CORE_ERROR);
  60. }
  61. /**********************************************************************
  62. * Format a string correctly for safe insert - over-ridden by specific
  63. * DB class
  64. */
  65. function escape()
  66. {
  67. die(EZSQL_CORE_ERROR);
  68. }
  69. /**********************************************************************
  70. * Return database specific system date syntax
  71. * i.e. Oracle: SYSDATE Mysql: NOW()
  72. */
  73. function sysdate()
  74. {
  75. die(EZSQL_CORE_ERROR);
  76. }
  77. /**********************************************************************
  78. * Print SQL/DB error - over-ridden by specific DB class
  79. */
  80. function register_error($err_str)
  81. {
  82. // Keep track of last error
  83. $this->last_error = $err_str;
  84. // Capture all errors to an error array no matter what happens
  85. $this->captured_errors[] = array
  86. (
  87. 'error_str' => $err_str,
  88. 'query' => $this->last_query
  89. );
  90. }
  91. /**********************************************************************
  92. * Turn error handling on or off..
  93. */
  94. function show_errors()
  95. {
  96. $this->show_errors = true;
  97. }
  98. function hide_errors()
  99. {
  100. $this->show_errors = false;
  101. }
  102. /**********************************************************************
  103. * Kill cached query results
  104. */
  105. function flush()
  106. {
  107. // Get rid of these
  108. $this->last_result = null;
  109. $this->col_info = null;
  110. $this->last_query = null;
  111. }
  112. /**********************************************************************
  113. * Get one variable from the DB - see docs for more detail
  114. */
  115. function get_var($query=null,$x=0,$y=0)
  116. {
  117. // Log how the function was called
  118. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  119. // If there is a query then perform it if not then use cached results..
  120. if ( $query )
  121. {
  122. $this->query($query);
  123. }
  124. // Extract var out of cached results based x,y vals
  125. if ( $this->last_result[$y] )
  126. {
  127. $values = array_values(get_object_vars($this->last_result[$y]));
  128. }
  129. // If there is a value return it else return null
  130. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  131. }
  132. /**********************************************************************
  133. * Get one row from the DB - see docs for more detail
  134. */
  135. function get_row($query=null,$output=OBJECT,$y=0)
  136. {
  137. // Log how the function was called
  138. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  139. // If there is a query then perform it if not then use cached results..
  140. if ( $query )
  141. {
  142. $this->query($query);
  143. }
  144. // If the output is an object then return object using the row offset..
  145. if ( $output == OBJECT )
  146. {
  147. return $this->last_result[$y]?$this->last_result[$y]:null;
  148. }
  149. // If the output is an associative array then return row as such..
  150. elseif ( $output == ARRAY_A )
  151. {
  152. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  153. }
  154. // If the output is an numerical array then return row as such..
  155. elseif ( $output == ARRAY_N )
  156. {
  157. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  158. }
  159. // If invalid output type was specified..
  160. else
  161. {
  162. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  163. }
  164. }
  165. /**********************************************************************
  166. * Function to get 1 column from the cached result set based in X index
  167. * see docs for usage and info
  168. */
  169. function get_col($query=null,$x=0)
  170. {
  171. // If there is a query then perform it if not then use cached results..
  172. if ( $query )
  173. {
  174. $this->query($query);
  175. }
  176. // Extract the column values
  177. for ( $i=0; $i < count($this->last_result); $i++ )
  178. {
  179. $new_array[$i] = $this->get_var(null,$x,$i);
  180. }
  181. return $new_array;
  182. }
  183. /**********************************************************************
  184. * Return the the query as a result set - see docs for more details
  185. */
  186. function get_results($query=null, $output = OBJECT)
  187. {
  188. // Log how the function was called
  189. $this->func_call = "\$db->get_results(\"$query\", $output)";
  190. // If there is a query then perform it if not then use cached results..
  191. if ( $query )
  192. {
  193. $this->query($query);
  194. }
  195. // Send back array of objects. Each row is an object
  196. if ( $output == OBJECT )
  197. {
  198. return $this->last_result;
  199. }
  200. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  201. {
  202. if ( $this->last_result )
  203. {
  204. $i=0;
  205. foreach( $this->last_result as $row )
  206. {
  207. $new_array[$i] = get_object_vars($row);
  208. if ( $output == ARRAY_N )
  209. {
  210. $new_array[$i] = array_values($new_array[$i]);
  211. }
  212. $i++;
  213. }
  214. return $new_array;
  215. }
  216. else
  217. {
  218. return null;
  219. }
  220. }
  221. }
  222. /**********************************************************************
  223. * Function to get column meta data info pertaining to the last query
  224. * see docs for more info and usage
  225. */
  226. function get_col_info($info_type="name",$col_offset=-1)
  227. {
  228. if ( $this->col_info )
  229. {
  230. if ( $col_offset == -1 )
  231. {
  232. $i=0;
  233. foreach($this->col_info as $col )
  234. {
  235. $new_array[$i] = $col->{$info_type};
  236. $i++;
  237. }
  238. return $new_array;
  239. }
  240. else
  241. {
  242. return $this->col_info[$col_offset]->{$info_type};
  243. }
  244. }
  245. }
  246. /**********************************************************************
  247. * Dumps the contents of any input variable to screen in a nicely
  248. * formatted and easy to understand way - any type: Object, Var or Array
  249. */
  250. function vardump($mixed='')
  251. {
  252. echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  253. echo "<pre><font face=arial>";
  254. if ( ! $this->vardump_called )
  255. {
  256. echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  257. }
  258. $var_type = gettype ($mixed);
  259. print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
  260. echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  261. echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  262. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  263. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  264. echo "</font></pre></font></blockquote></td></tr></table>".$this->donation();
  265. echo "\n<hr size=1 noshade color=dddddd>";
  266. $this->vardump_called = true;
  267. }
  268. /**********************************************************************
  269. * Alias for the above function
  270. */
  271. function dumpvar($mixed)
  272. {
  273. $this->vardump($mixed);
  274. }
  275. /**********************************************************************
  276. * Displays the last query string that was sent to the database & a
  277. * table listing results (if there were any).
  278. * (abstracted into a seperate file to save server overhead).
  279. */
  280. function debug()
  281. {
  282. echo "<blockquote>";
  283. // Only show ezSQL credits once..
  284. if ( ! $this->debug_called )
  285. {
  286. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  287. }
  288. if ( $this->last_error )
  289. {
  290. echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->last_error</b></font>]<p>";
  291. }
  292. echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  293. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  294. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  295. echo "<blockquote>";
  296. if ( $this->col_info )
  297. {
  298. // =====================================================
  299. // Results top rows
  300. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  301. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  302. for ( $i=0; $i < count($this->col_info); $i++ )
  303. {
  304. echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>{$this->col_info[$i]->name}</span></td>";
  305. }
  306. echo "</tr>";
  307. // ======================================================
  308. // print main results
  309. if ( $this->last_result )
  310. {
  311. $i=0;
  312. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  313. {
  314. $i++;
  315. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  316. foreach ( $one_row as $item )
  317. {
  318. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  319. }
  320. echo "</tr>";
  321. }
  322. } // if last result
  323. else
  324. {
  325. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  326. }
  327. echo "</table>";
  328. } // if col_info
  329. else
  330. {
  331. echo "<font face=arial size=2>No Results</font>";
  332. }
  333. echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
  334. $this->debug_called = true;
  335. }
  336. /**********************************************************************
  337. * Naughty little function to ask for some remuniration!
  338. */
  339. function donation()
  340. {
  341. return "<font size=1 face=arial color=000000>If ezSQL has helped <a href=\"https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0\" style=\"color: 0000CC;\">make a donation!?</a> &nbsp;&nbsp;<!--[ go on! you know you want to! ]--></font>";
  342. }
  343. }
  344. ?>