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