forked from osde8info/ClickHeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
313 lines (297 loc) · 8.5 KB
/
index.php
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
<?php
/**
* ClickHeat : Fichier principal / Main file
*
* @author Yvan Taviaud - LabsMedia - www.labsmedia.com
* @since 27/10/2006
**/
/* Remove the last space on this line (between * and /) to enable debugging. Don't forget to disable this when you're done! * /
error_reporting(E_ALL);
restore_error_handler();
ini_set('display_errors', 1);
/* Languages */
$__languages = array('bg', 'cz', 'de', 'en', 'es', 'fr', 'hu', 'id', 'it', 'ja', 'nl', 'pl', 'pt', 'ro', 'ru', 'sr', 'tr', 'uk', 'zh');
$__action = isset($_GET['action']) && $_GET['action'] !== '' ? $_GET['action'] : 'view';
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] !== '')
{
$realPath = &$_SERVER['REQUEST_URI'];
}
elseif (isset($_SERVER['SCRIPT_NAME']) && $_SERVER['SCRIPT_NAME'] !== '')
{
$realPath = &$_SERVER['SCRIPT_NAME'];
}
else
{
exit(LANG_UNKNOWN_DIR);
}
if (substr($realPath, -1) === '/')
{
header('Location: '.$realPath.'index.php');
exit;
}
$dirName = dirname($realPath);
if ($dirName === '/')
{
$dirName = '';
}
define('CLICKHEAT_PATH', $dirName.'/');
define('CLICKHEAT_INDEX_PATH', 'index.php?');
define('CLICKHEAT_ROOT', str_replace('\\', '/', dirname(__FILE__)).'/');
define('CLICKHEAT_CONFIG', CLICKHEAT_ROOT.'config/config.php');
define('IS_PIWIK_MODULE', false);
/* Improve buffer usage and compression */
if (function_exists('ob_start'))
{
/* Check that Zlib is not enabled by default (value should be 1-9, else it will be an empty string) */
if (@ini_get('zlib.output_compression') || !function_exists('ob_gzhandler'))
{
ob_start();
}
else
{
ob_start('ob_gzhandler');
}
}
/* Loading language according to browser's Accept-Language or cookie «language» */
if (isset($_GET['language']))
{
$lang = $_GET['language'];
}
elseif (isset($_COOKIE['language']))
{
$lang = $_COOKIE['language'];
}
elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
$lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}
if (!isset($lang) || !in_array($lang, $__languages))
{
$lang = 'en';
}
if (!isset($_COOKIE['language']) || $_COOKIE['language'] !== $lang)
{
setcookie('language', $lang, time() + 365 * 86400, '/');
}
define('CLICKHEAT_LANGUAGE', $lang);
unset($lang);
include CLICKHEAT_ROOT.'languages/'.CLICKHEAT_LANGUAGE.'.php';
/* If there's no config file, run check script */
if (!file_exists(CLICKHEAT_CONFIG))
{
if ($__action !== 'check' && $__action !== 'config')
{
$__action = 'check';
}
}
else
{
include CLICKHEAT_CONFIG;
if (isset($_COOKIE['clickheat']))
{
if ($_COOKIE['clickheat'] === $clickheatConf['adminLogin'].'||'.$clickheatConf['adminPass'])
{
/* Everything is fine, admin logged */
define('CLICKHEAT_ADMIN', true);
}
elseif ($_COOKIE['clickheat'] === $clickheatConf['viewerLogin'].'||'.$clickheatConf['viewerPass'])
{
/* Viewer logged, force it to 'view' action if not view|generate|png */
if ($__action !== 'generate' && $__action !== 'png' && $__action !== 'iframe' && $__action !== 'cleaner' && $__action !== 'logout')
{
$__action = 'view';
}
}
else
{
/* Not logged, send him to login form */
$__action = 'logout';
}
}
else
{
if (isset($_POST['login']) && isset($_POST['pass']))
{
if ($_POST['login'] === $clickheatConf['adminLogin'] && md5($_POST['pass']) === $clickheatConf['adminPass'])
{
/* Set a session cookie */
setcookie('clickheat', $clickheatConf['adminLogin'].'||'.$clickheatConf['adminPass'], 0, '/');
/* Redirect to index.php */
header('Content-Type: text/html');
/* Upgrade needed ? */
include CLICKHEAT_ROOT.'version.php';
if (!isset($clickheatConf['version']) || $clickheatConf['version'] !== CLICKHEAT_VERSION)
{
$url = CLICKHEAT_INDEX_PATH.'action=config';
}
else
{
$url = CLICKHEAT_INDEX_PATH.'action=view';
}
/* IIS removes cookies when sending a 301/302 header, so we need to do some crap (and yes, this HTML code is crap too) */
if (strpos($_SERVER['SERVER_SOFTWARE'], 'IIS'))
{
echo '<meta http-equiv="refresh" content="0;', $url, '" />';
echo '<a href="', $url, '">click here</a>';
}
else
{
header('Location: '.$url);
}
exit;
}
elseif ($clickheatConf['viewerLogin'] !== '' && $_POST['login'] === $clickheatConf['viewerLogin'] && md5($_POST['pass']) === $clickheatConf['viewerPass'])
{
/* Set a session cookie */
setcookie('clickheat', $clickheatConf['viewerLogin'].'||'.$clickheatConf['viewerPass'], 0, '/');
/* Redirect to index.php */
header('Content-Type: text/html');
/* IIS removes cookies when sending a 301/302 header, so we need to do some crap (and yes, this HTML code is crap too) */
if (strpos($_SERVER['SERVER_SOFTWARE'], 'IIS'))
{
echo '<meta http-equiv="refresh" content="0;', CLICKHEAT_INDEX_PATH, 'action=view" />';
echo '<a href="', CLICKHEAT_INDEX_PATH, 'action=view">click here</a>';
}
else
{
header('Location: '.CLICKHEAT_INDEX_PATH.'action=view');
}
exit;
}
}
$__action = 'login';
}
}
if (!defined('CLICKHEAT_ADMIN'))
{
define('CLICKHEAT_ADMIN', false);
}
/* Specific definitions */
$clickheatConf['__screenSizes'] = array(0 /* Must start with 0 */, 240, 640, 800, 1024, 1152, 1280, 1440, 1600, 1800);
$clickheatConf['__browsersList'] = array('all' => '', 'msie' => 'Internet Explorer', 'firefox' => 'Firefox', 'chrome' => 'Chrome', 'safari' => 'Safari', 'opera' => 'Opera', 'unknown' => '');
switch ($__action)
{
case 'config':
{
if (file_exists(CLICKHEAT_CONFIG) && CLICKHEAT_ADMIN !== true)
{
exit('Error');
}
/* No break here */
}
case 'check':
case 'view':
case 'login':
{
header('Content-Type: text/html; charset=utf-8');
include CLICKHEAT_ROOT.'header.php';
include CLICKHEAT_ROOT.$__action.'.php';
include CLICKHEAT_ROOT.'footer.php';
break;
}
case 'generate':
case 'layout':
case 'javascript':
case 'latest':
case 'cleaner':
{
header('Content-Type: text/html; charset=utf-8');
include CLICKHEAT_ROOT.$__action.'.php';
break;
}
case 'iframe':
{
$group = isset($_GET['group']) ? str_replace('/', '', $_GET['group']) : '';
if (is_dir($clickheatConf['logPath'].$group))
{
$webPage = array('/');
if (file_exists($clickheatConf['logPath'].$group.'/url.txt'))
{
$f = fopen($clickheatConf['logPath'].$group.'/url.txt', 'r');
if ($f !== false)
{
$webPage = explode('>', trim(fgets($f, 1024)));
fclose($f);
}
}
echo $webPage[0];
}
break;
}
case 'png':
{
$imagePath = $clickheatConf['cachePath'].(isset($_GET['file']) ? str_replace('/', '', $_GET['file']) : '**unknown**');
header('Content-Type: image/png');
if (file_exists($imagePath))
{
readfile($imagePath);
}
else
{
readfile(CLICKHEAT_ROOT.'images/warning.png');
}
break;
}
case 'layoutupdate':
{
if (CLICKHEAT_ADMIN !== true)
{
exit('Error');
}
$group = isset($_GET['group']) ? str_replace('/', '', $_GET['group']) : '';
$url = isset($_GET['url']) ? $_GET['url'] : '';
if (strpos($url, 'http') !== 0)
{
$url = 'http://'.$_SERVER['SERVER_NAME'].'/'.ltrim($url, '/');
}
/* Improved security for PHP injection (PMV2.3b3 bug) */
$url = parse_url(str_replace(array('<', '>'), array('', ''), $url));
$left = isset($_GET['left']) ? (int) $_GET['left'] : 0;
$center = isset($_GET['center']) ? (int) $_GET['center'] : 0;
$right = isset($_GET['right']) ? (int) $_GET['right'] : 0;
if (!is_dir($clickheatConf['logPath'].$group) || !isset($url['host']) || !isset($url['path']))
{
exit('Error');
}
if ($url['scheme'] !== 'http' && $url['scheme'] !== 'https')
{
$url['scheme'] = 'http';
}
if (isset($url['query']))
{
$url = $url['scheme'].'://'.$url['host'].$url['path'].'?'.$url['query'];
}
else
{
$url = $url['scheme'].'://'.$url['host'].$url['path'];
}
$f = fopen($clickheatConf['logPath'].$group.'/url.txt', 'w');
fputs($f, $url.'>'.$left.'>'.$center.'>'.$right);
fclose($f);
echo 'OK';
break;
}
case 'logout':
{
setcookie('clickheat', '', time() - 30 * 86400, '/');
/* IIS removes cookies when sending a 301/302 header, so we need to do some crap (and yes, this HTML code is crap too) */
if (strpos($_SERVER['SERVER_SOFTWARE'], 'IIS'))
{
echo '<meta http-equiv="refresh" content="0;', CLICKHEAT_INDEX_PATH, 'action=view" />';
echo '<a href="', CLICKHEAT_INDEX_PATH, 'action=view">click here</a>';
}
else
{
header('Location: index.php');
}
exit;
break;
}
default:
{
header('HTTP/1.0 404 Not Found');
exit('Error, page not found');
break;
}
}
?>