Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| PassInfoFromRewriteToScriptThroughEnvTester | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace HtaccessCapabilityTester\Testers; |
| 4 | |
| 5 | /** |
| 6 | * Class for testing if an environment variable can be set in a rewrite rule and received in PHP. |
| 7 | * |
| 8 | * @package HtaccessCapabilityTester |
| 9 | * @author Bjørn Rosell <it@rosell.dk> |
| 10 | * @since Class available since 0.7 |
| 11 | */ |
| 12 | class PassInfoFromRewriteToScriptThroughEnvTester extends CustomTester |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * Constructor. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function __construct() |
| 21 | { |
| 22 | $htaccessFile = <<<'EOD' |
| 23 | <IfModule mod_rewrite.c> |
| 24 | |
| 25 | # Testing if we can pass environment variable from .htaccess to script in a RewriteRule |
| 26 | # We pass document root, because that can easily be checked by the script |
| 27 | |
| 28 | RewriteEngine On |
| 29 | RewriteRule ^test\.php$ - [E=PASSTHROUGHENV:%{DOCUMENT_ROOT},L] |
| 30 | |
| 31 | </IfModule> |
| 32 | EOD; |
| 33 | |
| 34 | $phpFile = <<<'EOD' |
| 35 | <?php |
| 36 | |
| 37 | /** |
| 38 | * Get environment variable set with mod_rewrite module |
| 39 | * Return false if the environment variable isn't found |
| 40 | */ |
| 41 | function getEnvPassedInRewriteRule($envName) { |
| 42 | // Environment variables passed through the REWRITE module have "REWRITE_" as a prefix |
| 43 | // (in Apache, not Litespeed, if I recall correctly). |
| 44 | // Multiple iterations causes multiple REWRITE_ prefixes, and we get many environment variables set. |
| 45 | // We simply look for an environment variable that ends with what we are looking for. |
| 46 | // (so make sure to make it unique) |
| 47 | $len = strlen($envName); |
| 48 | foreach ($_SERVER as $key => $item) { |
| 49 | if (substr($key, -$len) == $envName) { |
| 50 | return $item; |
| 51 | } |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | $result = getEnvPassedInRewriteRule('PASSTHROUGHENV'); |
| 57 | if ($result === false) { |
| 58 | echo '0'; |
| 59 | exit; |
| 60 | } |
| 61 | echo ($result == $_SERVER['DOCUMENT_ROOT'] ? '1' : '0'); |
| 62 | EOD; |
| 63 | |
| 64 | $test = [ |
| 65 | 'subdir' => 'pass-info-from-rewrite-to-script-through-env', |
| 66 | 'files' => [ |
| 67 | ['.htaccess', $htaccessFile], |
| 68 | ['test.php', $phpFile], |
| 69 | ], |
| 70 | 'request' => 'test.php', |
| 71 | 'interpretation' => [ |
| 72 | ['success', 'body', 'equals', '1'], |
| 73 | ['failure', 'body', 'equals', '0'], |
| 74 | ['inconclusive', 'body', 'begins-with', '<' . '?php'], |
| 75 | ['inconclusive'] |
| 76 | ] |
| 77 | ]; |
| 78 | |
| 79 | parent::__construct($test); |
| 80 | } |
| 81 | } |