00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00042 include_once XOOPS_ROOT_PATH."/class/database/database.php";
00043
00055 class XoopsMySQLDatabase extends XoopsDatabase
00056 {
00061 var $conn;
00062
00067 var $mPrepareQuery=null;
00068
00075 function connect($selectdb = true)
00076 {
00077 if (XOOPS_DB_PCONNECT == 1) {
00078 $this->conn = @mysql_pconnect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS);
00079 } else {
00080 $this->conn = @mysql_connect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS);
00081 }
00082
00083 if (!$this->conn) {
00084 $this->logger->addQuery('', $this->error(), $this->errno());
00085 return false;
00086 }
00087
00088 if($selectdb != false){
00089 if (!mysql_select_db(XOOPS_DB_NAME)) {
00090 $this->logger->addQuery('', $this->error(), $this->errno());
00091 return false;
00092 }
00093 }
00094 return true;
00095 }
00096
00106 function genId($sequence)
00107 {
00108 return 0;
00109 }
00110
00117 function fetchRow($result)
00118 {
00119 return @mysql_fetch_row($result);
00120 }
00121
00127 function fetchArray($result)
00128 {
00129 return @mysql_fetch_assoc( $result );
00130 }
00131
00137 function fetchBoth($result)
00138 {
00139 return @mysql_fetch_array( $result, MYSQL_BOTH );
00140 }
00141
00147 function getInsertId()
00148 {
00149 return mysql_insert_id($this->conn);
00150 }
00151
00158 function getRowsNum($result)
00159 {
00160 return @mysql_num_rows($result);
00161 }
00162
00168 function getAffectedRows()
00169 {
00170 return mysql_affected_rows($this->conn);
00171 }
00172
00177 function close()
00178 {
00179 mysql_close($this->conn);
00180 }
00181
00188 function freeRecordSet($result)
00189 {
00190 return mysql_free_result($result);
00191 }
00192
00198 function error()
00199 {
00200 return @mysql_error();
00201 }
00202
00208 function errno()
00209 {
00210 return @mysql_errno();
00211 }
00212
00219 function quoteString($str)
00220 {
00221 $str = "'".str_replace('\\"', '"', addslashes($str))."'";
00222 return $str;
00223 }
00224
00234 function &queryF($sql, $limit=0, $start=0)
00235 {
00236 if ( !empty($limit) ) {
00237 if (empty($start)) {
00238 $start = 0;
00239 }
00240 $sql = $sql. ' LIMIT '.(int)$start.', '.(int)$limit;
00241 }
00242 $result = mysql_query($sql, $this->conn);
00243 if ( $result ) {
00244 $this->logger->addQuery($sql);
00245 return $result;
00246 } else {
00247 $this->logger->addQuery($sql, $this->error(), $this->errno());
00248 $ret = false;
00249 return $ret;
00250 }
00251 }
00252
00265 function &query($sql, $limit=0, $start=0)
00266 {
00267
00268 }
00269
00277 function queryFromFile($file){
00278 if (false !== ($fp = fopen($file, 'r'))) {
00279 include_once XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php';
00280 $sql_queries = trim(fread($fp, filesize($file)));
00281 OldSqlUtility::splitMySqlFile($pieces, $sql_queries);
00282 foreach ($pieces as $query) {
00283
00284
00285 $prefixed_query = OldSqlUtility::prefixQuery(trim($query), $this->prefix());
00286 if ($prefixed_query != false) {
00287 $this->query($prefixed_query[0]);
00288 }
00289 }
00290 return true;
00291 }
00292 return false;
00293 }
00294
00302 function getFieldName($result, $offset)
00303 {
00304 return mysql_field_name($result, $offset);
00305 }
00306
00314 function getFieldType($result, $offset)
00315 {
00316 return mysql_field_type($result, $offset);
00317 }
00318
00325 function getFieldsNum($result)
00326 {
00327 return mysql_num_fields($result);
00328 }
00329
00330 function prepare($query)
00331 {
00332 $count=0;
00333 while(($pos=strpos($query,"?"))!==false) {
00334 $pre=substr($query,0,$pos);
00335 $after="";
00336 if($pos+1<=strlen($query))
00337 $after=substr($query,$pos+1);
00338
00339 $query=$pre."{".$count."}".$after;
00340 $count++;
00341 }
00342 $this->mPrepareQuery=$query;
00343 }
00344
00345 function bind_param()
00346 {
00347 if(func_num_args()<2)
00348 return;
00349
00350 $types=func_get_arg(0);
00351 $count=strlen($types);
00352 if(func_num_args()<$count)
00353 return;
00354
00355 $searches=array();
00356 $replaces=array();
00357 for($i=0;$i<$count;$i++) {
00358 $searches[$i]="{".$i."}";
00359 switch(substr($types,$i,1)) {
00360 case "i":
00361 $replaces[$i]=intval(func_get_arg($i+1));
00362 break;
00363
00364 case "s":
00365 $replaces[$i]=$this->quoteString(func_get_arg($i+1));
00366 break;
00367
00368 case "d":
00369 $replaces[$i]=doubleval(func_get_arg($i+1));
00370 break;
00371
00372 case "b":
00373
00374 die();
00375 }
00376 }
00377
00378 $this->mPrepareQuery=str_replace($searches,$replaces,$this->mPrepareQuery);
00379 }
00380
00381 function &execute()
00382 {
00383 $result=&$this->query($this->mPrepareQuery);
00384 $this->mPrepareQuery=null;
00385 return $result;
00386 }
00387
00388 function &executeF()
00389 {
00390 $result=&$this->queryF($this->mPrepareQuery);
00391 $this->mPrepareQuery=null;
00392 return $result;
00393 }
00394 }
00395
00406 class XoopsMySQLDatabaseSafe extends XoopsMySQLDatabase
00407 {
00408
00418 function &query($sql, $limit=0, $start=0)
00419 {
00420 $result =& $this->queryF($sql, $limit, $start);
00421 return $result;
00422 }
00423 }
00424
00438 class XoopsMySQLDatabaseProxy extends XoopsMySQLDatabase
00439 {
00440
00451 function &query($sql, $limit=0, $start=0)
00452 {
00453 $sql = ltrim($sql);
00454 if (strtolower(substr($sql, 0, 6)) == 'select') {
00455
00456 $ret = $this->queryF($sql, $limit, $start);
00457 return $ret;
00458 }
00459 $this->logger->addQuery($sql, 'Database update not allowed during processing of a GET request', 0);
00460
00461 $ret = false;
00462 return $ret;
00463 }
00464 }
00465 ?>