PageRenderTime 34ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/lib/lib_db.php

http://pixie-cms.googlecode.com/
PHP | 432 lines | 349 code | 4 blank | 79 comment | 75 complexity | 0d728e74f31373b0aed80eef58546bec MD5 | raw file
  1. <?php
  2. if (!defined('DIRECT_ACCESS')) {
  3. header('Location: ../../');
  4. exit();
  5. }
  6. /**
  7. * Pixie: The Small, Simple, Site Maker.
  8. *
  9. * Licence: GNU General Public License v3
  10. * Copyright (C) 2010, Scott Evans
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see http://www.gnu.org/licenses/
  24. *
  25. * Title: lib_db - Class to interface with MySQL DB
  26. *
  27. * @package Pixie
  28. * @copyright 2008-2010 Scott Evans
  29. * @author Scott Evans
  30. * @author Sam Collett
  31. * @author Tony White
  32. * @author Isa Worcs
  33. * @author Dean Allen
  34. * @link http://www.getpixie.co.uk
  35. * @link http://textpattern.com
  36. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3
  37. *
  38. */
  39. //------------------------------------------------------------------
  40. if (!empty($pixieconfig['table_prefix'])) {
  41. define('PFX', $pixieconfig['table_prefix']);
  42. } else {
  43. if (!defined('PFX')) {
  44. define('PFX', '');
  45. }
  46. }
  47. //------------------------------------------------------------------
  48. function adjust_prefix($table) {
  49. if (stripos($table, PFX) === 0)
  50. return $table;
  51. else
  52. return PFX . $table;
  53. }
  54. //------------------------------------------------------------------
  55. function safe_query($q = '', $debug = '', $unbuf = '') {
  56. global $DB, $pixieconfig, $message, $dst, $tzHM, $timezone;
  57. $method = (!$unbuf) ? 'mysql_query' : 'mysql_unbuffered_query';
  58. if (!$q)
  59. return FALSE;
  60. if ($debug) {
  61. $message = 'MySQL Query: ' . $q . '<br/>MySQL Error : ' . mysql_error() . "";
  62. }
  63. if (!isset($tzHM)) {
  64. /* Get time zone */
  65. if (defined('TZ')) {
  66. $tz = TZ;
  67. } else {
  68. if (isset($timezone)) {
  69. $tz = $timezone;
  70. }
  71. }
  72. // calculate hours from the TZ constant - works only if time zone set as +3600, +7200 etc
  73. if (is_numeric($tz)) {
  74. $hours = ($tz / 3600 % 3600);
  75. $mins = ($tz % 3600 / 60);
  76. // if daylight saving time
  77. if ($dst == 'yes' && date('I') != 0) {
  78. $hours++;
  79. }
  80. // if $hours < 0 then prepend -, otherwise prepend +
  81. $tzHM = (($hours < 0) ? "-" : "+") . "$hours:$mins";
  82. $method("SET SESSION time_zone='$tzHM'", $DB->link);
  83. }
  84. }
  85. if ((isset($q)) && ($q != '') && ($q !== NULL)) {
  86. $result = $method($q, $DB->link);
  87. }
  88. if ((isset($result)) && ($result)) {
  89. // if (strnatcmp(phpversion(),'5.0.0') >= 0) {
  90. // $test_resource = $result;
  91. // if ( (is_resource($test_resource)) && ($unbuf != 'mysql_unbuffered_query') ) {
  92. // if ( (first_word($q) == 'SELECT' or 'SHOW' or 'EXPLAIN' or 'DESCRIBE') ) {
  93. // mysql_free_result( mysql_query($q) );
  94. // }
  95. // }
  96. // } /* Don't need to do this for php 4 */ /* While this works, something else is causing an Unknown: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0 error */
  97. return $result;
  98. } else {
  99. return FALSE;
  100. }
  101. }
  102. //------------------------------------------------------------------
  103. function safe_delete($table, $where, $debug = '') {
  104. $table = adjust_prefix($table);
  105. $q = "delete from $table where $where";
  106. if ($r = safe_query($q, $debug)) {
  107. return TRUE;
  108. } else {
  109. return FALSE;
  110. }
  111. }
  112. //------------------------------------------------------------------
  113. function safe_update($table, $set, $where, $debug = '') {
  114. $table = adjust_prefix($table);
  115. $q = "update $table set $set where $where";
  116. if ($r = safe_query($q, $debug)) {
  117. return TRUE;
  118. } else {
  119. return FALSE;
  120. }
  121. }
  122. //------------------------------------------------------------------
  123. function safe_insert($table, $set, $debug = '') {
  124. global $DB;
  125. $table = adjust_prefix($table);
  126. $q = "insert into $table set $set";
  127. if ($r = safe_query($q, $debug)) {
  128. $id = mysql_insert_id($DB->link);
  129. return ($id === 0 ? TRUE : $id);
  130. }
  131. return FALSE;
  132. }
  133. //------------------------------------------------------------------
  134. function safe_alter($table, $alter, $debug = '') {
  135. $table = adjust_prefix($table);
  136. $q = "alter table $table $alter";
  137. if ($r = safe_query($q, $debug)) {
  138. return TRUE;
  139. }
  140. return FALSE;
  141. }
  142. //------------------------------------------------------------------
  143. function safe_optimize($table, $debug = '') {
  144. $table = adjust_prefix($table);
  145. $q = "optimize table $table";
  146. if ($r = safe_query($q, $debug)) {
  147. return TRUE;
  148. }
  149. return FALSE;
  150. }
  151. //------------------------------------------------------------------
  152. function safe_repair($table, $debug = '') {
  153. $table = adjust_prefix($table);
  154. $q = "repair table $table";
  155. if ($r = safe_query($q, $debug)) {
  156. return TRUE;
  157. }
  158. return FALSE;
  159. }
  160. //------------------------------------------------------------------
  161. function safe_field($thing, $table, $where, $debug = '') {
  162. $table = adjust_prefix($table);
  163. $q = "select $thing from $table where $where";
  164. $r = safe_query($q, $debug);
  165. if (@mysql_num_rows($r) > 0) {
  166. return mysql_result($r, 0);
  167. }
  168. return FALSE;
  169. }
  170. //------------------------------------------------------------------
  171. function safe_column($thing, $table, $where, $debug = '') {
  172. $table = adjust_prefix($table);
  173. $q = "select $thing from $table where $where";
  174. $rs = getRows($q, $debug);
  175. if ($rs) {
  176. foreach ($rs as $a) {
  177. $v = array_shift($a);
  178. $out[$v] = $v;
  179. }
  180. return $out;
  181. }
  182. return array();
  183. }
  184. //------------------------------------------------------------------
  185. function safe_row($things, $table, $where, $debug = '') {
  186. $table = adjust_prefix($table);
  187. $q = "select $things from $table where $where";
  188. $rs = getRow($q, $debug);
  189. if ($rs) {
  190. return $rs;
  191. }
  192. return array();
  193. }
  194. //------------------------------------------------------------------
  195. function safe_rows($things, $table, $where, $debug = '') {
  196. $table = adjust_prefix($table);
  197. $q = "select $things from $table where $where";
  198. $rs = getRows($q, $debug);
  199. if ($rs) {
  200. return $rs;
  201. }
  202. return array();
  203. }
  204. //------------------------------------------------------------------
  205. function safe_rows_start($things, $table, $where, $debug = '') {
  206. $table = adjust_prefix($table);
  207. $q = "select $things from $table where $where";
  208. return startRows($q, $debug);
  209. }
  210. //------------------------------------------------------------------
  211. function safe_count($table, $where, $debug = '') {
  212. $table = adjust_prefix($table);
  213. return getThing("select count(*) from $table where $where", $debug);
  214. }
  215. //------------------------------------------------------------------
  216. function fetch($col, $table, $key, $val, $debug = '') {
  217. $table = adjust_prefix($table);
  218. $q = "select $col from $table where `$key` = '$val' limit 1";
  219. if ($r = safe_query($q, $debug)) {
  220. return (mysql_num_rows($r) > 0) ? mysql_result($r, 0) : '';
  221. }
  222. return FALSE;
  223. }
  224. //------------------------------------------------------------------
  225. function getRow($query, $debug = '') {
  226. if ($r = safe_query($query, $debug)) {
  227. return (mysql_num_rows($r) > 0) ? mysql_fetch_assoc($r) : FALSE;
  228. }
  229. return FALSE;
  230. }
  231. //------------------------------------------------------------------
  232. function getRows($query, $debug = '') {
  233. if ($r = safe_query($query, $debug)) {
  234. if (mysql_num_rows($r) > 0) {
  235. while ($a = mysql_fetch_assoc($r))
  236. $out[] = $a;
  237. return $out;
  238. }
  239. }
  240. return FALSE;
  241. }
  242. //------------------------------------------------------------------
  243. function startRows($query, $debug = '') {
  244. return safe_query($query, $debug);
  245. }
  246. //------------------------------------------------------------------
  247. function nextRow($r) {
  248. return mysql_fetch_assoc($r);
  249. }
  250. //------------------------------------------------------------------
  251. function getThing($query, $debug = '') {
  252. if ($r = safe_query($query, $debug)) {
  253. return (mysql_num_rows($r) != 0) ? mysql_result($r, 0) : '';
  254. }
  255. return FALSE;
  256. }
  257. //------------------------------------------------------------------
  258. function getThings($query, $debug = '')
  259. // return values of one column from multiple rows in an num indexed array
  260. {
  261. $rs = getRows($query, $debug);
  262. if ($rs) {
  263. foreach ($rs as $a)
  264. $out[] = array_shift($a);
  265. return $out;
  266. }
  267. return array();
  268. }
  269. //------------------------------------------------------------------
  270. function getCount($table, $where, $debug = '') {
  271. $table = adjust_prefix($table);
  272. return getThing("select count(*) from $table where $where", $debug);
  273. }
  274. //------------------------------------------------------------------
  275. function get_prefs() {
  276. $r = safe_row('*', 'pixie_settings', 'settings_id = 1');
  277. if ($r) {
  278. return $r;
  279. }
  280. return FALSE;
  281. }
  282. //------------------------------------------------------------------
  283. // Creates a drop down menu box from a db
  284. function db_dropdown($table, $current, $name, $condition) {
  285. global $edit, $go;
  286. $table = adjust_prefix($table);
  287. $rs = safe_query("select * from $table where $condition");
  288. $num = mysql_num_rows($rs);
  289. $i = 0;
  290. echo "\t\t\t\t\t\t\t\t<select class=\"form_select\" name=\"$name\" id=\"$name\">\n";
  291. if ((!$current) && (isset($go)) && ($go == 'new')) {
  292. echo "\t\t\t\t\t\t\t\t\t<option selected=\"selected\" value=\"NULL\">-</option>\n";
  293. } else if (($current === NULL) && (isset($edit)) && ($edit)) {
  294. echo "\t\t\t\t\t\t\t\t\t<option selected=\"selected\" value=\"NULL\">-</option>\n";
  295. } else if ((isset($edit)) && ($edit)) {
  296. echo "\t\t\t\t\t\t\t\t\t<option value=\"NULL\">-</option>\n";
  297. }
  298. while ($i < $num) {
  299. $F = mysql_fetch_array($rs);
  300. for ($j = 0; $j < mysql_num_fields($rs); $j++) {
  301. if (last_word(mysql_field_name($rs, $j)) == 'id') {
  302. $id = simplify($F[$j]);
  303. } else {
  304. $fieldname = $F[1];
  305. }
  306. }
  307. if ($current == $id) {
  308. print "\t\t\t\t\t\t\t\t\t<option selected=\"selected\" value=\"$id\">$fieldname</option>\n";
  309. } else {
  310. print "\t\t\t\t\t\t\t\t\t<option value=\"$id\">$fieldname</option>\n";
  311. }
  312. $i++;
  313. }
  314. echo "\t\t\t\t\t\t\t\t</select>";
  315. }
  316. //------------------------------------------------------------------
  317. function table_exists($table_name) {
  318. if (isset($table_name)) {
  319. $table_name = adjust_prefix($table_name);
  320. $rs = safe_query("select * from $table_name WHERE 1=0");
  321. }
  322. if ((isset($rs)) && ($rs)) {
  323. return TRUE;
  324. } else {
  325. return FALSE;
  326. }
  327. }
  328. //------------------------------------------------------------------
  329. function getSqlVersion() {
  330. $output = @shell_exec('mysql -V');
  331. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  332. if (isset($version[0])) {
  333. return $version[0];
  334. } else {
  335. return FALSE;
  336. }
  337. }
  338. //------------------------------------------------------------------
  339. class DB {
  340. function getTzdiff() {
  341. if (strnatcmp(phpversion(), '5.1.0') <= 0) {
  342. extract(getdate());
  343. $serveroffset = gmmktime(0, 0, 0, $mon, $mday, $year) - mktime(0, 0, 0, $mon, $mday, $year);
  344. return $serveroffset / 3600;
  345. } else {
  346. return 0;
  347. }
  348. }
  349. function DB() {
  350. global $pixieconfig;
  351. $this->host = $pixieconfig['host'];
  352. $this->db = $pixieconfig['db'];
  353. $this->user = $pixieconfig['user'];
  354. $this->pass = $pixieconfig['pass'];
  355. $this->link = mysql_connect($this->host, $this->user, $this->pass);
  356. if (!$this->link) {
  357. $GLOBALS['connected'] = FALSE;
  358. } else
  359. $GLOBALS['connected'] = TRUE;
  360. mysql_select_db($this->db) or die(db_down());
  361. /* Connect to the database */
  362. if ((isset($pixieconfig['site_charset'])) && ($pixieconfig['site_charset'])) {
  363. $charset = strtolower(str_replace('-', '', $pixieconfig['site_charset']));
  364. $query_names = "SET NAMES '{$charset}'";
  365. $set_db_names_charset = mysql_query($query_names);
  366. /* Set the name character set for database connection */
  367. $query_char = "SET CHARACTER SET '{$charset}'";
  368. $set_db_charset = mysql_query($query_char);
  369. /* Set the character set for database connection */
  370. }
  371. $diff = $this->getTzdiff();
  372. if ($diff >= 0)
  373. $diff = "+{$diff}";
  374. mysql_query("set time_zone = '" . "$diff:00'");
  375. }
  376. }
  377. $DB = new DB;
  378. //------------------------------------------------------------------
  379. function db_down()
  380. {
  381. header('Status: 503 Service Unavailable'); /* 503 status might discourage search engines from indexing or caching the error message */
  382. return <<<eod
  383. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  384. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  385. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  386. <head>
  387. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  388. <title>Pixie (www.getpixie.co.uk) - Unable to connect to database</title>
  389. <style type="text/css">
  390. body
  391. {
  392. font-family: Arial, 'Lucida Grande', Verdana, Sans-Serif;
  393. color: #333;
  394. }
  395. a, a:visited
  396. {
  397. text-decoration: none;
  398. color: #0497d3;
  399. }
  400. a:hover
  401. {
  402. color: #191919;
  403. text-decoration: none;
  404. }
  405. .helper
  406. {
  407. position: relative;
  408. top: 60px;
  409. border: 5px solid #e1e1e1;
  410. clear: left;
  411. padding: 15px 30px;
  412. margin: 0 auto;
  413. background-color: #F0F0F0;
  414. width: 500px;
  415. line-height: 15pt;
  416. }
  417. </style>
  418. </head>
  419. <body>
  420. <div class="helper">
  421. <h3>Database Unavailable</h3><p><a href="http://www.getpixie.co.uk" alt="Get Pixie!">Pixie</a> has not been able to display the website your are visiting as a database connection could not be established. Try to visit the site again in a few moments.</p>
  422. </div>
  423. </body>
  424. </html>
  425. eod;
  426. exit();
  427. }
  428. ?>