forked from pstimpel/phpcaptchapiwigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpcaptchaconfig.php
152 lines (118 loc) · 3.67 KB
/
phpcaptchaconfig.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
<?php
class PhpCaptchaConfig {
const SESSIONPATH = __DIR__.'/sessions/';
const CLEANSESSIONFILESAFTERSECONDS = 3600;
public static function cleanSessionDir() {
$path = PhpCaptchaConfig::SESSIONPATH;
$seconds = PhpCaptchaConfig::CLEANSESSIONFILESAFTERSECONDS;
$filter = '/\.session\.php$/i';
if(is_dir($path)) {
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ((time()-filectime($path.$file)) > $seconds) {
if (preg_match($filter, $file)) {
try {
if(file_exists($path.$file)) {
unlink( $path . $file );
}
} catch(Exception $ex) {
$ex->getMessage();
}
}
}
}
}
}
}
public static function putSessionfile($hash, $challenge) {
$sessionfilename = PhpCaptchaConfig::getSessionFilename($hash);
$filename = PhpCaptchaConfig::SESSIONPATH . $sessionfilename;
if(!is_dir(PhpCaptchaConfig::SESSIONPATH)) {
mkdir(PhpCaptchaConfig::SESSIONPATH);
$content = '';
$content .= '<?php'."\n";
$content .= '//do not touch'."\n";
$content .= "\n\n\n";
file_put_contents(PhpCaptchaConfig::SESSIONPATH.'index.php', $content);
}
if(file_exists($filename)) {
unlink($filename);
}
$content = '';
$content .= '<?php'."\n";
$content .= '//do not touch'."\n";
$content .= '$captcha="'.$challenge.'";'."\n\n\n";
file_put_contents($filename, $content);
}
public static function getChallengeFromFile($hash) {
$sessionfilename = PhpCaptchaConfig::getSessionFilename($hash);
$filename = PhpCaptchaConfig::SESSIONPATH . $sessionfilename;
if(file_exists($filename)) {
include $filename;
return $captcha;
}
return 'invalidvalue'.rand(0,10000000);
}
public static function getSessionFilename($hash) {
require_once __DIR__."/salt.php";
global $PHPCAPTCHA_SALT;
return md5($PHPCAPTCHA_SALT.$hash).".session.php";
}
public static function getPresets() {
$valid=array();
$valid['stringlength']=6;
$valid['charstouse']='abcdefghkmnpqrtuvwxyz23456789';
$valid['strictlowercase']=true;
$valid['bgcolor']="000000";
$valid['textcolor']="ffffff";
$valid['linecolor']="323232";
$valid['sizewidth']=200;
$valid['sizeheight']=50;
$valid['fontsize']=25;
$valid['numberoflines']=6;
$valid['thicknessoflines']=2;
$valid['allowad']=true;
$valid['guestonly']=false;
$valid['picture']=true;
$valid['category']=true;
$valid['register']=true;
$valid['guestbook']=true;
$valid['contactform']=true;
return $valid;
}
public static function readConfig() {
$config = PhpCaptchaConfig::getPresets();
if(file_exists(__DIR__ . "/config.php")) {
require_once __DIR__ . "/config.php";
$config['stringlength']=$stringlength;
$config['charstouse']=$charstouse;
$config['strictlowercase']=$strictlowercase;
$config['bgcolor']=$bgcolor;
$config['textcolor']=$textcolor;
$config['linecolor']=$linecolor;
$config['sizewidth']=$sizewidth;
$config['sizeheight']=$sizeheight;
$config['fontsize']=$fontsize;
$config['numberoflines']=$numberoflines;
$config['thicknessoflines']=$thicknessoflines;
$config['allowad']=$allowad;
$config['guestonly']=$guestonly;
$config['picture']=$picture;
$config['category']=$category;
$config['register']=$register;
// Attention: whenever you add future stuff, make sure to check for isset , since config.php might not
// carry the new vars
if(isset($guestbook)) {
$config['guestbook']=$guestbook;
} else {
$config['guestbook']=true;
}
if(isset($contactform)) {
$config['contactform']=$contactform;
} else {
$config['contactform']=true;
}
}
return $config;
}
}