@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class AphrontMultipartPart extends Phobject {
4
5 private $headers = array();
6 private $value = '';
7
8 private $name;
9 private $filename;
10 private $tempFile;
11 private $byteSize = 0;
12
13 public function appendRawHeader($bytes) {
14 $parser = id(new AphrontHTTPHeaderParser())
15 ->parseRawHeader($bytes);
16
17 $header_name = $parser->getHeaderName();
18
19 $this->headers[] = array(
20 $header_name,
21 $parser->getHeaderContent(),
22 );
23
24 if (strtolower($header_name) === 'content-disposition') {
25 $pairs = $parser->getHeaderContentAsPairs();
26 foreach ($pairs as $pair) {
27 list($key, $value) = $pair;
28 switch ($key) {
29 case 'filename':
30 $this->filename = $value;
31 break;
32 case 'name':
33 $this->name = $value;
34 break;
35 }
36 }
37 }
38
39 return $this;
40 }
41
42 public function appendData($bytes) {
43 $this->byteSize += strlen($bytes);
44
45 if ($this->isVariable()) {
46 $this->value .= $bytes;
47 } else {
48 if (!$this->tempFile) {
49 $this->tempFile = new TempFile(getmypid().'.upload');
50 }
51 Filesystem::appendFile($this->tempFile, $bytes);
52 }
53
54 return $this;
55 }
56
57 public function isVariable() {
58 return ($this->filename === null);
59 }
60
61 public function getName() {
62 return $this->name;
63 }
64
65 public function getVariableValue() {
66 if (!$this->isVariable()) {
67 throw new Exception(pht('This part is not a variable!'));
68 }
69
70 return $this->value;
71 }
72
73 public function getPHPFileDictionary() {
74 if (!$this->tempFile) {
75 $this->appendData('');
76 }
77
78 $mime_type = 'application/octet-stream';
79 foreach ($this->headers as $header) {
80 list($name, $value) = $header;
81 if (strtolower($name) == 'content-type') {
82 $mime_type = $value;
83 break;
84 }
85 }
86
87 return array(
88 'name' => $this->filename,
89 'type' => $mime_type,
90 'tmp_name' => (string)$this->tempFile,
91 'error' => 0,
92 'size' => $this->byteSize,
93 );
94 }
95
96}