From 016b40f99d679f0787f7c8a5f61f4a411b6c3632 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sun, 23 Apr 2017 12:46:48 +0200 Subject: Render comments when storing them in the database Instead of converting package comments from plain text to HTML code when they are displayed, do the conversion when the comment is posted and store the rendered result in the database. The conversion itself is done by a Python script which uses Bleach for sanitizing the text. Signed-off-by: Lukas Fleischer --- web/lib/pkgbasefuncs.inc.php | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'web/lib') diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index 57933e86..3e783094 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -54,7 +54,7 @@ function pkgbase_comments($base_id, $limit, $include_deleted, $only_pinned=false $dbh = DB::connect(); $q = "SELECT PackageComments.ID, A.UserName AS UserName, UsersID, Comments, "; $q.= "PackageBaseID, CommentTS, DelTS, EditedTS, B.UserName AS EditUserName, "; - $q.= "DelUsersID, C.UserName AS DelUserName, "; + $q.= "DelUsersID, C.UserName AS DelUserName, RenderedComment, "; $q.= "PinnedTS FROM PackageComments "; $q.= "LEFT JOIN Users A ON PackageComments.UsersID = A.ID "; $q.= "LEFT JOIN Users B ON PackageComments.EditedUsersID = B.ID "; @@ -79,6 +79,36 @@ function pkgbase_comments($base_id, $limit, $include_deleted, $only_pinned=false return $result->fetchAll(); } +/* + * Invoke the comment rendering script. + * + * @param int $id ID of the comment to render + * + * @return void + */ +function render_comment($id) { + $cmd = config_get('options', 'render-comment-cmd'); + $cmd .= ' ' . intval($id); + + $descspec = array( + 0 => array('pipe', 'r'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w') + ); + + $p = proc_open($cmd, $descspec, $pipes); + + if (!is_resource($p)) { + return false; + } + + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + + return proc_close($p); +} + /** * Add a comment to a package page and send out appropriate notifications * @@ -96,12 +126,14 @@ function pkgbase_add_comment($base_id, $uid, $comment) { } $q = "INSERT INTO PackageComments "; - $q.= "(PackageBaseID, UsersID, Comments, CommentTS) VALUES ("; - $q.= intval($base_id) . ", " . $uid . ", "; - $q.= $dbh->quote($comment) . ", " . strval(time()) . ")"; + $q.= "(PackageBaseID, UsersID, Comments, RenderedComment, CommentTS) "; + $q.= "VALUES (" . intval($base_id) . ", " . $uid . ", "; + $q.= $dbh->quote($comment) . ", '', " . strval(time()) . ")"; $dbh->exec($q); $comment_id = $dbh->lastInsertId(); + render_comment($comment_id); + notify(array('comment', $uid, $base_id, $comment_id)); return array(true, __('Comment has been added.')); -- cgit v1.2.3-24-g4f1b