This repository was archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.inc.php
392 lines (330 loc) · 10.3 KB
/
process.inc.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/*
* TWCMS 1.x
* Page Processing
*
* This file processes page headers and then searches
* the content directory (CPATH) for the appropriate page.
*
* Returns template variables such as header, title, content,
* all within the $T global array.
*
* Additional processing details can be found within
* the $P global array.
*
* The main template is mainly responsible for parsing
* and displaying these variables. Though they are also
* accessible by dynamic content files (CPATH/*.inc.php)
*/
/*
* Header Variable
* All header values are stored here for quick access
* Stored in # => val format for all headers
* plus special vars (/cat:5/); cat => 5
* pure integers are saved as id (/10/); id => 10,
* additional integers get numbers: (/10/15/); id => 10, id2 => 15
*/
$H = array();
// Process Variable
// All values key to processing will be in the $P array.
$P = array();
// Determines if a 404 error needs to be thrown (404)
$P['404'] = FALSE;
// Strip BASEURL from REQUESTURL, so baseurl becomes root
$requrl = BASEURL === '/' ? REQUESTURL : str_replace(BASEURL, '', REQUESTURL);
if ($requrl === '') $requrl = '/';
// Define current URL as a constant without strings and achors
// Dumb-ass work-around because PHP doesn't like colons in parse_url
$url = str_replace(':', ';', $requrl);
$path = parse_url($url, PHP_URL_PATH);
$path = str_replace(';', ':', $path);
define('CURRURL', $path);
// Run event 'beforeProcess'
tw_event('beforeProcess');
// Special case for index as default
if (CURRURL === '/') $headers = array('index');
else $headers = explode('/', CURRURL);
// Generate $H array based on headers
$lastid = 1;
foreach ($headers AS $k => $val) {
if ($val === '' || $val === '/') continue;
$H[] = path_escape($val);
// If value is a pure integer
// save as specific ID #
if (ctype_digit($val)) {
// Remove ID from main array
array_pop($H);
// Process which ID it should be
$val = intval($val);
if ($lastid === 1) {
$H['id'] = $val;
}
else {
$H['id'+$lastid] = $val;
}
++$lastid;
}
// If : is present, separate for variable
// to use as key (cat:5); cat => 5
elseif (strpos($val, ':') !== FALSE) {
// Remove var from main array
array_pop($H);
// Split value based on colon
$colon = explode(':', $val, 2);
$ckey = $colon[0];
$cval = $colon[1];
// Save as associative array,
// but also check if it is an integer.
// Integers should always remain their type
$H[$ckey] = ctype_digit($cval) ? (int) $cval : path_escape($cval);
}
// If _ is present, just remove
// Underscores are illegal characters
elseif (strpos($val, '_') !== FALSE) {
array_pop($H);
$P['404'] = TRUE;
}
}
/*
* Get Raw Header Variables
* GET variable can override normal URL variables
* Example: /store/cat:5/?cat=10; $H['cat'] = 10
*/
foreach ($_GET AS $key => $val) {
$H[$key] = $val;
}
// Determine which headers belong where
$P['root'] = ''; // Main page (first root in the chain)
$P['pages'] = array(); // Array of all pages
foreach ($H AS $key => $val) {
// Exclude special variables
if (!is_int($key)) continue;
// Set root page (first name found)
if ($key === 0) $P['root'] = $H[$key];
$P['pages'][] = $H[$key];
}
// For tracking breadcrumbs
// (format: <title> => $url)
$T['bcrumbs'] = array('Home' => FULLURL);
// Generate starting breadcrumbs if not on home page
if ($P['root'] !== 'index') {
// Add breadcrumbs for current page + subpages
$prev = '';
foreach ($P['pages'] AS $bcpage) {
// Make titles look nice (space and captialize)
// Use $prev to track previous url's
$T['bcrumbs'][p_url2name($bcpage)] = $prev.'/'.$bcpage.'/';
$prev = $prev.'/'.$bcpage;
}
}
/** Start file tracking **/
// Full page string using _ format (used for file lookup)
$P['page'] = implode('_', $P['pages']);
// Full page URL based on pages array
// No beginning '/', and no variables
$P['pageurl'] = implode('/', $P['pages']);
$P['php'] = FALSE; // Determines if include file is PHP or HTML
$P['file'] = CPATH.$P['page']; // Full path to page file
$P['num'] = sizeof($P['pages']); // Number of sub-pages requested (nesting #)
/*
* This checks for a .html file in CPATH;
* If .html is not found, it looks for .inc.php
* If not found, it will go up the path tree until
* it finds any file it can.
*/
$notFound = $P['404'] ? FALSE : TRUE;
while ($notFound) {
// Makes sure that file exists AND is readable
if (is_readable($P['file'].'.html')) {
$P['file'] .= '.html';
$notFound = FALSE;
}
elseif (is_readable($P['file'].'.inc.php')) {
$P['file'] .= '.inc.php';
$P['php'] = TRUE;
$notFound = FALSE;
}
// If tryParents is disabled, just end here
if (!$cfg['p_tryParents']) break;
// If still not found, try to load parent page
if ($notFound) {
// Remove last ending to find parent file
$split = explode('_', $P['file']);
$sizeof = sizeof($split);
// Break out of loop, file still not found
if ($sizeof === 1) break;
// Create new file string
array_pop($split);
$P['file'] = implode('_', $split);
}
}
// Set 404 error if file is never found
if ($notFound) $P['404'] = TRUE;
// HARDCODED EXCEPTIONS
// 'sidebar' and 'error' are protected roots
// and used for internal files. These files are
// not accessible directly.
$stest = explode('.', $P['root'], 2);
if ($stest[0] === 'error' || $stest[0] === 'sidebar') {
$P['404'] = TRUE;
}
// Is this the index page? (Bool)
// 'indexnew' is hardcoded here, so you can have a 'non-public index'
// that still behaves like the real index page
define('ISINDEX', ($P['page'] === 'index' || $P['page'] === 'indexnew'));
// Run module event 'beforeContent'
tw_event('beforeContent');
// If 404 flag, use 404 functions
if ($P['404']) {
if (!p_showerror(404)) {
print 'Error 404 Times TWO: A 404 error occured, then the 404'.
'document could not be found. Please contact the administrator!';
exit;
}
}
elseif ($P['php']) {
/*
* Set the variables allowed by scripts
* These 3 variables should be over-ridden in the .inc.php scripts
* Other than that, these scripts can do anything.
* Including requesting authentication, accepting post data, etc.
* This simply locks you into the default layout provided
*/
// Sets the main header in template content area
$T['header'] = '';
// Used for <title> in template
// Usually same as header, but may be different in some cases
$T['title'] = '';
// Actual content put into main content div
$T['content'] = '';
// $P['file'] is include safe
// See path_escape in lib/security.inc.php
include $P['file'];
}
else {
/*
* Handle .html files
* NOTE: The first line of every .html file becomes the header
* and is stripped of all html
*/
// p_htmlfile returns array with header and content
// $file is include safe
$html = p_htmlfile($P['file']);
if (!$html && !p_showerror(404)) {
// This should really never happen
print 'Error 404 Times TWO: A 404-1 error occured, then the 404'.
'document could not be found. Please contact the administrator!';
exit;
}
$T['content'] = $html['content'];
$T['title'] = $T['header'] = $html['header'];
}
// Run module event 'duringProcess'
tw_event('duringProcess');
/*
* AJAX Check
* Returns content directly, skipping template entirely
*/
if ($cfg['p_ajax'] && AJAX) {
print $T['content'];
exit;
}
// Swap bread crumbs for full title (only if this isnt already set)
if ($T['header'] !== '' && $P['page'] !== strtolower($T['header'])) {
foreach ($T['bcrumbs'] AS $name => $url) {
// Locate pre-set header and remove
// Comparision: /page/ === /page.'/'; /page/ === /page/
if ($url === CURRURL || $url === CURRURL.'/') {
unset($T['bcrumbs'][$name]);
}
}
// Set new header for this URL (if not already set)
if (!isset($T['bcrumbs'][$T['header']])) {
$T['bcrumbs'][$T['header']] = CURRURL;
}
}
// Add previous pages to title format
// make sure to exclude the current page
// which should already be set
if ($cfg['p_crumbTitles'] && $T['title'] !== '') {
$tpages = array(); // Array to hold formatted title pages
$revpages = array_slice(array_reverse($P['pages']), 1);
foreach ($revpages AS $val) {
// Skip array values to prevent errors and recursion
if (is_array($val)) continue;
$tpages[] = p_url2name($val);
}
// Add to title (make sure its not empty)
if (sizeof($tpages) > 0) {
$T['title'] .= ' :: '.implode(' > ',$tpages);
}
}
// Remove any left-over characters from title
$T['title'] = str_replace("\r",'',str_replace("\n",'',trim($T['title'])));
// Exception for indexnew (dev/debug)
// this allows indexnew to load the normal index page resources
if ($P['page'] === 'indexnew') {
$P['root'] = $P['page'] = 'index';
}
/*
* Figure out which sidebar to load
* Possible options:
* sidebar.<rootpage>.(html|inc.php)
* sidebar.default.(html|inc.php)
*/
$sb = 'sidebar.'; // String justs helps make the lines smaller
$T['sidebar'] =
(is_file(CPATH.$sb.$P['root'].'.inc.php') ? CPATH.$sb.$P['root'].'.inc.php' :
(is_file(CPATH.$sb.$P['root'].'.html') ? CPATH.$sb.$P['root'].'.html' :
(is_file(CPATH.$sb.'default.inc.php') ? CPATH.$sb.'default.inc.php' :
(is_file(CPATH.$sb.'default.html') ? CPATH.$sb.'default.html' : ''))));
/* Add Resources */
// Default files
t_addRes('css', 'global', 2);
t_addRes('js', 'global', 2);
// Load subpage resources
if (!ISINDEX) {
t_addRes('css', 'subpage', 2);
t_addRes('js', 'subpage', 2);
}
// Load root page resources
// if checking parent, or if there is no parent
if ($cfg['res_checkRoot'] || $P['root'] === $P['page']) {
t_addRes('css', $P['root'], 3);
t_addRes('js', $P['root'], 3);
}
// Check for current page resources
// if current page different from root
if ($P['root'] !== $P['page']) {
t_addRes('css', $P['page'], 4);
t_addRes('js', $P['page'], 4);
}
/*
* Recursive Resource Checks
* (Applies to both css & js)
* First Make sure environment is sane
* 1: Check if enabled
* 2: Check if on a subpage
* 3: Not if res_checkRoot is TRUE && num pages is 2
*/
if ($cfg['res_recursive']) {
if ($P['root'] !== $P['page'] &&
!($cfg['res_checkRoot'] && $P['num'] === 2)) {
// Check all pages
$track = array();
foreach ($P['pages'] AS $k => $cp) {
$track[] = $cp;
// Skip current page (last item)
// and rootpage if checkRoot is TRUE (first item)
if ($k === ($P['num']-1) || ($cfg['res_checkRoot'] && $k === 0)) {
continue;
}
$curr = implode($track, '_');
t_addRes('css', $curr, 3);
t_addRes('js', $curr, 3);
}
}
}
// Run module event 'afterProcess'
tw_event('afterProcess');
// End of processing