PluginProbe ʕ •ᴥ•ʔ
Wordfence Security – Firewall, Malware Scan, and Login Security / 3.2.6
Wordfence Security – Firewall, Malware Scan, and Login Security v3.2.6
8.2.2 8.2.1 8.2.0 3.7.1 3.7.2 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.9.1 4.0.1 4.0.2 4.0.3 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.1 5.1.2 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.1 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.1 5.3.10 5.3.11 5.3.12 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.3.8 5.3.9 6.0.1 6.0.10 6.0.11 6.0.12 6.0.14 6.0.15 6.0.16 6.0.17 6.0.18 6.0.19 6.0.2 6.0.20 6.0.21 6.0.22 6.0.23 6.0.24 6.0.25 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.1.1 6.1.10 6.1.11 6.1.12 6.1.14 6.1.15 6.1.16 6.1.17 6.1.2 6.1.3 6.1.4 6.1.5 6.1.6 6.1.7 6.1.8 6.1.9 6.2.0 6.2.1 6.2.10 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.7 6.2.8 6.2.9 6.3.0 6.3.1 6.3.10 6.3.11 6.3.12 6.3.14 6.3.15 6.3.16 6.3.17 6.3.18 6.3.19 6.3.2 6.3.20 6.3.21 6.3.22 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1.0 7.1.1 7.1.10 7.1.11 7.1.12 7.1.14 7.1.15 7.1.16 7.1.17 7.1.18 7.1.19 7.1.2 7.1.20 7.1.3 7.1.4 7.1.5 7.1.6 7.1.7 7.1.8 7.1.9 7.10.0 7.10.1 7.10.2 7.10.3 7.10.4 7.10.5 7.10.6 7.10.7 7.11.0 7.11.1 7.11.2 7.11.3 7.11.4 7.11.5 7.11.6 7.11.7 7.2.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.4.0 7.4.1 7.4.10 7.4.11 7.4.12 7.4.14 7.4.2 7.4.3 trunk 7.4.4 1.1 7.4.5 1.2 7.4.6 1.3 7.4.7 1.3.1 7.4.8 1.3.2 7.4.9 1.3.3 7.5.0 1.4.2 7.5.1 1.4.3 7.5.10 1.4.4 7.5.11 1.4.5 7.5.2 1.4.6 7.5.3 1.4.7 7.5.4 1.4.8 7.5.5 1.5.1 7.5.6 1.5.2 7.5.7 1.5.3 7.5.8 1.5.4 7.5.9 1.5.5 7.6.0 1.5.6 7.6.1 2.0.1 7.6.2 2.0.2 7.7.0 2.0.3 7.7.1 2.0.5 7.8.0 2.0.6 7.8.1 2.0.7 7.8.2 2.1.0 7.9.0 2.1.1 7.9.1 2.1.2 7.9.2 2.1.3 7.9.3 2.1.4 8.0.0 2.1.5 8.0.1 3.0.2 8.0.2 3.0.3 8.0.3 3.0.4 8.0.4 3.0.5 8.0.5 3.0.6 8.1.0 3.0.7 8.1.1 3.0.8 8.1.2 3.0.9 8.1.3 3.1.0 8.1.4 3.1.1 v1.4.1 3.1.2 3.1.4 3.1.6 3.2.1 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.1 3.4.4 3.4.5 3.5.1 3.5.2 3.6.1 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
wordfence / lib / wfConfig.php
wordfence / lib Last commit date
Diff 14 years ago .htaccess 14 years ago Diff.php 14 years ago GeoIP.dat 13 years ago IPTraf.php 13 years ago diffResult.php 14 years ago email_genericAlert.php 14 years ago email_newIssues.php 14 years ago email_unlockRequest.php 14 years ago menu_activity.php 13 years ago menu_blockedIPs.php 13 years ago menu_countryBlocking.php 13 years ago menu_options.php 13 years ago menu_scan.php 13 years ago menu_scanSchedule.php 13 years ago schedWeekEntry.php 13 years ago sysinfo.php 14 years ago unknownFiles.php 13 years ago viewFullActivityLog.php 13 years ago wf503.php 13 years ago wfAPI.php 13 years ago wfAction.php 14 years ago wfArray.php 13 years ago wfBrowscap.php 14 years ago wfBrowscapCache.php 14 years ago wfBulkCountries.php 13 years ago wfConfig.php 13 years ago wfCountryMap.php 13 years ago wfCrawl.php 13 years ago wfDB.php 13 years ago wfDict.php 14 years ago wfGeoIP.php 13 years ago wfIssues.php 13 years ago wfLockedOut.php 14 years ago wfLog.php 13 years ago wfModTracker.php 14 years ago wfRate.php 14 years ago wfScanEngine.php 13 years ago wfSchema.php 13 years ago wfUnlockMsg.php 14 years ago wfUtils.php 13 years ago wfViewResult.php 14 years ago wordfenceClass.php 13 years ago wordfenceConstants.php 13 years ago wordfenceHash.php 13 years ago wordfenceScanner.php 13 years ago wordfenceURLHoover.php 13 years ago
wfConfig.php
581 lines
1 <?php
2 class wfConfig {
3 private static $table = false;
4 private static $cache = array();
5 private static $DB = false;
6 private static $tmpFileHeader = "<?php\n/* Wordfence temporary file security header */\necho \"Nothing to see here!\\n\"; exit(0);\n?>";
7 private static $tmpDirCache = false;
8 public static $securityLevels = array(
9 array( //level 0
10 "checkboxes" => array(
11 "alertOn_critical" => false,
12 "alertOn_warnings" => false,
13 "alertOn_throttle" => false,
14 "alertOn_block" => false,
15 "alertOn_loginLockout" => false,
16 "alertOn_lostPasswdForm" => false,
17 "alertOn_adminLogin" => false,
18 "alertOn_nonAdminLogin" => false,
19 "liveTrafficEnabled" => true,
20 "liveTraf_ignorePublishers" => true,
21 "scheduledScansEnabled" => false,
22 "scansEnabled_public" => false,
23 "scansEnabled_core" => false,
24 "scansEnabled_themes" => false,
25 "scansEnabled_plugins" => false,
26 "scansEnabled_malware" => false,
27 "scansEnabled_fileContents" => false,
28 "scansEnabled_posts" => false,
29 "scansEnabled_comments" => false,
30 "scansEnabled_passwds" => false,
31 "scansEnabled_diskSpace" => false,
32 "scansEnabled_dns" => false,
33 "scansEnabled_oldVersions" => false,
34 "firewallEnabled" => false,
35 "blockFakeBots" => false,
36 "autoBlockScanners" => false,
37 "loginSecurityEnabled" => false,
38 "loginSec_lockInvalidUsers" => false,
39 "loginSec_maskLoginErrors" => false,
40 "other_hideWPVersion" => false,
41 "other_noAnonMemberComments" => false,
42 "other_scanComments" => false,
43 "other_pwStrengthOnUpdate" => false,
44 "other_WFNet" => true,
45 "other_scanOutside" => false,
46 "deleteTablesOnDeact" => false,
47 "debugOn" => false
48 ),
49 "otherParams" => array(
50 'securityLevel' => '0',
51 "alertEmails" => "", "liveTraf_ignoreUsers" => "", "liveTraf_ignoreIPs" => "", "liveTraf_ignoreUA" => "", "apiKey" => "", "maxMem" => '256', 'whitelisted' => '',
52 "neverBlockBG" => "neverBlockVerified",
53 "loginSec_countFailMins" => "5",
54 "loginSec_lockoutMins" => "5",
55 'loginSec_maxFailures' => "500",
56 'loginSec_maxForgotPasswd' => "500",
57 'maxGlobalRequests' => "DISABLED",
58 'maxGlobalRequests_action' => "throttle",
59 'maxRequestsCrawlers' => "DISABLED",
60 'maxRequestsCrawlers_action' => "throttle",
61 'maxRequestsHumans' => "DISABLED",
62 'maxRequestsHumans_action' => "throttle",
63 'max404Crawlers' => "DISABLED",
64 'max404Crawlers_action' => "throttle",
65 'max404Humans' => "DISABLED",
66 'max404Humans_action' => "throttle",
67 'maxScanHits' => "DISABLED",
68 'maxScanHits_action' => "throttle",
69 'blockedTime' => "300"
70 )
71 ),
72 array( //level 1
73 "checkboxes" => array(
74 "alertOn_critical" => true,
75 "alertOn_warnings" => false,
76 "alertOn_throttle" => false,
77 "alertOn_block" => false,
78 "alertOn_loginLockout" => false,
79 "alertOn_lostPasswdForm" => false,
80 "alertOn_adminLogin" => false,
81 "alertOn_nonAdminLogin" => false,
82 "liveTrafficEnabled" => true,
83 "liveTraf_ignorePublishers" => true,
84 "scheduledScansEnabled" => true,
85 "scansEnabled_public" => false,
86 "scansEnabled_core" => true,
87 "scansEnabled_themes" => false,
88 "scansEnabled_plugins" => false,
89 "scansEnabled_malware" => true,
90 "scansEnabled_fileContents" => true,
91 "scansEnabled_posts" => true,
92 "scansEnabled_comments" => true,
93 "scansEnabled_passwds" => true,
94 "scansEnabled_diskSpace" => true,
95 "scansEnabled_dns" => true,
96 "scansEnabled_oldVersions" => true,
97 "firewallEnabled" => false,
98 "blockFakeBots" => false,
99 "autoBlockScanners" => true,
100 "loginSecurityEnabled" => true,
101 "loginSec_lockInvalidUsers" => false,
102 "loginSec_maskLoginErrors" => true,
103 "other_hideWPVersion" => true,
104 "other_noAnonMemberComments" => true,
105 "other_scanComments" => true,
106 "other_pwStrengthOnUpdate" => true,
107 "other_WFNet" => true,
108 "other_scanOutside" => false,
109 "deleteTablesOnDeact" => false,
110 "debugOn" => false
111 ),
112 "otherParams" => array(
113 'securityLevel' => '1',
114 "alertEmails" => "", "liveTraf_ignoreUsers" => "", "liveTraf_ignoreIPs" => "", "liveTraf_ignoreUA" => "", "apiKey" => "", "maxMem" => '256', 'whitelisted' => '',
115 "neverBlockBG" => "neverBlockVerified",
116 "loginSec_countFailMins" => "5",
117 "loginSec_lockoutMins" => "5",
118 'loginSec_maxFailures' => "50",
119 'loginSec_maxForgotPasswd' => "50",
120 'maxGlobalRequests' => "DISABLED",
121 'maxGlobalRequests_action' => "throttle",
122 'maxRequestsCrawlers' => "DISABLED",
123 'maxRequestsCrawlers_action' => "throttle",
124 'maxRequestsHumans' => "DISABLED",
125 'maxRequestsHumans_action' => "throttle",
126 'max404Crawlers' => "DISABLED",
127 'max404Crawlers_action' => "throttle",
128 'max404Humans' => "DISABLED",
129 'max404Humans_action' => "throttle",
130 'maxScanHits' => "DISABLED",
131 'maxScanHits_action' => "throttle",
132 'blockedTime' => "300"
133 )
134 ),
135 array( //level 2
136 "checkboxes" => array(
137 "alertOn_critical" => true,
138 "alertOn_warnings" => true,
139 "alertOn_throttle" => false,
140 "alertOn_block" => false,
141 "alertOn_loginLockout" => false,
142 "alertOn_lostPasswdForm" => false,
143 "alertOn_adminLogin" => false,
144 "alertOn_nonAdminLogin" => false,
145 "liveTrafficEnabled" => true,
146 "liveTraf_ignorePublishers" => true,
147 "scheduledScansEnabled" => true,
148 "scansEnabled_public" => false,
149 "scansEnabled_core" => true,
150 "scansEnabled_themes" => false,
151 "scansEnabled_plugins" => false,
152 "scansEnabled_malware" => true,
153 "scansEnabled_fileContents" => true,
154 "scansEnabled_posts" => true,
155 "scansEnabled_comments" => true,
156 "scansEnabled_passwds" => true,
157 "scansEnabled_diskSpace" => true,
158 "scansEnabled_dns" => true,
159 "scansEnabled_oldVersions" => true,
160 "firewallEnabled" => false,
161 "blockFakeBots" => false,
162 "autoBlockScanners" => true,
163 "loginSecurityEnabled" => true,
164 "loginSec_lockInvalidUsers" => false,
165 "loginSec_maskLoginErrors" => true,
166 "other_hideWPVersion" => true,
167 "other_noAnonMemberComments" => true,
168 "other_scanComments" => true,
169 "other_pwStrengthOnUpdate" => true,
170 "other_WFNet" => true,
171 "other_scanOutside" => false,
172 "deleteTablesOnDeact" => false,
173 "debugOn" => false
174 ),
175 "otherParams" => array(
176 'securityLevel' => '2',
177 "alertEmails" => "", "liveTraf_ignoreUsers" => "", "liveTraf_ignoreIPs" => "", "liveTraf_ignoreUA" => "", "apiKey" => "", "maxMem" => '256', 'whitelisted' => '',
178 "neverBlockBG" => "neverBlockVerified",
179 "loginSec_countFailMins" => "240",
180 "loginSec_lockoutMins" => "240",
181 'loginSec_maxFailures' => "20",
182 'loginSec_maxForgotPasswd' => "20",
183 'maxGlobalRequests' => "DISABLED",
184 'maxGlobalRequests_action' => "throttle",
185 'maxRequestsCrawlers' => "DISABLED",
186 'maxRequestsCrawlers_action' => "throttle",
187 'maxRequestsHumans' => "DISABLED",
188 'maxRequestsHumans_action' => "throttle",
189 'max404Crawlers' => "DISABLED",
190 'max404Crawlers_action' => "throttle",
191 'max404Humans' => "DISABLED",
192 'max404Humans_action' => "throttle",
193 'maxScanHits' => "DISABLED",
194 'maxScanHits_action' => "throttle",
195 'blockedTime' => "300"
196 )
197 ),
198 array( //level 3
199 "checkboxes" => array(
200 "alertOn_critical" => true,
201 "alertOn_warnings" => true,
202 "alertOn_throttle" => false,
203 "alertOn_block" => false,
204 "alertOn_loginLockout" => false,
205 "alertOn_lostPasswdForm" => false,
206 "alertOn_adminLogin" => false,
207 "alertOn_nonAdminLogin" => false,
208 "liveTrafficEnabled" => true,
209 "liveTraf_ignorePublishers" => true,
210 "scheduledScansEnabled" => true,
211 "scansEnabled_public" => false,
212 "scansEnabled_core" => true,
213 "scansEnabled_themes" => false,
214 "scansEnabled_plugins" => false,
215 "scansEnabled_malware" => true,
216 "scansEnabled_fileContents" => true,
217 "scansEnabled_posts" => true,
218 "scansEnabled_comments" => true,
219 "scansEnabled_passwds" => true,
220 "scansEnabled_diskSpace" => true,
221 "scansEnabled_dns" => true,
222 "scansEnabled_oldVersions" => true,
223 "firewallEnabled" => true,
224 "blockFakeBots" => false,
225 "autoBlockScanners" => true,
226 "loginSecurityEnabled" => true,
227 "loginSec_lockInvalidUsers" => false,
228 "loginSec_maskLoginErrors" => true,
229 "other_hideWPVersion" => true,
230 "other_noAnonMemberComments" => true,
231 "other_scanComments" => true,
232 "other_pwStrengthOnUpdate" => true,
233 "other_WFNet" => true,
234 "other_scanOutside" => false,
235 "deleteTablesOnDeact" => false,
236 "debugOn" => false
237 ),
238 "otherParams" => array(
239 'securityLevel' => '3',
240 "alertEmails" => "", "liveTraf_ignoreUsers" => "", "liveTraf_ignoreIPs" => "", "liveTraf_ignoreUA" => "", "apiKey" => "", "maxMem" => '256', 'whitelisted' => '',
241 "neverBlockBG" => "neverBlockVerified",
242 "loginSec_countFailMins" => "1440",
243 "loginSec_lockoutMins" => "1440",
244 'loginSec_maxFailures' => "10",
245 'loginSec_maxForgotPasswd' => "10",
246 'maxGlobalRequests' => "960",
247 'maxGlobalRequests_action' => "throttle",
248 'maxRequestsCrawlers' => "960",
249 'maxRequestsCrawlers_action' => "throttle",
250 'maxRequestsHumans' => "60",
251 'maxRequestsHumans_action' => "throttle",
252 'max404Crawlers' => "60",
253 'max404Crawlers_action' => "throttle",
254 'max404Humans' => "60",
255 'max404Humans_action' => "throttle",
256 'maxScanHits' => "30",
257 'maxScanHits_action' => "throttle",
258 'blockedTime' => "1800"
259 )
260 ),
261 array( //level 4
262 "checkboxes" => array(
263 "alertOn_critical" => true,
264 "alertOn_warnings" => true,
265 "alertOn_throttle" => false,
266 "alertOn_block" => false,
267 "alertOn_loginLockout" => false,
268 "alertOn_lostPasswdForm" => false,
269 "alertOn_adminLogin" => false,
270 "alertOn_nonAdminLogin" => false,
271 "liveTrafficEnabled" => true,
272 "liveTraf_ignorePublishers" => true,
273 "scheduledScansEnabled" => true,
274 "scansEnabled_public" => false,
275 "scansEnabled_core" => true,
276 "scansEnabled_themes" => false,
277 "scansEnabled_plugins" => false,
278 "scansEnabled_malware" => true,
279 "scansEnabled_fileContents" => true,
280 "scansEnabled_posts" => true,
281 "scansEnabled_comments" => true,
282 "scansEnabled_passwds" => true,
283 "scansEnabled_diskSpace" => true,
284 "scansEnabled_dns" => true,
285 "scansEnabled_oldVersions" => true,
286 "firewallEnabled" => true,
287 "blockFakeBots" => true,
288 "autoBlockScanners" => true,
289 "loginSecurityEnabled" => true,
290 "loginSec_lockInvalidUsers" => true,
291 "loginSec_maskLoginErrors" => true,
292 "other_hideWPVersion" => true,
293 "other_noAnonMemberComments" => true,
294 "other_scanComments" => true,
295 "other_pwStrengthOnUpdate" => true,
296 "other_WFNet" => true,
297 "other_scanOutside" => false,
298 "deleteTablesOnDeact" => false,
299 "debugOn" => false
300 ),
301 "otherParams" => array(
302 'securityLevel' => '4',
303 "alertEmails" => "", "liveTraf_ignoreUsers" => "", "liveTraf_ignoreIPs" => "", "liveTraf_ignoreUA" => "", "apiKey" => "", "maxMem" => '256', 'whitelisted' => '',
304 "neverBlockBG" => "neverBlockVerified",
305 "loginSec_countFailMins" => "1440",
306 "loginSec_lockoutMins" => "1440",
307 'loginSec_maxFailures' => "5",
308 'loginSec_maxForgotPasswd' => "5",
309 'maxGlobalRequests' => "960",
310 'maxGlobalRequests_action' => "throttle",
311 'maxRequestsCrawlers' => "960",
312 'maxRequestsCrawlers_action' => "throttle",
313 'maxRequestsHumans' => "30",
314 'maxRequestsHumans_action' => "block",
315 'max404Crawlers' => "30",
316 'max404Crawlers_action' => "block",
317 'max404Humans' => "60",
318 'max404Humans_action' => "block",
319 'maxScanHits' => "10",
320 'maxScanHits_action' => "block",
321 'blockedTime' => "7200"
322 )
323 )
324 );
325 public static function setDefaults(){
326 foreach(self::$securityLevels[2]['checkboxes'] as $key => $val){
327 if(self::get($key) === false){
328 self::set($key, $val ? '1' : '0');
329 }
330 }
331 foreach(self::$securityLevels[2]['otherParams'] as $key => $val){
332 if(self::get($key) === false){
333 self::set($key, $val);
334 }
335 }
336 self::set('encKey', substr(wfUtils::bigRandomHex(),0 ,16) );
337 if(self::get('maxMem', false) === false ){
338 self::set('maxMem', '256');
339 }
340 if(self::get('other_scanOutside', false) === false){
341 self::set('other_scanOutside', 0);
342 }
343 }
344 public static function parseOptions(){
345 $ret = array();
346 foreach(self::$securityLevels[2]['checkboxes'] as $key => $val){ //value is not used. We just need the keys for validation
347 $ret[$key] = isset($_POST[$key]) ? '1' : '0';
348 }
349 foreach(self::$securityLevels[2]['otherParams'] as $key => $val){
350 if(isset($_POST[$key])){
351 $ret[$key] = $_POST[$key];
352 } else {
353 error_log("Missing options param \"$key\" when parsing parameters.");
354 }
355 }
356 /* for debugging only:
357 foreach($_POST as $key => $val){
358 if($key != 'action' && $key != 'nonce' && (! array_key_exists($key, self::$checkboxes)) && (! array_key_exists($key, self::$otherParams)) ){
359 error_log("Unrecognized option: $key");
360 }
361 }
362 */
363 return $ret;
364 }
365 public static function setArray($arr){
366 foreach($arr as $key => $val){
367 self::set($key, $val);
368 }
369 }
370 public static function clearCache(){
371 self::$cache = array();
372 }
373 public static function getHTML($key){
374 return htmlspecialchars(self::get($key));
375 }
376 public static function set($key, $val){
377 if(is_array($val)){
378 $msg = "wfConfig::set() got an array as second param with key: $key and value: " . var_export($val, true);
379 wordfence::status(1, 'error', $msg);
380 return;
381 }
382
383 self::getDB()->query("insert into " . self::table() . " (name, val) values ('%s', '%s') ON DUPLICATE KEY UPDATE val='%s'", $key, $val, $val);
384 self::$cache[$key] = $val;
385 }
386 public static function get($key, $default = false){
387 if(! isset(self::$cache[$key])){
388 $val = self::getDB()->querySingle("select val from " . self::table() . " where name='%s'", $key);
389 if(isset($val)){
390 self::$cache[$key] = $val;
391 } else {
392 self::$cache[$key] = $default;
393 }
394 }
395 return self::$cache[$key];
396 }
397 public static function get_ser($key, $default, $canUseDisk = false){ //When using disk, reading a value deletes it.
398 //If we can use disk, check if there are any values stored on disk first and read them instead of the DB if there are values
399 if($canUseDisk){
400 $filename = 'wordfence_tmpfile_' . $key . '.php';
401 $dir = self::getTempDir();
402 if($dir){
403 $obj = false;
404 $foundFiles = false;
405 $fullFile = $dir . $filename;
406 if(file_exists($fullFile)){
407 wordfence::status(4, 'info', "Loading serialized data from file $fullFile");
408 $obj = unserialize(substr(file_get_contents($fullFile), strlen(self::$tmpFileHeader))); //Strip off security header and unserialize
409 if(! $obj){
410 wordfence::status(2, 'error', "Could not unserialize file $fullFile");
411 }
412 self::deleteOldTempFile($fullFile);
413 }
414 if($obj){ //If we managed to deserialize something, clean ALL tmp dirs of this file and return obj
415 return $obj;
416 }
417 }
418 }
419 self::getDB()->reconnect();
420 //We do our own query handling here because we are dealing with some very big strings
421 $dbh = self::getDB()->getDBH();
422 $res = mysql_query("select val from " . self::table() . " where name='" . mysql_real_escape_string($key) . "'", $dbh);
423 $err = mysql_error();
424 if($err){
425 $trace=debug_backtrace();
426 $caller=array_shift($trace);
427 wordfence::status(2, 'error', "Wordfence DB error in " . $caller['file'] . " line " . $caller['line'] . ": $err");
428 return false;
429 }
430
431 if(mysql_num_rows($res) > 0){
432 $row = mysql_fetch_row($res);
433 return unserialize($row[0]);
434 }
435 return $default;
436 }
437 public static function set_ser($key, $val, $canUseDisk = false){
438 //We serialize some very big values so this is ultra-memory efficient. We don't make any copies of $val and don't use ON DUPLICATE KEY UPDATE
439 // because we would have to concatenate $val twice into the query which could also exceed max packet for the mysql server
440 self::getDB()->reconnect();
441 $dbh = self::getDB()->getDBH();
442 $serialized = serialize($val);
443 $tempFilename = 'wordfence_tmpfile_' . $key . '.php';
444 if((strlen($serialized) * 1.1) > self::getDB()->getMaxAllowedPacketBytes()){ //If it's greater than max_allowed_packet + 10% for escaping and SQL
445 if($canUseDisk){
446 $dir = self::getTempDir();
447 if($dir){
448 $fh = false;
449 $fullFile = $dir . $tempFilename;
450 self::deleteOldTempFile($fullFile);
451 $fh = fopen($fullFile, 'w');
452 if($fh){
453 wordfence::status(4, 'info', "Serialized data for $key is " . strlen($serialized) . " bytes and is greater than max_allowed packet so writing it to disk file: " . $fullFile);
454 } else {
455 wordfence::status(1, 'error', "Your database doesn't allow big packets so we have to use files to store temporary data and Wordfence can't find a place to write them. Either ask your admin to increase max_allowed_packet on your MySQL database, or make one of the following directories writable by your web server: " . implode(', ', $dirs));
456 return false;
457 }
458 fwrite($fh, self::$tmpFileHeader);
459 fwrite($fh, $serialized);
460 fclose($fh);
461 return true;
462 } else {
463 wordfence::status(1, 'error', "Wordfence tried to save a variable with name '$key' and your database max_allowed_packet is set to be too small. We then tried to save it to disk, but you don't have a temporary directory that is writable. You can fix this by making the /wp-content/plugins/wordfence/tmp/ directory writable by your web server. Or by increasing your max_allowed_packet configuration variable in your mysql database.");
464 return false;
465 }
466
467 } else {
468 wordfence::status(1, 'error', "Wordfence tried to save a variable with name '$key' and your database max_allowed_packet is set to be too small. This particular variable can't be saved to disk. Please ask your administrator to increase max_allowed_packet and also report this in the Wordfence forums because it may be a bug. Thanks.");
469 return false;
470 }
471 } else {
472 //Delete temp files on disk or else the DB will be written to but get_ser will see files on disk and read them instead
473 $tempDir = self::getTempDir();
474 if($tempDir){
475 self::deleteOldTempFile($tempDir . $tempFilename);
476 }
477 $exists = self::getDB()->querySingle("select name from " . self::table() . " where name='%s'", $key);
478 if($exists){
479 $res = mysql_query("update " . self::table() . " set val='" . mysql_real_escape_string($serialized) . "' where name='" . mysql_real_escape_string($key) . "'", $dbh);
480 } else {
481 $res = mysql_query("insert IGNORE into " . self::table() . " (name, val) values ('" . mysql_real_escape_string($key) . "', '" . mysql_real_escape_string($serialized) . "')", $dbh);
482 }
483 $err = mysql_error();
484 if($err){
485 $trace=debug_backtrace();
486 $caller=array_shift($trace);
487 wordfence::status(2, 'error', "Wordfence DB error in " . $caller['file'] . " line " . $caller['line'] . ": $err");
488 return false;
489 }
490 }
491 return true;
492 }
493 private static function deleteOldTempFile($filename){
494 if(file_exists($filename)){
495 unlink($filename);
496 }
497 }
498 private static function getTempDir(){
499 if(! self::$tmpDirCache){
500 $dirs = array(wfUtils::getPluginBaseDir() . 'wordfence/tmp/', sys_get_temp_dir(), ABSPATH . 'wp-content/uploads/');
501 $finalDir = 'notmp';
502 wfUtils::errorsOff();
503 foreach($dirs as $dir){
504 $dir = rtrim($dir, '/') . '/';
505 $fh = @fopen($dir . 'wftmptest.txt', 'w');
506 if(! $fh){ continue; }
507 $bytes = @fwrite($fh, 'test');
508 if($bytes != 4){ @fclose($fh); continue; }
509 @fclose($fh);
510 if(! @unlink($dir . 'wftmptest.txt')){ continue; }
511 $finalDir = $dir;
512 break;
513 }
514 wfUtils::errorsOn();
515 self::$tmpDirCache = $finalDir;
516 }
517 if(self::$tmpDirCache == 'notmp'){
518 return false;
519 } else {
520 return self::$tmpDirCache;
521 }
522 }
523 public static function f($key){
524 echo esc_attr(self::get($key));
525 }
526 public static function cb($key){
527 if(self::get($key)){
528 echo ' checked ';
529 }
530 }
531 public static function sel($key, $val, $isDefault = false){
532 if((! self::get($key)) && $isDefault){ echo ' selected '; }
533 if(self::get($key) == $val){ echo ' selected '; }
534 }
535 public static function getArray(){
536 $ret = array();
537 $q = self::getDB()->query("select name, val from " . self::table());
538 while($row = mysql_fetch_assoc($q)){
539 self::$cache[$row['name']] = $row['val'];
540 }
541 return self::$cache;
542 }
543 private static function getDB(){
544 if(! self::$DB){
545 self::$DB = new wfDB();
546 }
547 return self::$DB;
548 }
549 private static function table(){
550 if(! self::$table){
551 global $wpdb;
552 self::$table = $wpdb->base_prefix . 'wfConfig';
553 }
554 return self::$table;
555 }
556 public static function haveAlertEmails(){
557 $emails = self::getAlertEmails();
558 return sizeof($emails) > 0 ? true : false;
559 }
560 public static function getAlertEmails(){
561 $dat = explode(',', self::get('alertEmails'));
562 $emails = array();
563 foreach($dat as $email){
564 if(preg_match('/\@/', $email)){
565 $emails[] = trim($email);
566 }
567 }
568 return $emails;
569 }
570 public static function getAlertLevel(){
571 if(self::get('alertOn_warnings')){
572 return 2;
573 } else if(self::get('alertOn_critical')){
574 return 1;
575 } else {
576 return 0;
577 }
578 }
579 }
580 ?>
581