| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /**********************************************************************
|
|---|
| 4 | * Author: Justin Vincent (jv@jvmultimedia.com)
|
|---|
| 5 | * Web...: http://twitter.com/justinvincent
|
|---|
| 6 | * Name..: ezSQL_mysql
|
|---|
| 7 | * Desc..: mySQL component (part of ezSQL databse abstraction library)
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | /**********************************************************************
|
|---|
| 12 | * ezSQL error strings - mySQL
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | $ezsql_mysql_str = array
|
|---|
| 16 | (
|
|---|
| 17 | 1 => 'Require $dbuser and $dbpassword to connect to a database server',
|
|---|
| 18 | 2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
|
|---|
| 19 | 3 => 'Require $dbname to select a database',
|
|---|
| 20 | 4 => 'mySQL database connection is not active',
|
|---|
| 21 | 5 => 'Unexpected error while trying to select database'
|
|---|
| 22 | );
|
|---|
| 23 |
|
|---|
| 24 | /**********************************************************************
|
|---|
| 25 | * ezSQL Database specific class - mySQL
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | if ( ! function_exists ('mysql_connect') ) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
|
|---|
| 29 | if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
|
|---|
| 30 |
|
|---|
| 31 | class ezSQL_mysql extends ezSQLcore
|
|---|
| 32 | {
|
|---|
| 33 |
|
|---|
| 34 | var $dbuser = false;
|
|---|
| 35 | var $dbpassword = false;
|
|---|
| 36 | var $dbname = false;
|
|---|
| 37 | var $dbhost = false;
|
|---|
| 38 |
|
|---|
| 39 | /**********************************************************************
|
|---|
| 40 | * Constructor - allow the user to perform a qucik connect at the
|
|---|
| 41 | * same time as initialising the ezSQL_mysql class
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | function ezSQL_mysql($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
|
|---|
| 45 | {
|
|---|
| 46 | $this->dbuser = $dbuser;
|
|---|
| 47 | $this->dbpassword = $dbpassword;
|
|---|
| 48 | $this->dbname = $dbname;
|
|---|
| 49 | $this->dbhost = $dbhost;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**********************************************************************
|
|---|
| 53 | * Set $h - global Hotaru object
|
|---|
| 54 | */
|
|---|
| 55 |
|
|---|
| 56 | function setHotaru($h)
|
|---|
| 57 | {
|
|---|
| 58 | $this->h = $h;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**********************************************************************
|
|---|
| 62 | * Short hand way to connect to mySQL database server
|
|---|
| 63 | * and select a mySQL database at the same time
|
|---|
| 64 | */
|
|---|
| 65 |
|
|---|
| 66 | function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
|
|---|
| 67 | {
|
|---|
| 68 | $return_val = false;
|
|---|
| 69 | if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
|
|---|
| 70 | else if ( ! $this->select($dbname) ) ;
|
|---|
| 71 | else $return_val = true;
|
|---|
| 72 | return $return_val;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**********************************************************************
|
|---|
| 76 | * Try to connect to mySQL database server
|
|---|
| 77 | */
|
|---|
| 78 |
|
|---|
| 79 | function connect($dbuser='', $dbpassword='', $dbhost='localhost')
|
|---|
| 80 | {
|
|---|
| 81 | global $ezsql_mysql_str; $return_val = false;
|
|---|
| 82 |
|
|---|
| 83 | // Must have a user and a password
|
|---|
| 84 | if ( ! $dbuser )
|
|---|
| 85 | {
|
|---|
| 86 | $this->register_error($ezsql_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
|
|---|
| 87 | $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
|
|---|
| 88 | }
|
|---|
| 89 | // Try to establish the server database handle
|
|---|
| 90 | else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true) )
|
|---|
| 91 | {
|
|---|
| 92 | $this->register_error($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
|
|---|
| 93 | $this->show_errors ? trigger_error($ezsql_mysql_str[2],E_USER_WARNING) : null;
|
|---|
| 94 | }
|
|---|
| 95 | else
|
|---|
| 96 | {
|
|---|
| 97 | $this->dbuser = $dbuser;
|
|---|
| 98 | $this->dbpassword = $dbpassword;
|
|---|
| 99 | $this->dbhost = $dbhost;
|
|---|
| 100 | $return_val = true;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return $return_val;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**********************************************************************
|
|---|
| 107 | * Try to select a mySQL database
|
|---|
| 108 | */
|
|---|
| 109 |
|
|---|
| 110 | function select($dbname='')
|
|---|
| 111 | {
|
|---|
| 112 | global $ezsql_mysql_str; $return_val = false;
|
|---|
| 113 |
|
|---|
| 114 | // Must have a database name
|
|---|
| 115 | if ( ! $dbname )
|
|---|
| 116 | {
|
|---|
| 117 | $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
|
|---|
| 118 | $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // Must have an active database connection
|
|---|
| 122 | else if ( ! $this->dbh )
|
|---|
| 123 | {
|
|---|
| 124 | $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
|
|---|
| 125 | $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | // Try to connect to the database
|
|---|
| 129 | else if ( !@mysql_select_db($dbname,$this->dbh) )
|
|---|
| 130 | {
|
|---|
| 131 | // Try to get error supplied by mysql if not use our own
|
|---|
| 132 | if ( !$str = @mysql_error($this->dbh))
|
|---|
| 133 | $str = $ezsql_mysql_str[5];
|
|---|
| 134 |
|
|---|
| 135 | $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
|
|---|
| 136 | $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
|
|---|
| 137 | }
|
|---|
| 138 | else
|
|---|
| 139 | {
|
|---|
| 140 | $this->dbname = $dbname;
|
|---|
| 141 | $return_val = true;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return $return_val;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**********************************************************************
|
|---|
| 148 | * Format a mySQL string correctly for safe mySQL insert
|
|---|
| 149 | * (no mater if magic quotes are on or not)
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | function escape($str = '')
|
|---|
| 153 | {
|
|---|
| 154 | return mysql_escape_string(stripslashes($str));
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /**********************************************************************
|
|---|
| 158 | * Return mySQL specific system date syntax
|
|---|
| 159 | * i.e. Oracle: SYSDATE Mysql: NOW()
|
|---|
| 160 | */
|
|---|
| 161 |
|
|---|
| 162 | function sysdate()
|
|---|
| 163 | {
|
|---|
| 164 | return 'NOW()';
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**********************************************************************
|
|---|
| 168 | * Perform mySQL query and try to detirmin result value
|
|---|
| 169 | */
|
|---|
| 170 |
|
|---|
| 171 | function query($query = '')
|
|---|
| 172 | {
|
|---|
| 173 |
|
|---|
| 174 | // Initialise return
|
|---|
| 175 | $return_val = 0;
|
|---|
| 176 |
|
|---|
| 177 | // Flush cached values..
|
|---|
| 178 | $this->flush();
|
|---|
| 179 |
|
|---|
| 180 | // For reg expressions
|
|---|
| 181 | $query = trim($query);
|
|---|
| 182 |
|
|---|
| 183 | // Log how the function was called
|
|---|
| 184 | $this->func_call = "\$db->query(\"$query\")";
|
|---|
| 185 |
|
|---|
| 186 | // Keep track of the last query for debug..
|
|---|
| 187 | $this->last_query = $query;
|
|---|
| 188 |
|
|---|
| 189 | // Perform the query via std mysql_query function.. (Borrowed from Wordpress)
|
|---|
| 190 | if ( defined('SAVEQUERIES') && SAVEQUERIES )
|
|---|
| 191 | $this->timer_start();
|
|---|
| 192 |
|
|---|
| 193 | // Use core file cache function
|
|---|
| 194 | if ( $cache = $this->get_cache($query) )
|
|---|
| 195 | {
|
|---|
| 196 | // Nick edit: Although it cached queries with zero results, the get_cache
|
|---|
| 197 | // function returns false (i.e 0) if there are zero rows, so if 0, I've made
|
|---|
| 198 | // it store and return "empty" instead, forcing the above queryto return true
|
|---|
| 199 | // and the cache (with no results) to be used. This saves making repeated SQL
|
|---|
| 200 | // queries that we already know return an empty set of results.
|
|---|
| 201 | // I did this because the pluginHook function ramps up the query counts
|
|---|
| 202 | // but rarely returns anything!
|
|---|
| 203 | return $cache;
|
|---|
| 204 | } else {
|
|---|
| 205 | //echo $query . "<br />"; // for testing purposes
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | // Count how many queries there have been
|
|---|
| 209 | $this->num_queries++;
|
|---|
| 210 |
|
|---|
| 211 | // If there is no existing database connection then try to connect
|
|---|
| 212 | if ( ! isset($this->dbh) || ! $this->dbh )
|
|---|
| 213 | {
|
|---|
| 214 | $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
|
|---|
| 215 | $this->select($this->dbname);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | // Perform the query via std mysql_query function..
|
|---|
| 219 | $this->result = @mysql_query($query,$this->dbh);
|
|---|
| 220 |
|
|---|
| 221 | if ( defined('SAVEQUERIES') && SAVEQUERIES ) // Borrowed from Wordpress
|
|---|
| 222 | $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
|
|---|
| 223 |
|
|---|
| 224 | // If there is an error then take note of it..
|
|---|
| 225 | if ( $str = @mysql_error($this->dbh) )
|
|---|
| 226 | {
|
|---|
| 227 | if (DEBUG == 'true') {
|
|---|
| 228 | $headers = "From: " . SITE_EMAIL . "\r\nReply-To: " . SITE_EMAIL . "\r\nX-Priority: 3\r\n";
|
|---|
| 229 | $subject = SITE_NAME . " Database Error";
|
|---|
| 230 | $body = SITE_NAME . " Database Error\r\n\r\n";
|
|---|
| 231 | $body .= "Date: " . date('d M Y H:i:s') . " (timezone: " . date_default_timezone_get() . ")\r\n\r\n";
|
|---|
| 232 | $body .= "SQL query:\r\n";
|
|---|
| 233 | $body .= $query . "\r\n\r\n";
|
|---|
| 234 |
|
|---|
| 235 | $body .= "PHP error log:\r\n";
|
|---|
| 236 | $body .= $str . "\r\n\r\n";
|
|---|
| 237 |
|
|---|
| 238 | if(isset($this->h)) {
|
|---|
| 239 | $body .= "Current User: " . $this->h->currentUser->name . " (id: " . $this->h->currentUser->id .")\r\n";
|
|---|
| 240 | $body .= "User Role: " . $this->h->currentUser->role . "\r\n";
|
|---|
| 241 | $body .= "Page Name: " . $this->h->pageName . "\r\n";
|
|---|
| 242 | $body .= "Sub Page: " . $this->h->subPage . "\r\n";
|
|---|
| 243 | $body .= "Plugin: " . $this->h->plugin->folder . "\r\n\r\n";
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | $body .= "If you need help, visit the forums at http://hotarucms.org\r\n";
|
|---|
| 247 | mail(SITE_EMAIL, $subject, $body, $headers);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | $is_insert = true;
|
|---|
| 251 | $this->register_error($str);
|
|---|
| 252 | $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
|
|---|
| 253 | return false;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | // Query was an insert, delete, update, replace
|
|---|
| 257 | $is_insert = false;
|
|---|
| 258 | if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
|
|---|
| 259 | {
|
|---|
| 260 | $this->rows_affected = @mysql_affected_rows();
|
|---|
| 261 |
|
|---|
| 262 | // Take note of the insert_id
|
|---|
| 263 | if ( preg_match("/^(insert|replace)\s+/i",$query) )
|
|---|
| 264 | {
|
|---|
| 265 | $this->insert_id = @mysql_insert_id($this->dbh);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | // Return number fo rows affected
|
|---|
| 269 | $return_val = $this->rows_affected;
|
|---|
| 270 | }
|
|---|
| 271 | // Query was a select
|
|---|
| 272 | else
|
|---|
| 273 | {
|
|---|
| 274 |
|
|---|
| 275 | // Take note of column info
|
|---|
| 276 | $i=0;
|
|---|
| 277 | if (is_resource($this->result)) {
|
|---|
| 278 | while ($i < @mysql_num_fields($this->result))
|
|---|
| 279 | {
|
|---|
| 280 | $this->col_info[$i] = @mysql_fetch_field($this->result);
|
|---|
| 281 | $i++;
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | // Store Query Results
|
|---|
| 286 | $num_rows=0;
|
|---|
| 287 | if (is_resource($this->result)) {
|
|---|
| 288 | while ( $row = @mysql_fetch_object($this->result) )
|
|---|
| 289 | {
|
|---|
| 290 | // Store relults as an objects within main array
|
|---|
| 291 | $this->last_result[$num_rows] = $row;
|
|---|
| 292 | $num_rows++;
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | if (is_resource($this->result)) { @mysql_free_result($this->result); }
|
|---|
| 297 |
|
|---|
| 298 | // Log number of rows the query returned
|
|---|
| 299 | $this->num_rows = $num_rows;
|
|---|
| 300 |
|
|---|
| 301 | // Return number of rows selected
|
|---|
| 302 | $return_val = $this->num_rows;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | // disk caching of queries
|
|---|
| 306 | $this->store_cache($query,$is_insert);
|
|---|
| 307 |
|
|---|
| 308 | // If debug ALL queries
|
|---|
| 309 | $this->trace || $this->debug_all ? $this->debug() : null ;
|
|---|
| 310 |
|
|---|
| 311 | return $return_val;
|
|---|
| 312 |
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 | /**
|
|---|
| 317 | * Check if table exists
|
|---|
| 318 | *
|
|---|
| 319 | * @param string $table2check
|
|---|
| 320 | * @return bool
|
|---|
| 321 | *
|
|---|
| 322 | * Notes: This is a custom function for Hotaru CMS
|
|---|
| 323 | */
|
|---|
| 324 |
|
|---|
| 325 | function table_exists($table2check) {
|
|---|
| 326 | foreach ( $this->get_col("SHOW TABLES",0) as $table_name ) {
|
|---|
| 327 | if($table_name == DB_PREFIX . $table2check) {
|
|---|
| 328 | return true;
|
|---|
| 329 | }
|
|---|
| 330 | }
|
|---|
| 331 | return false;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /**
|
|---|
| 335 | * Check if table empty
|
|---|
| 336 | *
|
|---|
| 337 | * @param string $table2check
|
|---|
| 338 | * @return bool
|
|---|
| 339 | *
|
|---|
| 340 | * Notes: This is a custom function for Hotaru CMS
|
|---|
| 341 | */
|
|---|
| 342 | function table_empty($table2check) {
|
|---|
| 343 | $rowcount = $this->get_var($this->prepare("SELECT COUNT(*) FROM " . DB_PREFIX . $table2check));
|
|---|
| 344 | if($rowcount && $rowcount > 0) {
|
|---|
| 345 | return false; // table not empty
|
|---|
| 346 | } else {
|
|---|
| 347 | return true; // table is empty
|
|---|
| 348 | }
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /**
|
|---|
| 352 | * Check if table column exists
|
|---|
| 353 | *
|
|---|
| 354 | * @param string $table2check
|
|---|
| 355 | * @param string $column
|
|---|
| 356 | * @return bool
|
|---|
| 357 | *
|
|---|
| 358 | * Notes: This is a custom function for Hotaru CMS
|
|---|
| 359 | */
|
|---|
| 360 | function column_exists($table2check, $column)
|
|---|
| 361 | {
|
|---|
| 362 | $sql = "SHOW COLUMNS FROM " . DB_PREFIX . $table2check;
|
|---|
| 363 | foreach ($this->get_col($sql,0) as $column_name)
|
|---|
| 364 | {
|
|---|
| 365 | if ($column_name == $column) {
|
|---|
| 366 | return true;
|
|---|
| 367 | }
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | return false;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | ?>
|
|---|