-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFileCookie.php
70 lines (64 loc) · 1.89 KB
/
FileCookie.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
<?php
namespace dokuwiki\plugin\captcha;
/**
* Remember the issuing (and use) of CAPTCHAs by placing a file on the server
*
* This is used to prevent replay attacks. It is generated when the captcha form
* is shown and checked with the captcha check. Since we can not be sure about the
* session state (might be closed or open) we're not using it.
*
* We're not using the stored values for displaying the captcha image (or audio)
* but continue to use our encryption scheme. This way it's still possible to have
* multiple captcha checks going on in parallel (eg. with multiple browser tabs)
*/
class FileCookie
{
protected $path;
/**
* Initialize the cookie
*
* @param $fixed string the fixed part, any string
* @param $rand float some random number between 0 and 1
*/
public function __construct($ident, $rand)
{
global $conf;
$this->path = $conf['tmpdir'] . '/captcha/' . date('Y-m-d') . '/' . md5($ident . $rand) . '.cookie';
io_makeFileDir($this->path);
}
/**
* Creates a one time captcha cookie
*/
public function set()
{
touch($this->path);
}
/**
* Checks if the captcha cookie exists and deletes it
*
* @return bool true if the cookie existed
*/
public function check()
{
if (file_exists($this->path)) {
unlink($this->path);
return true;
}
return false;
}
/**
* remove all outdated captcha cookies
*/
public static function clean()
{
global $conf;
$path = $conf['tmpdir'] . '/captcha/';
$dirs = glob("$path/*", GLOB_ONLYDIR);
$today = date('Y-m-d');
foreach ($dirs as $dir) {
if (basename($dir) === $today) continue;
if (!preg_match('/\/captcha\//', $dir)) continue; // safety net
io_rmdir($dir, true);
}
}
}