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