source: trunk/admin/history.php @ 1893

Last change on this file since 1893 was 1893, checked in by plg, 17 years ago

New: history search results summary gives the number of filtered lines and
give details about number of total users with the number of guests and the
member names (that can be added to filter criteria).

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