| 1 |
<?php |
| 2 |
|
| 3 |
namespace HtaccessCapabilityTester\Testers; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class for testing if rewriting works at the tested location. |
| 7 |
* |
| 8 |
* The tester reports success when: |
| 9 |
* - a rewrite is proven to be working |
| 10 |
* |
| 11 |
* The tester reports failure when: |
| 12 |
* - Server does not have mod_rewrite installed |
| 13 |
* - Server is set up to ignore .htaccess files in the directory |
| 14 |
* - Server disallows any the following directives in the directory: RewriteEngine, Rewrite, IfModule |
| 15 |
* (if disallowed, the result is either a 500 Internal Server Error or that the directive is |
| 16 |
* ignored, depending on whether Nonfatal is set) |
| 17 |
* - The request results in a 500 Internal Server Error due to another problem than a disallowed |
| 18 |
* directive (this is, there is a risk for a false negative) |
| 19 |
* |
| 20 |
* The test works by creating an .htaccess which redirects requests to "0.txt" |
| 21 |
* to "1.txt" and then requesting "0.txt". |
| 22 |
* |
| 23 |
* Notes: |
| 24 |
* - The test might result in the following being written to the error log: |
| 25 |
* "RewriteEngine not allowed here" |
| 26 |
* - We are not redirecting to a php, because that would additionally require phps |
| 27 |
* to be run in that directory |
| 28 |
* - We are wrapping the .htaccess directives in a "<IfModule mod_rewrite.c>" and therefore this test |
| 29 |
* also relies on the IfModule directive being allowed. It probably usually is, as it is harmless. |
| 30 |
* Also, it is good practice to use it, so in most cases it is good that this is checked |
| 31 |
* too. Actually, the <IfModule> wrap isn't neccessary for our test to work, as the test |
| 32 |
* identifies a 500 Internal Error as test failure. However, not having the wrap would |
| 33 |
* cause the test to generate an entry in the error log when mod_rewrite isn't installed |
| 34 |
* (regardless if overrides are configured to Nonfatal or not): |
| 35 |
* "Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included |
| 36 |
* in the server configuration" |
| 37 |
* |
| 38 |
* @package HtaccessCapabilityTester |
| 39 |
* @author Bjørn Rosell <[email protected]> |
| 40 |
* @since Class available since 0.7 |
| 41 |
*/ |
| 42 |
class RewriteTester extends CustomTester |
| 43 |
{ |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function __construct() |
| 51 |
{ |
| 52 |
$htaccessFile = <<<'EOD' |
| 53 |
# Testing for mod_rewrite |
| 54 |
# ----------------------- |
| 55 |
# If mod_rewrite is enabled, redirect to 1.txt, which returns "1". |
| 56 |
# If mod_rewrite is disabled, the rewriting fails, and we end at 0.txt, which returns "0". |
| 57 |
# |
| 58 |
# Notes: |
| 59 |
# - We are not redirecting to a php, because that would additionally require phps |
| 60 |
# to be run in that directory |
| 61 |
# - We are wrapping it in a "<IfModule mod_rewrite.c>" and therefore this test also relies |
| 62 |
# on the IfModule directive being allowed. It probably usually is, as it is harmless. |
| 63 |
# Also, it is good practice to use it, so in most cases it is good that this is checked |
| 64 |
# too. Actually, the <IfModule> wrap isn't neccessary for our test to work, as the test |
| 65 |
# identifies a 500 Internal Error as test failure. However, not having the wrap would |
| 66 |
# cause the test to generate an entry in the error log when mod_rewrite isn't installed |
| 67 |
# (regardless if configured to Nonfatal or not): "Invalid command 'RewriteEngine', perhaps |
| 68 |
# misspelled or defined by a module not included |
| 69 |
# in the server configuration" |
| 70 |
|
| 71 |
<IfModule mod_rewrite.c> |
| 72 |
RewriteEngine On |
| 73 |
RewriteRule ^0\.txt$ 1\.txt [L] |
| 74 |
</IfModule> |
| 75 |
EOD; |
| 76 |
|
| 77 |
$test = [ |
| 78 |
'subdir' => 'rewrite', |
| 79 |
'files' => [ |
| 80 |
['.htaccess', $htaccessFile], |
| 81 |
['0.txt', "0"], |
| 82 |
['1.txt', "1"] |
| 83 |
], |
| 84 |
'request' => '0.txt', |
| 85 |
'interpretation' => [ |
| 86 |
['success', 'body', 'equals', '1'], |
| 87 |
['failure', 'body', 'equals', '0'], |
| 88 |
] |
| 89 |
]; |
| 90 |
|
| 91 |
parent::__construct($test); |
| 92 |
} |
| 93 |
} |
| 94 |
|