| 1 | <?php |
|---|
| 2 | // $Id: mysqldatabase.php,v 1.3 2006/05/01 02:37:24 onokazu Exp $ |
|---|
| 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 | /** |
|---|
| 32 | * @package kernel |
|---|
| 33 | * @subpackage database |
|---|
| 34 | * |
|---|
| 35 | * @author Kazumi Ono <[email protected]> |
|---|
| 36 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 37 | */ |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * base class |
|---|
| 41 | */ |
|---|
| 42 | include_once XOOPS_ROOT_PATH."/class/database/database.php"; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * connection to a mysql database |
|---|
| 46 | * |
|---|
| 47 | * @abstract |
|---|
| 48 | * |
|---|
| 49 | * @author Kazumi Ono <[email protected]> |
|---|
| 50 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 51 | * |
|---|
| 52 | * @package kernel |
|---|
| 53 | * @subpackage database |
|---|
| 54 | */ |
|---|
| 55 | class XoopsMySQLDatabase extends XoopsDatabase |
|---|
| 56 | { |
|---|
| 57 | /** |
|---|
| 58 | * Database connection |
|---|
| 59 | * @var resource |
|---|
| 60 | */ |
|---|
| 61 | var $conn; |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * connect to the database |
|---|
| 65 | * |
|---|
| 66 | * @param bool $selectdb select the database now? |
|---|
| 67 | * @return bool successful? |
|---|
| 68 | */ |
|---|
| 69 | function connect($selectdb = true) |
|---|
| 70 | { |
|---|
| 71 | if (XOOPS_DB_PCONNECT == 1) { |
|---|
| 72 | $this->conn = @mysql_pconnect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS); |
|---|
| 73 | } else { |
|---|
| 74 | $this->conn = @mysql_connect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | if (!$this->conn) { |
|---|
| 78 | $this->logger->addQuery('', $this->error(), $this->errno()); |
|---|
| 79 | return false; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | if($selectdb != false){ |
|---|
| 83 | if (!mysql_select_db(XOOPS_DB_NAME)) { |
|---|
| 84 | $this->logger->addQuery('', $this->error(), $this->errno()); |
|---|
| 85 | return false; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * generate an ID for a new row |
|---|
| 93 | * |
|---|
| 94 | * This is for compatibility only. Will always return 0, because MySQL supports |
|---|
| 95 | * autoincrement for primary keys. |
|---|
| 96 | * |
|---|
| 97 | * @param string $sequence name of the sequence from which to get the next ID |
|---|
| 98 | * @return int always 0, because mysql has support for autoincrement |
|---|
| 99 | */ |
|---|
| 100 | function genId($sequence) |
|---|
| 101 | { |
|---|
| 102 | return 0; // will use auto_increment |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Get a result row as an enumerated array |
|---|
| 107 | * |
|---|
| 108 | * @param resource $result |
|---|
| 109 | * @return array |
|---|
| 110 | */ |
|---|
| 111 | function fetchRow($result) |
|---|
| 112 | { |
|---|
| 113 | return @mysql_fetch_row($result); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Fetch a result row as an associative array |
|---|
| 118 | * |
|---|
| 119 | * @return array |
|---|
| 120 | */ |
|---|
| 121 | function fetchArray($result) |
|---|
| 122 | { |
|---|
| 123 | return @mysql_fetch_assoc( $result ); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Fetch a result row as an associative array |
|---|
| 128 | * |
|---|
| 129 | * @return array |
|---|
| 130 | */ |
|---|
| 131 | function fetchBoth($result) |
|---|
| 132 | { |
|---|
| 133 | return @mysql_fetch_array( $result, MYSQL_BOTH ); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Get the ID generated from the previous INSERT operation |
|---|
| 138 | * |
|---|
| 139 | * @return int |
|---|
| 140 | */ |
|---|
| 141 | function getInsertId() |
|---|
| 142 | { |
|---|
| 143 | return mysql_insert_id($this->conn); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Get number of rows in result |
|---|
| 148 | * |
|---|
| 149 | * @param resource query result |
|---|
| 150 | * @return int |
|---|
| 151 | */ |
|---|
| 152 | function getRowsNum($result) |
|---|
| 153 | { |
|---|
| 154 | return @mysql_num_rows($result); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * Get number of affected rows |
|---|
| 159 | * |
|---|
| 160 | * @return int |
|---|
| 161 | */ |
|---|
| 162 | function getAffectedRows() |
|---|
| 163 | { |
|---|
| 164 | return mysql_affected_rows($this->conn); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Close MySQL connection |
|---|
| 169 | * |
|---|
| 170 | */ |
|---|
| 171 | function close() |
|---|
| 172 | { |
|---|
| 173 | mysql_close($this->conn); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * will free all memory associated with the result identifier result. |
|---|
| 178 | * |
|---|
| 179 | * @param resource query result |
|---|
| 180 | * @return bool TRUE on success or FALSE on failure. |
|---|
| 181 | */ |
|---|
| 182 | function freeRecordSet($result) |
|---|
| 183 | { |
|---|
| 184 | return mysql_free_result($result); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Returns the text of the error message from previous MySQL operation |
|---|
| 189 | * |
|---|
| 190 | * @return bool Returns the error text from the last MySQL function, or '' (the empty string) if no error occurred. |
|---|
| 191 | */ |
|---|
| 192 | function error() |
|---|
| 193 | { |
|---|
| 194 | return @mysql_error(); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Returns the numerical value of the error message from previous MySQL operation |
|---|
| 199 | * |
|---|
| 200 | * @return int Returns the error number from the last MySQL function, or 0 (zero) if no error occurred. |
|---|
| 201 | */ |
|---|
| 202 | function errno() |
|---|
| 203 | { |
|---|
| 204 | return @mysql_errno(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Returns escaped string text with single quotes around it to be safely stored in database |
|---|
| 209 | * |
|---|
| 210 | * @param string $str unescaped string text |
|---|
| 211 | * @return string escaped string text with single quotes around |
|---|
| 212 | */ |
|---|
| 213 | function quoteString($str) |
|---|
| 214 | { |
|---|
| 215 | $str = "'".str_replace('\\"', '"', addslashes($str))."'"; |
|---|
| 216 | return $str; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** |
|---|
| 220 | * perform a query on the database |
|---|
| 221 | * |
|---|
| 222 | * @param string $sql a valid MySQL query |
|---|
| 223 | * @param int $limit number of records to return |
|---|
| 224 | * @param int $start offset of first record to return |
|---|
| 225 | * @return resource query result or FALSE if successful |
|---|
| 226 | * or TRUE if successful and no result |
|---|
| 227 | */ |
|---|
| 228 | function &queryF($sql, $limit=0, $start=0) |
|---|
| 229 | { |
|---|
| 230 | if ( !empty($limit) ) { |
|---|
| 231 | if (empty($start)) { |
|---|
| 232 | $start = 0; |
|---|
| 233 | } |
|---|
| 234 | $sql = $sql. ' LIMIT '.(int)$start.', '.(int)$limit; |
|---|
| 235 | } |
|---|
| 236 | $result = mysql_query($sql, $this->conn); |
|---|
| 237 | if ( $result ) { |
|---|
| 238 | $this->logger->addQuery($sql); |
|---|
| 239 | return $result; |
|---|
| 240 | } else { |
|---|
| 241 | $this->logger->addQuery($sql, $this->error(), $this->errno()); |
|---|
| 242 | $ret = false; |
|---|
| 243 | return $ret; |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | * perform a query |
|---|
| 249 | * |
|---|
| 250 | * This method is empty and does nothing! It should therefore only be |
|---|
| 251 | * used if nothing is exactly what you want done! ;-) |
|---|
| 252 | * |
|---|
| 253 | * @param string $sql a valid MySQL query |
|---|
| 254 | * @param int $limit number of records to return |
|---|
| 255 | * @param int $start offset of first record to return |
|---|
| 256 | * |
|---|
| 257 | * @abstract |
|---|
| 258 | */ |
|---|
| 259 | function &query($sql, $limit=0, $start=0) |
|---|
| 260 | { |
|---|
| 261 | |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /** |
|---|
| 265 | * perform queries from SQL dump file in a batch |
|---|
| 266 | * |
|---|
| 267 | * @param string $file file path to an SQL dump file |
|---|
| 268 | * |
|---|
| 269 | * @return bool FALSE if failed reading SQL file or TRUE if the file has been read and queries executed |
|---|
| 270 | */ |
|---|
| 271 | function queryFromFile($file){ |
|---|
| 272 | if (false !== ($fp = fopen($file, 'r'))) { |
|---|
| 273 | include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php'; |
|---|
| 274 | $sql_queries = trim(fread($fp, filesize($file))); |
|---|
| 275 | SqlUtility::splitMySqlFile($pieces, $sql_queries); |
|---|
| 276 | foreach ($pieces as $query) { |
|---|
| 277 | // [0] contains the prefixed query |
|---|
| 278 | // [4] contains unprefixed table name |
|---|
| 279 | $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix()); |
|---|
| 280 | if ($prefixed_query != false) { |
|---|
| 281 | $this->query($prefixed_query[0]); |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | return true; |
|---|
| 285 | } |
|---|
| 286 | return false; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | * Get field name |
|---|
| 291 | * |
|---|
| 292 | * @param resource $result query result |
|---|
| 293 | * @param int numerical field index |
|---|
| 294 | * @return string |
|---|
| 295 | */ |
|---|
| 296 | function getFieldName($result, $offset) |
|---|
| 297 | { |
|---|
| 298 | return mysql_field_name($result, $offset); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | /** |
|---|
| 302 | * Get field type |
|---|
| 303 | * |
|---|
| 304 | * @param resource $result query result |
|---|
| 305 | * @param int $offset numerical field index |
|---|
| 306 | * @return string |
|---|
| 307 | */ |
|---|
| 308 | function getFieldType($result, $offset) |
|---|
| 309 | { |
|---|
| 310 | return mysql_field_type($result, $offset); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | * Get number of fields in result |
|---|
| 315 | * |
|---|
| 316 | * @param resource $result query result |
|---|
| 317 | * @return int |
|---|
| 318 | */ |
|---|
| 319 | function getFieldsNum($result) |
|---|
| 320 | { |
|---|
| 321 | return mysql_num_fields($result); |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | /** |
|---|
| 326 | * Safe Connection to a MySQL database. |
|---|
| 327 | * |
|---|
| 328 | * |
|---|
| 329 | * @author Kazumi Ono <[email protected]> |
|---|
| 330 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 331 | * |
|---|
| 332 | * @package kernel |
|---|
| 333 | * @subpackage database |
|---|
| 334 | */ |
|---|
| 335 | class XoopsMySQLDatabaseSafe extends XoopsMySQLDatabase |
|---|
| 336 | { |
|---|
| 337 | |
|---|
| 338 | /** |
|---|
| 339 | * perform a query on the database |
|---|
| 340 | * |
|---|
| 341 | * @param string $sql a valid MySQL query |
|---|
| 342 | * @param int $limit number of records to return |
|---|
| 343 | * @param int $start offset of first record to return |
|---|
| 344 | * @return resource query result or FALSE if successful |
|---|
| 345 | * or TRUE if successful and no result |
|---|
| 346 | */ |
|---|
| 347 | function &query($sql, $limit=0, $start=0) |
|---|
| 348 | { |
|---|
| 349 | $ret =& $this->queryF($sql, $limit, $start); |
|---|
| 350 | return $ret; |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | /** |
|---|
| 355 | * Read-Only connection to a MySQL database. |
|---|
| 356 | * |
|---|
| 357 | * This class allows only SELECT queries to be performed through its |
|---|
| 358 | * {@link query()} method for security reasons. |
|---|
| 359 | * |
|---|
| 360 | * |
|---|
| 361 | * @author Kazumi Ono <[email protected]> |
|---|
| 362 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 363 | * |
|---|
| 364 | * @package kernel |
|---|
| 365 | * @subpackage database |
|---|
| 366 | */ |
|---|
| 367 | class XoopsMySQLDatabaseProxy extends XoopsMySQLDatabase |
|---|
| 368 | { |
|---|
| 369 | |
|---|
| 370 | /** |
|---|
| 371 | * perform a query on the database |
|---|
| 372 | * |
|---|
| 373 | * this method allows only SELECT queries for safety. |
|---|
| 374 | * |
|---|
| 375 | * @param string $sql a valid MySQL query |
|---|
| 376 | * @param int $limit number of records to return |
|---|
| 377 | * @param int $start offset of first record to return |
|---|
| 378 | * @return resource query result or FALSE if unsuccessful |
|---|
| 379 | */ |
|---|
| 380 | function &query($sql, $limit=0, $start=0) |
|---|
| 381 | { |
|---|
| 382 | $ret = false; |
|---|
| 383 | $sql = ltrim($sql); |
|---|
| 384 | if (strtolower(substr($sql, 0, 6)) == 'select') { |
|---|
| 385 | //if (preg_match("/^SELECT.*/i", $sql)) { |
|---|
| 386 | $ret =& $this->queryF($sql, $limit, $start); |
|---|
| 387 | } else { |
|---|
| 388 | $this->logger->addQuery($sql, 'Database update not allowed during processing of a GET request', 0); |
|---|
| 389 | } |
|---|
| 390 | return $ret; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | ?> |
|---|