MultipartFormDataParser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Libs;
  3. use Psr\Http\Message\ServerRequestInterface;
  4. class MultipartFormDataParser
  5. {
  6. public static function parse(ServerRequestInterface $request): array
  7. {
  8. $contentType = $request->getHeaderLine('Content-Type');
  9. if (!preg_match('/multipart\/form-data;\s*boundary=(?:(?:"([^"]+)")|([^;\s]+))/i', $contentType, $m)) {
  10. throw new \RuntimeException('Invalid multipart Content-Type');
  11. }
  12. $boundary = (isset($m[1]) && $m[1] !== '') ? $m[1] : ($m[2] ?? '');
  13. if ($boundary === '') {
  14. throw new \RuntimeException('Missing multipart boundary');
  15. }
  16. $rawBody = '';
  17. $stream = $request->getBody();
  18. $streamMeta = [];
  19. if (is_object($stream)) {
  20. if (method_exists($stream, 'isSeekable')) {
  21. $streamMeta['seekable'] = (bool)$stream->isSeekable();
  22. }
  23. if (method_exists($stream, 'isReadable')) {
  24. $streamMeta['readable'] = (bool)$stream->isReadable();
  25. }
  26. if (method_exists($stream, 'getSize')) {
  27. $streamMeta['size'] = $stream->getSize();
  28. }
  29. if (method_exists($stream, 'tell')) {
  30. try {
  31. $streamMeta['tell'] = $stream->tell();
  32. } catch (\Throwable $e) {
  33. }
  34. }
  35. }
  36. if (is_object($stream) && method_exists($stream, 'getContents')) {
  37. if (method_exists($stream, 'rewind')) {
  38. try {
  39. $stream->rewind();
  40. } catch (\Throwable $e) {
  41. }
  42. }
  43. $rawBody = (string)$stream->getContents();
  44. if (method_exists($stream, 'rewind')) {
  45. try {
  46. $stream->rewind();
  47. } catch (\Throwable $e) {
  48. }
  49. }
  50. } else {
  51. $rawBody = (string)$stream;
  52. }
  53. if ($rawBody === '') {
  54. $rawBody = (string)file_get_contents('php://input');
  55. }
  56. $delimiter = "--" . $boundary;
  57. $parts = explode($delimiter, $rawBody);
  58. $fields = [];
  59. $files = [];
  60. $meta = [
  61. 'content_type' => $contentType,
  62. 'boundary' => $boundary,
  63. 'stream' => $streamMeta,
  64. 'raw_length' => strlen($rawBody),
  65. 'parts_count' => count($parts),
  66. 'detected_parts' => [],
  67. ];
  68. foreach ($parts as $part) {
  69. $part = ltrim($part, "\r\n\n");
  70. $part = rtrim($part, "\r\n\n");
  71. if ($part === '' || $part === '--') {
  72. continue;
  73. }
  74. $split = preg_split("/\r?\n\r?\n/", $part, 2);
  75. if (!$split || count($split) !== 2) {
  76. continue;
  77. }
  78. [$rawHeaders, $body] = $split;
  79. $headers = self::parseHeaders($rawHeaders);
  80. $cd = $headers['content-disposition'] ?? '';
  81. if (!preg_match('/name="([^"]+)"/i', $cd, $nm)) {
  82. continue;
  83. }
  84. $name = $nm[1];
  85. $filename = null;
  86. if (preg_match('/filename="([^"]*)"/i', $cd, $fm)) {
  87. $filename = $fm[1];
  88. }
  89. $meta['detected_parts'][] = [
  90. 'name' => $name,
  91. 'has_filename' => $filename !== null && $filename !== '',
  92. 'filename' => ($filename !== null && $filename !== '') ? $filename : null,
  93. 'content_type' => $headers['content-type'] ?? null,
  94. ];
  95. $body = preg_replace("/\r\n\z/", '', $body);
  96. if ($filename !== null && $filename !== '') {
  97. $files[$name] = [
  98. 'field' => $name,
  99. 'filename' => $filename,
  100. 'content_type' => $headers['content-type'] ?? 'application/octet-stream',
  101. 'content' => $body,
  102. ];
  103. } else {
  104. $fields[$name] = $body;
  105. }
  106. }
  107. return ['fields' => $fields, 'files' => $files, 'meta' => $meta];
  108. }
  109. private static function parseHeaders(string $rawHeaders): array
  110. {
  111. $headers = [];
  112. foreach (preg_split('/\r?\n/', $rawHeaders) as $line) {
  113. $line = trim($line);
  114. if ($line === '' || strpos($line, ':') === false) {
  115. continue;
  116. }
  117. [$k, $v] = explode(':', $line, 2);
  118. $headers[strtolower(trim($k))] = trim($v);
  119. }
  120. return $headers;
  121. }
  122. }