[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[445] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[445] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[445] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[1] | 24 | * DB.inc.php |
---|
| 25 | * |
---|
[136] | 26 | * Very lightweight DB semi-abstraction layer. Mainly to catch errors with mysql_query, with some goodies. |
---|
[1] | 27 | * |
---|
| 28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[136] | 29 | * @version 2.1 |
---|
[1] | 30 | */ |
---|
[42] | 31 | |
---|
[1] | 32 | class DB { |
---|
| 33 | |
---|
[136] | 34 | // If $db->connect has successfully opened a db connection. |
---|
[439] | 35 | private $_connected = false; |
---|
[1] | 36 | |
---|
[136] | 37 | // Database handle. |
---|
[439] | 38 | public $dbh; |
---|
[445] | 39 | |
---|
[172] | 40 | // Count how many queries run during the whole instance. |
---|
[439] | 41 | private $_query_count = 0; |
---|
[1] | 42 | |
---|
| 43 | // Hash of DB parameters. |
---|
[439] | 44 | private $_params = array(); |
---|
[1] | 45 | |
---|
| 46 | // Default parameters. |
---|
[439] | 47 | private $_param_defaults = array( |
---|
[1] | 48 | |
---|
| 49 | // DB passwords should be set as apache environment variables in httpd.conf, readable only by root. |
---|
| 50 | 'db_server' => 'localhost', |
---|
| 51 | 'db_name' => null, |
---|
| 52 | 'db_user' => null, |
---|
| 53 | 'db_pass' => null, |
---|
| 54 | |
---|
[231] | 55 | // Display all SQL queries. FALSE recommended for production sites. |
---|
[136] | 56 | 'db_always_debug' => false, |
---|
| 57 | |
---|
[231] | 58 | // Display db errors. FALSE recommended for production sites. |
---|
[136] | 59 | 'db_debug' => false, |
---|
[445] | 60 | |
---|
[231] | 61 | // Script stops on db error. TRUE recommended for production sites. |
---|
| 62 | 'db_die_on_failure' => true, |
---|
[1] | 63 | ); |
---|
[42] | 64 | |
---|
[1] | 65 | // Translate between HTML and MySQL character set names. |
---|
[439] | 66 | public $mysql_character_sets = array( |
---|
[1] | 67 | 'utf-8' => 'utf8', |
---|
| 68 | 'iso-8859-1' => 'latin1', |
---|
| 69 | ); |
---|
[42] | 70 | |
---|
[1] | 71 | // Caches. |
---|
[445] | 72 | private $existing_tables; |
---|
| 73 | private $table_columns; |
---|
[42] | 74 | |
---|
[1] | 75 | /** |
---|
| 76 | * This method enforces the singleton pattern for this class. |
---|
| 77 | * |
---|
[136] | 78 | * @return object Reference to the global DB object. |
---|
[1] | 79 | * @access public |
---|
| 80 | * @static |
---|
| 81 | */ |
---|
[439] | 82 | public static function &getInstance() |
---|
[1] | 83 | { |
---|
| 84 | static $instance = null; |
---|
| 85 | |
---|
| 86 | if ($instance === null) { |
---|
| 87 | $instance = new DB(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return $instance; |
---|
| 91 | } |
---|
[42] | 92 | |
---|
[1] | 93 | /** |
---|
| 94 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 95 | * |
---|
| 96 | * @access public |
---|
| 97 | * |
---|
| 98 | * @param array $params Array of parameters (key => val pairs). |
---|
| 99 | */ |
---|
[439] | 100 | public function setParam($params) |
---|
[1] | 101 | { |
---|
[136] | 102 | $app =& App::getInstance(); |
---|
[445] | 103 | |
---|
[1] | 104 | if (isset($params) && is_array($params)) { |
---|
| 105 | // Merge new parameters with old overriding only those passed. |
---|
[136] | 106 | $this->_params = array_merge($this->_params, $params); |
---|
[1] | 107 | } else { |
---|
[136] | 108 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
[136] | 113 | * Return the value of a parameter, if it exists. |
---|
[1] | 114 | * |
---|
[136] | 115 | * @access public |
---|
| 116 | * @param string $param Which parameter to return. |
---|
| 117 | * @return mixed Configured parameter value. |
---|
[1] | 118 | */ |
---|
[439] | 119 | public function getParam($param) |
---|
[1] | 120 | { |
---|
[136] | 121 | $app =& App::getInstance(); |
---|
[445] | 122 | |
---|
[136] | 123 | if (isset($this->_params[$param])) { |
---|
| 124 | return $this->_params[$param]; |
---|
[1] | 125 | } else { |
---|
[146] | 126 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 127 | return null; |
---|
| 128 | } |
---|
| 129 | } |
---|
[42] | 130 | |
---|
[1] | 131 | /** |
---|
| 132 | * Connect to database with credentials in params. |
---|
| 133 | * |
---|
| 134 | * @access public |
---|
| 135 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 136 | * @since 28 Aug 2005 14:02:49 |
---|
| 137 | */ |
---|
[439] | 138 | public function connect() |
---|
[1] | 139 | { |
---|
[136] | 140 | $app =& App::getInstance(); |
---|
[445] | 141 | |
---|
[136] | 142 | if (!$this->getParam('db_name') || !$this->getParam('db_user') || !$this->getParam('db_pass')) { |
---|
| 143 | $app->logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__); |
---|
[1] | 144 | return false; |
---|
| 145 | } |
---|
[42] | 146 | |
---|
[1] | 147 | // Connect to database. Always create a new link to the server. |
---|
[450] | 148 | if ($this->dbh = @mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) { |
---|
[1] | 149 | // Select database |
---|
[136] | 150 | mysql_select_db($this->getParam('db_name'), $this->dbh); |
---|
[1] | 151 | } |
---|
[42] | 152 | |
---|
[15] | 153 | // Test for connection errors. |
---|
[136] | 154 | if (!$this->dbh || mysql_error($this->dbh)) { |
---|
| 155 | $mysql_error_msg = $this->dbh ? 'Codebase MySQL error: (' . mysql_errno($this->dbh) . ') ' . mysql_error($this->dbh) : 'Codebase MySQL error: Could not connect to server.'; |
---|
| 156 | $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__); |
---|
[15] | 157 | |
---|
| 158 | // Print helpful or pretty error? |
---|
[136] | 159 | if ($this->getParam('db_debug')) { |
---|
[1] | 160 | echo $mysql_error_msg . "\n"; |
---|
| 161 | } |
---|
[15] | 162 | |
---|
[416] | 163 | // Die if db_die_on_failure = true, or just continue without connection. |
---|
[230] | 164 | return $this->_fail(); |
---|
[1] | 165 | } |
---|
[42] | 166 | |
---|
[1] | 167 | // DB connection success! |
---|
[136] | 168 | $this->_connected = true; |
---|
[1] | 169 | |
---|
[334] | 170 | // Tell MySQL what character set we're using. Available only on MySQL versions > 4.01.01. |
---|
[247] | 171 | if ('' != $app->getParam('character_set') && isset($this->mysql_character_sets[mb_strtolower($app->getParam('character_set'))])) { |
---|
| 172 | $this->query("/*!40101 SET NAMES '" . $this->mysql_character_sets[mb_strtolower($app->getParam('character_set'))] . "' */"); |
---|
[209] | 173 | } else { |
---|
| 174 | $app->logMsg(sprintf('%s is not a known character_set.', $app->getParam('character_set')), LOG_ERR, __FILE__, __LINE__); |
---|
| 175 | } |
---|
[10] | 176 | |
---|
[1] | 177 | return true; |
---|
| 178 | } |
---|
[42] | 179 | |
---|
[1] | 180 | /** |
---|
| 181 | * Close db connection. |
---|
| 182 | * |
---|
| 183 | * @access public |
---|
| 184 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 185 | * @since 28 Aug 2005 14:32:01 |
---|
| 186 | */ |
---|
[439] | 187 | public function close() |
---|
[1] | 188 | { |
---|
[136] | 189 | if (!$this->_connected) { |
---|
[1] | 190 | return false; |
---|
| 191 | } |
---|
[416] | 192 | $this->_connected = false; |
---|
[136] | 193 | return mysql_close($this->dbh); |
---|
[1] | 194 | } |
---|
[445] | 195 | |
---|
[230] | 196 | /* |
---|
[416] | 197 | * |
---|
[445] | 198 | * |
---|
[416] | 199 | * @access public |
---|
[445] | 200 | * @param |
---|
| 201 | * @return |
---|
[416] | 202 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 203 | * @version 1.0 |
---|
| 204 | * @since 03 Jul 2013 14:50:23 |
---|
| 205 | */ |
---|
[439] | 206 | public function reconnect() |
---|
[416] | 207 | { |
---|
| 208 | $this->close(); |
---|
| 209 | $this->connect(); |
---|
| 210 | } |
---|
[445] | 211 | |
---|
[416] | 212 | /* |
---|
[230] | 213 | * Die only if db_die_on_failure is true. This will be set to false for some cases |
---|
| 214 | * when a database is not required for web app functionality. |
---|
| 215 | * |
---|
| 216 | * @access public |
---|
| 217 | * @param string $msg Print $msg when dying. |
---|
| 218 | * @return bool false If we don't die. |
---|
| 219 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 220 | * @version 1.0 |
---|
| 221 | * @since 15 Jan 2007 15:59:00 |
---|
| 222 | */ |
---|
[439] | 223 | private function _fail() |
---|
[230] | 224 | { |
---|
| 225 | if ($this->getParam('db_die_on_failure')) { |
---|
[418] | 226 | header(' ', true, 503); |
---|
[230] | 227 | echo _("This page is temporarily unavailable. Please try again in a few minutes."); |
---|
| 228 | die; |
---|
| 229 | } else { |
---|
| 230 | return false; |
---|
| 231 | } |
---|
| 232 | } |
---|
[42] | 233 | |
---|
[1] | 234 | /** |
---|
| 235 | * Return the current database handler. |
---|
| 236 | * |
---|
| 237 | * @access public |
---|
| 238 | * @return resource Current value of $this->dbh. |
---|
| 239 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 240 | * @since 20 Aug 2005 13:50:36 |
---|
| 241 | */ |
---|
[439] | 242 | public function getDBH() |
---|
[1] | 243 | { |
---|
[136] | 244 | if (!$this->_connected) { |
---|
[1] | 245 | return false; |
---|
| 246 | } |
---|
| 247 | |
---|
[136] | 248 | return $this->dbh; |
---|
[1] | 249 | } |
---|
[42] | 250 | |
---|
[1] | 251 | /** |
---|
| 252 | * Returns connection status |
---|
| 253 | * |
---|
| 254 | * @access public |
---|
| 255 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 256 | * @since 28 Aug 2005 14:58:09 |
---|
| 257 | */ |
---|
[439] | 258 | public function isConnected() |
---|
[1] | 259 | { |
---|
[136] | 260 | return (true === $this->_connected); |
---|
[1] | 261 | } |
---|
[445] | 262 | |
---|
[71] | 263 | /** |
---|
| 264 | * Returns a properly escaped string using mysql_real_escape_string() with the current connection's charset. |
---|
| 265 | * |
---|
| 266 | * @access public |
---|
| 267 | * @param string $string Input string to be sent as SQL query. |
---|
| 268 | * @return string Escaped string from mysql_real_escape_string() |
---|
| 269 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 270 | * @since 06 Mar 2006 16:41:32 |
---|
| 271 | */ |
---|
[439] | 272 | public function escapeString($string) |
---|
[71] | 273 | { |
---|
[136] | 274 | if (!$this->_connected) { |
---|
| 275 | return false; |
---|
[71] | 276 | } |
---|
[136] | 277 | |
---|
| 278 | return mysql_real_escape_string($string, $this->dbh); |
---|
[71] | 279 | } |
---|
[42] | 280 | |
---|
[1] | 281 | /** |
---|
| 282 | * A wrapper for mysql_query. Allows us to set the database link_identifier, |
---|
| 283 | * to trap errors and ease debugging. |
---|
| 284 | * |
---|
| 285 | * @param string $query The SQL query to execute |
---|
| 286 | * @param bool $debug If true, prints debugging info |
---|
| 287 | * @return resource Query identifier |
---|
| 288 | */ |
---|
[439] | 289 | public function query($query, $debug=false) |
---|
[445] | 290 | { |
---|
[136] | 291 | $app =& App::getInstance(); |
---|
[42] | 292 | |
---|
[136] | 293 | if (!$this->_connected) { |
---|
[1] | 294 | return false; |
---|
| 295 | } |
---|
| 296 | |
---|
[172] | 297 | $this->_query_count++; |
---|
| 298 | |
---|
[1] | 299 | $debugqry = preg_replace("/\n[\t ]+/", "\n", $query); |
---|
[136] | 300 | if ($this->getParam('db_always_debug') || $debug) { |
---|
[172] | 301 | echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n"; |
---|
[1] | 302 | } |
---|
[445] | 303 | |
---|
| 304 | // Ensure we have an active connection. |
---|
[419] | 305 | // If we continue on a dead connection we might experience a "MySQL server has gone away" error. |
---|
| 306 | // http://dev.mysql.com/doc/refman/5.0/en/gone-away.html |
---|
[416] | 307 | if (!mysql_ping($this->dbh)) { |
---|
[419] | 308 | $app->logMsg(sprintf('MySQL ping failed; reconnecting⊠("%s")', truncate(trim($debugqry), 150)), LOG_NOTICE, __FILE__, __LINE__); |
---|
[416] | 309 | $this->reconnect(); |
---|
| 310 | } |
---|
[42] | 311 | |
---|
[1] | 312 | // Execute! |
---|
[136] | 313 | $qid = mysql_query($query, $this->dbh); |
---|
[42] | 314 | |
---|
[1] | 315 | // Error checking. |
---|
[136] | 316 | if (!$qid || mysql_error($this->dbh)) { |
---|
[230] | 317 | $app->logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($this->dbh), mysql_error($this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__); |
---|
[136] | 318 | if ($this->getParam('db_debug')) { |
---|
| 319 | echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>'; |
---|
[445] | 320 | } |
---|
[230] | 321 | // Die if db_die_on_failure = true, or just continue without connection |
---|
| 322 | return $this->_fail(); |
---|
[1] | 323 | } |
---|
[42] | 324 | |
---|
[1] | 325 | return $qid; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | /** |
---|
[42] | 329 | * Loads a list of tables in the current database into an array, and returns |
---|
[1] | 330 | * true if the requested table is found. Use this function to enable/disable |
---|
[334] | 331 | * functionality based upon the current available db tables or to dynamically |
---|
[1] | 332 | * create tables if missing. |
---|
| 333 | * |
---|
[396] | 334 | * @param string $table The name of the table to search. |
---|
| 335 | * @param bool $use_cached_results Get fresh table info (in case DB changed). |
---|
| 336 | * @return bool true if given $table exists. |
---|
[1] | 337 | */ |
---|
[439] | 338 | public function tableExists($table, $use_cached_results=true) |
---|
[42] | 339 | { |
---|
[136] | 340 | $app =& App::getInstance(); |
---|
[445] | 341 | |
---|
[136] | 342 | if (!$this->_connected) { |
---|
[1] | 343 | return false; |
---|
| 344 | } |
---|
| 345 | |
---|
[136] | 346 | if (!isset($this->existing_tables) || !$use_cached_results) { |
---|
| 347 | $this->existing_tables = array(); |
---|
| 348 | $qid = $this->query("SHOW TABLES"); |
---|
[1] | 349 | while (list($row) = mysql_fetch_row($qid)) { |
---|
[136] | 350 | $this->existing_tables[] = $row; |
---|
[1] | 351 | } |
---|
| 352 | } |
---|
[136] | 353 | if (in_array($table, $this->existing_tables)) { |
---|
[1] | 354 | return true; |
---|
| 355 | } else { |
---|
[156] | 356 | $app->logMsg(sprintf('Nonexistent DB table: %s.%s', $this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__); |
---|
[1] | 357 | return false; |
---|
| 358 | } |
---|
| 359 | } |
---|
[42] | 360 | |
---|
[1] | 361 | /** |
---|
| 362 | * Tests if the given array of columns exists in the specified table. |
---|
| 363 | * |
---|
[396] | 364 | * @param string $table The name of the table to search. |
---|
| 365 | * @param array $columns An array of column names. |
---|
| 366 | * @param bool $strict Exact schema match, or are additional fields in the table okay? |
---|
| 367 | * @param bool $use_cached_results Get fresh table info (in case DB changed). |
---|
| 368 | * @return bool true if column(s) exist. |
---|
[1] | 369 | */ |
---|
[439] | 370 | public function columnExists($table, $columns, $strict=true, $use_cached_results=true) |
---|
[42] | 371 | { |
---|
[136] | 372 | if (!$this->_connected) { |
---|
[1] | 373 | return false; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | // Ensure the table exists. |
---|
[136] | 377 | if (!$this->tableExists($table, $use_cached_results)) { |
---|
[1] | 378 | return false; |
---|
| 379 | } |
---|
[42] | 380 | |
---|
[1] | 381 | // For single-value columns. |
---|
| 382 | if (!is_array($columns)) { |
---|
| 383 | $columns = array($columns); |
---|
| 384 | } |
---|
[42] | 385 | |
---|
[136] | 386 | if (!isset($this->table_columns[$table]) || !$use_cached_results) { |
---|
[1] | 387 | // Populate and cache array of current columns for this table. |
---|
[136] | 388 | $this->table_columns[$table] = array(); |
---|
| 389 | $qid = $this->query("DESCRIBE $table"); |
---|
[1] | 390 | while ($row = mysql_fetch_row($qid)) { |
---|
[136] | 391 | $this->table_columns[$table][] = $row[0]; |
---|
[1] | 392 | } |
---|
| 393 | } |
---|
[42] | 394 | |
---|
[1] | 395 | if ($strict) { |
---|
| 396 | // Do an exact comparison of table schemas. |
---|
| 397 | sort($columns); |
---|
[136] | 398 | sort($this->table_columns[$table]); |
---|
| 399 | return $this->table_columns[$table] == $columns; |
---|
[1] | 400 | } else { |
---|
| 401 | // Only check that the specified columns are available in the table. |
---|
[136] | 402 | $match_columns = array_intersect($this->table_columns[$table], $columns); |
---|
[1] | 403 | sort($columns); |
---|
| 404 | sort($match_columns); |
---|
| 405 | return $match_columns == $columns; |
---|
| 406 | } |
---|
| 407 | } |
---|
[445] | 408 | |
---|
[172] | 409 | /* |
---|
[396] | 410 | * Return the total number of queries executed thus far. |
---|
[172] | 411 | * |
---|
| 412 | * @access public |
---|
| 413 | * @return int Number of queries |
---|
| 414 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 415 | * @version 1.0 |
---|
| 416 | * @since 15 Jun 2006 11:46:05 |
---|
| 417 | */ |
---|
[439] | 418 | public function numQueries() |
---|
[172] | 419 | { |
---|
| 420 | return $this->_query_count; |
---|
| 421 | } |
---|
[42] | 422 | |
---|
[1] | 423 | /** |
---|
| 424 | * Reset cached items. |
---|
| 425 | * |
---|
| 426 | * @access public |
---|
| 427 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 428 | * @since 28 Aug 2005 22:10:50 |
---|
| 429 | */ |
---|
[439] | 430 | public function resetCache() |
---|
[1] | 431 | { |
---|
| 432 | $this->existing_tables = null; |
---|
| 433 | $this->table_columns = null; |
---|
| 434 | } |
---|
[42] | 435 | |
---|
[1] | 436 | } // End. |
---|
| 437 | |
---|