Subversion Repositories webcounter

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * PHP Counter mit Reloadsperre, Textdatenbank und Graphic-Libary (without Error Images)
5
 * (C) 2010 Daniel Marschall
6
 */
7
 
8
define('VERSION', '2010-07-05 20:05');
9
 
10
// Usage
11
 
12
// index.php            --> shows Counter PNG
13
// index.php?mode=blind --> only logs the visit and show only a PNG spacer
14
// index.php?mode=text  --> show text only
15
 
16
error_reporting(E_ALL | E_NOTICE);
17
 
18
$USER  = isset($_GET['user'])  ? $_GET['user']  : '';
19
$THEME = isset($_GET['theme']) ? $_GET['theme'] : '';
20
$MODE  = isset($_GET['mode'])  ? $_GET['mode']  : '';
21
 
22
$USER  = str_replace('.', '', $USER);
23
$THEME = str_replace('.', '', $THEME);
24
 
25
// Einstellungen
26
 
27
define('USER_DIR', 'data/'.$USER.'/');
28
define('THEME_DIR', 'themes/'.$THEME.'/');
29
 
30
define('COUNTER_FILE', USER_DIR.'counter.txt');
31
define('STELLEN', 6);
32
 
33
define('IP_FILE', USER_DIR.'ips.txt');
34
define('RELOADSPERRE_AKTIV', true);
35
define('RELOAD_MINUTES', 10);
36
 
37
define('DIGIT_PREFIX', THEME_DIR.'digit_');
38
define('DIGIT_SUFFIX', '.png');
39
define('BG_IMG', THEME_DIR.'bg.png');
40
define('SPACER', 'spacer.png');
41
define('SETTINGS', 'settings.inc.php');
42
 
43
// Beginn Programmcode
44
 
45
if ($USER == '') {
46
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
47
        echo '<html>';
48
        echo '<head>';
49
        echo '<title>Counter by Daniel Marschall</title>';
50
        echo '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">';
51
        echo '</head>';
52
        echo '<body>';
53
        echo '<h1>Counter by Daniel Marschall</h1>';
54
        echo '<p>Version '.VERSION.'</p>';
55
        echo '<h2>Usage</h2>';
56
        echo '<p><b>?user=[username]&amp;theme=[theme]</b> -- shows the graphic counter</p>';
57
        echo '<p><b>?user=[username]&amp;mode=text</b> -- shows the text counter</p>';
58
        echo '<p><b>?user=[username]&amp;mode=blind</b> -- shows a hidden spacer image</p>';
59
        echo '<h2>Available themes</h2>';
60
        echo '<ul>';
61
        foreach(glob('themes/*', GLOB_ONLYDIR) as $dir) {
62
                $dir = str_replace('themes/', '', $dir);
63
                echo '<li>'.$dir.'</li>';
64
        }
65
        echo '</ul>';
66
        echo '<h2>Available users</h2>';
67
        echo '<ul>';
68
        foreach(glob('data/*', GLOB_ONLYDIR) as $dir) {
69
                $dir = str_replace('data/', '', $dir);
70
                echo '<li>'.$dir.'</li>';
71
        }
72
        echo '</ul>';
73
        echo '<h2>Want a counter too?</h2>';
74
        echo '<p>Ask Daniel Marschall: info at daniel-marschall dot de.</p>';
75
        echo '</body>';
76
        echo '</html>';
77
        die();
78
}
79
 
80
if (!is_dir(USER_DIR)) {
81
        die('User "'.$USER.'" does not exist');
82
}
83
 
84
if (($MODE != 'blind') && ($MODE != 'text')) {
85
        if ((!is_dir(THEME_DIR)) || ($THEME == '')) {
86
                die('Theme "'.$THEME.'" does not exist');
87
        }
88
        if (file_exists(THEME_DIR.SETTINGS)) {
89
                include(THEME_DIR.SETTINGS);
90
        }
91
}
92
 
93
if (!file_exists(COUNTER_FILE)) {
94
        // Die Datei counter.txt existiert nicht, sie wird neu angelegt und mit dem Wert 0 gefüllt.
95
        $fp = fopen(COUNTER_FILE, 'w');
96
        $zahl = "0";
97
        fputs($fp, $zahl, STELLEN);
98
        fclose($fp);
99
}
100
 
101
if (!file_exists(IP_FILE)) {
102
        $fp = fopen(IP_FILE, 'w');
103
        fclose($fp);
104
}
105
 
