Subversion Repositories webcounter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 daniel-mar 1
<?php
2
 
3
/*
4
 * PHP Counter mit Reloadsperre, Textdatenbank und Graphic-Libary (without Error Images)
5
 * (C)Copyright 2010 - 2017 Daniel Marschall
6
 * Revision: 2017-05-14
7
 */
8
 
9
error_reporting(E_ALL | E_NOTICE);
10
assert_options(ASSERT_ACTIVE, true);
11
assert_options(ASSERT_BAIL, true);
12
 
13
if (!file_exists(__DIR__ . '/config/config.inc.php')) {
14
        die('Please edit config/config_dist.inc.php and then rename it to config/config.inc.php !');
15
}
16
 
17
require_once __DIR__ . '/config/config.inc.php';
18
require_once __DIR__ . '/includes/functions.inc.php';
19
require_once __DIR__ . '/includes/VtsCounterTheme.class.php';
20
require_once __DIR__ . '/includes/VtsCounter.class.php';
21
require_once __DIR__ . '/includes/VtsCounterInfo.class.php';
22
 
23
if ((!isset($_REQUEST['id'])) || ($_REQUEST['id'] == '')) {
24
        die('Argument "id" is missing');
25
}
26
 
27
if ($_REQUEST['id'] == 'demo') {
28
        $visitors = isset($_REQUEST['demo']) ? $_REQUEST['demo'] : 123456;
29
        $created = '2017-05-04 00:00:00';
30
} else {
31
        $pdo = new PDO(PDO_HOST, PDO_USER, PDO_PASS);
32
        $c = new VtsCounter($pdo);
33
        $c->clearReloadSperre(RELOADSPERRE_MINS);
34
        $counter_id = $c->getIDfromIDStr($_REQUEST['id']);
35
        $c->visitCount($counter_id, fetchip());
36
        $info = $c->getCounterInfo($counter_id);
37
        $visitors = $info->visitors;
38
        $created = $info->created;
39
}
40
$querytime = gmdate('Y-m-d\TH:i:s\Z'); // ISO 8601
41
$created = date('Y-m-d\TH:i:s\Z', strtotime($created));
42
 
43
/*
44
// No caching
45
header("HTTP/1.1 200 OK");
46
header("Expires: Thu, 24 Dec 1987 08:30:00 GMT");
47
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
48
header("Cache-Control: no-store, no-cache, must-revalidate");
49
header("Cache-Control: post-check=0, pre-check=0", false);
50
header("Pragma: no-cache");
51
*/
52
 
53
$etag = md5($visitors . $_SERVER['QUERY_STRING']);
54
header("ETag: $etag");
55
if (anyTagMatched($etag)) {
56
        header("HTTP/1.1 304 Not Modified");
57
        die();
58
} else {
59
        header("HTTP/1.1 200 OK");
60
}
61
 
62
 
63
$format = isset($_REQUEST['format']) ? $_REQUEST['format'] : 'graphic';
64
 
65
if ($format == 'graphic') {
66
        $theme = isset($_REQUEST['theme']) ? preg_replace('/[^a-z0-9_\-]/', '', $_REQUEST['theme']) : null;
67
 
68
        if (is_null($theme)) die('Parameter "theme" is missing');
69
 
70
        $themeFile = __DIR__ . "/themes/$theme/theme.inc.php";
71
 
72
        if (!file_exists($themeFile)) die("Theme '$theme' does not exist.");
73
 
74
        $themeObj = null;
75
        include $themeFile;
76
        assert(!is_null($themeObj));
77
 
78
        $hue = isset($_REQUEST['hue']) ? $_REQUEST['hue'] : 0;
79
        $themeObj->outputCounterImage($visitors, 'png', $hue);
80
} else if ($format == 'silent') {
81
        // nothing
82
} else if ($format == 'spacer') {
83
        header('Content-Type: image/png');
84
        readfile(__DIR__ . '/spacer.png');
85
} else if ($format == 'plaintext') {
86
        header('Content-Type: text/plain');
87
        echo $visitors;
88
} else if ($format == 'json') {
89
        header('Content-Type: application/json');
90
        $out = array();
91
        $out['created'] = $created;
92
        $out['querytime'] = $querytime;
93
        $out['visitors'] = $visitors;
94
        echo json_encode($out);
95
} else {
96
        die('Argument "format" must be either graphic, silent, spacer, plaintext or json');
97
}