[ Index ]
 

Code source de WordPress 2.1.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/wp-includes/ -> wp-db.php (source)

   1  <?php
   2  //  WordPress DB Class
   3  
   4  //  ORIGINAL CODE FROM:
   5  //  Justin Vincent (justin@visunet.ie)
   6  //    http://php.justinvincent.com
   7  
   8  define('EZSQL_VERSION', 'WP1.25');
   9  define('OBJECT', 'OBJECT', true);
  10  define('ARRAY_A', 'ARRAY_A', false);
  11  define('ARRAY_N', 'ARRAY_N', false);
  12  
  13  if (!defined('SAVEQUERIES'))
  14      define('SAVEQUERIES', false);
  15  
  16  class wpdb {
  17  
  18      var $show_errors = true;
  19      var $num_queries = 0;
  20      var $last_query;
  21      var $col_info;
  22      var $queries;
  23  
  24      // Our tables
  25      var $posts;
  26      var $users;
  27      var $categories;
  28      var $post2cat;
  29      var $comments;
  30      var $links;
  31      var $options;
  32      var $optiontypes;
  33      var $optionvalues;
  34      var $optiongroups;
  35      var $optiongroup_options;
  36      var $postmeta;
  37  
  38      /**
  39       * Connects to the database server and selects a database
  40       * @param string $dbuser
  41       * @param string $dbpassword
  42       * @param string $dbname
  43       * @param string $dbhost
  44       */
  45  	function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
  46          return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
  47      }
  48      
  49  	function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
  50          register_shutdown_function(array(&$this, "__destruct"));
  51  
  52          $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);
  53          if (!$this->dbh) {
  54              $this->bail("
  55  <h1>Error establishing a database connection</h1>
  56  <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
  57  <ul>
  58      <li>Are you sure you have the correct username and password?</li>
  59      <li>Are you sure that you have typed the correct hostname?</li>
  60      <li>Are you sure that the database server is running?</li>
  61  </ul>
  62  <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
  63  ");
  64          }
  65  
  66          $this->select($dbname);
  67      }
  68  
  69  	function __destruct() {
  70          return true;        
  71      }
  72  
  73      /**
  74       * Selects a database using the current class's $this->dbh
  75       * @param string $db name
  76       */
  77  	function select($db) {
  78          if (!@mysql_select_db($db, $this->dbh)) {
  79              $this->bail("
  80  <h1>Can&#8217;t select database</h1>
  81  <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>$db</code> database.</p>
  82  <ul>
  83  <li>Are you sure it exists?</li>
  84  <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
  85  </ul>
  86  <p>If you don't know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>");
  87          }
  88      }
  89  
  90      /**
  91       * Escapes content for insertion into the database, for security
  92       *
  93       * @param string $string
  94       * @return string query safe string
  95       */
  96  	function escape($string) {
  97          return addslashes( $string ); // Disable rest for now, causing problems
  98          if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
  99              return mysql_escape_string( $string );
 100          else
 101              return mysql_real_escape_string( $string, $this->dbh );
 102      }
 103  
 104      // ==================================================================
 105      //    Print SQL/DB error.
 106  
 107  	function print_error($str = '') {
 108          global $EZSQL_ERROR;
 109          if (!$str) $str = mysql_error($this->dbh);
 110          $EZSQL_ERROR[] =
 111          array ('query' => $this->last_query, 'error_str' => $str);
 112  
 113          $str = htmlspecialchars($str, ENT_QUOTES);
 114          $query = htmlspecialchars($this->last_query, ENT_QUOTES);
 115          // Is error output turned on or not..
 116          if ( $this->show_errors ) {
 117              // If there is an error then take note of it
 118              print "<div id='error'>
 119              <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
 120              <code>$query</code></p>
 121              </div>";
 122          } else {
 123              return false;
 124          }
 125      }
 126  
 127      // ==================================================================
 128      //    Turn error handling on or off..
 129  
 130  	function show_errors() {
 131          $this->show_errors = true;
 132      }
 133  
 134  	function hide_errors() {
 135          $this->show_errors = false;
 136      }
 137  
 138      // ==================================================================
 139      //    Kill cached query results
 140  
 141  	function flush() {
 142          $this->last_result = array();
 143          $this->col_info = null;
 144          $this->last_query = null;
 145      }
 146  
 147      // ==================================================================
 148      //    Basic Query    - see docs for more detail
 149  
 150  	function query($query) {
 151          // filter the query, if filters are available
 152          // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
 153          if ( function_exists('apply_filters') )
 154              $query = apply_filters('query', $query);
 155  
 156          // initialise return
 157          $return_val = 0;
 158          $this->flush();
 159  
 160          // Log how the function was called
 161          $this->func_call = "\$db->query(\"$query\")";
 162  
 163          // Keep track of the last query for debug..
 164          $this->last_query = $query;
 165  
 166          // Perform the query via std mysql_query function..
 167          if (SAVEQUERIES)
 168              $this->timer_start();
 169  
 170          $this->result = @mysql_query($query, $this->dbh);
 171          ++$this->num_queries;
 172      
 173          if (SAVEQUERIES)
 174              $this->queries[] = array( $query, $this->timer_stop() );
 175  
 176          // If there is an error then take note of it..
 177          if ( mysql_error($this->dbh) ) {
 178              $this->print_error();
 179              return false;
 180          }
 181  
 182          if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
 183              $this->rows_affected = mysql_affected_rows();
 184              // Take note of the insert_id
 185              if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
 186                  $this->insert_id = mysql_insert_id($this->dbh);
 187              }
 188              // Return number of rows affected
 189              $return_val = $this->rows_affected;
 190          } else {
 191              $i = 0;
 192              while ($i < @mysql_num_fields($this->result)) {
 193                  $this->col_info[$i] = @mysql_fetch_field($this->result);
 194                  $i++;
 195              }
 196              $num_rows = 0;
 197              while ( $row = @mysql_fetch_object($this->result) ) {
 198                  $this->last_result[$num_rows] = $row;
 199                  $num_rows++;
 200              }
 201  
 202              @mysql_free_result($this->result);
 203  
 204              // Log number of rows the query returned
 205              $this->num_rows = $num_rows;
 206  
 207              // Return number of rows selected
 208              $return_val = $this->num_rows;
 209          }
 210  
 211          return $return_val;
 212      }
 213  
 214      /**
 215       * Get one variable from the database
 216       * @param string $query (can be null as well, for caching, see codex)
 217       * @param int $x = 0 row num to return
 218       * @param int $y = 0 col num to return
 219       * @return mixed results
 220       */
 221  	function get_var($query=null, $x = 0, $y = 0) {
 222          $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
 223          if ( $query )
 224              $this->query($query);
 225  
 226          // Extract var out of cached results based x,y vals
 227          if ( $this->last_result[$y] ) {
 228              $values = array_values(get_object_vars($this->last_result[$y]));
 229          }
 230  
 231          // If there is a value return it else return null
 232          return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
 233      }
 234  
 235      /**
 236       * Get one row from the database
 237       * @param string $query
 238       * @param string $output ARRAY_A | ARRAY_N | OBJECT
 239       * @param int $y row num to return
 240       * @return mixed results
 241       */
 242  	function get_row($query = null, $output = OBJECT, $y = 0) {
 243          $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
 244          if ( $query )
 245              $this->query($query);
 246          
 247          if ( !isset($this->last_result[$y]) )
 248              return null;
 249  
 250          if ( $output == OBJECT ) {
 251              return $this->last_result[$y] ? $this->last_result[$y] : null;
 252          } elseif ( $output == ARRAY_A ) {
 253              return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
 254          } elseif ( $output == ARRAY_N ) {
 255              return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
 256          } else {
 257              $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
 258          }
 259      }
 260  
 261      /**
 262       * Gets one column from the database
 263       * @param string $query (can be null as well, for caching, see codex)
 264       * @param int $x col num to return
 265       * @return array results
 266       */
 267  	function get_col($query = null , $x = 0) {
 268          if ( $query )
 269              $this->query($query);
 270  
 271          // Extract the column values
 272          for ( $i=0; $i < count($this->last_result); $i++ ) {
 273              $new_array[$i] = $this->get_var(null, $x, $i);
 274          }
 275          return $new_array;
 276      }
 277  
 278      /**
 279       * Return an entire result set from the database
 280       * @param string $query (can also be null to pull from the cache)
 281       * @param string $output ARRAY_A | ARRAY_N | OBJECT
 282       * @return mixed results
 283       */
 284  	function get_results($query = null, $output = OBJECT) {
 285          $this->func_call = "\$db->get_results(\"$query\", $output)";
 286  
 287          if ( $query )
 288              $this->query($query);
 289  
 290          // Send back array of objects. Each row is an object
 291          if ( $output == OBJECT ) {
 292              return $this->last_result;
 293          } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
 294              if ( $this->last_result ) {
 295                  $i = 0;
 296                  foreach( $this->last_result as $row ) {
 297                      $new_array[$i] = (array) $row;
 298                      if ( $output == ARRAY_N ) {
 299                          $new_array[$i] = array_values($new_array[$i]);
 300                      }
 301                      $i++;
 302                  }
 303                  return $new_array;
 304              } else {
 305                  return null;
 306              }
 307          }
 308      }
 309  
 310      /**
 311       * Grabs column metadata from the last query
 312       * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
 313       * @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
 314       * @return mixed results
 315       */
 316  	function get_col_info($info_type = 'name', $col_offset = -1) {
 317          if ( $this->col_info ) {
 318              if ( $col_offset == -1 ) {
 319                  $i = 0;
 320                  foreach($this->col_info as $col ) {
 321                      $new_array[$i] = $col->{$info_type};
 322                      $i++;
 323                  }
 324                  return $new_array;
 325              } else {
 326                  return $this->col_info[$col_offset]->{$info_type};
 327              }
 328          }
 329      }
 330  
 331      /**
 332       * Starts the timer, for debugging purposes
 333       */
 334  	function timer_start() {
 335          $mtime = microtime();
 336          $mtime = explode(' ', $mtime);
 337          $this->time_start = $mtime[1] + $mtime[0];
 338          return true;
 339      }
 340  
 341      /**
 342       * Stops the debugging timer
 343       * @return int total time spent on the query, in milliseconds
 344       */
 345  	function timer_stop() {
 346          $mtime = microtime();
 347          $mtime = explode(' ', $mtime);
 348          $time_end = $mtime[1] + $mtime[0];
 349          $time_total = $time_end - $this->time_start;
 350          return $time_total;
 351      }
 352  
 353      /**
 354       * Wraps fatal errors in a nice header and footer and dies.
 355       * @param string $message
 356       */
 357  	function bail($message) { // Just wraps errors in a nice header and footer
 358          if ( !$this->show_errors )
 359              return false;
 360  
 361          header('Content-Type: text/html; charset=utf-8');
 362  
 363          if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') )
 364              $admin_dir = '';
 365          else
 366              $admin_dir = 'wp-admin/';
 367  
 368  ?>
 369  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 370  <html xmlns="http://www.w3.org/1999/xhtml">
 371  <head>
 372      <title>WordPress &rsaquo; Error</title>
 373      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 374      <link rel="stylesheet" href="<?php echo $admin_dir; ?>install.css" type="text/css" />
 375  </head>
 376  <body>
 377      <h1 id="logo"><img alt="WordPress" src="<?php echo $admin_dir; ?>images/wordpress-logo.png" /></h1>
 378      <p><?php echo $message; ?></p>
 379  </body>
 380  </html>
 381  <?php
 382          die();
 383      }
 384  }
 385  
 386  if ( ! isset($wpdb) )
 387      $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
 388  ?>


Généré le : Fri Mar 30 19:41:27 2007 par Balluche grâce à PHPXref 0.7