From 88d4c55ec38c68c07496ed059616b15622fc83e7 Mon Sep 17 00:00:00 2001 From: uckelman Date: Sun, 31 Oct 2010 20:53:21 +0000 Subject: Major refactoring to make bridge easier to test. git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@7431 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/PhpBB3ToMailman.php | 175 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/PhpBB3ToMailman.php (limited to 'src/PhpBB3ToMailman.php') diff --git a/src/PhpBB3ToMailman.php b/src/PhpBB3ToMailman.php new file mode 100644 index 0000000..fccd2b4 --- /dev/null +++ b/src/PhpBB3ToMailman.php @@ -0,0 +1,175 @@ +. +# + +require_once('Log.php'); +require_once('Mail.php'); + +require_once(__DIR__ . '/BBCodeParser.php'); +require_once(__DIR__ . '/Bridge.php'); +require_once(__DIR__ . '/PhpBB3.php'); +require_once(__DIR__ . '/Util.php'); +require_once(__DIR__ . '/build_email.php'); + +class PhpBB3ToMailman { + + protected $bridge; + protected $phpbb; + protected $logger; + + public function __construct(Bridge $bridge, PhpBB3 $phpbb, Log $logger) { + $this->bridge = $bridge; + $this->phpbb = $phpbb; + $this->logger = $logger; + } + + public function process($config, $user, $mode, $data, $post_data) { + # Sanity check + if (!in_array($mode, array('post', 'reply', 'quote', 'edit'))) { + throw new Exception('unrecognized mode: ' . $mode); + } + + $postId = $data['post_id']; + $forumId = $data['forum_id']; + + $this->logger->info($postId . ' received from phpBB forum ' . $forumId); + + $to = $this->bridge->getLists($forumId); + if (count($to) == 0) { + # No lists to send to, bail out. + return; + } + $to = implode(', ', $to); + + $userName = $user->data['username']; + $userEmail = $user->data['user_email']; + + $sender = 'forum-bridge@vassalengine.org'; + + $subject = html_entity_decode( + '[' . $post_data['forum_name'] . '] ' . $post_data['post_subject'], + ENT_QUOTES + ); + + $time = null; + if ($mode == 'edit') { + # Post time is NOT updated on edit, so we get the current time + $time = time(); + } + else { + $time = $this->phpbb->getPostTime($postId); + if ($time === false) { + throw new Exception('no post time: ' . $postId); + } + } + + $inReplyTo = null; + $references = null; + + if ($mode == 'reply' || $mode == 'quote') { + $firstId = $data['topic_first_post_id']; + $firstMessageId = $this->bridge->getMessageId($firstId); + if ($firstMessageId === false) { + $this->logger->info($postId . ' replies to an unknown message'); + } + else { + $inReplyTo = $references = $firstMessageId; + $this->logger->info($postId . ' replies to ' . $firstMessageId); + } + } + else if ($mode == 'edit') { + $inReplyTo = $this->bridge->getMessageId($postId); + } + + $forumURL = 'http://' . $_SERVER['SERVER_NAME'] . + dirname($_SERVER['SCRIPT_NAME']); + + $editId = $this->bridge->reserveEditId($postId); + $messageId = build_message_id($postId, $editId, + $time, $_SERVER['SERVER_NAME']); + + # Assemble the message headers + $headers = build_headers( + $userName, + $userEmail, + $to, + $sender, + $subject, + $mode == 'edit', + $time, + $messageId, + $forumURL, + $inReplyTo, + $references + ); + + # Build the message body + $parser = new BBCodeParser(); + $text = $parser->parse($data['message'], $data['bbcode_uid']); + $text = build_text($text, $mode == 'edit'); + + # Build the bridge footer + $footer = build_footer($postId, $forumURL); + + $attachments = array(); + foreach ($data['attachment_data'] as $a) { + $attachId = $a['attach_id']; + + $adata = $this->phpbb->getAttachmentData($attachId); + if ($adata === false) { + throw new Exception('unrecognized attachment id: ' . $attachId); + } + + $adata['path'] = $phpbb_root_path . $config['upload_path'] . '/' . + utf8_basename($adata['physical_filename']); + + $attachments[] = $adata; + } + + # Build the message body + $body = build_body($headers, $text, $attachments, $footer); + + $mailer = Mail::factory('sendmail'); + + # Register the message + $seen = !$this->bridge->registerByEditId($editId, $messageId, $inReplyTo); + if ($seen) { + throw new Exception('message id already seen: ' . $messageId); + } + + try { + # Send the message + $err = $mailer->send($to, $headers, $body); + if (PEAR::isError($err)) { + throw new Exception('Mail::send error: ' . $err->toString()); + } + + $this->logger->info($postId . ' sent to ' . $to . ' as ' . $messageId); + } + catch (Exception $e) { + # Bridging failed, unregister message. + $this->bridge->unregisterMessage($editId); + throw $e; + } + } +} + +?> -- cgit v1.2.3