source: trunk/admin/history.php @ 9808

Last change on this file since 9808 was 9808, checked in by plg, 13 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
RevLine 
[1727]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1727]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');
[1881]42include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
[1727]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
[1844]53$types = array('none', 'picture', 'high', 'other');
54
[5576]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
[1727]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
[1892]70$page['errors'] = array();
[1727]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
[2157]96  if (empty($_POST['types']))
97  {
98    $search['fields']['types'] = $types;
99  }
100  else
101  {
102    $search['fields']['types'] = $_POST['types'];
103  }
[1890]104
105  $search['fields']['user'] = $_POST['user'];
[1892]106
107  if (!empty($_POST['image_id']))
108  {
109    $search['fields']['image_id'] = intval($_POST['image_id']);
110  }
[2245]111
[1892]112  if (!empty($_POST['filename']))
113  {
114    $search['fields']['filename'] = str_replace(
115      '*',
116      '%',
[4325]117      pwg_db_real_escape_string($_POST['filename'])
[1892]118      );
119  }
120
[1992]121  $search['fields']['display_thumbnail'] = $_POST['display_thumbnail'];
122  // Display choise are also save to one cookie
[5576]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') );
[1992]134
[1892]135  // TODO manage inconsistency of having $_POST['image_id'] and
136  // $_POST['filename'] simultaneously
[2245]137
[1727]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
[4892]150    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[2245]151
[1727]152    redirect(
153      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
154      );
155  }
156  else
157  {
[5021]158    array_push($page['errors'], l10n('Empty query. No criteria has been entered.'));
[1727]159  }
160}
161
162// +-----------------------------------------------------------------------+
163// |                             template init                             |
164// +-----------------------------------------------------------------------+
165
[2530]166$template->set_filename('history', 'history.tpl');
[1727]167
[1881]168// TabSheet initialization
169history_tabsheet();
170
[2245]171$template->assign(
[1727]172  array(
[5920]173    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
[2245]174    'F_ACTION' => get_root_url().'admin.php?page=history'
[1727]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;';
[4325]191  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
[1727]192
193  $page['search'] = unserialize($serialized_rules);
194
[1890]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'];
[2157]203
[1890]204    $query ='
205INSERT INTO '.SEARCH_TABLE.'
206  (rules)
207  VALUES
208  (\''.serialize($page['search']).'\')
209;';
210    pwg_query($query);
211
[4892]212    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[2245]213
[1890]214    redirect(
215      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
216      );
217  }
218
[2157]219  $data = trigger_event('get_history', array(), $page['search'], $types);
220  usort($data, 'history_compare');
[1727]221
[2157]222  $page['nb_lines'] = count($data);
[1727]223
[1883]224  $history_lines = array();
225  $user_ids = array();
[1929]226  $username_of = array();
[1883]227  $category_ids = array();
228  $image_ids = array();
229  $tag_ids = array();
[2157]230
231  foreach ($data as $row)
[1727]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
[1891]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
[1727]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();
[4325]271    while ($row = pwg_db_fetch_assoc($result))
[1727]272    {
[4304]273      $username_of[$row['id']] = stripslashes($row['username']);
[1727]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();
[2245]287
[1727]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 = '
[1883]299SELECT
300    id,
301    IF(name IS NULL, file, name) AS label,
302    filesize,
[1991]303    high_filesize,
304    file,
305    path,
306    tn_ext
[1727]307  FROM '.IMAGES_TABLE.'
308  WHERE id IN ('.implode(',', array_keys($image_ids)).')
309;';
[1883]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();
[1991]314    $file_of_image = array();
315    $path_of_image = array();
316    $tn_ext_of_image = array();
317
[1883]318    $result = pwg_query($query);
[4325]319    while ($row = pwg_db_fetch_assoc($result))
[1883]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      }
[1991]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'];
[1883]336    }
337
338    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
[1727]339  }
[1891]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);
[4325]355    while ($row = pwg_db_fetch_assoc($result))
[1891]356    {
357      $name_of_tag[ $row['id'] ] = $row['name'];
358    }
359  }
[2245]360
[1727]361  $i = 0;
[1883]362  $first_line = $page['start'] + 1;
363  $last_line = $page['start'] + $conf['nb_logs_page'];
[1727]364
[1893]365  $summary['total_filesize'] = 0;
366  $summary['guests_IP'] = array();
[1883]367
[1727]368  foreach ($history_lines as $line)
369  {
[1893]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.
[2245]375
[1883]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        {
[1893]382          $summary['total_filesize']+=
383            $high_filesize_of_image[$line['image_id']];
[1883]384        }
385      }
386      else
387      {
388        if (isset($filesize_of_image[$line['image_id']]))
389        {
[1893]390          $summary['total_filesize']+=
391            $filesize_of_image[$line['image_id']];
[1883]392        }
393      }
394    }
[1893]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      }
[2245]402
[1893]403      $summary['guests_IP'][ $line['IP'] ]++;
404    }
[2245]405
[1883]406    $i++;
[2245]407
[1883]408    if ($i < $first_line or $i > $last_line)
409    {
410      continue;
411    }
[1890]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>';
[1891]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    }
[1896]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        );
[1992]450
[9808]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      }
[1992]465
466      $image_title = '('.$line['image_id'].')';
467
[1896]468      if (isset($label_of_image[$line['image_id']]))
469      {
[1992]470        $image_title.= ' '.$label_of_image[$line['image_id']];
[1896]471      }
472      else
473      {
[1992]474        $image_title.= ' unknown filename';
[1896]475      }
[1992]476
477      $image_string = '';
478
[9808]479      switch ($thumbnail_display)
[1992]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      }
[1896]505    }
[2245]506
507    $template->append(
508      'search_results',
[1727]509      array(
510        'DATE'      => $line['date'],
511        'TIME'      => $line['time'],
[1890]512        'USER'      => $user_string,
[1727]513        'IP'        => $line['IP'],
[1896]514        'IMAGE'     => $image_string,
[1844]515        'TYPE'      => $line['image_type'],
[1727]516        'SECTION'   => $line['section'],
517        'CATEGORY'  => isset($line['category_id'])
[1821]518          ? ( isset($name_of_category[$line['category_id']])
519                ? $name_of_category[$line['category_id']]
520                : 'deleted '.$line['category_id'] )
[1727]521          : '',
[1891]522        'TAGS'       => $tags_string,
[1727]523        )
524      );
525  }
[1883]526
[1893]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  }
[2245]536
[1893]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="';
[2245]543    $member_string.= get_root_url().'admin.php?page=history';
[1893]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  }
[2245]550
551  $template->assign(
552    'search_summary',
[1883]553    array(
[1932]554      'NB_LINES' => l10n_dec(
555        '%d line filtered', '%d lines filtered',
[1893]556        $page['nb_lines']
557        ),
558      'FILESIZE' => $summary['total_filesize'].' KB',
[1932]559      'USERS' => l10n_dec(
560        '%d user', '%d users',
[1893]561        $summary['nb_members'] + $summary['nb_guests']
562        ),
563      'MEMBERS' => sprintf(
[1932]564        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
[1893]565        implode(
566          ', ',
567          $member_strings
568          )
569        ),
[1932]570      'GUESTS' => l10n_dec(
571        '%d guest', '%d guests',
[1893]572        $summary['nb_guests']
573        ),
[1883]574      )
575    );
[1727]576}
577
578// +-----------------------------------------------------------------------+
579// |                            navigation bar                             |
580// +-----------------------------------------------------------------------+
581
582if (isset($page['search_id']))
583{
584  $navbar = create_navigation_bar(
[2245]585    get_root_url().'admin.php'.get_query_string_diff(array('start')),
[1727]586    $page['nb_lines'],
587    $page['start'],
588    $conf['nb_logs_page']
589    );
590
[3172]591  $template->assign('navbar', $navbar);
[1727]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']);
[2245]605
[1727]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']);
[1817]614
615    $form['end_year']  = (int)$tokens[0];
616    $form['end_month'] = (int)$tokens[1];
617    $form['end_day']   = (int)$tokens[2];
[1727]618  }
[1817]619
[1844]620  $form['types'] = $page['search']['fields']['types'];
[1890]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  }
[1892]630
631  $form['image_id'] = @$page['search']['fields']['image_id'];
632  $form['filename'] = @$page['search']['fields']['filename'];
[1992]633
634  $form['display_thumbnail'] = @$page['search']['fields']['display_thumbnail'];
[1727]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');
[1844]643  $form['types'] = $types;
[1992]644  // Hoverbox by default
645  $form['display_thumbnail'] =
[5576]646    pwg_get_cookie_var('display_thumbnail', 'no_display_thumbnail');
[1727]647}
648
649
[2245]650$month_list = $lang['month'];
651$month_list[0]='------------';
652ksort($month_list);
653
654$template->assign(
[1727]655  array(
[2245]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'],
[1727]663    'START_YEAR' => @$form['start_year'],
[2245]664
665    'END_DAY_SELECTED' => @$form['end_day'],
666    'END_MONTH_SELECTED' => @$form['end_month'],
[1727]667    'END_YEAR'   => @$form['end_year'],
668    )
669  );
[1817]670
[2245]671$template->assign(
[1844]672    array(
[2245]673      'type_option_values' => $types,
674      'type_option_selected' => $form['types']
[1890]675    )
676  );
677
[2245]678
[1890]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;';
[2245]686$template->assign(
687  array(
688    'user_options' => simple_hash_from_query($query, 'id','username'),
689    'user_options_selected' => array(@$form['user'])
690  )
691);
[1890]692
[5576]693$template->assign('display_thumbnails', $display_thumbnails);
694$template->assign('display_thumbnail_selected', $form['display_thumbnail']);
[1890]695
[1727]696// +-----------------------------------------------------------------------+
697// |                           html code display                           |
698// +-----------------------------------------------------------------------+
699
700$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
[2245]701?>
Note: See TracBrowser for help on using the repository browser.