[ Index ]
 

Code source de XOOPS 2.0.17.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/htdocs/class/database/ -> mysqldatabase.php (source)

   1  <?php
   2  // $Id: mysqldatabase.php 694 2006-09-04 11:33:22Z skalpa $

   3  //  ------------------------------------------------------------------------ //

   4  //                XOOPS - PHP Content Management System                      //

   5  //                    Copyright (c) 2000 XOOPS.org                           //

   6  //                       <http://www.xoops.org/>                             //

   7  //  ------------------------------------------------------------------------ //

   8  //  This program is free software; you can redistribute it and/or modify     //

   9  //  it under the terms of the GNU General Public License as published by     //

  10  //  the Free Software Foundation; either version 2 of the License, or        //

  11  //  (at your option) any later version.                                      //

  12  //                                                                           //

  13  //  You may not change or alter any portion of this comment or credits       //

  14  //  of supporting developers from this source code or any supporting         //

  15  //  source code which is considered copyrighted (c) material of the          //

  16  //  original comment or credit authors.                                      //

  17  //                                                                           //

  18  //  This program is distributed in the hope that it will be useful,          //

  19  //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //

  20  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //

  21  //  GNU General Public License for more details.                             //

  22  //                                                                           //

  23  //  You should have received a copy of the GNU General Public License        //

  24  //  along with this program; if not, write to the Free Software              //

  25  //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //

  26  //  ------------------------------------------------------------------------ //

  27  // Author: Kazumi Ono (AKA onokazu)                                          //

  28  // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //

  29  // Project: The XOOPS Project                                                //

  30  // ------------------------------------------------------------------------- //

  31  if (!defined("XOOPS_ROOT_PATH")) {
  32      die("XOOPS root path not defined");
  33  }
  34  /**

  35   * @package     kernel

  36   * @subpackage  database

  37   * 

  38   * @author        Kazumi Ono    <onokazu@xoops.org>

  39   * @copyright    copyright (c) 2000-2003 XOOPS.org

  40   */
  41  
  42  /**

  43   * base class

  44   */
  45  include_once XOOPS_ROOT_PATH."/class/database/database.php";
  46  
  47  /**

  48   * connection to a mysql database

  49   * 

  50   * @abstract

  51   * 

  52   * @author      Kazumi Ono  <onokazu@xoops.org>

  53   * @copyright   copyright (c) 2000-2003 XOOPS.org

  54   * 

  55   * @package     kernel

  56   * @subpackage  database

  57   */
  58  class XoopsMySQLDatabase extends XoopsDatabase
  59  {
  60      /**

  61       * Database connection

  62       * @var resource

  63       */
  64      var $conn;
  65  
  66      /**

  67       * connect to the database

  68       * 

  69       * @param bool $selectdb select the database now?

  70       * @return bool successful?

  71       */
  72  	function connect($selectdb = true)
  73      {
  74          if ( !extension_loaded( 'mysql' ) ) {
  75              trigger_error( 'notrace:mysql extension not loaded', E_USER_ERROR );
  76              return false;
  77          }
  78          if (XOOPS_DB_PCONNECT == 1) {
  79              $this->conn = @mysql_pconnect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS);
  80          } else {
  81              $this->conn = @mysql_connect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS);
  82          }
  83      
  84          if (!$this->conn) {
  85              $this->logger->addQuery('', $this->error(), $this->errno());
  86              return false;
  87          }
  88          if($selectdb != false){
  89              if (!mysql_select_db(XOOPS_DB_NAME)) {
  90                  $this->logger->addQuery('', $this->error(), $this->errno());
  91                  return false;
  92              }
  93          }
  94          return true;
  95      }
  96  
  97      /**

  98       * generate an ID for a new row

  99       * 

 100       * This is for compatibility only. Will always return 0, because MySQL supports

 101       * autoincrement for primary keys.

 102       * 

 103       * @param string $sequence name of the sequence from which to get the next ID

 104       * @return int always 0, because mysql has support for autoincrement

 105       */
 106  	function genId($sequence)
 107      {
 108          return 0; // will use auto_increment

 109      }
 110  
 111      /**

 112       * Get a result row as an enumerated array

 113       * 

 114       * @param resource $result

 115       * @return array

 116       */
 117  	function fetchRow($result)
 118      {
 119          return @mysql_fetch_row($result);
 120      }
 121  
 122      /**

 123       * Fetch a result row as an associative array

 124       *

 125       * @return array

 126       */
 127  	function fetchArray($result)
 128      {
 129          return @mysql_fetch_assoc( $result );
 130      }
 131  
 132      /**

 133       * Fetch a result row as an associative array

 134       *

 135       * @return array

 136       */
 137      function fetchBoth($result)
 138      {
 139          return @mysql_fetch_array( $result, MYSQL_BOTH );
 140      }
 141  
 142      /**

 143       * Get the ID generated from the previous INSERT operation

 144       * 

 145       * @return int

 146       */
 147  	function getInsertId()
 148      {
 149          return mysql_insert_id($this->conn);
 150      }
 151  
 152      /**

 153       * Get number of rows in result

 154       * 

 155       * @param resource query result

 156       * @return int

 157       */
 158  	function getRowsNum($result)
 159      {
 160          return @mysql_num_rows($result);
 161      }
 162  
 163      /**

 164       * Get number of affected rows

 165       *

 166       * @return int

 167       */
 168  	function getAffectedRows()
 169      {
 170          return mysql_affected_rows($this->conn);
 171      }
 172  
 173      /**

 174       * Close MySQL connection

 175       * 

 176       */
 177  	function close()
 178      {
 179          mysql_close($this->conn);
 180      }
 181  
 182      /**

 183       * will free all memory associated with the result identifier result.

 184       * 

 185       * @param resource query result

 186       * @return bool TRUE on success or FALSE on failure. 

 187       */
 188  	function freeRecordSet($result)
 189      {
 190          return mysql_free_result($result);
 191      }
 192  
 193      /**

 194       * Returns the text of the error message from previous MySQL operation

 195       * 

 196       * @return bool Returns the error text from the last MySQL function, or '' (the empty string) if no error occurred. 

 197       */
 198  	function error()
 199      {
 200          return @mysql_error();
 201      }
 202  
 203      /**

 204       * Returns the numerical value of the error message from previous MySQL operation 

 205       * 

 206       * @return int Returns the error number from the last MySQL function, or 0 (zero) if no error occurred. 

 207       */
 208  	function errno()
 209      {
 210          return @mysql_errno();
 211      }
 212  
 213      /**

 214       * Returns escaped string text with single quotes around it to be safely stored in database

 215       * 

 216       * @param string $str unescaped string text

 217       * @return string escaped string text with single quotes around

 218       */
 219      function quoteString($str)
 220      {
 221           $str = "'".str_replace('\\"', '"', addslashes($str))."'";
 222           return $str;
 223      }
 224  
 225      /**

 226       * perform a query on the database

 227       * 

 228       * @param string $sql a valid MySQL query

 229       * @param int $limit number of records to return

 230       * @param int $start offset of first record to return

 231       * @return resource query result or FALSE if successful

 232       * or TRUE if successful and no result

 233       */
 234      function queryF($sql, $limit=0, $start=0)
 235      {
 236          if ( !empty($limit) ) {
 237              if (empty($start)) {
 238                  $start = 0;
 239              }
 240              $sql = $sql. ' LIMIT '.(int)$start.', '.(int)$limit;
 241          }
 242          $result = mysql_query($sql, $this->conn);
 243          if ( $result ) {
 244              $this->logger->addQuery($sql);
 245              return $result;
 246          } else {
 247              $this->logger->addQuery($sql, $this->error(), $this->errno());
 248              return false;
 249          }
 250      }
 251  
 252      /**

 253       * perform a query

 254       * 

 255       * This method is empty and does nothing! It should therefore only be

 256       * used if nothing is exactly what you want done! ;-)

 257       * 

 258       * @param string $sql a valid MySQL query

 259       * @param int $limit number of records to return

 260       * @param int $start offset of first record to return

 261       * 

 262       * @abstract

 263       */
 264  	function query($sql, $limit=0, $start=0)
 265      {
 266  
 267      }
 268  
 269      /**

 270       * perform queries from SQL dump file in a batch

 271       * 

 272       * @param string $file file path to an SQL dump file

 273       * 

 274       * @return bool FALSE if failed reading SQL file or TRUE if the file has been read and queries executed

 275       */
 276  	function queryFromFile($file){
 277          if (false !== ($fp = fopen($file, 'r'))) {
 278              include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php';
 279              $sql_queries = trim(fread($fp, filesize($file)));
 280              SqlUtility::splitMySqlFile($pieces, $sql_queries);
 281              foreach ($pieces as $query) {
 282                  // [0] contains the prefixed query

 283                  // [4] contains unprefixed table name

 284                  $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix());
 285                  if ($prefixed_query != false) {
 286                      $this->query($prefixed_query[0]);
 287                  }
 288              }
 289              return true;
 290          }
 291          return false;
 292      }
 293      
 294      /**

 295       * Get field name

 296       *

 297       * @param resource $result query result

 298       * @param int numerical field index

 299       * @return string

 300       */
 301  	function getFieldName($result, $offset)
 302      {
 303          return mysql_field_name($result, $offset);
 304      }
 305  
 306      /**

 307       * Get field type

 308       *

 309       * @param resource $result query result

 310       * @param int $offset numerical field index

 311       * @return string

 312       */
 313      function getFieldType($result, $offset)
 314      {
 315          return mysql_field_type($result, $offset);
 316      }
 317  
 318      /**

 319       * Get number of fields in result

 320       *

 321       * @param resource $result query result

 322       * @return int

 323       */
 324  	function getFieldsNum($result)
 325      {
 326          return mysql_num_fields($result);
 327      }
 328  }
 329  
 330  /**

 331   * Safe Connection to a MySQL database.

 332   * 

 333   * 

 334   * @author Kazumi Ono <onokazu@xoops.org>

 335   * @copyright copyright (c) 2000-2003 XOOPS.org

 336   * 

 337   * @package kernel

 338   * @subpackage database

 339   */
 340  class XoopsMySQLDatabaseSafe extends XoopsMySQLDatabase
 341  {
 342  
 343      /**

 344       * perform a query on the database

 345       * 

 346       * @param string $sql a valid MySQL query

 347       * @param int $limit number of records to return

 348       * @param int $start offset of first record to return

 349       * @return resource query result or FALSE if successful

 350       * or TRUE if successful and no result

 351       */
 352  	function query($sql, $limit=0, $start=0)
 353      {
 354          return $this->queryF($sql, $limit, $start);
 355      }
 356  }
 357  
 358  /**

 359   * Read-Only connection to a MySQL database.

 360   * 

 361   * This class allows only SELECT queries to be performed through its 

 362   * {@link query()} method for security reasons.

 363   * 

 364   * 

 365   * @author Kazumi Ono <onokazu@xoops.org>

 366   * @copyright copyright (c) 2000-2003 XOOPS.org

 367   * 

 368   * @package kernel

 369   * @subpackage database

 370   */
 371  class XoopsMySQLDatabaseProxy extends XoopsMySQLDatabase
 372  {
 373  
 374      /**

 375       * perform a query on the database

 376       * 

 377       * this method allows only SELECT queries for safety.

 378       * 

 379       * @param string $sql a valid MySQL query

 380       * @param int $limit number of records to return

 381       * @param int $start offset of first record to return

 382       * @return resource query result or FALSE if unsuccessful

 383       */
 384  	function query($sql, $limit=0, $start=0)
 385      {
 386          $sql = ltrim($sql);
 387          if (strtolower(substr($sql, 0, 6)) == 'select') {
 388          //if (preg_match("/^SELECT.*/i", $sql)) {

 389              return $this->queryF($sql, $limit, $start);
 390          }
 391          $this->logger->addQuery($sql, 'Database update not allowed during processing of a GET request', 0);
 392          return false;
 393      }
 394  }
 395  ?>


Généré le : Sun Nov 25 11:44:32 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics