[php-blog-cvs] r1457 - in trunk: . docs include include/admin plugins/serendipity_event_spamblock
A reliable, secure & extensible PHP blog | Not mainstream since 2002
Brought to you by:
garvinhicking,
jhermanns
From: garvinhicking at B. <gar...@ma...> - 2006-10-26 09:41:24
|
Author: garvinhicking Date: 2006-10-26 11:41:10 +0200 (Thu, 26 Oct 2006) New Revision: 1457 Modified: trunk/docs/NEWS trunk/include/admin/comments.inc.php trunk/include/functions_config.inc.php trunk/plugins/serendipity_event_spamblock/UTF-8/lang_de.inc.php trunk/plugins/serendipity_event_spamblock/lang_de.inc.php trunk/plugins/serendipity_event_spamblock/lang_en.inc.php trunk/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php trunk/serendipity_admin.php Log: Admin-based comment response, CSRF-protection for comment form in antispam plugin Modified: trunk/docs/NEWS =================================================================== --- trunk/docs/NEWS 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/docs/NEWS 2006-10-26 09:41:10 UTC (rev 1457) @@ -3,6 +3,14 @@ Version 1.1 () ------------------------------------------------------------------------ + * Added functionality to reply to comments in the admin interface + (garvinhicking) + + * Enhance spamblock plugin with session hash check, to prevent + automatted comment posting. Also prevents possible CSRF for + tricking you into submitting comments to your own blog. Thanks + to Stefan Esser! (garvinhicking) + * Support to delete multiple entries at once via checkboxes in the entry admin panel, fix admin entry pagination to not show next pages, if that next page were empty. (garvinhicking) Modified: trunk/include/admin/comments.inc.php =================================================================== --- trunk/include/admin/comments.inc.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/include/admin/comments.inc.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -13,7 +13,7 @@ $commentsPerPage = (int)(!empty($serendipity['GET']['filter']['perpage']) ? $serendipity['GET']['filter']['perpage'] : 10); $summaryLength = 200; -if ( $serendipity['POST']['formAction'] == 'multiDelete' && sizeof($serendipity['POST']['delete']) != 0 && serendipity_checkFormToken()) { +if ($serendipity['POST']['formAction'] == 'multiDelete' && sizeof($serendipity['POST']['delete']) != 0 && serendipity_checkFormToken()) { foreach ( $serendipity['POST']['delete'] as $k => $v ) { serendipity_deleteComment($k, $v); echo DONE . ': '. sprintf(COMMENT_DELETED, $k) . '<br />'; @@ -22,7 +22,7 @@ /* We are asked to save the edited comment, and we are not in preview mode */ -if ( isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'doEdit' && !isset($serendipity['POST']['preview']) && serendipity_checkFormToken()) { +if (isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'doEdit' && !isset($serendipity['POST']['preview']) && serendipity_checkFormToken()) { $sql = "UPDATE {$serendipity['dbPrefix']}comments SET author = '" . serendipity_db_escape_string($serendipity['POST']['name']) . "', @@ -36,9 +36,32 @@ echo COMMENT_EDITED; } +/* Submit a new comment */ +if (isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'doReply' && !isset($serendipity['POST']['preview']) && serendipity_checkFormToken()) { + $comment = array(); + $comment['url'] = $serendipity['POST']['url']; + $comment['comment'] = trim($serendipity['POST']['comment']); + $comment['name'] = $serendipity['POST']['name']; + $comment['email'] = $serendipity['POST']['email']; + $comment['subscribe'] = $serendipity['POST']['subscribe']; + $comment['parent_id'] = $serendipity['POST']['replyTo']; + if (!empty($comment['comment'])) { + if (serendipity_saveComment($serendipity['POST']['entry_id'], $comment, 'NORMAL')) { + echo '<script type="text/javascript">alert("' . COMMENT_ADDED . '"); parent.focus(); this.close();</script>'; + echo '<noscript>' . COMMENT_ADDED . '</noscript>'; + return true; + } else { + echo COMMENT_NOT_ADDED; + $serendipity['GET']['adminAction'] = 'reply'; + } + } else { + echo COMMENT_NOT_ADDED; + $serendipity['GET']['adminAction'] = 'reply'; + } +} /* We approve a comment */ -if ( isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'approve' && serendipity_checkFormToken()) { +if (isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'approve' && serendipity_checkFormToken()) { $sql = "SELECT c.*, e.title, a.email as authoremail, a.mail_comments FROM {$serendipity['dbPrefix']}comments c LEFT JOIN {$serendipity['dbPrefix']}entries e ON (e.id = c.entry_id) @@ -47,58 +70,89 @@ $rs = serendipity_db_query($sql, true); if ($rs === false) { - echo ERROR .': '. sprintf(COMMENT_ALREADY_APPROVED, $serendipity['GET']['id']); + echo ERROR .': '. sprintf(COMMENT_ALREADY_APPROVED, (int)$serendipity['GET']['id']); } else { serendipity_approveComment($serendipity['GET']['id'], $rs['entry_id']); - echo DONE . ': '. sprintf(COMMENT_APPROVED, $serendipity['GET']['id']); + echo DONE . ': '. sprintf(COMMENT_APPROVED, (int)$serendipity['GET']['id']); } } /* We are asked to delete a comment */ -if ( isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'delete' && serendipity_checkFormToken()) { +if (isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'delete' && serendipity_checkFormToken()) { serendipity_deleteComment($serendipity['GET']['id'], $serendipity['GET']['entry_id']); - echo DONE . ': '. sprintf(COMMENT_DELETED, $serendipity['GET']['id']); + echo DONE . ': '. sprintf(COMMENT_DELETED, (int)$serendipity['GET']['id']); } /* We are either in edit mode, or preview mode */ -if ( isset($serendipity['GET']['adminAction']) && $serendipity['GET']['adminAction'] == 'edit' || isset($serendipity['POST']['preview'])) { +if (isset($serendipity['GET']['adminAction']) && ($serendipity['GET']['adminAction'] == 'edit' || $serendipity['GET']['adminAction'] == 'reply') || isset($serendipity['POST']['preview'])) { $serendipity['smarty_raw_mode'] = true; // Force output of Smarty stuff in the backend serendipity_smarty_init(); - /* If we are not in preview, we need data from our database */ - if (!isset($serendipity['POST']['preview']) ) { - $comment = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}comments WHERE id = ". (int)$serendipity['GET']['id']); - $data['name'] = $comment[0]['author']; - $data['email'] = $comment[0]['email']; - $data['url'] = $comment[0]['url']; - $data['replyTo'] = $comment[0]['parent_id']; - $data['comment'] = $comment[0]['body']; + if ($serendipity['GET']['adminAction'] == 'reply' || $serendipity['GET']['adminAction'] == 'doReply') { + $c = serendipity_fetchComments($serendipity['GET']['entry_id'], 1, 'co.id', false, 'NORMAL', ' AND co.id=' . (int)$serendipity['GET']['id']); - /* If we are in preview, we get data from our form */ - } elseif ( isset($serendipity['POST']['preview']) ) { - $data['name'] = $serendipity['POST']['name']; - $data['email'] = $serendipity['POST']['email']; - $data['url'] = $serendipity['POST']['url']; - $data['replyTo'] = $serendipity['POST']['replyTo']; - $data['comment'] = $serendipity['POST']['comment']; - $pc_data = array( - array( - 'email' => $serendipity['POST']['email'], - 'author' => $serendipity['POST']['name'], - 'body' => $serendipity['POST']['comment'], - 'url' => $serendipity['POST']['url'], - 'timestamp' => time() - ) - ); + if (isset($serendipity['POST']['preview'])) { + $c[] = array( + 'email' => $serendipity['POST']['email'], + 'author' => $serendipity['POST']['name'], + 'body' => $serendipity['POST']['comment'], + 'url' => $serendipity['POST']['url'], + 'timestamp' => time(), + 'parent_id' => $serendipity['GET']['id'] + ); + } + + $target_url = '?serendipity[action]=admin&serendipity[adminModule]=comments&serendipity[adminAction]=doReply&serendipity[id]=' . (int)$serendipity['GET']['id'] . '&serendipity[entry_id]=' . (int)$serendipity['GET']['entry_id'] . '&serendipity[noBanner]=true&serendipity[noSidebar]=true&' . serendipity_setFormToken('url'); + $data = $serendipity['POST']; + $data['replyTo'] = (int)$serendipity['GET']['id']; + $out = serendipity_printComments($c); + $serendipity['smarty']->display(serendipity_getTemplateFile('comments.tpl', 'serendipityPath')); + + if (!isset($data['name'])) { + $data['name'] = $serendipity['serendipityRealname']; + } - serendipity_printComments($pc_data); - $serendipity['smarty']->display(serendipity_getTemplateFile('comments.tpl', 'serendipityPath')); + if (!isset($data['email'])) { + $data['email'] = $serendipity['serendipityEmail']; + } + } else { + $target_url = '?serendipity[action]=admin&serendipity[adminModule]=comments&serendipity[adminAction]=doEdit&serendipity[id]=' . (int)$serendipity['GET']['id'] . '&serendipity[entry_id]=' . (int)$serendipity['GET']['entry_id'] . '&' . serendipity_setFormToken('url'); + + /* If we are not in preview, we need data from our database */ + if (!isset($serendipity['POST']['preview'])) { + $comment = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}comments WHERE id = ". (int)$serendipity['GET']['id']); + $data['name'] = $comment[0]['author']; + $data['email'] = $comment[0]['email']; + $data['url'] = $comment[0]['url']; + $data['replyTo'] = $comment[0]['parent_id']; + $data['comment'] = $comment[0]['body']; + + /* If we are in preview, we get data from our form */ + } elseif (isset($serendipity['POST']['preview'])) { + $data['name'] = $serendipity['POST']['name']; + $data['email'] = $serendipity['POST']['email']; + $data['url'] = $serendipity['POST']['url']; + $data['replyTo'] = $serendipity['POST']['replyTo']; + $data['comment'] = $serendipity['POST']['comment']; + $pc_data = array( + array( + 'email' => $serendipity['POST']['email'], + 'author' => $serendipity['POST']['name'], + 'body' => $serendipity['POST']['comment'], + 'url' => $serendipity['POST']['url'], + 'timestamp' => time() + ) + ); + + serendipity_printComments($pc_data); + $serendipity['smarty']->display(serendipity_getTemplateFile('comments.tpl', 'serendipityPath')); + } } serendipity_displayCommentForm( $serendipity['GET']['entry_id'], - '?serendipity[action]=admin&serendipity[adminModule]=comments&serendipity[adminAction]=doEdit&serendipity[id]=' . $serendipity['GET']['id'] . '&serendipity[entry_id]=' . $serendipity['GET']['entry_id'] . '&' . serendipity_setFormToken('url'), + $target_url, NULL, $data, false, @@ -407,6 +461,7 @@ <a target="_blank" href="<?php echo $entrylink; ?>" title="<?php echo VIEW; ?>" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/zoom.png'); ?>" alt="<?php echo VIEW; ?>" /><?php echo VIEW ?></a> <a href="?serendipity[action]=admin&serendipity[adminModule]=comments&serendipity[adminAction]=edit&serendipity[id]=<?php echo $comment['id'] ?>&serendipity[entry_id]=<?php echo $comment['entry_id'] ?>&<?php echo serendipity_setFormToken('url'); ?>" title="<?php echo EDIT; ?>" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/edit.png'); ?>" alt="<?php echo EDIT; ?>" /><?php echo EDIT ?></a> <a href="?serendipity[action]=admin&serendipity[adminModule]=comments&serendipity[adminAction]=delete&serendipity[id]=<?php echo $comment['id'] ?>&serendipity[entry_id]=<?php echo $comment['entry_id'] ?>&<?php echo serendipity_setFormToken('url'); ?>" onclick='return confirm("<?php echo sprintf(COMMENT_DELETE_CONFIRM, $comment['id'], htmlspecialchars($comment['author'])) ?>")' title="<?php echo DELETE ?>" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/delete.png'); ?>" alt="<?php echo DELETE; ?>" /><?php echo DELETE ?></a> + <a target="_blank" onclick="cf = window.open(this.href, 'CommentForm', 'width=800,height=600,toolbar=no,scrollbars=1,scrollbars,resize=1,resizable=1'); cf.focus(); return false;" href="?serendipity[action]=admin&serendipity[adminModule]=comments&serendipity[adminAction]=reply&serendipity[id]=<?php echo $comment['id'] ?>&serendipity[entry_id]=<?php echo $comment['entry_id'] ?>&serendipity[noBanner]=true&serendipity[noSidebar]=true&<?php echo serendipity_setFormToken('url'); ?>" title="<?php echo REPLY ?>" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/user_editor.png'); ?>" alt="<?php echo REPLY; ?>" /><?php echo REPLY ?></a> <?php echo $comment['action_more']; ?> </td> </tr> Modified: trunk/include/functions_config.inc.php =================================================================== --- trunk/include/functions_config.inc.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/include/functions_config.inc.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -1786,7 +1786,7 @@ * @see serendipity_setFormToken() * @return boolean Returns true, if XSRF attempt was found and the token was missing */ -function serendipity_checkFormToken() { +function serendipity_checkFormToken($output = true) { global $serendipity; $token = ''; @@ -1797,13 +1797,13 @@ } if (empty($token)) { - echo serendipity_reportXSRF('token', false); + if ($output) echo serendipity_reportXSRF('token', false); return false; } if ($token != md5(session_id()) && $token != md5($serendipity['COOKIE']['old_session'])) { - echo serendipity_reportXSRF('token', false); + if ($output) echo serendipity_reportXSRF('token', false); return false; } Modified: trunk/plugins/serendipity_event_spamblock/UTF-8/lang_de.inc.php =================================================================== --- trunk/plugins/serendipity_event_spamblock/UTF-8/lang_de.inc.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/plugins/serendipity_event_spamblock/UTF-8/lang_de.inc.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -99,3 +99,7 @@ @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT_TREAT', 'Was soll mit auto-moderierten Trackbacks passieren?'); @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT', 'Trackbackmoderation nach wievielen Tagen erzwingen'); @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT_DESC', 'Alle Trackbacks zu einem Artikel können abhängig vom Alter des Artikels automatisch moderiert werden. Tragen Sie hier das Minimalalter eines Artikels in Tagen ein, ab dem jedes Trackback erst nach Ihrer Moderation dargestellt wird. 0 bedeutet, dass keine automatische Moderation erzeugt wird.'); + +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF', 'CSRF-Schutz aktivieren?'); +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF_DESC', 'Falls aktiviert, wird ein spezieller Hash-Wert sicherstellen, dass nur Benutzer Kommentare hinterlassen dürfen , die eine gültige Session-ID haben. Dies wird Spam etwas eindämmen und es unmöglich machen, dass Sie ungewollt Kommentare via CSRF-Angriffen hinterlassen, aber es wird auch dazu führen dass nur Benutzer mit aktivierten Cookies kommentieren können.'); +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF_REASON', 'Ihr Kommentar enthielt keinen gültigen Session-Hash. Kommentare auf diesem Blog können nur mit aktivierten Cookies hinterlassen werden!'); \ No newline at end of file Modified: trunk/plugins/serendipity_event_spamblock/lang_de.inc.php =================================================================== --- trunk/plugins/serendipity_event_spamblock/lang_de.inc.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/plugins/serendipity_event_spamblock/lang_de.inc.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -100,3 +100,8 @@ @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT_TREAT', 'Was soll mit auto-moderierten Trackbacks passieren?'); @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT', 'Trackbackmoderation nach wievielen Tagen erzwingen'); @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT_DESC', 'Alle Trackbacks zu einem Artikel kn abhig vom Alter des Artikels automatisch moderiert werden. Tragen Sie hier das Minimalalter eines Artikels in Tagen ein, ab dem jedes Trackback erst nach Ihrer Moderation dargestellt wird. 0 bedeutet, dass keine automatische Moderation erzeugt wird.'); + +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF', 'CSRF-Schutz aktivieren?'); +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF_DESC', 'Falls aktiviert, wird ein spezieller Hash-Wert sicherstellen, dass nur Benutzer Kommentare hinterlassen d, die eine g Session-ID haben. Dies wird Spam etwas einden und es unmch machen, dass Sie ungewollt Kommentare via CSRF-Angriffen hinterlassen, aber es wird auch dazu fdass nur Benutzer mit aktivierten Cookies kommentieren kn.'); + +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF_REASON', 'Ihr Kommentar enthielt keinen gn Session-Hash. Kommentare auf diesem Blog kn nur mit aktivierten Cookies hinterlassen werden!'); \ No newline at end of file Modified: trunk/plugins/serendipity_event_spamblock/lang_en.inc.php =================================================================== --- trunk/plugins/serendipity_event_spamblock/lang_en.inc.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/plugins/serendipity_event_spamblock/lang_en.inc.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -110,3 +110,7 @@ @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT_TREAT', 'What to do with trackbacks when being auto-moderated?'); @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT', 'Force trackback moderation after how many days'); @define('PLUGIN_EVENT_SPAMBLOCK_FORCEMODERATIONT_DESC', 'You can automatically set all trackbacks for entries to be moderated. Enter the age of an entry in days, after which it should be auto-moderated. 0 means no auto-moderation.'); + +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF', 'Use CSRF protection for comments?'); +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF_DESC', 'If enabled, a special hash value will check that only users can submit a comment with a valid session ID. This will decrease spam and prevent users from tricking you into submitting comments via CSRF, but it will also prevent users commenting on your blog without cookies.'); +@define('PLUGIN_EVENT_SPAMBLOCK_CSRF_REASON', 'Your comment did not contain a Session-Hash. Comments can only be made on this blog when having cookies enabled!'); \ No newline at end of file Modified: trunk/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php =================================================================== --- trunk/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -34,7 +34,7 @@ 'smarty' => '2.6.7', 'php' => '4.1.0' )); - $propbag->add('version', '1.53'); + $propbag->add('version', '1.60'); $propbag->add('event_hooks', array( 'frontend_saveComment' => true, 'external_plugin' => true, @@ -49,6 +49,7 @@ 'bodyclone', 'entrytitle', 'ipflood', + 'csrf', 'captchas', 'captchas_ttl', 'captcha_color', @@ -113,6 +114,13 @@ $propbag->add('default', false); break; + case 'csrf': + $propbag->add('type', 'boolean'); + $propbag->add('name', PLUGIN_EVENT_SPAMBLOCK_CSRF); + $propbag->add('description', PLUGIN_EVENT_SPAMBLOCK_CSRF_DESC); + $propbag->add('default', true); + break; + case 'entrytitle': $propbag->add('type', 'boolean'); $propbag->add('name', PLUGIN_EVENT_SPAMBLOCK_FILTER_TITLE); @@ -596,6 +604,15 @@ $logfile = $this->logfile = $this->get_config('logfile', $serendipity['serendipityPath'] . 'spamblock.log'); $required_fields = $this->get_config('required_fields', ''); + // Check CSRF [comments only, cannot be applied to trackbacks] + if ($addData['type'] == 'NORMAL' && serendipity_db_bool($this->get_config('csrf', true))) { + if (!serendipity_checkFormToken(false)) { + $this->log($logfile, $eventData['id'], 'REJECTED', PLUGIN_EVENT_SPAMBLOCK_CSRF_REASON, $addData); + $eventData = array('allow_comments' => false); + $serendipity['messagestack']['comments'][] = PLUGIN_EVENT_SPAMBLOCK_CSRF_REASON; + } + } + // Check required fields if ($addData['type'] == 'NORMAL' && !empty($required_fields)) { $required_field_list = explode(',', $required_fields); @@ -905,6 +922,10 @@ echo '<div class="serendipity_commentDirection serendipity_comment_spamblock">' . PLUGIN_EVENT_SPAMBLOCK_HIDE_EMAIL_NOTICE . '</div>'; } + if (serendipity_db_bool($this->get_config('csrf', true))) { + echo serendipity_setFormToken('form'); + } + // Check whether to allow comments from registered authors if (serendipity_userLoggedIn() && $this->inGroup()) { return true; Modified: trunk/serendipity_admin.php =================================================================== --- trunk/serendipity_admin.php 2006-10-25 08:27:16 UTC (rev 1456) +++ trunk/serendipity_admin.php 2006-10-26 09:41:10 UTC (rev 1457) @@ -87,6 +87,7 @@ </head> <body id="serendipity_admin_page" onload="spawn()"> <table cellspacing="0" cellpadding="0" border="0" id="serendipityAdminFrame"> + <?php if (!isset($serendipity['GET']['noBanner']) && !isset($serendipity['POST']['noBanner'])) { ?> <tr> <td colspan="2" id="serendipityAdminBanner"> <?php if ( IS_installed === true && IS_up2date === true ) { ?> @@ -104,6 +105,7 @@ <?php } ?> </td> </tr> + <?php } ?> <tr valign="top"> <?php if (!isset($serendipity['serendipityPath']) || IS_installed === false || IS_up2date === false ) { @@ -159,6 +161,7 @@ } else { ?> +<?php if (!isset($serendipity['GET']['noSidebar']) && !isset($serendipity['POST']['noSidebar'])) { ?> <td id="serendipitySideBar"> <ul class="serendipitySideBarMenu"> <li><a href="serendipity_admin.php"><?php echo ADMIN_FRONTPAGE; ?></a></li> @@ -241,6 +244,7 @@ </ul> </td> +<?php } ?> <td class="serendipityAdminContent"> <?php if (!isset($serendipity['GET']['adminModule'])) { |