diff options
author | Arno Richter <oelna@oelna.de> | 2022-12-13 23:28:38 +0100 |
---|---|---|
committer | Arno Richter <oelna@oelna.de> | 2022-12-13 23:28:38 +0100 |
commit | 67b51e9f905c568444741eaad0eb5d82df7f09c8 (patch) | |
tree | b7852e7d57e15a42f3dd752f891e8d513e3139a8 | |
parent | 0399f48a1f6a5e7151e3539038662686ed365ca7 (diff) | |
download | microblog-67b51e9f905c568444741eaad0eb5d82df7f09c8.tar.gz microblog-67b51e9f905c568444741eaad0eb5d82df7f09c8.tar.bz2 microblog-67b51e9f905c568444741eaad0eb5d82df7f09c8.zip |
make pagination more robust. add undelete method for posts, if you know the id.
-rw-r--r-- | css/microblog.css | 1 | ||||
-rw-r--r-- | lib/database.php | 3 | ||||
-rw-r--r-- | lib/functions.php | 10 | ||||
-rw-r--r-- | templates/single.inc.php | 22 | ||||
-rw-r--r-- | templates/timeline.inc.php | 4 |
5 files changed, 36 insertions, 4 deletions
diff --git a/css/microblog.css b/css/microblog.css index deb68c8..0490d80 100644 --- a/css/microblog.css +++ b/css/microblog.css @@ -225,6 +225,7 @@ form.edit, .login input[type="password"] { width: 100%; border: 2px solid var(--background-color); + background: #fff; padding: 0.5rem; font-size: 1.25rem; resize: vertical; diff --git a/lib/database.php b/lib/database.php index 0ffdb3a..5774d95 100644 --- a/lib/database.php +++ b/lib/database.php @@ -3,6 +3,9 @@ //connect or create the database try { $db = new PDO('sqlite:'.ROOT.DS.'posts.db'); + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + $config['db_version'] = $db->query("PRAGMA user_version")->fetch(PDO::FETCH_ASSOC)['user_version']; } catch(PDOException $e) { print 'Exception : '.$e->getMessage(); diff --git a/lib/functions.php b/lib/functions.php index 268808c..7046eb5 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -40,7 +40,7 @@ function db_insert($content, $timestamp=NOW) { return $db->lastInsertId(); } -function db_delete($post_id) { +function db_delete($post_id, $undelete=false) { global $db; if(empty($db)) return false; if(!is_numeric($post_id) || $post_id <= 0) return false; @@ -50,10 +50,14 @@ function db_delete($post_id) { $statement->bindParam(':id', $post_id, PDO::PARAM_INT); */ + // delete or undelete/restore + $post_deleted = !$undelete ? time() : null; + $type = !$undelete ? PDO::PARAM_INT : PDO::PARAM_NULL; + // mark as deleted instead (for undo?!) $statement = $db->prepare('UPDATE posts SET post_deleted = :post_deleted WHERE id = :id'); $statement->bindValue(':id', $post_id, PDO::PARAM_INT); - $statement->bindValue(':post_deleted', time(), PDO::PARAM_INT); + $statement->bindValue(':post_deleted', $post_deleted, $type); $statement->execute(); @@ -115,7 +119,7 @@ function db_posts_count() { global $db; if(empty($db)) return false; - $statement = $db->prepare('SELECT COUNT(*) AS posts_count FROM posts'); + $statement = $db->prepare('SELECT COUNT(*) AS posts_count FROM posts WHERE post_deleted IS NULL'); $statement->execute(); $row = $statement->fetch(PDO::FETCH_ASSOC); diff --git a/templates/single.inc.php b/templates/single.inc.php index af486d5..cd01127 100644 --- a/templates/single.inc.php +++ b/templates/single.inc.php @@ -1,11 +1,11 @@ <?php if(!defined('ROOT')) die('Don\'t call this directly.'); - header('Content-Type: text/html; charset=utf-8'); $id = (!empty(path(0))) ? (int) path(0) : 0; $action = 'display'; if(mb_strtolower(path(1)) == 'delete') $action = 'delete'; + if(mb_strtolower(path(1)) == 'undelete') $action = 'undelete'; if(mb_strtolower(path(1)) == 'edit') $action = 'edit'; $error = false; @@ -25,6 +25,17 @@ } } + // undelete post + if($action == 'undelete') { + $result = db_delete((int) $id, true); + + if(!$result) { + $error = 'Post could not be restored!'; + } else { + rebuild_feeds(); + } + } + // edit post if(!empty($_POST['action']) && $_POST['action'] == 'edit') { @@ -43,6 +54,11 @@ // load the actual post $post = db_select_post($id); + if(is_numeric($post['post_deleted'])) { + if(!$config['logged_in']) { + header('Location: '.$config['url']); + } + } $title_suffix = 'entry #' . $id; require(ROOT.DS.'snippets'.DS.'header.snippet.php'); @@ -73,8 +89,12 @@ <span class="post-timestamp"><time datetime="<?= $datetime ?>" data-unix-time="<?= $post['post_timestamp'] ?>"><?= $formatted_time ?></time></span> <nav class="post-meta"> <?php if($config['logged_in']): ?><ul> + <?php if(is_numeric($post['post_deleted'])): ?> + <li><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/undelete" title="Restore">Deleted on <?= date('M d Y', $post['post_deleted']) ?></a></li> + <?php else: ?> <li><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/edit">Edit</a></li> <li><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/delete">Delete</a></li> + <?php endif; ?> </ul><?php endif; ?> </nav> <p class="post-content"><?= nl2br(autolink($post['post_content'])) ?></p> diff --git a/templates/timeline.inc.php b/templates/timeline.inc.php index 234ea83..a29e9dd 100644 --- a/templates/timeline.inc.php +++ b/templates/timeline.inc.php @@ -15,6 +15,10 @@ // get posts $posts = db_select_posts(NOW, $config['posts_per_page'], 'desc', $offset); + if(empty($posts)) { + header('Location: '.$config['url']); + die(); + } $title_suffix = ''; require(ROOT.DS.'snippets'.DS.'header.snippet.php'); |