-
Notifications
You must be signed in to change notification settings - Fork 1
/
original.php
121 lines (100 loc) · 2.02 KB
/
original.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
<?php
/*
* Copyright (c) Sebastian Kucharczyk <kuchen@kekse.biz>
*/
/*
* BE SURE TO `chmod 1777 ./counter/`.. (so that PHP can access there, with write permissions, too)!
*/
//
define('DIRECTORY', 'counter');
//define('THRESHOLD', 600);
define('THRESHOLD', 7200);
define('LENGTH', 255);
define('CHARS', array_merge(range('a', 'z'), range('0', '9'), ['.','-']));
define('COOKIE', 'timestamp');
define('COOKIE_SAME_SITE', 'Strict');
define('COOKIE_PATH', '/');
define('COOKIE_HTTP_ONLY', true);
//
function secureHost($_hostname)
{
$_hostname = strtolower($_hostname);
$length = min(strlen($_hostname), LENGTH);
$result = '';
for($i = 0; $i < $length; $i++)
{
if(in_array($_hostname[$i], CHARS))
{
$result .= $_hostname[$i];
}
}
if(strlen($result) === 0)
{
die('Filtered hostname got no length');
}
return $result;
}
//
define('HOSTNAME', secureHost($_SERVER['HTTP_HOST']));
define('PATH', (DIRECTORY . '/' . HOSTNAME));
//
if(! file_exists(DIRECTORY))
{
die('Directory \'' . DIRECTORY . '\' doesn\'t exist - create with `chmod 1777`.');
}
//
function timestamp($_difference = null)
{
if(gettype($_difference) !== 'integer')
{
return time();
}
return (time() - $_difference);
}
function testCookie()
{
if(! isset($_COOKIE[COOKIE]))
{
makeCookie();
}
else if(timestamp((int)$_COOKIE[COOKIE]) < THRESHOLD)
{
return false;
}
return true;
}
function makeCookie()
{
return setcookie(COOKIE, timestamp(), array(
'expires' => (time() + THRESHOLD),
'domain' => HOSTNAME,
//'secure' => !!$_SERVER['HTTPS'],
'path' => COOKIE_PATH,
'samesite' => COOKIE_SAME_SITE,
'httponly' => COOKIE_HTTP_ONLY
));
}
function readCounter($_path = PATH)
{
if(! file_exists($_path))
{
touch($_path);
}
return (int)file_get_contents($_path);
}
function writeCounter($_value = 0, $_path = PATH)
{
return file_put_contents($_path, (string)$_value);
}
//
$count = readCounter();
if(testCookie())
{
writeCounter(++$count);
}
makeCookie();
//
header('Content-Type: text/plain;charset=UTF-8');
echo $count;
exit();
?>