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