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