-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
311 lines (232 loc) · 7.39 KB
/
index.php
File metadata and controls
311 lines (232 loc) · 7.39 KB
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
<?php
ini_set('display_errors', true);
ini_set('post_max_size', '2M');
ini_set('upload_max_filesize', '2M');
$db = new SQLite3('database.sqlite', SQLITE3_OPEN_READWRITE);
interface CRUD {
public function create(array $fields): int;
public function read(int $page, int $perPage): array;
public function update(int $key, array $fields): int;
public function delete(int $key): int;
}
final class Author implements CRUD
{
protected $field_names = array('first_name', 'second_name', 'third_name');
public function create(array $fields): int
{
global $db;
$query = $db->prepare("
INSERT INTO author (first_name, second_name, third_name)
VALUES (:a, :b, :c);"
);
$query->bindValue(':a', $fields['first_name'], SQLITE3_TEXT);
$query->bindValue(':b', $fields['second_name'], SQLITE3_TEXT);
$query->bindValue(':c', $fields['third_name'], SQLITE3_TEXT);
$query->execute();
return $db->lastInsertRowID();
}
public function read(int $page = 1, int $limit = 20): array
{
global $db;
$offset = ($page - 1) * $limit;
$result = $db->query("
SELECT author_key, first_name, second_name, third_name
FROM author LIMIT $limit OFFSET $offset;"
);
$authors = array();
while ($row = $result->fetchArray(SQLITE3_ASSOC))
$authors[] = $row;
return $authors;
}
public function update(int $key, array $fields) : int
{
global $db;
foreach ($this->field_names as $name) {
if (isset($fields[$name])) {
$escaped = $db->escapeString($fields[$name]);
$values[] = "$name = '$escaped'";
}
}
$values = join(', ', $values);
return $db->exec("UPDATE author SET $values WHERE author_key = $key;");
}
public function delete(int $key) : int
{
global $db;
return $db->exec("DELETE FROM author WHERE author_key = $key;");
}
}
final class Magazine implements CRUD
{
protected $field_names = array('title', 'short', 'image', 'published');
protected function magazine_authors(int $magazine_key, array $authors) : bool
{
global $db;
$author_keys = array_map('intval', $authors);
foreach ($author_keys as $author_key)
$pairs[] = "($magazine_key, $author_key)";
$values = join(', ', $pairs);
return $db->exec("
INSERT INTO magazine_authors (magazine_key, author_key) VALUES $values;"
);
}
public function create(array $fields): int
{
global $db;
$query = $db->prepare("
INSERT INTO magazine (title, short, image, published)
VALUES (:title, :short, :image, :published);"
);
foreach ($this->field_names as $name)
$query->bindValue($name, $fields[$name], SQLITE3_TEXT);
$query->execute();
$magazine_key = $db->lastInsertRowID();
$this->magazine_authors($magazine_key, $fields['authors']);
return $magazine_key;
}
public function read(int $page = 1, int $limit = 20): array
{
global $db;
$offset = ($page - 1) * $limit;
$result = $db->query("
SELECT magazine_key, title, short, image, published
FROM magazine LIMIT $limit OFFSET $offset;"
);
$magazines = array();
while ($row = $result->fetchArray(SQLITE3_ASSOC))
$magazines[] = $row;
return $magazines;
}
public function update(int $key, array $fields): int
{
global $db;
if (array_key_exists('authors', $fields)) {
$insert = $this->magazine_authors($key, $fields['authors']);
if (!$insert) return false;
}
foreach ($this->field_names as $name) {
if (isset($fields[$name])) {
$escaped = $db->escapeString($fields[$name]);
$values[] = "$name = '$escaped'";
}
}
$values = join(', ', $values);
return $db->exec("UPDATE magazine SET $values WHERE magazine_key = $key;");
}
public function delete(int $key): int
{
global $db;
return $db->exec("
DELETE FROM magazine_authors WHERE magazine_key = $key;
DELETE FROM magazine WHERE magazine_key = $key;"
);
}
}
function response(string $key, $data)
{
die(json_encode(array($key => $data)));
}
function error(string $message)
{
http_response_code(400);
response('error', $message);
}
function should_be_set(string $key, array $arr)
{
if (array_key_exists($key, $arr))
return;
http_response_code(400);
response($key, 'not set');
}
function should_be(bool $sign, string $key, string $message)
{
if ($sign) return;
http_response_code(400);
response($key, $message);
}
function method_should_be(string $method)
{
if ($method == $_SERVER['REQUEST_METHOD'])
return;
http_response_code(403);
die('Forbidden');
}
// phpinfo(INFO_VARIABLES); // INFO_CONFIGURATION
if ('/photo/upload' == $_SERVER['REQUEST_URI']) {
method_should_be('POST');
should_be_set('magazine_key', $_POST);
should_be_set('image', $_FILES);
should_be(UPLOAD_ERR_OK == $_FILES['image']['error'], 'upload', 'failed');
$tmp_name = $_FILES['image']['tmp_name'];
$jpg_or_png = strstr($tmp_name, 'jpg') > 0 || strstr($tmp_name, 'png') > 0;
should_be($jpg_or_png, 'image', 'jpg or png only');
$fname = 'assets/' . $tmp_name;
should_be(move_uploaded_file($tmp_name, $fname), 'upload', 'cancelled');
$magazine['image'] = '/' . $fname;
$update = (new Magazine)->update($_POST['magazine_key'], $magazine);
$update ? response('update', 'success') : error('not updated');
}
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$json = file_get_contents('php://input');
$in = json_decode($json, true); // assoc
if (!is_array($in))
error('invalid query');
}
switch ($_SERVER['REQUEST_URI']) {
case '/author/add':
method_should_be('POST');
should_be_set('first_name', $in);
should_be_set('second_name', $in);
$name_length = strlen($in['second_name']);
should_be($name_length > 2, 'second_name', 'too short');
$author_key = (new Author)->create($in);
$author_key > 0 ? response('author_key', $author_key) : error('not created');
case '/author/update':
method_should_be('POST');
should_be_set('author_key', $in);
if (isset($in['second_name'])) {
$name_length = strlen($in['second_name']);
should_be($name_length > 2, 'second_name', 'too short');
}
$author_key = $in['author_key'];
unset($in['author_key']);
$update = (new Author)->update($author_key, $in);
$update ? response('update', 'success') : error('not updated');
case '/author/delete':
method_should_be('POST');
should_be_set('author_key', $in);
$delete = (new Author)->delete($in['author_key']);
$delete ? response('delete', 'success') : error('not deleted');
case '/author/list':
method_should_be('GET');
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = isset($_GET['perPage']) ? intval($_GET['perPage']) : 20;
$authors = (new Author)->read($page, $limit);
response('authors', $authors);
case '/magazine/add':
method_should_be('POST');
should_be_set('title', $in);
should_be_set('authors', $in);
should_be(is_array($in['authors']), 'authors', 'not array');
$magazine_key = (new Magazine)->create($in);
response('magazine_key', $magazine_key);
case '/magazine/update':
method_sould_be('POST');
should_be_set('magazine_key', $in);
$update = (new Magazine)->update($in['magazine_key'], $in);
$update ? response('update', 'success') : error('not updated');
case '/magazine/delete':
method_should_be('POST');
should_be_set('magazine_key', $in);
$delete = (new Magazine)->delete($in['magazine_key']);
$delete ? response('delete', 'success') : error('not deleted');
case '/magazine/list':
method_should_be('GET');
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = isset($_GET['perPage']) ? intval($_GET['perPage']) : 20;
$magazines = (new Magazine)->read($page, $limit);
response('magazines', $magazines);
default:
http_response_code(403);
die('Forbidden');
}