source: trunk/admin/history.php @ 15583

Last change on this file since 15583 was 14688, checked in by rvelices, 12 years ago

feature 2601: Allow searching by ip in admin history
also remove php warnings when tags zere deleted after visits

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