|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Upload; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
6
|
|
|
use Jaxon\Response\ResponseInterface; |
|
7
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
8
|
|
|
use Nyholm\Psr7\Stream; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function addslashes; |
|
12
|
|
|
use function array_reduce; |
|
13
|
|
|
use function json_encode; |
|
14
|
|
|
|
|
15
|
|
|
class UploadResponse implements ResponseInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use \Jaxon\Response\Traits\CommandTrait; |
|
18
|
|
|
use \Jaxon\Response\Traits\DomTrait; |
|
19
|
|
|
use \Jaxon\Response\Traits\JsTrait; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Psr17Factory |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $xPsr17Factory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The path to the uploaded file |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $sUploadedFile = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The error message |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $sErrorMessage = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The debug messages |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $aDebugMessages = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The constructor |
|
49
|
|
|
* |
|
50
|
|
|
* @param Psr17Factory $xPsr17Factory |
|
51
|
|
|
* @param string $sUploadedFile |
|
52
|
|
|
* @param string $sErrorMessage |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(Psr17Factory $xPsr17Factory, string $sUploadedFile, string $sErrorMessage = '') |
|
55
|
|
|
{ |
|
56
|
|
|
$this->xPsr17Factory = $xPsr17Factory; |
|
57
|
|
|
$this->sUploadedFile = $sUploadedFile; |
|
58
|
|
|
$this->sErrorMessage = $sErrorMessage; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritDoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getContentType(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return 'text/html'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the path to the uploaded file |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getUploadedFile(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->sUploadedFile; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the error message |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getErrorMessage(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->sErrorMessage; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @inheritDoc |
|
91
|
|
|
*/ |
|
92
|
|
|
public function debug(string $sMessage): ResponseInterface |
|
93
|
|
|
{ |
|
94
|
|
|
$this->aDebugMessages[] = $sMessage; |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritDoc |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getOutput(): string |
|
102
|
|
|
{ |
|
103
|
|
|
$sResult = json_encode(($this->sUploadedFile) ? |
|
104
|
|
|
['code' => 'success', 'upl' => $this->sUploadedFile] : |
|
105
|
|
|
['code' => 'error', 'msg' => $this->sErrorMessage]) . ';'; |
|
106
|
|
|
$sConsoleLog = array_reduce($this->aDebugMessages, function($sJsLog, $sMessage) { |
|
107
|
|
|
return "$sJsLog\n\t" . 'console.log("' . addslashes($sMessage) . '");'; |
|
108
|
|
|
}, ''); |
|
109
|
|
|
|
|
110
|
|
|
return ' |
|
111
|
|
|
<!DOCTYPE html> |
|
112
|
|
|
<html> |
|
113
|
|
|
<body> |
|
114
|
|
|
<h1>HTTP Upload for Jaxon</h1> |
|
115
|
|
|
</body> |
|
116
|
|
|
<script> |
|
117
|
|
|
res = ' . $sResult . $sConsoleLog . ' |
|
118
|
|
|
</script> |
|
119
|
|
|
</html>'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Convert this response to a PSR7 response object |
|
124
|
|
|
* |
|
125
|
|
|
* @return PsrResponseInterface |
|
126
|
|
|
*/ |
|
127
|
|
|
public function toPsr(): PsrResponseInterface |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->xPsr17Factory->createResponse(($this->sUploadedFile) ? 200 : 500) |
|
130
|
|
|
->withHeader('content-type', $this->getContentType()) |
|
131
|
|
|
->withBody(Stream::create($this->getOutput())); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Empty method, just to have the ResponseInterface methods implemented. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array $aAttributes |
|
138
|
|
|
* @param mixed $mData |
|
139
|
|
|
* |
|
140
|
|
|
* @return ResponseInterface |
|
141
|
|
|
*/ |
|
142
|
|
|
public function addCommand(array $aAttributes, $mData): ResponseInterface |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Empty method, just to have the ResponseInterface methods implemented. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $sName |
|
151
|
|
|
* @param array $aAttributes |
|
152
|
|
|
* @param mixed $mData |
|
153
|
|
|
* @param bool $bRemoveEmpty |
|
154
|
|
|
* |
|
155
|
|
|
* @return ResponseInterface |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function _addCommand(string $sName, array $aAttributes, |
|
|
|
|
|
|
158
|
|
|
$mData, bool $bRemoveEmpty = false): ResponseInterface |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Empty method, just to have the ResponseInterface methods implemented. |
|
165
|
|
|
* |
|
166
|
|
|
* @param ResponsePlugin $xPlugin |
|
167
|
|
|
* @param array $aAttributes |
|
168
|
|
|
* @param mixed $mData |
|
169
|
|
|
* |
|
170
|
|
|
* @return ResponseInterface |
|
171
|
|
|
*/ |
|
172
|
|
|
public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): ResponseInterface |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|