source: trunk/admin/stats.php @ 52

Last change on this file since 52 was 52, checked in by z0rglub, 21 years ago

Correcting possible division by 0

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1<?php
2/***************************************************************************
3 *                                 stats.php                               *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************/
9
10/***************************************************************************
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 ***************************************************************************/
17include_once( './include/isadmin.inc.php' );
18$max_pixels = 500;
19//------------------------------------------------------------ comment deletion
20if ( isset( $_GET['del'] ) and is_numeric( $_GET['del'] ) )
21{
22  $query = 'DELETE FROM '.PREFIX_TABLE.'comments';
23  $query.= ' WHERE id = '.$_GET['del'];
24  $query.= ';';
25  mysql_query( $query );
26}
27//--------------------------------------------------------- history table empty
28if ( isset( $_GET['act'] ) and $_GET['act'] == 'empty' )
29{
30  $query = 'DELETE FROM '.PREFIX_TABLE.'history';
31  $query.= ';';
32  mysql_query( $query );
33}
34//----------------------------------------------------- template initialization
35$sub = $vtp->Open( '../template/'.$user['template'].'/admin/stats.vtp' );
36$tpl = array( 'stats_last_days','date','login',
37              'IP','file','picture','category','stats_pages_seen',
38              'stats_visitors','stats_empty', 'stats_pages_seen_graph_title',
39              'stats_visitors_graph_title');
40templatize_array( $tpl, 'lang', $sub );
41$vtp->setGlobalVar( $sub, 'user_template', $user['template'] );
42//--------------------------------------------------- number of days to display
43if ( isset( $_GET['last_days'] ) ) define( MAX_DAYS, $_GET['last_days'] );
44else                               define( MAX_DAYS, 0 );
45
46foreach ( $conf['last_days'] as $option ) {
47  $vtp->addSession( $sub, 'last_day_option' );
48  $vtp->setVar( $sub, 'last_day_option.option', $option );
49  $url = './admin.php?page=stats&amp;expand='.$_GET['expand'];
50  $url.= '&amp;last_days='.($option - 1);
51  $vtp->setVar( $sub, 'last_day_option.link', add_session_id( $url ) );
52  if ( $option == MAX_DAYS + 1 )
53  {
54    $vtp->setVar( $sub, 'last_day_option.style', 'font-weight:bold;');
55  }
56  $vtp->closeSession( $sub, 'last_day_option' );
57}
58//---------------------------------------------------------------- log  history
59// empty link
60$url = './admin.php?page=stats&amp;last_days='.$_GET['last_days'];
61$url.= '&amp;expand='.$_GET['expand'];
62$url.= '&amp;act=empty';
63$vtp->setVar( $sub, 'emply_url', add_session_id( $url ) );
64// expand array management
65$expand_days = explode( ',', $_GET['expand'] );
66$page['expand_days'] = array();
67foreach ( $expand_days as $expand_day ) {
68  if ( is_numeric( $expand_day ) )
69  {
70    array_push( $page['expand_days'], $expand_day );
71  }
72}
73
74$days = array();
75$max_nb_visitors = 0;
76$max_pages_seen = 0;
77
78$starttime = mktime(  0, 0, 0,date('n'),date('j'),date('Y') );
79$endtime   = mktime( 23,59,59,date('n'),date('j'),date('Y') );
80for ( $i = 0; $i <= MAX_DAYS; $i++ )
81{
82  $day = array();
83  $vtp->addSession( $sub, 'day' );
84  // link to open the day to see details
85  $local_expand = $page['expand_days'];
86  if ( in_array( $i, $page['expand_days'] ) )
87  {
88    $vtp->addSession( $sub, 'expanded' );
89    $vtp->closeSession( $sub, 'expanded' );
90    $local_expand = array_remove( $local_expand, $i );
91  }
92  else
93  {
94    $vtp->addSession( $sub, 'collapsed' );
95    $vtp->closeSession( $sub, 'collapsed' );
96    array_push( $local_expand, $i );
97  }
98  $url = './admin.php?page=stats&amp;last_days='.$_GET['last_days'];
99  $url.= '&amp;expand='.implode( ',', $local_expand );
100  $vtp->setVar( $sub, 'day.url', add_session_id( $url ) );
101  // date displayed like this (in English ) :
102  //                     Sunday 15 June 2003
103  $date = $lang['day'][date( 'w', $starttime )];   // Sunday
104  $date.= date( ' j ', $starttime );               // 15
105  $date.= $lang['month'][date( 'n', $starttime )]; // June
106  $date.= date( ' Y', $starttime );                // 2003
107  $day['date'] = $date;
108  $vtp->setVar( $sub, 'day.name', $date );
109  // number of visitors for this day
110  $query = 'SELECT DISTINCT(IP) as nb_visitors';
111  $query.= ' FROM '.PREFIX_TABLE.'history';
112  $query.= ' WHERE date > '.$starttime;
113  $query.= ' AND date < '.$endtime;
114  $query.= ';';
115  $result = mysql_query( $query );
116  $nb_visitors = mysql_num_rows( $result );
117  $day['nb_visitors'] = $nb_visitors;
118  if ( $nb_visitors > $max_nb_visitors ) $max_nb_visitors = $nb_visitors;
119  $vtp->setVar( $sub, 'day.nb_visitors', $nb_visitors );
120  // log lines for this day
121  $query = 'SELECT date,login,IP,category,file,picture';
122  $query.= ' FROM '.PREFIX_TABLE.'history';
123  $query.= ' WHERE date > '.$starttime;
124  $query.= ' AND date < '.$endtime;
125  $query.= ' ORDER BY date ASC';
126  $query.= ';';
127  $result = mysql_query( $query );
128  $nb_pages_seen = mysql_num_rows( $result );
129  $day['nb_pages_seen'] = $nb_pages_seen;
130  if ( $nb_pages_seen > $max_pages_seen ) $max_pages_seen = $nb_pages_seen;
131  $vtp->setVar( $sub, 'day.nb_pages', $nb_pages_seen );
132  if ( in_array( $i, $page['expand_days'] ) )
133  {
134    while ( $row = mysql_fetch_array( $result ) )
135    {
136      $vtp->addSession( $sub, 'line' );
137      $vtp->setVar( $sub, 'line.date', date( 'G:i', $row['date'] ) );
138      $vtp->setVar( $sub, 'line.login', $row['login'] );
139      $vtp->setVar( $sub, 'line.IP', $row['IP'] );
140      $vtp->setVar( $sub, 'line.category', $row['category'] );
141      $vtp->setVar( $sub, 'line.file', $row['file'] );
142      $vtp->setVar( $sub, 'line.picture', $row['picture'] );
143      $vtp->closeSession( $sub, 'line' );
144    }
145  }
146  $starttime-= 24*60*60;
147  $endtime  -= 24*60*60;
148  $vtp->closeSession( $sub, 'day' );
149  array_push( $days, $day );
150}
151//------------------------------------------------------------ pages seen graph
152foreach ( $days as $day ) {
153  $vtp->addSession( $sub, 'pages_day' );
154  if ( $max_pages_seen > 0 )
155    $width = floor( ( $day['nb_pages_seen']*$max_pixels ) / $max_pages_seen );
156  else $width = 0;
157  $vtp->setVar( $sub, 'pages_day.date', $day['date'] );
158  $vtp->setVar( $sub, 'pages_day.width', $width );
159  $vtp->setVar( $sub, 'pages_day.nb_pages', $day['nb_pages_seen'] );
160  $vtp->closeSession( $sub, 'pages_day' );
161}
162//-------------------------------------------------------------- visitors grpah
163foreach ( $days as $day ) {
164  $vtp->addSession( $sub, 'visitors_day' );
165  if ( $max_nb_visitors > 0 )
166    $width = floor( ( $day['nb_visitors'] * $max_pixels ) / $max_nb_visitors );
167  else $width = 0;
168  $vtp->setVar( $sub, 'visitors_day.date', $day['date'] );
169  $vtp->setVar( $sub, 'visitors_day.width', $width );
170  $vtp->setVar( $sub, 'visitors_day.nb_visitors', $day['nb_visitors'] );
171  $vtp->closeSession( $sub, 'visitors_day' );
172}
173//----------------------------------------------------------- sending html code
174$vtp->Parse( $handle , 'sub', $sub );
175?>
Note: See TracBrowser for help on using the repository browser.