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