-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite_cond.php
395 lines (374 loc) · 16.8 KB
/
rewrite_cond.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Determine the kind of RewriteCond comparison required
* @param string $cond_pattern The 2nd argument on a RewriteCond line
* @return array Type constant indicating comparison required, pattern indicating
* what to compare against
*/
function process_cond_pattern($cond_pattern, $htaccess_line) {
$match = array();
if ($cond_pattern === "expr") {
output("ap_expr not supported yet", $htaccess_line, LOG_FAILURE);
return false;
} else if (substr($cond_pattern, 0, 1) == "-") {
$pref_3 = substr($cond_pattern, 0, 3);
$pref_2 = substr($cond_pattern, 0, 2);
$retval = false;
switch ($pref_3) {
case "-eq":
$retval = array("type" => COND_COMPARE_INT_EQ, "pattern" => substr($cond_pattern, 3));
break;
case "-ge":
$retval = array("type" => COND_COMPARE_INT_GTE, "pattern" => substr($cond_pattern, 3));
break;
case "-gt":
$retval = array("type" => COND_COMPARE_INT_GT, "pattern" => substr($cond_pattern, 3));
break;
case "-le":
$retval = array("type" => COND_COMPARE_INT_LTE, "pattern" => substr($cond_pattern, 3));
break;
case "-lt":
$retval = array("type" => COND_COMPARE_INT_LT, "pattern" => substr($cond_pattern, 3));
break;
default:
break;
}
if ($retval === false) {
switch ($pref_2) {
case "-d":
output("Can't determine existing directories", $htaccess_line, LOG_FAILURE);
break;
case "-f":
case "-F":
output("Can't determine existing files", $htaccess_line, LOG_FAILURE);
break;
case "-H":
case "-l":
case "-L":
output("Can't determine existing symbolic links", $htaccess_line, LOG_FAILURE);
break;
case "-s":
output("Can't determine file sizes", $htaccess_line, LOG_FAILURE);
break;
case "-U":
output("Can't do internal URL request check", $htaccess_line, LOG_FAILURE);
break;
case "-x":
output("Can't determine file permissions", $htaccess_line, LOG_FAILURE);
break;
default:
break;
}
}
if ($retval === false) {
output("Unknown condition", $htaccess_line, LOG_FAILURE);
}
return $retval;
} else if (preg_match("/^(<=?|>=?|=)(.*)$/", $cond_pattern, $match)) {
$retval = false;
switch($match[1]) {
case "<":
$retval = array("type" => COND_COMPARE_STR_LT, "pattern" => $match[2]);
break;
case ">":
$retval = array("type" => COND_COMPARE_STR_GT, "pattern" => $match[2]);
break;
case "=":
$retval = array("type" => COND_COMPARE_STR_EQ, "pattern" => $match[2]);
break;
case "<=":
$retval = array("type" => COND_COMPARE_STR_LTE, "pattern" => $match[2]);
break;
case ">=":
$retval = array("type" => COND_COMPARE_STR_GTE, "pattern" => $match[2]);
break;
}
return $retval;
} else {
return array("type" => COND_COMPARE_REGEX, "pattern" => $cond_pattern);
}
}
/**
* Some bit magic<br>TODO: handle whitespace
* @param string $flag_string The 3rd argument on RewriteCond
* @param int $htaccess_line Which line we're on
* @return int Bit flags indicating which options are set
*/
function parse_cond_flags($flag_string, $htaccess_line) {
$opts = FLAG_COND_NONE;
if (empty($flag_string)) {
return $opts;
}
$trim_flags = preg_replace("/(^\[|\]$)/", "", $flag_string);
$flags = explode(",", $trim_flags);
foreach($flags as $flag) {
switch ($flag) {
case 'NV':
$opts = $opts | FLAG_COND_NV;
output("No Vary flag... ignoring", $htaccess_line, LOG_COMMENT);
break;
case 'NC':
$opts = $opts | FLAG_COND_NC;
output("Case-insensitive flag", $htaccess_line, LOG_COMMENT);
break;
case 'OR':
$opts = $opts | FLAG_COND_OR;
output("OR flag", $htaccess_line, LOG_COMMENT);
break;
default:
output("Unknown flag: `$flag`", $htaccess_line, LOG_FAILURE);
break;
}
}
return $opts;
}
/**
* Evaluates a RewriteCond line
* @param string $test_string First param, the string to match against
* @param string $orig_cond_pattern Second param, the condition to match first param against
* @param string $flags Flags indicating case-insensitivity NC, of the OR logic flag (ignore NV flag)
* @param int $htaccess_line Which line we're on/to put output on
* @param array $rewrite_backreferences Backreferences to the following RewriteRule
* @param array $cond_backreferences Backreferences to the preceeding RewriteCond
* @param array $server_vars Server variables
* @return array|Boolean Array on success/regex match (contains "success" and "flags" keys), false on failure
*/
function interpret_cond($test_string, $orig_cond_pattern, $flags, $htaccess_line, $rewrite_backreferences, $cond_backreferences, $server_vars) {
// Step 1
$parsed_flags = parse_cond_flags($flags, $htaccess_line);
// Step 2
$expanded_test_string = expand_teststring($test_string, $rewrite_backreferences, $cond_backreferences, $htaccess_line, $server_vars);
if ($expanded_test_string === false) {
return false;
}
if (empty($expanded_test_string)) {
output("`$test_string` contains nothing", $htaccess_line, LOG_HELP);
} else {
output("`$test_string` contains `$expanded_test_string`", $htaccess_line, LOG_HELP);
}
// Step 3
$negative_match = substr($orig_cond_pattern, 0, 1) === "!";
if ($negative_match) {
output("Negative match", $htaccess_line, LOG_COMMENT);
$cond_pattern = substr($orig_cond_pattern, 1);
} else {
$cond_pattern = $orig_cond_pattern;
}
$pattern_type = process_cond_pattern($cond_pattern, $htaccess_line);
// Do the comparison
if ($pattern_type === false) {
return false;
} else if (is_array($pattern_type)) {
if ($parsed_flags & FLAG_COND_NC) {
$strcmp = strcasecmp($expanded_test_string, $pattern_type['pattern']);
} else {
$strcmp = strcmp($expanded_test_string, $pattern_type['pattern']);
}
$lt = (int)$expanded_test_string < (int)$pattern_type['pattern'];
$eq = (int)$expanded_test_string === (int)$pattern_type['pattern'];
$retval = false;
switch ($pattern_type["type"]) {
case COND_COMPARE_STR_LT:
if ($strcmp < 0) {
if ($negative_match) {
output("FAIL: string comparison `$expanded_test_string` < `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: string comparison `$expanded_test_string` < `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: string comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: string comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_STR_GT:
if ($strcmp > 0) {
if ($negative_match) {
output("FAIL: string comparison `$expanded_test_string` > `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: string comparison `$expanded_test_string` > `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: string comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: string comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_STR_EQ:
if ($strcmp === 0) {
if ($negative_match) {
output("FAIL: string comparison `$expanded_test_string` = `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: string comparison `$expanded_test_string` = `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: string comparison `$expanded_test_string` != `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: string comparison `$expanded_test_string` != `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_STR_LTE:
if ($strcmp <= 0) {
if ($negative_match) {
output("FAIL: string comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: string comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: string comparison `$expanded_test_string` > `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: string comparison `$expanded_test_string` > `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_STR_GTE:
if ($strcmp >= 0) {
if ($negative_match) {
output("FAIL: string comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: string comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: string comparison `$expanded_test_string` < `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: string comparison `$expanded_test_string` < `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_INT_EQ:
if ($eq) {
if ($negative_match) {
output("FAIL: integer comparison `$expanded_test_string` == `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: integer comparison `$expanded_test_string` == `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: integer comparison `$expanded_test_string` != `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: integer comparison `$expanded_test_string` != `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_INT_GT:
if ( ! $lt and ! $eq) {
if ($negative_match) {
output("FAIL: integer comparison `$expanded_test_string` > `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: integer comparison `$expanded_test_string` > `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: integer comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: integer comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_INT_GTE:
if ( ! $lt or $eq) {
if ($negative_match) {
output("FAIL: integer comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: integer comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: integer comparison `$expanded_test_string` < `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: integer comparison `$expanded_test_string` < `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_INT_LT:
if ($lt) {
if ($negative_match) {
output("FAIL: integer comparison `$expanded_test_string` < `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: integer comparison `$expanded_test_string` < `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: integer comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: integer comparison `$expanded_test_string` >= `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_INT_LTE:
if ($lt or $eq) {
if ($negative_match) {
output("FAIL: integer comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`, but we don't want it to be", $htaccess_line, LOG_FAILURE);
$retval = false;
} else {
output("PASS: integer comparison `$expanded_test_string` <= `{$pattern_type['pattern']}`", $htaccess_line, LOG_SUCCESS);
$retval = true;
}
} else {
if ($negative_match) {
output("PASS: integer comparison `$expanded_test_string` > `{$pattern_type['pattern']}`, and we don't want it to be", $htaccess_line, LOG_SUCCESS);
$retval = true;
} else {
output("FAIL: integer comparison `$expanded_test_string` > `{$pattern_type['pattern']}`", $htaccess_line, LOG_FAILURE);
$retval = false;
}
}
break;
case COND_COMPARE_REGEX:
$retval = regex_match($pattern_type['pattern'], $expanded_test_string, $negative_match, $parsed_flags & FLAG_COND_NC, $htaccess_line);
break;
default:
output("`$cond_pattern` not supported yet", $htaccess_line, LOG_FAILURE);
$retval = false;
break;
}
return array("success" => $retval, "flags" => $parsed_flags);
} else {
output("Unknown", $htaccess_line, LOG_FAILURE);
return false;
}
}