Admin plugin : added create page option

Also introduced AntTools::repairFilePath
This commit is contained in:
Belle Aerni 2023-01-08 16:53:58 -08:00
parent d6588573b0
commit 6d3f8a3da1
5 changed files with 47 additions and 11 deletions

View file

@ -70,7 +70,7 @@ class AntCMS
{
$page = strtolower($page);
$pagePath = AntDir . "/Content/$page";
$pagePath = str_replace('//', '/', $pagePath);
$pagePath = AntTools::repairFilePath($pagePath);
if (is_dir($pagePath)) {
$pagePath = $pagePath . '/index.md';
@ -103,15 +103,15 @@ class AntCMS
$theme = 'Default';
}
$templatePath = antThemePath . '/' . $theme . '/' . 'Templates';
$defaultTemplates = antThemePath . '/Default/Templates';
$templatePath = AntTools::repairFilePath(antThemePath . '/' . $theme . '/' . 'Templates');
$defaultTemplates = AntTools::repairFilePath(antThemePath . '/Default/Templates');
$templates = AntTools::getFileList($templatePath, 'html');
if (in_array($layout . '.html', $templates)) {
$template = file_get_contents($templatePath . '/' . $layout . '.html');
$template = file_get_contents(AntTools::repairFilePath($templatePath . '/' . $layout . '.html'));
} else {
$template = file_get_contents($defaultTemplates . '/' . $layout . '.html');
$template = file_get_contents(AntTools::repairFilePath($defaultTemplates . '/' . $layout . '.html'));
}
if ($layout == 'default_layout' && !$template) {

View file

@ -16,6 +16,7 @@ class AntPages
$pageList = array();
foreach ($pages as $page) {
$page = AntTools::repairFilePath($page);
$pageContent = file_get_contents($page);
$pageHeader = AntCMS::getPageHeaders($pageContent);
$pageFunctionalPath = str_replace(antContentPath, "", $page);

View file

@ -15,7 +15,7 @@ class AntPluginLoader
foreach ($files as $file) {
if (substr($file, -10) === "Plugin.php") {
include_once $file;
include_once AntTools::repairFilePath($file);
$className = pathinfo($file, PATHINFO_FILENAME);
$plugins[] = new $className();
}

View file

@ -8,7 +8,7 @@ class AntTools
{
$dir = new \RecursiveDirectoryIterator($dir);
$iterator = new \RecursiveIteratorIterator($dir);
$files=array();
$files = array();
foreach ($iterator as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == $extension || $extension == null) {
$files[] = ($returnPath) ? $file->getPathname() : $file->getFilename();
@ -16,4 +16,15 @@ class AntTools
}
return $files;
}
public static function repairFilePath($path)
{
$newPath = realpath($path);
if (!$newPath) {
$newPath = str_replace('//', '/', $path);
$newPath = str_replace('/', DIRECTORY_SEPARATOR, $newPath);
}
return $newPath;
}
}

View file

@ -6,6 +6,7 @@ use AntCMS\AntConfig;
use AntCMS\AntPages;
use AntCMS\AntYaml;
use AntCMS\AntAuth;
use AntCMS\AntTools;
class AdminPlugin extends AntPlugin
{
@ -112,9 +113,20 @@ class AdminPlugin extends AntPlugin
exit;
case 'edit':
array_shift($route);
$pagePath = implode('/', $route);
$page = file_get_contents(antContentPath . '/' . $pagePath);
if (!isset($_POST['newpage'])) {
array_shift($route);
$pagePath = implode('/', $route);
$page = file_get_contents(antContentPath . '/' . $pagePath);
} else {
$pagePath = '/' . $_POST['newpage'];
if (substr($pagePath, -3) !== ".md") {
$pagePath .= '.md';
}
$page = "--AntCMS--\nTitle: New Page Title\nAuthor: Author\nDescription: Description of this page.\nKeywords: Keywords\n--AntCMS--\n";
}
$pagePath = AntTools::repairFilePath($pagePath);
$HTMLTemplate = str_replace('<!--AntCMS-ActionURL-->', '//' . $currentConfig['baseURL'] . "plugin/admin/pages/save/$pagePath", $HTMLTemplate);
$HTMLTemplate = str_replace('<!--AntCMS-TextAreaContent-->', htmlspecialchars($page), $HTMLTemplate);
break;
@ -122,16 +134,28 @@ class AdminPlugin extends AntPlugin
case 'save':
array_shift($route);
$pagePath = antContentPath . '/' . implode('/', $route);
if (!$_POST['textarea']) {
if (!isset($_POST['textarea'])) {
header('Location: //' . $currentConfig['baseURL'] . "plugin/admin/pages/");
}
file_put_contents($pagePath, $_POST['textarea']);
header('Location: //' . $currentConfig['baseURL'] . "plugin/admin/pages/");
exit;
case 'create':
$HTMLTemplate = "<h1>Page Management</h1>\n";
$HTMLTemplate .= "<p>Create new page</p>\n";
$HTMLTemplate .= '<form method="post" action="' . '//' . $currentConfig['baseURL'] . 'plugin/admin/pages/edit">';
$HTMLTemplate .=
'<div style="display:flex; flex-direction: row; justify-content: center; align-items: center">
<label for="input">URL for new page: ' . $currentConfig['baseURL'] . ' </label> <input type="text" name="newpage" id="input">
<input type="submit" value="Submit">
</div></form>';
break;
default:
$HTMLTemplate = "<h1>Page Management</h1>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/pages/regenerate'>Click here to regenerate the page list</a><br>\n";
$HTMLTemplate .= "<a href='//" . $currentConfig['baseURL'] . "plugin/admin/pages/create'>Click here to create a new page</a><br>\n";
$HTMLTemplate .= "<ul>\n";
foreach ($pages as $page) {
$HTMLTemplate .= "<li>\n";