|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConfigToken\FileServer; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use ConfigToken\ConnectionSettings\ConnectionSettingsInterface; |
|
7
|
|
|
use ConfigToken\FileServer\Exception\RequestFormatException; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractFileServer implements FileServerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var ConnectionSettingsInterface */ |
|
12
|
|
|
protected $connectionSettings; |
|
13
|
|
|
|
|
14
|
|
|
function __construct(ConnectionSettingsInterface $connectionSettings) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
$this->setConnectionSettings($connectionSettings); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Check if the connection settings interface was set. |
|
21
|
|
|
* |
|
22
|
|
|
* @return boolean |
|
23
|
|
|
*/ |
|
24
|
|
|
public function hasConnectionSettings() |
|
25
|
|
|
{ |
|
26
|
|
|
return isset($this->connectionSettings); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the connection settings interface. |
|
31
|
|
|
* |
|
32
|
|
|
* @return ConnectionSettingsInterface|null |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getConnectionSettings() |
|
35
|
|
|
{ |
|
36
|
|
|
if (!$this->hasConnectionSettings()) { |
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
return $this->connectionSettings; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set the connection settings interface. |
|
44
|
|
|
* |
|
45
|
|
|
* @param ConnectionSettingsInterface $value The new value. |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setConnectionSettings($value) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->connectionSettings = $value; |
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the HTTP response code and optionally print it. |
|
56
|
|
|
* |
|
57
|
|
|
* @param integer|null $code The desired response code. Default is the previous one or 200. |
|
58
|
|
|
* @param bool|true $output If true, the response will be printed. |
|
59
|
|
|
* @param string $outputTemplate The output template to use when printing the response. |
|
60
|
|
|
* @return integer|null The response code. |
|
61
|
|
|
* @throws \Exception For unknown HTTP status codes. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setHttpResponseCode($code=null, $output=true, $outputTemplate='<h1>HTTP <code/> <message/></h1>') |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
if (!isset($code)) { |
|
66
|
|
|
if (function_exists('http_response_code')) { |
|
67
|
|
|
$code = http_response_code(); |
|
68
|
|
|
} else { |
|
69
|
|
|
$code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
switch ($code) { |
|
73
|
|
|
case 100: $text = 'Continue'; break; |
|
|
|
|
|
|
74
|
|
|
case 101: $text = 'Switching Protocols'; break; |
|
|
|
|
|
|
75
|
|
|
case 200: $text = 'OK'; break; |
|
|
|
|
|
|
76
|
|
|
case 201: $text = 'Created'; break; |
|
|
|
|
|
|
77
|
|
|
case 202: $text = 'Accepted'; break; |
|
|
|
|
|
|
78
|
|
|
case 203: $text = 'Non-Authoritative Information'; break; |
|
|
|
|
|
|
79
|
|
|
case 204: $text = 'No Content'; break; |
|
|
|
|
|
|
80
|
|
|
case 205: $text = 'Reset Content'; break; |
|
|
|
|
|
|
81
|
|
|
case 206: $text = 'Partial Content'; break; |
|
|
|
|
|
|
82
|
|
|
case 300: $text = 'Multiple Choices'; break; |
|
|
|
|
|
|
83
|
|
|
case 301: $text = 'Moved Permanently'; break; |
|
|
|
|
|
|
84
|
|
|
case 302: $text = 'Moved Temporarily'; break; |
|
|
|
|
|
|
85
|
|
|
case 303: $text = 'See Other'; break; |
|
|
|
|
|
|
86
|
|
|
case 304: $text = 'Not Modified'; break; |
|
|
|
|
|
|
87
|
|
|
case 305: $text = 'Use Proxy'; break; |
|
|
|
|
|
|
88
|
|
|
case 400: $text = 'Bad Request'; break; |
|
|
|
|
|
|
89
|
|
|
case 401: $text = 'Unauthorized'; break; |
|
|
|
|
|
|
90
|
|
|
case 402: $text = 'Payment Required'; break; |
|
|
|
|
|
|
91
|
|
|
case 403: $text = 'Forbidden'; break; |
|
|
|
|
|
|
92
|
|
|
case 404: $text = 'Not Found'; break; |
|
|
|
|
|
|
93
|
|
|
case 405: $text = 'Method Not Allowed'; break; |
|
|
|
|
|
|
94
|
|
|
case 406: $text = 'Not Acceptable'; break; |
|
|
|
|
|
|
95
|
|
|
case 407: $text = 'Proxy Authentication Required'; break; |
|
|
|
|
|
|
96
|
|
|
case 408: $text = 'Request Time-out'; break; |
|
|
|
|
|
|
97
|
|
|
case 409: $text = 'Conflict'; break; |
|
|
|
|
|
|
98
|
|
|
case 410: $text = 'Gone'; break; |
|
|
|
|
|
|
99
|
|
|
case 411: $text = 'Length Required'; break; |
|
|
|
|
|
|
100
|
|
|
case 412: $text = 'Precondition Failed'; break; |
|
|
|
|
|
|
101
|
|
|
case 413: $text = 'Request Entity Too Large'; break; |
|
|
|
|
|
|
102
|
|
|
case 414: $text = 'Request-URI Too Large'; break; |
|
|
|
|
|
|
103
|
|
|
case 415: $text = 'Unsupported Media Type'; break; |
|
|
|
|
|
|
104
|
|
|
case 500: $text = 'Internal Server Error'; break; |
|
|
|
|
|
|
105
|
|
|
case 501: $text = 'Not Implemented'; break; |
|
|
|
|
|
|
106
|
|
|
case 502: $text = 'Bad Gateway'; break; |
|
|
|
|
|
|
107
|
|
|
case 503: $text = 'Service Unavailable'; break; |
|
|
|
|
|
|
108
|
|
|
case 504: $text = 'Gateway Time-out'; break; |
|
|
|
|
|
|
109
|
|
|
case 505: $text = 'HTTP Version not supported'; break; |
|
|
|
|
|
|
110
|
|
|
default: |
|
111
|
|
|
throw new \Exception('Unknown http status code "' . htmlentities($code) . '"'); |
|
112
|
|
|
break; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
if (function_exists('http_response_code')) { |
|
115
|
|
|
http_response_code($code); |
|
116
|
|
|
} else { |
|
117
|
|
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); |
|
118
|
|
|
header($protocol . ' ' . $code . ' ' . $text); |
|
119
|
|
|
$GLOBALS['http_response_code'] = $code; |
|
120
|
|
|
} |
|
121
|
|
|
if ($output) { |
|
122
|
|
|
echo str_replace(array('<code/>', '<message/>'), array($code, $text), $outputTemplate); |
|
123
|
|
|
} |
|
124
|
|
|
return $code; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Override to specify a different request file key. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getRequestFileKey() |
|
133
|
|
|
{ |
|
134
|
|
|
return 'fileName'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Implement to return the default request or null if required to be specified. |
|
139
|
|
|
* |
|
140
|
|
|
* @return array|null |
|
141
|
|
|
*/ |
|
142
|
|
|
public abstract function getDefaultRequest(); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Implement to return the default request method or null if required to be specified. |
|
146
|
|
|
* |
|
147
|
|
|
* @return string|null |
|
148
|
|
|
*/ |
|
149
|
|
|
public abstract function getDefaultRequestMethod(); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Implement to validate the given request format. |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $request |
|
155
|
|
|
* @return boolean |
|
156
|
|
|
*/ |
|
157
|
|
|
public function isRequestFormatValid($request) { |
|
158
|
|
|
return isset($request[$this->getRequestFileKey()]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Implement to return array of allowed request methods. |
|
163
|
|
|
* |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
public abstract function getAllowedRequestMethods(); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Process either the current request or the request with the specified parameters and method. |
|
170
|
|
|
* |
|
171
|
|
|
* @param boolean $output If true, standard error messages will be printed. |
|
172
|
|
|
* @param array $request The request parameters. |
|
173
|
|
|
* @param string $requestMethod The request method. |
|
174
|
|
|
* @throws RequestFormatException |
|
175
|
|
|
* @return boolean |
|
176
|
|
|
*/ |
|
177
|
|
|
public function processRequest($output = true, $request = null, $requestMethod = null) |
|
178
|
|
|
{ |
|
179
|
|
|
if (!isset($request)) { |
|
180
|
|
|
$request = $this->getDefaultRequest(); |
|
181
|
|
|
} |
|
182
|
|
|
if (!isset($request)) { |
|
183
|
|
|
throw new RequestFormatException('Request not specified and no default available.'); |
|
184
|
|
|
} |
|
185
|
|
|
if (!isset($requestMethod)) { |
|
186
|
|
|
$requestMethod = $this->getDefaultRequestMethod(); |
|
187
|
|
|
} |
|
188
|
|
|
if (!isset($requestMethod)) { |
|
189
|
|
|
throw new RequestFormatException('Request method not specified and no default available.'); |
|
190
|
|
|
} |
|
191
|
|
|
$allowedMethods = $this->getAllowedRequestMethods(); |
|
192
|
|
|
if (!in_array($requestMethod, $allowedMethods)) { |
|
193
|
|
|
$allowedMethodsStr = implode(', ', $allowedMethods); |
|
194
|
|
|
header('Allow: ' . $allowedMethodsStr); |
|
195
|
|
|
$this->setHttpResponseCode(405, $output); // method not allowed |
|
196
|
|
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
if (!$this->isRequestFormatValid($request)) { |
|
199
|
|
|
$this->setHttpResponseCode(400, $output); // bad request |
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
return $this->serveFile($request[$this->getRequestFileKey()], $output); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Write the file attachment HTTP response headers. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $contentType |
|
209
|
|
|
* @param integer $fileSize |
|
210
|
|
|
* @param string $filePath |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function writeHeaders($contentType, $fileSize, $filePath) |
|
213
|
|
|
{ |
|
214
|
|
|
header('Cache-Control: private'); |
|
215
|
|
|
header('Content-Type: ' . $contentType); |
|
216
|
|
|
if ($fileSize !== false) { |
|
217
|
|
|
header('Content-Length: ' . $fileSize); |
|
218
|
|
|
} |
|
219
|
|
|
header('Content-Disposition: attachment; filename=' . basename($filePath)); |
|
220
|
|
|
} |
|
221
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.