[4967] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[4967] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
| 23 | |
---|
[6090] | 24 | define('REQUIRED_PDO_SQLITE_VERSION', '3.0.0'); |
---|
[4967] | 25 | define('DB_ENGINE', 'SQLite'); |
---|
| 26 | |
---|
| 27 | define('DB_REGEX_OPERATOR', 'REGEXP'); |
---|
| 28 | define('DB_RANDOM_FUNCTION', 'RANDOM'); |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * |
---|
| 32 | * simple functions |
---|
| 33 | * |
---|
| 34 | */ |
---|
| 35 | |
---|
[5236] | 36 | function pwg_db_connect($host, $user, $password, $database) |
---|
[4967] | 37 | { |
---|
| 38 | global $conf; |
---|
| 39 | |
---|
| 40 | $db_file = sprintf('sqlite:%s/%s.db', $conf['local_data_dir'], $database); |
---|
| 41 | |
---|
[5236] | 42 | $link = new PDO($db_file); |
---|
| 43 | if (!$link) |
---|
| 44 | { |
---|
| 45 | throw new Exception('Connection to server succeed, but it was impossible to connect to database'); |
---|
[4967] | 46 | } |
---|
| 47 | |
---|
| 48 | $link->sqliteCreateFunction('now', 'pwg_now', 0); |
---|
| 49 | $link->sqliteCreateFunction('unix_timestamp', 'pwg_unix_timestamp', 0); |
---|
| 50 | $link->sqliteCreateFunction('md5', 'md5', 1); |
---|
| 51 | $link->sqliteCreateFunction('if', 'pwg_if', 3); |
---|
| 52 | |
---|
| 53 | $link->sqliteCreateFunction('regexp', 'pwg_regexp', 2); |
---|
| 54 | |
---|
| 55 | return $link; |
---|
| 56 | } |
---|
| 57 | |
---|
[6090] | 58 | function pwg_db_check_version() |
---|
| 59 | { |
---|
| 60 | $current_version = pwg_get_db_version(); |
---|
| 61 | if (version_compare($current_version, REQUIRED_PDO_SQLITE_VERSION, '<')) |
---|
| 62 | { |
---|
| 63 | fatal_error( |
---|
| 64 | sprintf( |
---|
| 65 | 'your database version is too old, you have "%s" and you need at least "%s"', |
---|
| 66 | $current_version, |
---|
| 67 | REQUIRED_PDO_SQLITE_VERSION |
---|
| 68 | ) |
---|
| 69 | ); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
[4967] | 73 | function pwg_db_check_charset() |
---|
| 74 | { |
---|
| 75 | return true; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | function pwg_get_db_version() |
---|
| 79 | { |
---|
| 80 | global $pwg_db_link; |
---|
| 81 | |
---|
| 82 | return $pwg_db_link->getAttribute(PDO::ATTR_SERVER_VERSION); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | function pwg_query($query) |
---|
| 86 | { |
---|
| 87 | global $conf,$page,$debug,$t2,$pwg_db_link; |
---|
| 88 | |
---|
| 89 | $start = get_moment(); |
---|
| 90 | |
---|
| 91 | $truncate_pattern = '`truncate(.*)`i'; |
---|
| 92 | $insert_pattern = '`(INSERT INTO [^)]*\)\s*VALUES)(\([^)]*\))\s*,\s*(.*)`mi'; |
---|
| 93 | |
---|
| 94 | if (preg_match($truncate_pattern, $query, $matches)) |
---|
| 95 | { |
---|
| 96 | $query = str_replace('TRUNCATE TABLE', 'DELETE FROM', $query); |
---|
| 97 | $truncate_query = true; |
---|
| 98 | ($result = $pwg_db_link->exec($query)) or die($query."\n<br>".$pwg_db_link->errorInfo()); |
---|
| 99 | } |
---|
| 100 | elseif (preg_match($insert_pattern, $query, $matches)) |
---|
| 101 | { |
---|
| 102 | $base_query = substr($query, 0, strlen($matches[1])+1); |
---|
| 103 | $values_pattern = '`\)\s*,\s*\(`'; |
---|
| 104 | $values = preg_split($values_pattern, substr($query, strlen($matches[1])+1)); |
---|
| 105 | $values[0] = substr($values[0], 1); |
---|
| 106 | $values[count($values)-1] = substr($values[count($values)-1], |
---|
| 107 | 0, |
---|
| 108 | strlen($values[count($values)-1])-1 |
---|
| 109 | ); |
---|
| 110 | for ($n=0;$n<count($values);$n++) |
---|
| 111 | { |
---|
| 112 | $query = $base_query . '('. $values[$n] . ")\n;"; |
---|
| 113 | ($result = $pwg_db_link->query($query)) |
---|
| 114 | or die($query."\n<br>".$pwg_db_link->lastErrorMsg()); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | ($result = $pwg_db_link->query($query)) |
---|
| 120 | or die($query."\n<br>".$pwg_db_link->errorInfo()); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | $time = get_moment() - $start; |
---|
| 124 | |
---|
| 125 | if (!isset($page['count_queries'])) |
---|
| 126 | { |
---|
| 127 | $page['count_queries'] = 0; |
---|
| 128 | $page['queries_time'] = 0; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | $page['count_queries']++; |
---|
| 132 | $page['queries_time']+= $time; |
---|
| 133 | |
---|
| 134 | if ($conf['show_queries']) |
---|
| 135 | { |
---|
| 136 | $output = ''; |
---|
| 137 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
| 138 | $output.= "\n".$query; |
---|
| 139 | $output.= "\n".'(this query time : '; |
---|
| 140 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
| 141 | $output.= "\n".'(total SQL time : '; |
---|
| 142 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
| 143 | $output.= "\n".'(total time : '; |
---|
| 144 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
| 145 | if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) ) |
---|
| 146 | { |
---|
| 147 | $output.= "\n".'(num rows : '; |
---|
| 148 | $output.= pwg_db_num_rows($result).' )'; |
---|
| 149 | } |
---|
| 150 | elseif ( $result!=null |
---|
| 151 | and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) |
---|
| 152 | and !isset($truncate_query)) |
---|
| 153 | { |
---|
| 154 | $output.= "\n".'(affected rows : '; |
---|
| 155 | $output.= pwg_db_changes($result).' )'; |
---|
| 156 | } |
---|
| 157 | $output.= "</pre>\n"; |
---|
| 158 | |
---|
| 159 | $debug .= $output; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return $result; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | function pwg_db_nextval($column, $table) |
---|
| 166 | { |
---|
| 167 | $query = ' |
---|
| 168 | SELECT MAX('.$column.')+1 |
---|
| 169 | FROM '.$table; |
---|
| 170 | list($next) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 171 | if (is_null($next)) |
---|
| 172 | { |
---|
| 173 | $next = 1; |
---|
| 174 | } |
---|
| 175 | return $next; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * |
---|
| 180 | * complex functions |
---|
| 181 | * |
---|
| 182 | */ |
---|
| 183 | |
---|
| 184 | function pwg_db_changes(PDOStatement $result=null) |
---|
| 185 | { |
---|
| 186 | return $result->rowCount(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | function pwg_db_num_rows(PDOStatement $result) |
---|
| 190 | { |
---|
[5452] | 191 | return $result->rowCount(); |
---|
[4967] | 192 | } |
---|
| 193 | |
---|
| 194 | function pwg_db_fetch_assoc($result) |
---|
| 195 | { |
---|
| 196 | return $result->fetch(PDO::FETCH_ASSOC); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | function pwg_db_fetch_row($result) |
---|
| 200 | { |
---|
| 201 | return $result->fetch(PDO::FETCH_NUM); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | function pwg_db_fetch_object($result) |
---|
| 205 | { |
---|
| 206 | return $result; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | function pwg_db_free_result($result) |
---|
| 210 | { |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | function pwg_db_real_escape_string($s) |
---|
| 214 | { |
---|
| 215 | global $pwg_db_link; |
---|
| 216 | |
---|
| 217 | return trim($pwg_db_link->quote($s), "'"); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | function pwg_db_insert_id($table=null, $column='id') |
---|
| 221 | { |
---|
| 222 | global $pwg_db_link; |
---|
| 223 | |
---|
| 224 | return $pwg_db_link->lastInsertRowID(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * |
---|
| 229 | * complex functions |
---|
| 230 | * |
---|
| 231 | */ |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * creates an array based on a query, this function is a very common pattern |
---|
| 235 | * used here |
---|
| 236 | * |
---|
| 237 | * @param string $query |
---|
| 238 | * @param string $fieldname |
---|
| 239 | * @return array |
---|
| 240 | */ |
---|
| 241 | function array_from_query($query, $fieldname) |
---|
| 242 | { |
---|
| 243 | $array = array(); |
---|
| 244 | |
---|
| 245 | $result = pwg_query($query); |
---|
| 246 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 247 | { |
---|
| 248 | array_push($array, $row[$fieldname]); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | return $array; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | define('MASS_UPDATES_SKIP_EMPTY', 1); |
---|
| 255 | /** |
---|
| 256 | * updates multiple lines in a table |
---|
| 257 | * |
---|
| 258 | * @param string table_name |
---|
| 259 | * @param array dbfields |
---|
| 260 | * @param array datas |
---|
| 261 | * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones |
---|
| 262 | * @return void |
---|
| 263 | */ |
---|
| 264 | function mass_updates($tablename, $dbfields, $datas, $flags=0) |
---|
| 265 | { |
---|
| 266 | if (count($datas) == 0) |
---|
| 267 | return; |
---|
| 268 | |
---|
| 269 | foreach ($datas as $data) |
---|
| 270 | { |
---|
| 271 | $query = ' |
---|
| 272 | UPDATE '.$tablename.' |
---|
| 273 | SET '; |
---|
| 274 | $is_first = true; |
---|
| 275 | foreach ($dbfields['update'] as $key) |
---|
| 276 | { |
---|
| 277 | $separator = $is_first ? '' : ",\n "; |
---|
| 278 | |
---|
| 279 | if (isset($data[$key]) and $data[$key] != '') |
---|
| 280 | { |
---|
| 281 | $query.= $separator.$key.' = \''.$data[$key].'\''; |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | if ($flags & MASS_UPDATES_SKIP_EMPTY ) |
---|
| 286 | continue; // next field |
---|
| 287 | $query.= "$separator$key = NULL"; |
---|
| 288 | } |
---|
| 289 | $is_first = false; |
---|
| 290 | } |
---|
| 291 | if (!$is_first) |
---|
| 292 | {// only if one field at least updated |
---|
| 293 | $query.= ' |
---|
| 294 | WHERE '; |
---|
| 295 | $is_first = true; |
---|
| 296 | foreach ($dbfields['primary'] as $key) |
---|
| 297 | { |
---|
| 298 | if (!$is_first) |
---|
| 299 | { |
---|
| 300 | $query.= ' AND '; |
---|
| 301 | } |
---|
| 302 | if ( isset($data[$key]) ) |
---|
| 303 | { |
---|
| 304 | $query.= $key.' = \''.$data[$key].'\''; |
---|
| 305 | } |
---|
| 306 | else |
---|
| 307 | { |
---|
| 308 | $query.= $key.' IS NULL'; |
---|
| 309 | } |
---|
| 310 | $is_first = false; |
---|
| 311 | } |
---|
| 312 | pwg_query($query); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * inserts multiple lines in a table |
---|
| 320 | * |
---|
| 321 | * @param string table_name |
---|
| 322 | * @param array dbfields |
---|
| 323 | * @param array inserts |
---|
| 324 | * @return void |
---|
| 325 | */ |
---|
| 326 | |
---|
| 327 | function mass_inserts($table_name, $dbfields, $datas) |
---|
| 328 | { |
---|
| 329 | if (count($datas) != 0) |
---|
| 330 | { |
---|
| 331 | $first = true; |
---|
| 332 | |
---|
| 333 | $packet_size = 16777216; |
---|
| 334 | $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/ |
---|
| 335 | $query = ''; |
---|
| 336 | |
---|
| 337 | foreach ($datas as $insert) |
---|
| 338 | { |
---|
| 339 | if (strlen($query) >= $packet_size) |
---|
| 340 | { |
---|
| 341 | pwg_query($query); |
---|
| 342 | $first = true; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if ($first) |
---|
| 346 | { |
---|
| 347 | $query = ' |
---|
| 348 | INSERT INTO '.$table_name.' |
---|
| 349 | ('.implode(',', $dbfields).') |
---|
| 350 | VALUES'; |
---|
| 351 | $first = false; |
---|
| 352 | } |
---|
| 353 | else |
---|
| 354 | { |
---|
| 355 | $query .= ' |
---|
| 356 | , '; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | $query .= '('; |
---|
| 360 | foreach ($dbfields as $field_id => $dbfield) |
---|
| 361 | { |
---|
| 362 | if ($field_id > 0) |
---|
| 363 | { |
---|
| 364 | $query .= ','; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | if (!isset($insert[$dbfield]) or $insert[$dbfield] === '') |
---|
| 368 | { |
---|
| 369 | $query .= 'NULL'; |
---|
| 370 | } |
---|
| 371 | else |
---|
| 372 | { |
---|
| 373 | $query .= "'".$insert[$dbfield]."'"; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | $query .= ')'; |
---|
| 377 | } |
---|
| 378 | pwg_query($query); |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | /** |
---|
| 383 | * Do maintenance on all PWG tables |
---|
| 384 | * |
---|
| 385 | * @return none |
---|
| 386 | */ |
---|
| 387 | function do_maintenance_all_tables() |
---|
| 388 | { |
---|
| 389 | global $prefixeTable, $page; |
---|
| 390 | |
---|
| 391 | $all_tables = array(); |
---|
| 392 | |
---|
| 393 | // List all tables |
---|
| 394 | $query = 'SELECT name FROM SQLITE_MASTER |
---|
| 395 | WHERE name LIKE \''.$prefixeTable.'%\''; |
---|
| 396 | |
---|
| 397 | $all_tables = array_from_query($query, 'name'); |
---|
| 398 | foreach ($all_tables as $table_name) |
---|
| 399 | { |
---|
| 400 | $query = 'VACUUM '.$table_name.';'; |
---|
| 401 | $result = pwg_query($query); |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | array_push($page['infos'], |
---|
[5036] | 405 | l10n('All optimizations have been successfully completed.') |
---|
[4967] | 406 | ); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | function pwg_db_concat($array) |
---|
| 410 | { |
---|
| 411 | return implode($array, ' || '); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | function pwg_db_concat_ws($array, $separator) |
---|
| 415 | { |
---|
| 416 | $glue = sprintf(' || \'%s\' || ', $separator); |
---|
| 417 | |
---|
| 418 | return implode($array, $glue); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | function pwg_db_cast_to_text($string) |
---|
| 422 | { |
---|
| 423 | return $string; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | /** |
---|
| 427 | * returns an array containing the possible values of an enum field |
---|
| 428 | * |
---|
| 429 | * @param string tablename |
---|
| 430 | * @param string fieldname |
---|
| 431 | */ |
---|
| 432 | function get_enums($table, $field) |
---|
| 433 | { |
---|
[6153] | 434 | $Enums['categories']['status'] = array('public', 'private'); |
---|
| 435 | $Enums['history']['section'] = array('categories','tags','search','list','favorites','most_visited','best_rated','recent_pics','recent_cats'); |
---|
| 436 | $Enums['user_infos']['status'] = array('webmaster','admin','normal','generic','guest'); |
---|
| 437 | $Enums['image']['type'] = array('picture','high','other'); |
---|
| 438 | $Enums['plugins']['state'] = array('active', 'inactive'); |
---|
| 439 | $Enums['user_cache_image']['access_type'] = array('NOT IN','IN'); |
---|
| 440 | |
---|
| 441 | $table = str_replace($GLOBALS['prefixeTable'], '', $table); |
---|
| 442 | if (isset($Enums[$table][$field])) { |
---|
| 443 | return $Enums[$table][$field]; |
---|
| 444 | } else { |
---|
| 445 | return array(); |
---|
| 446 | } |
---|
[4967] | 447 | } |
---|
| 448 | |
---|
| 449 | // get_boolean transforms a string to a boolean value. If the string is |
---|
| 450 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
| 451 | // any other case, true is returned. |
---|
| 452 | function get_boolean( $string ) |
---|
| 453 | { |
---|
| 454 | $boolean = true; |
---|
| 455 | if ('f' === $string || 'false' === $string) |
---|
| 456 | { |
---|
| 457 | $boolean = false; |
---|
| 458 | } |
---|
| 459 | return $boolean; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | /** |
---|
| 463 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
| 464 | * |
---|
| 465 | * @param mixed $var |
---|
| 466 | * @return mixed |
---|
| 467 | */ |
---|
| 468 | function boolean_to_string($var) |
---|
| 469 | { |
---|
[6342] | 470 | if (is_bool($var)) |
---|
[4967] | 471 | { |
---|
[6342] | 472 | return $var ? 'true' : 'false'; |
---|
[4967] | 473 | } |
---|
| 474 | else |
---|
| 475 | { |
---|
[6342] | 476 | return $var; |
---|
[4967] | 477 | } |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | /** |
---|
| 481 | * |
---|
| 482 | * interval and date functions |
---|
| 483 | * |
---|
| 484 | */ |
---|
| 485 | |
---|
| 486 | function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE') |
---|
| 487 | { |
---|
| 488 | if ($date!='CURRENT_DATE') |
---|
| 489 | { |
---|
| 490 | $date = '\''.$date.'\''; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | return 'date('.$date.',\''.-$period.' DAY\')'; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | function pwg_db_get_recent_period($period, $date='CURRENT_DATE') |
---|
| 497 | { |
---|
| 498 | $query = 'select '.pwg_db_get_recent_period_expression($period, $date); |
---|
| 499 | list($d) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 500 | |
---|
| 501 | return $d; |
---|
| 502 | } |
---|
| 503 | |
---|
[6579] | 504 | function pwg_db_get_hour($date) |
---|
| 505 | { |
---|
| 506 | return 'strftime(\'%H\', '.$date.')'; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | |
---|
[4967] | 510 | function pwg_db_get_date_YYYYMM($date) |
---|
| 511 | { |
---|
| 512 | return 'strftime(\'%Y%m\','.$date.')'; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | function pwg_db_get_date_MMDD($date) |
---|
| 516 | { |
---|
| 517 | return 'strftime(\'%m%d\','.$date.')'; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | function pwg_db_get_year($date) |
---|
| 521 | { |
---|
| 522 | return 'strftime(\'%Y\','.$date.')'; |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | function pwg_db_get_month($date) |
---|
| 526 | { |
---|
| 527 | return 'strftime(\'%m\','.$date.')'; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | function pwg_db_get_week($date, $mode=null) |
---|
| 531 | { |
---|
| 532 | return 'strftime(\'%W\','.$date.')'; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | function pwg_db_get_dayofmonth($date) |
---|
| 536 | { |
---|
| 537 | return 'strftime(\'%d\','.$date.')'; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | function pwg_db_get_dayofweek($date) |
---|
| 541 | { |
---|
| 542 | return 'strftime(\'%w\','.$date.')'; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | function pwg_db_get_weekday($date) |
---|
| 546 | { |
---|
| 547 | return 'strftime(\'%w\',date('.$date.',\'-1 DAY\'))'; |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | // my_error returns (or send to standard output) the message concerning the |
---|
| 551 | // error occured for the last mysql query. |
---|
| 552 | function my_error($header, $die) |
---|
| 553 | { |
---|
| 554 | global $pwg_db_link; |
---|
| 555 | |
---|
| 556 | $error = ''; |
---|
| 557 | if (isset($pwg_db_link)) |
---|
| 558 | { |
---|
| 559 | $error .= '[sqlite error]'.$pwg_db_link->errorInfo()."\n"; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | $error .= $header; |
---|
| 563 | |
---|
| 564 | if ($die) |
---|
| 565 | { |
---|
| 566 | fatal_error($error); |
---|
| 567 | } |
---|
| 568 | echo("<pre>"); |
---|
| 569 | trigger_error($error, E_USER_WARNING); |
---|
| 570 | echo("</pre>"); |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | // sqlite create functions |
---|
| 574 | function pwg_now() |
---|
| 575 | { |
---|
| 576 | return date('Y-m-d H:i:s'); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | function pwg_unix_timestamp() |
---|
| 580 | { |
---|
| 581 | return time(); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | function pwg_if($expression, $value1, $value2) |
---|
| 585 | { |
---|
| 586 | if ($expression) |
---|
| 587 | { |
---|
| 588 | return $value1; |
---|
| 589 | } |
---|
| 590 | else |
---|
| 591 | { |
---|
| 592 | return $value2; |
---|
| 593 | } |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | function pwg_regexp($pattern, $string) |
---|
| 597 | { |
---|
| 598 | $pattern = sprintf('`%s`', $pattern); |
---|
| 599 | return preg_match($pattern, $string); |
---|
| 600 | } |
---|
| 601 | ?> |
---|