From 1d6e1d8fc082b0188f2bb57b45da1a9365368f29 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Wed, 23 Feb 2022 14:15:10 +0100 Subject: [PATCH] Page URL from title, UUID for pages files --- bl-kernel/admin/views/editor.php | 9 +++++--- bl-kernel/pages.class.php | 22 +++++++++++++++----- bl-plugins/api/plugin.php | 35 ++++++++++++-------------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/bl-kernel/admin/views/editor.php b/bl-kernel/admin/views/editor.php index ec4ed040..a4b67003 100644 --- a/bl-kernel/admin/views/editor.php +++ b/bl-kernel/admin/views/editor.php @@ -38,7 +38,7 @@ // Set the global variable with the page key _pageKey = response.data.key; // Set Friendly URL - $('#friendlyURL').val(response.data.key); + //$('#friendlyURL').val(response.data.key); // Get current files fmGetFiles(); } else { @@ -76,7 +76,7 @@ // The page key can change after save the page so you need to set again the variable _pageKey = response.data.key; // Set friendly URL with the key - $('#friendlyURL').val(response.data.key); + //$('#friendlyURL').val(response.data.key); } else { logs('An error occurred while trying to save the current page.'); showAlertError(response.message); @@ -138,6 +138,7 @@ customFields[field] = value }); var args = { + slug: $('#friendlyURL').val(), title: $('#title').val(), content: editorGetContent(), custom: customFields, @@ -191,8 +192,8 @@ var value = $(this).val() customFields[field] = value }); - var args = { + slug: $('#friendlyURL').val(), title: $('#title').val(), content: editorGetContent(), custom: customFields, @@ -215,6 +216,7 @@ }); $('#coverImagePreview').dblclick(function() { + logs('Removing cover image.'); $('#coverImage').val(''); $(this).attr('src', HTML_PATH_CORE_IMG + 'default.svg'); var args = { coverImage: '' } @@ -389,6 +391,7 @@ }); var args = { + slug: $('#friendlyURL').val(), title: $('#title').val(), content: content, custom: customFields, diff --git a/bl-kernel/pages.class.php b/bl-kernel/pages.class.php index 19c9c1ad..e2b9316b 100644 --- a/bl-kernel/pages.class.php +++ b/bl-kernel/pages.class.php @@ -153,8 +153,14 @@ class Pages extends dbJSON { } // Create the upload directory for the page - if (Filesystem::mkdir(PATH_UPLOADS_PAGES.$key, true) === false) { - Log::set(__METHOD__.LOG_SEP.'An error occurred while trying to create the directory: '.PATH_UPLOADS_PAGES.$key, LOG_TYPE_ERROR); + if (Filesystem::mkdir(PATH_UPLOADS_PAGES.$row['uuid'], true) === false) { + Log::set(__METHOD__.LOG_SEP.'An error occurred while trying to create the directory: '.PATH_UPLOADS_PAGES.$row['uuid'], LOG_TYPE_ERROR); + return false; + } + + // Create symlink for the upload directory + if (symlink(PATH_UPLOADS_PAGES.$row['uuid'], PATH_UPLOADS_PAGES.$key) === false) { + Log::set(__METHOD__.LOG_SEP.'An error occurred while trying to create the symlink: '.PATH_UPLOADS_PAGES.$key, LOG_TYPE_ERROR); return false; } @@ -238,8 +244,12 @@ class Pages extends dbJSON { // If the user send an empty slug the page key doesn't change // This variable is not belong to the database so is not defined in $row if (empty($args['slug'])) { - $explode = explode('/', $key); - $slug = end($explode); + if (!empty($args['title'])) { + $slug = $args['title']; + } else { + $explode = explode('/', $key); + $slug = end($explode); + } } else { $slug = $args['slug']; } @@ -271,7 +281,9 @@ class Pages extends dbJSON { return false; } - if (Filesystem::mv(PATH_UPLOADS_PAGES.$key, PATH_UPLOADS_PAGES.$newKey) === false) { + // Regenerate the symlink to a proper directory + unlink(PATH_UPLOADS_PAGES.$key); + if (symlink(PATH_UPLOADS_PAGES.$row['uuid'], PATH_UPLOADS_PAGES.$newKey) === false) { Log::set(__METHOD__.LOG_SEP.'An error occurred while trying to move the directory: '.PATH_UPLOADS_PAGES.$newKey, LOG_TYPE_ERROR); return false; } diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 7ae4fe1a..e9962ec3 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -954,26 +954,17 @@ class pluginAPI extends Plugin { ); } - /* - Returns all files uploaded for a specific page. - Includes all files types. - - @pageKey string The page's key - - @return['data'] array The list of files - */ + /** + * Returns all files uploaded for a specific page. + * @param string $pageKey Page's key + * @return array List of files in return['data'], status in return['status'] + */ private function getFiles($pageKey) { $chunk = false; $sortByDate = true; - $path = PATH_UPLOADS_PAGES.$pageKey.DS; - - if (Sanitize::pathFile($path) === false) { - return array( - 'status'=>'1', - 'message'=>'Invalid path.' - ); - } + $page = new Page($pageKey); + $path = PATH_UPLOADS_PAGES.$page->uuid().DS; $files = array(); $listFiles = Filesystem::listFiles($path, '*', '*', $sortByDate, $chunk); @@ -984,17 +975,17 @@ class pluginAPI extends Plugin { $filename = Filesystem::filename($file); $fileExtension = Filesystem::extension($file); - $absoluteURL = DOMAIN_UPLOADS_PAGES.$pageKey.DS.$filename.'.'.$fileExtension; - $absolutePath = PATH_UPLOADS_PAGES.$pageKey.DS.$filename.'.'.$fileExtension; + $absoluteURL = DOMAIN_UPLOADS_PAGES.$page->uuid().DS.$filename.'.'.$fileExtension; + $absolutePath = PATH_UPLOADS_PAGES.$page->uuid().DS.$filename.'.'.$fileExtension; $thumbnailSmall = ''; - if (Filesystem::fileExists(PATH_UPLOADS_PAGES.$pageKey.DS.$filename.'-thumbnail-s.'.$fileExtension)) { - $thumbnailSmall = DOMAIN_UPLOADS_PAGES.$pageKey.DS.$filename.'-thumbnail-s.'.$fileExtension; + if (Filesystem::fileExists(PATH_UPLOADS_PAGES.$page->uuid().DS.$filename.'-thumbnail-s.'.$fileExtension)) { + $thumbnailSmall = DOMAIN_UPLOADS_PAGES.$page->uuid().DS.$filename.'-thumbnail-s.'.$fileExtension; } $thumbnailMedium = ''; - if (Filesystem::fileExists(PATH_UPLOADS_PAGES.$pageKey.DS.$filename.'-thumbnail-m.'.$fileExtension)) { - $thumbnailMedium = DOMAIN_UPLOADS_PAGES.$pageKey.DS.$filename.'-thumbnail-m.'.$fileExtension; + if (Filesystem::fileExists(PATH_UPLOADS_PAGES.$page->uuid().DS.$filename.'-thumbnail-m.'.$fileExtension)) { + $thumbnailMedium = DOMAIN_UPLOADS_PAGES.$page->uuid().DS.$filename.'-thumbnail-m.'.$fileExtension; } $data = array(