| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Genkgo\Mail\Header; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | final class HeaderValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     private const PARSE_START = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private const PARSE_QUOTE = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array<string, HeaderValueParameter> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var array<string, bool> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $parametersForceNewLine = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $needsEncoding = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 167 |  |     public function __construct(string $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 167 |  |         $this->assertValid($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 164 |  |         $this->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 164 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param HeaderValueParameter $parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param bool $forceNewLine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return HeaderValue | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 64 |  |     public function withParameter(HeaderValueParameter $parameter, bool $forceNewLine = false): HeaderValue | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 64 |  |         $clone = clone $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 64 |  |         $clone->parameters[$parameter->getName()] = $parameter; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 64 |  |         $clone->parametersForceNewLine[$parameter->getName()] = $forceNewLine; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 64 |  |         return $clone; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 45 |  |     public function getRaw(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 45 |  |         return $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 140 |  |     public function __toString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 140 |  |         $value = $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 140 |  |         if ($this->needsEncoding) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 128 |  |             $value = new OptimalEncodedHeaderValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 140 |  |         $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 140 |  |         foreach ($this->parameters as $name => $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 54 |  |             $parameters[] = \sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 54 |  |                 ';%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 54 |  |                 empty($this->parametersForceNewLine[$name]) ? ' ' : "\r\n ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 54 |  |                 (string) $parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 140 |  |         return \implode('', \array_merge([$value], $parameters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 167 |  |     private function assertValid(string $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 167 |  |         if ($this->validate($value) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |             throw new \InvalidArgumentException('Invalid header value ' . $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 164 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 167 |  |     private function validate(string $value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 167 |  |         $total = \strlen($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 167 |  |         for ($i = 0; $i < $total; $i += 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 167 |  |             $ord = \ord($value[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 167 |  |             if ($ord === 10) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 167 |  |             if ($ord === 13) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 7 |  |                 if ($i + 2 >= $total) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 7 |  |                 $lf = \ord($value[$i + 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 7 |  |                 $sp = \ord($value[$i + 2]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 7 |  |                 if ($lf !== 10 || ! \in_array($sp, [9, 32], true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |                     return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 // skip over the LF following this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 5 |  |                 $i += 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 164 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @return HeaderValueParameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 32 |  |     public function getParameter(string $name): HeaderValueParameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 32 |  |         if (isset($this->parameters[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 32 |  |             return $this->parameters[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         throw new \UnexpectedValueException('No parameter with name ' . $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return HeaderValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 37 |  |     public static function fromEncodedString(string $value): HeaderValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 37 |  |         $headerValue = new self($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 37 |  |         $headerValue->needsEncoding = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 37 |  |         return $headerValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param string $headerValueAsString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @return HeaderValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 54 |  |     public static function fromString(string $headerValueAsString): HeaderValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 54 |  |         $values = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 54 |  |         $headerValueAsString = \str_replace("\r\n ", ' ', \trim($headerValueAsString)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 54 |  |         $length = \strlen($headerValueAsString) - 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 54 |  |         $n = -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 54 |  |         $state = self::PARSE_START; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 54 |  |         $escapeNext = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 54 |  |         $sequence = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 54 |  |         while ($n < $length) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 54 |  |             $n++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 54 |  |             $char = $headerValueAsString[$n]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 54 |  |             $sequence .= $char; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 54 |  |             if ($char === '\\') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 $escapeNext = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 54 |  |             if ($escapeNext) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 $escapeNext = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             switch ($state) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 54 |  |                 case self::PARSE_QUOTE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 6 |  |                     if ($char === '"') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 6 |  |                         $state = self::PARSE_START; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 6 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 54 |  |                     if ($char === '"') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 6 |  |                         $state = self::PARSE_QUOTE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 54 |  |                     if ($char === ';') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 26 |  |                         $values[] = \trim(\substr($sequence, 0, -1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 26 |  |                         $sequence = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 26 |  |                         $state = self::PARSE_START; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 54 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 54 |  |         $values[] = \trim($sequence); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 54 |  |         $primaryValue = $values[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 54 |  |         $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 54 |  |         foreach (\array_slice($values, 1) as $parameterString) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 26 |  |                 $parameter = HeaderValueParameter::fromString($parameterString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 25 |  |                 $parameters[$parameter->getName()] = $parameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 1 |  |             } catch (\InvalidArgumentException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 1 |  |                 $primaryValue .= '; ' . $parameterString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 54 |  |         $headerValue = new self($primaryValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 54 |  |         $headerValue->parameters = $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 54 |  |         return $headerValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 223 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 224 |  |  |  |