-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoblog.php
60 lines (53 loc) · 1.96 KB
/
nanoblog.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
<?php
DEFINE('NOW', time());
DEFINE('SECRET', 'chewbacca');
// setlocale(LC_TIME, "de_DE");
// date_default_timezone_set('Europe/Berlin');
try {
$db = new PDO('sqlite:'.ROOT.DS.'posts.db');
$db->exec("CREATE TABLE IF NOT EXISTS posts (
id integer PRIMARY KEY NOT NULL,
post_content TEXT,
post_timestamp integer
);");
} catch(PDOException $e) {
print 'Exception : '.$e->getMessage();
die('database error');
}
function db_insert($message, $timestamp=NOW) {
global $db;
if (strlen($message)===0) return;
if(empty($db)) return false;
$statement = $db->prepare('INSERT INTO posts (post_content, post_timestamp) VALUES (:post_content, :post_timestamp)');
$statement->bindParam(':post_content', $message, PDO::PARAM_STR);
$statement->bindParam(':post_timestamp', $timestamp, PDO::PARAM_INT);
$statement->execute();
return $db->lastInsertId();
}
function db_delete_latest() {
global $db;
if(empty($db)) return false;
$statement = $db->prepare('DELETE FROM posts ORDER BY id DESC LIMIT 1');
$statement->execute();
return true;
}
function db_select_posts($from=NOW, $amount=10, $sort='desc', $page=1) {
global $db;
if(empty($db)) return false;
if($sort !== 'desc') $sort = 'asc';
$postsperpage = 20;
$offset = ($page-1)*$postsperpage;
$statement = $db->prepare('SELECT * FROM posts WHERE post_timestamp < :post_timestamp ORDER BY id '.$sort.' LIMIT :limit OFFSET :page');
$statement->bindParam(':post_timestamp', $from, PDO::PARAM_INT);
$statement->bindParam(':limit', $amount, PDO::PARAM_INT);
$statement->bindParam(':page', $offset, PDO::PARAM_INT);
$statement->execute();
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
return (!empty($rows)) ? $rows : false;
}
/////////////// HTTP API
if (isset($_POST["add"]) ) {
print $_POST["secret"] === SECRET?db_insert($_POST["add"]):"access denied";
} elseif (isset($_POST["delete"]) ) {
print $_POST["secret"] === SECRET? db_delete_latest():"access denied";
}