@@ -163,6 +163,9 @@ |
||
| 163 | 163 | return trim($row,"\r\n"); |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | + /** |
|
| 167 | + * @param resource $stream |
|
| 168 | + */ |
|
| 166 | 169 | protected function parseParts($stream, array $part): array |
| 167 | 170 | { |
| 168 | 171 | while (!feof($stream)) { |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | public function __construct(array $allowedHeaders = array(), array $removeHeaders = array()) |
| 34 | 34 | { |
| 35 | - $this->contentDecoder = new ContentDecoder (); |
|
| 36 | - $this->headerDecoder = new HeaderDecoder (); |
|
| 35 | + $this->contentDecoder = new ContentDecoder(); |
|
| 36 | + $this->headerDecoder = new HeaderDecoder(); |
|
| 37 | 37 | |
| 38 | 38 | $this->allowedHeaders = array_merge($this->allowedHeaders, $allowedHeaders); |
| 39 | 39 | $this->removeHeaders = array_merge($this->removeHeaders, $removeHeaders); |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $partHeaders = $this->extractHeaders($stream); |
| 89 | 89 | |
| 90 | 90 | $filteredHeaders = $this->filterHeaders($partHeaders); |
| 91 | - $part=array( |
|
| 91 | + $part = array( |
|
| 92 | 92 | "type" => $this->extractValueHeader($this->getContentType($partHeaders)), |
| 93 | 93 | "headers" => $partHeaders, |
| 94 | 94 | "body" => '', |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | $parts = $this->parseParts($stream, $part); |
| 100 | 100 | |
| 101 | 101 | if (!$message) { |
| 102 | - $message = new \Swift_Message (); |
|
| 102 | + $message = new \Swift_Message(); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | $headers = $this->createHeadersSet($filteredHeaders); |
@@ -119,25 +119,25 @@ discard block |
||
| 119 | 119 | $headers = array(); |
| 120 | 120 | $hName = null; |
| 121 | 121 | while (!feof($stream)) { |
| 122 | - $row = $this->getCurrentLine($stream,false); |
|
| 122 | + $row = $this->getCurrentLine($stream, false); |
|
| 123 | 123 | if ($row == "") { |
| 124 | 124 | break; |
| 125 | 125 | } |
| 126 | - $row=$this->getCurrentLine($stream); |
|
| 126 | + $row = $this->getCurrentLine($stream); |
|
| 127 | 127 | if (preg_match('/^([a-z0-9\-]+)\s*:(.*)/i', $row, $mch)) { |
| 128 | - $hName = strtolower($mch[1]); |
|
| 128 | + $hName = strtolower($mch[ 1 ]); |
|
| 129 | 129 | if (!in_array($hName, array("content-type", "content-transfer-encoding"))) { |
| 130 | - $hName = $mch[1]; |
|
| 130 | + $hName = $mch[ 1 ]; |
|
| 131 | 131 | } |
| 132 | - $row = $mch[2]; |
|
| 132 | + $row = $mch[ 2 ]; |
|
| 133 | 133 | } |
| 134 | 134 | if (empty($hName)) { |
| 135 | 135 | continue; |
| 136 | 136 | } |
| 137 | - $headers[$hName][] = trim($row); |
|
| 137 | + $headers[ $hName ][ ] = trim($row); |
|
| 138 | 138 | } |
| 139 | 139 | foreach ($headers as $header => $values) { |
| 140 | - $headers[$header] = $this->headerDecoder->decode(trim(implode(" ", $values))); |
|
| 140 | + $headers[ $header ] = $this->headerDecoder->decode(trim(implode(" ", $values))); |
|
| 141 | 141 | } |
| 142 | 142 | return $headers; |
| 143 | 143 | } |
@@ -146,30 +146,30 @@ discard block |
||
| 146 | 146 | { |
| 147 | 147 | foreach ($headers as $header => $values) { |
| 148 | 148 | if (in_array(strtolower($header), $this->removeHeaders) && !in_array(strtolower($header), $this->allowedHeaders)) { |
| 149 | - unset ($headers[$header]); |
|
| 149 | + unset ($headers[ $header ]); |
|
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | 152 | return $headers; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - public function getCurrentLine($stream, $auto_advance=true){ |
|
| 156 | - $row=null; |
|
| 157 | - if (!feof($stream)){ |
|
| 158 | - $pos=ftell($stream); |
|
| 155 | + public function getCurrentLine($stream, $auto_advance = true) { |
|
| 156 | + $row = null; |
|
| 157 | + if (!feof($stream)) { |
|
| 158 | + $pos = ftell($stream); |
|
| 159 | 159 | $row = fgets($stream); |
| 160 | - if(!$auto_advance) |
|
| 161 | - fseek($stream,$pos); |
|
| 160 | + if (!$auto_advance) |
|
| 161 | + fseek($stream, $pos); |
|
| 162 | 162 | } |
| 163 | - return trim($row,"\r\n"); |
|
| 163 | + return trim($row, "\r\n"); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | protected function parseParts($stream, array $part): array |
| 167 | 167 | { |
| 168 | 168 | while (!feof($stream)) { |
| 169 | - $line=$this->getCurrentLine($stream); |
|
| 170 | - if (strpos($part["type"],'multipart/') !== false) { |
|
| 171 | - $headerParts = $this->extractHeaderParts($this->getContentType($part['headers'])); |
|
| 172 | - if (empty($headerParts["boundary"])) { |
|
| 169 | + $line = $this->getCurrentLine($stream); |
|
| 170 | + if (strpos($part[ "type" ], 'multipart/') !== false) { |
|
| 171 | + $headerParts = $this->extractHeaderParts($this->getContentType($part[ 'headers' ])); |
|
| 172 | + if (empty($headerParts[ "boundary" ])) { |
|
| 173 | 173 | throw new InvalidMessageFormatException("The Content-Type header is not well formed, boundary is missing"); |
| 174 | 174 | } |
| 175 | 175 | |
@@ -186,10 +186,10 @@ discard block |
||
| 186 | 186 | "parts" => array() |
| 187 | 187 | ); |
| 188 | 188 | $child = $this->parseParts($stream, $child_parts); |
| 189 | - $part['parts'][]=$child; |
|
| 189 | + $part[ 'parts' ][ ] = $child; |
|
| 190 | 190 | } |
| 191 | 191 | } else { |
| 192 | - $part['body']=$this->parseContent($stream,$this->getTransferEncoding($part['headers'])); |
|
| 192 | + $part[ 'body' ] = $this->parseContent($stream, $this->getTransferEncoding($part[ 'headers' ])); |
|
| 193 | 193 | return $part; |
| 194 | 194 | } |
| 195 | 195 | } |
@@ -200,15 +200,15 @@ discard block |
||
| 200 | 200 | { |
| 201 | 201 | $contents = array(); |
| 202 | 202 | while (!feof($stream)) { |
| 203 | - $line = $this->getCurrentLine($stream,false); |
|
| 203 | + $line = $this->getCurrentLine($stream, false); |
|
| 204 | 204 | if (preg_match(self::REGEX_BOUNDARY, $line)) { |
| 205 | 205 | break; |
| 206 | 206 | } else { |
| 207 | - $contents[] = $this->getCurrentLine($stream,true); |
|
| 207 | + $contents[ ] = $this->getCurrentLine($stream, true); |
|
| 208 | 208 | } |
| 209 | 209 | } |
| 210 | 210 | //remove last newline ( part of boundary) |
| 211 | - if(end($contents) =="") |
|
| 211 | + if (end($contents) == "") |
|
| 212 | 212 | array_pop($contents); |
| 213 | 213 | return $this->contentDecoder->decode(implode(PHP_EOL, $contents), $encoding); |
| 214 | 214 | } |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | private function getContentType(array $partHeaders): string |
| 228 | 228 | { |
| 229 | 229 | if (array_key_exists('content-type', $partHeaders)) { |
| 230 | - return $partHeaders['content-type']; |
|
| 230 | + return $partHeaders[ 'content-type' ]; |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | return ''; |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | continue; |
| 259 | 259 | } |
| 260 | 260 | list ($k, $v) = explode("=", trim($pv), 2); |
| 261 | - $p[$k] = trim($v, '"'); |
|
| 261 | + $p[ $k ] = trim($v, '"'); |
|
| 262 | 262 | } |
| 263 | 263 | return $p; |
| 264 | 264 | } else { |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | private function getTransferEncoding(array $partHeaders): string |
| 271 | 271 | { |
| 272 | 272 | if (array_key_exists('content-transfer-encoding', $partHeaders)) { |
| 273 | - return $partHeaders['content-transfer-encoding']; |
|
| 273 | + return $partHeaders[ 'content-transfer-encoding' ]; |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | return ''; |
@@ -284,13 +284,13 @@ discard block |
||
| 284 | 284 | switch (strtolower($name)) { |
| 285 | 285 | case "content-type": |
| 286 | 286 | $parts = $this->extractHeaderParts($value); |
| 287 | - unset ($parts["boundary"]); |
|
| 287 | + unset ($parts[ "boundary" ]); |
|
| 288 | 288 | $headers->addParameterizedHeader($name, $this->extractValueHeader($value), $parts); |
| 289 | 289 | break; |
| 290 | 290 | case "return-path": |
| 291 | 291 | if (preg_match_all('/([a-z][a-z0-9_\-\.]*@[a-z0-9\.\-]*\.[a-z]{2,5})/i', $value, $mch)) { |
| 292 | - foreach ($mch[0] as $k => $mails) { |
|
| 293 | - $headers->addPathHeader($name, $mch[1][$k]); |
|
| 292 | + foreach ($mch[ 0 ] as $k => $mails) { |
|
| 293 | + $headers->addPathHeader($name, $mch[ 1 ][ $k ]); |
|
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | break; |
@@ -304,16 +304,16 @@ discard block |
||
| 304 | 304 | case "cc": |
| 305 | 305 | $adresses = array(); |
| 306 | 306 | if (preg_match_all('/(.*?)<([a-z][a-z0-9_\-\.]*@[a-z0-9\.\-]*\.[a-z]{2,5})>\s*[;,]*/i', $value, $mch)) { |
| 307 | - foreach ($mch[0] as $k => $mail) { |
|
| 308 | - if (!$mch[1][$k]) { |
|
| 309 | - $adresses[$mch[2][$k]] = trim($mch[2][$k]); |
|
| 307 | + foreach ($mch[ 0 ] as $k => $mail) { |
|
| 308 | + if (!$mch[ 1 ][ $k ]) { |
|
| 309 | + $adresses[ $mch[ 2 ][ $k ] ] = trim($mch[ 2 ][ $k ]); |
|
| 310 | 310 | } else { |
| 311 | - $adresses[$mch[2][$k]] = trim($mch[1][$k]); |
|
| 311 | + $adresses[ $mch[ 2 ][ $k ] ] = trim($mch[ 1 ][ $k ]); |
|
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | 314 | } elseif (preg_match_all('/([a-z][a-z0-9_\-\.]*@[a-z0-9\.\-]*\.[a-z]{2,5})/i', $value, $mch)) { |
| 315 | - foreach ($mch[0] as $k => $mails) { |
|
| 316 | - $adresses[$mch[1][$k]] = trim($mch[1][$k]); |
|
| 315 | + foreach ($mch[ 0 ] as $k => $mails) { |
|
| 316 | + $adresses[ $mch[ 1 ][ $k ] ] = trim($mch[ 1 ][ $k ]); |
|
| 317 | 317 | } |
| 318 | 318 | } |
| 319 | 319 | $headers->addMailboxHeader($name, $adresses); |
@@ -328,44 +328,44 @@ discard block |
||
| 328 | 328 | |
| 329 | 329 | protected function createMessage(array $message, \Swift_Mime_SimpleMimeEntity $entity): void |
| 330 | 330 | { |
| 331 | - if (stripos($message["type"], 'multipart/') !== false && !empty($message["parts"])) { |
|
| 331 | + if (stripos($message[ "type" ], 'multipart/') !== false && !empty($message[ "parts" ])) { |
|
| 332 | 332 | |
| 333 | - if (strpos($message["type"], '/alternative')) { |
|
| 333 | + if (strpos($message[ "type" ], '/alternative')) { |
|
| 334 | 334 | $nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_ALTERNATIVE; |
| 335 | - } elseif (strpos($message["type"], '/related')) { |
|
| 335 | + } elseif (strpos($message[ "type" ], '/related')) { |
|
| 336 | 336 | $nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_RELATED; |
| 337 | - } elseif (strpos($message["type"], '/mixed')) { |
|
| 337 | + } elseif (strpos($message[ "type" ], '/mixed')) { |
|
| 338 | 338 | $nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_MIXED; |
| 339 | 339 | } else { |
| 340 | 340 | $nestingLevel = \Swift_Mime_SimpleMimeEntity::LEVEL_TOP; |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | $childs = array(); |
| 344 | - foreach ($message["parts"] as $part) { |
|
| 344 | + foreach ($message[ "parts" ] as $part) { |
|
| 345 | 345 | |
| 346 | - $headers = $this->createHeadersSet($part["headers"]); |
|
| 347 | - $encoder = $this->getEncoder($this->getTransferEncoding($part["headers"])); |
|
| 346 | + $headers = $this->createHeadersSet($part[ "headers" ]); |
|
| 347 | + $encoder = $this->getEncoder($this->getTransferEncoding($part[ "headers" ])); |
|
| 348 | 348 | |
| 349 | - if (stripos($part["type"], 'multipart/') !== false) { |
|
| 350 | - $newEntity = new \Swift_Mime_MimePart ($headers, $encoder, $this->getCache(), $this->getIdGenertor()); |
|
| 349 | + if (stripos($part[ "type" ], 'multipart/') !== false) { |
|
| 350 | + $newEntity = new \Swift_Mime_MimePart($headers, $encoder, $this->getCache(), $this->getIdGenertor()); |
|
| 351 | 351 | } else { |
| 352 | - $newEntity = new \Swift_Mime_SimpleMimeEntity ($headers, $encoder, $this->getCache(), $this->getIdGenertor()); |
|
| 352 | + $newEntity = new \Swift_Mime_SimpleMimeEntity($headers, $encoder, $this->getCache(), $this->getIdGenertor()); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | $this->createMessage($part, $newEntity); |
| 356 | 356 | |
| 357 | - $ref = new \ReflectionObject ($newEntity); |
|
| 357 | + $ref = new \ReflectionObject($newEntity); |
|
| 358 | 358 | $m = $ref->getMethod('setNestingLevel'); |
| 359 | 359 | $m->setAccessible(true); |
| 360 | 360 | $m->invoke($newEntity, $nestingLevel); |
| 361 | 361 | |
| 362 | - $childs[] = $newEntity; |
|
| 362 | + $childs[ ] = $newEntity; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | - $entity->setContentType($part["type"]); |
|
| 365 | + $entity->setContentType($part[ "type" ]); |
|
| 366 | 366 | $entity->setChildren($childs); |
| 367 | 367 | } else { |
| 368 | - $entity->setBody($message["body"], $message["type"]); |
|
| 368 | + $entity->setBody($message[ "body" ], $message[ "type" ]); |
|
| 369 | 369 | } |
| 370 | 370 | } |
| 371 | 371 | |
@@ -157,8 +157,9 @@ discard block |
||
| 157 | 157 | if (!feof($stream)){ |
| 158 | 158 | $pos=ftell($stream); |
| 159 | 159 | $row = fgets($stream); |
| 160 | - if(!$auto_advance) |
|
| 161 | - fseek($stream,$pos); |
|
| 160 | + if(!$auto_advance) { |
|
| 161 | + fseek($stream,$pos); |
|
| 162 | + } |
|
| 162 | 163 | } |
| 163 | 164 | return trim($row,"\r\n"); |
| 164 | 165 | } |
@@ -208,8 +209,9 @@ discard block |
||
| 208 | 209 | } |
| 209 | 210 | } |
| 210 | 211 | //remove last newline ( part of boundary) |
| 211 | - if(end($contents) =="") |
|
| 212 | - array_pop($contents); |
|
| 212 | + if(end($contents) =="") { |
|
| 213 | + array_pop($contents); |
|
| 214 | + } |
|
| 213 | 215 | return $this->contentDecoder->decode(implode(PHP_EOL, $contents), $encoding); |
| 214 | 216 | } |
| 215 | 217 | |