source: trunk/admin/history.php @ 2250

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