From fb2cd3080123be1dbe10be782045cd2b021602fc Mon Sep 17 00:00:00 2001 From: Andy Dixon Date: Thu, 19 Dec 2024 21:04:47 +0000 Subject: [PATCH] Pages list will only list CMS editable files --- cms/src/NeoCMS/CMSController.php | 35 +++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/cms/src/NeoCMS/CMSController.php b/cms/src/NeoCMS/CMSController.php index 83e270a..4d744fb 100644 --- a/cms/src/NeoCMS/CMSController.php +++ b/cms/src/NeoCMS/CMSController.php @@ -164,17 +164,19 @@ class CMSController $filePath = $file->getRealPath(); // Exclude CMS directory - if (strpos($filePath, $this->documentRoot . 'cms' . DIRECTORY_SEPARATOR) === 0) { + if (str_starts_with($filePath, $this->documentRoot . 'cms' . DIRECTORY_SEPARATOR)) { continue; } // Include only .html and .htm files if (preg_match('/\.(html|htm)$/i', $filePath)) { $relativePath = str_replace($this->documentRoot, '/', $filePath); - $pages[] = [ - 'name' => $relativePath, - 'url' => $relativePath, - ]; + if ($this->hasEditableClass($relativePath)) { + $pages[] = [ + 'name' => $relativePath, + 'url' => $relativePath, + ]; + } } } @@ -283,4 +285,27 @@ class CMSController header('Content-Type: application/json'); echo json_encode($data); } + + /** + * Checks to see if a file has an editable class + * @param $filePath + * @return bool + */ + private function hasEditableClass($filePath) { + // Load the HTML content from the file + $htmlContent = file_get_contents($filePath); + + // Initialize a new DOMDocument object + $dom = new DOMDocument(); + + // Suppress warnings that might occur due to malformed HTML + @$dom->loadHTML($htmlContent); + + // Find all elements with the class "editable" + $xpath = new DOMXPath($dom); + $editableElements = $xpath->query('//*[contains(@class, "editable")]'); + + // Return true if there are any elements with the "editable" class + return $editableElements->length > 0; + } }