forked from isofarro/php5-markdown-wiki
/
markdown-wiki.php
430 lines (353 loc) · 9.87 KB
/
markdown-wiki.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
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
<?php
class MarkdownWiki {
// Wiki default configuration. All overridableindex
protected $config = array(
'docDir' => '/tmp/',
'defaultPage' => 'index',
'newPageText' => 'Start editing your new page',
'markdownExt' => 'markdown'
);
// An instance of the Markdown parser
protected $parser;
protected $baseUrl;
public function __construct($config=false) {
$this->initWiki();
if ($config) {
$this->setConfig($config);
}
}
protected function initWiki() {
$baseDir = dirname(__FILE__) . '/';
// Including the markdown parser
//echo "BaseDir: {$baseDir}\n";
require_once $baseDir . 'markdown.php';
}
public function wikiLink($link) {
global $docIndex;
$isNew = false;
$wikiUrl = $link;
if (preg_match('/^\/?([a-z0-9-]+(\/[a-z0-9-]+)*)$/i', $link, $matches)) {
$wikiUrl = "{$this->baseUrl}{$matches[1]}";
$isNew = !$this->isMarkdownFile($link);
} elseif ($link=='/') {
$wikiUrl = "{$this->baseUrl}{$this->config['defaultPage']}";
$isNew = !$this->isMarkdownFile($this->config['defaultPage']);
}
return array($isNew, $wikiUrl);
}
public function isMarkdownFile($link) {
$filename = "{$this->config['docDir']}{$link}.{$this->config['markdownExt']}";
return file_exists($filename);
}
public function setConfig($config) {
$this->config = array_merge($this->config, $config);
}
public function handleRequest($request=false, $server=false) {
$action = $this->parseRequest($request, $server);
$action->model = $this->getModelData($action);
// If this is a new file, switch to edit mode
if ($action->model->updated==0 && $action->action=='display') {
$action->action = 'edit';
}
$action->response = $this->doAction($action);
$output = $this->renderResponse($action->response);
//echo '<pre>'; print_r($action); echo '</pre>';
}
##
## Methods handling each action
##
public function doAction($action) {
switch($action->action) {
case 'UNKNOWN': # Default to display
case 'display':
$response = $this->doDisplay($action);
break;
case 'edit':
$response = $this->doEdit($action);
break;
case 'preview':
$response = $this->doPreview($action);
break;
case 'save':
$response = $this->doSave($action);
break;
case 'history':
case 'admin':
case 'browse':
default:
$response = array(
'messages' => array(
"Action {$action->action} not implemented."
)
);
print_r($action);
break;
}
return $response;
}
protected function doDisplay($action) {
$response = array(
'title' => "Displaying: {$action->page}",
'content' => $this->renderDocument($action),
'editForm' => '',
'options' => array(
'Edit' => "{$action->base}{$action->page}?action=edit&id={$action->page}"
),
'related' => ''
);
return $response;
}
protected function doEdit($action) {
$response = array(
'title' => "Editing: {$action->page}",
'content' => '',
'editForm' => $this->renderEditForm($action),
'options' => array(
'Cancel' => "{$action->base}{$action->page}"
),
'related' => ''
);
return $response;
}
protected function doPreview($action) {
$response = array(
'title' => "Editing: {$action->page}",
'content' => $this->renderPreviewDocument($action),
'editForm' => $this->renderEditForm($action),
'options' => array(
'Cancel' => "{$action->base}{$action->page}"
),
'related' => ''
);
return $response;
}
protected function doSave($action) {
// TODO: Implement some sort of versioning
if (empty($action->model)) {
// This is a new file
echo "INFO: Saving a new file\n";
} elseif ($action->model->updated==$action->post->updated) {
// Check there isn't an editing conflict
$action->model->content = $action->post->text;
$this->setModelData($action->model);
} else {
echo "WARN: Editing conflict!\n";
}
return $this->doDisplay($action);
}
##
## Methods dealing with the model (plain old file system)
##
protected function getModelData($action) {
$data = (object) NULL;
$data->file = $this->getFilename($action->page);
$data->content = $this->getContent($data->file);
$data->updated = $this->getLastUpdated($data->file);
return $data;
}
protected function setModelData($model) {
$directory = dirname($model->file);
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
} elseif (!is_dir($directory)) {
echo "ERROR: Cannot create {$model->file}\n";
}
file_put_contents($model->file, $model->content);
}
##
## Methods for parsing the incoming request
##
public function parseRequest($request=false, $server=false) {
$action = (object) NULL;
if (!$request) { $request = $_REQUEST; }
if (!$server) { $server = $_SERVER; }
//echo "Request: "; print_r($request);
//echo "Server : "; print_r($server);
$action->method = $this->getMethod($request, $server);
$action->page = $this->getPage($request, $server);
$action->action = $this->getAction($request, $server);
$action->base = $this->getBaseUrl($request, $server);
if ($action->method=='POST') {
$action->post = $this->getPostDetails($request, $server);
}
// Take a copy of the action base for the wikiLink function
$this->baseUrl = $action->base;
return $action;
}
protected function getFilename($page) {
return "{$this->config['docDir']}{$page}.{$this->config['markdownExt']}";
}
protected function getContent($filename) {
if (file_exists($filename)) {
return file_get_contents($filename);
}
return $this->config['newPageText'];
}
protected function getLastUpdated($filename) {
if (file_exists($filename)) {
return filectime($filename);
}
return 0;
}
protected function getMethod($request, $server) {
if (!empty($server['REQUEST_METHOD'])) {
return $server['REQUEST_METHOD'];
}
return 'UNKNOWN';
}
protected function getPage($request, $server) {
$page = '';
// Determine the page name
if (!empty($server['PATH_INFO'])) {
//echo "Path info detected\n";
// If we are using PATH_INFO then that's the page name
$page = substr($server['PATH_INFO'], 1);
} elseif (!empty($request['id'])) {
$page = $request['id'];
} else {
// TODO: Keep checking
//echo "WARN: Could not find a pagename\n";
}
// Check whether a default Page is being requested
if ($page=='' || preg_match('/\/$/', $page)) {
$page .= $this->config['defaultPage'];
}
return $page;
}
protected function getAction($request, $server) {
if ($server['REQUEST_METHOD']=='POST') {
if (!empty($request['preview'])) {
return 'preview';
} elseif (!empty($request['save'])) {
return 'save';
}
} elseif (!empty($request['action'])) {
return $request['action'];
} elseif (!empty($server['PATH_INFO'])) {
return 'display';
}
// TODO: handle version history etc.
return 'UNKNOWN';
}
protected function getBaseUrl($request, $server) {
if (!empty($this->config['baseUrl'])) {
return $this->config['baseUrl'];
}
/**
PATH_INFO $_SERVER keys
[SERVER_NAME] => localhost
[DOCUMENT_ROOT] => /home/user/sites/default/htdocs
[SCRIPT_FILENAME] => /home/user/sites/default/htdocs/index-sample.php
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[REQUEST_URI] => /index-sample.php
[SCRIPT_NAME] => /index-sample.php
[PHP_SELF] => /index-sample.php
**/
$scriptName = $server['SCRIPT_NAME'];
$requestUrl = $server['REQUEST_URI'];
$phpSelf = $server['PHP_SELF'];
if ($requestUrl==$scriptName) {
// PATH_INFO based
} elseif(strpos($requestUrl, $scriptName)===0) {
// Query string based
} else {
// Maybe mod_rewrite based?
// Perhaps we need a config entry here
}
return '/index-sample.php/'; // PATH-INFO base
}
protected function getPostDetails($request, $server) {
$post = (object) NULL;
$post->text = stripslashes($request['text']);
$post->updated = $request['updated'];
return $post;
}
/*********
RESPONSE RENDERERS
*********/
public function renderResponse($response) {
if (!empty($this->config['layout'])) {
// TODO: Use a custom template
} else {
$footer = array();
if (!empty($response['options'])) {
$footer[] = '<ul>';
foreach($response['options'] as $label=>$link) {
$footer[] = <<<HTML
<li><a href="{$link}">{$label}</a></li>
HTML;
}
$footer[] = '</ul>';
}
$response['footer'] = implode("\n", $footer);
echo <<<PAGE
<html lang="en-GB">
<head>
<title>{$response['title']}</title>
</head>
<body>
<div id="page">
<div id="head"></div>
<div id="content">
{$response['content']}
{$response['editForm']}
</div>
<div id="related">
{$response['related']}
</div>
<div id="foot">
{$response['footer']}
</div>
</div>
</body>
</html>
PAGE;
}
}
protected function renderDocument($action) {
return Markdown(
$action->model->content,
array($this, 'wikiLink')
);
}
protected function renderPreviewDocument($action) {
return Markdown(
$action->post->text,
array($this, 'wikiLink')
);
}
protected function renderEditForm($action) {
if (!empty($action->post)) {
$form = array(
'raw' => $action->post->text,
'updated' => $action->post->updated
);
} else {
$form = array(
'raw' => $action->model->content,
'updated' => $action->model->updated
);
}
return <<<HTML
<form action="{$action->base}{$action->page}" method="post">
<fieldset>
<legend>Editing</legend>
<label for="text">Content:</label><br>
<textarea cols="78" rows="20" name="text" id="text">{$form['raw']}</textarea>
<br>
<input type="submit" name="preview" value="Preview">
<input type="submit" name="save" value="Save">
<input type="hidden" name="updated" value="{$form['updated']}">
</fieldset>
</form>
HTML;
}
}
if (!empty($_SERVER['REQUEST_URI'])) {
# Dealing with a web request
$wiki = new MarkdownWiki($config);
$wiki->handleRequest();
//print_r($wiki);
}
?>