I'm designing a small CMS with best practices I learned. I came to this conclusion when comes to including page content and themes to each page depending by user or whatever.
But how could I make this better? I can change title of included modules this way, but I cannot modify titles if the included modules have another included sub modules.
My main question would be how should I rather handle the themes. But I would also like some criticism about the actual CMS design. Any flaws, security issues or anything that could be much better.
Index page from where I include all 'modules':
<?php
error_reporting(-1);
define('CMS_TOKEN', '');
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
$cms->IncludeModule($_GET['page']);
}
else {
foreach($cms->GetModuleList() as $module) {
$cms->ConcatContent('<a href="index.php?page='.$module.'">'.$module.'</a><br />');
}
}
include($cms->GetTheme() . "/head.php");
echo $cms->GetContent();
include($cms->GetTheme() . "/foot.php");
?>
Example module.
<?php
$module_name = 'User Panel';
$module_directory = 'user';
$this->SetPageName($module_name);
if(!defined('CMS_TOKEN')) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user']) && $_SESSION['user']['username'] != 'anonymous') {
$this->ConcatContent("<h2>$module_name</h2>");
if(isset($_GET['edit'])) {
$edit = $_GET['edit'];
foreach($this->GetCustomModuleList($module_directory) as $module) {
$this->ConcatContent("<a href=\"index.php?page=user&edit=$module\"> $module </a> | ");
}
$this->ConcatContent("<br /><br />");
if(in_array($module, $this->GetCustomModuleList('user'))) {
include($this->GetModuleDirectory() . '/' . $module_directory . '/' . $edit . $this->GetModuleExtention());
}
else {
$this->ConcatContent("Selected module doesn't exist!");
}
}
else {
foreach($this->GetCustomModuleList($module_directory) as $module) {
$this->ConcatContent("<a href=\"index.php?page=user&edit=$module\">$module</a> | ");
}
}
}
else {
header('Location: index.php?page=login');
}
}
?>
Some core CMS code:
<?php
class cms {
// lots of variables
// custom modules config variables - will always change
private $config_custom_module_list;
private $config_custom_module_directory;
function __construct() { /* ... */ }
// lots of getter/setters
public function GetModuleList() {
$moduleList = array();
foreach($this->config_module_list as $module) {
if($this->CheckModule($module) == 1) { // 1 for pure module result
$moduleNoExt = str_replace($this->GetModuleExtention(), '', $module);
$moduleList[] = str_replace($this->GetModuleExtention(), '', $moduleNoExt);
}
}
return $moduleList;
}
private function SetModuleList() {
$this->config_module_list = array_diff(scandir($this->GetModuleDirectory(), 1), array('..', '.'));
}
private function SetCustomModuleList($owner) {
if($this->CheckCustomModuleDirectory($owner)) {
$this->config_custom_module_list = array_diff(scandir($this->GetModuleDirectory() . '/' . $owner, 1), array('..', '.'));
return true;
}
return false;
}
private function CheckModule($module) {
$moduleNameArray = explode(".", $module);
$countParts = count($moduleNameArray);
if($countParts == 1) {
return 2;
}
else if($countParts == 3) {
$moduleNameExtention = '.' . $moduleNameArray[1] . '.' . $moduleNameArray[2];
if($this->GetModuleExtention() == $moduleNameExtention) {
return 1;
}
}
return 0;
}
public function IncludeModule($name) {
if(in_array($name, $this->GetModuleList())) {
include($this->GetModuleDirectory() . '/' . $name . $this->GetModuleExtention());
}
else {
echo 'Module Doesn\'t Exist! Critical Error!';
}
}
}
?>
1 Answer 1
I found easy solution. Just control the title from the end of the script like this:
<?php
$module_name = 'User Panel';
$module_directory = 'user';
if(!defined('CMS_TOKEN')) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user']) && $_SESSION['user']['username'] != 'anonymous') {
$this->ConcatContent("<h2>$module_name</h2>");
if(isset($_GET['edit'])) {
$edit = $_GET['edit'];
foreach($this->GetCustomModuleList($module_directory) as $module) {
$this->ConcatContent("<a href=\"index.php?page=user&edit=$module\"> $module </a> | ");
}
$this->ConcatContent("<br /><br />");
$module_name = 'SOME TITLE'; // ---- here
if(in_array($module, $this->GetCustomModuleList('user'))) {
include($this->GetModuleDirectory() . '/' . $module_directory . '/' . $edit . $this->GetModuleExtention());
}
else {
$this->ConcatContent("Selected module doesn't exist!");
}
}
else {
foreach($this->GetCustomModuleList($module_directory) as $module) {
$this->ConcatContent("<a href=\"index.php?page=user&edit=$module\">$module</a> | ");
}
}
}
else {
header('Location: index.php?page=login');
}
}
$this->SetPageName($module_name);
?>