source: trunk/admin/history.php @ 8728

Last change on this file since 8728 was 8728, checked in by plg, 13 years ago

Happy new year 2011

Change "Piwigo - a PHP based picture gallery" into "Piwigo - a PHP based photo gallery"

  • Property svn:eol-style set to LF
File size: 18.7 KB
RevLine 
[1727]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1727]23
24/**
25 * Display filtered history lines
26 */
27
28// +-----------------------------------------------------------------------+
29// |                              functions                                |
30// +-----------------------------------------------------------------------+
31
32// +-----------------------------------------------------------------------+
33// |                           initialization                              |
34// +-----------------------------------------------------------------------+
35
36if (!defined('PHPWG_ROOT_PATH'))
37{
38  die('Hacking attempt!');
39}
40
41include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[1881]42include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
[1727]43
44if (isset($_GET['start']) and is_numeric($_GET['start']))
45{
46  $page['start'] = $_GET['start'];
47}
48else
49{
50  $page['start'] = 0;
51}
52
[1844]53$types = array('none', 'picture', 'high', 'other');
54
[5576]55$display_thumbnails = array('no_display_thumbnail' => l10n('No display'),
56                            'display_thumbnail_classic' => l10n('Classic display'),
57                            'display_thumbnail_hoverbox' => l10n('Hoverbox display')
58  );
59
[1727]60// +-----------------------------------------------------------------------+
61// | Check Access and exit when user status is not ok                      |
62// +-----------------------------------------------------------------------+
63
64check_status(ACCESS_ADMINISTRATOR);
65
66// +-----------------------------------------------------------------------+
67// | Build search criteria and redirect to results                         |
68// +-----------------------------------------------------------------------+
69
[1892]70$page['errors'] = array();
[1727]71$search = array();
72
73if (isset($_POST['submit']))
74{
75  // dates
76  if (!empty($_POST['start_year']))
77  {
78    $search['fields']['date-after'] = sprintf(
79      '%d-%02d-%02d',
80      $_POST['start_year'],
81      $_POST['start_month'],
82      $_POST['start_day']
83      );
84  }
85
86  if (!empty($_POST['end_year']))
87  {
88    $search['fields']['date-before'] = sprintf(
89      '%d-%02d-%02d',
90      $_POST['end_year'],
91      $_POST['end_month'],
92      $_POST['end_day']
93      );
94  }
95
[2157]96  if (empty($_POST['types']))
97  {
98    $search['fields']['types'] = $types;
99  }
100  else
101  {
102    $search['fields']['types'] = $_POST['types'];
103  }
[1890]104
105  $search['fields']['user'] = $_POST['user'];
[1892]106
107  if (!empty($_POST['image_id']))
108  {
109    $search['fields']['image_id'] = intval($_POST['image_id']);
110  }
[2245]111
[1892]112  if (!empty($_POST['filename']))
113  {
114    $search['fields']['filename'] = str_replace(
115      '*',
116      '%',
[4325]117      pwg_db_real_escape_string($_POST['filename'])
[1892]118      );
119  }
120
[1992]121  $search['fields']['display_thumbnail'] = $_POST['display_thumbnail'];
122  // Display choise are also save to one cookie
[5576]123  if (!empty($_POST['display_thumbnail'])
124      and isset($display_thumbnails[$_POST['display_thumbnail']]))
125  {
126    $cookie_val = $_POST['display_thumbnail'];
127  }
128  else
129  {
130    $cookie_val = null;
131  }
132 
133  pwg_set_cookie_var('display_thumbnail', $cookie_val, strtotime('+1 month') );
[1992]134
[1892]135  // TODO manage inconsistency of having $_POST['image_id'] and
136  // $_POST['filename'] simultaneously
[2245]137
[1727]138  if (!empty($search))
139  {
140    // register search rules in database, then they will be available on
141    // thumbnails page and picture page.
142    $query ='
143INSERT INTO '.SEARCH_TABLE.'
144  (rules)
145  VALUES
146  (\''.serialize($search).'\')
147;';
148    pwg_query($query);
149
[4892]150    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[2245]151
[1727]152    redirect(
153      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
154      );
155  }
156  else
157  {
[5021]158    array_push($page['errors'], l10n('Empty query. No criteria has been entered.'));
[1727]159  }
160}
161
162// +-----------------------------------------------------------------------+
163// |                             template init                             |
164// +-----------------------------------------------------------------------+
165
[2530]166$template->set_filename('history', 'history.tpl');
[1727]167
[1881]168// TabSheet initialization
169history_tabsheet();
170
[2245]171$template->assign(
[1727]172  array(
[5920]173    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
[2245]174    'F_ACTION' => get_root_url().'admin.php?page=history'
[1727]175    )
176  );
177
178// +-----------------------------------------------------------------------+
179// |                             history lines                             |
180// +-----------------------------------------------------------------------+
181
182if (isset($_GET['search_id'])
183    and $page['search_id'] = (int)$_GET['search_id'])
184{
185  // what are the lines to display in reality ?
186  $query = '
187SELECT rules
188  FROM '.SEARCH_TABLE.'
189  WHERE id = '.$page['search_id'].'
190;';
[4325]191  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
[1727]192
193  $page['search'] = unserialize($serialized_rules);
194
[1890]195  if (isset($_GET['user_id']))
196  {
197    if (!is_numeric($_GET['user_id']))
198    {
199      die('user_id GET parameter must be an integer value');
200    }
201
202    $page['search']['fields']['user'] = $_GET['user_id'];
[2157]203
[1890]204    $query ='
205INSERT INTO '.SEARCH_TABLE.'
206  (rules)
207  VALUES
208  (\''.serialize($page['search']).'\')
209;';
210    pwg_query($query);
211
[4892]212    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[2245]213
[1890]214    redirect(
215      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
216      );
217  }
218
[2157]219  $data = trigger_event('get_history', array(), $page['search'], $types);
220  usort($data, 'history_compare');
[1727]221
[2157]222  $page['nb_lines'] = count($data);
[1727]223
[1883]224  $history_lines = array();
225  $user_ids = array();
[1929]226  $username_of = array();
[1883]227  $category_ids = array();
228  $image_ids = array();
229  $tag_ids = array();
[2157]230
231  foreach ($data as $row)
[1727]232  {
233    $user_ids[$row['user_id']] = 1;
234
235    if (isset($row['category_id']))
236    {
237      $category_ids[$row['category_id']] = 1;
238    }
239
240    if (isset($row['image_id']))
241    {
242      $image_ids[$row['image_id']] = 1;
243    }
244
[1891]245    if (isset($row['tag_ids']))
246    {
247      foreach (explode(',', $row['tag_ids']) as $tag_id)
248      {
249        array_push($tag_ids, $tag_id);
250      }
251    }
252
[1727]253    array_push(
254      $history_lines,
255      $row
256      );
257  }
258
259  // prepare reference data (users, tags, categories...)
260  if (count($user_ids) > 0)
261  {
262    $query = '
263SELECT '.$conf['user_fields']['id'].' AS id
264     , '.$conf['user_fields']['username'].' AS username
265  FROM '.USERS_TABLE.'
266  WHERE id IN ('.implode(',', array_keys($user_ids)).')
267;';
268    $result = pwg_query($query);
269
270    $username_of = array();
[4325]271    while ($row = pwg_db_fetch_assoc($result))
[1727]272    {
[4304]273      $username_of[$row['id']] = stripslashes($row['username']);
[1727]274    }
275  }
276
277  if (count($category_ids) > 0)
278  {
279    $query = '
280SELECT id, uppercats
281  FROM '.CATEGORIES_TABLE.'
282  WHERE id IN ('.implode(',', array_keys($category_ids)).')
283;';
284    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
285
286    $name_of_category = array();
[2245]287
[1727]288    foreach ($uppercats_of as $category_id => $uppercats)
289    {
290      $name_of_category[$category_id] = get_cat_display_name_cache(
291        $uppercats
292        );
293    }
294  }
295
296  if (count($image_ids) > 0)
297  {
298    $query = '
[1883]299SELECT
300    id,
301    IF(name IS NULL, file, name) AS label,
302    filesize,
[1991]303    high_filesize,
304    file,
305    path,
306    tn_ext
[1727]307  FROM '.IMAGES_TABLE.'
308  WHERE id IN ('.implode(',', array_keys($image_ids)).')
309;';
[1883]310    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
311    $label_of_image = array();
312    $filesize_of_image = array();
313    $high_filesize_of_image = array();
[1991]314    $file_of_image = array();
315    $path_of_image = array();
316    $tn_ext_of_image = array();
317
[1883]318    $result = pwg_query($query);
[4325]319    while ($row = pwg_db_fetch_assoc($result))
[1883]320    {
321      $label_of_image[ $row['id'] ] = $row['label'];
322
323      if (isset($row['filesize']))
324      {
325        $filesize_of_image[ $row['id'] ] = $row['filesize'];
326      }
327
328      if (isset($row['high_filesize']))
329      {
330        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
331      }
[1991]332
333      $file_of_image[ $row['id'] ] = $row['file'];
334      $path_of_image[ $row['id'] ] = $row['path'];
335      $tn_ext_of_image[ $row['id'] ] = $row['tn_ext'];
[1883]336    }
337
338    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
[1727]339  }
[1891]340
341  if (count($tag_ids) > 0)
342  {
343    $tag_ids = array_unique($tag_ids);
344
345    $query = '
346SELECT
347    id,
348    name
349  FROM '.TAGS_TABLE.'
350  WHERE id IN ('.implode(', ', $tag_ids).')
351;';
352    $name_of_tag = array();
353
354    $result = pwg_query($query);
[4325]355    while ($row = pwg_db_fetch_assoc($result))
[1891]356    {
357      $name_of_tag[ $row['id'] ] = $row['name'];
358    }
359  }
[2245]360
[1727]361  $i = 0;
[1883]362  $first_line = $page['start'] + 1;
363  $last_line = $page['start'] + $conf['nb_logs_page'];
[1727]364
[1893]365  $summary['total_filesize'] = 0;
366  $summary['guests_IP'] = array();
[1883]367
[1727]368  foreach ($history_lines as $line)
369  {
[1893]370    // FIXME when we watch the representative of a non image element, it is
371    // the not the representative filesize that is counted (as it is
372    // unknown) but the non image element filesize. Proposed solution: add
373    // #images.representative_filesize and add 'representative' in the
374    // choices of #history.image_type.
[2245]375
[1883]376    if (isset($line['image_type']))
377    {
378      if ($line['image_type'] == 'high')
379      {
380        if (isset($high_filesize_of_image[$line['image_id']]))
381        {
[1893]382          $summary['total_filesize']+=
383            $high_filesize_of_image[$line['image_id']];
[1883]384        }
385      }
386      else
387      {
388        if (isset($filesize_of_image[$line['image_id']]))
389        {
[1893]390          $summary['total_filesize']+=
391            $filesize_of_image[$line['image_id']];
[1883]392        }
393      }
394    }
[1893]395
396    if ($line['user_id'] == $conf['guest_id'])
397    {
398      if (!isset($summary['guests_IP'][ $line['IP'] ]))
399      {
400        $summary['guests_IP'][ $line['IP'] ] = 0;
401      }
[2245]402
[1893]403      $summary['guests_IP'][ $line['IP'] ]++;
404    }
[2245]405
[1883]406    $i++;
[2245]407
[1883]408    if ($i < $first_line or $i > $last_line)
409    {
410      continue;
411    }
[1890]412
413    $user_string = '';
414    if (isset($username_of[$line['user_id']]))
415    {
416      $user_string.= $username_of[$line['user_id']];
417    }
418    else
419    {
420      $user_string.= $line['user_id'];
421    }
422    $user_string.= '&nbsp;<a href="';
423    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
424    $user_string.= '&amp;search_id='.$page['search_id'];
425    $user_string.= '&amp;user_id='.$line['user_id'];
426    $user_string.= '">+</a>';
[1891]427
428    $tags_string = '';
429    if (isset($line['tag_ids']))
430    {
431      $tags_string = preg_replace(
432        '/(\d+)/e',
433        '$name_of_tag["$1"]',
434        str_replace(
435          ',',
436          ', ',
437          $line['tag_ids']
438          )
439        );
440    }
[1896]441
442    $image_string = '';
443    if (isset($line['image_id']))
444    {
445      $picture_url = make_picture_url(
446        array(
447          'image_id' => $line['image_id'],
448          )
449        );
[1992]450
[1991]451      $element = array(
452           'id' => $line['image_id'],
453           'file' => $file_of_image[$line['image_id']],
454           'path' => $path_of_image[$line['image_id']],
455           'tn_ext' => $tn_ext_of_image[$line['image_id']],
456           );
[1992]457
458      $image_title = '('.$line['image_id'].')';
459
[1896]460      if (isset($label_of_image[$line['image_id']]))
461      {
[1992]462        $image_title.= ' '.$label_of_image[$line['image_id']];
[1896]463      }
464      else
465      {
[1992]466        $image_title.= ' unknown filename';
[1896]467      }
[1992]468
469      $image_string = '';
470
471      switch ($page['search']['fields']['display_thumbnail'])
472      {
473        case 'no_display_thumbnail':
474        {
475          $image_string= '<a href="'.$picture_url.'">'.$image_title.'</a>';
476          break;
477        }
478        case 'display_thumbnail_classic':
479        {
480          $image_string =
481            '<a class="thumbnail" href="'.$picture_url.'">'
482            .'<span><img src="'.get_thumbnail_url($element)
483            .'" alt="'.$image_title.'" title="'.$image_title.'">'
484            .'</span></a>';
485          break;
486        }
487        case 'display_thumbnail_hoverbox':
488        {
489          $image_string =
490            '<a class="over" href="'.$picture_url.'">'
491            .'<span><img src="'.get_thumbnail_url($element)
492            .'" alt="'.$image_title.'" title="'.$image_title.'">'
493            .'</span>'.$image_title.'</a>';
494          break;
495        }
496      }
[1896]497    }
[2245]498
499    $template->append(
500      'search_results',
[1727]501      array(
502        'DATE'      => $line['date'],
503        'TIME'      => $line['time'],
[1890]504        'USER'      => $user_string,
[1727]505        'IP'        => $line['IP'],
[1896]506        'IMAGE'     => $image_string,
[1844]507        'TYPE'      => $line['image_type'],
[1727]508        'SECTION'   => $line['section'],
509        'CATEGORY'  => isset($line['category_id'])
[1821]510          ? ( isset($name_of_category[$line['category_id']])
511                ? $name_of_category[$line['category_id']]
512                : 'deleted '.$line['category_id'] )
[1727]513          : '',
[1891]514        'TAGS'       => $tags_string,
[1727]515        )
516      );
517  }
[1883]518
[1893]519  $summary['nb_guests'] = 0;
520  if (count(array_keys($summary['guests_IP'])) > 0)
521  {
522    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
523
524    // we delete the "guest" from the $username_of hash so that it is
525    // avoided in next steps
526    unset($username_of[ $conf['guest_id'] ]);
527  }
[2245]528
[1893]529  $summary['nb_members'] = count($username_of);
530
531  $member_strings = array();
532  foreach ($username_of as $user_id => $user_name)
533  {
534    $member_string = $user_name.'&nbsp;<a href="';
[2245]535    $member_string.= get_root_url().'admin.php?page=history';
[1893]536    $member_string.= '&amp;search_id='.$page['search_id'];
537    $member_string.= '&amp;user_id='.$user_id;
538    $member_string.= '">+</a>';
539
540    $member_strings[] = $member_string;
541  }
[2245]542
543  $template->assign(
544    'search_summary',
[1883]545    array(
[1932]546      'NB_LINES' => l10n_dec(
547        '%d line filtered', '%d lines filtered',
[1893]548        $page['nb_lines']
549        ),
550      'FILESIZE' => $summary['total_filesize'].' KB',
[1932]551      'USERS' => l10n_dec(
552        '%d user', '%d users',
[1893]553        $summary['nb_members'] + $summary['nb_guests']
554        ),
555      'MEMBERS' => sprintf(
[1932]556        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
[1893]557        implode(
558          ', ',
559          $member_strings
560          )
561        ),
[1932]562      'GUESTS' => l10n_dec(
563        '%d guest', '%d guests',
[1893]564        $summary['nb_guests']
565        ),
[1883]566      )
567    );
[1727]568}
569
570// +-----------------------------------------------------------------------+
571// |                            navigation bar                             |
572// +-----------------------------------------------------------------------+
573
574if (isset($page['search_id']))
575{
576  $navbar = create_navigation_bar(
[2245]577    get_root_url().'admin.php'.get_query_string_diff(array('start')),
[1727]578    $page['nb_lines'],
579    $page['start'],
580    $conf['nb_logs_page']
581    );
582
[3172]583  $template->assign('navbar', $navbar);
[1727]584}
585
586// +-----------------------------------------------------------------------+
587// |                             filter form                               |
588// +-----------------------------------------------------------------------+
589
590$form = array();
591
592if (isset($page['search']))
593{
594  if (isset($page['search']['fields']['date-after']))
595  {
596    $tokens = explode('-', $page['search']['fields']['date-after']);
[2245]597
[1727]598    $form['start_year']  = (int)$tokens[0];
599    $form['start_month'] = (int)$tokens[1];
600    $form['start_day']   = (int)$tokens[2];
601  }
602
603  if (isset($page['search']['fields']['date-before']))
604  {
605    $tokens = explode('-', $page['search']['fields']['date-before']);
[1817]606
607    $form['end_year']  = (int)$tokens[0];
608    $form['end_month'] = (int)$tokens[1];
609    $form['end_day']   = (int)$tokens[2];
[1727]610  }
[1817]611
[1844]612  $form['types'] = $page['search']['fields']['types'];
[1890]613
614  if (isset($page['search']['fields']['user']))
615  {
616    $form['user'] = $page['search']['fields']['user'];
617  }
618  else
619  {
620    $form['user'] = null;
621  }
[1892]622
623  $form['image_id'] = @$page['search']['fields']['image_id'];
624  $form['filename'] = @$page['search']['fields']['filename'];
[1992]625
626  $form['display_thumbnail'] = @$page['search']['fields']['display_thumbnail'];
[1727]627}
628else
629{
630  // by default, at page load, we want the selected date to be the current
631  // date
632  $form['start_year']  = $form['end_year']  = date('Y');
633  $form['start_month'] = $form['end_month'] = date('n');
634  $form['start_day']   = $form['end_day']   = date('j');
[1844]635  $form['types'] = $types;
[1992]636  // Hoverbox by default
637  $form['display_thumbnail'] =
[5576]638    pwg_get_cookie_var('display_thumbnail', 'no_display_thumbnail');
[1727]639}
640
641
[2245]642$month_list = $lang['month'];
643$month_list[0]='------------';
644ksort($month_list);
645
646$template->assign(
[1727]647  array(
[2245]648    'IMAGE_ID' => @$form['image_id'],
649    'FILENAME' => @$form['filename'],
650
651    'month_list' => $month_list,
652
653    'START_DAY_SELECTED' => @$form['start_day'],
654    'START_MONTH_SELECTED' => @$form['start_month'],
[1727]655    'START_YEAR' => @$form['start_year'],
[2245]656
657    'END_DAY_SELECTED' => @$form['end_day'],
658    'END_MONTH_SELECTED' => @$form['end_month'],
[1727]659    'END_YEAR'   => @$form['end_year'],
660    )
661  );
[1817]662
[2245]663$template->assign(
[1844]664    array(
[2245]665      'type_option_values' => $types,
666      'type_option_selected' => $form['types']
[1890]667    )
668  );
669
[2245]670
[1890]671$query = '
672SELECT
673    '.$conf['user_fields']['id'].' AS id,
674    '.$conf['user_fields']['username'].' AS username
675  FROM '.USERS_TABLE.'
676  ORDER BY username ASC
677;';
[2245]678$template->assign(
679  array(
680    'user_options' => simple_hash_from_query($query, 'id','username'),
681    'user_options_selected' => array(@$form['user'])
682  )
683);
[1890]684
[5576]685$template->assign('display_thumbnails', $display_thumbnails);
686$template->assign('display_thumbnail_selected', $form['display_thumbnail']);
[1890]687
[1727]688// +-----------------------------------------------------------------------+
689// |                           html code display                           |
690// +-----------------------------------------------------------------------+
691
692$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
[2245]693?>
Note: See TracBrowser for help on using the repository browser.