| 1 |
<?php |
| 2 |
|
| 3 |
namespace Shellbox\Multipart; |
| 4 |
|
| 5 |
/** |
| 6 |
* Static utility functions for the multipart reader |
| 7 |
*/ |
| 8 |
class MultipartUtils { |
| 9 |
/** |
| 10 |
* Extract the boundary from a Content-Type string. If the Content-Type is |
| 11 |
* not multipart, returns false. |
| 12 |
* |
| 13 |
* This can be used to get a boundary when constructing a MultipartReader |
| 14 |
* from a request. |
| 15 |
* |
| 16 |
* @param string $contentType |
| 17 |
* @return bool|string |
| 18 |
*/ |
| 19 |
public static function extractBoundary( $contentType ) { |
| 20 |
if ( !preg_match( '!^multipart/[a-z]+ *; *(.*)$!i', $contentType, $m ) ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
$params = self::decodeParameters( $m[1] ); |
| 24 |
return $params['boundary'] ?? false; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Decode semicolon-separated parameters into an associative array. The |
| 29 |
* values may be quoted or unquoted. We try to follow the "parameter" |
| 30 |
* production in RFC 1341. |
| 31 |
* |
| 32 |
* @param string $input |
| 33 |
* @return array |
| 34 |
* @throws MultipartError |
| 35 |
*/ |
| 36 |
public static function decodeParameters( $input ) { |
| 37 |
$params = []; |
| 38 |
$parts = explode( ';', $input ); |
| 39 |
foreach ( $parts as $paramString ) { |
| 40 |
$paramParts = explode( '=', $paramString, 2 ); |
| 41 |
$paramName = strtolower( trim( $paramParts[0] ) ); |
| 42 |
if ( count( $paramParts ) < 2 ) { |
| 43 |
$paramValue = true; |
| 44 |
} else { |
| 45 |
$paramValue = self::decodeTokenOrQuotedString( $paramParts[1] ); |
| 46 |
} |
| 47 |
$params[$paramName] = $paramValue; |
| 48 |
} |
| 49 |
return $params; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Parse the "value" production from RFC 1341, which is either an unquoted |
| 54 |
* "token" or a quoted string with peculiar backslash escaping. |
| 55 |
* |
| 56 |
* @param string $input |
| 57 |
* @return string |
| 58 |
* @throws MultipartError |
| 59 |
*/ |
| 60 |
public static function decodeTokenOrQuotedString( $input ) { |
| 61 |
$input = trim( $input ); |
| 62 |
if ( $input === '' ) { |
| 63 |
return ''; |
| 64 |
} |
| 65 |
$tspecials = "()<>@,;:\\\"/[]?="; |
| 66 |
|
| 67 |
$result = ''; |
| 68 |
if ( $input[0] === '"' ) { |
| 69 |
$length = strlen( $input ); |
| 70 |
for ( $i = 1; $i < $length; $i++ ) { |
| 71 |
$char = $input[$i]; |
| 72 |
if ( $char === '\\' && $i < $length - 1 ) { |
| 73 |
$result .= $input[++$i]; |
| 74 |
} elseif ( $char === '"' ) { |
| 75 |
if ( $i !== $length - 1 ) { |
| 76 |
throw new MultipartError( "Invalid quoted string" ); |
| 77 |
} |
| 78 |
break; |
| 79 |
} else { |
| 80 |
$result .= $char; |
| 81 |
} |
| 82 |
} |
| 83 |
} else { |
| 84 |
$length = strlen( $input ); |
| 85 |
for ( $i = 0; $i < $length; $i++ ) { |
| 86 |
$char = $input[$i]; |
| 87 |
$ord = ord( $char ); |
| 88 |
if ( strpos( $tspecials, $char ) !== false |
| 89 |
|| $char === ' ' || $char === "\x7f" || $ord < 32 |
| 90 |
) { |
| 91 |
throw new MultipartError( "Invalid unquoted string" ); |
| 92 |
} |
| 93 |
$result .= $char; |
| 94 |
} |
| 95 |
} |
| 96 |
return $result; |
| 97 |
} |
| 98 |
} |