This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.php
468 lines (395 loc) · 10.5 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
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
/**
* WikWiki - simple wiki in one PHP file. http://smasty.net/wikwiki
* Copyright (c) 2011 Martin Srank, http://smasty.net
*
* Licensed under the terms and conditions of
* the MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
define('PAGE_TITLE', 'WikWiki');
define('BASE_PAGE', 'Home Page');
define('FOOTER_TEXT', 'Copyright %Y Martin Srank. | Powered by <a href="https://github.com/smasty/WikWiki">WikWiki</a>.');
@set_magic_quotes_runtime(false);
if(!@file_exists(dirname(__FILE__) . '/wikdata') || !@is_writable(dirname(__FILE__) . '/wikdata')){
$ok = @mkdir(dirname(__FILE__) . '/wikdata');
if(!$ok){
die('WikWiki cannot access the data directory ./wikdata/.
Please create the directory and make it writeable by PHP.');
}
}
$msg = '';
// Instantiate Texy parser.
require dirname(__FILE__) . '/texy.min.php';
$texy = new Texy();
$texy->encoding = 'utf-8';
$texy->headingModule->top = 2;
$texy->headingModule->generateID = true;
$texy->allowed['image'] = FALSE;
$texy->registerLinePattern('parseWikiLinks', '~\[([^|\]]+)(?:\s*\|\s*([^\]]+)\s*)?\]~', 'wikilinks');
// init path
$page = parseQueryString(@$_SERVER['QUERY_STRING']);
if(empty($_GET)){
$page = titleToId(BASE_PAGE);
}
// Save content.
if(!empty($_POST)){
if(!savePageContent($_POST)){
$msg = 'Edit failed. Please, try again.';
}
}
// Edit/create page
if(array_key_exists('edit', $_GET)){
$title = idToTitle($_GET['edit']);
printHeader(!$title ? "Create new page" : "Edit page '$title'");
printEdit($title);
printFooter($title);
exit;
}
// Backlinks
elseif(isset($_GET['backlinks']) && pageExists($_GET['backlinks'])){
$title = idToTitle($_GET['backlinks']);
printHeader("Backlinks for '$title'");
printBacklinks($title);
printFooter($title);
exit;
}
elseif(isset($_GET['recent'])){
$count = $_GET['recent'];
if(!is_numeric($count)){
$count = 10;
}
printHeader("$count Most Recent Changes");
printRecentChanges($count);
printFooter();
exit;
}
// Show page
elseif($page){
$title = idToTitle($page);
printHeader($title);
printContent($title);
printFooter($title);
exit;
}
else{
header('Location: ./?Special:NotFound');
exit;
}
/**
* Get page ID form HTTP query-string.
* @param string $queryString
* @return strng|false
*/
function parseQueryString($queryString){
return preg_match('~\w+=.*~', $queryString) ? false : $queryString;
}
/**
* Texy parser for Wiki links syntax.
* @param TexyParser $parser
* @param array $matches
* @param string $name
* @return TexyHtml|string
*/
function parseWikiLinks($parser, $matches, $name){
$page = trim($matches[1]);
$id = titleToId($matches[1]);
$el = TexyHtml::el('a')
->href("?$id")
->setText(isset($matches[2]) ? $matches[2] : $page);
if(!pageExists($page)){
$el->class = 'new';
$el->title = "Create page '$page'";
$el->href("?edit=$id");
}
return $el;
}
/**
* Convert page title to ID.
* @param string $title
* @return string
*/
function titleToId($title){
return preg_replace('~\s+~i', '_', trim($title));
}
/**
* Convert ID to page title.
* @param string $id
* @return string
*/
function idToTitle($id){
return str_replace('_', ' ', $id);
}
/**
* Print HTML header.
* @param string $page
* @return void
*/
function printHeader($page = BASE_PAGE){
global $msg;
$message = $msg ? "<div class=\"msg\">$msg</div>" : '';
$html_title = "$page | " . PAGE_TITLE;
echo <<<PAGE_HEAD
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>$html_title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
<div id="main">
$message
<h1>$page</h1>
<div id="content">
PAGE_HEAD;
}
/**
* Print HTML footer.
* @param string $page
* @return void
*/
function printFooter($page = BASE_PAGE){
$sidebar = getSidebar($page);
$footer = strftime(FOOTER_TEXT);
echo <<<PAGE_FOOT
</div>
</div>
<div id="sidebar">
$sidebar
</div>
<div id="footer">
$footer
</div>
</div>
</body>
</html>
PAGE_FOOT;
}
/**
* Generate HTML sidebar.
* @param string $page
* @return void
*/
function getSidebar($page = BASE_PAGE){
$title = PAGE_TITLE;
$id = titleToId($page);
$mod = 'not yet.';
$toc = $bl = '';
if(pageExists($page)){
$mod = date('d.m.Y, H:i:s', filemtime(getFilePath($page)));
$toc = generateToc();
$bl = "<li class=\"backlinks\"><a href=\"./?backlinks=$id\">Backlinks</a></li>";
}
return <<<PAGE_SIDEBAR
<p id="title"><a href="./">$title</a></p>
<ul class="sidebar-list">
<li class="edit-link"><a href="./?edit=$id">Edit page</a></li>
$bl
<li class="modified">Last modified: <em>$mod</em></li>
<li class="create-new-link"><a href="./?edit=">Create new page</a></li>
<li class="recent-changes-link"><a href="./?recent=10">Recent changes</a></li>
</ul>
$toc
PAGE_SIDEBAR;
}
/**
* Print the content of the page.
* @global Texy $texy
* @param string $page
* @return void
*/
function printContent($page = BASE_PAGE){
global $texy;
if(pageExists($page)){
echo $texy->process(getContent($page));
} elseif(pageExists('Special:NotFound')){
echo $texy->process(str_replace('%PAGE%', $page, getContent('Special:NotFound')));
} else{
echo $texy->process("The page you're looking for does not exist at the moment. "
. "However, you can [$page | create it] right now.");
}
}
/**
* Print HTML for edit <form>.
* @param string $page
* @return void
*/
function printEdit($page){
$title = $content = $id = '';
if($page){
$title = $page;
$id = titleToId($page);
if(pageExists($page)){
$content = getContent($page);
}
}
echo <<<EDIT_FORM
<form action="./?edit=$id" method="post" id="edit-form">
<p id="edit-block-title" class="edit-block">
<label for="edit-title">Page:</label>
<input type="text" id="edit-title" name="title" value="$title">
</p>
<div id="edit-block-content" class="edit-block">
<label for="edit-content">Content:</label>
<textarea name="content" id="edit-content" rows="15" cols="80">$content</textarea>
</div>
<p id="edit-block-submit" class="edit-block">
<button type="submit">Save changes</button>
<a href="./?$id">Cancel</a>
</p>
<p id="edit-block-help" class="edit-block">
You can use <a href="http://texy.info/en/syntax">Texy! syntax</a> and some HTML too.<br>
<small>Use [Sample Page] to link to "Sample Page". Use [Sample Page | link to sample] for custom label.</small>
</form>
EDIT_FORM;
}
/**
* Print HTML for backlinks.
* @global Texy $texy
* @param string $page
* @return void
*/
function printBacklinks($page){
global $texy;
$s = "[$page | Go back to page]
This is a list of all pages that link to [$page].\n";
$l = array();
foreach(new FilesystemIterator(dirname(__FILE__) . '/wikdata', FilesystemIterator::SKIP_DOTS) as $file){
if(checkBacklink($file, $page)){
$l[] = "- [" . fileToTitle($file) . "]";
}
}
if(empty($l)){
$s .= "\nNo backlinks found.";
} else{
$s .= ".[#backlinks-list]\n" . implode("\n", $l);
}
echo $texy->process($s);
}
/**
* Check file for baclinks to $page.
* @param string $file
* @param string $page
* @return string
*/
function checkBacklink($file, $page){
$c = file_get_contents($file);
return preg_match("~\[$page(\s*\|\s*[^\w]*)?]~m", $c);
}
/**
* Convert file path to title.
* @param string $file
* @return string
*/
function fileToTitle($file){
return idToTitle(pathinfo($file, PATHINFO_FILENAME));
}
/**
* Print HTML for recent changes.
* @global Texy $texy
* @param int $count
* @return void
*/
function printRecentChanges($count){
global $texy;
$list = array();
foreach(new FilesystemIterator(dirname(__FILE__) . '/wikdata', FilesystemIterator::SKIP_DOTS) as $file){
$list[filemtime($file)] = fileToTitle($file);
}
krsort($list);
$list = array_slice($list, 0, $count, true);
$s = '';
foreach($list as $t => $l){
$s .= "\n- [$l] (" . date("d.m.Y, H:i:s", $t) . ")";
}
echo $texy->process($s);
}
/**
* Get file path for page.
* @param string $page
* @return string
*/
function getFilePath($page){
return dirname(__FILE__) . '/wikdata/' . titleToId($page) . '.wik';
}
/**
* Checks for existence of page.
* @param string $page
* @return bool
*/
function pageExists($page){
$path = getFilePath($page);
return file_exists($path) && is_readable($path);
}
/**
* Get the content of the page.
* @param string $page
* @return string|false
*/
function getContent($page){
$path = getFilePath($page);
if(pageExists($page)){
return file_get_contents($path);
}
return false;
}
/**
* Save page.
* @param array $fields
* @return bool
*/
function savePageContent($fields){
if(@get_magic_quotes_gpc()){
$fields = array_map(stripslashes, $fields);
}
$file = getFilePath($fields['title']);
$do = file_put_contents($file, trim($fields['content']));
if($do){
header("Location: ./?" . titleToId($fields['title']));
exit;
} else{
return false;
}
}
/**
* Generate table of contents HTML blockz.
* @global Texy $texy
* @return TexyHtml
*/
function generateToc(){
global $texy;
if(!$texy->headingModule->TOC){
return '';
}
$block = TexyHTML::el('div');
$block->id = 'toc';
$block->create('h3', 'Contents');
$toc = TexyHTML::el('ul');
$block->add($toc);
$lists[0] = $toc;
$aList = 0;
$level = 2;
foreach($texy->headingModule->TOC as $heading){
if($heading['level'] > $level){
for($level; $heading['level'] > $level; ++$level){
if($lists[$aList]->count() != 0){
$ul = $lists[$aList][$lists[$aList]->count() - 1]->create('ul');
} else{
$li = $lists[$aList]->create('li');
$ul = $li->create('ul');
}
$lists[] = $ul;
}
$aList = count($lists) - 1;
} elseif($heading['level'] < $level){
$diff = $level - $heading['level'];
$lists = array_slice($lists, 0, - $diff);
$level = $heading['level'];
}
$aList = count($lists) - 1;
$li = $lists[$aList]->create('li');
$a = $li->create('a')->href('#' . $heading['el']->attrs['id'])->setText($heading['title']);
}
return $block->toHtml($texy);
}