source: trunk/admin/history.php @ 9917

Last change on this file since 9917 was 9808, checked in by plg, 14 years ago

bug 1197 fixed: avoid warning message on history search when a photo was deleted

  • Property svn:eol-style set to LF
File size: 18.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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/**
25 * Display filtered history lines
26 */
27
28// +-----------------------------------------------------------------------+
29// |                              functions                                |
30// +-----------------------------------------------------------------------+
31
32// +-----------------------------------------------------------------------+
33// |                           initialization                              |
34// +-----------------------------------------------------------------------+
35
36if (!defined('PHPWG_ROOT_PATH'))
37{
38  die('Hacking attempt!');
39}
40
41include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
42include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
43
44if (isset($_GET['start']) and is_numeric($_GET['start']))
45{
46  $page['start'] = $_GET['start'];
47}
48else
49{
50  $page['start'] = 0;
51}
52
53$types = array('none', 'picture', 'high', 'other');
54
55$display_thumbnails = array('no_display_thumbnail' => l10n('No display'),
56                            'display_thumbnail_classic' => l10n('Classic display'),
57                            'display_thumbnail_hoverbox' => l10n('Hoverbox display')
58  );
59
60// +-----------------------------------------------------------------------+
61// | Check Access and exit when user status is not ok                      |
62// +-----------------------------------------------------------------------+
63
64check_status(ACCESS_ADMINISTRATOR);
65
66// +-----------------------------------------------------------------------+
67// | Build search criteria and redirect to results                         |
68// +-----------------------------------------------------------------------+
69
70$page['errors'] = array();
71$search = array();
72
73if (isset($_POST['submit']))
74{
75  // dates
76  if (!empty($_POST['start_year']))
77  {
78    $search['fields']['date-after'] = sprintf(
79      '%d-%02d-%02d',
80      $_POST['start_year'],
81      $_POST['start_month'],
82      $_POST['start_day']
83      );
84  }
85
86  if (!empty($_POST['end_year']))
87  {
88    $search['fields']['date-before'] = sprintf(
89      '%d-%02d-%02d',
90      $_POST['end_year'],
91      $_POST['end_month'],
92      $_POST['end_day']
93      );
94  }
95
96  if (empty($_POST['types']))
97  {
98    $search['fields']['types'] = $types;
99  }
100  else
101  {
102    $search['fields']['types'] = $_POST['types'];
103  }
104
105  $search['fields']['user'] = $_POST['user'];
106
107  if (!empty($_POST['image_id']))
108  {
109    $search['fields']['image_id'] = intval($_POST['image_id']);
110  }
111
112  if (!empty($_POST['filename']))
113  {
114    $search['fields']['filename'] = str_replace(
115      '*',
116      '%',
117      pwg_db_real_escape_string($_POST['filename'])
118      );
119  }
120
121  $search['fields']['display_thumbnail'] = $_POST['display_thumbnail'];
122  // Display choise are also save to one cookie
123  if (!empty($_POST['display_thumbnail'])
124      and isset($display_thumbnails[$_POST['display_thumbnail']]))
125  {
126    $cookie_val = $_POST['display_thumbnail'];
127  }
128  else
129  {
130    $cookie_val = null;
131  }
132 
133  pwg_set_cookie_var('display_thumbnail', $cookie_val, strtotime('+1 month') );
134
135  // TODO manage inconsistency of having $_POST['image_id'] and
136  // $_POST['filename'] simultaneously
137
138  if (!empty($search))
139  {
140    // register search rules in database, then they will be available on
141    // thumbnails page and picture page.
142    $query ='
143INSERT INTO '.SEARCH_TABLE.'
144  (rules)
145  VALUES
146  (\''.serialize($search).'\')
147;';
148    pwg_query($query);
149
150    $search_id = pwg_db_insert_id(SEARCH_TABLE);
151
152    redirect(
153      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
154      );
155  }
156  else
157  {
158    array_push($page['errors'], l10n('Empty query. No criteria has been entered.'));
159  }
160}
161
162// +-----------------------------------------------------------------------+
163// |                             template init                             |
164// +-----------------------------------------------------------------------+
165
166$template->set_filename('history', 'history.tpl');
167
168// TabSheet initialization
169history_tabsheet();
170
171$template->assign(
172  array(
173    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
174    'F_ACTION' => get_root_url().'admin.php?page=history'
175    )
176  );
177
178// +-----------------------------------------------------------------------+
179// |                             history lines                             |
180// +-----------------------------------------------------------------------+
181
182if (isset($_GET['search_id'])
183    and $page['search_id'] = (int)$_GET['search_id'])
184{
185  // what are the lines to display in reality ?
186  $query = '
187SELECT rules
188  FROM '.SEARCH_TABLE.'
189  WHERE id = '.$page['search_id'].'
190;';
191  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
192
193  $page['search'] = unserialize($serialized_rules);
194
195  if (isset($_GET['user_id']))
196  {
197    if (!is_numeric($_GET['user_id']))
198    {
199      die('user_id GET parameter must be an integer value');
200    }
201
202    $page['search']['fields']['user'] = $_GET['user_id'];
203
204    $query ='
205INSERT INTO '.SEARCH_TABLE.'
206  (rules)
207  VALUES
208  (\''.serialize($page['search']).'\')
209;';
210    pwg_query($query);
211
212    $search_id = pwg_db_insert_id(SEARCH_TABLE);
213
214    redirect(
215      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
216      );
217  }
218
219  $data = trigger_event('get_history', array(), $page['search'], $types);
220  usort($data, 'history_compare');
221
222  $page['nb_lines'] = count($data);
223
224  $history_lines = array();
225  $user_ids = array();
226  $username_of = array();
227  $category_ids = array();
228  $image_ids = array();
229  $tag_ids = array();
230
231  foreach ($data as $row)
232  {
233    $user_ids[$row['user_id']] = 1;
234
235    if (isset($row['category_id']))
236    {
237      $category_ids[$row['category_id']] = 1;
238    }
239
240    if (isset($row['image_id']))
241    {
242      $image_ids[$row['image_id']] = 1;
243    }
244
245    if (isset($row['tag_ids']))
246    {
247      foreach (explode(',', $row['tag_ids']) as $tag_id)
248      {
249        array_push($tag_ids, $tag_id);
250      }
251    }
252
253    array_push(
254      $history_lines,
255      $row
256      );
257  }
258
259  // prepare reference data (users, tags, categories...)
260  if (count($user_ids) > 0)
261  {
262    $query = '
263SELECT '.$conf['user_fields']['id'].' AS id
264     , '.$conf['user_fields']['username'].' AS username
265  FROM '.USERS_TABLE.'
266  WHERE id IN ('.implode(',', array_keys($user_ids)).')
267;';
268    $result = pwg_query($query);
269
270    $username_of = array();
271    while ($row = pwg_db_fetch_assoc($result))
272    {
273      $username_of[$row['id']] = stripslashes($row['username']);
274    }
275  }
276
277  if (count($category_ids) > 0)
278  {
279    $query = '
280SELECT id, uppercats
281  FROM '.CATEGORIES_TABLE.'
282  WHERE id IN ('.implode(',', array_keys($category_ids)).')
283;';
284    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
285
286    $name_of_category = array();
287
288    foreach ($uppercats_of as $category_id => $uppercats)
289    {
290      $name_of_category[$category_id] = get_cat_display_name_cache(
291        $uppercats
292        );
293    }
294  }
295
296  if (count($image_ids) > 0)
297  {
298    $query = '
299SELECT
300    id,
301    IF(name IS NULL, file, name) AS label,
302    filesize,
303    high_filesize,
304    file,
305    path,
306    tn_ext
307  FROM '.IMAGES_TABLE.'
308  WHERE id IN ('.implode(',', array_keys($image_ids)).')
309;';
310    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
311    $label_of_image = array();
312    $filesize_of_image = array();
313    $high_filesize_of_image = array();
314    $file_of_image = array();
315    $path_of_image = array();
316    $tn_ext_of_image = array();
317
318    $result = pwg_query($query);
319    while ($row = pwg_db_fetch_assoc($result))
320    {
321      $label_of_image[ $row['id'] ] = $row['label'];
322
323      if (isset($row['filesize']))
324      {
325        $filesize_of_image[ $row['id'] ] = $row['filesize'];
326      }
327
328      if (isset($row['high_filesize']))
329      {
330        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
331      }
332
333      $file_of_image[ $row['id'] ] = $row['file'];
334      $path_of_image[ $row['id'] ] = $row['path'];
335      $tn_ext_of_image[ $row['id'] ] = $row['tn_ext'];
336    }
337
338    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
339  }
340
341  if (count($tag_ids) > 0)
342  {
343    $tag_ids = array_unique($tag_ids);
344
345    $query = '
346SELECT
347    id,
348    name
349  FROM '.TAGS_TABLE.'
350  WHERE id IN ('.implode(', ', $tag_ids).')
351;';
352    $name_of_tag = array();
353
354    $result = pwg_query($query);
355    while ($row = pwg_db_fetch_assoc($result))
356    {
357      $name_of_tag[ $row['id'] ] = $row['name'];
358    }
359  }
360
361  $i = 0;
362  $first_line = $page['start'] + 1;
363  $last_line = $page['start'] + $conf['nb_logs_page'];
364
365  $summary['total_filesize'] = 0;
366  $summary['guests_IP'] = array();
367
368  foreach ($history_lines as $line)
369  {
370    // FIXME when we watch the representative of a non image element, it is
371    // the not the representative filesize that is counted (as it is
372    // unknown) but the non image element filesize. Proposed solution: add
373    // #images.representative_filesize and add 'representative' in the
374    // choices of #history.image_type.
375
376    if (isset($line['image_type']))
377    {
378      if ($line['image_type'] == 'high')
379      {
380        if (isset($high_filesize_of_image[$line['image_id']]))
381        {
382          $summary['total_filesize']+=
383            $high_filesize_of_image[$line['image_id']];
384        }
385      }
386      else
387      {
388        if (isset($filesize_of_image[$line['image_id']]))
389        {
390          $summary['total_filesize']+=
391            $filesize_of_image[$line['image_id']];
392        }
393      }
394    }
395
396    if ($line['user_id'] == $conf['guest_id'])
397    {
398      if (!isset($summary['guests_IP'][ $line['IP'] ]))
399      {
400        $summary['guests_IP'][ $line['IP'] ] = 0;
401      }
402
403      $summary['guests_IP'][ $line['IP'] ]++;
404    }
405
406    $i++;
407
408    if ($i < $first_line or $i > $last_line)
409    {
410      continue;
411    }
412
413    $user_string = '';
414    if (isset($username_of[$line['user_id']]))
415    {
416      $user_string.= $username_of[$line['user_id']];
417    }
418    else
419    {
420      $user_string.= $line['user_id'];
421    }
422    $user_string.= '&nbsp;<a href="';
423    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
424    $user_string.= '&amp;search_id='.$page['search_id'];
425    $user_string.= '&amp;user_id='.$line['user_id'];
426    $user_string.= '">+</a>';
427
428    $tags_string = '';
429    if (isset($line['tag_ids']))
430    {
431      $tags_string = preg_replace(
432        '/(\d+)/e',
433        '$name_of_tag["$1"]',
434        str_replace(
435          ',',
436          ', ',
437          $line['tag_ids']
438          )
439        );
440    }
441
442    $image_string = '';
443    if (isset($line['image_id']))
444    {
445      $picture_url = make_picture_url(
446        array(
447          'image_id' => $line['image_id'],
448          )
449        );
450
451      if (isset($file_of_image[$line['image_id']]))
452      {
453        $element = array(
454          'id' => $line['image_id'],
455          'file' => $file_of_image[$line['image_id']],
456          'path' => $path_of_image[$line['image_id']],
457          'tn_ext' => $tn_ext_of_image[$line['image_id']],
458          );
459        $thumbnail_display = $page['search']['fields']['display_thumbnail'];
460      }
461      else
462      {
463        $thumbnail_display = 'no_display_thumbnail';
464      }
465
466      $image_title = '('.$line['image_id'].')';
467
468      if (isset($label_of_image[$line['image_id']]))
469      {
470        $image_title.= ' '.$label_of_image[$line['image_id']];
471      }
472      else
473      {
474        $image_title.= ' unknown filename';
475      }
476
477      $image_string = '';
478
479      switch ($thumbnail_display)
480      {
481        case 'no_display_thumbnail':
482        {
483          $image_string= '<a href="'.$picture_url.'">'.$image_title.'</a>';
484          break;
485        }
486        case 'display_thumbnail_classic':
487        {
488          $image_string =
489            '<a class="thumbnail" href="'.$picture_url.'">'
490            .'<span><img src="'.get_thumbnail_url($element)
491            .'" alt="'.$image_title.'" title="'.$image_title.'">'
492            .'</span></a>';
493          break;
494        }
495        case 'display_thumbnail_hoverbox':
496        {
497          $image_string =
498            '<a class="over" href="'.$picture_url.'">'
499            .'<span><img src="'.get_thumbnail_url($element)
500            .'" alt="'.$image_title.'" title="'.$image_title.'">'
501            .'</span>'.$image_title.'</a>';
502          break;
503        }
504      }
505    }
506
507    $template->append(
508      'search_results',
509      array(
510        'DATE'      => $line['date'],
511        'TIME'      => $line['time'],
512        'USER'      => $user_string,
513        'IP'        => $line['IP'],
514        'IMAGE'     => $image_string,
515        'TYPE'      => $line['image_type'],
516        'SECTION'   => $line['section'],
517        'CATEGORY'  => isset($line['category_id'])
518          ? ( isset($name_of_category[$line['category_id']])
519                ? $name_of_category[$line['category_id']]
520                : 'deleted '.$line['category_id'] )
521          : '',
522        'TAGS'       => $tags_string,
523        )
524      );
525  }
526
527  $summary['nb_guests'] = 0;
528  if (count(array_keys($summary['guests_IP'])) > 0)
529  {
530    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
531
532    // we delete the "guest" from the $username_of hash so that it is
533    // avoided in next steps
534    unset($username_of[ $conf['guest_id'] ]);
535  }
536
537  $summary['nb_members'] = count($username_of);
538
539  $member_strings = array();
540  foreach ($username_of as $user_id => $user_name)
541  {
542    $member_string = $user_name.'&nbsp;<a href="';
543    $member_string.= get_root_url().'admin.php?page=history';
544    $member_string.= '&amp;search_id='.$page['search_id'];
545    $member_string.= '&amp;user_id='.$user_id;
546    $member_string.= '">+</a>';
547
548    $member_strings[] = $member_string;
549  }
550
551  $template->assign(
552    'search_summary',
553    array(
554      'NB_LINES' => l10n_dec(
555        '%d line filtered', '%d lines filtered',
556        $page['nb_lines']
557        ),
558      'FILESIZE' => $summary['total_filesize'].' KB',
559      'USERS' => l10n_dec(
560        '%d user', '%d users',
561        $summary['nb_members'] + $summary['nb_guests']
562        ),
563      'MEMBERS' => sprintf(
564        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
565        implode(
566          ', ',
567          $member_strings
568          )
569        ),
570      'GUESTS' => l10n_dec(
571        '%d guest', '%d guests',
572        $summary['nb_guests']
573        ),
574      )
575    );
576}
577
578// +-----------------------------------------------------------------------+
579// |                            navigation bar                             |
580// +-----------------------------------------------------------------------+
581
582if (isset($page['search_id']))
583{
584  $navbar = create_navigation_bar(
585    get_root_url().'admin.php'.get_query_string_diff(array('start')),
586    $page['nb_lines'],
587    $page['start'],
588    $conf['nb_logs_page']
589    );
590
591  $template->assign('navbar', $navbar);
592}
593
594// +-----------------------------------------------------------------------+
595// |                             filter form                               |
596// +-----------------------------------------------------------------------+
597
598$form = array();
599
600if (isset($page['search']))
601{
602  if (isset($page['search']['fields']['date-after']))
603  {
604    $tokens = explode('-', $page['search']['fields']['date-after']);
605
606    $form['start_year']  = (int)$tokens[0];
607    $form['start_month'] = (int)$tokens[1];
608    $form['start_day']   = (int)$tokens[2];
609  }
610
611  if (isset($page['search']['fields']['date-before']))
612  {
613    $tokens = explode('-', $page['search']['fields']['date-before']);
614
615    $form['end_year']  = (int)$tokens[0];
616    $form['end_month'] = (int)$tokens[1];
617    $form['end_day']   = (int)$tokens[2];
618  }
619
620  $form['types'] = $page['search']['fields']['types'];
621
622  if (isset($page['search']['fields']['user']))
623  {
624    $form['user'] = $page['search']['fields']['user'];
625  }
626  else
627  {
628    $form['user'] = null;
629  }
630
631  $form['image_id'] = @$page['search']['fields']['image_id'];
632  $form['filename'] = @$page['search']['fields']['filename'];
633
634  $form['display_thumbnail'] = @$page['search']['fields']['display_thumbnail'];
635}
636else
637{
638  // by default, at page load, we want the selected date to be the current
639  // date
640  $form['start_year']  = $form['end_year']  = date('Y');
641  $form['start_month'] = $form['end_month'] = date('n');
642  $form['start_day']   = $form['end_day']   = date('j');
643  $form['types'] = $types;
644  // Hoverbox by default
645  $form['display_thumbnail'] =
646    pwg_get_cookie_var('display_thumbnail', 'no_display_thumbnail');
647}
648
649
650$month_list = $lang['month'];
651$month_list[0]='------------';
652ksort($month_list);
653
654$template->assign(
655  array(
656    'IMAGE_ID' => @$form['image_id'],
657    'FILENAME' => @$form['filename'],
658
659    'month_list' => $month_list,
660
661    'START_DAY_SELECTED' => @$form['start_day'],
662    'START_MONTH_SELECTED' => @$form['start_month'],
663    'START_YEAR' => @$form['start_year'],
664
665    'END_DAY_SELECTED' => @$form['end_day'],
666    'END_MONTH_SELECTED' => @$form['end_month'],
667    'END_YEAR'   => @$form['end_year'],
668    )
669  );
670
671$template->assign(
672    array(
673      'type_option_values' => $types,
674      'type_option_selected' => $form['types']
675    )
676  );
677
678
679$query = '
680SELECT
681    '.$conf['user_fields']['id'].' AS id,
682    '.$conf['user_fields']['username'].' AS username
683  FROM '.USERS_TABLE.'
684  ORDER BY username ASC
685;';
686$template->assign(
687  array(
688    'user_options' => simple_hash_from_query($query, 'id','username'),
689    'user_options_selected' => array(@$form['user'])
690  )
691);
692
693$template->assign('display_thumbnails', $display_thumbnails);
694$template->assign('display_thumbnail_selected', $form['display_thumbnail']);
695
696// +-----------------------------------------------------------------------+
697// |                           html code display                           |
698// +-----------------------------------------------------------------------+
699
700$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
701?>
Note: See TracBrowser for help on using the repository browser.