/
index.php
251 lines (251 loc) · 8.45 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
class cms {
function _setup () {
function _parsePanel ($parse, $str_in, &$array_out) {
$array_out = array();
preg_match_all($parse, $str_in, $i);
array_shift($i);
for ($a = 0; $a < count($i[0]); $a++) {
$name = trim((($i[0][$a]) ? strtolower($i[0][$a]) : $a));
$array_out[$name] = trim($i[1][$a]);
}
}
function _parseWebsite ($parse, &$array_out) {
_parsePanel($parse, $array_out, $array_out);
$opt_default = array('missing'=>'missing', 'theme'=>'cimus', 'template'=>'index.html');
$opt = array();
foreach ($array_out as $a => $b) {
if ($a !== 'opts') {
$opt[$a] = $b;
if ($a !== 'name') {
unset($array_out[$a]);
}
}
}
$array_out['opts'] = array_merge($opt_default, $opt);
$array_out['page'] = strtolower(((isset($_GET['p']) && $_GET['p']) ? $_GET['p'] : 'index'));
if (isset($array_out['opts']['index']) && $array_out['opts']['index']) {
$array_out['index'] = $array_out['opts']['index'];
unset($array_out['opts']['index']);
}
else {
$array_out['index'] = 'index';
}
}
function _parseNavigation ($parse, &$panel) {
$website = &$panel['website'];
$array_out = &$panel['sitemap'];
preg_match_all($parse, $array_out, $i);
array_shift($i);
$array_out = array();
$array_deep = array();
for ($a = 0; $a < count($i[0]); $a++) {
$call = $i[1][$a];
$deep = strlen($i[0][$a]);
$name = trim(preg_replace('/\[.*?\]/', '', $call));
$page = strtolower(str_replace(array(0=>' ', 1=>'.'), array(0=>'-', 1=>''), $name));
preg_match_all('/\[(.*?):(.*?)\]|\[(.*?)\]/', $call, $opts);
if (count($opts[0])) {
$opts_ = array();
for ($b = 0; $b < count($opts[0]); $b++) {
if ($opts[1][$b] == 'url') {
$page = trim($opts[2][$b]);
}
else {
$opts_[trim($opts[1][$b])] = trim($opts[2][$b]);
}
}
$opts = $opts_;
}
else {
$opts = array();
}
if ($a == 0) {
$page = 'index';
if ($website['page'] == $page) {
$website['opts'][$page] = $page;
}
}
$opts = array_merge(array('name'=>$name), $opts);
$array_deep[$deep] = $page;
$array_out_ = &$array_out;
for ($b = 0; $b < $deep; $b++) {
$opts = array_merge($array_out_[$array_deep[$b]]['opts'], $opts);
$array_out_ = &$array_out_[$array_deep[$b]]['kids'];
}
if (isset($array_out_['name']) && $array_out_['name']) {
if (!$array_out_['kids']) {
$array_out_['kids'] = array();
}
$array_write = &$array_out_['kids'];
}
else {
$array_write = &$array_out_;
}
$array_write[$page] = array('name'=>$name, 'deep'=>$deep, 'opts'=>$opts);
if ($page == $website['page']) {
$array_write[$page]['current'] = true;
}
}
}
function _parsePage (&$sitemap, &$current) {
foreach ($sitemap as &$a) {
if (isset($a['kids']) && $a['kids']) {
$is_selected = _parsePage($a['kids'], $current);
if ($is_selected) {
$a['selected'] = true;
return true;
}
}
if (isset($a['current']) && $a['current']) {
$a['selected'] = true;
$current = $a;
return true;
}
}
return false;
}
function _htmlAllNavigation (&$panel) {
$index = $panel['website']['index'];
$sitemap = $panel['sitemap'];
$panel['html'] = array(
'navigation-sitemap' => _htmlNavigationSitemap($sitemap, $index),
'navigation-breadcrumb' => _htmlNavigationBreadcrumb($sitemap, $index)
);
$hold = true;
$deep = 1;
while ($hold) {
$hold = false;
foreach ($sitemap as &$b) {
if (isset($b['selected']) && $b['selected']) {
$hold = true;
$panel['html']['navigation-level-' . $deep] = _htmlNavigation($sitemap, $index, $deep);
$sitemap = isset($b['kids'])?$b['kids']:array();
$deep++;
break;
}
}
if ($sitemap) {
$panel['html']['navigation-siblings'] = $panel['html']['navigation-level-' . ($deep - 1)];
$panel['html']['navigation-children'] = $panel['html']['navigation-level-' . $deep] = _htmlNavigation($sitemap, $index, $deep);
}
else {
$hold = false;
}
}
}
function _htmlNavigationSitemap (&$sitemap, $index, $deep = 1) {
$h = '<ul class="level-' . $deep . '">';
foreach ($sitemap as $a => $b) {
$a_ = &$sitemap[$a];
$c = 'level-' . $deep . ((isset($a_['selected']) && $a_['selected']) ? ' selected' : '') . ((isset($a_['current']) && $a_['current']) ? ' current' : '');
$h_ = '<li class="' . $c . '"><a class="' . $c . '" href="./' . (($a !== $index) ? $a : '') . '"><span>' . $a_['name'] . '</span></a>';
if (isset($a_['kids']) && $a_['kids']) {
$h_ .= _htmlNavigationSitemap($a_['kids'], $index, $deep + 1);
}
$h_ .= '</li>';
if (!isset($a_['hidden']) || !$a_['hidden']) {
$h .= $h_;
}
}
$h .= '</ul>';
return $h;
}
function _htmlNavigation (&$sitemap, $index, $deep = 1) {
$h = '<ul class="level-' . $deep . '">';
foreach ($sitemap as $a => $b) {
$a_ = &$sitemap[$a];
$c = 'level-' . $deep . ((isset($a_['selected']) && $a_['selected']) ? ' selected' : '') . ((isset($a_['current']) && $a_['current']) ? ' current' : '');
if (!isset($a_['hidden']) || !$a_['hidden']) {
$h .= '<li class="' . $c . '"><a class="' . $c . '" href="./' . (($a !== $index) ? $a : '') . '"><span>' . $a_['name'] . '</span></a>';
}
}
$h .= '</ul>';
return $h;
}
function _htmlNavigationBreadcrumb (&$sitemap, $index, $deep = 1) {
$h = false;
foreach ($sitemap as $a => $b) {
$a_ = &$sitemap[$a];
if (isset($a_['selected']) && $a_['selected']) {
$c = 'level-' . $deep . ' selected' . ((isset($a_['current']) && $a_['current']) ? ' current' : '');
$h = '<a class="' . $c . '" href="./' . (($a !== $index) ? $a : '') . '"><span>' . $a_['name'] . '</span></a>';
if (isset($a_['current']) && $a_['current']) {
return $h;
}
elseif ($a_['kids']) {
$return = _htmlNavigationBreadcrumb($a_['kids'], $index, $deep + 1);
if ($return) {
return $h . ' > ' . $return;
}
}
}
}
return $h;
}
_parsePanel('/\/\*\s{0,}-+\s{0,}(.*?)\s{0,}-+\s{0,}\*\/([^\/]+)/', trim(file_get_contents('cms.txt')), $panel);
_parseWebsite('/(.*?):(.+)/', $panel['website']);
_parseNavigation('/(\t{0,})(.+?)(\n|$)/sm', $panel);
_parsePage($panel['sitemap'], $panel['current']);
$panel['current']['opts'] = array_merge($panel['website']['opts'], $panel['current']['opts']);
_htmlAllNavigation($panel);
return $panel;
}
function _theme (&$panel) {
function _commentCompression ($str) {
$str = preg_replace('/<!-- BEGIN:(.*?) -->.*?<!-- END:\1 -->/s', '##$1##', $str);
$str = preg_replace('/##BEGIN:(.*?)##.*?##END:\1##/s', '##$1##', $str);
return $str;
}
function _replace ($replace, $with, &$inside) {
$inside = str_ireplace('<!-- ' . $replace . ' -->', $with, $inside);
$inside = str_ireplace('##' . $replace . '##', $with, $inside);
}
function _include ($u, $a) {
foreach ($a as $b) {
if (file_exists($u . $b)) {
ob_start();
include $u . $b;
$u = ob_get_contents();
ob_end_clean();
return _commentCompression($u);
}
}
return false;
}
$current = $panel['current'];
if (!$current) {
$current = $panel['website'];
}
$theme = _include('themes/' . $current['opts']['theme'] . '/templates/' . $current['opts']['template'], array(0=>'', 1=>'.php', 2=>'.html', 3=>'.txt'));
$page = _include('pages/' . $panel['website']['page'], array(0=>'.php', 1=>'.html', 2=>'.txt'));
if (!$page) {
$page = _include('pages/' . $current['opts']['missing'], array(0=>'.php', 1=>'.html', 2=>'.txt'));
}
_replace('content', $page, $theme);
$theme = preg_replace('/(href|src)="\.\./', '$1="' . 'themes/' . $current['opts']['theme'], $theme);
foreach ($panel['html'] as $a => $b) {
_replace($a, $b, $theme);
}
if ($current['opts']) {
foreach ($current['opts'] as $a => $b) {
if (is_string($b)) {
_replace($a, $b, $theme);
}
}
}
foreach ($panel['website']['opts'] as $a => $b) {
if (is_string($b)) {
_replace('site-' . $a, $b, $theme);
}
}
$theme = preg_replace('/(<!--\s|##).*?(\s-->|##)/', '', $theme);
echo $theme;
}
function __construct () {
$t = $this;
$panel = $t->_setup();
$theme = $t->_theme($panel);
}
}
$site = new cms();