Pages list will only list CMS editable files

This commit is contained in:
Andy Dixon
2024-12-19 21:04:47 +00:00
parent 7f4cf860e2
commit d39d344be2
+26 -1
View File
@@ -164,19 +164,21 @@ class CMSController
$filePath = $file->getRealPath(); $filePath = $file->getRealPath();
// Exclude CMS directory // Exclude CMS directory
if (strpos($filePath, $this->documentRoot . 'cms' . DIRECTORY_SEPARATOR) === 0) { if (str_starts_with($filePath, $this->documentRoot . 'cms' . DIRECTORY_SEPARATOR)) {
continue; continue;
} }
// Include only .html and .htm files // Include only .html and .htm files
if (preg_match('/\.(html|htm)$/i', $filePath)) { if (preg_match('/\.(html|htm)$/i', $filePath)) {
$relativePath = str_replace($this->documentRoot, '/', $filePath); $relativePath = str_replace($this->documentRoot, '/', $filePath);
if ($this->hasEditableClass($relativePath)) {
$pages[] = [ $pages[] = [
'name' => $relativePath, 'name' => $relativePath,
'url' => $relativePath, 'url' => $relativePath,
]; ];
} }
} }
}
$this->sendJsonResponse($pages); $this->sendJsonResponse($pages);
} }
@@ -283,4 +285,27 @@ class CMSController
header('Content-Type: application/json'); header('Content-Type: application/json');
echo json_encode($data); 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;
}
} }