forked from LeafCMS/LeafCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaf.php
161 lines (141 loc) · 5.35 KB
/
leaf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
* ------------------------------------------------------------------------------------------------------------------ *
* Copyright 2014 LeafCMS (Primary Developer(s): Matthew Gross, Kirk Morris) *
* ------------------------------------------------------------------------------------------------------------------ *
* Description * LeafCMS (Lightweight, Easy and Friendly) is a simple CMS allowing creation of websites in static *
* or dynamic ways. Although mentioned as a CMS (Content management system), the CMS can also be valued as a *
* framework; as large logically-based websites can be developed using LeafCMS due to the availability of extensions. *
* All in all; whether your website is large or small, LeafCMS can power it. *
* ------------------------------------------------------------------------------------------------------------------ *
* Last Edit: December 11, 2014 *
* License: MIT (see LICENSE file) *
* ------------------------------------------------------------------------------------------------------------------ *
*/
/* Standard Definitions
* Description: We are defining all neccessary file paths for the script to use. These are defined in a global fashion
* You can change these, but be careful!
*/
define('ROOT_DIR', dirname(__FILE__));
define('BRANCHES_DIR', ROOT_DIR.'/branches/');
define('WEB_DIR', '/');
define('ASSETS_DIR', 'assets/');
define('CONFIG_FILE', BRANCHES_DIR.'config.php');
/* Actual Class
* This defines all logic for the CMS
* Every request to the server uses this class!
*/
class LeafCMS {
private $default_bindings;
public $output;
protected $config;
public function __construct() {
$this->default_bindings = array(
"template_dir" => BRANCHES_DIR . 'templates/',
"page_templates" => array(
"home" => "home",
),
);
$this->config = include(CONFIG_FILE);
// Set default bindings.
$this->setDefaultConfigValues();
}
public function setDefaultConfigValues() {
$default_config_values = $this->default_bindings;
foreach($default_config_values as $k => $v) {
if(!(isset($this->config[$k]))) {
// wasn't set
$this->config[$k] = $v;
}
}
}
public function getTemplate($template) {
$template_dir = $this->config['template_dir'];
$template = $template_dir.$template.'.template.html';
return file_get_contents($template);
}
public function setBinding($binding, $setting, $template = null) {
$binded = ($template === null) ? $this->output : $template;
$binded = str_replace('~{'.$binding.'}~', $setting, $binded);
if($template === null) {
$this->output = $binded;
return true;
}
else {
return $binded;
}
return false;
}
/* returnError(code)
* description = easily return an error (such as 404) to the user. helper function
* code = The error code id.
*/
public function returnError($code) {
$message = 'Unknown Error'; // by default.
switch($code) {
case 404:
// Not Found.
http_response_code(404);
$message = "404 Error. The page could not be found!"; // @todo: set to config value later;
break;
case 403:
// Access Denied.
http_response_code(403);
$message = "403 Access Denied / Forbidden.";
break;
}
$template = $this->getTemplate("error");
$template = $this->setBinding("message", $message, $template);
$template = $this->setBinding("code", $code, $template);
return $template;
}
public function runPage($page) {
$config = $this->config;
$output = '';
if(isset($config['page_templates'][$page])) {
$page = $config['page_templates'][$page];
$this->output = $this->getTemplate($page['template']);
foreach($page['bindings'] as $k => $v) {
$this->setBinding($k, $v);
}
foreach($page['extensions'] as $extension) {
$this->runExtension($extension);
}
}
else {
// return 404.
$this->output = $this->returnError('404');
return false; // error.
}
return true;
}
public function runExtension($extension='all') {
$extension_dir = BRANCHES_DIR.'extensions/';
$config = $this->config;
if($extension == 'all') {
if(isset($config['extensions'])) { // if extensions exist. if not; do nothing.
// load all.
foreach($config['extensions'] as $extension) {
// remove .php if the user added it by mistake and include the file.
include(str_replace('.php', '', $extension_dir.$extension['file']).'.php');
// call the function.
// also, remove () if added in config by mistake.
call_user_func(str_replace('()', '', $extension['function']));
return true;
}
}
}
else {
include($extension_dir.$config['extensions'][$extension]['file'].'.php');
call_user_func(str_replace('()', '', $config['extensions'][$extension]['function']));
return true;
}
return false;
}
}
$leaf = new LeafCMS;
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
$leaf->runPage($page);
$html = $leaf->output;
echo $html;
?>