source: trunk/admin/history.php @ 1844

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

New: non picture files are now logged in history when downloaded. The
history filter was redesigned: #history.is_high replaced by
#history.image_type = high. The filter is simpler.

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