106
function fetchip()
107
{
108
        // Source: http://lists.phpbar.de/pipermail/php/Week-of-Mon-20040322/007749.html
109
 
110
        $client_ip = (isset($_SERVER['HTTP_CLIENT_IP'])) ? $_SERVER['HTTP_CLIENT_IP'] : '';
111
        $x_forwarded_for = (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
112
        $remote_addr = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '';
113
 
114
        if (!empty($client_ip))
115
        {
116
                $ip_expl = explode('.',$client_ip);
117
                $referer = explode('.',$remote_addr);
118
                if($referer[0] != $ip_expl[0])
119
                {
120
                        $ip=array_reverse($ip_expl);
121
                        $return=implode('.',$ip);
122
                }
123
                else
124
                {
125
                        $return = $client_ip;
126
                }
127
        }
128
        else if (!empty($x_forwarded_for))
129
        {
130
                if(strstr($x_forwarded_for,','))
131
                {
132
                        $ip_expl = explode(',',$x_forwarded_for);
133
                        $return = end($ip_expl);
134
                }
135
        }
136
        else
137
        {
138
                $return = $remote_addr;
139
        }
140
        unset ($client_ip, $x_forwarded_for, $remote_addr, $ip_expl);
141
        return $return;
142
}
143
 
144
function isReloadLocked() {
145
        $rem_addr = fetchip();
146
        @$ip_array = file(IP_FILE);
147
        $reload_dat = fopen(IP_FILE, 'w');
148
        $this_time = time();
149
        $found = false;
150
        for ($i=0; $i<count($ip_array); $i++) {
151
                list($ip_addr, $time_stamp) = explode('|', $ip_array[$i]);
152
                if ($this_time < ($time_stamp + 60*RELOAD_MINUTES)) {
153
                        if ($ip_addr == $rem_addr) {
154
                                $found = true;
155
                        } else {
156
                                fwrite($reload_dat, "$ip_addr|$time_stamp");
157
                        }
158
                }
159
        }
160
        fwrite($reload_dat, "$rem_addr|$this_time\n");
161
        fclose($reload_dat);
162
        return $found;
163
}
164
 
165
/////////////////////////////////////////
166
// Counter-Abfrage
167
/////////////////////////////////////////
168
 
169
function getCounter() {
170
        if ((!RELOADSPERRE_AKTIV) || (RELOADSPERRE_AKTIV && (!isReloadLocked()))) {
171
                // Es ist ein neuer Besucher
172
                $fp = fopen(COUNTER_FILE, 'r+');
173
                $zahl = fgets($fp, STELLEN);
174
                $zahl++;
175
                rewind($fp);
176
                flock($fp,2);
177
                fputs($fp, $zahl, STELLEN);
178
                flock($fp,3);
179
                fclose($fp);
180
        } else {
181
                // Es handelt sich wahrscheinlich um den gleichen Besucher
182
                $fp = fopen(COUNTER_FILE, 'r');
183
                $zahl = fgets($fp,STELLEN);
184
                fclose($fp);
185
        }
186
 
187
        $zahl = sprintf("%0".STELLEN."d", $zahl);
188
 
189
        return $zahl;
190
}
191
 
192
$counter = getCounter();
193
 
194
// No caching
195
header("HTTP/1.1 200 OK");
196
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
197
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
198
header("Cache-Control: no-store, no-cache, must-revalidate");
199
header("Cache-Control: post-check=0, pre-check=0", false);
200
header("Pragma: no-cache");
201
 
202
if ($MODE == 'blind') {
203
        header('Content-Type: image/png');
204
 
205
        readfile(SPACER);
206
} else if ($MODE == 'text') {
207
        header('Content-Type: text/plain');
208
 
209
        echo number_format ($counter, 0, ',', '.');
210
} else {
211
        header('Content-Type: image/png');
212
 
213
        $im = ImageCreateFromPNG (BG_IMG);
214
        imagesavealpha($im, true);
215
 
216
        $offset_x = DIGITS_X_OFFSET;
217
        $offset_y = DIGITS_Y_OFFSET;
218
 
219
        for ($i=0; $i<strlen($counter); $i++) {
220
                $digit_im = ImageCreateFromPNG(DIGIT_PREFIX.$counter[$i].DIGIT_SUFFIX);
221
                $digit_w = imagesx($digit_im);
222
                $digit_h = imagesy($digit_im);
223
 
224
                ImageCopy($im, $digit_im, $offset_x, $offset_y, 0, 0, $digit_w, $digit_h);
225
                ImageDestroy($digit_im);
226
 
227
                $offset_x += $digit_w + DIGITS_SPACER;
228
        }
229
 
230
        imagepng($im);
231
        imagedestroy($im);
232
}
233
 
234
?>