99 */
1010class FileManagerApi
1111{
12- private $ basePath = '' ;
12+ private $ basePath = null ;
1313
1414 private $ translate ;
1515
16- public function __construct ($ basePath = __DIR__ . ' /../files ' , $ lang = 'en ' , $ muteErrors = true )
16+ public function __construct ($ basePath = null , $ lang = 'en ' , $ muteErrors = true )
1717 {
18- if ($ muteErrors === true ) {
18+ if ($ muteErrors ) {
1919 ini_set ('display_errors ' , 0 );
2020 }
2121
22- if (!file_exists ($ basePath )){
23- mkdir ($ basePath );
24- }
25-
26- $ this ->basePath = $ basePath ;
22+ $ this ->basePath = $ basePath ?: dirname (__DIR__ );
2723 $ this ->translate = new Translate ($ lang );
2824 }
2925
@@ -36,7 +32,7 @@ public function postHandler($query, $request, $files)
3632 $ uploaded = $ this ->uploadAction ($ request ['destination ' ], $ files );
3733 if ($ uploaded === true ) {
3834 $ response = $ this ->simpleSuccessResponse ();
39- }else {
35+ } else {
4036 $ response = $ this ->simpleErrorResponse ($ t ->upload_failed );
4137 }
4238
@@ -63,7 +59,7 @@ public function postHandler($query, $request, $files)
6359 $ response = $ this ->simpleSuccessResponse ();
6460 } elseif ($ renamed === 'notfound ' ){
6561 $ response = $ this ->simpleErrorResponse ($ t ->file_not_found );
66- }else {
62+ } else {
6763 $ response = $ this ->simpleErrorResponse ($ t ->renaming_failed );
6864 }
6965 break ;
@@ -72,7 +68,7 @@ public function postHandler($query, $request, $files)
7268 $ moved = $ this ->moveAction ($ request ['items ' ], $ request ['newPath ' ]);
7369 if ($ moved === true ) {
7470 $ response = $ this ->simpleSuccessResponse ();
75- }else {
71+ } else {
7672 $ response = $ this ->simpleErrorResponse ($ t ->moving_failed );
7773 }
7874 break ;
@@ -81,7 +77,7 @@ public function postHandler($query, $request, $files)
8177 $ copied = $ this ->copyAction ($ request ['items ' ], $ request ['newPath ' ]);
8278 if ($ copied === true ) {
8379 $ response = $ this ->simpleSuccessResponse ();
84- }else {
80+ } else {
8581 $ response = $ this ->simpleErrorResponse ($ t ->copying_failed );
8682 }
8783 break ;
@@ -101,7 +97,7 @@ public function postHandler($query, $request, $files)
10197 $ edited = $ this ->editAction ($ request ['item ' ], $ request ['content ' ]);
10298 if ($ edited !== false ) {
10399 $ response = $ this ->simpleSuccessResponse ();
104- }else {
100+ } else {
105101 $ response = $ this ->simpleErrorResponse ($ t ->saving_failed );
106102 }
107103 break ;
@@ -113,7 +109,7 @@ public function postHandler($query, $request, $files)
113109 $ response ->setData ([
114110 'result ' => $ content
115111 ]);
116- }else {
112+ } else {
117113 $ response = $ this ->simpleErrorResponse ($ t ->file_not_found );
118114 }
119115 break ;
@@ -144,7 +140,7 @@ public function postHandler($query, $request, $files)
144140 $ compressed = $ this ->compressAction ($ request ['items ' ], $ request ['destination ' ], $ request ['compressedFilename ' ]);
145141 if ($ compressed === true ) {
146142 $ response = $ this ->simpleSuccessResponse ();
147- }else {
143+ } else {
148144 $ response = $ this ->simpleErrorResponse ($ t ->compression_failed );
149145 }
150146 break ;
@@ -175,7 +171,7 @@ public function getHandler($queries)
175171 $ downloaded = $ this ->downloadAction ($ queries ['path ' ]);
176172 if ($ downloaded === true ) {
177173 exit ;
178- }else {
174+ } else {
179175 $ response = $ this ->simpleErrorResponse ($ t ->file_not_found );
180176 }
181177
@@ -193,7 +189,9 @@ private function downloadAction($path)
193189 {
194190 $ path = $ this ->basePath . $ path ;
195191
196- if (!file_exists ($ path )) return false ;
192+ if (! file_exists ($ path )) {
193+ return false ;
194+ }
197195
198196 header ('Cache-Control: must-revalidate ' );
199197 header ('Pragma: public ' );
@@ -208,7 +206,10 @@ private function uploadAction($path, $files)
208206 $ path = $ this ->basePath . $ path ;
209207
210208 foreach ($ _FILES as $ file ) {
211- $ uploaded = move_uploaded_file ($ file ['tmp_name ' ] , rtrim ($ path , '/ ' ) . '/ ' . $ file ['name ' ]);
209+ $ uploaded = move_uploaded_file (
210+ $ file ['tmp_name ' ],
211+ rtrim ($ path , '/ ' ) . '/ ' . $ file ['name ' ]
212+ );
212213 if ($ uploaded === false ) {
213214 return false ;
214215 }
@@ -228,7 +229,7 @@ private function listAction($path)
228229 'rights ' => $ this ->parsePerms (fileperms ($ file )),
229230 'size ' => filesize ($ file ),
230231 'date ' => $ date ->format ('Y-m-d H:i:s ' ),
231- 'type ' => ( is_dir ($ file ) ? 'dir ' : 'file ' )
232+ 'type ' => is_dir ($ file ) ? 'dir ' : 'file '
232233 ];
233234 }, $ files );
234235
@@ -240,7 +241,9 @@ private function renameAction($oldPath, $newPath)
240241 $ oldPath = $ this ->basePath . $ oldPath ;
241242 $ newPath = $ this ->basePath . $ newPath ;
242243
243- if (!file_exists ($ oldPath )) return 'notfound ' ;
244+ if (! file_exists ($ oldPath )) {
245+ return 'notfound ' ;
246+ }
244247
245248 return rename ($ oldPath , $ newPath );
246249 }
@@ -250,10 +253,14 @@ private function moveAction($oldPaths, $newPath)
250253 $ newPath = $ this ->basePath . rtrim ($ newPath , '/ ' ) . '/ ' ;
251254
252255 foreach ($ oldPaths as $ oldPath ) {
253- if (!file_exists ($ this ->basePath . $ oldPath )) return false ;
256+ if (! file_exists ($ this ->basePath . $ oldPath )) {
257+ return false ;
258+ }
254259
255260 $ renamed = rename ($ this ->basePath . $ oldPath , $ newPath . basename ($ oldPath ));
256- if ($ renamed === false ) return false ;
261+ if ($ renamed === false ) {
262+ return false ;
263+ }
257264 }
258265
259266 return true ;
@@ -264,10 +271,17 @@ private function copyAction($oldPaths, $newPath)
264271 $ newPath = $ this ->basePath . rtrim ($ newPath , '/ ' ) . '/ ' ;
265272
266273 foreach ($ oldPaths as $ oldPath ) {
267- if (!file_exists ($ this ->basePath . $ oldPath )) return false ;
268-
269- $ copied = copy ($ this ->basePath . $ oldPath , $ newPath . basename ($ oldPath ));
270- if ($ copied === false ) return false ;
274+ if (! file_exists ($ this ->basePath . $ oldPath )) {
275+ return false ;
276+ }
277+
278+ $ copied = copy (
279+ $ this ->basePath . $ oldPath ,
280+ $ newPath . basename ($ oldPath )
281+ );
282+ if ($ copied === false ) {
283+ return false ;
284+ }
271285 }
272286
273287 return true ;
@@ -286,12 +300,13 @@ private function removeAction($paths)
286300 } else {
287301 $ removed = rmdir ($ path );
288302 }
289- }else {
303+ } else {
290304 $ removed = unlink ($ path );
291305 }
292306
293-
294- if ($ removed === false ) return false ;
307+ if ($ removed === false ) {
308+ return false ;
309+ }
295310 }
296311
297312 return true ;
@@ -300,15 +315,16 @@ private function removeAction($paths)
300315 private function editAction ($ path , $ content )
301316 {
302317 $ path = $ this ->basePath . $ path ;
303-
304318 return file_put_contents ($ path , $ content );
305319 }
306320
307321 private function getContentAction ($ path )
308322 {
309323 $ path = $ this ->basePath . $ path ;
310324
311- if (!file_exists ($ path )) return false ;
325+ if (! file_exists ($ path )) {
326+ return false ;
327+ }
312328
313329 return file_get_contents ($ path );
314330 }
@@ -317,7 +333,9 @@ private function createFolderAction($path)
317333 {
318334 $ path = $ this ->basePath . $ path ;
319335
320- if (file_exists ($ path ) AND is_dir ($ path )) return 'exists ' ;
336+ if (file_exists ($ path ) && is_dir ($ path )) {
337+ return 'exists ' ;
338+ }
321339
322340 return mkdir ($ path );
323341 }
@@ -327,13 +345,18 @@ private function changePermissionsAction($paths, $permissions, $recursive)
327345 foreach ($ paths as $ path ) {
328346 if (!file_exists ($ this ->basePath . $ path )) return 'missing ' ;
329347
330- if (is_dir ($ path ) AND $ recursive === true ) {
331- $ iterator = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ path ), RecursiveIteratorIterator::SELF_FIRST );
348+ if (is_dir ($ path ) && $ recursive === true ) {
349+ $ iterator = new RecursiveIteratorIterator (
350+ new RecursiveDirectoryIterator ($ path ),
351+ RecursiveIteratorIterator::SELF_FIRST
352+ );
332353
333354 foreach ($ iterator as $ item ) {
334355 $ changed = chmod ($ this ->basePath . $ item , octdec ($ permissions ));
335356
336- if ($ changed === false ) return false ;
357+ if ($ changed === false ) {
358+ return false ;
359+ }
337360 }
338361 }
339362
@@ -346,7 +369,9 @@ private function compressAction($paths, $destination, $archiveName)
346369 $ archivePath = $ this ->basePath . $ destination . $ archiveName ;
347370
348371 $ zip = new ZipArchive ();
349- if ($ zip ->open ($ archivePath , ZipArchive::CREATE ) !== true ) return false ;
372+ if ($ zip ->open ($ archivePath , ZipArchive::CREATE ) !== true ) {
373+ return false ;
374+ }
350375
351376 foreach ($ paths as $ path ) {
352377 $ zip ->addFile ($ this ->basePath . $ path , basename ($ path ));
@@ -358,11 +383,12 @@ private function compressAction($paths, $destination, $archiveName)
358383 private function extractAction ($ destination , $ archivePath , $ folderName )
359384 {
360385 $ archivePath = $ this ->basePath . $ archivePath ;
361-
362386 $ folderPath = $ this ->basePath . rtrim ($ destination , '/ ' ) . '/ ' . $ folderName ;
363387
364388 $ zip = new ZipArchive ;
365- if ($ zip ->open ($ archivePath ) === false ) return 'unsupported ' ;
389+ if ($ zip ->open ($ archivePath ) === false ) {
390+ return 'unsupported ' ;
391+ }
366392
367393 mkdir ($ folderPath );
368394 $ zip ->extractTo ($ folderPath );
@@ -384,13 +410,14 @@ private function simpleSuccessResponse()
384410 private function simpleErrorResponse ($ message )
385411 {
386412 $ response = new Response ();
387- $ response ->setStatus (500 , 'Internal Server Error ' )
388- ->setData ([
389- 'result ' => [
390- 'success ' => false ,
391- 'error ' => $ message
392- ]
393- ]);
413+ $ response
414+ ->setStatus (500 , 'Internal Server Error ' )
415+ ->setData ([
416+ 'result ' => [
417+ 'success ' => false ,
418+ 'error ' => $ message
419+ ]
420+ ]);
394421
395422 return $ response ;
396423 }
0 commit comments