source: trunk/admin/history.php @ 2299

Revision 2299, 18.4 KB checked in by plg, 12 years ago (diff)

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

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