forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_site_access.php
303 lines (280 loc) · 12.2 KB
/
admin_site_access.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// Restrict/allow site access based on IP address and user-agent string
//
// webtrees: Web based Family History software
// Copyright (C) 2013 webtrees development team.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
define('WT_SCRIPT_NAME', 'admin_site_access.php');
require './includes/session.php';
require WT_ROOT.'includes/functions/functions_edit.php';
$controller=new WT_Controller_Page();
$controller
->requireAdminLogin()
->addExternalJavascript(WT_JQUERY_DATATABLES_URL)
->addExternalJavascript(WT_JQUERY_JEDITABLE_URL)
->setPageTitle(WT_I18N::translate('Site access rules'));
$action = WT_Filter::get('action');
switch ($action) {
case 'delete':
$user_access_rule_id = WT_Filter::getInteger('site_access_rule_id');
WT_DB::prepare("DELETE FROM `##site_access_rule` WHERE site_access_rule_id=?")->execute(array($user_access_rule_id));
break;
case 'allow':
case 'deny':
case 'robot':
$user_access_rule_id = WT_Filter::getInteger('site_access_rule_id');
WT_DB::prepare("UPDATE `##site_access_rule` SET rule=? WHERE site_access_rule_id=?")->execute(array($action, $user_access_rule_id));
break;
case 'load_rules':
Zend_Session::writeClose();
// AJAX callback for datatables
$sql=
"SELECT SQL_CALC_FOUND_ROWS".
" INET_NTOA(ip_address_start), ip_address_start, INET_NTOA(ip_address_end), ip_address_end, user_agent_pattern, rule, comment, site_access_rule_id".
" FROM `##site_access_rule`".
" WHERE rule<>'unknown'";
$args=array();
$sSearch = WT_Filter::get('sSearch');
if ($sSearch) {
$sql.=
" AND (INET_ATON(?) BETWEEN ip_address_start AND ip_address_end".
" OR INET_NTOA(ip_address_start) LIKE CONCAT('%', ?, '%')".
" OR INET_NTOA(ip_address_end) LIKE CONCAT('%', ?, '%')".
" OR user_agent_pattern LIKE CONCAT('%', ?, '%')".
" OR comment LIKE CONCAT('%', ?, '%'))";
$args[]=$sSearch;
$args[]=$sSearch;
$args[]=$sSearch;
$args[]=$sSearch;
$args[]=$sSearch;
}
$iSortingCols = WT_Filter::getInteger('iSortingCols');
if ($iSortingCols) {
$sql.=" ORDER BY ";
for ($i=0; $i<$iSortingCols; ++$i) {
// Datatables numbers columns 0, 1, 2, ...
// MySQL numbers columns 1, 2, 3, ...
switch (WT_Filter::get('sSortDir_'.$i)) {
case 'asc':
$sql.=(1 + WT_Filter::getInteger('iSortCol_'.$i)).' ASC ';
break;
case 'desc':
$sql.=(1 + WT_Filter::getInteger('iSortCol_'.$i)).' DESC ';
break;
}
if ($i<$iSortingCols-1) {
$sql.=',';
}
}
} else {
$sql.=" ORDER BY updated DESC";
}
$iDisplayStart = WT_Filter::getInteger('iDisplayStart');
$iDisplayLength = WT_Filter::getInteger('iDisplayLength');
if ($iDisplayLength>0) {
$sql.=" LIMIT " . $iDisplayStart . ',' . $iDisplayLength;
}
// This becomes a JSON list, not a JSON array, so we need numeric keys.
$aaData=WT_DB::prepare($sql)->execute($args)->fetchAll(PDO::FETCH_NUM);
// Reformat the data for display
foreach ($aaData as &$row) {
$site_access_rule_id=$row[7];
$user_agent=$row[4];
$row[0]=edit_field_inline('site_access_rule-ip_address_start-'.$site_access_rule_id, $row[0]);
$row[2]=edit_field_inline('site_access_rule-ip_address_end-'.$site_access_rule_id, $row[2]);
$row[4]=edit_field_inline('site_access_rule-user_agent_pattern-'.$site_access_rule_id, $row[4]);
$row[5]=select_edit_control_inline('site_access_rule-rule-'.$site_access_rule_id, array(
'allow'=>/* I18N: An access rule - allow access to the site */ WT_I18N::translate('allow'),
'deny' =>/* I18N: An access rule - deny access to the site */ WT_I18N::translate('deny'),
'robot'=>/* I18N: http://en.wikipedia.org/wiki/Web_crawler */ WT_I18N::translate('robot'),
), null, $row[5]);
$row[6]=edit_field_inline('site_access_rule-comment-'.$site_access_rule_id, $row[6]);
$row[7]='<i class="icon-delete" onclick="if (confirm(\''.WT_Filter::escapeHtml(WT_I18N::translate('Are you sure you want to delete “%s”?', strip_tags($user_agent))).'\')) { document.location=\''.WT_SCRIPT_NAME.'?action=delete&site_access_rule_id='.$site_access_rule_id.'\'; }"></i>';
}
// Total filtered rows
$iTotalDisplayRecords=WT_DB::prepare("SELECT FOUND_ROWS()")->fetchColumn();
// Total unfiltered rows
$iTotalRecords=WT_DB::prepare("SELECT COUNT(*) FROM `##site_access_rule` WHERE rule<>'unknown'")->fetchColumn();
header('Content-type: application/json');
echo json_encode(array( // See http://www.datatables.net/usage/server-side
'sEcho' => WT_Filter::getInteger('sEcho'), // Always an integer
'iTotalRecords' => $iTotalRecords,
'iTotalDisplayRecords' => $iTotalDisplayRecords,
'aaData' => $aaData
));
exit;
case 'load_unknown':
Zend_Session::writeClose();
// AJAX callback for datatables
$sql=
"SELECT SQL_CALC_FOUND_ROWS".
" INET_NTOA(ip_address_start), ip_address_start, user_agent_pattern, site_access_rule_id".
" FROM `##site_access_rule`".
" WHERE rule='unknown'";
$args=array();
$sSearch = WT_Filter::get('sSearch');
if ($sSearch) {
$sql.=
" AND (INET_ATON(ip_address_start) LIKE CONCAT('%', ?, '%')".
" OR user_agent_pattern LIKE CONCAT('%', ?, '%'))";
$args[]=$sSearch;
$args[]=$sSearch;
}
$iSortingCols = WT_Filter::getInteger('iSortingCols');
if ($iSortingCols) {
$sql.=" ORDER BY ";
for ($i=0; $i<$iSortingCols; ++$i) {
// Datatables numbers columns 0, 1, 2, ...
// MySQL numbers columns 1, 2, 3, ...
switch (WT_Filter::get('sSortDir_'.$i)) {
case 'asc':
$sql.=(1 + WT_Filter::getInteger('iSortCol_'.$i)).' ASC ';
break;
case 'desc':
$sql.=(1 + WT_Filter::getInteger('iSortCol_'.$i)).' DESC ';
break;
}
if ($i<$iSortingCols-1) {
$sql.=',';
}
}
} else {
$sql.=" ORDER BY updated DESC";
}
$iDisplayStart = WT_Filter::getInteger('iDisplayStart');
$iDisplayLength = WT_Filter::getInteger('iDisplayLength');
if ($iDisplayLength>0) {
$sql.=" LIMIT " . $iDisplayStart . ',' . $iDisplayLength;
}
// This becomes a JSON list, not a JSON array, so we need numeric keys.
$aaData=WT_DB::prepare($sql)->execute($args)->fetchAll(PDO::FETCH_NUM);
// Reformat the data for display
foreach ($aaData as &$row) {
$site_access_rule_id=$row[3];
$row[3]='<i class="icon-yes" onclick="document.location=\''.WT_SCRIPT_NAME.'?action=allow&site_access_rule_id='.$site_access_rule_id.'\';"></i>';
$row[4]='<i class="icon-yes" onclick="document.location=\''.WT_SCRIPT_NAME.'?action=deny&site_access_rule_id='.$site_access_rule_id.'\';"></i>';
$row[5]='<i class="icon-yes" onclick="document.location=\''.WT_SCRIPT_NAME.'?action=robot&site_access_rule_id='.$site_access_rule_id.'\';"></i>';
}
// Total filtered rows
$iTotalDisplayRecords=WT_DB::prepare("SELECT FOUND_ROWS()")->fetchColumn();
// Total unfiltered rows
$iTotalRecords=WT_DB::prepare("SELECT COUNT(*) FROM `##site_access_rule` WHERE rule='unknown'")->fetchColumn();
header('Content-type: application/json');
echo json_encode(array( // See http://www.datatables.net/usage/server-side
'sEcho' => WT_Filter::getInteger('sEcho'), // Always an integer
'iTotalRecords' => $iTotalRecords,
'iTotalDisplayRecords' => $iTotalDisplayRecords,
'aaData' => $aaData
));
exit;
}
$controller
->pageHeader()
->addInlineJavascript('
jQuery.fn.dataTableExt.oSort["unicode-asc" ]=function(a,b) {return a.replace(/<[^<]*>/, "").localeCompare(b.replace(/<[^<]*>/, ""))};
jQuery.fn.dataTableExt.oSort["unicode-desc"]=function(a,b) {return b.replace(/<[^<]*>/, "").localeCompare(a.replace(/<[^<]*>/, ""))};
jQuery("#site_access_rules").dataTable({
"sDom": \'<"H"pf<"dt-clear">irl>t<"F"pl>\',
"sAjaxSource": "'.WT_SERVER_NAME.WT_SCRIPT_PATH.WT_SCRIPT_NAME.'?action=load_rules",
"bServerSide":true,
'.WT_I18N::datatablesI18N().',
"bJQueryUI": true,
"bAutoWidth":false,
"bProcessing": true,
"sPaginationType": "full_numbers",
"bStateSave": true,
"iCookieDuration": 180,
"aoColumns": [
/* 0 ip_address_start */ {"iDataSort": 1, "sClass": "ip_address"},
/* 1 ip_address_start (sort) */ {"sType": "numeric", "bVisible": false},
/* 2 ip_address_end */ {"iDataSort": 3, "sClass": "ip_address"},
/* 3 ip_address_end (sort) */ {"sType": "numeric", "bVisible": false},
/* 4 user_agent_pattern */ {"sClass": "ua_string"},
/* 5 comment */ {},
/* 6 rule */ {},
/* 7 <delete> */ {"bSortable": false, "sClass": "center"}
],
"fnDrawCallback": function() {
// Our JSON responses include Javascript as well as HTML. This does not get
// executed, So extract it, and execute it
jQuery("#site_access_rules script").each(function() {
eval(this.text);
});
}
});
jQuery("#unknown_site_visitors").dataTable({
"sDom": \'<"H"pf<"dt-clear">irl>t<"F"pl>\',
"sAjaxSource": "'.WT_SERVER_NAME.WT_SCRIPT_PATH.WT_SCRIPT_NAME.'?action=load_unknown",
"bServerSide":true,
'.WT_I18N::datatablesI18N().',
"bJQueryUI": true,
"bAutoWidth":false,
"bProcessing": true,
"bStateSave": true,
"iCookieDuration": 180,
"sPaginationType": "full_numbers",
"aoColumns": [
/* 0 ip_address */ {"iDataSort": 1, "sClass": "ip_address"},
/* 0 ip_address (sort) */ {"sType": "numeric", "bVisible": false},
/* 1 user_agent_pattern */ {"sClass": "ua_string"},
/* 2 <allowed> */ {"bSortable": false, "sClass": "center"},
/* 3 <banned> */ {"bSortable": false, "sClass": "center"},
/* 4 <search-engine> */ {"bSortable": false, "sClass": "center"}
]
});
');
// Delete any "unknown" visitors that are now "known".
// This could happen every time we create/update a rule.
WT_DB::exec(
"DELETE unknown".
" FROM `##site_access_rule` AS unknown".
" JOIN `##site_access_rule` AS known ON (unknown.user_agent_pattern LIKE known.user_agent_pattern)".
" WHERE unknown.rule='unknown' AND known.rule<>'unknown'".
" AND unknown.ip_address_start BETWEEN known.ip_address_start AND known.ip_address_end"
);
?>
<h2><?php echo /* I18N: http://en.wikipedia.org/wiki/User_agent */ WT_I18N::translate('Restrict access to the site, using IP addresses and user-agent strings'); ?></h2>
<p><?php echo WT_I18N::translate('The following rules are used to decide whether a visitor is a human being (allow full access), a search-engine robot (allow restricted access) or an unwanted crawler (deny all access).'); ?></p>
<table id="site_access_rules" style="width:100%;">
<thead>
<tr>
<th><?php echo /* I18N [...] of a range of addresses */ WT_I18N::translate('Start IP address'); ?></th>
<th>-</th>
<th><?php echo /* I18N [...] of a range of addresses */ WT_I18N::translate('End IP address'); ?></th>
<th>-</th>
<th><?php echo /* I18N: http://en.wikipedia.org/wiki/User_agent_string */ WT_I18N::translate('User-agent string'); ?></th>
<th><?php echo /* I18N: noun */ WT_I18N::translate('Rule'); ?></th>
<th><?php echo WT_I18N::translate('Comment'); ?></th>
<th><?php echo WT_I18N::translate('Delete'); ?></th>
</tr>
</thead>
</table>
<p><?php echo WT_I18N::translate('The following visitors were not recognized, and were assumed to be search engines.'); ?></p>
<table id="unknown_site_visitors" style="width:100%;">
<thead>
<tr>
<th rowspan="2"><?php /* I18N: http://en.wikipedia.org/wiki/IP_address */ echo WT_I18N::translate('IP address'); ?></th>
<th rowspan="2">-</th>
<th rowspan="2"><?php echo WT_I18N::translate('User-agent string'); ?></th>
<th colspan="3"><?php echo WT_I18N::translate('Create a new rule'); ?></th>
</tr>
<tr>
<th><?php echo WT_I18N::translate('allow'); ?></th>
<th><?php echo WT_I18N::translate('deny'); ?></th>
<th><?php echo WT_I18N::translate('robot'); ?></th>
</tr>
</thead>
</table>