source: trunk/admin/history.php @ 30956

Last change on this file since 30956 was 30956, checked in by mistic100, 9 years ago

bug 3188: history search, trailing space in dates

  • Property svn:eol-style set to LF
File size: 17.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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']))
77  {
78    check_input_parameter('start', $_POST, false, '/^\d{4}-\d{2}-\d{2}$/');
79    $search['fields']['date-after'] = $_POST['start'];
80  }
81
82  if (!empty($_POST['end']))
83  {
84    check_input_parameter('end', $_POST, false, '/^\d{4}-\d{2}-\d{2}$/');
85    $search['fields']['date-before'] = $_POST['end'];
86  }
87
88  if (empty($_POST['types']))
89  {
90    $search['fields']['types'] = $types;
91  }
92  else
93  {
94    check_input_parameter('types', $_POST, true, '/^('.implode('|', $types).')$/');
95    $search['fields']['types'] = $_POST['types'];
96  }
97
98  $search['fields']['user'] = intval($_POST['user']);
99
100  if (!empty($_POST['image_id']))
101  {
102    $search['fields']['image_id'] = intval($_POST['image_id']);
103  }
104
105  if (!empty($_POST['filename']))
106  {
107    $search['fields']['filename'] = str_replace(
108      '*',
109      '%',
110      pwg_db_real_escape_string($_POST['filename'])
111      );
112  }
113
114  if (!empty($_POST['ip']))
115  {
116    $search['fields']['ip'] = str_replace(
117      '*',
118      '%',
119      pwg_db_real_escape_string($_POST['ip'])
120      );
121  }
122
123  check_input_parameter('display_thumbnail', $_POST, false, '/^('.implode('|', array_keys($display_thumbnails)).')$/');
124 
125  $search['fields']['display_thumbnail'] = $_POST['display_thumbnail'];
126  // Display choise are also save to one cookie
127  if (!empty($_POST['display_thumbnail'])
128      and isset($display_thumbnails[$_POST['display_thumbnail']]))
129  {
130    $cookie_val = $_POST['display_thumbnail'];
131  }
132  else
133  {
134    $cookie_val = null;
135  }
136
137  pwg_set_cookie_var('display_thumbnail', $cookie_val, strtotime('+1 month') );
138
139  // TODO manage inconsistency of having $_POST['image_id'] and
140  // $_POST['filename'] simultaneously
141
142  if (!empty($search))
143  {
144    // register search rules in database, then they will be available on
145    // thumbnails page and picture page.
146    $query ='
147INSERT INTO '.SEARCH_TABLE.'
148  (rules)
149  VALUES
150  (\''.pwg_db_real_escape_string(serialize($search)).'\')
151;';
152
153    pwg_query($query);
154
155    $search_id = pwg_db_insert_id(SEARCH_TABLE);
156
157    redirect(
158      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
159      );
160  }
161  else
162  {
163    $page['errors'][] = l10n('Empty query. No criteria has been entered.');
164  }
165}
166
167// +-----------------------------------------------------------------------+
168// |                             template init                             |
169// +-----------------------------------------------------------------------+
170
171$template->set_filename('history', 'history.tpl');
172
173// TabSheet initialization
174history_tabsheet();
175
176$template->assign(
177  array(
178    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
179    'F_ACTION' => get_root_url().'admin.php?page=history'
180    )
181  );
182
183// +-----------------------------------------------------------------------+
184// |                             history lines                             |
185// +-----------------------------------------------------------------------+
186
187if (isset($_GET['search_id'])
188    and $page['search_id'] = (int)$_GET['search_id'])
189{
190  // what are the lines to display in reality ?
191  $query = '
192SELECT rules
193  FROM '.SEARCH_TABLE.'
194  WHERE id = '.$page['search_id'].'
195;';
196  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
197
198  $page['search'] = unserialize($serialized_rules);
199
200  if (isset($_GET['user_id']))
201  {
202    if (!is_numeric($_GET['user_id']))
203    {
204      die('user_id GET parameter must be an integer value');
205    }
206
207    $page['search']['fields']['user'] = $_GET['user_id'];
208
209    $query ='
210INSERT INTO '.SEARCH_TABLE.'
211  (rules)
212  VALUES
213  (\''.serialize($page['search']).'\')
214;';
215    pwg_query($query);
216
217    $search_id = pwg_db_insert_id(SEARCH_TABLE);
218
219    redirect(
220      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
221      );
222  }
223
224  /*TODO - no need to get a huge number of rows from db (should take only what needed for display + SQL_CALC_FOUND_ROWS*/
225  $data = trigger_change('get_history', array(), $page['search'], $types);
226  usort($data, 'history_compare');
227
228  $page['nb_lines'] = count($data);
229
230  $history_lines = array();
231  $user_ids = array();
232  $username_of = array();
233  $category_ids = array();
234  $image_ids = array();
235  $has_tags = false;
236
237  foreach ($data as $row)
238  {
239    $user_ids[$row['user_id']] = 1;
240
241    if (isset($row['category_id']))
242    {
243      $category_ids[$row['category_id']] = 1;
244    }
245
246    if (isset($row['image_id']))
247    {
248      $image_ids[$row['image_id']] = 1;
249    }
250
251    if (isset($row['tag_ids']))
252    {
253      $has_tags = true;
254    }
255
256    $history_lines[] = $row;
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 = query2array($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    file,
304    path,
305    representative_ext
306  FROM '.IMAGES_TABLE.'
307  WHERE id IN ('.implode(',', array_keys($image_ids)).')
308;';
309    $image_infos = query2array($query, 'id');
310  }
311
312  if ($has_tags > 0)
313  {
314    $query = '
315SELECT
316    id,
317    name, url_name
318  FROM '.TAGS_TABLE;
319
320    global $name_of_tag; // used for preg_replace
321    $name_of_tag = array();
322    $result = pwg_query($query);
323    while ($row=pwg_db_fetch_assoc($result))
324    {
325      $name_of_tag[ $row['id'] ] = '<a href="'.make_index_url( array('tags'=>array($row))).'">'.trigger_change("render_tag_name", $row['name'], $row).'</a>';
326    }
327  }
328
329  $i = 0;
330  $first_line = $page['start'] + 1;
331  $last_line = $page['start'] + $conf['nb_logs_page'];
332
333  $summary['total_filesize'] = 0;
334  $summary['guests_IP'] = array();
335
336  foreach ($history_lines as $line)
337  {
338    if (isset($line['image_type']) and $line['image_type'] == 'high')
339    {
340      $summary['total_filesize'] += @intval($image_infos[$line['image_id']]['filesize']);
341    }
342
343    if ($line['user_id'] == $conf['guest_id'])
344    {
345      if (!isset($summary['guests_IP'][ $line['IP'] ]))
346      {
347        $summary['guests_IP'][ $line['IP'] ] = 0;
348      }
349
350      $summary['guests_IP'][ $line['IP'] ]++;
351    }
352
353    $i++;
354
355    if ($i < $first_line or $i > $last_line)
356    {
357      continue;
358    }
359
360    $user_string = '';
361    if (isset($username_of[$line['user_id']]))
362    {
363      $user_string.= $username_of[$line['user_id']];
364    }
365    else
366    {
367      $user_string.= $line['user_id'];
368    }
369    $user_string.= '&nbsp;<a href="';
370    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
371    $user_string.= '&amp;search_id='.$page['search_id'];
372    $user_string.= '&amp;user_id='.$line['user_id'];
373    $user_string.= '">+</a>';
374
375    $tags_string = '';
376    if (isset($line['tag_ids']))
377    {
378      $tags_string = preg_replace_callback(
379        '/(\d+)/',
380        create_function('$m', 'global $name_of_tag; return isset($name_of_tag[$m[1]]) ? $name_of_tag[$m[1]] : $m[1];'),
381        str_replace(
382          ',',
383          ', ',
384          $line['tag_ids']
385          )
386        );
387    }
388
389    $image_string = '';
390    if (isset($line['image_id']))
391    {
392      $picture_url = make_picture_url(
393        array(
394          'image_id' => $line['image_id'],
395          )
396        );
397
398      if (isset($image_infos[$line['image_id']]))
399      {
400        $element = array(
401          'id' => $line['image_id'],
402          'file' => $image_infos[$line['image_id']]['file'],
403          'path' => $image_infos[$line['image_id']]['path'],
404          'representative_ext' => $image_infos[$line['image_id']]['representative_ext'],
405          );
406        $thumbnail_display = $page['search']['fields']['display_thumbnail'];
407      }
408      else
409      {
410        $thumbnail_display = 'no_display_thumbnail';
411      }
412
413      $image_title = '('.$line['image_id'].')';
414
415      if (isset($image_infos[$line['image_id']]['label']))
416      {
417        $image_title.= ' '.trigger_change('render_element_description', $image_infos[$line['image_id']]['label']);
418      }
419      else
420      {
421        $image_title.= ' unknown filename';
422      }
423
424      $image_string = '';
425
426      switch ($thumbnail_display)
427      {
428        case 'no_display_thumbnail':
429        {
430          $image_string= '<a href="'.$picture_url.'">'.$image_title.'</a>';
431          break;
432        }
433        case 'display_thumbnail_classic':
434        {
435          $image_string =
436            '<a class="thumbnail" href="'.$picture_url.'">'
437            .'<span><img src="'.DerivativeImage::thumb_url($element)
438            .'" alt="'.$image_title.'" title="'.$image_title.'">'
439            .'</span></a>';
440          break;
441        }
442        case 'display_thumbnail_hoverbox':
443        {
444          $image_string =
445            '<a class="over" href="'.$picture_url.'">'
446            .'<span><img src="'.DerivativeImage::thumb_url($element)
447            .'" alt="'.$image_title.'" title="'.$image_title.'">'
448            .'</span>'.$image_title.'</a>';
449          break;
450        }
451      }
452    }
453
454    $template->append(
455      'search_results',
456      array(
457        'DATE'      => $line['date'],
458        'TIME'      => $line['time'],
459        'USER'      => $user_string,
460        'IP'        => $line['IP'],
461        'IMAGE'     => $image_string,
462        'TYPE'      => $line['image_type'],
463        'SECTION'   => $line['section'],
464        'CATEGORY'  => isset($line['category_id'])
465          ? ( isset($name_of_category[$line['category_id']])
466                ? $name_of_category[$line['category_id']]
467                : 'deleted '.$line['category_id'] )
468          : '',
469        'TAGS'       => $tags_string,
470        )
471      );
472  }
473
474  $summary['nb_guests'] = 0;
475  if (count(array_keys($summary['guests_IP'])) > 0)
476  {
477    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
478
479    // we delete the "guest" from the $username_of hash so that it is
480    // avoided in next steps
481    unset($username_of[ $conf['guest_id'] ]);
482  }
483
484  $summary['nb_members'] = count($username_of);
485
486  $member_strings = array();
487  foreach ($username_of as $user_id => $user_name)
488  {
489    $member_string = $user_name.'&nbsp;<a href="';
490    $member_string.= get_root_url().'admin.php?page=history';
491    $member_string.= '&amp;search_id='.$page['search_id'];
492    $member_string.= '&amp;user_id='.$user_id;
493    $member_string.= '">+</a>';
494
495    $member_strings[] = $member_string;
496  }
497
498  $template->assign(
499    'search_summary',
500    array(
501      'NB_LINES' => l10n_dec(
502        '%d line filtered', '%d lines filtered',
503        $page['nb_lines']
504        ),
505      'FILESIZE' => $summary['total_filesize'] != 0 ? ceil($summary['total_filesize']/1024).' MB' : '',
506      'USERS' => l10n_dec(
507        '%d user', '%d users',
508        $summary['nb_members'] + $summary['nb_guests']
509        ),
510      'MEMBERS' => sprintf(
511        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
512        implode(', ', $member_strings)
513        ),
514      'GUESTS' => l10n_dec(
515        '%d guest', '%d guests',
516        $summary['nb_guests']
517        ),
518      )
519    );
520
521  unset($name_of_tag);
522}
523
524// +-----------------------------------------------------------------------+
525// |                            navigation bar                             |
526// +-----------------------------------------------------------------------+
527
528if (isset($page['search_id']))
529{
530  $navbar = create_navigation_bar(
531    get_root_url().'admin.php'.get_query_string_diff(array('start')),
532    $page['nb_lines'],
533    $page['start'],
534    $conf['nb_logs_page']
535    );
536
537  $template->assign('navbar', $navbar);
538}
539
540// +-----------------------------------------------------------------------+
541// |                             filter form                               |
542// +-----------------------------------------------------------------------+
543
544$form = array();
545
546if (isset($page['search']))
547{
548  if (isset($page['search']['fields']['date-after']))
549  {
550    $form['start'] = $page['search']['fields']['date-after'];
551  }
552
553  if (isset($page['search']['fields']['date-before']))
554  {
555    $form['end'] = $page['search']['fields']['date-before'];
556  }
557
558  $form['types'] = $page['search']['fields']['types'];
559
560  if (isset($page['search']['fields']['user']))
561  {
562    $form['user'] = $page['search']['fields']['user'];
563  }
564  else
565  {
566    $form['user'] = null;
567  }
568
569  $form['image_id'] = @$page['search']['fields']['image_id'];
570  $form['filename'] = @$page['search']['fields']['filename'];
571  $form['ip'] = @$page['search']['fields']['ip'];
572
573  $form['display_thumbnail'] = @$page['search']['fields']['display_thumbnail'];
574}
575else
576{
577  // by default, at page load, we want the selected date to be the current
578  // date
579  $form['start'] = $form['end'] = date('Y-m-d');
580  $form['types'] = $types;
581  // Hoverbox by default
582  $form['display_thumbnail'] =
583    pwg_get_cookie_var('display_thumbnail', 'no_display_thumbnail');
584}
585
586
587$template->assign(
588  array(
589    'IMAGE_ID' => @$form['image_id'],
590    'FILENAME' => @$form['filename'],
591    'IP' => @$form['ip'],
592    'START' => @$form['start'],
593    'END' => @$form['end'],
594    )
595  );
596
597$template->assign(
598    array(
599      'type_option_values' => $types,
600      'type_option_selected' => $form['types']
601    )
602  );
603
604
605$query = '
606SELECT
607    '.$conf['user_fields']['id'].' AS id,
608    '.$conf['user_fields']['username'].' AS username
609  FROM '.USERS_TABLE.'
610  ORDER BY username ASC
611;';
612$template->assign(
613  array(
614    'user_options' => query2array($query, 'id','username'),
615    'user_options_selected' => array(@$form['user'])
616  )
617);
618
619$template->assign('display_thumbnails', $display_thumbnails);
620$template->assign('display_thumbnail_selected', $form['display_thumbnail']);
621
622// +-----------------------------------------------------------------------+
623// |                           html code display                           |
624// +-----------------------------------------------------------------------+
625
626$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
627?>
Note: See TracBrowser for help on using the repository browser.