Skip to content

Commit cfa7474

Browse files
authored
Merge pull request joni2back#249 from durasj/php-local-bridge-updates
Php local bridge updates
2 parents 6f01ac8 + 3169103 commit cfa7474

File tree

5 files changed

+386
-366
lines changed

5 files changed

+386
-366
lines changed

bridges/php-local/LocalBridge/FileManagerApi.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* File Manager API Class
66
*
77
* Made for PHP Local filesystem bridge for angular-filemanager to handle file manipulations
8-
* @author Jakub Ďuraš <[email protected]>
8+
* @author Jakub Ďuraš <[email protected]>
99
*/
1010
class FileManagerApi
1111
{
@@ -28,7 +28,10 @@ public function postHandler($query, $request, $files)
2828
$t = $this->translate;
2929

3030
// Probably file upload
31-
if (!isset($request['action']) && (isset($_SERVER["CONTENT_TYPE"]) && strpos($_SERVER["CONTENT_TYPE"], 'multipart/form-data') !== false)) {
31+
if (!isset($request['action'])
32+
&& (isset($_SERVER["CONTENT_TYPE"])
33+
&& strpos($_SERVER["CONTENT_TYPE"], 'multipart/form-data') !== false)
34+
) {
3235
$uploaded = $this->uploadAction($request['destination'], $files);
3336
if ($uploaded === true) {
3437
$response = $this->simpleSuccessResponse();
@@ -57,7 +60,7 @@ public function postHandler($query, $request, $files)
5760
$renamed = $this->renameAction($request['item'], $request['newItemPath']);
5861
if ($renamed === true) {
5962
$response = $this->simpleSuccessResponse();
60-
} elseif ($renamed === 'notfound'){
63+
} elseif ($renamed === 'notfound') {
6164
$response = $this->simpleErrorResponse($t->file_not_found);
6265
} else {
6366
$response = $this->simpleErrorResponse($t->renaming_failed);
@@ -137,7 +140,11 @@ public function postHandler($query, $request, $files)
137140
break;
138141

139142
case 'compress':
140-
$compressed = $this->compressAction($request['items'], $request['destination'], $request['compressedFilename']);
143+
$compressed = $this->compressAction(
144+
$request['items'],
145+
$request['destination'],
146+
$request['compressedFilename']
147+
);
141148
if ($compressed === true) {
142149
$response = $this->simpleSuccessResponse();
143150
} else {
@@ -192,15 +199,17 @@ private function downloadAction($path)
192199
$file_name = basename($path);
193200
$path = $this->basePath . $path;
194201

195-
if (! file_exists($path)) {
202+
if (!file_exists($path)) {
196203
return false;
197204
}
198205

199206
$finfo = finfo_open(FILEINFO_MIME_TYPE);
200207
$mime_type = finfo_file($finfo, $path);
201208
finfo_close($finfo);
202209

203-
if (ob_get_level()) ob_end_clean();
210+
if (ob_get_level()) {
211+
ob_end_clean();
212+
}
204213

205214
header("Content-Disposition: attachment; filename=\"$file_name\"");
206215
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
@@ -218,7 +227,7 @@ private function uploadAction($path, $files)
218227

219228
foreach ($_FILES as $file) {
220229
$uploaded = move_uploaded_file(
221-
$file['tmp_name'],
230+
$file['tmp_name'],
222231
rtrim($path, '/') . '/' . $file['name']
223232
);
224233
if ($uploaded === false) {
@@ -227,16 +236,16 @@ private function uploadAction($path, $files)
227236
}
228237

229238
return true;
230-
}
239+
}
231240

232241
private function listAction($path)
233242
{
234243
$files = glob($this->basePath . rtrim($path, '/') . '/*');
235244

236-
$files = array_map(function($file){
245+
$files = array_map(function ($file) {
237246
$date = new \DateTime('@' . filemtime($file));
238247
return [
239-
'name' => utf8_encode(basename($file)),
248+
'name' => basename($file),
240249
'rights' => $this->parsePerms(fileperms($file)),
241250
'size' => filesize($file),
242251
'date' => $date->format('Y-m-d H:i:s'),
@@ -287,11 +296,11 @@ private function copyAction($oldPaths, $newPath)
287296
}
288297

289298
$copied = copy(
290-
$this->basePath . $oldPath,
299+
$this->basePath . $oldPath,
291300
$newPath . basename($oldPath)
292301
);
293302
if ($copied === false) {
294-
return false;
303+
return false;
295304
}
296305
}
297306

@@ -354,15 +363,17 @@ private function createFolderAction($path)
354363
private function changePermissionsAction($paths, $permissions, $recursive)
355364
{
356365
foreach ($paths as $path) {
357-
if (!file_exists($this->basePath . $path)) return 'missing';
366+
if (!file_exists($this->basePath . $path)) {
367+
return 'missing';
368+
}
358369

359370
if (is_dir($path) && $recursive === true) {
360371
$iterator = new RecursiveIteratorIterator(
361-
new RecursiveDirectoryIterator($path),
372+
new RecursiveDirectoryIterator($path),
362373
RecursiveIteratorIterator::SELF_FIRST
363374
);
364375

365-
foreach($iterator as $item) {
376+
foreach ($iterator as $item) {
366377
$changed = chmod($this->basePath . $item, octdec($permissions));
367378

368379
if ($changed === false) {
@@ -423,11 +434,11 @@ private function simpleErrorResponse($message)
423434
$response = new Response();
424435
$response
425436
->setStatus(500, 'Internal Server Error')
426-
->setData([
427-
'result' => [
437+
->setData([
438+
'result' => [
428439
'success' => false,
429440
'error' => $message
430-
]
441+
]
431442
]);
432443

433444
return $response;

bridges/php-local/LocalBridge/Response.php

Lines changed: 121 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,129 @@
33

44
/**
55
* Response class
6-
*
6+
*
77
* HTTP response to requests handled in REST API in callback
88
* Chaineable
9-
* @author Jakub Ďuraš <[email protected]>
9+
* @author Jakub Ďuraš <[email protected]>
1010
*/
1111
class Response
1212
{
13-
/**
14-
* @var integer
15-
*/
16-
private $statusCode = 200;
17-
18-
/**
19-
* @var string
20-
*/
21-
private $status = 'OK';
22-
23-
/**
24-
* @var array
25-
*/
26-
private $additionalHeaders = [];
27-
28-
/**
29-
* @var mixed
30-
*/
31-
private $data = NULL;
32-
33-
/**
34-
* Body which will be used instead of data if set
35-
* @var string
36-
*/
37-
private $body = NULL;
38-
39-
/**
40-
* HTTP Status according to http://tools.ietf.org/html/rfc7231
41-
* @param integer $statusCode
42-
* @param string $status without status code
43-
* @return object this
44-
*/
45-
public function setStatus ($statusCode, $status)
46-
{
47-
$this->statusCode = $statusCode;
48-
$this->status = $status;
49-
50-
return $this;
51-
}
52-
53-
/**
54-
* Add additional HTTP header
55-
* @param string $header
56-
* @return object this
57-
*/
58-
public function addHeaders ($header)
59-
{
60-
$this->additionalHeaders[] = $header;
61-
62-
return $this;
63-
}
64-
65-
/**
66-
* @param mixed $body used instead of data if set
67-
*/
68-
public function setBody ($body)
69-
{
70-
$this->body = $body;
71-
72-
return $this;
73-
}
74-
75-
/**
76-
* @param mixed $data any data which should be later encoded to body
77-
*/
78-
public function setData ($data)
79-
{
80-
$this->data = $data;
81-
82-
return $this;
83-
}
84-
85-
/**
86-
* @return mixed data which should be later encoded to body
87-
*/
88-
public function getData ()
89-
{
90-
return $this->data;
91-
}
92-
93-
/**
94-
* @return mixed used instead of data if set
95-
*/
96-
public function getBody ()
97-
{
98-
if ($this->body !== NULL) {
99-
return $this->body;
100-
}
101-
102-
if ($this->data === NULL) {
103-
return NULL;
104-
}
105-
106-
return json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
107-
}
108-
109-
/**
110-
* HTTP Status according to http://tools.ietf.org/html/rfc7231 in form of array
111-
* @return array associative, e.g. ['code' => 200, 'status' => 'OK']
112-
*/
113-
public function getStatus ()
114-
{
115-
return [
116-
'code' => $this->statusCode,
117-
'status' => $this->status
118-
];
119-
}
120-
121-
/**
122-
* @return array list of all headers including first status line
123-
*/
124-
public function getHeaders ()
125-
{
126-
return array_merge([$_SERVER['SERVER_PROTOCOL'].' '.$this->statusCode.' '.$this->status], $this->additionalHeaders);
127-
}
128-
}
13+
/**
14+
* @var integer
15+
*/
16+
private $statusCode = 200;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $status = 'OK';
22+
23+
/**
24+
* @var array
25+
*/
26+
private $additionalHeaders = [];
27+
28+
/**
29+
* @var mixed
30+
*/
31+
private $data = null;
32+
33+
/**
34+
* Body which will be used instead of data if set
35+
* @var string
36+
*/
37+
private $body = null;
38+
39+
/**
40+
* HTTP Status according to http://tools.ietf.org/html/rfc7231
41+
* @param integer $statusCode
42+
* @param string $status without status code
43+
* @return object this
44+
*/
45+
public function setStatus($statusCode, $status)
46+
{
47+
$this->statusCode = $statusCode;
48+
$this->status = $status;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* Add additional HTTP header
55+
* @param string $header
56+
* @return object this
57+
*/
58+
public function addHeaders($header)
59+
{
60+
$this->additionalHeaders[] = $header;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @param mixed $body used instead of data if set
67+
*/
68+
public function setBody($body)
69+
{
70+
$this->body = $body;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* @param mixed $data any data which should be later encoded to body
77+
*/
78+
public function setData($data)
79+
{
80+
$this->data = $data;
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* @return mixed data which should be later encoded to body
87+
*/
88+
public function getData()
89+
{
90+
return $this->data;
91+
}
92+
93+
/**
94+
* @return mixed used instead of data if set
95+
*/
96+
public function getBody()
97+
{
98+
if ($this->body !== null) {
99+
return $this->body;
100+
}
101+
102+
if ($this->data === null) {
103+
return null;
104+
}
105+
106+
return json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
107+
}
108+
109+
/**
110+
* HTTP Status according to http://tools.ietf.org/html/rfc7231 in form of array
111+
* @return array associative, e.g. ['code' => 200, 'status' => 'OK']
112+
*/
113+
public function getStatus()
114+
{
115+
return [
116+
'code' => $this->statusCode,
117+
'status' => $this->status
118+
];
119+
}
120+
121+
/**
122+
* @return array list of all headers including first status line
123+
*/
124+
public function getHeaders()
125+
{
126+
return array_merge(
127+
[$_SERVER['SERVER_PROTOCOL'].' '.$this->statusCode.' '.$this->status],
128+
$this->additionalHeaders
129+
);
130+
}
131+
}

0 commit comments

Comments
 (0)