-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.php
143 lines (109 loc) · 3.72 KB
/
app.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
<?php namespace Silverplate;
require 'vendor/autoload.php';
class Http404 extends \Exception {
}
class Http302 extends \Exception {
private $location;
public function __construct($location) {
$this->location = $location;
}
public function getLocation() {
return $this->location;
}
}
class App {
const
MD = 'md',
PHP = 'php',
REDIR = 'redir';
protected static $path = null;
public static function uri() {
return trim($_GET['p'], '/');
}
public static function path($filename='') {
if(static::$path === null) {
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http';
$server = $_SERVER['SERVER_NAME'];
$directory = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
static::$path = sprintf("%s://%s%s/", $protocol, $server, $directory);
}
return static::$path . $filename;
}
public function get_pathname($filename) {
$filename = __DIR__ . '/' . $filename;
if(!file_exists($filename)) {
return false;
}
$pathname = realpath($filename);
if(!$pathname) {
return false;
}
if(strpos($pathname, __DIR__) !== 0) {
return false;
}
return $pathname;
}
public function find_file($filename) {
if(is_dir($filename)) {
$filename = rtrim($filename, '/') . '/index';
}
foreach(array(App::MD, App::PHP, App::REDIR) as $extension) {
if($pathname = $this->get_pathname($filename . '.' . $extension)) {
return $pathname;
}
}
throw new Http404;
}
public function run() {
$uri = static::uri();
try {
if(!$uri) {
throw new Http404;
}
if(in_array($uri, array('404', 'layout', 'app'))) {
throw new Http404;
}
$pathname = $this->find_file($uri);
$extension = pathinfo($pathname, PATHINFO_EXTENSION);
if($extension == App::REDIR) {
throw new Http302(trim(file_get_contents($pathname)));
}
if($extension == App::PHP) {
$content = $this->renderHTML($pathname);
} elseif($extension == App::MD) {
$content = $this->renderMD($pathname);
}
$response = $this->renderLayout($content, $extension);
} catch(Http404 $e) {
header("HTTP/1.1 404 Not Found");
$response = $this->renderHTML('404.php');
} catch(Http302 $e) {
header("HTTP/1.1 302 Found");
header("Location: " . $e->getLocation());
exit();
}
echo $response;
}
public function parseMDMeta($file_contents) {
return preg_replace_callback('/^Meta\s+([^:]+):(.*)$/mi', function($matches) {
meta(trim($matches[1]), trim($matches[2]));
return '';
}, $file_contents);
}
public function renderMD($pathname) {
$parser = new \dflydev\markdown\MarkdownParser;
$contents = $this->parseMDMeta(file_get_contents($pathname));
return $parser->transformMarkdown(str_replace('path://', App::path(), $contents));
}
public function renderLayout($content, $extension) {
return $this->renderHTML($this->get_pathname(get('layout', 'layout') . '.php'), array('content' => $content, 'type' => $extension . '-file'));
}
public function renderHTML($pathname, $data=array()) {
extract($data);
ob_start();
include $pathname;
return ob_get_clean();
}
}
$app = new App;
$app->run();