| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace HtaccessCapabilityTester\Testers\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \HtaccessCapabilityTester\HttpResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \HtaccessCapabilityTester\TestResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \HtaccessCapabilityTester\Testers\AbstractTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class for interpreting responses using a defined interpretation table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package    HtaccessCapabilityTester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author     Bjørn Rosell <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @since      Class available since 0.7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ResponseInterpreter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Parse status string (failure | success | inconclusive) to bool|null. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param  string  $statusString  (failure | success | inconclusive) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 71 |  |     private static function parseStatusString($statusString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 71 |  |         $status = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 71 |  |         switch ($statusString) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 71 |  |             case 'failure': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 46 |  |                 $status = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 46 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 68 |  |             case 'inconclusive': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 35 |  |                 $status = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 35 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 67 |  |             case 'success': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 67 |  |                 $status = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 67 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 71 |  |         return $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Interpret headers line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param  HttpResponse  $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param  string        $operator (has-key | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param  string        $fieldName  field name of the header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param  string        $fieldValue (optional) field value to look for. Only required when | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      *                             operator is "contains-key-value" or "not-contains-key-value" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return bool          true if the condition matches, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 28 |  |     private static function evaluateHeadersLine($response, $operator, $fieldName, $fieldValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 28 |  |         switch ($operator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 28 |  |             case 'contains-key': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |                 return $response->hasHeader($fieldName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 28 |  |             case 'not-contains-key': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 21 |  |                 return (!($response->hasHeader($fieldName))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 22 |  |             case 'contains-key-value': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 22 |  |                 return $response->hasHeaderValue($fieldName, $fieldValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 15 |  |             case 'not-contains-key-value': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 15 |  |                 return (!($response->hasHeaderValue($fieldName, $fieldValue))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Interpret string line (body or status-code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param  HttpResponse  $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param  string        $property ("body" or "status-code") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param  string        $operator  (is-empty | equals | not-equals | begins-with) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param  string        $arg1  (only required for some operators) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return bool          true if the condition matches, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 63 |  |     private static function evaluateStringLine($response, $property, $operator, $arg1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 63 |  |         $val = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 63 |  |         switch ($property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 63 |  |             case 'status-code': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 45 |  |                 $val = $response->statusCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 45 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 43 |  |             case 'body': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 43 |  |                 $val = $response->body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 43 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 63 |  |         switch ($operator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 63 |  |             case 'is-empty': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 return ($val == ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 63 |  |             case 'equals': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 59 |  |                 return ($val == $arg1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 36 |  |             case 'not-equals': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 33 |  |                 return ($val != $arg1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 26 |  |             case 'begins-with': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |                 return (strpos($val, $arg1) === 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 23 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Interpret line. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param HttpResponse    $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param array           $line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return  TestResult|null  If the condition matches, a TestResult is returned, otherwise null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     private static function interpretLine($response, $line) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 71 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         // ie: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         // ['inconclusive', 'body', 'is-empty'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         // ['failure', 'statusCode', 'equals', '500'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         // ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $status = self::parseStatusString($line[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 71 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if (!isset($line[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 71 |  |             return new TestResult($status, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 24 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $propertyToExamine = $line[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 71 |  |         $operator = $line[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 71 |  |         $arg1 = (isset($line[3]) ? $line[3] : ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 71 |  |         $arg2 = (isset($line[4]) ? $line[4] : ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 71 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if ($propertyToExamine == 'headers') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 71 |  |             $match = self::evaluateHeadersLine($response, $operator, $arg1, $arg2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 28 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $match = self::evaluateStringLine($response, $propertyToExamine, $operator, $arg1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 63 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if ($match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 71 |  |             $reason = $propertyToExamine . ' ' . $operator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 65 |  |             if (isset($line[3])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 65 |  |                 $reason .= ' "' . implode('" "', array_slice($line, 3)) . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 65 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             if (($propertyToExamine == 'status-code') && ($operator == 'not-equals') && (gettype($val) == 'string')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 $reason .= ' - it was: ' . $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             }*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return new TestResult($status, $reason); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 65 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 47 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * Interpret a response using an interpretation table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param HttpResponse    $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @param array           $interpretationTable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return TestResult   If there is no match, the test result will have status = false and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      *                      info = "no-match". | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     public static function interpret($response, $interpretationTable) | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 71 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         foreach ($interpretationTable as $i => $line) { | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 71 |  |             $testResult = self::interpretLine($response, $line); | 
            
                                                                        
                            
            
                                    
            
            
                | 162 | 71 |  |             if (!is_null($testResult)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 | 71 |  |                 return $testResult; | 
            
                                                                        
                            
            
                                    
            
            
                | 164 | 70 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |         return new TestResult(null, 'no-match'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 29 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  |