source: trunk/admin/history.php @ 1892

Last change on this file since 1892 was 1892, checked in by plg, 17 years ago

New: history search can be filtered on image id and filename.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: history.php 1892 2007-03-11 15:53:25Z plg $
9// | last update   : $Date: 2007-03-11 15:53:25 +0000 (Sun, 11 Mar 2007) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1892 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28/**
29 * Display filtered history lines
30 */
31
32// echo '<pre>$_POST:
33// '; print_r($_POST); echo '</pre>';
34// echo '<pre>$_GET:
35// '; print_r($_GET); echo '</pre>';
36
37// +-----------------------------------------------------------------------+
38// |                              functions                                |
39// +-----------------------------------------------------------------------+
40
41// +-----------------------------------------------------------------------+
42// |                           initialization                              |
43// +-----------------------------------------------------------------------+
44
45if (!defined('PHPWG_ROOT_PATH'))
46{
47  die('Hacking attempt!');
48}
49
50include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
51include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
52
53if (isset($_GET['start']) and is_numeric($_GET['start']))
54{
55  $page['start'] = $_GET['start'];
56}
57else
58{
59  $page['start'] = 0;
60}
61
62$types = array('none', 'picture', 'high', 'other');
63
64// +-----------------------------------------------------------------------+
65// | Check Access and exit when user status is not ok                      |
66// +-----------------------------------------------------------------------+
67
68check_status(ACCESS_ADMINISTRATOR);
69
70// +-----------------------------------------------------------------------+
71// | Build search criteria and redirect to results                         |
72// +-----------------------------------------------------------------------+
73
74$page['errors'] = array();
75$search = array();
76
77if (isset($_POST['submit']))
78{
79  // dates
80  if (!empty($_POST['start_year']))
81  {
82    $search['fields']['date-after'] = sprintf(
83      '%d-%02d-%02d',
84      $_POST['start_year'],
85      $_POST['start_month'],
86      $_POST['start_day']
87      );
88  }
89
90  if (!empty($_POST['end_year']))
91  {
92    $search['fields']['date-before'] = sprintf(
93      '%d-%02d-%02d',
94      $_POST['end_year'],
95      $_POST['end_month'],
96      $_POST['end_day']
97      );
98  }
99
100  $search['fields']['types'] = $_POST['types'];
101
102  $search['fields']['user'] = $_POST['user'];
103
104  if (!empty($_POST['image_id']))
105  {
106    $search['fields']['image_id'] = intval($_POST['image_id']);
107  }
108 
109  if (!empty($_POST['filename']))
110  {
111    $search['fields']['filename'] = str_replace(
112      '*',
113      '%',
114      mysql_escape_string($_POST['filename'])
115      );
116  }
117
118  // TODO manage inconsistency of having $_POST['image_id'] and
119  // $_POST['filename'] simultaneously
120 
121  // echo '<pre>'; print_r($search); echo '</pre>';
122 
123  if (!empty($search))
124  {
125    // register search rules in database, then they will be available on
126    // thumbnails page and picture page.
127    $query ='
128INSERT INTO '.SEARCH_TABLE.'
129  (rules)
130  VALUES
131  (\''.serialize($search).'\')
132;';
133    pwg_query($query);
134
135    $search_id = mysql_insert_id();
136   
137    redirect(
138      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
139      );
140  }
141  else
142  {
143    array_push($page['errors'], $lang['search_one_clause_at_least']);
144  }
145}
146
147// +-----------------------------------------------------------------------+
148// |                             template init                             |
149// +-----------------------------------------------------------------------+
150
151$template->set_filename('history', 'admin/history.tpl');
152
153// TabSheet initialization
154history_tabsheet();
155
156$base_url = PHPWG_ROOT_PATH.'admin.php?page=history';
157
158$template->assign_vars(
159  array(
160    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=history',
161
162    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=history'
163    )
164  );
165
166$template->assign_vars(
167  array(
168    'TODAY_DAY'   => date('d', time()),
169    'TODAY_MONTH' => date('m', time()),
170    'TODAY_YEAR'  => date('Y', time()),
171    )
172  );
173
174// +-----------------------------------------------------------------------+
175// |                             history lines                             |
176// +-----------------------------------------------------------------------+
177
178if (isset($_GET['search_id'])
179    and $page['search_id'] = (int)$_GET['search_id'])
180{
181  // what are the lines to display in reality ?
182  $query = '
183SELECT rules
184  FROM '.SEARCH_TABLE.'
185  WHERE id = '.$page['search_id'].'
186;';
187  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
188
189  $page['search'] = unserialize($serialized_rules);
190
191  if (isset($_GET['user_id']))
192  {
193    if (!is_numeric($_GET['user_id']))
194    {
195      die('user_id GET parameter must be an integer value');
196    }
197
198    $page['search']['fields']['user'] = $_GET['user_id'];
199   
200    $query ='
201INSERT INTO '.SEARCH_TABLE.'
202  (rules)
203  VALUES
204  (\''.serialize($page['search']).'\')
205;';
206    pwg_query($query);
207
208    $search_id = mysql_insert_id();
209   
210    redirect(
211      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
212      );
213  }
214
215 
216  if (isset($page['search']['fields']['filename']))
217  {
218    $query = '
219SELECT
220    id
221  FROM '.IMAGES_TABLE.'
222  WHERE file LIKE \''.$page['search']['fields']['filename'].'\'
223;';
224    $page['search']['image_ids'] = array_from_query($query, 'id');
225  }
226 
227  // echo '<pre>'; print_r($page['search']); echo '</pre>';
228 
229  $clauses = array();
230
231  if (isset($page['search']['fields']['date-after']))
232  {
233    array_push(
234      $clauses,
235      "date >= '".$page['search']['fields']['date-after']."'"
236      );
237  }
238
239  if (isset($page['search']['fields']['date-before']))
240  {
241    array_push(
242      $clauses,
243      "date <= '".$page['search']['fields']['date-before']."'"
244      );
245  }
246
247  if (isset($page['search']['fields']['types']))
248  {
249    $local_clauses = array();
250   
251    foreach ($types as $type) {
252      if (in_array($type, $page['search']['fields']['types'])) {
253        $clause = 'image_type ';
254        if ($type == 'none')
255        {
256          $clause.= 'IS NULL';
257        }
258        else
259        {
260          $clause.= "= '".$type."'";
261        }
262       
263        array_push($local_clauses, $clause);
264      }
265    }
266   
267    if (count($local_clauses) > 0)
268    {
269      array_push(
270        $clauses,
271        implode(' OR ', $local_clauses)
272        );
273    }
274  }
275
276  if (isset($page['search']['fields']['user'])
277      and $page['search']['fields']['user'] != -1)
278  {
279    array_push(
280      $clauses,
281      'user_id = '.$page['search']['fields']['user']
282      );
283  }
284
285  if (isset($page['search']['fields']['image_id']))
286  {
287    array_push(
288      $clauses,
289      'image_id = '.$page['search']['fields']['image_id']
290      );
291  }
292 
293  if (isset($page['search']['fields']['filename']))
294  {
295    if (count($page['search']['image_ids']) == 0)
296    {
297      // a clause that is always false
298      array_push($clauses, '1 = 2 ');
299    }
300    else
301    {
302      array_push(
303        $clauses,
304        'image_id IN ('.implode(', ', $page['search']['image_ids']).')'
305        );
306    }
307  }
308 
309  $clauses = prepend_append_array_items($clauses, '(', ')');
310
311  $where_separator =
312    implode(
313      "\n    AND ",
314      $clauses
315      );
316 
317  $query = '
318SELECT
319    date,
320    time,
321    user_id,
322    IP,
323    section,
324    category_id,
325    tag_ids,
326    image_id,
327    image_type
328  FROM '.HISTORY_TABLE.'
329  WHERE '.$where_separator.'
330;';
331
332  // LIMIT '.$page['start'].', '.$conf['nb_logs_page'].'
333
334  $result = pwg_query($query);
335
336  $page['nb_lines'] = mysql_num_rows($result);
337 
338  $history_lines = array();
339  $user_ids = array();
340  $category_ids = array();
341  $image_ids = array();
342  $tag_ids = array();
343 
344  while ($row = mysql_fetch_assoc($result))
345  {
346    $user_ids[$row['user_id']] = 1;
347
348    if (isset($row['category_id']))
349    {
350      $category_ids[$row['category_id']] = 1;
351    }
352
353    if (isset($row['image_id']))
354    {
355      $image_ids[$row['image_id']] = 1;
356    }
357
358    if (isset($row['tag_ids']))
359    {
360      foreach (explode(',', $row['tag_ids']) as $tag_id)
361      {
362        array_push($tag_ids, $tag_id);
363      }
364    }
365
366    array_push(
367      $history_lines,
368      $row
369      );
370  }
371
372  // prepare reference data (users, tags, categories...)
373  if (count($user_ids) > 0)
374  {
375    $query = '
376SELECT '.$conf['user_fields']['id'].' AS id
377     , '.$conf['user_fields']['username'].' AS username
378  FROM '.USERS_TABLE.'
379  WHERE id IN ('.implode(',', array_keys($user_ids)).')
380;';
381    $result = pwg_query($query);
382
383    $username_of = array();
384    while ($row = mysql_fetch_array($result))
385    {
386      $username_of[$row['id']] = $row['username'];
387    }
388  }
389
390  if (count($category_ids) > 0)
391  {
392    $query = '
393SELECT id, uppercats
394  FROM '.CATEGORIES_TABLE.'
395  WHERE id IN ('.implode(',', array_keys($category_ids)).')
396;';
397    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
398
399    $name_of_category = array();
400   
401    foreach ($uppercats_of as $category_id => $uppercats)
402    {
403      $name_of_category[$category_id] = get_cat_display_name_cache(
404        $uppercats
405        );
406    }
407  }
408
409  if (count($image_ids) > 0)
410  {
411    $query = '
412SELECT
413    id,
414    IF(name IS NULL, file, name) AS label,
415    filesize,
416    high_filesize
417  FROM '.IMAGES_TABLE.'
418  WHERE id IN ('.implode(',', array_keys($image_ids)).')
419;';
420    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
421    $label_of_image = array();
422    $filesize_of_image = array();
423    $high_filesize_of_image = array();
424   
425    $result = pwg_query($query);
426    while ($row = mysql_fetch_array($result))
427    {
428      $label_of_image[ $row['id'] ] = $row['label'];
429
430      if (isset($row['filesize']))
431      {
432        $filesize_of_image[ $row['id'] ] = $row['filesize'];
433      }
434
435      if (isset($row['high_filesize']))
436      {
437        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
438      }
439    }
440
441    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
442  }
443
444  if (count($tag_ids) > 0)
445  {
446    $tag_ids = array_unique($tag_ids);
447
448    $query = '
449SELECT
450    id,
451    name
452  FROM '.TAGS_TABLE.'
453  WHERE id IN ('.implode(', ', $tag_ids).')
454;';
455    $name_of_tag = array();
456
457    $result = pwg_query($query);
458    while ($row = mysql_fetch_array($result))
459    {
460      $name_of_tag[ $row['id'] ] = $row['name'];
461    }
462  }
463 
464  $i = 0;
465  $first_line = $page['start'] + 1;
466  $last_line = $page['start'] + $conf['nb_logs_page'];
467
468  $total_filesize = 0;
469
470  foreach ($history_lines as $line)
471  {
472    if (isset($line['image_type']))
473    {
474      if ($line['image_type'] == 'high')
475      {
476        if (isset($high_filesize_of_image[$line['image_id']]))
477        {
478          $total_filesize+= $high_filesize_of_image[$line['image_id']];
479        }
480      }
481      else
482      {
483        if (isset($filesize_of_image[$line['image_id']]))
484        {
485          $total_filesize+= $filesize_of_image[$line['image_id']];
486        }
487      }
488    }
489   
490    $i++;
491   
492    if ($i < $first_line or $i > $last_line)
493    {
494      continue;
495    }
496
497    $user_string = '';
498    if (isset($username_of[$line['user_id']]))
499    {
500      $user_string.= $username_of[$line['user_id']];
501    }
502    else
503    {
504      $user_string.= $line['user_id'];
505    }
506    $user_string.= '&nbsp;<a href="';
507    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
508    $user_string.= '&amp;search_id='.$page['search_id'];
509    $user_string.= '&amp;user_id='.$line['user_id'];
510    $user_string.= '">+</a>';
511
512    $tags_string = '';
513    if (isset($line['tag_ids']))
514    {
515      $tags_string = preg_replace(
516        '/(\d+)/e',
517        '$name_of_tag["$1"]',
518        str_replace(
519          ',',
520          ', ',
521          $line['tag_ids']
522          )
523        );
524    }
525   
526    $template->assign_block_vars(
527      'detail',
528      array(
529        'DATE'      => $line['date'],
530        'TIME'      => $line['time'],
531        'USER'      => $user_string,
532        'IP'        => $line['IP'],
533        'IMAGE'     => isset($line['image_id'])
534          ? ( isset($label_of_image[$line['image_id']])
535                ? sprintf(
536                    '(%u) %s',
537                    $line['image_id'],
538                    $label_of_image[$line['image_id']]
539                  )
540                : sprintf(
541                    '(%u) deleted ',
542                    $line['image_id']
543                  )
544            )
545          : '',
546        'TYPE'      => $line['image_type'],
547        'SECTION'   => $line['section'],
548        'CATEGORY'  => isset($line['category_id'])
549          ? ( isset($name_of_category[$line['category_id']])
550                ? $name_of_category[$line['category_id']]
551                : 'deleted '.$line['category_id'] )
552          : '',
553        'TAGS'       => $tags_string,
554        'T_CLASS'   => ($i % 2) ? 'row1' : 'row2',
555        )
556      );
557  }
558
559  $template->assign_block_vars(
560    'summary',
561    array(
562      'FILESIZE' => $total_filesize.' KB',
563      )
564    );
565}
566
567// +-----------------------------------------------------------------------+
568// |                            navigation bar                             |
569// +-----------------------------------------------------------------------+
570
571if (isset($page['search_id']))
572{
573  $navbar = create_navigation_bar(
574    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
575    $page['nb_lines'],
576    $page['start'],
577    $conf['nb_logs_page']
578    );
579
580  $template->assign_block_vars(
581    'navigation',
582    array(
583      'NAVBAR' => $navbar
584      )
585    );
586}
587
588// +-----------------------------------------------------------------------+
589// |                             filter form                               |
590// +-----------------------------------------------------------------------+
591
592$form = array();
593
594if (isset($page['search']))
595{
596  if (isset($page['search']['fields']['date-after']))
597  {
598    $tokens = explode('-', $page['search']['fields']['date-after']);
599   
600    $form['start_year']  = (int)$tokens[0];
601    $form['start_month'] = (int)$tokens[1];
602    $form['start_day']   = (int)$tokens[2];
603  }
604
605  if (isset($page['search']['fields']['date-before']))
606  {
607    $tokens = explode('-', $page['search']['fields']['date-before']);
608
609    $form['end_year']  = (int)$tokens[0];
610    $form['end_month'] = (int)$tokens[1];
611    $form['end_day']   = (int)$tokens[2];
612  }
613
614  $form['types'] = $page['search']['fields']['types'];
615
616  if (isset($page['search']['fields']['user']))
617  {
618    $form['user'] = $page['search']['fields']['user'];
619  }
620  else
621  {
622    $form['user'] = null;
623  }
624
625  $form['image_id'] = @$page['search']['fields']['image_id'];
626  $form['filename'] = @$page['search']['fields']['filename'];
627}
628else
629{
630  // by default, at page load, we want the selected date to be the current
631  // date
632  $form['start_year']  = $form['end_year']  = date('Y');
633  $form['start_month'] = $form['end_month'] = date('n');
634  $form['start_day']   = $form['end_day']   = date('j');
635  $form['types'] = $types;
636}
637
638// start date
639get_day_list('start_day', @$form['start_day']);
640get_month_list('start_month', @$form['start_month']);
641// end date
642get_day_list('end_day', @$form['end_day']);
643get_month_list('end_month', @$form['end_month']);
644
645$template->assign_vars(
646  array(
647    'START_YEAR' => @$form['start_year'],
648    'END_YEAR'   => @$form['end_year'],
649    'IMAGE_ID' => @$form['image_id'],
650    'FILENAME' => @$form['filename'],
651    )
652  );
653
654foreach ($types as $option)
655{
656  $selected = '';
657 
658  if (in_array($option, $form['types']))
659  {
660    $selected = 'selected="selected"';
661  }
662 
663  $template->assign_block_vars(
664    'types_option',
665    array(
666      'VALUE' => $option,
667      'CONTENT' => l10n($option),
668      'SELECTED' => $selected,
669      )
670    );
671}
672
673$template->assign_block_vars(
674  'user_option',
675  array(
676    'VALUE'=> -1,
677    'CONTENT' => '------------',
678    'SELECTED' => ''
679    )
680  );
681
682$query = '
683SELECT
684    '.$conf['user_fields']['id'].' AS id,
685    '.$conf['user_fields']['username'].' AS username
686  FROM '.USERS_TABLE.'
687  ORDER BY username ASC
688;';
689$result = pwg_query($query);
690
691while ($row = mysql_fetch_array($result))
692{
693  $selected = '';
694
695  if (isset($form['user'])
696      and $row['id'] == $form['user'])
697  {
698    $selected = 'selected="selected"';
699  }
700 
701  $template->assign_block_vars(
702    'user_option',
703    array(
704      'VALUE' => $row['id'],
705      'CONTENT' => $row['username'],
706      'SELECTED' => $selected,
707      )
708    );
709}
710 
711// +-----------------------------------------------------------------------+
712// |                           html code display                           |
713// +-----------------------------------------------------------------------+
714
715$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
716?>
Note: See TracBrowser for help on using the repository browser.