From bd6c9e79c925384187f7752ddbda4f5cccf0aa47 Mon Sep 17 00:00:00 2001 From: onli Date: Wed, 26 May 2021 00:08:32 +0200 Subject: [PATCH] more php 8 compat fixes for receiving trackacks Also includes a code-cleanup by introducing log_trackback, a less code heavy way to log the trackback flow --- comment.php | 36 +++++++-------- include/functions_comments.inc.php | 16 ++----- include/functions_trackbacks.inc.php | 44 +++++-------------- .../serendipity_event_spamblock.php | 12 ++--- 4 files changed, 35 insertions(+), 73 deletions(-) diff --git a/comment.php b/comment.php index 1c044ad1..87b54db3 100644 --- a/comment.php +++ b/comment.php @@ -88,9 +88,8 @@ if ($type == 'trackback') { $tmp = ob_get_contents(); ob_end_clean(); - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] RECEIVED TRACKBACK' . "\n"); - fwrite($fp, '[' . date('d.m.Y H:i') . '] ' . $tmp . "\n"); + log_trackback('RECEIVED TRACKBACK'); + log_trackback($tmp); } $uri = $_SERVER['REQUEST_URI']; @@ -103,29 +102,16 @@ if ($type == 'trackback') { $id = (int)$matches[1]; } - if ($tb_logging) { - fwrite($fp, '[' . date('d.m.Y H:i') . '] Match on ' . $uri . "\n"); - fwrite($fp, '[' . date('d.m.Y H:i') . '] ID: ' . $id . "\n"); - fclose($fp); - } + log_trackback('Match on ' . $uri); + log_trackback('ID: ' . $id); if (add_trackback($id, $_REQUEST['title'], $_REQUEST['url'], $_REQUEST['blog_name'], $_REQUEST['excerpt'])) { - if ($tb_logging) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] TRACKBACK SUCCESS' . "\n"); - } + log_trackback('TRACKBACK SUCCESS'); report_trackback_success(); } else { - if ($tb_logging) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] TRACKBACK FAILURE' . "\n"); - } + log_trackback('TRACKBACK FAILURE'); report_trackback_failure(); } - - if ($tb_logging) { - fclose($fp); - } } else if ($type == 'pingback') { if ($pb_logging) { log_pingback('RECEIVED PINGBACK'); @@ -238,4 +224,14 @@ function log_pingback($message){ fclose($fp); } } + +// Debug logging for trackback receiving +function log_trackback($message){ + global $tb_logging; + if ($tb_logging) { + $fp = fopen('trackback2.log', 'a'); + fwrite($fp, '[' . date('d.m.Y H:i') . '] ' . $message . "\n"); + fclose($fp); + } +} /* vim: set sts=4 ts=4 expandtab : */ diff --git a/include/functions_comments.inc.php b/include/functions_comments.inc.php index ffbd256d..5e8dcbbc 100644 --- a/include/functions_comments.inc.php +++ b/include/functions_comments.inc.php @@ -799,7 +799,7 @@ function serendipity_insertComment($id, $commentInfo, $type = 'NORMAL', $source $commentInfo['status'] = $ca['status']; } - if ($serendipity['serendipityAuthedUser']) { + if ($serendipity['serendipityAuthedUser'] ?? false) { $authorReply = true; $authorEmail = $serendipity['serendipityEmail']; } @@ -887,7 +887,7 @@ function serendipity_insertComment($id, $commentInfo, $type = 'NORMAL', $source if ($status != 'confirm' && (serendipity_db_bool($ca['moderate_comments']) || ($type == 'NORMAL' && serendipity_db_bool($row['mail_comments'])) || (($type == 'TRACKBACK' || $type == 'PINGBACK') && serendipity_db_bool($row['mail_trackbacks'])))) { - if (! ($authorReply && $authorEmail == $row['email'])) { + if (! (($authorReply ?? false) && $authorEmail == $row['email'])) { serendipity_sendComment($cid, $row['email'], $name, $email, $url, $id, $row['title'], $comments, $type, serendipity_db_bool($ca['moderate_comments']), $referer); } } @@ -1016,22 +1016,14 @@ function serendipity_saveComment($id, $commentInfo, $type = 'NORMAL', $source = serendipity_plugin_api::hook_event('frontend_saveComment', $ca, $commentInfo); if (!is_array($ca) || serendipity_db_bool($ca['allow_comments'])) { - if ($GLOBALS['tb_logging'] ?? false) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] insert comment into DB' . "\n"); - fclose($fp); - } + log_trackback('insert comment into DB'); $commentInfo['comment_cid'] = serendipity_insertComment($id, $commentInfo, $type, $source, $ca); $commentInfo['comment_id'] = $id; serendipity_plugin_api::hook_event('frontend_saveComment_finish', $ca, $commentInfo); return true; } else { - if ($GLOBALS['tb_logging'] ?? false) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] discarding comment from DB' . "\n"); - fclose($fp); - } + log_trackback('discarding comment from DB'); return false; } diff --git a/include/functions_trackbacks.inc.php b/include/functions_trackbacks.inc.php index 05c57a8a..cdb4f180 100644 --- a/include/functions_trackbacks.inc.php +++ b/include/functions_trackbacks.inc.php @@ -324,20 +324,12 @@ function serendipity_reference_autodiscover($loc, $url, $author, $title, $text) function add_trackback($id, $title, $url, $name, $excerpt) { global $serendipity; - if ($GLOBALS['tb_logging']) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] add_trackback:' . print_r(func_get_args(), true) . "\n"); - fclose($fp); - } + log_trackback('add_trackback:' . print_r(func_get_args(), true)); // We can't accept a trackback if we don't get any URL // This is a protocol rule. if (empty($url)) { - if ($GLOBALS['tb_logging']) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] Empty URL.' . "\n"); - fclose($fp); - } + log_trackback('Empty URL.'); return 0; } @@ -350,12 +342,7 @@ function add_trackback($id, $title, $url, $name, $excerpt) { // Decode HTML Entities $excerpt = trackback_body_strip($excerpt); - - if ($GLOBALS['tb_logging']) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] Trackback body:' . $excerpt . "\n"); - fclose($fp); - } + log_trackback('Trackback body:' . $excerpt); $comment = array( 'title' => $title, @@ -365,49 +352,40 @@ function add_trackback($id, $title, $url, $name, $excerpt) { ); $is_utf8 = strtolower(LANG_CHARSET) == 'utf-8'; - - if ($GLOBALS['tb_logging']) { - $fp = fopen('trackback2.log', 'a'); - fwrite($fp, '[' . date('d.m.Y H:i') . '] TRACKBACK TRANSCODING CHECK' . "\n"); - } + log_trackback('TRACKBACK TRANSCODING CHECK'); foreach($comment AS $idx => $field) { if (is_utf8($field)) { // Trackback is in UTF-8. Check if our blog also is UTF-8. if (!$is_utf8) { - if ($GLOBALS['tb_logging']) { - fwrite($fp, '[' . date('d.m.Y H:i') . '] Transcoding ' . $idx . ' from UTF-8 to ISO' . "\n"); - } + log_trackback('Transcoding ' . $idx . ' from UTF-8 to ISO'); $comment[$idx] = utf8_decode($field); } } else { // Trackback is in some native format. We assume ISO-8859-1. Check if our blog is also ISO. if ($is_utf8) { - if ($GLOBALS['tb_logging']) { - fwrite($fp, '[' . date('d.m.Y H:i') . '] Transcoding ' . $idx . ' from ISO to UTF-8' . "\n"); - } + log_trackback('Transcoding ' . $idx . ' from ISO to UTF-8'); $comment[$idx] = utf8_encode($field); } } } - if ($GLOBALS['tb_logging']) { - fwrite($fp, '[' . date('d.m.Y H:i') . '] TRACKBACK DATA: ' . print_r($comment, true) . '...' . "\n"); - fwrite($fp, '[' . date('d.m.Y H:i') . '] TRACKBACK STORING...' . "\n"); - fclose($fp); - } + log_trackback('TRACKBACK DATA: ' . print_r($comment, true) . '...'); + log_trackback('TRACKBACK STORING...'); if ($id>0) { // first check, if we already have this pingback $comments = serendipity_fetchComments($id,1,'co.id',true,'TRACKBACK'," AND co.url='" . serendipity_db_escape_string($url) . "'"); if (is_array($comments) && sizeof($comments) == 1) { - log_pingback("We already have that TRACKBACK!"); + log_trackback("We already have that TRACKBACK!"); return 0; // We already have it! } // We don't have it, so save the pingback + log_trackback("SAVING TRACKBACK!"); serendipity_saveComment($id, $comment, 'TRACKBACK'); return 1; } else { + log_trackback("ID invalid"); return 0; } } diff --git a/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php b/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php index b6ed4678..362d9095 100644 --- a/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php +++ b/plugins/serendipity_event_spamblock/serendipity_event_spamblock.php @@ -983,7 +983,7 @@ class serendipity_event_spamblock extends serendipity_event if (!is_array($auth)) { // Filter authors names, Filter URL, Filter Content, Filter Emails, Check for maximum number of links before rejecting // moderate false - if(false === $this->wordfilter($logfile, $eventData, $wordmatch, $addData)) { + if(false === $this->wordfilter($logfile, $eventData, $wordmatch ?? null, $addData)) { // already there #$this->log($logfile, $eventData['id'], 'REJECTED', PLUGIN_EVENT_SPAMBLOCK_FILTER_WORDS, $addData); // already there #$eventData = array('allow_comments' => false); // already there #$serendipity['messagestack']['emails'][] = PLUGIN_EVENT_SPAMBLOCK_ERROR_BODY; @@ -1149,7 +1149,7 @@ class serendipity_event_spamblock extends serendipity_event } } - if(false === $this->wordfilter($logfile, $eventData, $wordmatch, $addData)) { + if(false === $this->wordfilter($logfile, $eventData, $wordmatch ?? null, $addData)) { return false; } @@ -1492,10 +1492,6 @@ class serendipity_event_spamblock extends serendipity_event // Check for word filtering if ($filter_type = $this->get_config('contentfilter_activate', 'moderate')) { - if($ftc) { - $filter_type = 'reject'; - } - // Filter authors names $filter_authors = explode(';', $this->get_config('contentfilter_authors', $this->filter_defaults['authors'])); if (is_array($filter_authors)) { @@ -1596,7 +1592,7 @@ class serendipity_event_spamblock extends serendipity_event // Check for maximum number of links in comment body to reject $link_count = substr_count(strtolower($addData['comment']), 'http://'); - if ($links_reject > 0 && $link_count > $links_reject) { + if (($links_reject ?? 0) > 0 && ($link_count ?? 0) > $links_reject) { $this->log($logfile, $eventData['id'], 'REJECTED', PLUGIN_EVENT_SPAMBLOCK_REASON_LINKS_REJECT, $addData); $eventData = array('allow_comments' => false); $serendipity['messagestack']['comments'][] = PLUGIN_EVENT_SPAMBLOCK_ERROR_BODY; @@ -1604,7 +1600,7 @@ class serendipity_event_spamblock extends serendipity_event } // Check for maximum number of links before forcing moderation - if ($links_moderate > 0 && $link_count > $links_moderate) { + if (($links_moderate ?? 0) > 0 && ($link_count ?? 0) > ($links_moderate ?? 0)) { $this->log($logfile, $eventData['id'], 'REJECTED', PLUGIN_EVENT_SPAMBLOCK_REASON_LINKS_MODERATE, $addData); $eventData['moderate_comments'] = true; $serendipity['csuccess'] = 'moderate';