This repository has been archived by the owner on Nov 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
165 lines (138 loc) · 4.77 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
<?php
class TextLoft {
public static $title = "TextLoft";
public static $path = "pages";
public static $files;
public static $home;
public static $writeable = true;
public static $extension = ".md";
public static function go() {
TextLoft::$home = substr($_SERVER["REQUEST_URI"], 0, strrpos($_SERVER["REQUEST_URI"], "/")+1);
$title = $_GET["page"];
$page = urldecode($title);
$replace = array("/[.|..]/");
$page = preg_replace("/\.\.|\~|\/|\'|\`/", "", $page);
if (!$page) $page = "index";
if (!$title) $title = TextLoft::$title;
$filename = TextLoft::$path . "/" . $page . TextLoft::$extension;
$edit = stristr($_SERVER["REQUEST_URI"], "?edit");
$delete = stristr($_SERVER["REQUEST_URI"], "?delete");
$rename = stristr($_SERVER["REQUEST_URI"], "?rename");
if (TextLoft::$writeable === true):
if ($rename) {
$new_page = $_POST["rename-page-title"];
file_put_contents(TextLoft::$path . "/" . $new_page . TextLoft::$extension, file_get_contents($filename));
unlink($filename);
header("Location:$new_page");
exit();
}
if ($delete && file_exists($filename)) {
unlink($filename);
header("Location:" . TextLoft::$home);
exit();
}
endif;
if (!file_exists(TextLoft::$path)) mkdir(TextLoft::$path);
$files = scandir(TextLoft::$path);
foreach($files as $key=>$file) {
if ($file == "." || $file == "..") {
unset ($files[$key]);
}else{
$files[$key] = str_replace(TextLoft::$extension, "", $file);
}
}
TextLoft::$files = $files;
if ((!file_exists($filename) || $edit) && TextLoft::$writeable) {
// File empty; show editor
if ($_POST) {
$content = $_POST["wiki-content"];
TextLoft::create($filename, $content);
header("Location:$page");
}else{
$current_content = $edit ? file_get_contents($filename) : "# ". ucwords($title) . "\n";
TextLoft::header("$page", $edit);
TextLoft::editor($current_content);
TextLoft::footer($edit);
}
}elseif(file_exists($filename)){
require("markdown_extended.php");
$contents = file_get_contents($filename);
TextLoft::header($page, $edit);
echo MarkDownExtended($contents);
TextLoft::footer($edit);
}else{
header(' ', true, 404);
exit();
}
}
public static function editor($current_content) {
?>
<form class="editor" action="" method="post">
<textarea name="wiki-content" id="wiki-content" cols="30" rows="10"><?= $current_content ?></textarea> <br/>
<input type="submit" value="Save">
<br/> <a href="#" id="wiki-cancel">Cancel</a>
</form>
<?php
}
public static function create ($filename, $content) {
$content = stripcslashes($content);
file_put_contents($filename, $content);
}
public static function header($title, $edit) {
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title><?= $title ?></title>
<link rel="stylesheet" href="<?= TextLoft::$home ?>css/style.css">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content = "width = device-width, initial-scale = 1, user-scalable = no" />
<link rel="shortcut icon" href="<?= TextLoft::$home ?>favicon.png">
<link rel="apple-touch-icon-precomposed" href="<?= TextLoft::$home ?>apple-touch-icon.png">
<link rel="apple-touch-startup-image" href="apple-touch-startup-image.png">
</head>
<body>
<div class="wrap">
<header>
<a class="home" href="<?= TextLoft::$home ?>"><?= TextLoft::$title ?></a>
»
<?php if ($title != "index" && !$edit): ?>
<form action="?rename" id="rename-page" method="post">
<input type="text" value="<?= $title ?>" name="rename-page-title" data-old="<?= $title ?>" id="rename-page-title" />
</form>
<?php else:?>
<?= $title ?>
<?php endif ?>
<?php if(count(TextLoft::$files)): ?>
<select id="wiki-jump">
<option value="#" >→ Jump</option>
<?php foreach(TextLoft::$files as $file): ?>
<option value="<?= $file ?>"><?= $file ?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</header>
<section role="main">
<?php
}
public static function footer($edit) {
?>
</section>
<footer>
<div class="group">
<?php if (!$edit && TextLoft::$writeable): ?> <a href="#" id="wiki-edit">Edit</a> | <a href="#" id="wiki-delete">Delete</a>
</div>
<?php endif ?>
<form class="wiki-new-page"><input type="text" size=10 name="wiki-page" placeholder="Enter New Page"/> <input type="submit" value="Add"></form>
</footer>
</div>
<script src="<?= TextLoft::$home ?>js/jquery.js"></script>
<script src="<?= TextLoft::$home ?>js/textloft.js"></script>
</body>
</html>
<?php
}
}
TextLoft::go();