nieuws/replies: read contents from database
[minimedit.git] / widget / nieuws / replies.php
index 06af5d3eea15cfed0b39468c29b8822f384b2a5e..54e552a0ff7cb86f7ade5fee3be6d7fcbd9a8777 100644 (file)
@@ -1,17 +1,16 @@
 <?php
 global $User;
+require_once 'database.inc.php';
 
 print '<h3>Reacties</h3>'."\n";
-$pagelink = $Page.$Args;
 
 if ($_POST) {
        try {
-               @mkdir($pagelink);
-               $target = $pagelink.'/'.date('YmdHis').':'.$User->login.'.html';
                $html = nl2br(htmlspecialchars($_POST['reply']));
-               $html = "<p>$html</p>\n";
-               $written = file_put_contents($target, $html);
-               if ($written === FALSE) {
+               $html = "<p>$html</p>";
+               $query = $Db->prepare('INSERT INTO comments (page, message, author) VALUES (?, ?, ?)');
+               $query->execute([ $Page, $html, $User->login ]);
+               if (!$query->rowCount()) {
                        throw new Exception('Fout bij opslaan');
                }
                $_POST['reply'] = NULL;
@@ -21,19 +20,18 @@ if ($_POST) {
        }
 }
 
+$query = $Db->prepare('SELECT * FROM comments WHERE page = ? ORDER BY created');
+$query->execute([$Page]);
+
 print '<ul class="replies">';
 
-foreach (glob("$pagelink/*.html") as $reply) {
-       preg_match('</(\d{2,14}) : ([^:]*) [^/]* \.html$>x', $reply, $replymeta);
-       if (!$replymeta) continue;
-       $replydate = str_split($replymeta[1], 2);
-       $replydate[0] = array_shift($replydate) . $replydate[0];
-       $replyuser = new User("profile/{$replymeta[2]}");
+while ($row = $query->fetch(PDO::FETCH_OBJ)) {
+       $rowuser = new User("profile/{$row->author}");
        print '<li>';
        printf('<strong>%s</strong> <small class=date>%s</small>',
-               $replyuser->html, showdate($replydate)
+               $rowuser->html, showdate(preg_split('/\D/', $row->created))
        );
-       printf("<blockquote>%s</blockquote>\n", file_get_contents($reply));
+       printf("<blockquote>%s</blockquote>\n", $row->message);
        print "</li>\n";
 }