| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Rev: 249 $ | $LastChangedBy: brieb $ |
|---|
| 4 | $LastChangedDate: 2007-07-02 15:50:36 -0600 (Mon, 02 Jul 2007) $ |
|---|
| 5 | +-------------------------------------------------------------------------+ |
|---|
| 6 | | Copyright (c) 2004 - 2010, Kreotek LLC | |
|---|
| 7 | | All rights reserved. | |
|---|
| 8 | +-------------------------------------------------------------------------+ |
|---|
| 9 | | | |
|---|
| 10 | | Redistribution and use in source and binary forms, with or without | |
|---|
| 11 | | modification, are permitted provided that the following conditions are | |
|---|
| 12 | | met: | |
|---|
| 13 | | | |
|---|
| 14 | | - Redistributions of source code must retain the above copyright | |
|---|
| 15 | | notice, this list of conditions and the following disclaimer. | |
|---|
| 16 | | | |
|---|
| 17 | | - Redistributions in binary form must reproduce the above copyright | |
|---|
| 18 | | notice, this list of conditions and the following disclaimer in the | |
|---|
| 19 | | documentation and/or other materials provided with the distribution. | |
|---|
| 20 | | | |
|---|
| 21 | | - Neither the name of Kreotek LLC nor the names of its contributore may | |
|---|
| 22 | | be used to endorse or promote products derived from this software | |
|---|
| 23 | | without specific prior written permission. | |
|---|
| 24 | | | |
|---|
| 25 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|---|
| 26 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|---|
| 27 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
|---|
| 28 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|---|
| 29 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|---|
| 30 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|---|
| 31 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|---|
| 32 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|---|
| 33 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|---|
| 34 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|---|
| 35 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|---|
| 36 | | | |
|---|
| 37 | +-------------------------------------------------------------------------+ |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | class db{ |
|---|
| 41 | |
|---|
| 42 | // We may want to do more than connect via mysql. Currently only MySQL code |
|---|
| 43 | // is provided. Some functions may offer a swtich on type, but others |
|---|
| 44 | // are currently coded for MySQL only |
|---|
| 45 | var $type="mysql"; |
|---|
| 46 | |
|---|
| 47 | // mysql vars; |
|---|
| 48 | var $db_link; |
|---|
| 49 | var $hostname; |
|---|
| 50 | var $schema; |
|---|
| 51 | var $dbuser; |
|---|
| 52 | var $dbpass; |
|---|
| 53 | var $pconnect = true; |
|---|
| 54 | |
|---|
| 55 | var $showError = false; |
|---|
| 56 | var $logError = true; |
|---|
| 57 | var $stopOnError = true; |
|---|
| 58 | var $errorFormat = "xhtml"; |
|---|
| 59 | |
|---|
| 60 | var $error = NULL; |
|---|
| 61 | |
|---|
| 62 | function db($connect = true, $hostname = NULL, $schema = NULL, $user = NULL, $pass = NULL, $pconnect = NULL, $type = "mysql"){ |
|---|
| 63 | |
|---|
| 64 | if($type!="mysql") |
|---|
| 65 | $this->type=$type; |
|---|
| 66 | |
|---|
| 67 | switch($this->type){ |
|---|
| 68 | |
|---|
| 69 | default: |
|---|
| 70 | case "mysql": |
|---|
| 71 | |
|---|
| 72 | if(defined("MYSQL_SERVER")) |
|---|
| 73 | $this->hostname = MYSQL_SERVER; |
|---|
| 74 | |
|---|
| 75 | if($hostname!=NULL) |
|---|
| 76 | $this->hostname = $hostname; |
|---|
| 77 | |
|---|
| 78 | if(defined("MYSQL_DATABASE")) |
|---|
| 79 | $this->schema = MYSQL_DATABASE; |
|---|
| 80 | |
|---|
| 81 | if($schema!=NULL) |
|---|
| 82 | $this->schema = $schema; |
|---|
| 83 | |
|---|
| 84 | if(defined("MYSQL_USER")) |
|---|
| 85 | $this->dbuser = MYSQL_USER; |
|---|
| 86 | |
|---|
| 87 | if($schema!=NULL) |
|---|
| 88 | $this->dbuser = $user; |
|---|
| 89 | |
|---|
| 90 | if(defined("MYSQL_USERPASS")) |
|---|
| 91 | $this->dbpass = MYSQL_USERPASS; |
|---|
| 92 | |
|---|
| 93 | if($schema!=NULL) |
|---|
| 94 | $this->dbpass = $pass; |
|---|
| 95 | |
|---|
| 96 | if(defined("MYSQL_PCONNECT")) |
|---|
| 97 | $this->pconnect = MYSQL_PCONNECT; |
|---|
| 98 | |
|---|
| 99 | if($pconnect!=NULL) |
|---|
| 100 | $this->pconnect = $pconnect; |
|---|
| 101 | |
|---|
| 102 | break; |
|---|
| 103 | |
|---|
| 104 | }//end switch |
|---|
| 105 | |
|---|
| 106 | if($connect){ |
|---|
| 107 | if($this->connect()){ |
|---|
| 108 | |
|---|
| 109 | if($this->selectSchema()) |
|---|
| 110 | return $this->db_link; |
|---|
| 111 | else |
|---|
| 112 | return false; |
|---|
| 113 | |
|---|
| 114 | } else |
|---|
| 115 | return false; |
|---|
| 116 | } else |
|---|
| 117 | return true; |
|---|
| 118 | |
|---|
| 119 | }//end function init (db) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Establishes a connection to the database |
|---|
| 124 | * |
|---|
| 125 | * Establishes a connection to the database. If the {@link $pconnect} setting |
|---|
| 126 | * is set, it uses the mysql_pconnect (for persistennt connections). We pass |
|---|
| 127 | * connection flags of 65536 so that calling simple stored procedures will |
|---|
| 128 | * successfully return results |
|---|
| 129 | */ |
|---|
| 130 | function connect(){ |
|---|
| 131 | |
|---|
| 132 | if($this->pconnect) |
|---|
| 133 | $function = "mysql_pconnect"; |
|---|
| 134 | else |
|---|
| 135 | $function = "mysql_connect"; |
|---|
| 136 | |
|---|
| 137 | $this->db_link = @ $function($this->hostname, $this->dbuser, $this->dbpass, 65536); |
|---|
| 138 | |
|---|
| 139 | if(!$this->db_link){ |
|---|
| 140 | |
|---|
| 141 | $error = new appError(-400,"Could not connect to database server.\n\n".$this->getError(),"",$this->showError,$this->stopOnError,false,$this->errorFormat); |
|---|
| 142 | return false; |
|---|
| 143 | |
|---|
| 144 | } else |
|---|
| 145 | return $this->db_link; |
|---|
| 146 | |
|---|
| 147 | }//end function connect |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | function selectSchema($schema=NULL){ |
|---|
| 151 | //Selects the database (schema) to use |
|---|
| 152 | |
|---|
| 153 | if($schema!=NULL) |
|---|
| 154 | $this->schema=$schema; |
|---|
| 155 | |
|---|
| 156 | if(! @ mysql_select_db($this->schema,$this->db_link)){ |
|---|
| 157 | |
|---|
| 158 | $error = new appError(-410,"Could not open schema ".$this->schema,"",$this->showError,$this->stopOnError,false,$this->errorFormat); |
|---|
| 159 | return false; |
|---|
| 160 | |
|---|
| 161 | } else |
|---|
| 162 | return true; |
|---|
| 163 | |
|---|
| 164 | }//end function selectSchema |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | function query($sqlstatement){ |
|---|
| 168 | //issues a SQL query |
|---|
| 169 | |
|---|
| 170 | switch($this->type){ |
|---|
| 171 | |
|---|
| 172 | case "mysql": |
|---|
| 173 | if(!isset($this->db_link)) |
|---|
| 174 | if(!$this->db()) |
|---|
| 175 | die($this->error); |
|---|
| 176 | |
|---|
| 177 | $queryresult = @ mysql_query($sqlstatement,$this->db_link); |
|---|
| 178 | |
|---|
| 179 | if(!$queryresult){ |
|---|
| 180 | |
|---|
| 181 | $this->error = $this->getError($this->db_link); |
|---|
| 182 | $error = new appError(-420,$this->getError($this->db_link)."\n\nStatement: ".$sqlstatement,"",$this->showError,$this->stopOnError,$this->logError,$this->errorFormat); |
|---|
| 183 | return false; |
|---|
| 184 | |
|---|
| 185 | }//endif |
|---|
| 186 | |
|---|
| 187 | break; |
|---|
| 188 | |
|---|
| 189 | }//endswitch type |
|---|
| 190 | |
|---|
| 191 | //success, clear the error and return the query result pointer |
|---|
| 192 | $this->error = NULL; |
|---|
| 193 | return $queryresult; |
|---|
| 194 | |
|---|
| 195 | }//end function query |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | function setEncoding($encoding = "utf8"){ |
|---|
| 199 | //set the database character encoding |
|---|
| 200 | |
|---|
| 201 | switch($this->type){ |
|---|
| 202 | |
|---|
| 203 | case "mysql": |
|---|
| 204 | @ mysql_query("SET NAMES ".$encoding, $this->db_link); |
|---|
| 205 | break; |
|---|
| 206 | |
|---|
| 207 | }//endswitch |
|---|
| 208 | |
|---|
| 209 | }//end function setEncoding |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | function getError($link = NULL){ |
|---|
| 213 | //retrieve the last error from the database server |
|---|
| 214 | |
|---|
| 215 | switch($this->type){ |
|---|
| 216 | |
|---|
| 217 | case "mysql": |
|---|
| 218 | if($link) |
|---|
| 219 | $thereturn = @ mysql_error($link); |
|---|
| 220 | else |
|---|
| 221 | $thereturn = @ mysql_error(); |
|---|
| 222 | |
|---|
| 223 | break; |
|---|
| 224 | |
|---|
| 225 | }//end switch --type-- |
|---|
| 226 | |
|---|
| 227 | return $thereturn; |
|---|
| 228 | |
|---|
| 229 | }//end function getError |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | function numRows($queryresult){ |
|---|
| 233 | //retrieve the number of rows of an issued query. |
|---|
| 234 | |
|---|
| 235 | switch($this->type){ |
|---|
| 236 | |
|---|
| 237 | case "mysql": |
|---|
| 238 | $numrows = @ mysql_num_rows($queryresult); |
|---|
| 239 | |
|---|
| 240 | if(!is_numeric($numrows)){ |
|---|
| 241 | |
|---|
| 242 | $error= new appError(-430,"","Could Not Retrieve Rows.","",$this->showError,$this->stopOnError,$this->logError,$this->errorFormat); |
|---|
| 243 | return false; |
|---|
| 244 | |
|---|
| 245 | }//end if |
|---|
| 246 | |
|---|
| 247 | break; |
|---|
| 248 | |
|---|
| 249 | }//end case |
|---|
| 250 | |
|---|
| 251 | $this->error=NULL; |
|---|
| 252 | return $numrows; |
|---|
| 253 | |
|---|
| 254 | }//end function numRows |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | /** |
|---|
| 258 | * function encrypt |
|---|
| 259 | * |
|---|
| 260 | * construct a database command with a stored key to encrypt the |
|---|
| 261 | * parameter. (string values should be enclosed in single quotes (')) |
|---|
| 262 | * |
|---|
| 263 | * @param string $value value/fieldname to be encrypted |
|---|
| 264 | * @param string $encryptionKey An overriding encryptionKey |
|---|
| 265 | * @return string Database command to encrypt $value or $value itself if no |
|---|
| 266 | * non-null encryptionkey was given and the ENCRYPTION_KEY constant is not |
|---|
| 267 | * defined |
|---|
| 268 | */ |
|---|
| 269 | |
|---|
| 270 | function encrypt($value, $encryptionKey = NULL) { |
|---|
| 271 | |
|---|
| 272 | if($encryptionKey === NULL && defined("ENCRYPTION_KEY")) |
|---|
| 273 | $encryptionKey = ENCRYPTION_KEY; |
|---|
| 274 | |
|---|
| 275 | if($value == "") |
|---|
| 276 | $value = "''"; |
|---|
| 277 | |
|---|
| 278 | switch($this->type){ |
|---|
| 279 | |
|---|
| 280 | case "mysql": |
|---|
| 281 | |
|---|
| 282 | $return = "AES_ENCRYPT(".$value.",'".mysql_real_escape_string($encryptionKey)."')"; |
|---|
| 283 | |
|---|
| 284 | break; |
|---|
| 285 | |
|---|
| 286 | }//end switch |
|---|
| 287 | |
|---|
| 288 | if($encryptionKey) |
|---|
| 289 | return $return; |
|---|
| 290 | else |
|---|
| 291 | return $value; |
|---|
| 292 | |
|---|
| 293 | }//end method --encrypt-- |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | /** |
|---|
| 297 | * function decrypt |
|---|
| 298 | * |
|---|
| 299 | * construct a database command with a stored key to decrypt the |
|---|
| 300 | * parameter. (string values should be enclosed in single quotes (')) |
|---|
| 301 | * |
|---|
| 302 | * @param string $value value/fieldname to be decrypted |
|---|
| 303 | * @param string $encryptionKey An overriding encryptionKey |
|---|
| 304 | * @return string Database command to decrypt $value or $value if no |
|---|
| 305 | * non-null encryptionkey was given and the ENCRYPTION_KEY constant is not |
|---|
| 306 | * defined |
|---|
| 307 | */ |
|---|
| 308 | |
|---|
| 309 | function decrypt($value, $encryptionKey = NULL) { |
|---|
| 310 | |
|---|
| 311 | if($encryptionKey === NULL && defined("ENCRYPTION_KEY")) |
|---|
| 312 | $encryptionKey = ENCRYPTION_KEY; |
|---|
| 313 | |
|---|
| 314 | if($value == "") |
|---|
| 315 | $value = "''"; |
|---|
| 316 | |
|---|
| 317 | switch($this->type){ |
|---|
| 318 | |
|---|
| 319 | case "mysql": |
|---|
| 320 | |
|---|
| 321 | $return = "AES_DECRYPT(".$value.",'".mysql_real_escape_string($encryptionKey)."')"; |
|---|
| 322 | |
|---|
| 323 | break; |
|---|
| 324 | |
|---|
| 325 | }//end switch |
|---|
| 326 | |
|---|
| 327 | if($encryptionKey) |
|---|
| 328 | return $return; |
|---|
| 329 | else |
|---|
| 330 | return $value; |
|---|
| 331 | |
|---|
| 332 | }//end method --decrypt-- |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | function fetchArray($queryresult){ |
|---|
| 336 | //Fetches associative array of current row from query result |
|---|
| 337 | |
|---|
| 338 | switch($this->type){ |
|---|
| 339 | |
|---|
| 340 | case "mysql": |
|---|
| 341 | $row = @ mysql_fetch_assoc($queryresult); |
|---|
| 342 | break; |
|---|
| 343 | |
|---|
| 344 | }//endswitch |
|---|
| 345 | |
|---|
| 346 | return $row; |
|---|
| 347 | |
|---|
| 348 | }//end function fetchArray |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | function startTransaction(){ |
|---|
| 352 | // Start a transaction |
|---|
| 353 | |
|---|
| 354 | switch($this->type){ |
|---|
| 355 | |
|---|
| 356 | case "mysql": |
|---|
| 357 | $this->query("START TRANSACTION;"); |
|---|
| 358 | break; |
|---|
| 359 | |
|---|
| 360 | }//end switch |
|---|
| 361 | |
|---|
| 362 | }//end function startTransaction |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | function commitTransaction(){ |
|---|
| 366 | // commits a started transaction |
|---|
| 367 | |
|---|
| 368 | switch($this->type){ |
|---|
| 369 | |
|---|
| 370 | case "mysql": |
|---|
| 371 | $this->query("COMMIT;"); |
|---|
| 372 | break; |
|---|
| 373 | |
|---|
| 374 | }//end switch |
|---|
| 375 | |
|---|
| 376 | }//end function commitTransaction |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | function rollbackTransaction(){ |
|---|
| 380 | // roll back any changes in a transaction |
|---|
| 381 | |
|---|
| 382 | switch($this->type){ |
|---|
| 383 | |
|---|
| 384 | case "mysql": |
|---|
| 385 | $this->query("ROLLBACK;"); |
|---|
| 386 | break; |
|---|
| 387 | |
|---|
| 388 | }//end switch |
|---|
| 389 | |
|---|
| 390 | }//end function rollbackTransaction |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | function seek($queryresult,$rownum){ |
|---|
| 394 | // moves the internal pointer of the current record |
|---|
| 395 | // on a query result to a specific location |
|---|
| 396 | |
|---|
| 397 | switch($this->type){ |
|---|
| 398 | |
|---|
| 399 | case "mysql": |
|---|
| 400 | $thereturn=@ mysql_data_seek($queryresult,$rownum); |
|---|
| 401 | break; |
|---|
| 402 | |
|---|
| 403 | }//endswitch |
|---|
| 404 | |
|---|
| 405 | return $thereturn; |
|---|
| 406 | |
|---|
| 407 | }//end function seek |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | function numFields($queryresult){ |
|---|
| 411 | // return the number of fields a query result has |
|---|
| 412 | |
|---|
| 413 | switch($this->type){ |
|---|
| 414 | |
|---|
| 415 | case "mysql": |
|---|
| 416 | $thereturn=@ mysql_num_fields($queryresult); |
|---|
| 417 | break; |
|---|
| 418 | |
|---|
| 419 | }//endswitch |
|---|
| 420 | |
|---|
| 421 | return $thereturn; |
|---|
| 422 | |
|---|
| 423 | }//end function numFields |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | function fieldTable($queryresult,$offset){ |
|---|
| 427 | //return the name of the table that a specified field is in |
|---|
| 428 | // pass a query result and a numerical field offset |
|---|
| 429 | |
|---|
| 430 | switch($this->type){ |
|---|
| 431 | |
|---|
| 432 | case "mysql": |
|---|
| 433 | $thereturn=@ mysql_field_table($queryresult,$offset); |
|---|
| 434 | break; |
|---|
| 435 | |
|---|
| 436 | }//endswitch |
|---|
| 437 | |
|---|
| 438 | return $thereturn; |
|---|
| 439 | |
|---|
| 440 | }//end function fieldTable |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | function fieldName($queryresult,$offset){ |
|---|
| 444 | //return the name of the field at a specified offset |
|---|
| 445 | |
|---|
| 446 | switch($this->type){ |
|---|
| 447 | |
|---|
| 448 | case "mysql": |
|---|
| 449 | $thereturn=@ mysql_field_name($queryresult,$offset); |
|---|
| 450 | break; |
|---|
| 451 | |
|---|
| 452 | }//end case |
|---|
| 453 | |
|---|
| 454 | return $thereturn; |
|---|
| 455 | |
|---|
| 456 | }//end function fieldName |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | function tableInfo($tablename){ |
|---|
| 460 | // returns a multi-dimensional array describing the fields in a |
|---|
| 461 | // provided table name |
|---|
| 462 | |
|---|
| 463 | $thereturn = false; |
|---|
| 464 | |
|---|
| 465 | switch($this->type){ |
|---|
| 466 | |
|---|
| 467 | case "mysql": |
|---|
| 468 | $queryresult = @ mysql_list_fields($this->schema,$tablename); |
|---|
| 469 | if($queryresult){ |
|---|
| 470 | |
|---|
| 471 | for($offset = 0; $offset < mysql_num_fields($queryresult); ++$offset){ |
|---|
| 472 | |
|---|
| 473 | $name = $this->fieldName($queryresult,$offset); |
|---|
| 474 | $thereturn[$name]["type"] = @ mysql_field_type($queryresult,$offset); |
|---|
| 475 | $thereturn[$name]["length"] = mysql_field_len($queryresult,$offset); |
|---|
| 476 | $thereturn[$name]["flags"] = mysql_field_flags($queryresult,$offset); |
|---|
| 477 | |
|---|
| 478 | }//endfor |
|---|
| 479 | |
|---|
| 480 | }//endif |
|---|
| 481 | |
|---|
| 482 | break; |
|---|
| 483 | |
|---|
| 484 | }//end case |
|---|
| 485 | |
|---|
| 486 | return $thereturn; |
|---|
| 487 | |
|---|
| 488 | }//end function tableInfo |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | function insertId(){ |
|---|
| 492 | //return the id of the last inserted record |
|---|
| 493 | |
|---|
| 494 | $thereturn = false; |
|---|
| 495 | |
|---|
| 496 | switch($this->type){ |
|---|
| 497 | |
|---|
| 498 | case "mysql": |
|---|
| 499 | $thereturn = @ mysql_insert_id($this->db_link); |
|---|
| 500 | break; |
|---|
| 501 | |
|---|
| 502 | }//endswitch |
|---|
| 503 | |
|---|
| 504 | return $thereturn; |
|---|
| 505 | |
|---|
| 506 | }//end function insertId |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | function affectedRows(){ |
|---|
| 510 | // return the number of affected rows, typically from an update |
|---|
| 511 | // or a delete |
|---|
| 512 | |
|---|
| 513 | $thereturn = false; |
|---|
| 514 | |
|---|
| 515 | switch($this->type){ |
|---|
| 516 | |
|---|
| 517 | case "mysql": |
|---|
| 518 | $thereturn = @ mysql_affected_rows($this->db_link); |
|---|
| 519 | break; |
|---|
| 520 | |
|---|
| 521 | }//endswitch |
|---|
| 522 | |
|---|
| 523 | return $thereturn; |
|---|
| 524 | |
|---|
| 525 | }//end function affectedRows |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | function processSQLFile($fileName){ |
|---|
| 529 | // process a standard .sql file. Should be able to handle |
|---|
| 530 | // comments, and semicolon within quotes. Returns an object with |
|---|
| 531 | // statistics, and possible errors. |
|---|
| 532 | // Will not work with double quoted variables, only single quoted. |
|---|
| 533 | |
|---|
| 534 | $filePointer = @ fopen($fileName, "r"); |
|---|
| 535 | |
|---|
| 536 | $return = new stdClass(); |
|---|
| 537 | $return->numQueries = 0; |
|---|
| 538 | $return->errors = array(); |
|---|
| 539 | |
|---|
| 540 | $inParents = false; |
|---|
| 541 | $sqlstatement = ""; |
|---|
| 542 | $lineNumber = 1; |
|---|
| 543 | |
|---|
| 544 | $this->showError = false; |
|---|
| 545 | $this->stopOnError = false; |
|---|
| 546 | |
|---|
| 547 | if(!$filePointer){ |
|---|
| 548 | //could not open file |
|---|
| 549 | |
|---|
| 550 | $return->errors[] = "Could Not Open File: '".$fileName."'"; |
|---|
| 551 | return $return; |
|---|
| 552 | |
|---|
| 553 | }//end if |
|---|
| 554 | |
|---|
| 555 | while($line = @ fgets($filePointer, 65536)){ |
|---|
| 556 | |
|---|
| 557 | // need to convert DOS or Mac line breaks |
|---|
| 558 | $line = preg_replace("/\r\n$/", "\n", $line); |
|---|
| 559 | $line = preg_replace("/\r$/", "\n", $line); |
|---|
| 560 | |
|---|
| 561 | // ignore comment lines, but only if they are not in quotes |
|---|
| 562 | if(!$inParents){ |
|---|
| 563 | |
|---|
| 564 | $skipline = false; |
|---|
| 565 | if(trim($line) == "" || strpos($line, "#") === 0 || strpos($line, "--") === 0) |
|---|
| 566 | $skipline = true; |
|---|
| 567 | |
|---|
| 568 | if($skipline){ |
|---|
| 569 | |
|---|
| 570 | $lineNumber++; |
|---|
| 571 | continue; |
|---|
| 572 | |
|---|
| 573 | }//endif |
|---|
| 574 | |
|---|
| 575 | }//endif |
|---|
| 576 | |
|---|
| 577 | // remove double backslashes before we count quotes |
|---|
| 578 | $deslashedLine = str_replace("\\\\", "", $line); |
|---|
| 579 | |
|---|
| 580 | // count single quotes and backslashed sing quotes so we can |
|---|
| 581 | // determine if any semicolons represent end of line |
|---|
| 582 | $parents = substr_count($deslashedLine, "'") - substr_count($deslashedLine, "\\'"); |
|---|
| 583 | if ($parents%2!=0) |
|---|
| 584 | $inParents = !$inParents; |
|---|
| 585 | |
|---|
| 586 | $sqlstatement .= $line; |
|---|
| 587 | |
|---|
| 588 | if (preg_match("/;$/", trim($line)) && !$inParents) { |
|---|
| 589 | // run the query. If there is an error, log it and the |
|---|
| 590 | // line number it started on |
|---|
| 591 | |
|---|
| 592 | $this->query(trim($sqlstatement)); |
|---|
| 593 | |
|---|
| 594 | if($this->error) |
|---|
| 595 | $return->errors[] = "Error Processing file '".$fileName."' on line ".$lineNumber.": ".$this->error."\n\n SQL Statement: '".$sqlstatement."'"; |
|---|
| 596 | |
|---|
| 597 | $return->numQueries++; |
|---|
| 598 | $sqlstatement = ""; |
|---|
| 599 | |
|---|
| 600 | }//end if |
|---|
| 601 | |
|---|
| 602 | $lineNumber++; |
|---|
| 603 | |
|---|
| 604 | }//endwhile |
|---|
| 605 | |
|---|
| 606 | @ fclose($filePointer); |
|---|
| 607 | |
|---|
| 608 | return $return; |
|---|
| 609 | |
|---|
| 610 | }//end function processSQLFile |
|---|
| 611 | |
|---|
| 612 | }//end db class |
|---|
| 613 | ?> |
|---|