source: trunk/admin/history.php @ 1896

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

Improvement: in history search filtered lines, clicking on filename leads to
standard size picture.

  • Property svn:eol-style set to native
  • 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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: history.php 1896 2007-03-12 00:15:45Z plg $
9// | last update   : $Date: 2007-03-12 00:15:45 +0000 (Mon, 12 Mar 2007) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1896 $
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    $image_string = '';
546    if (isset($line['image_id']))
547    {
548      $picture_url = make_picture_url(
549        array(
550          'image_id' => $line['image_id'],
551          )
552        );
553     
554      $image_string = '<a href="'.$picture_url.'">';
555      $image_string.= '('.$line['image_id'].')';
556
557      if (isset($label_of_image[$line['image_id']]))
558      {
559        $image_string.= ' '.$label_of_image[$line['image_id']];
560      }
561      else
562      {
563        $image_string.= ' unknown filename';
564      }
565    }
566   
567    $template->assign_block_vars(
568      'detail',
569      array(
570        'DATE'      => $line['date'],
571        'TIME'      => $line['time'],
572        'USER'      => $user_string,
573        'IP'        => $line['IP'],
574        'IMAGE'     => $image_string,
575        'TYPE'      => $line['image_type'],
576        'SECTION'   => $line['section'],
577        'CATEGORY'  => isset($line['category_id'])
578          ? ( isset($name_of_category[$line['category_id']])
579                ? $name_of_category[$line['category_id']]
580                : 'deleted '.$line['category_id'] )
581          : '',
582        'TAGS'       => $tags_string,
583        'T_CLASS'   => ($i % 2) ? 'row1' : 'row2',
584        )
585      );
586  }
587
588  $summary['nb_guests'] = 0;
589  if (count(array_keys($summary['guests_IP'])) > 0)
590  {
591    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
592
593    // we delete the "guest" from the $username_of hash so that it is
594    // avoided in next steps
595    unset($username_of[ $conf['guest_id'] ]);
596  }
597 
598  $summary['nb_members'] = count($username_of);
599
600  $member_strings = array();
601  foreach ($username_of as $user_id => $user_name)
602  {
603    $member_string = $user_name.'&nbsp;<a href="';
604    $member_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
605    $member_string.= '&amp;search_id='.$page['search_id'];
606    $member_string.= '&amp;user_id='.$user_id;
607    $member_string.= '">+</a>';
608
609    $member_strings[] = $member_string;
610  }
611 
612  $template->assign_block_vars(
613    'summary',
614    array(
615      'NB_LINES' => sprintf(
616        l10n('%d lines filtered'),
617        $page['nb_lines']
618        ),
619      'FILESIZE' => $summary['total_filesize'].' KB',
620      'USERS' => sprintf(
621        l10n('%d users'),
622        $summary['nb_members'] + $summary['nb_guests']
623        ),
624      'MEMBERS' => sprintf(
625        l10n('%d members: %s'),
626        $summary['nb_members'],
627        implode(
628          ', ',
629          $member_strings
630          )
631        ),
632      'GUESTS' => sprintf(
633        l10n('%d guests'),
634        $summary['nb_guests']
635        ),
636      )
637    );
638}
639
640// +-----------------------------------------------------------------------+
641// |                            navigation bar                             |
642// +-----------------------------------------------------------------------+
643
644if (isset($page['search_id']))
645{
646  $navbar = create_navigation_bar(
647    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
648    $page['nb_lines'],
649    $page['start'],
650    $conf['nb_logs_page']
651    );
652
653  $template->assign_block_vars(
654    'navigation',
655    array(
656      'NAVBAR' => $navbar
657      )
658    );
659}
660
661// +-----------------------------------------------------------------------+
662// |                             filter form                               |
663// +-----------------------------------------------------------------------+
664
665$form = array();
666
667if (isset($page['search']))
668{
669  if (isset($page['search']['fields']['date-after']))
670  {
671    $tokens = explode('-', $page['search']['fields']['date-after']);
672   
673    $form['start_year']  = (int)$tokens[0];
674    $form['start_month'] = (int)$tokens[1];
675    $form['start_day']   = (int)$tokens[2];
676  }
677
678  if (isset($page['search']['fields']['date-before']))
679  {
680    $tokens = explode('-', $page['search']['fields']['date-before']);
681
682    $form['end_year']  = (int)$tokens[0];
683    $form['end_month'] = (int)$tokens[1];
684    $form['end_day']   = (int)$tokens[2];
685  }
686
687  $form['types'] = $page['search']['fields']['types'];
688
689  if (isset($page['search']['fields']['user']))
690  {
691    $form['user'] = $page['search']['fields']['user'];
692  }
693  else
694  {
695    $form['user'] = null;
696  }
697
698  $form['image_id'] = @$page['search']['fields']['image_id'];
699  $form['filename'] = @$page['search']['fields']['filename'];
700}
701else
702{
703  // by default, at page load, we want the selected date to be the current
704  // date
705  $form['start_year']  = $form['end_year']  = date('Y');
706  $form['start_month'] = $form['end_month'] = date('n');
707  $form['start_day']   = $form['end_day']   = date('j');
708  $form['types'] = $types;
709}
710
711// start date
712get_day_list('start_day', @$form['start_day']);
713get_month_list('start_month', @$form['start_month']);
714// end date
715get_day_list('end_day', @$form['end_day']);
716get_month_list('end_month', @$form['end_month']);
717
718$template->assign_vars(
719  array(
720    'START_YEAR' => @$form['start_year'],
721    'END_YEAR'   => @$form['end_year'],
722    'IMAGE_ID' => @$form['image_id'],
723    'FILENAME' => @$form['filename'],
724    )
725  );
726
727foreach ($types as $option)
728{
729  $selected = '';
730 
731  if (in_array($option, $form['types']))
732  {
733    $selected = 'selected="selected"';
734  }
735 
736  $template->assign_block_vars(
737    'types_option',
738    array(
739      'VALUE' => $option,
740      'CONTENT' => l10n($option),
741      'SELECTED' => $selected,
742      )
743    );
744}
745
746$template->assign_block_vars(
747  'user_option',
748  array(
749    'VALUE'=> -1,
750    'CONTENT' => '------------',
751    'SELECTED' => ''
752    )
753  );
754
755$query = '
756SELECT
757    '.$conf['user_fields']['id'].' AS id,
758    '.$conf['user_fields']['username'].' AS username
759  FROM '.USERS_TABLE.'
760  ORDER BY username ASC
761;';
762$result = pwg_query($query);
763
764while ($row = mysql_fetch_array($result))
765{
766  $selected = '';
767
768  if (isset($form['user'])
769      and $row['id'] == $form['user'])
770  {
771    $selected = 'selected="selected"';
772  }
773 
774  $template->assign_block_vars(
775    'user_option',
776    array(
777      'VALUE' => $row['id'],
778      'CONTENT' => $row['username'],
779      'SELECTED' => $selected,
780      )
781    );
782}
783 
784// +-----------------------------------------------------------------------+
785// |                           html code display                           |
786// +-----------------------------------------------------------------------+
787
788$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
789?>
Note: See TracBrowser for help on using the repository browser.