source: trunk/admin/history.php @ 1991

Last change on this file since 1991 was 1991, checked in by vdigital, 17 years ago

Bug 0000684: History [Search] - Add a thumbnail display.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 20.7 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 1991 2007-04-29 19:04:57Z vdigital $
8// | last update   : $Date: 2007-04-29 19:04:57 +0000 (Sun, 29 Apr 2007) $
9// | last modifier : $Author: vdigital $
10// | revision      : $Revision: 1991 $
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    file,
418    path,
419    tn_ext
420  FROM '.IMAGES_TABLE.'
421  WHERE id IN ('.implode(',', array_keys($image_ids)).')
422;';
423    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
424    $label_of_image = array();
425    $filesize_of_image = array();
426    $high_filesize_of_image = array();
427    $file_of_image = array();
428    $path_of_image = array();
429    $tn_ext_of_image = array();
430
431    $result = pwg_query($query);
432    while ($row = mysql_fetch_array($result))
433    {
434      $label_of_image[ $row['id'] ] = $row['label'];
435
436      if (isset($row['filesize']))
437      {
438        $filesize_of_image[ $row['id'] ] = $row['filesize'];
439      }
440
441      if (isset($row['high_filesize']))
442      {
443        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
444      }
445
446      $file_of_image[ $row['id'] ] = $row['file'];
447      $path_of_image[ $row['id'] ] = $row['path'];
448      $tn_ext_of_image[ $row['id'] ] = $row['tn_ext'];
449    }
450
451    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
452  }
453
454  if (count($tag_ids) > 0)
455  {
456    $tag_ids = array_unique($tag_ids);
457
458    $query = '
459SELECT
460    id,
461    name
462  FROM '.TAGS_TABLE.'
463  WHERE id IN ('.implode(', ', $tag_ids).')
464;';
465    $name_of_tag = array();
466
467    $result = pwg_query($query);
468    while ($row = mysql_fetch_array($result))
469    {
470      $name_of_tag[ $row['id'] ] = $row['name'];
471    }
472  }
473 
474  $i = 0;
475  $first_line = $page['start'] + 1;
476  $last_line = $page['start'] + $conf['nb_logs_page'];
477
478  $summary['total_filesize'] = 0;
479  $summary['guests_IP'] = array();
480
481  foreach ($history_lines as $line)
482  {
483    // FIXME when we watch the representative of a non image element, it is
484    // the not the representative filesize that is counted (as it is
485    // unknown) but the non image element filesize. Proposed solution: add
486    // #images.representative_filesize and add 'representative' in the
487    // choices of #history.image_type.
488   
489    if (isset($line['image_type']))
490    {
491      if ($line['image_type'] == 'high')
492      {
493        if (isset($high_filesize_of_image[$line['image_id']]))
494        {
495          $summary['total_filesize']+=
496            $high_filesize_of_image[$line['image_id']];
497        }
498      }
499      else
500      {
501        if (isset($filesize_of_image[$line['image_id']]))
502        {
503          $summary['total_filesize']+=
504            $filesize_of_image[$line['image_id']];
505        }
506      }
507    }
508
509    if ($line['user_id'] == $conf['guest_id'])
510    {
511      if (!isset($summary['guests_IP'][ $line['IP'] ]))
512      {
513        $summary['guests_IP'][ $line['IP'] ] = 0;
514      }
515     
516      $summary['guests_IP'][ $line['IP'] ]++;
517    }
518   
519    $i++;
520   
521    if ($i < $first_line or $i > $last_line)
522    {
523      continue;
524    }
525
526    $user_string = '';
527    if (isset($username_of[$line['user_id']]))
528    {
529      $user_string.= $username_of[$line['user_id']];
530    }
531    else
532    {
533      $user_string.= $line['user_id'];
534    }
535    $user_string.= '&nbsp;<a href="';
536    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
537    $user_string.= '&amp;search_id='.$page['search_id'];
538    $user_string.= '&amp;user_id='.$line['user_id'];
539    $user_string.= '">+</a>';
540
541    $tags_string = '';
542    if (isset($line['tag_ids']))
543    {
544      $tags_string = preg_replace(
545        '/(\d+)/e',
546        '$name_of_tag["$1"]',
547        str_replace(
548          ',',
549          ', ',
550          $line['tag_ids']
551          )
552        );
553    }
554
555    $image_string = '';
556    if (isset($line['image_id']))
557    {
558      $picture_url = make_picture_url(
559        array(
560          'image_id' => $line['image_id'],
561          )
562        );
563     
564      // <a class="thumbnail" href="#thumb">(1258)<span><img src="./galleries/category/thumbnail/th-dsc1258.png"></span></a>
565      $element = array(
566           'id' => $line['image_id'],
567           'file' => $file_of_image[$line['image_id']],
568           'path' => $path_of_image[$line['image_id']],
569           'tn_ext' => $tn_ext_of_image[$line['image_id']],
570           );
571      $image_string = '';   
572      if (!isset($conf['history_no_thumb']) or $conf['history_no_thumb']) {
573        $thumb_mode = "over";
574        if (isset($conf['history_no_hover']) and $conf['history_no_hover']) {
575          $thumb_mode = "thumbnail";
576        }
577        $image_string = '<a class="'.$thumb_mode.'" href="#thumb">'   
578                      .'('.$line['image_id'].') <span><img src="'
579                      . get_thumbnail_url( $element ) 
580                      .'"></span></a><a href="'.$picture_url.'">';
581      }
582      else {
583        $image_string= '<a href="'.$picture_url.'">';
584        $image_string.= '('.$line['image_id'].')';
585      }
586     
587      if (isset($label_of_image[$line['image_id']]))
588      {
589        $image_string.= ' '.$label_of_image[$line['image_id']];
590      }
591      else
592      {
593        $image_string.= ' unknown filename';
594      }
595    }
596   
597    $template->assign_block_vars(
598      'detail',
599      array(
600        'DATE'      => $line['date'],
601        'TIME'      => $line['time'],
602        'USER'      => $user_string,
603        'IP'        => $line['IP'],
604        'IMAGE'     => $image_string,
605        'TYPE'      => $line['image_type'],
606        'SECTION'   => $line['section'],
607        'CATEGORY'  => isset($line['category_id'])
608          ? ( isset($name_of_category[$line['category_id']])
609                ? $name_of_category[$line['category_id']]
610                : 'deleted '.$line['category_id'] )
611          : '',
612        'TAGS'       => $tags_string,
613        'T_CLASS'   => ($i % 2) ? 'row1' : 'row2',
614        )
615      );
616  }
617
618  $summary['nb_guests'] = 0;
619  if (count(array_keys($summary['guests_IP'])) > 0)
620  {
621    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
622
623    // we delete the "guest" from the $username_of hash so that it is
624    // avoided in next steps
625    unset($username_of[ $conf['guest_id'] ]);
626  }
627 
628  $summary['nb_members'] = count($username_of);
629
630  $member_strings = array();
631  foreach ($username_of as $user_id => $user_name)
632  {
633    $member_string = $user_name.'&nbsp;<a href="';
634    $member_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
635    $member_string.= '&amp;search_id='.$page['search_id'];
636    $member_string.= '&amp;user_id='.$user_id;
637    $member_string.= '">+</a>';
638
639    $member_strings[] = $member_string;
640  }
641 
642  $template->assign_block_vars(
643    'summary',
644    array(
645      'NB_LINES' => l10n_dec(
646        '%d line filtered', '%d lines filtered',
647        $page['nb_lines']
648        ),
649      'FILESIZE' => $summary['total_filesize'].' KB',
650      'USERS' => l10n_dec(
651        '%d user', '%d users',
652        $summary['nb_members'] + $summary['nb_guests']
653        ),
654      'MEMBERS' => sprintf(
655        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
656        implode(
657          ', ',
658          $member_strings
659          )
660        ),
661      'GUESTS' => l10n_dec(
662        '%d guest', '%d guests',
663        $summary['nb_guests']
664        ),
665      )
666    );
667}
668
669// +-----------------------------------------------------------------------+
670// |                            navigation bar                             |
671// +-----------------------------------------------------------------------+
672
673if (isset($page['search_id']))
674{
675  $navbar = create_navigation_bar(
676    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
677    $page['nb_lines'],
678    $page['start'],
679    $conf['nb_logs_page']
680    );
681
682  $template->assign_block_vars(
683    'navigation',
684    array(
685      'NAVBAR' => $navbar
686      )
687    );
688}
689
690// +-----------------------------------------------------------------------+
691// |                             filter form                               |
692// +-----------------------------------------------------------------------+
693
694$form = array();
695
696if (isset($page['search']))
697{
698  if (isset($page['search']['fields']['date-after']))
699  {
700    $tokens = explode('-', $page['search']['fields']['date-after']);
701   
702    $form['start_year']  = (int)$tokens[0];
703    $form['start_month'] = (int)$tokens[1];
704    $form['start_day']   = (int)$tokens[2];
705  }
706
707  if (isset($page['search']['fields']['date-before']))
708  {
709    $tokens = explode('-', $page['search']['fields']['date-before']);
710
711    $form['end_year']  = (int)$tokens[0];
712    $form['end_month'] = (int)$tokens[1];
713    $form['end_day']   = (int)$tokens[2];
714  }
715
716  $form['types'] = $page['search']['fields']['types'];
717
718  if (isset($page['search']['fields']['user']))
719  {
720    $form['user'] = $page['search']['fields']['user'];
721  }
722  else
723  {
724    $form['user'] = null;
725  }
726
727  $form['image_id'] = @$page['search']['fields']['image_id'];
728  $form['filename'] = @$page['search']['fields']['filename'];
729}
730else
731{
732  // by default, at page load, we want the selected date to be the current
733  // date
734  $form['start_year']  = $form['end_year']  = date('Y');
735  $form['start_month'] = $form['end_month'] = date('n');
736  $form['start_day']   = $form['end_day']   = date('j');
737  $form['types'] = $types;
738}
739
740// start date
741get_day_list('start_day', @$form['start_day']);
742get_month_list('start_month', @$form['start_month']);
743// end date
744get_day_list('end_day', @$form['end_day']);
745get_month_list('end_month', @$form['end_month']);
746
747$template->assign_vars(
748  array(
749    'START_YEAR' => @$form['start_year'],
750    'END_YEAR'   => @$form['end_year'],
751    'IMAGE_ID' => @$form['image_id'],
752    'FILENAME' => @$form['filename'],
753    )
754  );
755
756foreach ($types as $option)
757{
758  $selected = '';
759 
760  if (in_array($option, $form['types']))
761  {
762    $selected = 'selected="selected"';
763  }
764 
765  $template->assign_block_vars(
766    'types_option',
767    array(
768      'VALUE' => $option,
769      'CONTENT' => l10n($option),
770      'SELECTED' => $selected,
771      )
772    );
773}
774
775$template->assign_block_vars(
776  'user_option',
777  array(
778    'VALUE'=> -1,
779    'CONTENT' => '------------',
780    'SELECTED' => ''
781    )
782  );
783
784$query = '
785SELECT
786    '.$conf['user_fields']['id'].' AS id,
787    '.$conf['user_fields']['username'].' AS username
788  FROM '.USERS_TABLE.'
789  ORDER BY username ASC
790;';
791$result = pwg_query($query);
792
793while ($row = mysql_fetch_array($result))
794{
795  $selected = '';
796
797  if (isset($form['user'])
798      and $row['id'] == $form['user'])
799  {
800    $selected = 'selected="selected"';
801  }
802 
803  $template->assign_block_vars(
804    'user_option',
805    array(
806      'VALUE' => $row['id'],
807      'CONTENT' => $row['username'],
808      'SELECTED' => $selected,
809      )
810    );
811}
812 
813// +-----------------------------------------------------------------------+
814// |                           html code display                           |
815// +-----------------------------------------------------------------------+
816
817$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
818?>
Note: See TracBrowser for help on using the repository browser.