From 980284108dbbb03db5d92ed5ccbf7830971c8e17 Mon Sep 17 00:00:00 2001 From: trendschau Date: Tue, 5 Sep 2023 14:49:44 +0200 Subject: [PATCH] improve delete folder --- content/00-welcome/05-todos.md | 6 +- .../ControllerApiAuthorArticle.php | 11 +++- .../typemill/Middleware/ApiAuthorization.php | 3 +- system/typemill/Models/Content.php | 59 ++----------------- system/typemill/Models/Storage.php | 42 +++++++++++++ 5 files changed, 62 insertions(+), 59 deletions(-) diff --git a/content/00-welcome/05-todos.md b/content/00-welcome/05-todos.md index fd867c2..a9c4463 100644 --- a/content/00-welcome/05-todos.md +++ b/content/00-welcome/05-todos.md @@ -103,17 +103,19 @@ * Clear cache * Show security Log * User search only for +10 users -* fix error messages +* DONE: fix error messages (check models) +* DONE: error status codes (check middleware) * Wrong frontend navigation if unpublished pages * Customfields not styled yet. * Warn if open another block * finish youtube component * Solution for logo and favicon +* For api translations should be done completely in backoffice ## Cleanups: * DONE: Events -* Error messages +* DONE: Error messages * Translations * https://stackoverflow.com/questions/15041608/searching-all-files-in-folder-for-strings * https://github.com/skfaisal93/AnyWhereInFiles/blob/master/anywhereinfiles-1.4.php diff --git a/system/typemill/Controllers/ControllerApiAuthorArticle.php b/system/typemill/Controllers/ControllerApiAuthorArticle.php index cd5fc11..c191d0b 100644 --- a/system/typemill/Controllers/ControllerApiAuthorArticle.php +++ b/system/typemill/Controllers/ControllerApiAuthorArticle.php @@ -641,7 +641,6 @@ class ControllerApiAuthorArticle extends Controller return $response->withHeader('Content-Type', 'application/json'); } - public function renameArticle(Request $request, Response $response, $args) { $validRights = $this->validateRights($request->getAttribute('c_userrole'), 'mycontent', 'edit'); @@ -902,7 +901,15 @@ class ControllerApiAuthorArticle extends Controller # check if it is a folder and if the folder has published pages. if($item->elementType == 'folder') { - $result = $content->deleteFolder($item); + # check if folder has published pages + if($content->hasPublishedItems($item)) + { + $result = Translations::translate('The folder contains published pages. Please unpublish or delete them first.'); + } + else + { + $result = $content->deleteFolder($item); + } } else { diff --git a/system/typemill/Middleware/ApiAuthorization.php b/system/typemill/Middleware/ApiAuthorization.php index a95d20e..fafabb0 100644 --- a/system/typemill/Middleware/ApiAuthorization.php +++ b/system/typemill/Middleware/ApiAuthorization.php @@ -21,7 +21,6 @@ class ApiAuthorization implements MiddlewareInterface { if(!$this->acl->isAllowed($request->getAttribute('c_userrole'), $this->resource, $this->action)) { - $message = 'userrole: ' . $request->getAttribute('c_userrole') . ' resource: ' . $this->resource . ' action: ' . $this->action; $response = new Response(); @@ -29,7 +28,7 @@ class ApiAuthorization implements MiddlewareInterface 'message' => $message ])); - return $response->withStatus(401); + return $response->withStatus(403); } $response = $handler->handle($request); diff --git a/system/typemill/Models/Content.php b/system/typemill/Models/Content.php index 7cb0bf1..1435d76 100644 --- a/system/typemill/Models/Content.php +++ b/system/typemill/Models/Content.php @@ -130,65 +130,18 @@ class Content return true; } + # overcomplicated. Next time just use a recursive delete everything method in storage public function deleteFolder($item, $result = true) { - if($item->elementType == 'folder' && isset($item->folderContent) && is_array($item->folderContent) && !empty($item->folderContent)) + if($this->storage->deleteContentFolderRecursive($item->path)) { - if($this->hasPublishedItems($item)) - { - return 'The folder contains published pages. Please unpublish or delete them first.'; - } - - foreach($item->folderContent as $subitem) - { - if($subitem->elementType == 'folder') - { - $result = $this->deleteFolder($subitem); - } - else - { - $result = $this->deletePage($subitem); - } - - if($result !== true) - { - return $result; - } - } - - $result = $this->deletePage($item); - - if($result !== true) - { - return $result; - } - - $result = $this->storage->deleteContentFolder($item->path); - - if($result !== true) - { - return $this->storage->getError(); - } + return true; } - - $result = $this->deletePage($item); - - if($result !== true) - { - return $result; - } - - $result = $this->storage->deleteContentFolder($item->path); - - if($result !== true) - { - return $this->storage->getError(); - } - - return true; + + return $this->storage->getError(); } - private function hasPublishedItems($folder, $published = false) + public function hasPublishedItems($folder, $published = false) { $published = false; diff --git a/system/typemill/Models/Storage.php b/system/typemill/Models/Storage.php index 335518d..93d509d 100644 --- a/system/typemill/Models/Storage.php +++ b/system/typemill/Models/Storage.php @@ -203,6 +203,48 @@ class Storage return true; } + public function deleteContentFolderRecursive($folderpath) + { + $folderdir = $this->getFolderPath('contentFolder'); + + if(!is_dir($folderdir . $folderpath)) + { + $this->error = "$folderpath is not a directory"; + return false; + } + + $filelist = array_diff(scandir($folderdir . $folderpath), array('..', '.')); + if(!empty($filelist)) + { + foreach($filelist as $filepath) + { + $fullfilepath = $folderdir . $folderpath . DIRECTORY_SEPARATOR . $filepath; + if(is_dir($fullfilepath)) + { + $this->deleteContentFolderRecursive($folderpath . DIRECTORY_SEPARATOR . $filepath); + } + else + { + if(!unlink($fullfilepath)) + { + $this->error = "Could not delete file $fullfilepath."; + + return false; + } + } + } + } + + if(!rmdir($folderdir . $folderpath)) + { + $this->error = "Could not delete folder $folderpath."; + + return false; + } + + return true; + } + public function checkFile($location, $folder, $filename) { $filepath = $this->getFolderPath($location, $folder) . $filename;