forked from CleanTalk/mediawiki-antispam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntispam.body.php
124 lines (97 loc) · 3.77 KB
/
Antispam.body.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
<?php
class CTBody {
/**
* Builds MD5 secret hash for JavaScript test (self::JSTest)
* @return string
*/
public static function getJSChallenge() {
global $wgCTAccessKey, $wgEmergencyContact;
return md5( $wgCTAccessKey . '+' . $wgEmergencyContact );
}
/**
* Tests hidden field falue for secret hash
* @return 0|1|null
*/
public static function JSTest() {
global $wgRequest, $wgCTHiddenFieldName;
$result = null;
$jsPostValue = $wgRequest->getVal( $wgCTHiddenFieldName );
if ( $jsPostValue ) {
$challenge = self::getJSChallenge();
if ( preg_match( "/$/", $jsPostValue ) ) {
$result = 1;
} else {
$result = 0;
}
}
return $result;
}
/**
* Calculate form fill time
* @return 0|1|null
*/
public static function SubmitTimeTest() {
global $wgCTSubmitTimeLabel;
$submit_time = null;
if (isset($_SESSION[$wgCTSubmitTimeLabel])) {
$submit_time = time() - (int) $_SESSION[$wgCTSubmitTimeLabel];
}
return $submit_time;
}
/**
* Adds hidden field to form for JavaScript test
* @return string
*/
public static function AddJSCode() {
global $wgCTHiddenFieldName, $wgCTHiddenFieldDefault, $wgCTExtName;
$ct_checkjs_key = CTBody::getJSChallenge();
$field_id = $wgCTHiddenFieldName . '_' . md5( rand( 0, 1000 ) );
$html = '
<input type="hidden" id="%s" name="%s" value="%s" />
<script type="text/javascript">
// <![CDATA[
var ct_input_name = \'%s\';
var ct_input_value = document.getElementById(ct_input_name).value;
var ct_input_challenge = \'%s\';
document.getElementById(ct_input_name).value = document.getElementById(ct_input_name).value.replace(ct_input_value, ct_input_challenge);
if (document.getElementById(ct_input_name).value == ct_input_value) {
document.getElementById(ct_input_name).value = ct_set_challenge(ct_input_challenge);
}
function ct_set_challenge(val) {
return val;
};
// ]]>
</script>
';
$html = sprintf( $html, $field_id, $wgCTHiddenFieldName, $wgCTHiddenFieldDefault, $field_id, $ct_checkjs_key );
$html .= '<noscript><p><b>Please enable JavaScript to pass anti-spam protection!</b><br />Here are the instructions how to enable JavaScript in your web browser <a href="http://www.enable-javascript.com" rel="nofollow" target="_blank">http://www.enable-javascript.com</a>.<br />' . $wgCTExtName . '.</p></noscript>';
return $html;
}
/**
* Sends email notificatioins to admins
* @return bool
*/
public static function SendAdminEmail( $title, $body ) {
global $wgCTExtName, $wgCTAdminAccountId, $wgCTDataStoreFile, $wgCTAdminNotificaionInteval;
if ( file_exists($wgCTDataStoreFile) ) {
$settings = file_get_contents ( $wgCTDataStoreFile );
if ( $settings ) {
$settings = json_decode($settings, true);
}
}
// Skip notification if permitted interval doesn't exhaust
if ( isset( $settings['lastAdminNotificaionSent'] ) && time() - $settings['lastAdminNotificaionSent'] < $wgCTAdminNotificaionInteval ) {
return false;
}
$u = User::newFromId( $wgCTAdminAccountId );
$status = $u->sendMail( $title , $body );
if ( $status->ok ) {
$fp = fopen( $wgCTDataStoreFile, 'w' ) or error_log( 'Could not open file:' . $wgCTDataStoreFile );
$settings['lastAdminNotificaionSent'] = time();
fwrite( $fp, json_encode($settings) );
fclose( $fp );
}
return $status->ok;
}
}
?>