/
index.php
280 lines (260 loc) · 6.82 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
<?php
$base_url = 'http://localhost/piki/';
$admin_name = 'admin';
$admin_password = 'password';
$db = null;
function logger($message) {
error_log(date('Y-m-d H:i:s') . ': ' . $message . "\n", 3, 'piki.log');
}
function init_db($db_file_name) {
global $db;
$need_init_db = !file_exists($db_file_name);
$dsn = 'sqlite:' . $db_file_name;
$db = new PDO($dsn);
if (!$db) {
die('failed to open sqlite.');
}
if ($need_init_db) {
$sql = <<<SQL
create table pages (
id integer primary key,
title varchar(255) unique,
body text,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp
)
SQL;
$result = $db->query($sql);
$sql = <<<SQL
insert into pages
(id, title, body)
values
(1, 'index', '...')
SQL;
$result = $db->query($sql);
}
}
function insert($title, $body) {
global $db;
if ($title == null) {
return false;
}
$stmt = $db->prepare('insert into pages (title, body) values (:title, :body)');
$stmt->execute(array(':title' => $title, ':body' => $body));
return $db->lastInsertId();
}
function update($id, $title, $body) {
global $db;
$stmt = $db->prepare('update pages set title = :title, body = :body, updated_at = current_timestamp where id = :id');
return $stmt->execute(array(':id' => $id, ':title' => $title, ':body' => $body));
}
function delete($id) {
global $db;
if ($id != '1') {
$stmt = $db->prepare('delete from pages where id = :id');
return $stmt->execute(array(':id' => $id));
}
}
function select($id) {
global $db;
$stmt = $db->prepare('select * from pages where id = :id');
$stmt->execute(array(':id' => $id));
return $stmt->fetch();
}
function select_all() {
global $db;
return $db->query('select id, title from pages order by updated_at desc');
}
function login() {
global $admin_name, $admin_password;
if (!isset($_SERVER["PHP_AUTH_USER"]) || ($_SERVER["PHP_AUTH_USER"] != $admin_name || $_SERVER["PHP_AUTH_PW"] != $admin_password)) {
header("WWW-Authenticate: Basic realm=\"Please Enter Your Password\"");
header("HTTP/1.0 401 Unauthorized");
echo "Authorization Required";
exit;
}
}
function post() {
global $base_url;
$action = $_POST['action'];
if ($action == 'new') {
$id = insert($_POST['title'], $_POST['body']);
header("Location: " . $base_url . "?id=" . $id);
} else if ($action == 'edit') {
update($_POST['id'], $_POST['title'], $_POST['body']);
header("Location: " . $base_url . "?id=" . $_POST['id']);
} else if ($action == 'delete') {
delete($_POST['id']);
header("Location: " . $base_url);
}
}
function get() {
global $base_url;
$id = $_GET['id'];
$action = $_GET['action'];
$edit_link = '';
$contents = '';
$title = '';
$page_link = '';
if ($action == 'edit') {
login();
if (!$id) {
$id = 1;
}
$piki_page = select($id);
if (!$piki_page) {
header("Location: " . $base_url . "?action=new");
exit;
}
$title = htmlspecialchars($piki_page['title']);
$body = htmlspecialchars($piki_page['body']);
$cansel_url = $base_url . '?id=' . $id;
$title_read_only = '';
$contents = <<<EOS
<form method="post">
<input type="text" name="title" value="$title" size="30" /><br />
<textarea name="body" rows=20 cols=80>$body</textarea><br />
<input type="hidden" name="id" value="$id" />
<input type="hidden" name="action" value="edit" />
<input type="submit" value="Save" />
<a href="$cansel_url">Cansel</a>
</form>
EOS;
if ($id != 1) {
$contents .= <<<EOS
<form method="post" onSubmit="return confirm('Are you sure?')">
<input type="hidden" name="id" value="$id" />
<input type="hidden" name="action" value="delete" />
<input type="submit" value="Delete" />
</form>
EOS;
}
} else if ($action == 'new') {
login();
$cansel_url = $base_url;
$contents = <<<EOS
<form method="post">
<input type="text" name="title" value="$title" size="30" /><br />
<textarea name="body" rows=20 cols=80></textarea><br />
<input type="submit" value="Save" />
<input type="hidden" name="action" value="new" />
<a href="$cansel_url">Cansel</a>
</form>
EOS;
} else {
if (!$id) {
$id = 1;
}
$piki_page = select($id);
if (!$piki_page) {
header("Location: " . $base_url . "?action=new");
exit;
}
$title = htmlspecialchars($piki_page['title']);
$page_link = "<a href=\"$base_url?id=$id\">$title</a>";
$edit_link = "<a class=\"action\" href=\"$base_url?id=$id&action=edit\">edit</a>";
$contents = '<h2>' . $page_link . '</h2>' . $edit_link;
$contents .= '<pre>' . htmlspecialchars($piki_page['body']) . '</pre>';
}
$piki_pages = select_all();
$list .= '<ul>';
foreach ($piki_pages as $piki_page) {
$page_id = $piki_page['id'];
$page_title = htmlspecialchars($piki_page['title']);
if ($id == $page_id) {
$list .= "<li class=\"current_page\">> <a href=\"$base_url?id=$page_id\">$page_title</a></li>";
} else {
$list .= "<li><a href=\"$base_url?id=$page_id\">$page_title</a></li>";
}
}
$list .= '</ul>';
echo <<<EOS
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
#header {
padding: 6px 0;
}
#header h1 {
display: inline;
margin: 4px 0;
}
#header h1 a {
color: #FF00AE;
}
#page {
border-left: dotted 1px silver;
padding:6px 12px;
float: left;
width: 80%;
}
#page h2 {
display: inline;
margin: 0 0 4px 0;
}
#page h2 a {
color: black;
}
#sidebar {
float: left;
width: 16%;
}
#footer {
clear: both;
}
#sidebar ul {
padding: 0;
margin: 16px 0;
}
#sidebar li {
margin: 1px 0;
padding: 1px 2px;
list-style-type: none;
}
#sidebar li.current_page {
color: black;
}
#sidebar li.current_page a {
color: black;
}
#contents {
margin: 0;
}
a {
color: #0063F7;
}
a.action {
margin: 0 8px;
color: #CCCCCC;
}
a.action:hover {
color: #0063F7;
}
</style>
<title>$title - Piki</title>
</head>
<body>
<div id="header">
<h1><a href="$base_url">Piki</a></h1>
<a class="action" href="$base_url?action=new">new</a>
</div>
<div id="sidebar">
$list
</div>
<div id="page">
<div id="contents">$contents</div>
</div>
<div id="footer"></div>
</body>
</html>
EOS;
}
init_db('piki.db');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
post();
exit;
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
get();
}
?>