source: trunk/admin/history.php @ 2157

Last change on this file since 2157 was 2157, checked in by rub, 16 years ago

Resolved issue 0000774: Statistics & plugin triggers => multi history

First part

Merge branch-1_7 2155:2156 into BSF

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: history.php 2157 2007-11-01 17:11:52Z rub $
8// | last update   : $Date: 2007-11-01 17:11:52 +0000 (Thu, 01 Nov 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2157 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27/**
28 * Display filtered history lines
29 */
30
31// +-----------------------------------------------------------------------+
32// |                              functions                                |
33// +-----------------------------------------------------------------------+
34
35// +-----------------------------------------------------------------------+
36// |                           initialization                              |
37// +-----------------------------------------------------------------------+
38
39if (!defined('PHPWG_ROOT_PATH'))
40{
41  die('Hacking attempt!');
42}
43
44include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
45include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
46
47if (isset($_GET['start']) and is_numeric($_GET['start']))
48{
49  $page['start'] = $_GET['start'];
50}
51else
52{
53  $page['start'] = 0;
54}
55
56$types = array('none', 'picture', 'high', 'other');
57$display_thumbnails = array('no_display_thumbnail', 'display_thumbnail_classic', 'display_thumbnail_hoverbox');
58
59// +-----------------------------------------------------------------------+
60// | Check Access and exit when user status is not ok                      |
61// +-----------------------------------------------------------------------+
62
63check_status(ACCESS_ADMINISTRATOR);
64
65// +-----------------------------------------------------------------------+
66// | Build search criteria and redirect to results                         |
67// +-----------------------------------------------------------------------+
68
69$page['errors'] = array();
70$search = array();
71
72if (isset($_POST['submit']))
73{
74  // dates
75  if (!empty($_POST['start_year']))
76  {
77    $search['fields']['date-after'] = sprintf(
78      '%d-%02d-%02d',
79      $_POST['start_year'],
80      $_POST['start_month'],
81      $_POST['start_day']
82      );
83  }
84
85  if (!empty($_POST['end_year']))
86  {
87    $search['fields']['date-before'] = sprintf(
88      '%d-%02d-%02d',
89      $_POST['end_year'],
90      $_POST['end_month'],
91      $_POST['end_day']
92      );
93  }
94
95  if (empty($_POST['types']))
96  {
97    $search['fields']['types'] = $types;
98  }
99  else
100  {
101    $search['fields']['types'] = $_POST['types'];
102  }
103
104  $search['fields']['user'] = $_POST['user'];
105
106  if (!empty($_POST['image_id']))
107  {
108    $search['fields']['image_id'] = intval($_POST['image_id']);
109  }
110 
111  if (!empty($_POST['filename']))
112  {
113    $search['fields']['filename'] = str_replace(
114      '*',
115      '%',
116      mysql_escape_string($_POST['filename'])
117      );
118  }
119
120  $search['fields']['display_thumbnail'] = $_POST['display_thumbnail'];
121  // Display choise are also save to one cookie
122  pwg_set_cookie_var('history_display_thumbnail', $_POST['display_thumbnail']);
123
124  // TODO manage inconsistency of having $_POST['image_id'] and
125  // $_POST['filename'] simultaneously
126 
127  // echo '<pre>'; print_r($search); echo '</pre>';
128 
129  if (!empty($search))
130  {
131    // register search rules in database, then they will be available on
132    // thumbnails page and picture page.
133    $query ='
134INSERT INTO '.SEARCH_TABLE.'
135  (rules)
136  VALUES
137  (\''.serialize($search).'\')
138;';
139    pwg_query($query);
140
141    $search_id = mysql_insert_id();
142   
143    redirect(
144      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
145      );
146  }
147  else
148  {
149    array_push($page['errors'], $lang['search_one_clause_at_least']);
150  }
151}
152
153// +-----------------------------------------------------------------------+
154// |                             template init                             |
155// +-----------------------------------------------------------------------+
156
157$template->set_filename('history', 'admin/history.tpl');
158
159// TabSheet initialization
160history_tabsheet();
161
162$base_url = PHPWG_ROOT_PATH.'admin.php?page=history';
163
164$template->assign_vars(
165  array(
166    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=history',
167
168    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=history'
169    )
170  );
171
172$template->assign_vars(
173  array(
174    'TODAY_DAY'   => date('d', time()),
175    'TODAY_MONTH' => date('m', time()),
176    'TODAY_YEAR'  => date('Y', time()),
177    )
178  );
179
180// +-----------------------------------------------------------------------+
181// |                             history lines                             |
182// +-----------------------------------------------------------------------+
183
184if (isset($_GET['search_id'])
185    and $page['search_id'] = (int)$_GET['search_id'])
186{
187  // what are the lines to display in reality ?
188  $query = '
189SELECT rules
190  FROM '.SEARCH_TABLE.'
191  WHERE id = '.$page['search_id'].'
192;';
193  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
194
195  $page['search'] = unserialize($serialized_rules);
196
197  if (isset($_GET['user_id']))
198  {
199    if (!is_numeric($_GET['user_id']))
200    {
201      die('user_id GET parameter must be an integer value');
202    }
203
204    $page['search']['fields']['user'] = $_GET['user_id'];
205
206    $query ='
207INSERT INTO '.SEARCH_TABLE.'
208  (rules)
209  VALUES
210  (\''.serialize($page['search']).'\')
211;';
212    pwg_query($query);
213
214    $search_id = mysql_insert_id();
215   
216    redirect(
217      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
218      );
219  }
220
221  $data = trigger_event('get_history', array(), $page['search'], $types);
222  usort($data, 'history_compare');
223
224  $page['nb_lines'] = count($data);
225
226  $history_lines = array();
227  $user_ids = array();
228  $username_of = array();
229  $category_ids = array();
230  $image_ids = array();
231  $tag_ids = array();
232
233  foreach ($data as $row)
234  {
235    $user_ids[$row['user_id']] = 1;
236
237    if (isset($row['category_id']))
238    {
239      $category_ids[$row['category_id']] = 1;
240    }
241
242    if (isset($row['image_id']))
243    {
244      $image_ids[$row['image_id']] = 1;
245    }
246
247    if (isset($row['tag_ids']))
248    {
249      foreach (explode(',', $row['tag_ids']) as $tag_id)
250      {
251        array_push($tag_ids, $tag_id);
252      }
253    }
254
255    array_push(
256      $history_lines,
257      $row
258      );
259  }
260
261  // prepare reference data (users, tags, categories...)
262  if (count($user_ids) > 0)
263  {
264    $query = '
265SELECT '.$conf['user_fields']['id'].' AS id
266     , '.$conf['user_fields']['username'].' AS username
267  FROM '.USERS_TABLE.'
268  WHERE id IN ('.implode(',', array_keys($user_ids)).')
269;';
270    $result = pwg_query($query);
271
272    $username_of = array();
273    while ($row = mysql_fetch_array($result))
274    {
275      $username_of[$row['id']] = $row['username'];
276    }
277  }
278
279  if (count($category_ids) > 0)
280  {
281    $query = '
282SELECT id, uppercats
283  FROM '.CATEGORIES_TABLE.'
284  WHERE id IN ('.implode(',', array_keys($category_ids)).')
285;';
286    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
287
288    $name_of_category = array();
289   
290    foreach ($uppercats_of as $category_id => $uppercats)
291    {
292      $name_of_category[$category_id] = get_cat_display_name_cache(
293        $uppercats
294        );
295    }
296  }
297
298  if (count($image_ids) > 0)
299  {
300    $query = '
301SELECT
302    id,
303    IF(name IS NULL, file, name) AS label,
304    filesize,
305    high_filesize,
306    file,
307    path,
308    tn_ext
309  FROM '.IMAGES_TABLE.'
310  WHERE id IN ('.implode(',', array_keys($image_ids)).')
311;';
312    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
313    $label_of_image = array();
314    $filesize_of_image = array();
315    $high_filesize_of_image = array();
316    $file_of_image = array();
317    $path_of_image = array();
318    $tn_ext_of_image = array();
319
320    $result = pwg_query($query);
321    while ($row = mysql_fetch_array($result))
322    {
323      $label_of_image[ $row['id'] ] = $row['label'];
324
325      if (isset($row['filesize']))
326      {
327        $filesize_of_image[ $row['id'] ] = $row['filesize'];
328      }
329
330      if (isset($row['high_filesize']))
331      {
332        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
333      }
334
335      $file_of_image[ $row['id'] ] = $row['file'];
336      $path_of_image[ $row['id'] ] = $row['path'];
337      $tn_ext_of_image[ $row['id'] ] = $row['tn_ext'];
338    }
339
340    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
341  }
342
343  if (count($tag_ids) > 0)
344  {
345    $tag_ids = array_unique($tag_ids);
346
347    $query = '
348SELECT
349    id,
350    name
351  FROM '.TAGS_TABLE.'
352  WHERE id IN ('.implode(', ', $tag_ids).')
353;';
354    $name_of_tag = array();
355
356    $result = pwg_query($query);
357    while ($row = mysql_fetch_array($result))
358    {
359      $name_of_tag[ $row['id'] ] = $row['name'];
360    }
361  }
362 
363  $i = 0;
364  $first_line = $page['start'] + 1;
365  $last_line = $page['start'] + $conf['nb_logs_page'];
366
367  $summary['total_filesize'] = 0;
368  $summary['guests_IP'] = array();
369
370  foreach ($history_lines as $line)
371  {
372    // FIXME when we watch the representative of a non image element, it is
373    // the not the representative filesize that is counted (as it is
374    // unknown) but the non image element filesize. Proposed solution: add
375    // #images.representative_filesize and add 'representative' in the
376    // choices of #history.image_type.
377   
378    if (isset($line['image_type']))
379    {
380      if ($line['image_type'] == 'high')
381      {
382        if (isset($high_filesize_of_image[$line['image_id']]))
383        {
384          $summary['total_filesize']+=
385            $high_filesize_of_image[$line['image_id']];
386        }
387      }
388      else
389      {
390        if (isset($filesize_of_image[$line['image_id']]))
391        {
392          $summary['total_filesize']+=
393            $filesize_of_image[$line['image_id']];
394        }
395      }
396    }
397
398    if ($line['user_id'] == $conf['guest_id'])
399    {
400      if (!isset($summary['guests_IP'][ $line['IP'] ]))
401      {
402        $summary['guests_IP'][ $line['IP'] ] = 0;
403      }
404     
405      $summary['guests_IP'][ $line['IP'] ]++;
406    }
407   
408    $i++;
409   
410    if ($i < $first_line or $i > $last_line)
411    {
412      continue;
413    }
414
415    $user_string = '';
416    if (isset($username_of[$line['user_id']]))
417    {
418      $user_string.= $username_of[$line['user_id']];
419    }
420    else
421    {
422      $user_string.= $line['user_id'];
423    }
424    $user_string.= '&nbsp;<a href="';
425    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
426    $user_string.= '&amp;search_id='.$page['search_id'];
427    $user_string.= '&amp;user_id='.$line['user_id'];
428    $user_string.= '">+</a>';
429
430    $tags_string = '';
431    if (isset($line['tag_ids']))
432    {
433      $tags_string = preg_replace(
434        '/(\d+)/e',
435        '$name_of_tag["$1"]',
436        str_replace(
437          ',',
438          ', ',
439          $line['tag_ids']
440          )
441        );
442    }
443
444    $image_string = '';
445    if (isset($line['image_id']))
446    {
447      $picture_url = make_picture_url(
448        array(
449          'image_id' => $line['image_id'],
450          )
451        );
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
460      $image_title = '('.$line['image_id'].')';
461
462      if (isset($label_of_image[$line['image_id']]))
463      {
464        $image_title.= ' '.$label_of_image[$line['image_id']];
465      }
466      else
467      {
468        $image_title.= ' unknown filename';
469      }
470
471      $image_string = '';
472
473      switch ($page['search']['fields']['display_thumbnail'])
474      {
475        case 'no_display_thumbnail':
476        {
477          $image_string= '<a href="'.$picture_url.'">'.$image_title.'</a>';
478          break;
479        }
480        case 'display_thumbnail_classic':
481        {
482          $image_string =
483            '<a class="thumbnail" href="'.$picture_url.'">'
484            .'<span><img src="'.get_thumbnail_url($element)
485            .'" alt="'.$image_title.'" title="'.$image_title.'">'
486            .'</span></a>';
487          break;
488        }
489        case 'display_thumbnail_hoverbox':
490        {
491          $image_string =
492            '<a class="over" href="'.$picture_url.'">'
493            .'<span><img src="'.get_thumbnail_url($element)
494            .'" alt="'.$image_title.'" title="'.$image_title.'">'
495            .'</span>'.$image_title.'</a>';
496          break;
497        }
498      }
499    }
500   
501    $template->assign_block_vars(
502      'detail',
503      array(
504        'DATE'      => $line['date'],
505        'TIME'      => $line['time'],
506        'USER'      => $user_string,
507        'IP'        => $line['IP'],
508        'IMAGE'     => $image_string,
509        'TYPE'      => $line['image_type'],
510        'SECTION'   => $line['section'],
511        'CATEGORY'  => isset($line['category_id'])
512          ? ( isset($name_of_category[$line['category_id']])
513                ? $name_of_category[$line['category_id']]
514                : 'deleted '.$line['category_id'] )
515          : '',
516        'TAGS'       => $tags_string,
517        'T_CLASS'   => ($i % 2) ? 'row1' : 'row2',
518        )
519      );
520  }
521
522  $summary['nb_guests'] = 0;
523  if (count(array_keys($summary['guests_IP'])) > 0)
524  {
525    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
526
527    // we delete the "guest" from the $username_of hash so that it is
528    // avoided in next steps
529    unset($username_of[ $conf['guest_id'] ]);
530  }
531 
532  $summary['nb_members'] = count($username_of);
533
534  $member_strings = array();
535  foreach ($username_of as $user_id => $user_name)
536  {
537    $member_string = $user_name.'&nbsp;<a href="';
538    $member_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
539    $member_string.= '&amp;search_id='.$page['search_id'];
540    $member_string.= '&amp;user_id='.$user_id;
541    $member_string.= '">+</a>';
542
543    $member_strings[] = $member_string;
544  }
545 
546  $template->assign_block_vars(
547    'summary',
548    array(
549      'NB_LINES' => l10n_dec(
550        '%d line filtered', '%d lines filtered',
551        $page['nb_lines']
552        ),
553      'FILESIZE' => $summary['total_filesize'].' KB',
554      'USERS' => l10n_dec(
555        '%d user', '%d users',
556        $summary['nb_members'] + $summary['nb_guests']
557        ),
558      'MEMBERS' => sprintf(
559        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
560        implode(
561          ', ',
562          $member_strings
563          )
564        ),
565      'GUESTS' => l10n_dec(
566        '%d guest', '%d guests',
567        $summary['nb_guests']
568        ),
569      )
570    );
571}
572
573// +-----------------------------------------------------------------------+
574// |                            navigation bar                             |
575// +-----------------------------------------------------------------------+
576
577if (isset($page['search_id']))
578{
579  $navbar = create_navigation_bar(
580    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
581    $page['nb_lines'],
582    $page['start'],
583    $conf['nb_logs_page']
584    );
585
586  $template->assign_block_vars(
587    'navigation',
588    array(
589      'NAVBAR' => $navbar
590      )
591    );
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']);
605   
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']);
614
615    $form['end_year']  = (int)$tokens[0];
616    $form['end_month'] = (int)$tokens[1];
617    $form['end_day']   = (int)$tokens[2];
618  }
619
620  $form['types'] = $page['search']['fields']['types'];
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  }
630
631  $form['image_id'] = @$page['search']['fields']['image_id'];
632  $form['filename'] = @$page['search']['fields']['filename'];
633
634  $form['display_thumbnail'] = @$page['search']['fields']['display_thumbnail'];
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');
643  $form['types'] = $types;
644  // Hoverbox by default
645  $form['display_thumbnail'] =
646    pwg_get_cookie_var('history_display_thumbnail', $display_thumbnails[2]);
647}
648
649// start date
650get_day_list('start_day', @$form['start_day']);
651get_month_list('start_month', @$form['start_month']);
652// end date
653get_day_list('end_day', @$form['end_day']);
654get_month_list('end_month', @$form['end_month']);
655
656$template->assign_vars(
657  array(
658    'START_YEAR' => @$form['start_year'],
659    'END_YEAR'   => @$form['end_year'],
660    'IMAGE_ID' => @$form['image_id'],
661    'FILENAME' => @$form['filename'],
662    )
663  );
664
665foreach ($types as $option)
666{
667  $selected = '';
668 
669  if (in_array($option, $form['types']))
670  {
671    $selected = 'selected="selected"';
672  }
673 
674  $template->assign_block_vars(
675    'types_option',
676    array(
677      'VALUE' => $option,
678      'CONTENT' => l10n($option),
679      'SELECTED' => $selected,
680      )
681    );
682}
683
684$template->assign_block_vars(
685  'user_option',
686  array(
687    'VALUE'=> -1,
688    'CONTENT' => '------------',
689    'SELECTED' => ''
690    )
691  );
692
693$query = '
694SELECT
695    '.$conf['user_fields']['id'].' AS id,
696    '.$conf['user_fields']['username'].' AS username
697  FROM '.USERS_TABLE.'
698  ORDER BY username ASC
699;';
700$result = pwg_query($query);
701
702while ($row = mysql_fetch_array($result))
703{
704  $selected = '';
705
706  if (isset($form['user'])
707      and $row['id'] == $form['user'])
708  {
709    $selected = 'selected="selected"';
710  }
711 
712  $template->assign_block_vars(
713    'user_option',
714    array(
715      'VALUE' => $row['id'],
716      'CONTENT' => $row['username'],
717      'SELECTED' => $selected,
718      )
719    );
720}
721
722foreach ($display_thumbnails as $display_thumbnail)
723{
724  $selected = '';
725 
726  if ($display_thumbnail === $form['display_thumbnail'])
727  {
728    $selected = 'selected="selected"';
729  }
730 
731  $template->assign_block_vars(
732    'display_thumbnail',
733    array(
734      'VALUE' => $display_thumbnail,
735      'CONTENT' => l10n($display_thumbnail),
736      'SELECTED' => $selected,
737      )
738    );
739}
740
741// +-----------------------------------------------------------------------+
742// |                           html code display                           |
743// +-----------------------------------------------------------------------+
744
745$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
746?>
Note: See TracBrowser for help on using the repository browser.