-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.class
More file actions
468 lines (372 loc) · 14.4 KB
/
Copy pathapplication.class
File metadata and controls
468 lines (372 loc) · 14.4 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
/*
Copyright (C) 2002-2004 Ryan C. Creasey. All rights reserved.
Copyright (C) 2004-2005 Samuel J. Greear. All rights reserved.
$Id$
*/
@define('SITE_BASE', dirname(__FILE__) . '/../..');
require_once SITE_BASE . '/lib/php/global.php';
class Application {
public $Conf;
public $Request;
public $DB = NULL;
public $protectedURIs;
// Default depth to look for pages
protected $SubpageDepth = 4;
// Must be setup by our subclass in each site
protected $SitePath = NULL;
// Placeholder for the combined content
protected $Content = NULL;
// Individual pieces of the page, assembled into $Content
// These must be set by the pages we pull in based on $SitePath
public $AppTitle = NULL;
public $PageTitle = NULL;
public $Header = NULL;
public $Navigation = NULL;
public $Data = NULL;
public $Footer = NULL;
// microtime() of when we started page generation
private $StartTime;
public function __construct() {
$this->StartTime = microtime();
global $_CONF, $_APPREQ;
$this->Conf = &$_CONF;
$this->Request = &$_APPREQ;
$this->protectedURIs = array();
// Setup session
session_start();
}
public function PrepareContent() {
// Handle URL-requested errors
if ($this->Request['page'] == 'error') {
$this->HandleHTTPError((int)$this->Request['subpage'][0]);
}
// Check credentials
$this->URICheckpoint();
// Setup database
try {
$this->DB = Database::Connect($this, 'default');
} catch ( Exception $e) {
$except = new Para('exception');
$except->AddPara($e->getMessage());
$except->AddPara(backtrace($e->getTrace()));
$this->Data['backtrace'] = $except;
}
try {
if ($this->FindPage() != false) {
ex_debug('Serving: ' . $this->Request['includePath'], 2);
switch ($this->Request['contentHandler']) {
case 'php':
$this->HandlePHPContent( $this->Request['includePath'] );
break;
case 'xhtml':
$this->HandleXHTMLContent( $this->Request['includePath'] );
break;
case 'text':
$this->HandleTextContent( $this->Request['includePath'] );
break;
case 'native':
default:
$this->HandleNativeContent( $this->Request['includePath'] );
}
} else {
// Check the CMS
if (! $this->HandleCMSContent( $this->Request['uri'] )) {
// Throw a 404
$this->HandleHTTPError(404);
}
}
} catch (Exception $e) {
if ($this->Conf['debug_level'] >= 2) {
$except = new Para('exception');
$except->AddPara($e->getMessage());
$except->AddPara(backtrace($e->getTrace()));
$this->Data['backtrace'] = $except;
} else {
// XXX: Output to file
}
}
}
// XXX: Make most-specific first
private function FindPage() {
$depth = $this->SubpageDepth;
$subpage = $this->Request['subpage'];
$page_name = $this->Conf['defaults']['page_name'];
if ($this->Request['page'] != '/')
$page = $this->SitePath . '/' . $this->Request['page'];
else
$page = $this->SitePath;
$subpage_depth = count($subpage);
if ($subpage_depth < $depth)
$depth = $subpage_depth;
ex_debug("Subpage search depth is: $depth", 3);
for ($i = $depth; $i > 0; $i--) {
$basepage = $page;
for ($j = 0; $j < $i-1; $j++)
$basepage .= '/' . $subpage[$j];
if ($subpage[$i-1] != '') {
$include_file = $basepage . '/' . $subpage[$i-1];
if ($this->CheckPage($include_file))
return true;
}
$basepage .= '/' . $subpage[$i-1];
if ($subpage[$i-1] != '') {
$include_file = $basepage . '/' . $subpage[$i-1];
if ($this->CheckPage($include_file))
return true;
$include_file = $basepage . '/' . $page_name;
if ($this->CheckPage($include_file))
return true;
}
array_pop($subpage);
}
if ($this->Request['page'] != '/') {
$include_file = $page;
if ($this->CheckPage($include_file))
return true;
$include_file = $page . '/' . $this->Request['page'];
if ($this->CheckPage($include_file))
return true;
}
$include_file = $page . '/' . $page_name . '/' . $page_name;
if ($this->CheckPage($include_file))
return true;
$include_file = $page . '/' . $page_name;
if ($this->CheckPage($include_file))
return true;
return false;
}
private function CheckPage($path) {
static $pagecount = 0;
$pagecount++;
$extensions = array();
$extensions['native'] = $this->Conf['defaults']['page_extension'];
$extensions['php'] = '.php';
$extensions['xhtml'] = '.xhtml';
$extensions['text'] = '.txt';
ex_debug(sprintf("Checking page: %s: %s{%s}", $pagecount, $path, implode("|",$extensions)), 3);
foreach ($extensions as $handler => $page_extension) {
if (file_exists($path . $page_extension)) {
$this->Request['contentHandler'] = $handler;
$this->Request['includePath'] = $path . $page_extension;
return true;
}
}
return false;
}
private function SetupHead() {
if ($this->AppTitle != NULL)
$this->Content->AddElement('apptitle', $this->AppTitle);
if ($this->PageTitle != NULL)
$this->Content->AddElement('pagetitle', $this->PageTitle);
if ($this->Header != NULL && ! $this->Request['ajax'])
$this->Content->AddElement('header', $this->Header);
}
private function SetupBody() {
if ($this->Navigation != NULL && ! $this->Request['ajax']) {
if (is_array($this->Navigation)) {
foreach ($this->Navigation as $element) {
$navigation = new XMLement('navigation');
$navigation->AddChild($element);
$this->Content->AddChild($navigation);
}
} else {
$this->Content->AddChild($this->Navigation);
}
}
$content = NULL;
/* XXX: Better next-page-message mechanism */
/* ryan: I don't like this at all. */
/*
if (isset($_SESSION['message'])) {
$message = new Para('session_message');
$message->AddPara($_SESSION['message']);
if ($content == NULL) {
$content = new XMLement('content');
$content->SetAttribute('style', 'content');
}
$content->AddChild($message);
unset($_SESSION['message']);
}
*/
if ($this->Data != NULL) {
if (is_array($this->Data)) {
// XXX: Make recursive?
foreach($this->Data as $datakey => $dataval) {
if (is_array($dataval)) {
foreach ($dataval as $datakey1 => $dataval1) {
$element = new XMLement('content');
$element->SetAttribute('style', $datakey1);
$element->AddChild($dataval1);
$this->Content->AddChild($element);
}
} elseif (is_numeric($datakey)) {
if ($content == NULL) {
$content = new XMLement('content');
$content->SetAttribute('style', 'content');
}
$content->AddChild($dataval);
} else {
$element = new XMLement('content');
$element->SetAttribute('style', $datakey);
$element->AddChild($dataval);
$this->Content->AddChild($element);
}
}
} else {
if ($content == NULL) {
$content = new XMLement('content');
$content->SetAttribute('style', 'content');
}
$content->AddChild($this->Data);
}
}
if ($content != NULL)
$this->Content->AddChild($content);
}
private function SetupFoot() {
if ($this->Footer != NULL && ! $this->Request['ajax'])
$this->Content->AddElement('footer', $this->Footer);
if ($this->Conf['debug']) {
$debug = new Debug();
$this->Content->AddChild($debug);
}
}
public function Render() {
try {
$this->Content = new XMLement('document');
$this->Content->SetAttribute('version', '1.0');
$this->Content->SetAttribute('uri', $this->Request['uri']);
$this->SetupHead();
$this->SetupBody();
$this->SetupFoot();
if ($this->Request['format'] == 'xml') {
header('Content-Type: text/xml; charset=utf-8');
print $this->Content->Render();
} else {
header('Content-Type: text/html; charset=utf-8');
$xsl = new DOMDocument();
// try the site's xsl stylesheets first
if (file_exists(SITE_BASE . '/private/xsl/' . $this->Request['format'] . '.xsl'))
$xsl->load(SITE_BASE . '/private/xsl/' . $this->Request['format'] . '.xsl');
else
$xsl->load(SITE_BASE . '/lib/xsl/' . $this->Request['format'] . '.xsl');
/* XXX */
$rendered_xml = $this->Content->Render();
$xml = new DOMDocument();
$xml->loadXML($rendered_xml);
$this->OutputXHTML($xml, $xsl);
}
} catch (Exception $e) {
// XXX: This will never get output, switch this up to feed some error page through the
// (XXX nonexistant) template engine, and log error to file
$this->Content = new Para('exception');
$this->Content->AddPara($e->getMessage());
if ($this->Conf['debug_level'] >= 2)
$this->Content->AddPara(backtrace($e->getTrace()));
}
}
private function OutputXHTML($xml, $xsl) {
$proc = new XSLTProcessor();
$proc->registerPhpFunctions();
$proc->importStyleSheet($xsl);
$output = $proc->transformToXML($xml);
// XXX: This is a problem, we need to figure out WHY html
// special characters are being escaped in the first place
print strtr($output,
array_flip(get_html_translation_table(HTML_ENTITIES)));
}
public function HandleHTTPError($error) {
$this->Request['error'] = $error;
// clear the Data array
$this->Data = array();
require_once(SITE_LIB . '/http-error.php');
header(sprintf("HTTP/1.1 %d", $this->Request['error']));
$this->Render();
exit();
}
private function HandleCMSContent($uri) {
$query = 'select id, title, style from section where uri=:uri';
$handle = $this->DB->Prepare($query);
$handle->BindParam(':uri', $uri, PDO::PARAM_STR);
$handle->Execute();
$results = $handle->FetchAll();
if (count($results)) {
// only select the first row
$row = $results[0];
$select = 'select copy, style, weight ';
$from = 'from content ';
$where = 'where content.section=:section_id ';
$order = 'order by weight';
$query = $select . $from . $where . $order;
$handle = $this->DB->Prepare($query);
$handle->BindParam(':section_id', $row['id'], PDO::PARAM_INT);
$handle->Execute();
$result = $handle->Fetch();
$copy = new Para();
$copy->AddPara($result['copy']);
$this->Header = $row['style'];
$this->PageTitle = $row['title'];
$this->Data[] = $copy;
return $result;
} else {
return false;
}
}
private function HandleNativeContent($file) {
require_once($this->Request['includePath']);
return true;
}
private function HandlePHPContent($file) {
$this->Header = ' ';
$content = file_get_contents($file);
$phpContent = preg_replace_callback('/\<\?(php|=)?(.*)\?\>/i', "PHParser", $content);
$copy = new Para('raw');
$copy->AddPara($phpContent);
$this->Data[] = $copy;
return true;
}
private function HandleXHTMLContent($file) {
$this->Header = ' ';
$content = file_get_contents($file);
$copy = new Para('raw');
$copy->AddPara($content);
$this->Data[] = $copy;
return true;
}
private function HandleTextContent($file) {
$this->Header = ' ';
$content = file_get_contents($file);
$copy = new Para();
$copy->AddPara($content);
$this->Data[] = $copy;
return true;
}
private function URICheckpoint() {
$access = (@$_SESSION['user']->Class === NULL ) ? NULL : $_SESSION['user']->Class;
foreach( $this->protectedURIs as $uri ) {
if (strncmp($this->Request['uri'], $uri['name'], strlen($uri['name'])) == 0
&& !in_array($access, explode(",", $uri['clearance'])) ) {
Application::HandleHTTPError(403);
}
}
}
public function SetUriClearance($page, $level) {
$this->protectedURIs[] = array('name' => $page, 'clearance' => $level);
}
}
// XXX: Make this return X.XX usec, X.XX msec, X.XX sec, X.XX min
global $StartTime;
$StartTime = microtime(true);
function GenerationTime() {
global $StartTime;
return sprintf("%2.4f", (microtime(true) - $StartTime));
}
function PHParser($matches) {
ob_start();
eval( array_pop($matches) );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
?>