source: trunk/admin/history.php @ 1883

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

New: #images.high_filesize was added so that we can sum the filesizes in the
filtered history. #images.high_filesize is filled during metadata
synchronization.

Bug fixed: in getAttribute XML function, when asking "filesize", it was
returning high_filesize. The regex was too simple.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 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 1883 2007-03-09 16:28:49Z plg $
9// | last update   : $Date: 2007-03-09 16:28:49 +0000 (Fri, 09 Mar 2007) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1883 $
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$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  // echo '<pre>'; print_r($search); echo '</pre>';
103 
104  if (!empty($search))
105  {
106    // register search rules in database, then they will be available on
107    // thumbnails page and picture page.
108    $query ='
109INSERT INTO '.SEARCH_TABLE.'
110  (rules)
111  VALUES
112  (\''.serialize($search).'\')
113;';
114    pwg_query($query);
115
116    $search_id = mysql_insert_id();
117   
118    redirect(
119      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
120      );
121  }
122  else
123  {
124    array_push($errors, $lang['search_one_clause_at_least']);
125  }
126}
127
128// +-----------------------------------------------------------------------+
129// |                             template init                             |
130// +-----------------------------------------------------------------------+
131
132$template->set_filename('history', 'admin/history.tpl');
133
134// TabSheet initialization
135history_tabsheet();
136
137$base_url = PHPWG_ROOT_PATH.'admin.php?page=history';
138
139$template->assign_vars(
140  array(
141    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=history',
142
143    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=history'
144    )
145  );
146
147$template->assign_vars(
148  array(
149    'TODAY_DAY'   => date('d', time()),
150    'TODAY_MONTH' => date('m', time()),
151    'TODAY_YEAR'  => date('Y', time()),
152    )
153  );
154
155// +-----------------------------------------------------------------------+
156// |                             history lines                             |
157// +-----------------------------------------------------------------------+
158
159if (isset($_GET['search_id'])
160    and $page['search_id'] = (int)$_GET['search_id'])
161{
162  // what are the lines to display in reality ?
163  $query = '
164SELECT rules
165  FROM '.SEARCH_TABLE.'
166  WHERE id = '.$page['search_id'].'
167;';
168  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
169
170  $page['search'] = unserialize($serialized_rules);
171
172  // echo '<pre>'; print_r($page['search']); echo '</pre>';
173 
174  $clauses = array();
175
176  if (isset($page['search']['fields']['date-after']))
177  {
178    array_push(
179      $clauses,
180      "date >= '".$page['search']['fields']['date-after']."'"
181      );
182  }
183
184  if (isset($page['search']['fields']['date-before']))
185  {
186    array_push(
187      $clauses,
188      "date <= '".$page['search']['fields']['date-before']."'"
189      );
190  }
191
192  if (isset($page['search']['fields']['types']))
193  {
194    $local_clauses = array();
195   
196    foreach ($types as $type) {
197      if (in_array($type, $page['search']['fields']['types'])) {
198        $clause = 'image_type ';
199        if ($type == 'none')
200        {
201          $clause.= 'IS NULL';
202        }
203        else
204        {
205          $clause.= "= '".$type."'";
206        }
207       
208        array_push($local_clauses, $clause);
209      }
210    }
211   
212    if (count($local_clauses) > 0)
213    {
214      array_push(
215        $clauses,
216        implode(' OR ', $local_clauses)
217        );
218    }
219  }
220 
221  $clauses = prepend_append_array_items($clauses, '(', ')');
222
223  $where_separator =
224    implode(
225      "\n    AND ",
226      $clauses
227      );
228 
229  $query = '
230SELECT
231    date,
232    time,
233    user_id,
234    IP,
235    section,
236    category_id,
237    tag_ids,
238    image_id,
239    image_type
240  FROM '.HISTORY_TABLE.'
241  WHERE '.$where_separator.'
242;';
243
244  // LIMIT '.$page['start'].', '.$conf['nb_logs_page'].'
245
246  $result = pwg_query($query);
247
248  $page['nb_lines'] = mysql_num_rows($result);
249 
250  $history_lines = array();
251  $user_ids = array();
252  $category_ids = array();
253  $image_ids = array();
254  $tag_ids = array();
255 
256  while ($row = mysql_fetch_assoc($result))
257  {
258    $user_ids[$row['user_id']] = 1;
259
260    if (isset($row['category_id']))
261    {
262      $category_ids[$row['category_id']] = 1;
263    }
264
265    if (isset($row['image_id']))
266    {
267      $image_ids[$row['image_id']] = 1;
268    }
269
270    array_push(
271      $history_lines,
272      $row
273      );
274  }
275
276  // prepare reference data (users, tags, categories...)
277  if (count($user_ids) > 0)
278  {
279    $query = '
280SELECT '.$conf['user_fields']['id'].' AS id
281     , '.$conf['user_fields']['username'].' AS username
282  FROM '.USERS_TABLE.'
283  WHERE id IN ('.implode(',', array_keys($user_ids)).')
284;';
285    $result = pwg_query($query);
286
287    $username_of = array();
288    while ($row = mysql_fetch_array($result))
289    {
290      $username_of[$row['id']] = $row['username'];
291    }
292  }
293
294  if (count($category_ids) > 0)
295  {
296    $query = '
297SELECT id, uppercats
298  FROM '.CATEGORIES_TABLE.'
299  WHERE id IN ('.implode(',', array_keys($category_ids)).')
300;';
301    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
302
303    $name_of_category = array();
304   
305    foreach ($uppercats_of as $category_id => $uppercats)
306    {
307      $name_of_category[$category_id] = get_cat_display_name_cache(
308        $uppercats
309        );
310    }
311  }
312
313  if (count($image_ids) > 0)
314  {
315    $query = '
316SELECT
317    id,
318    IF(name IS NULL, file, name) AS label,
319    filesize,
320    high_filesize
321  FROM '.IMAGES_TABLE.'
322  WHERE id IN ('.implode(',', array_keys($image_ids)).')
323;';
324    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
325    $label_of_image = array();
326    $filesize_of_image = array();
327    $high_filesize_of_image = array();
328   
329    $result = pwg_query($query);
330    while ($row = mysql_fetch_array($result))
331    {
332      $label_of_image[ $row['id'] ] = $row['label'];
333
334      if (isset($row['filesize']))
335      {
336        $filesize_of_image[ $row['id'] ] = $row['filesize'];
337      }
338
339      if (isset($row['high_filesize']))
340      {
341        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
342      }
343    }
344
345    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
346  }
347 
348  $i = 0;
349  $first_line = $page['start'] + 1;
350  $last_line = $page['start'] + $conf['nb_logs_page'];
351
352  $total_filesize = 0;
353
354  foreach ($history_lines as $line)
355  {
356    if (isset($line['image_type']))
357    {
358      if ($line['image_type'] == 'high')
359      {
360        if (isset($high_filesize_of_image[$line['image_id']]))
361        {
362          $total_filesize+= $high_filesize_of_image[$line['image_id']];
363        }
364      }
365      else
366      {
367        if (isset($filesize_of_image[$line['image_id']]))
368        {
369          $total_filesize+= $filesize_of_image[$line['image_id']];
370        }
371      }
372    }
373   
374    $i++;
375   
376    if ($i < $first_line or $i > $last_line)
377    {
378      continue;
379    }
380   
381    $template->assign_block_vars(
382      'detail',
383      array(
384        'DATE'      => $line['date'],
385        'TIME'      => $line['time'],
386        'USER'      => isset($username_of[$line['user_id']])
387          ? $username_of[$line['user_id']]
388          : $line['user_id']
389        ,
390        'IP'        => $line['IP'],
391        'IMAGE'     => isset($line['image_id'])
392          ? ( isset($label_of_image[$line['image_id']])
393                ? sprintf(
394                    '(%u) %s',
395                    $line['image_id'],
396                    $label_of_image[$line['image_id']]
397                  )
398                : sprintf(
399                    '(%u) deleted ',
400                    $line['image_id']
401                  )
402            )
403          : '',
404        'TYPE'      => $line['image_type'],
405        'SECTION'   => $line['section'],
406        'CATEGORY'  => isset($line['category_id'])
407          ? ( isset($name_of_category[$line['category_id']])
408                ? $name_of_category[$line['category_id']]
409                : 'deleted '.$line['category_id'] )
410          : '',
411        'TAGS'       => $line['tag_ids'],
412        'T_CLASS'   => ($i % 2) ? 'row1' : 'row2',
413        )
414      );
415  }
416
417  $template->assign_block_vars(
418    'summary',
419    array(
420      'FILESIZE' => $total_filesize.' KB',
421      )
422    );
423}
424
425// $groups_string = preg_replace(
426//     '/(\d+)/e',
427//     "\$groups['$1']",
428//     implode(
429//       ', ',
430//       $local_user['groups']
431//       )
432//     );
433
434// +-----------------------------------------------------------------------+
435// |                            navigation bar                             |
436// +-----------------------------------------------------------------------+
437
438if (isset($page['search_id']))
439{
440  $navbar = create_navigation_bar(
441    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
442    $page['nb_lines'],
443    $page['start'],
444    $conf['nb_logs_page']
445    );
446
447  $template->assign_block_vars(
448    'navigation',
449    array(
450      'NAVBAR' => $navbar
451      )
452    );
453}
454
455// +-----------------------------------------------------------------------+
456// |                             filter form                               |
457// +-----------------------------------------------------------------------+
458
459$form = array();
460
461if (isset($page['search']))
462{
463  if (isset($page['search']['fields']['date-after']))
464  {
465    $tokens = explode('-', $page['search']['fields']['date-after']);
466   
467    $form['start_year']  = (int)$tokens[0];
468    $form['start_month'] = (int)$tokens[1];
469    $form['start_day']   = (int)$tokens[2];
470  }
471
472  if (isset($page['search']['fields']['date-before']))
473  {
474    $tokens = explode('-', $page['search']['fields']['date-before']);
475
476    $form['end_year']  = (int)$tokens[0];
477    $form['end_month'] = (int)$tokens[1];
478    $form['end_day']   = (int)$tokens[2];
479  }
480
481  $form['types'] = $page['search']['fields']['types'];
482}
483else
484{
485  // by default, at page load, we want the selected date to be the current
486  // date
487  $form['start_year']  = $form['end_year']  = date('Y');
488  $form['start_month'] = $form['end_month'] = date('n');
489  $form['start_day']   = $form['end_day']   = date('j');
490  $form['types'] = $types;
491}
492
493// start date
494get_day_list('start_day', @$form['start_day']);
495get_month_list('start_month', @$form['start_month']);
496// end date
497get_day_list('end_day', @$form['end_day']);
498get_month_list('end_month', @$form['end_month']);
499
500$template->assign_vars(
501  array(
502    'START_YEAR' => @$form['start_year'],
503    'END_YEAR'   => @$form['end_year'],
504    )
505  );
506
507foreach ($types as $option)
508{
509  $selected = '';
510 
511  if (in_array($option, $form['types']))
512  {
513    $selected = 'selected="selected"';
514  }
515 
516  $template->assign_block_vars(
517    'types_option',
518    array(
519      'VALUE' => $option,
520      'CONTENT' => l10n($option),
521      'SELECTED' => $selected,
522      )
523    );
524}
525 
526// +-----------------------------------------------------------------------+
527// |                           html code display                           |
528// +-----------------------------------------------------------------------+
529
530$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
531?>
Note: See TracBrowser for help on using the repository browser.