-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzipstat.php
More file actions
433 lines (375 loc) · 13.3 KB
/
zipstat.php
File metadata and controls
433 lines (375 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
//The following uncommented code can be used for profiling.
#class TimeLogger
#{
# var $lastTime;
#
# var $usr;
#
# var $start;
#
# function __construct($usr)
# {
# list($usec, $sec) = explode(" ",microtime());
# $this->usr = $usr."[".($sec + $usec)."]";
# $this->lastTime = $sec + $usec;
# $this->start = $sec + $usec;
# }
#
# function timeLog($msg)
# {
# list($usec, $sec) = explode(" ",microtime());
# $thisTime = $sec + $usec;
# $out = $this->usr." ".($thisTime - $this->lastTime)." ".$msg."\n";
# $this->lastTime = $thisTime;
# $fp = fopen("timelog2.txt","a");
# fwrite($fp, $out);
# fclose($fp);
# }
#
# function theend()
# {
# list($usec, $sec) = explode(" ",microtime());
# $thisTime = $sec + $usec;
# $out = $this->usr." ".($thisTime - $this->start)." ----- End. ------\n";
# $fp = fopen("timelog3.txt","a");
# fwrite($fp, $out);
# fclose($fp);
# }
#}
//$tl = new TimeLogger("x");
//$tl->timeLog("1");
#list($usec, $sec) = explode(" ",microtime());
#$startTime = $sec + $usec;
//Now fire up ZIP Stat!
require_once(dirname(__FILE__))."/Html.php";
require_once(dirname(__FILE__))."/Stier.php";
require_once(dirname(__FILE__))."/lib/StatMail.php";
//Program
$stier = new Stier();
$ind = Html::setPostOrGetVars(array(), array());
Html::outputNoCacheHeaders();
//Shall this user be ignored?
if (isset($ind['brugernavn']) and isset($_COOKIE[$ind['brugernavn']])
and $_COOKIE[$ind['brugernavn']] === 'ikkeop')
{
$ind['taelop'] = "nej";
$timeAdjusted = Html::getTimeAdjusted(NULL, $stier);
setcookie ($ind['brugernavn'], 'ikkeop',$timeAdjusted+28*24*3600, "/", ".".$stier->getOption('domain'));
}
if ($stier->getOption('logMode') !== 0 and (!isset($ind['taelop']) or $ind['taelop'] !== "nej")) {
require "lib/Logger.php";
//Find the username to log
if (isset($ind['brugernavn']) and strlen($ind['brugernavn']) > 0)
$username = $ind['brugernavn'];
else if (isset($ind['username']) and strlen($ind['username']) > 0)
$username = $ind['username'];
else
$username = '';
//Log the visit
$timeAdjusted = Html::getTimeAdjusted(NULL, $stier);
$logger = new Logger($stier);
$logger->logVisit($timeAdjusted,
(isset($ind['ssto']) ? $ind['ssto'] : ''),
(isset($ind['referer']) ? $ind['referer'] : ''),
(isset($ind['colors']) ? $ind['colors'] : ''),
(isset($ind['java']) ? $ind['java'] : ''),
(isset($ind['taelnr']) ? $ind['taelnr'] : ''),
(isset($ind['taelnavn']) ? $ind['taelnavn'] : ''),
(isset($ind['js']) ? $ind['js'] : ''),
getenv('HTTP_USER_AGENT'),
getenv('REMOTE_ADDR'),
getenv('HTTP_ACCEPT_LANGUAGE'),
getenv('HTTP_REFERER'),
$username,
'' //Not used yet
);
} //End if is log mode enabled
//Set the engine to NULL, so we can see if it has not been created.
$engine = NULL;
if ($stier->getOption('processMode') !== 0) {
require_once "Mstat.php";
require_once "lib/ZipStatEngine.php";
require_once "lib/SiteContext.php";
require_once "lib/Localizer.php";
//Loads the data file
if (! isset($ind['brugernavn'])) {
$ind['brugernavn'] = "";
}
$datafil = DataSource::createInstance($ind['brugernavn'],$stier);
$res = $datafil->hentFil();
//Handle errors:
$errors = new Errors();
//Was the datafile fetched successfully
if ($res === -2) {
//Temporarely instance for error handling
//'da' is danish, currently the only user interface language.
$siteContext = new ShortSiteContext($stier, $ind, 'da');
$errors->addError(new ZsError(2, sprintf($siteContext->getLocale('errDamagedDatasource'), $stier->getOption('name_of_service'))));
} elseif (! $res or $res === 0) {
//Temporarely instance for error handling
$siteContext = new ShortSiteContext($stier, $ind, 'da');
$errors->addError(new ZsError(2, sprintf($siteContext->getLocale('errDatasourceInaccessible'), $stier->getOption('name_of_service'))));
}
$lib = new Html($ind,$datafil);
$lib->setStier($stier);
//Write the stat image
if (!$errors->isOccured()) {
//'billed' is 'image' in danish: The image to display.
if (! array_key_exists('billed', $ind)) {
writeImage("stats1.gif",$stier);
} else {
//1-8: Normal images. trans: transparent/invisible.
//sh: Blach/white, hs: White/blach.
if ($ind['billed'] == "2") writeImage("stats2.gif",$stier);
elseif ($ind['billed'] == "3") writeImage("stats3.gif",$stier);
elseif ($ind['billed'] == "4") writeImage("stats4.gif",$stier);
elseif ($ind['billed'] == "5") writeImage("stats5.gif",$stier);
elseif ($ind['billed'] == "6") writeImage("stats6.gif",$stier);
elseif ($ind['billed'] == "7") writeImage("stats7.gif",$stier);
elseif ($ind['billed'] == "8") writeImage("stats8.gif",$stier);
elseif ($ind['billed'] == "trans") writeImage("stats_trans.gif",$stier);
elseif ($ind['billed'] == "taelsh") zipcount(0,$stier,$ind,$datafil);
elseif ($ind['billed'] == "taelhs") zipcount(1,$stier,$ind,$datafil);
else writeImage("stats1.gif",$stier);
}
} else {
require_once "lib/UsersArea/Utils.php";
$uaUtils = new UsersAreaUtils($siteContext);
$uaUtils->showErrors($errors);
exit;
}
//Ignore this visit on this page?
//'taelop' is danish for count up. nej is danish for no.
if (!$lib->countVisit(getenv("HTTP_REFERER"),$datafil->getLine(111))) {
$ind['taelop'] = "nej";
}
//Ignore visits from this IP-address?
if ((getenv("REMOTE_ADDR") === $datafil->getLine(52)) and ($datafil->getLine(52) != "")) {
$ind['taelop'] = "nej";
}
//Update the http vars.
$lib->setHTTPVars($ind);
//Only register visit if the taelop parameter does not forbid it.
if (isset($ind['taelop']) and $ind['taelop'] !== "nej" or !isset($ind['taelop']))
{
//Do the actual registering of the visit.
$engine = new ZipStatEngine($lib);
//Set user settings.
$engine->setCounterIgnoreQuery($datafil->getUserSetting('ignoreQuery') !== 'false');
//Process the visit.
$engine->process(Html::getTimeAdjusted(),
(isset($ind['ssto']) ? $ind['ssto'] : ''),
(isset($ind['referer']) ? $ind['referer'] : ''),
(isset($ind['colors']) ? $ind['colors'] : ''),
(isset($ind['java']) ? $ind['java'] : ''),
(isset($ind['taelnr']) ? $ind['taelnr'] : ''),
(isset($ind['taelnavn']) ? $ind['taelnavn'] : ''),
(isset($ind['js']) ? $ind['js'] : ''),
getenv('HTTP_USER_AGENT'),
getenv('REMOTE_ADDR'),
getenv('HTTP_ACCEPT_LANGUAGE'),
getenv('HTTP_REFERER')
);
}
//Is it time to send the user an e-mail with stats - if the user wants it.
$send = explode("::",$datafil->getLine(67));
$lastMailSend = array_shift($send);
$event = new EventCalculator(Html::getTimeAdjusted());
$found = $event->repeatNow($lastMailSend, $send);
if ($stier->getOption('send_stat_mails') === 0)
$found = false;
$datafil->setOperation('hit');
//Yes: Send an e-mail
if ($found === true)
{
//Decode settings for the requested stat site.
$parts = explode("&",$datafil->getLine(68));
$varsForStatSite = array();
for ($i = 0; $i < sizeof($parts); $i++)
{
$keyVal = explode("=",$parts[$i]);
if (count($keyVal) >= 2)
$varsForStatSite[$keyVal[0]] = $keyVal[1];
}
//Store that the mail has been send before it is send:
//Worst case: An e-mail is lorst, but we don't risk sending an e-mail
//that and not storing that it has been send, resulting in spamming the user.
array_unshift($send, Html::getTimeAdjusted());
$datafil->setLine(67,implode("::",$send));
$datafil->gemFil();
//Create the statsite
require_once "lib/StatSite.php";
require_once "lib/LegacyMapper.php";
require_once "lib/SiteContext.php";
require_once "lib/Localizer.php";
//Maps old to new parameters
$statSiteMapper = new StatSiteLegacyMapper();
$mappedInd = $statSiteMapper->applyMapping(array_merge($varsForStatSite, $ind));
//Creates a new site context for the stat site.
$siteContext = new SiteContext($lib, $stier, $mappedInd, 'da');
//Creates the statsite
$statSite = new StatSite($siteContext, 'text');
$statSite->setShowStatselector(0);
$statSite->setSendByMail(true);
$statSite->setMaxStatsToShow(-1); //Disable bandwidth limitation
//Generates the html for the statsite.
$side = $statSite->generateSite();
@mail($datafil->getLine(2),"Statistikker fra ZIP Stat", $side, "From: zipstat+mailstats@zipstat.dk\nReply-to: zipstat+mailstats@zipstat.dk\nX-abuse: zipstat+abuse@zipstat.dk\nX-Mailer: ZIP Stat mailstats\nContent-Type: text/plain");
//Stat site end
//Log the sending of the mail
//$fp = fopen("mailsSend.txt","a");
//fwrite($fp, "Mail send ".date('r')." ".$ind['brugernavn'].", adress: ".$datafil->getLine(2)."\n");
//fclose($fp);
}
else
{
$datafil->gemFil();
}
} //End if is processing mode not enabled
else
{
//Processing mode: Save the visit in a log file so it can be processed later.
if (! array_key_exists('billed', $ind)) {
writeImage("stats1.gif",$stier);
} else {
if ($ind['billed'] == "taelsh" and $ind['billed'] == "taelhs")
$ind['billed'] = "trans";
if ($ind['billed'] == "2") writeImage("stats2.gif",$stier);
elseif ($ind['billed'] == "3") writeImage("stats3.gif",$stier);
elseif ($ind['billed'] == "4") writeImage("stats4.gif",$stier);
elseif ($ind['billed'] == "5") writeImage("stats5.gif",$stier);
elseif ($ind['billed'] == "6") writeImage("stats6.gif",$stier);
elseif ($ind['billed'] == "7") writeImage("stats7.gif",$stier);
elseif ($ind['billed'] == "8") writeImage("stats8.gif",$stier);
elseif ($ind['billed'] == "trans") writeImage("stats_trans.gif",$stier);
elseif ($ind['billed'] == "taelsh") zipcount(0,$stier,$ind,$datafil);
elseif ($ind['billed'] == "taelhs") zipcount(1,$stier,$ind,$datafil);
else writeImage("stats1.gif",$stier);
}
}
//Register visits collectively?
if ($stier->getOption('collective') === 1) {
require_once "lib/ZipStatEngine.php";
$visit = new Visit();
$visit->setUnique($lib->isVisitUnique(getenv('REMOTE_ADDR')) ? 1 : 0);
$visit->setTime(Html::getTimeAdjusted());
$visit->setBrowser(ZipStatEngine::short_browser(getenv('HTTP_USER_AGENT')));
$visit->setOs(ZipStatEngine::platform(getenv('HTTP_USER_AGENT')));
$visit->setResolution((isset($ind['ssto']) ? $ind['ssto'] : '')); //Todo: Test for correct syntax
$visit->setColorDepth((isset($ind['colors']) ? $ind['colors'] : ''));
if (isset($ind['java']) and strlen($ind['java']) > 0) {
$visit->setJavaEnabled($ind['java'] === "true" ? 1 : 0);
} else {
$visit->setJavaEnabled(2);
}
if (isset($ind['js']) and strlen($ind['js']) > 0) {
$visit->setJavaScriptEnabled($ind['js'] === "true" ? 1 : 0);
} else {
$visit->setJavaScriptEnabled(2);
}
//Only set if the engine exists
if ($engine !== NULL) {
$visit->setSearchEngine($engine->getLatestSearchEngine());
$visit->setSearchWords($engine->getLatestSearchWords());
$visit->setLanguage($engine->getLatestPrefLanguage());
$visit->setTopdom($engine->getLatestTopdom());
}
$writer = $datafil->getWriter();
$writer->logVisitCollectively($visit);
}
///////////////////
function numberOrZero($str) {
if (is_numeric(trim($str))) {
return trim($str);
} else {
return 0;
}
}
/**
* Generates and displays a counter image.
*
* @public
* @version 0.0.1
* @since 0.0.1
* @param $coltype 0 is b/w and 1 is w/b.
* @param $stier an instance of Stier.
* @param $ind the http input parameters the script was given.
* @param $datafil the current datasource with the users data.
* @return void
*/
function zipcount($coltype,$stier,$ind,$datafil) {
if (isset($ind['taelnr']) and $ind['taelnr'] > 0 and (! isset($ind['etael']) or $ind['etael'] === "") and (! isset($ind['ntael']) or $ind['ntael'] === "")) {
$tmp = explode("::",$datafil->getLine(37));
$ind['taelnr'] = round($ind['taelnr']);
$counter = $tmp[$ind['taelnr']] + 1;
}
elseif (isset($ind['etael']))
$counter = numberOrZero($datafil->getLine(13)) + 1 + numberOrZero($datafil->getLine(82));
elseif (isset($ind['ntael']))
$counter = numberOrZero($datafil->getLine(7)) + 1 + numberOrZero($datafil->getLine(82));
elseif (isset($ind['taelnavn']))
{
$tmp = explode("::",$datafil->getLine(38));
$i = 0;
while (($i < sizeof($tmp)) and ($tmp[$i] != $ind['taelnavn']))
$i++;
$tmp = explode("::",$datafil->getLine(37));
$counter = $tmp[$i] +1;
}
else
{ //If nothing is given and the file name of the page does not exist in
//any of the counters
$counter = numberOrZero($datafil->getLine(7)) + 1 + numberOrZero($datafil->getLine(82));
}
$tillad = explode("::",$datafil->getLine(106));
//If the counter image may not be shown:
if (! $tillad[2]) {
$counter = "Skjult";
}
//$counter = 1234567890;
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/png");
$brede = strlen($counter)*8 +3;
$im = imagecreate($brede,16);
if ($coltype === 1)
{
$white = ImageColorAllocate($im, 255,255,255);
$black = ImageColorAllocate($im, 0,0,0);
$txtCol = $black;
}
else
{
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$txtCol = $white;
}
imagestring($im, 4, 0, 0, $counter, $txtCol);
ImagePng($im);
ImageDestroy($im);
}
/**
* Writes an image to the browser.
*
* @public
* @version 0.0.1
* @since 0.0.1
* @param $imageFile The file name of the image to write.
* @param $stier instance of the settings class Stier.
* @return void
*/
function writeImage($imageFile,&$stier)
{
//return 1; //Uncomment for debugging purposes - then text can be output instead of an image
$filename = $stier->getSti('zipstat_icons')."/".$imageFile;
$fd = fopen ($filename, "rb");
header("Content-type: image/gif");
header("Content-Length: " . filesize($filename));
fpassthru($fd);
fclose ($fd);
flush();
}
//$tl->theend();
?>