source: trunk/admin/history.php @ 29060

Last change on this file since 29060 was 28587, checked in by mistic100, 10 years ago

feature 3010 : replace trigger_action/event by trigger_notify/change

  • Property svn:eol-style set to LF
File size: 17.1 KB
RevLine 
[1727]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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
[28497]76  if (!empty($_POST['start']))
[1727]77  {
[28497]78    $search['fields']['date-after'] = $_POST['start'];
[1727]79  }
80
[28497]81  if (!empty($_POST['end']))
[1727]82  {
[28497]83    $search['fields']['date-before'] = $_POST['end'];
[1727]84  }
85
[2157]86  if (empty($_POST['types']))
87  {
88    $search['fields']['types'] = $types;
89  }
90  else
91  {
92    $search['fields']['types'] = $_POST['types'];
93  }
[1890]94
95  $search['fields']['user'] = $_POST['user'];
[1892]96
97  if (!empty($_POST['image_id']))
98  {
99    $search['fields']['image_id'] = intval($_POST['image_id']);
100  }
[2245]101
[1892]102  if (!empty($_POST['filename']))
103  {
104    $search['fields']['filename'] = str_replace(
105      '*',
106      '%',
[4325]107      pwg_db_real_escape_string($_POST['filename'])
[1892]108      );
109  }
110
[14688]111  if (!empty($_POST['ip']))
112  {
113    $search['fields']['ip'] = str_replace(
114      '*',
115      '%',
116      pwg_db_real_escape_string($_POST['ip'])
117      );
118  }
119
[1992]120  $search['fields']['display_thumbnail'] = $_POST['display_thumbnail'];
121  // Display choise are also save to one cookie
[5576]122  if (!empty($_POST['display_thumbnail'])
123      and isset($display_thumbnails[$_POST['display_thumbnail']]))
124  {
125    $cookie_val = $_POST['display_thumbnail'];
126  }
127  else
128  {
129    $cookie_val = null;
130  }
[14688]131
[5576]132  pwg_set_cookie_var('display_thumbnail', $cookie_val, strtotime('+1 month') );
[1992]133
[1892]134  // TODO manage inconsistency of having $_POST['image_id'] and
135  // $_POST['filename'] simultaneously
[2245]136
[1727]137  if (!empty($search))
138  {
139    // register search rules in database, then they will be available on
140    // thumbnails page and picture page.
141    $query ='
142INSERT INTO '.SEARCH_TABLE.'
143  (rules)
144  VALUES
145  (\''.serialize($search).'\')
146;';
147    pwg_query($query);
148
[4892]149    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[2245]150
[1727]151    redirect(
152      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
153      );
154  }
155  else
156  {
[25018]157    $page['errors'][] = l10n('Empty query. No criteria has been entered.');
[1727]158  }
159}
160
161// +-----------------------------------------------------------------------+
162// |                             template init                             |
163// +-----------------------------------------------------------------------+
164
[2530]165$template->set_filename('history', 'history.tpl');
[1727]166
[1881]167// TabSheet initialization
168history_tabsheet();
169
[2245]170$template->assign(
[1727]171  array(
[5920]172    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
[2245]173    'F_ACTION' => get_root_url().'admin.php?page=history'
[1727]174    )
175  );
176
177// +-----------------------------------------------------------------------+
178// |                             history lines                             |
179// +-----------------------------------------------------------------------+
180
181if (isset($_GET['search_id'])
182    and $page['search_id'] = (int)$_GET['search_id'])
183{
184  // what are the lines to display in reality ?
185  $query = '
186SELECT rules
187  FROM '.SEARCH_TABLE.'
188  WHERE id = '.$page['search_id'].'
189;';
[4325]190  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
[1727]191
192  $page['search'] = unserialize($serialized_rules);
193
[1890]194  if (isset($_GET['user_id']))
195  {
196    if (!is_numeric($_GET['user_id']))
197    {
198      die('user_id GET parameter must be an integer value');
199    }
200
201    $page['search']['fields']['user'] = $_GET['user_id'];
[2157]202
[1890]203    $query ='
204INSERT INTO '.SEARCH_TABLE.'
205  (rules)
206  VALUES
207  (\''.serialize($page['search']).'\')
208;';
209    pwg_query($query);
210
[4892]211    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[2245]212
[1890]213    redirect(
214      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
215      );
216  }
217
[27572]218  /*TODO - no need to get a huge number of rows from db (should take only what needed for display + SQL_CALC_FOUND_ROWS*/
[28587]219  $data = trigger_change('get_history', array(), $page['search'], $types);
[2157]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();
[14688]229  $has_tags = false;
[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    {
[14688]247      $has_tags = true;
[1891]248    }
249
[14688]250    $history_lines[] = $row;
[1727]251  }
252
253  // prepare reference data (users, tags, categories...)
254  if (count($user_ids) > 0)
255  {
256    $query = '
257SELECT '.$conf['user_fields']['id'].' AS id
258     , '.$conf['user_fields']['username'].' AS username
259  FROM '.USERS_TABLE.'
260  WHERE id IN ('.implode(',', array_keys($user_ids)).')
261;';
262    $result = pwg_query($query);
263
264    $username_of = array();
[4325]265    while ($row = pwg_db_fetch_assoc($result))
[1727]266    {
[4304]267      $username_of[$row['id']] = stripslashes($row['username']);
[1727]268    }
269  }
270
271  if (count($category_ids) > 0)
272  {
273    $query = '
274SELECT id, uppercats
275  FROM '.CATEGORIES_TABLE.'
276  WHERE id IN ('.implode(',', array_keys($category_ids)).')
277;';
[27572]278    $uppercats_of = query2array($query, 'id', 'uppercats');
[1727]279
280    $name_of_category = array();
[2245]281
[1727]282    foreach ($uppercats_of as $category_id => $uppercats)
283    {
284      $name_of_category[$category_id] = get_cat_display_name_cache(
285        $uppercats
286        );
287    }
288  }
289
290  if (count($image_ids) > 0)
291  {
292    $query = '
[1883]293SELECT
294    id,
295    IF(name IS NULL, file, name) AS label,
296    filesize,
[1991]297    file,
298    path,
[12796]299    representative_ext
[1727]300  FROM '.IMAGES_TABLE.'
301  WHERE id IN ('.implode(',', array_keys($image_ids)).')
302;';
[27572]303    $image_infos = query2array($query, 'id');
[1727]304  }
[1891]305
[14688]306  if ($has_tags > 0)
[1891]307  {
308    $query = '
309SELECT
310    id,
[23151]311    name, url_name
[14688]312  FROM '.TAGS_TABLE;
[26972]313
314    global $name_of_tag; // used for preg_replace
[23151]315    $name_of_tag = array();
316    $result = pwg_query($query);
317    while ($row=pwg_db_fetch_assoc($result))
318    {
[28587]319      $name_of_tag[ $row['id'] ] = '<a href="'.make_index_url( array('tags'=>array($row))).'">'.trigger_change("render_tag_name", $row['name'], $row).'</a>';
[23151]320    }
[1891]321  }
[2245]322
[1727]323  $i = 0;
[1883]324  $first_line = $page['start'] + 1;
325  $last_line = $page['start'] + $conf['nb_logs_page'];
[1727]326
[1893]327  $summary['total_filesize'] = 0;
328  $summary['guests_IP'] = array();
[1883]329
[1727]330  foreach ($history_lines as $line)
331  {
[16282]332    if (isset($line['image_type']) and $line['image_type'] == 'high')
[1883]333    {
[27572]334      $summary['total_filesize'] += @intval($image_infos[$line['image_id']]['filesize']);
[1883]335    }
[1893]336
337    if ($line['user_id'] == $conf['guest_id'])
338    {
339      if (!isset($summary['guests_IP'][ $line['IP'] ]))
340      {
341        $summary['guests_IP'][ $line['IP'] ] = 0;
342      }
[2245]343
[1893]344      $summary['guests_IP'][ $line['IP'] ]++;
345    }
[2245]346
[1883]347    $i++;
[2245]348
[1883]349    if ($i < $first_line or $i > $last_line)
350    {
351      continue;
352    }
[1890]353
354    $user_string = '';
355    if (isset($username_of[$line['user_id']]))
356    {
357      $user_string.= $username_of[$line['user_id']];
358    }
359    else
360    {
361      $user_string.= $line['user_id'];
362    }
363    $user_string.= '&nbsp;<a href="';
364    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
365    $user_string.= '&amp;search_id='.$page['search_id'];
366    $user_string.= '&amp;user_id='.$line['user_id'];
367    $user_string.= '">+</a>';
[1891]368
369    $tags_string = '';
370    if (isset($line['tag_ids']))
371    {
[26972]372      $tags_string = preg_replace_callback(
373        '/(\d+)/',
[27692]374        create_function('$m', 'global $name_of_tag; return isset($name_of_tag[$m[1]]) ? $name_of_tag[$m[1]] : $m[1];'),
[1891]375        str_replace(
376          ',',
377          ', ',
378          $line['tag_ids']
379          )
380        );
381    }
[1896]382
383    $image_string = '';
384    if (isset($line['image_id']))
385    {
386      $picture_url = make_picture_url(
387        array(
388          'image_id' => $line['image_id'],
389          )
390        );
[1992]391
[27572]392      if (isset($image_infos[$line['image_id']]))
[9808]393      {
394        $element = array(
395          'id' => $line['image_id'],
[27572]396          'file' => $image_infos[$line['image_id']]['file'],
397          'path' => $image_infos[$line['image_id']]['path'],
398          'representative_ext' => $image_infos[$line['image_id']]['representative_ext'],
[9808]399          );
400        $thumbnail_display = $page['search']['fields']['display_thumbnail'];
401      }
402      else
403      {
404        $thumbnail_display = 'no_display_thumbnail';
405      }
[1992]406
407      $image_title = '('.$line['image_id'].')';
408
[27572]409      if (isset($image_infos[$line['image_id']]['label']))
[1896]410      {
[28587]411        $image_title.= ' '.trigger_change('render_element_description', $image_infos[$line['image_id']]['label']);
[1896]412      }
413      else
414      {
[1992]415        $image_title.= ' unknown filename';
[1896]416      }
[1992]417
418      $image_string = '';
419
[9808]420      switch ($thumbnail_display)
[1992]421      {
422        case 'no_display_thumbnail':
423        {
424          $image_string= '<a href="'.$picture_url.'">'.$image_title.'</a>';
425          break;
426        }
427        case 'display_thumbnail_classic':
428        {
429          $image_string =
430            '<a class="thumbnail" href="'.$picture_url.'">'
[12796]431            .'<span><img src="'.DerivativeImage::thumb_url($element)
[1992]432            .'" alt="'.$image_title.'" title="'.$image_title.'">'
433            .'</span></a>';
434          break;
435        }
436        case 'display_thumbnail_hoverbox':
437        {
438          $image_string =
439            '<a class="over" href="'.$picture_url.'">'
[12796]440            .'<span><img src="'.DerivativeImage::thumb_url($element)
[1992]441            .'" alt="'.$image_title.'" title="'.$image_title.'">'
442            .'</span>'.$image_title.'</a>';
443          break;
444        }
445      }
[1896]446    }
[2245]447
448    $template->append(
449      'search_results',
[1727]450      array(
451        'DATE'      => $line['date'],
452        'TIME'      => $line['time'],
[1890]453        'USER'      => $user_string,
[1727]454        'IP'        => $line['IP'],
[1896]455        'IMAGE'     => $image_string,
[1844]456        'TYPE'      => $line['image_type'],
[1727]457        'SECTION'   => $line['section'],
458        'CATEGORY'  => isset($line['category_id'])
[1821]459          ? ( isset($name_of_category[$line['category_id']])
460                ? $name_of_category[$line['category_id']]
461                : 'deleted '.$line['category_id'] )
[1727]462          : '',
[1891]463        'TAGS'       => $tags_string,
[1727]464        )
465      );
466  }
[1883]467
[1893]468  $summary['nb_guests'] = 0;
469  if (count(array_keys($summary['guests_IP'])) > 0)
470  {
471    $summary['nb_guests'] = count(array_keys($summary['guests_IP']));
472
473    // we delete the "guest" from the $username_of hash so that it is
474    // avoided in next steps
475    unset($username_of[ $conf['guest_id'] ]);
476  }
[2245]477
[1893]478  $summary['nb_members'] = count($username_of);
479
480  $member_strings = array();
481  foreach ($username_of as $user_id => $user_name)
482  {
483    $member_string = $user_name.'&nbsp;<a href="';
[2245]484    $member_string.= get_root_url().'admin.php?page=history';
[1893]485    $member_string.= '&amp;search_id='.$page['search_id'];
486    $member_string.= '&amp;user_id='.$user_id;
487    $member_string.= '">+</a>';
488
489    $member_strings[] = $member_string;
490  }
[2245]491
492  $template->assign(
493    'search_summary',
[1883]494    array(
[1932]495      'NB_LINES' => l10n_dec(
496        '%d line filtered', '%d lines filtered',
[1893]497        $page['nb_lines']
498        ),
[16282]499      'FILESIZE' => $summary['total_filesize'] != 0 ? ceil($summary['total_filesize']/1024).' MB' : '',
[1932]500      'USERS' => l10n_dec(
501        '%d user', '%d users',
[1893]502        $summary['nb_members'] + $summary['nb_guests']
503        ),
504      'MEMBERS' => sprintf(
[1932]505        l10n_dec('%d member', '%d members', $summary['nb_members']).': %s',
[25005]506        implode(', ', $member_strings)
[1893]507        ),
[1932]508      'GUESTS' => l10n_dec(
509        '%d guest', '%d guests',
[1893]510        $summary['nb_guests']
511        ),
[1883]512      )
513    );
[26972]514
515  unset($name_of_tag);
[1727]516}
517
518// +-----------------------------------------------------------------------+
519// |                            navigation bar                             |
520// +-----------------------------------------------------------------------+
521
522if (isset($page['search_id']))
523{
524  $navbar = create_navigation_bar(
[2245]525    get_root_url().'admin.php'.get_query_string_diff(array('start')),
[1727]526    $page['nb_lines'],
527    $page['start'],
528    $conf['nb_logs_page']
529    );
530
[3172]531  $template->assign('navbar', $navbar);
[1727]532}
533
534// +-----------------------------------------------------------------------+
535// |                             filter form                               |
536// +-----------------------------------------------------------------------+
537
538$form = array();
539
540if (isset($page['search']))
541{
542  if (isset($page['search']['fields']['date-after']))
543  {
[28497]544    $form['start'] = $page['search']['fields']['date-after'];
[1727]545  }
546
547  if (isset($page['search']['fields']['date-before']))
548  {
[28497]549    $form['end'] = $page['search']['fields']['date-before'];
[1727]550  }
[1817]551
[1844]552  $form['types'] = $page['search']['fields']['types'];
[1890]553
554  if (isset($page['search']['fields']['user']))
555  {
556    $form['user'] = $page['search']['fields']['user'];
557  }
558  else
559  {
560    $form['user'] = null;
561  }
[1892]562
563  $form['image_id'] = @$page['search']['fields']['image_id'];
564  $form['filename'] = @$page['search']['fields']['filename'];
[14688]565  $form['ip'] = @$page['search']['fields']['ip'];
[1992]566
567  $form['display_thumbnail'] = @$page['search']['fields']['display_thumbnail'];
[1727]568}
569else
570{
571  // by default, at page load, we want the selected date to be the current
572  // date
[28500]573  $form['start'] = $form['end'] = date('Y-m-d');
[1844]574  $form['types'] = $types;
[1992]575  // Hoverbox by default
576  $form['display_thumbnail'] =
[5576]577    pwg_get_cookie_var('display_thumbnail', 'no_display_thumbnail');
[1727]578}
579
580
[2245]581$template->assign(
[1727]582  array(
[2245]583    'IMAGE_ID' => @$form['image_id'],
584    'FILENAME' => @$form['filename'],
[14688]585    'IP' => @$form['ip'],
[28497]586    'START' => @$form['start'],
587    'END' => @$form['end'],
[1727]588    )
589  );
[1817]590
[2245]591$template->assign(
[1844]592    array(
[2245]593      'type_option_values' => $types,
594      'type_option_selected' => $form['types']
[1890]595    )
596  );
597
[2245]598
[1890]599$query = '
600SELECT
601    '.$conf['user_fields']['id'].' AS id,
602    '.$conf['user_fields']['username'].' AS username
603  FROM '.USERS_TABLE.'
604  ORDER BY username ASC
605;';
[2245]606$template->assign(
607  array(
[27572]608    'user_options' => query2array($query, 'id','username'),
[2245]609    'user_options_selected' => array(@$form['user'])
610  )
611);
[1890]612
[5576]613$template->assign('display_thumbnails', $display_thumbnails);
614$template->assign('display_thumbnail_selected', $form['display_thumbnail']);
[1890]615
[1727]616// +-----------------------------------------------------------------------+
617// |                           html code display                           |
618// +-----------------------------------------------------------------------+
619
620$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
[2245]621?>
Note: See TracBrowser for help on using the repository browser.