Mercurial > hg > pnewss
view dbreindex.php @ 14:372f4e195986 draft default tip
Added article blacklist function, switch to binary-safe latin1 character set for database
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Thu, 17 Jul 2014 23:24:17 +0200 |
parents | 005339a1b2ce |
children |
line wrap: on
line source
#!/usr/bin/php <?php /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ chdir(__DIR__); require_once './common.php'; $lastposted = NULL; while (TRUE) { if ($lastposted === NULL) { $articles = $db->evalAllAssoc('SELECT * FROM `messages` LIMIT 10'); } else { $articles = $db->evalAllAssoc('SELECT * FROM `messages` WHERE `id` > ? LIMIT 10', $lastposted); } if (!count($articles)) break; foreach ($articles as $article) { $headers = array(); $header = array(); $headerchanged = FALSE; foreach (explode("\r\n", $article['header']) as $line) { if (!strlen($line) || $line == '.') { print("Article $article[id] Contains empty or terminating header line, fixing.\n"); $headerchanged = TRUE; continue; } else if (strpos($line, "\r") !== FALSE || strpos($line, "\n") !== FALSE || strpos($line, "\0")) { print("Article $article[id] Contains invalid newline or NUL character in header, fixing.\n"); $line = str_replace(array("\r","\n","\0"), '', $line); $headerchanged = TRUE; } $parts = explode(': ', $line, 2); $headername = strtoupper($parts[0]); switch ($headername) { case 'PATH': case 'FROM': case 'NEWSGROUPS': case 'SUBJECT': case 'DATE': case 'MESSAGE-ID': case 'SENDER': if (isset($headers[$headername])) { print("Article $article[id] Contains duplicate header $headername, removing.\n"); $headerchanged = TRUE; break; } $header[] = $line; $headers[strtoupper($parts[0])] = $parts[1]; break; case 'ORGANIZATION': case 'LINES': case 'MIME-VERSION': case 'CONTENT-TYPE': case 'CONTENT-TRANSFER-ENCODING': case 'USER-AGENT': case 'REFERENCES': case 'REPLY-TO': case 'SENDER': case 'FOLLOWUP-TO': case 'IN-REPLY-TO': case 'EXPIRES': case 'CONTROL': case 'DISTRIBUTION': case 'KEYWORDS': case 'SUMMARY': $header[] = $line; break; case 'NNTP-POSTING-HOST': case 'X-TRACE': case 'XREF': case 'X-COMPLAINTS-TO': case 'NNTP-POSTING-DATE': print("Article $article[id] Contains unacceptable header $headername\n"); $headerchanged = TRUE; break; default: $header[] = $line; break; } } foreach (explode("\r\n", $article['body']) as $line) { if ($line == '.') { print("Article $article[id] Contains terminating body line\n"); } else if (strpos($line, "\r") !== FALSE || strpos($line, "\n") !== FALSE || strpos($line, "\0")) { print("Article $article[id] Contains invalid newline or NUL character in body\n"); } } if (!isset($headers['NEWSGROUPS'])) { print("Article $article[id] Missing required Newsgroups header\n"); continue; } $newsgroups = array(); foreach (explode(',', $headers['NEWSGROUPS']) as $groupname) { $group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `name` = ?', $groupname); if ($group === FALSE) continue; $newsgroups[] = $group['id']; } if (!count($newsgroups)) { print("Article $article[id] No known newsgroups listed\n"); continue; } if (!isset($headers['MESSAGE-ID'])) { print("Article $article[id] Missing required Message-ID header\n"); continue; } $msgid = $headers['MESSAGE-ID']; if (strlen($msgid) <= 2 || $msgid[0] != '<' || $msgid[strlen($msgid)-1] != '>') { print("Article $article[id] Malformed Message-ID\n"); } else { $msgid = substr($msgid, 1, -1); if ($msgid != $article['messageid']) { print("Article $article[id] Message-ID header does not match database, fixing.\n"); $db->update('UPDATE `messages` SET `messageid` = ? WHERE `id` = ?', array($msgid, $article['id'])); } } if ($headerchanged) { print("Article $article[id] Updating headers.\n"); $db->update('UPDATE `messages` SET `header` = ? WHERE `id` = ?', array(implode("\r\n", $header), $article['id'])); } foreach ($newsgroups as $groupid) { if (FALSE === $db->evalRow('SELECT * FROM `groupmessages` WHERE `group` = ? AND `message` = ?', array($groupid, $article['id']))) { print("Article $article[id] Missing link in group $groupid, fixing.\n"); $db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($groupid, $article['id'])); } } if ($article['id'] > $lastposted) $lastposted = $article['id']; } }