1 rizwank 1.1 <?
2
3 /**
4 * AWStats Totals is a simple php script to view the totals
5 * (Unique visitors, Number of visits, Pages, Hits, Bandwidth)
6 * for multiple sites per month with sort options.
7 *
8 * @author Jeroen de Jong <jeroen@telartis.nl>
9 * @copyright 2004 Telartis
10 * @version 1.7
11 *
12 * @link http://www.telartis.nl/xcms/awstats
13 *
14 * Changelog:
15 * 1.0 initial version
16 * 1.1 use awstats language files to set your language
17 * 1.2 register_globals setting can be off
18 * 1.3 display yearly totals and last entry (Marco Gruber)
19 * 1.4 use english messages when no language files found
20 * 1.5 error_reporting setting can be E_ALL
21 * 1.6 fixed incorrect unique visitors in year view (ConteZero)
22 rizwank 1.1 * 1.7 changed number and byte format
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 */
38
39
40 /*******************************************************
41 * SETUP SECTION
42 *******************************************************/
43 rizwank 1.1
44 /**
45 * Set this value to the directory where AWStats
46 * saves its database and working files into.
47 */
48 $DirData = './data';
49
50 /**
51 * The URL of the AWStats script.
52 */
53 $AWStatsURL = '/awstats-6.3/wwwroot/cgi-bin/awstats.pl';
54
55 /**
56 * Set your language.
57 * Possible value:
58 * Albanian=al, Bosnian=ba, Bulgarian=bg, Catalan=ca,
59 * Chinese (Taiwan)=tw, Chinese (Simpliefied)=cn, Czech=cz, Danish=dk,
60 * Dutch=nl, English=en, Estonian=et, Euskara=eu, Finnish=fi,
61 * French=fr, Galician=gl, German=de, Greek=gr, Hebrew=he, Hungarian=hu,
62 * Icelandic=is, Indonesian=id, Italian=it, Japanese=jp, Korean=kr,
63 * Latvian=lv, Norwegian (Nynorsk)=nn, Norwegian (Bokmal)=nb, Polish=pl,
64 rizwank 1.1 * Portuguese=pt, Portuguese (Brazilian)=br, Romanian=ro, Russian=ru,
65 * Serbian=sr, Slovak=sk, Spanish=es, Swedish=se, Turkish=tr, Ukrainian=ua,
66 * Welsh=wlk.
67 */
68 $Lang = 'en';
69
70 /**
71 * Set the location of language files.
72 */
73 $DirLang = './lang';
74
75 /**
76 * Set number format.
77 */
78 $dec_point = '.';
79 $thousands_sep = ' ';
80
81 /**
82 * Config names to filter. Shows all if empty array.
83 */
84 $FilterConfigs = array();
85 rizwank 1.1
86 /*
87 To read website configs from database, do something like:
88 $sql = 'SELECT config FROM users WHERE (user=...)';
89 $rs = mysql_query($sql); if ($rs) while ($row = mysql_fetch_array($rs)) $FilterConfigs[] = $row['config'];
90 */
91
92
93 /*******************************************************
94 * PROGRAM SECTION
95 *******************************************************/
96
97 if (isset($_GET["year"])) $year = $_GET["year"]; else $year = date("Y");
98 if (isset($_GET["month"])) $month = $_GET["month"]; else $month = date("n");
99 if (isset($_GET["sort"])) $sort = $_GET["sort"]; else $sort = "bandwidth";
100
101 function get_config($file) {
102 $r = '';
103 if (preg_match('/awstats\d{6}\.(.+)\.txt/', $file, $match)) $r = $match[1];
104 return $r;
105 }
106 rizwank 1.1
107 function read_history($file) {
108 $f = fopen($file, "r");
109 $s = fread($f, filesize($file));
110 fclose($f);
111
112 $visits_total = 0; $unique_total = 0; $pages_total = 0; $hits_total = 0; $bandwidth_total = 0;
113
114 $config = get_config($file);
115
116 if (preg_match('/TotalVisits (\d+)/', $s, $match)) $visits_total = (int)$match[1];
117 if (preg_match('/TotalUnique (\d+)/', $s, $match)) $unique_total = (int)$match[1];
118
119 if (preg_match('/\nBEGIN_DAY \d+\n(.*)\nEND_DAY\n/s', $s, $match)) {
120 foreach (explode("\n", $match[1]) as $row) {
121 list($date, $pages, $hits, $bandwidth, $visits) = split(' ', $row);
122 $pages_total += $pages; $hits_total += $hits; $bandwidth_total += $bandwidth;
123 }
124 }
125 return array("config"=>$config, "visits"=>$visits_total, "unique"=>$unique_total,
126 "pages"=>$pages_total, "hits"=>$hits_total, "bandwidth"=>$bandwidth_total);
127 rizwank 1.1 }
128
129 $dirfiles = array();
130 $dir = @opendir($DirData);
131 if (!$dir) die("Could not open directory $DirData");
132 while ($file = readdir($dir)) $dirfiles[] = $file;
133
134 $files = array();
135 $config = array();
136 if ($month == 'all') $pat = '\d{2}'; else $pat = substr("0".$month, -2);
137 $pat = '/awstats'.$pat.$year.'\.(.+)\.txt/';
138 foreach ($dirfiles as $file) if (preg_match($pat, $file, $match)) {
139 $config = $match[1];
140 if (!$FilterConfigs || in_array($config, $FilterConfigs)) {
141 $configs[] = $config;
142 $files[] = $file;
143 }
144 }
145
146 $rows = array();
147 if ($files) {
148 rizwank 1.1 array_multisort($configs, $files);
149 $visits_total = 0; $unique_total = 0; $pages_total = 0; $hits_total = 0; $bandwidth_total = 0;
150 $row_prev = array();
151 for ($i = 0, $cnt = count($files); $i <= $cnt; $i++) {
152 $row = array();
153 if ($i < $cnt) {
154 $row = read_history($DirData.'/'.$files[$i]);
155 $visits_total += $row["visits"];
156 $unique_total += $row["unique"];
157 $pages_total += $row["pages"];
158 $hits_total += $row["hits"];
159 $bandwidth_total += $row["bandwidth"];
160 }
161 if ( isset($row["config"]) && isset($row_prev["config"]) &&
162 ($row["config"] == $row_prev["config"]) ) {
163
164 $row["visits"] += $row_prev["visits"];
165 $row["unique"] += $row_prev["unique"];
166 $row["pages"] += $row_prev["pages"];
167 $row["hits"] += $row_prev["hits"];
168 $row["bandwidth"] += $row_prev["bandwidth"];
169 rizwank 1.1 } elseif ($i > 0) $rows[] = $row_prev;
170 $row_prev = $row;
171 }
172 }
173
174 function multisort(&$array, $key) {
175 $cmp = create_function('$a, $b',
176 'if ($a["'.$key.'"] == $b["'.$key.'"]) return 0;'.
177 'return ($a["'.$key.'"] > $b["'.$key.'"]) ? -1 : 1;');
178 usort($array, $cmp);
179 }
180
181 if ($sort == "config") sort($rows); else multisort($rows, $sort);
182
183 function read_language_data($file) {
184 $r = array();
185 $lines = file($file);
186 foreach ($lines as $line)
187 if (preg_match('/^message(\d+)=(.*)$/', $line, $match))
188 $r[$match[1]] = $match[2];
189 return $r;
190 rizwank 1.1 }
191
192 $message = read_language_data("$DirLang/awstats-$Lang.txt");
193
194 if (!$message) {
195 $message[7] = 'Statistics for';
196 $message[10] = 'Number of visits';
197 $message[11] = 'Unique visitors';
198 $message[56] = 'Pages';
199 $message[57] = 'Hits';
200 $message[60] = 'Jan';
201 $message[61] = 'Feb';
202 $message[62] = 'Mar';
203 $message[63] = 'Apr';
204 $message[64] = 'May';
205 $message[65] = 'Jun';
206 $message[66] = 'Jul';
207 $message[67] = 'Aug';
208 $message[68] = 'Sep';
209 $message[69] = 'Oct';
210 $message[70] = 'Nov';
211 rizwank 1.1 $message[71] = 'Dec';
212 $message[75] = 'Bandwidth';
213 $message[102] = 'Total';
214 $message[115] = 'OK';
215 $message[133] = 'Reported period';
216 }
217
218 function byte_format($number, $decimals = 2) {
219 global $dec_point, $thousands_sep;
220 // kilo, mega, giga, tera, peta, exa, zetta, yotta
221 $prefix_arr = array('','k','M','G','T','P','E','Z','Y');
222 $i = 0;
223 if ($number == 0) $result = 0; else {
224 $value = round($number, $decimals);
225 while ($value > 1024) { $value /= 1024; $i++; }
226 $result = number_format($value, $decimals, $dec_point, $thousands_sep);
227 }
228 $result .= ' '.$prefix_arr[$i].'B'; if (!$i) $result .= 'ytes';
229 return $result;
230 }
231
232 rizwank 1.1 function num_format($number, $decimals = 0) {
233 global $dec_point, $thousands_sep;
234 return number_format($number, $decimals, $dec_point, $thousands_sep);
235 }
236
237
238 /*******************************************************
239 * HTML SECTION
240 *******************************************************/
241
242 ?>
243 <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
244 <html>
245 <head>
246 <title>AWStats Totals</title>
247 <style type="text/css">
248 body { font: 12px arial,verdana,helvetica,sans-serif; background-color: white }
249 td { font: 12px arial,verdana,helvetica,sans-serif; text-align: center; color: black }
250 .l { text-align: left }
251 .b { background-color: #ccccdd; padding: 2px; margin: 0 }
252 .d { background-color: white }
253 rizwank 1.1 .f { font: 14px verdana,arial,helvetica }
254 a { text-decoration: none }
255 a:hover { text-decoration: underline }
256 a.h { color: black }
257 </style>
258 </head>
259 <body>
260
261 <form action="<?=$SCRIPT_NAME?>">
262 <table class=b border=0 cellpadding=2 cellspacing=0 width="100%">
263 <tr><td class=l>
264 <table class=d border=0 cellpadding=8 cellspacing=0 width="100%">
265 <tr>
266 <th><?=$message[133]?>:</th>
267 <td class=l>
268 <?
269 echo "<select class=f name=month>\n";
270 for ($i = 1; $i <= 12; $i++) {
271 echo "<option value=$i"; if ($month == $i) echo " selected"; echo ">".$message[$i + 59]."\n";
272 }
273 echo "<option value=all"; if ($month == 'all') echo " selected"; echo ">-\n";
274 rizwank 1.1 echo "</select>\n";
275
276 echo "<select class=f name=year>\n";
277 for ($curyear = date("Y"), $i = $curyear - 4; $i <= $curyear; $i++) {
278 echo "<option value=$i"; if ($year == $i) echo " selected"; echo ">$i\n";
279 }
280 echo "</select>";
281 ?>
282 <input type=submit class=f value="<?=$message[115]?>">
283 </td></tr>
284 </table>
285 </td></tr>
286 </table>
287 </form>
288
289 <table align=center>
290 <tr>
291 <? $url = "$SCRIPT_NAME?month=$month&year=$year&sort="; ?>
292 <td bgcolor=#ECECEC class=l nowrap> <a href="<?=$url?>config" class=h><?=$message[7]?></a>
293 <td width=80 bgcolor=#FFB055><a href="<?=$url?>unique" class=h><?=$message[11]?></a>
294 <td width=80 bgcolor=#F8E880><a href="<?=$url?>visits" class=h><?=$message[10]?></a>
295 rizwank 1.1 <td width=80 bgcolor=#4477DD><a href="<?=$url?>pages" class=h><?=$message[56]?></a>
296 <td width=80 bgcolor=#66F0FF><a href="<?=$url?>hits" class=h><?=$message[57]?></a>
297 <td width=80 bgcolor=#2EA495><a href="<?=$url?>bandwidth" class=h><?=$message[75]?></a>
298 <?
299 foreach ($rows as $row)
300 echo "<tr><td class=l><a href=$AWStatsURL?month=$month&year=$year&config=".
301 $row["config"].">".$row["config"]."</a><td>".num_format($row["unique"]).
302 "<td>".num_format($row["visits"])."<td>".num_format($row["pages"]).
303 "<td>".num_format($row["hits"])."<td>".byte_format($row["bandwidth"])."\n";
304
305 echo "<tr><td class=l bgcolor=#ECECEC> Total<td bgcolor=#ECECEC>".num_format($unique_total).
306 "<td bgcolor=#ECECEC>".num_format($visits_total)."<td bgcolor=#ECECEC>".num_format($pages_total).
307 "<td bgcolor=#ECECEC>".num_format($hits_total)."<td bgcolor=#ECECEC>".byte_format($bandwidth_total)."\n";
308 ?>
309 </table>
310
311 <br><br><center><b>AWStats Totals 1.7</b> - <a
312 href="http://www.telartis.nl/xcms/awstats">© 2004 Telartis</a></center><br><br>
313
314 </body>
315 </html>
|