source: trunk/admin/history.php @ 28542

Last change on this file since 28542 was 28500, checked in by mistic100, 11 years ago

feature 2679 : allow to change creation time

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