| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | **************************************************************************/ |
| 21 | |
| 22 | class XDB |
| 23 | { |
| 24 | private static $mysqli = null; |
| 25 | |
| 26 | public static function connect() |
| 27 | { |
| 28 | global $globals; |
| 29 | self::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb); |
| 30 | if ($globals->debug & DEBUG_BT) { |
| 31 | $bt = new PlBacktrace('MySQL'); |
| 32 | if (mysqli_connect_errno()) { |
| 33 | $bt->newEvent("MySQLI connection", 0, mysqli_connect_error()); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | self::$mysqli->autocommit(true); |
| 38 | self::$mysqli->set_charset($globals->dbcharset); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | public static function _prepare($args) |
| 43 | { |
| 44 | $query = array_map(Array('XDB', 'escape'), $args); |
| 45 | $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0])); |
| 46 | return call_user_func_array('sprintf', $query); |
| 47 | } |
| 48 | |
| 49 | public static function _reformatQuery($query) |
| 50 | { |
| 51 | $query = preg_split("/\n\\s*/", trim($query)); |
| 52 | $length = 0; |
| 53 | foreach ($query as $key=>$line) { |
| 54 | $local = -2; |
| 55 | if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches) |
| 56 | && $matches[1] != 'AND' && $matches[1] != 'OR') |
| 57 | { |
| 58 | $local = strlen($matches[1]); |
| 59 | $line = $matches[1] . ' ' . $matches[2]; |
| 60 | $length = max($length, $local); |
| 61 | } |
| 62 | $query[$key] = array($line, $local); |
| 63 | } |
| 64 | $res = ''; |
| 65 | foreach ($query as $array) { |
| 66 | list($line, $local) = $array; |
| 67 | $local = max(0, $length - $local); |
| 68 | $res .= str_repeat(' ', $local) . $line . "\n"; |
| 69 | $length += 2 * (substr_count($line, '(') - substr_count($line, ')')); |
| 70 | } |
| 71 | return $res; |
| 72 | } |
| 73 | |
| 74 | public static function _query($query) |
| 75 | { |
| 76 | global $globals; |
| 77 | |
| 78 | if (!self::$mysqli && !self::connect()) { |
| 79 | header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); |
| 80 | Platal::page()->kill('Impossible de se connecter à la base de données.'); |
| 81 | exit; |
| 82 | } |
| 83 | |
| 84 | if ($globals->debug & DEBUG_BT) { |
| 85 | $explain = array(); |
| 86 | if (strpos($query, 'FOUND_ROWS()') === false) { |
| 87 | $res = self::$mysqli->query("EXPLAIN $query"); |
| 88 | if ($res) { |
| 89 | while ($row = $res->fetch_assoc()) { |
| 90 | $explain[] = $row; |
| 91 | } |
| 92 | $res->free(); |
| 93 | } |
| 94 | } |
| 95 | PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query)); |
| 96 | } |
| 97 | |
| 98 | $res = XDB::$mysqli->query($query); |
| 99 | |
| 100 | if ($globals->debug & DEBUG_BT) { |
| 101 | PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : self::$mysqli->affected_rows, |
| 102 | self::$mysqli->error, |
| 103 | $explain); |
| 104 | } |
| 105 | |
| 106 | if ($res === false) { |
| 107 | header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); |
| 108 | if (strpos($query, 'INSERT') === false && strpos($query, 'UPDATE') === false |
| 109 | && strpos($query, 'REPLACE') === false && strpos($query, 'DELETE') === false) { |
| 110 | $text = 'Erreur lors de l\'interrogation de la base de données'; |
| 111 | } else { |
| 112 | $text = 'Erreur lors de l\'écriture dans la base de données'; |
| 113 | } |
| 114 | if ($globals->debug) { |
| 115 | $text .= '<pre>' . pl_entities(XDB::_reformatQuery($query)) . '</pre>'; |
| 116 | } else { |
| 117 | $file = fopen($globals->spoolroot . '/spool/tmp/query_errors', 'a'); |
| 118 | fwrite($file, '<pre>' . pl_entities(XDB::_reformatQuery($query)) . '</pre>' |
| 119 | . '<pre>' . XDB::$mysqli->error . '</pre>' . "\n"); |
| 120 | fclose($file); |
| 121 | } |
| 122 | Platal::page()->kill($text); |
| 123 | exit; |
| 124 | } |
| 125 | return $res; |
| 126 | } |
| 127 | |
| 128 | private static function queryv($query) |
| 129 | { |
| 130 | return new XOrgDBResult(self::_prepare($query)); |
| 131 | } |
| 132 | |
| 133 | public static function query() |
| 134 | { |
| 135 | return self::queryv(func_get_args()); |
| 136 | } |
| 137 | |
| 138 | public static function format() |
| 139 | { |
| 140 | return self::_prepare(func_get_args()); |
| 141 | } |
| 142 | |
| 143 | public static function execute() |
| 144 | { |
| 145 | global $globals; |
| 146 | $args = func_get_args(); |
| 147 | if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) { |
| 148 | return; |
| 149 | } |
| 150 | return self::_query(XDB::_prepare($args)); |
| 151 | } |
| 152 | |
| 153 | public static function iterator() |
| 154 | { |
| 155 | return new XOrgDBIterator(self::_prepare(func_get_args())); |
| 156 | } |
| 157 | |
| 158 | public static function iterRow() |
| 159 | { |
| 160 | return new XOrgDBIterator(self::_prepare(func_get_args()), MYSQL_NUM); |
| 161 | } |
| 162 | |
| 163 | private static function findQuery($params, $default = array()) |
| 164 | { |
| 165 | for ($i = 0 ; $i < count($default) ; ++$i) { |
| 166 | $is_query = false; |
| 167 | foreach (array('insert', 'select', 'replace', 'delete', 'update') as $kwd) { |
| 168 | if (stripos($params[0], $kwd) !== false) { |
| 169 | $is_query = true; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | if ($is_query) { |
| 174 | break; |
| 175 | } else { |
| 176 | $default[$i] = array_shift($params); |
| 177 | } |
| 178 | } |
| 179 | return array($default, $params); |
| 180 | } |
| 181 | |
| 182 | /** Fetch all rows returned by the given query. |
| 183 | * This functions can take 2 optional arguments (cf XOrgDBResult::fetchAllRow()). |
| 184 | * Optional arguments are given *before* the query. |
| 185 | */ |
| 186 | public static function fetchAllRow() |
| 187 | { |
| 188 | list($args, $query) = self::findQuery(func_get_args(), array(false, false)); |
| 189 | return self::queryv($query)->fetchAllRow($args[0], $args[1]); |
| 190 | } |
| 191 | |
| 192 | /** Fetch all rows returned by the given query. |
| 193 | * This functions can take 2 optional arguments (cf XOrgDBResult::fetchAllAssoc()). |
| 194 | * Optional arguments are given *before* the query. |
| 195 | */ |
| 196 | public static function fetchAllAssoc() |
| 197 | { |
| 198 | list($args, $query) = self::findQuery(func_get_args(), array(false, false)); |
| 199 | return self::queryv($query)->fetchAllAssoc($args[0], $args[1]); |
| 200 | } |
| 201 | |
| 202 | public static function fetchOneCell() |
| 203 | { |
| 204 | list($args, $query) = self::findQuery(func_get_args()); |
| 205 | return self::queryv($query)->fetchOneCell(); |
| 206 | } |
| 207 | |
| 208 | public static function fetchOneRow() |
| 209 | { |
| 210 | list($args, $query) = self::findQuery(func_get_args()); |
| 211 | return self::queryv($query)->fetchOneRow(); |
| 212 | } |
| 213 | |
| 214 | public static function fetchOneAssoc() |
| 215 | { |
| 216 | list($args, $query) = self::findQuery(func_get_args()); |
| 217 | return self::queryv($query)->fetchOneAssoc(); |
| 218 | } |
| 219 | |
| 220 | /** Fetch a column from the result of the given query. |
| 221 | * This functions can take 1 optional arguments (cf XOrgDBResult::fetchColumn()). |
| 222 | * Optional arguments are given *before* the query. |
| 223 | */ |
| 224 | public static function fetchColumn() |
| 225 | { |
| 226 | list($args, $query) = self::findQuery(func_get_args(), array(0)); |
| 227 | return self::queryv($query)->fetchColumn(); |
| 228 | } |
| 229 | |
| 230 | public static function insertId() |
| 231 | { |
| 232 | return self::$mysqli->insert_id; |
| 233 | } |
| 234 | |
| 235 | public static function errno() |
| 236 | { |
| 237 | return self::$mysqli->errno; |
| 238 | } |
| 239 | |
| 240 | public static function error() |
| 241 | { |
| 242 | return self::$mysqli->error; |
| 243 | } |
| 244 | |
| 245 | public static function affectedRows() |
| 246 | { |
| 247 | return self::$mysqli->affected_rows; |
| 248 | } |
| 249 | |
| 250 | public static function escape($var) |
| 251 | { |
| 252 | switch (gettype($var)) { |
| 253 | case 'boolean': |
| 254 | return $var ? 1 : 0; |
| 255 | |
| 256 | case 'integer': |
| 257 | case 'double': |
| 258 | case 'float': |
| 259 | return $var; |
| 260 | |
| 261 | case 'string': |
| 262 | return "'".addslashes($var)."'"; |
| 263 | |
| 264 | case 'NULL': |
| 265 | return 'NULL'; |
| 266 | |
| 267 | case 'object': |
| 268 | if ($var instanceof PlFlagSet) { |
| 269 | return "'" . addslashes($var->flags()) . "'"; |
| 270 | } |
| 271 | case 'array': |
| 272 | return "'".addslashes(serialize($var))."'"; |
| 273 | |
| 274 | default: |
| 275 | die(var_export($var, true).' is not a valid for a database entry'); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | class XOrgDBResult |
| 281 | { |
| 282 | |
| 283 | private $_res; |
| 284 | |
| 285 | public function __construct($query) |
| 286 | { |
| 287 | $this->_res = XDB::_query($query); |
| 288 | } |
| 289 | |
| 290 | public function free() |
| 291 | { |
| 292 | if ($this->_res) { |
| 293 | $this->_res->free(); |
| 294 | } |
| 295 | unset($this); |
| 296 | } |
| 297 | |
| 298 | protected function _fetchRow() |
| 299 | { |
| 300 | return $this->_res ? $this->_res->fetch_row() : null; |
| 301 | } |
| 302 | |
| 303 | protected function _fetchAssoc() |
| 304 | { |
| 305 | return $this->_res ? $this->_res->fetch_assoc() : null; |
| 306 | } |
| 307 | |
| 308 | public function fetchAllRow($id = false, $keep_array = false) |
| 309 | { |
| 310 | $result = Array(); |
| 311 | if (!$this->_res) { |
| 312 | return $result; |
| 313 | } |
| 314 | while (($data = $this->_res->fetch_row())) { |
| 315 | if ($id !== false) { |
| 316 | $key = $data[$id]; |
| 317 | unset($data[$id]); |
| 318 | if (!$keep_array && count($data) == 1) { |
| 319 | reset($data); |
| 320 | $result[$key] = current($data); |
| 321 | } else { |
| 322 | $result[$key] = $data; |
| 323 | } |
| 324 | } else { |
| 325 | $result[] = $data; |
| 326 | } |
| 327 | } |
| 328 | $this->free(); |
| 329 | return $result; |
| 330 | } |
| 331 | |
| 332 | public function fetchAllAssoc($id = false, $keep_array = false) |
| 333 | { |
| 334 | $result = Array(); |
| 335 | if (!$this->_res) { |
| 336 | return $result; |
| 337 | } |
| 338 | while (($data = $this->_res->fetch_assoc())) { |
| 339 | if ($id !== false) { |
| 340 | $key = $data[$id]; |
| 341 | unset($data[$id]); |
| 342 | if (!$keep_array && count($data) == 1) { |
| 343 | reset($data); |
| 344 | $result[$key] = current($data); |
| 345 | } else { |
| 346 | $result[$key] = $data; |
| 347 | } |
| 348 | } else { |
| 349 | $result[] = $data; |
| 350 | } |
| 351 | } |
| 352 | $this->free(); |
| 353 | return $result; |
| 354 | } |
| 355 | |
| 356 | public function fetchOneAssoc() |
| 357 | { |
| 358 | $tmp = $this->_fetchAssoc(); |
| 359 | $this->free(); |
| 360 | return $tmp; |
| 361 | } |
| 362 | |
| 363 | public function fetchOneRow() |
| 364 | { |
| 365 | $tmp = $this->_fetchRow(); |
| 366 | $this->free(); |
| 367 | return $tmp; |
| 368 | } |
| 369 | |
| 370 | public function fetchOneCell() |
| 371 | { |
| 372 | $tmp = $this->_fetchRow(); |
| 373 | $this->free(); |
| 374 | return $tmp[0]; |
| 375 | } |
| 376 | |
| 377 | public function fetchColumn($key = 0) |
| 378 | { |
| 379 | $res = Array(); |
| 380 | if (is_numeric($key)) { |
| 381 | while($tmp = $this->_fetchRow()) { |
| 382 | $res[] = $tmp[$key]; |
| 383 | } |
| 384 | } else { |
| 385 | while($tmp = $this->_fetchAssoc()) { |
| 386 | $res[] = $tmp[$key]; |
| 387 | } |
| 388 | } |
| 389 | $this->free(); |
| 390 | return $res; |
| 391 | } |
| 392 | |
| 393 | public function fetchOneField() |
| 394 | { |
| 395 | return $this->_res ? $this->_res->fetch_field() : null; |
| 396 | } |
| 397 | |
| 398 | public function fetchFields() |
| 399 | { |
| 400 | $res = array(); |
| 401 | while ($res[] = $this->fetchOneField()); |
| 402 | return $res; |
| 403 | } |
| 404 | |
| 405 | public function numRows() |
| 406 | { |
| 407 | return $this->_res ? $this->_res->num_rows : 0; |
| 408 | } |
| 409 | |
| 410 | public function fieldCount() |
| 411 | { |
| 412 | return $this->_res ? $this->_res->field_count : 0; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | require_once dirname(__FILE__) . '/pliterator.php'; |
| 417 | |
| 418 | class XOrgDBIterator extends XOrgDBResult implements PlIterator |
| 419 | { |
| 420 | private $_result; |
| 421 | private $_pos; |
| 422 | private $_total; |
| 423 | private $_fpos; |
| 424 | private $_fields; |
| 425 | private $_mode = MYSQL_ASSOC; |
| 426 | |
| 427 | public function __construct($query, $mode = MYSQL_ASSOC) |
| 428 | { |
| 429 | parent::__construct($query); |
| 430 | $this->_pos = 0; |
| 431 | $this->_total = $this->numRows(); |
| 432 | $this->_fpost = 0; |
| 433 | $this->_fields = $this->fieldCount(); |
| 434 | $this->_mode = $mode; |
| 435 | } |
| 436 | |
| 437 | public function next() |
| 438 | { |
| 439 | $this->_pos ++; |
| 440 | if ($this->_pos > $this->_total) { |
| 441 | $this->free(); |
| 442 | unset($this); |
| 443 | return null; |
| 444 | } |
| 445 | return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc(); |
| 446 | } |
| 447 | |
| 448 | public function first() |
| 449 | { |
| 450 | return $this->_pos == 1; |
| 451 | } |
| 452 | |
| 453 | public function last() |
| 454 | { |
| 455 | return $this->_pos == $this->_total; |
| 456 | } |
| 457 | |
| 458 | public function total() |
| 459 | { |
| 460 | return $this->_total; |
| 461 | } |
| 462 | |
| 463 | public function nextField() |
| 464 | { |
| 465 | $this->_fpos++; |
| 466 | if ($this->_fpos > $this->_fields) { |
| 467 | return null; |
| 468 | } |
| 469 | return $this->fetchOneField(); |
| 470 | } |
| 471 | |
| 472 | public function firstField() |
| 473 | { |
| 474 | return $this->_fpos == 1; |
| 475 | } |
| 476 | |
| 477 | public function lastField() |
| 478 | { |
| 479 | return $this->_fpos == $this->_fields; |
| 480 | } |
| 481 | |
| 482 | public function totalFields() |
| 483 | { |
| 484 | return $this->_fields; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 489 | ?> |