forked from sunny/edith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
133 lines (103 loc) · 3.2 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
<?php
/*
* Edith's dispatching controller.
*
* RESTfully answers to GET, HEAD, POST, PUT and DELETE to these resources:
* /{pagename}
* /{pagename}.{representation}
*/
@include 'config.php';
if (!defined('EDITH_URI'))
die('Please copy config.php.example to config.php');
if (!is_dir(EDITH_DATA_PATH))
die(EDITH_DATA_PATH . " is not a directory");
// all representations are in the templates directory
$representations = array();
foreach (glob('templates/*.php') as $file)
$representations[] = basename($file, '.php');
// regular expression to distinguish page and extension
if (!defined('URI_REGEX'))
define('URI_REGEX', '#^/?([^/]+?)\.?('.implode('|', $representations).')?$#');
// include libraries
require 'lib/helpers.php';
require 'lib/page.class.php';
// find page and repr from request
$method = $_SERVER['REQUEST_METHOD'];
$request_uri = substr($_SERVER['REQUEST_URI'], strlen(dirname($_SERVER['PHP_SELF'])));
preg_match(URI_REGEX, $request_uri, $request_matches);
$page = new Page(isset($request_matches[1]) ? $request_matches[1] : '');
$page_exists = $page->exists();
$representation = isset($request_matches[2]) ? $request_matches[2] : '';
// don't allow pages with unsafe names
if (!$page->has_safe_name()) {
header('HTTP/1.0 404 Not Found');
exit('The page name can only contain dashes, dots and alphanumerical characters.');
}
// /{pagename}.{representation}
if ($representation != '') {
if (!$page_exists) {
header('HTTP/1.0 404 Not Found');
die("404 Not Found: $page->name");
}
if (!in_array($representation, $representations)) {
header('HTTP/1.0 404 Not Found');
die('Representation can only be one of: '.implode($representations, ', '));
}
switch ($method) {
case 'GET': case 'HEAD':
$page->load();
require "templates/$representation.php";
exit;
case 'POST': case 'PUT': case 'DELETE':
header('HTTP/1.0 405 Method Not Allowed');
header('Allow: GET, HEAD');
exit;
default:
header('HTTP/1.0 501 Not Implemented');
header('Allow: GET, HEAD');
exit;
}
}
// /{pagename}
switch ($method) {
case 'GET': case 'HEAD':
if (!$page_exists)
header('HTTP/1.0 404 Not Found');
header('Content-type: text/html');
$page->load();
$template = 'default';
if (!$page->is_writeable())
if ($page_exists)
$template = 'html';
else
die("Sorry but you cannot create new pages");
require "templates/$template.php";
exit;
case 'DELETE':
if (!$page_exists)
header('HTTP/1.0 404 Not Found');
else
$page->delete();
exit;
case 'PUT': case 'POST':
$page->text = request_var('text');
try {
$saved = $page->save();
} catch (Exception $e) {
$saved = $e->getMessage();
}
if ($saved !== true) {
header('HTTP/1.0 500 Internal Server Error');
die($saved ? $saved : 'Error saving page.');
}
if (!$page_exists)
header('HTTP/1.0 201 Created');
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
exit('Saved successfully!');
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
default:
header('HTTP/1.0 501 Not Implemented');
header('Allow: GET, HEAD');
exit;
}