source: trunk/admin/history.php @ 1932

Last change on this file since 1932 was 1932, checked in by rub, 17 years ago

o add missing $lang
o use of l10n_dec
o normalize file header

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 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 1932 2007-03-29 19:04:54Z rub $
8// | last update   : $Date: 2007-03-29 19:04:54 +0000 (Thu, 29 Mar 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1932 $
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// echo '<pre>$_POST:
32// '; print_r($_POST); echo '</pre>';
33// echo '<pre>$_GET:
34// '; print_r($_GET); echo '</pre>';
35
36// +-----------------------------------------------------------------------+
37// |                              functions                                |
38// +-----------------------------------------------------------------------+
39
40// +-----------------------------------------------------------------------+
41// |                           initialization                              |
42// +-----------------------------------------------------------------------+
43
44if (!defined('PHPWG_ROOT_PATH'))
45{
46  die('Hacking attempt!');
47}
48
49include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
50include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
51
52if (isset($_GET['start']) and is_numeric($_GET['start']))
53{
54  $page['start'] = $_GET['start'];
55}
56else
57{
58  $page['start'] = 0;
59}
60
61$types = array('none', 'picture', 'high', 'other');
62
63// +-----------------------------------------------------------------------+
64// | Check Access and exit when user status is not ok                      |
65// +-----------------------------------------------------------------------+
66
67check_status(ACCESS_ADMINISTRATOR);
68
69// +-----------------------------------------------------------------------+
70// | Build search criteria and redirect to results                         |
71// +-----------------------------------------------------------------------+
72
73$page['errors'] = array();
74$search = array();
75
76if (isset($_POST['submit']))
77{
78  // dates
79  if (!empty($_POST['start_year']))
80  {
81    $search['fields']['date-after'] = sprintf(
82      '%d-%02d-%02d',
83      $_POST['start_year'],
84      $_POST['start_month'],
85      $_POST['start_day']
86      );
87  }
88
89  if (!empty($_POST['end_year']))
90  {
91    $search['fields']['date-before'] = sprintf(
92      '%d-%02d-%02d',
93      $_POST['end_year'],
94      $_POST['end_month'],
95      $_POST['end_day']
96      );
97  }
98
99  $search['fields']['types'] = $_POST['types'];
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  // TODO manage inconsistency of having $_POST['image_id'] and
118  // $_POST['filename'] simultaneously
119 
120  // echo '<pre>'; print_r($search); echo '</pre>';
121 
122  if (!empty($search))
123  {
124    // register search rules in database, then they will be available on
125    // thumbnails page and picture page.
126    $query ='
127INSERT INTO '.SEARCH_TABLE.'
128  (rules)
129  VALUES
130  (\''.serialize($search).'\')
131;';
132    pwg_query($query);
133
134    $search_id = mysql_insert_id();
135   
136    redirect(
137      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
138      );
139  }
140  else
141  {
142    array_push($page['errors'], $lang['search_one_clause_at_least']);
143  }
144}
145
146// +-----------------------------------------------------------------------+
147// |                             template init                             |
148// +-----------------------------------------------------------------------+
149
150$template->set_filename('history', 'admin/history.tpl');
151
152// TabSheet initialization
153history_tabsheet();
154
155$base_url = PHPWG_ROOT_PATH.'admin.php?page=history';
156
157$template->assign_vars(
158  array(
159    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=history',
160
161    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=history'
162    )
163  );
164
165$template->assign_vars(
166  array(
167    'TODAY_DAY'   => date('d', time()),
168    'TODAY_MONTH' => date('m', time()),
169    'TODAY_YEAR'  => date('Y', time()),
170    )
171  );
172
173// +-----------------------------------------------------------------------+
174// |                             history lines                             |
175// +-----------------------------------------------------------------------+
176
177if (isset($_GET['search_id'])
178    and $page['search_id'] = (int)$_GET['search_id'])
179{
180  // what are the lines to display in reality ?
181  $query = '
182SELECT rules
183  FROM '.SEARCH_TABLE.'
184  WHERE id = '.$page['search_id'].'
185;';
186  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
187
188  $page['search'] = unserialize($serialized_rules);
189
190  if (isset($_GET['user_id']))
191  {
192    if (!is_numeric($_GET['user_id']))
193    {
194      die('user_id GET parameter must be an integer value');
195    }
196
197    $page['search']['fields']['user'] = $_GET['user_id'];
198   
199    $query ='
200INSERT INTO '.SEARCH_TABLE.'
201  (rules)
202  VALUES
203  (\''.serialize($page['search']).'\')
204;';
205    pwg_query($query);
206
207    $search_id = mysql_insert_id();
208   
209    redirect(
210      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
211      );
212  }
213
214 
215  if (isset($page['search']['fields']['filename']))
216  {
217    $query = '
218SELECT
219    id
220  FROM '.IMAGES_TABLE.'
221  WHERE file LIKE \''.$page['search']['fields']['filename'].'\'
222;';
223    $page['search']['image_ids'] = array_from_query($query, 'id');
224  }
225 
226  // echo '<pre>'; print_r($page['search']); echo '</pre>';
227 
228  $clauses = array();
229
230  if (isset($page['search']['fields']['date-after']))
231  {
232    array_push(
233      $clauses,
234      "date >= '".$page['search']['fields']['date-after']."'"
235      );
236  }
237
238  if (isset($page['search']['fields']['date-before']))
239  {
240    array_push(
241      $clauses,
242      "date <= '".$page['search']['fields']['date-before']."'"
243      );
244  }
245
246  if (isset($page['search']['fields']['types']))
247  {
248    $local_clauses = array();
249   
250    foreach ($types as $type) {
251      if (in_array($type, $page['search']['fields']['types'])) {
252        $clause = 'image_type ';
253        if ($type == 'none')
254        {
255          $clause.= 'IS NULL';
256        }
257        else
258        {
259          $clause.= "= '".$type."'";
260        }
261       
262        array_push($local_clauses, $clause);
263      }
264    }
265   
266    if (count($local_clauses) > 0)
267    {
268      array_push(
269        $clauses,
270        implode(' OR ', $local_clauses)
271        );
272    }
273  }
274
275  if (isset($page['search']['fields']['user'])
276      and $page['search']['fields']['user'] != -1)
277  {
278    array_push(
279      $clauses,
280      'user_id = '.$page['search']['fields']['user']
281      );
282  }
283
284  if (isset($page['search']['fields']['image_id']))
285  {
286    array_push(
287      $clauses,
288      'image_id = '.$page['search']['fields']['image_id']
289      );
290  }
291 
292  if (isset($page['search']['fields']['filename']))
293  {
294    if (count($page['search']['image_ids']) == 0)
295    {
296      // a clause that is always false
297      array_push($clauses, '1 = 2 ');
298    }
299    else
300    {
301      array_push(
302        $clauses,
303        'image_id IN ('.implode(', ', $page['search']['image_ids']).')'
304        );
305    }
306  }
307 
308  $clauses = prepend_append_array_items($clauses, '(', ')');
309
310  $where_separator =
311    implode(
312      "\n    AND ",
313      $clauses
314      );
315 
316  $query = '
317SELECT
318    date,
319    time,
320    user_id,
321    IP,
322    section,
323    category_id,
324    tag_ids,
325    image_id,
326    image_type
327  FROM '.HISTORY_TABLE.'
328  WHERE '.$where_separator.'
329;';
330
331  // LIMIT '.$page['start'].', '.$conf['nb_logs_page'].'
332
333  $result = pwg_query($query);
334
335  $page['nb_lines'] = mysql_num_rows($result);
336 
337  $history_lines = array();
338  $user_ids = array();
339  $username_of = 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' => l10n_dec(
616        '%d line filtered', '%d lines filtered',
617        $page['nb_lines']
618        ),
619      'FILESIZE' => $summary['total_filesize'].' KB',
620      'USERS' => l10n_dec(
621        '%d user', '%d users',
622        $summary['nb_members'] + $summary['nb_guests']
623        ),
624      'MEMBERS' => sprintf(
625        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
626        implode(
627          ', ',
628          $member_strings
629          )
630        ),
631      'GUESTS' => l10n_dec(
632        '%d guest', '%d guests',
633        $summary['nb_guests']
634        ),
635      )
636    );
637}
638
639// +-----------------------------------------------------------------------+
640// |                            navigation bar                             |
641// +-----------------------------------------------------------------------+
642
643if (isset($page['search_id']))
644{
645  $navbar = create_navigation_bar(
646    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
647    $page['nb_lines'],
648    $page['start'],
649    $conf['nb_logs_page']
650    );
651
652  $template->assign_block_vars(
653    'navigation',
654    array(
655      'NAVBAR' => $navbar
656      )
657    );
658}
659
660// +-----------------------------------------------------------------------+
661// |                             filter form                               |
662// +-----------------------------------------------------------------------+
663
664$form = array();
665
666if (isset($page['search']))
667{
668  if (isset($page['search']['fields']['date-after']))
669  {
670    $tokens = explode('-', $page['search']['fields']['date-after']);
671   
672    $form['start_year']  = (int)$tokens[0];
673    $form['start_month'] = (int)$tokens[1];
674    $form['start_day']   = (int)$tokens[2];
675  }
676
677  if (isset($page['search']['fields']['date-before']))
678  {
679    $tokens = explode('-', $page['search']['fields']['date-before']);
680
681    $form['end_year']  = (int)$tokens[0];
682    $form['end_month'] = (int)$tokens[1];
683    $form['end_day']   = (int)$tokens[2];
684  }
685
686  $form['types'] = $page['search']['fields']['types'];
687
688  if (isset($page['search']['fields']['user']))
689  {
690    $form['user'] = $page['search']['fields']['user'];
691  }
692  else
693  {
694    $form['user'] = null;
695  }
696
697  $form['image_id'] = @$page['search']['fields']['image_id'];
698  $form['filename'] = @$page['search']['fields']['filename'];
699}
700else
701{
702  // by default, at page load, we want the selected date to be the current
703  // date
704  $form['start_year']  = $form['end_year']  = date('Y');
705  $form['start_month'] = $form['end_month'] = date('n');
706  $form['start_day']   = $form['end_day']   = date('j');
707  $form['types'] = $types;
708}
709
710// start date
711get_day_list('start_day', @$form['start_day']);
712get_month_list('start_month', @$form['start_month']);
713// end date
714get_day_list('end_day', @$form['end_day']);
715get_month_list('end_month', @$form['end_month']);
716
717$template->assign_vars(
718  array(
719    'START_YEAR' => @$form['start_year'],
720    'END_YEAR'   => @$form['end_year'],
721    'IMAGE_ID' => @$form['image_id'],
722    'FILENAME' => @$form['filename'],
723    )
724  );
725
726foreach ($types as $option)
727{
728  $selected = '';
729 
730  if (in_array($option, $form['types']))
731  {
732    $selected = 'selected="selected"';
733  }
734 
735  $template->assign_block_vars(
736    'types_option',
737    array(
738      'VALUE' => $option,
739      'CONTENT' => l10n($option),
740      'SELECTED' => $selected,
741      )
742    );
743}
744
745$template->assign_block_vars(
746  'user_option',
747  array(
748    'VALUE'=> -1,
749    'CONTENT' => '------------',
750    'SELECTED' => ''
751    )
752  );
753
754$query = '
755SELECT
756    '.$conf['user_fields']['id'].' AS id,
757    '.$conf['user_fields']['username'].' AS username
758  FROM '.USERS_TABLE.'
759  ORDER BY username ASC
760;';
761$result = pwg_query($query);
762
763while ($row = mysql_fetch_array($result))
764{
765  $selected = '';
766
767  if (isset($form['user'])
768      and $row['id'] == $form['user'])
769  {
770    $selected = 'selected="selected"';
771  }
772 
773  $template->assign_block_vars(
774    'user_option',
775    array(
776      'VALUE' => $row['id'],
777      'CONTENT' => $row['username'],
778      'SELECTED' => $selected,
779      )
780    );
781}
782 
783// +-----------------------------------------------------------------------+
784// |                           html code display                           |
785// +-----------------------------------------------------------------------+
786
787$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
788?>
Note: See TracBrowser for help on using the repository browser.