コード例 #1
0
ファイル: commentslib.php プロジェクト: jkimdon/cohomeals
 function process_inbound_mail($forumId)
 {
     global $prefs;
     require_once "lib/webmail/net_pop3.php";
     require_once "lib/mail/mimelib.php";
     $info = $this->get_forum($forumId);
     // for any reason my sybase test machine adds a space to
     // the inbound_pop_server field in the table.
     $info["inbound_pop_server"] = trim($info["inbound_pop_server"]);
     if (!$info["inbound_pop_server"] || empty($info["inbound_pop_server"])) {
         return;
     }
     $pop3 = new Net_POP3();
     $pop3->connect($info["inbound_pop_server"]);
     $pop3->login($info["inbound_pop_user"], $info["inbound_pop_password"]);
     if (!$pop3) {
         return;
     }
     $mailSum = $pop3->numMsg();
     //we don't want the operation to time out... this would result in the same messages being imported over and over...
     //(messages are only removed from the pop server on a gracefull connection termination... ie .not php or webserver a timeout)
     //$maximport should be in a admin config screen, but I don't know how to do that yet.
     $maxImport = 10;
     if ($mailSum > $maxImport) {
         $mailSum = $maxImport;
     }
     for ($i = 1; $i <= $mailSum; $i++) {
         //echo 'loop ' . $i;
         $aux = $pop3->getParsedHeaders($i);
         // If the mail came from Tiki, we don't need to add it again
         if (isset($aux['X-Tiki']) && $aux['X-Tiki'] == 'yes') {
             $pop3->deleteMsg($i);
             continue;
         }
         // If the connection is done, or the mail has an error, or whatever,
         // we try to delete the current mail (because something is wrong with it)
         // and continue on. --rlpowell
         if ($aux == FALSE) {
             $pop3->deleteMsg($i);
             continue;
         }
         //echo '<pre>';
         //print_r ($aux);
         //echo '</pre>';
         if (!isset($aux['From'])) {
             if (isset($aux['Return-path'])) {
                 $aux['From'] = $aux['Return-path'];
             } else {
                 $aux['From'] = "";
                 $aux['Return-path'] = "";
             }
         }
         //try to get the date from the email:
         $postDate = strtotime($aux['Date']);
         if ($postDate == false) {
             $postDate = $this->now;
         }
         //save the original email address, if we don't get a user match, then we
         //can at least give some info about the poster.
         $original_email = $aux["From"];
         //fix mailman addresses, or there is no chance to get a match
         $aux["From"] = str_replace(' at ', '@', $original_email);
         preg_match('/<?([-!#$%&\'*+\\.\\/0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\\.[-!#$%&\'*+\\.\\/0-9=?A-Z^_`a-z{|}~]+)>?/', $aux["From"], $mail);
         // should we throw out emails w/ invalid (possibly obfusicated) email addressses?
         //this should be an admin option, but I don't know how to put it there yet.
         $throwOutInvalidEmails = false;
         if (!array_key_exists(1, $mail)) {
             if ($throwOutInvalidEmails) {
                 continue;
             }
         }
         $email = $mail[1];
         $full = $pop3->getMsg($i);
         $mimelib = new mime();
         $output = $mimelib->decode($full);
         $body = '';
         if ($output['type'] == 'multipart/report') {
             // mimelib doesn't seem to parse error reports properly
             $pop3->deleteMsg($i);
             // and we almost certainly don't want them in the forum
             continue;
             // so do what exactly? log them somewhere? TODO
         }
         require_once 'lib/htmlpurifier_tiki/HTMLPurifier.tiki.php';
         if ($prefs['feature_forum_parse'] === 'y' && $prefs['forum_inbound_mail_parse_html'] === 'y') {
             $body = $mimelib->getPartBody($output, 'html');
             if ($body) {
                 // on some systems HTMLPurifier fails with smart quotes in the html
                 $body = $mimelib->cleanQuotes($body);
                 // some emails have invalid font and span tags that create incorrect purifying of lists
                 $body = preg_replace_callback('/\\<(ul|ol).*\\>(.*)\\<\\/(ul|ol)\\>/Umis', array($this, 'process_inbound_mail_cleanlists'), $body);
                 // Clean the string using HTML Purifier next
                 $body = HTMLPurifier($body);
                 // html emails require some speciaal handling
                 $body = preg_replace('/--(.*)--/', '~np~--$1--~/np~', $body);
                 // disable strikethough syntax
                 $body = preg_replace('/\\{(.*)\\}/', '~np~{$1}~/np~', $body);
                 // disable plugin type things
                 // special handling for MS links which contain underline tags in the label which wiki doesn't like
                 $body = preg_replace('/(\\<a .*\\>)\\<font .*\\>\\<u\\>(.*)\\<\\/u\\>\\<\\/font\\>\\<\\/a\\>/Umis', '$1$2</a>', $body);
                 $body = str_replace("<br /><br />", "<br /><br /><br />", $body);
                 // double linebreaks seem to work better as three?
                 $body = TikiLib::lib('edit')->parseToWiki($body);
                 $body = str_replace("\n\n", "\n", $body);
                 // for some reason emails seem to get line feeds quadrupled
                 $body = preg_replace('/\\[\\[(.*?)\\]\\]/', '[~np~~/np~[$1]]', $body);
                 // links surrounded by [square brackets] need help
             }
         }
         if (!$body) {
             $body = $mimelib->getPartBody($output, 'text');
             if (empty($body)) {
                 // no text part so look for html
                 $body = $mimelib->getPartBody($output, 'html');
                 $body = HTMLPurifier($body);
                 $body = $this->htmldecode(strip_tags($body));
                 $body = str_replace("\n\n", "\n", $body);
                 // and again
                 $body = str_replace("\n\n", "\n", $body);
             }
             if ($prefs['feature_forum_parse'] === 'y') {
                 $body = preg_replace('/--(.*)--/', '~np~--$1--~/np~', $body);
                 // disable strikethough if...
                 $body = preg_replace('/\\{(.*)\\}/', '~np~\\{$1\\}~/np~', $body);
                 // disable plugin type things
             }
             $body = $mimelib->cleanQuotes($body);
         }
         if (!empty($info['outbound_mails_reply_link']) && $info['outbound_mails_reply_link'] === 'y') {
             $body = preg_replace('/^.*?Reply Link\\: \\<[^\\>]*\\>.*\\r?\\n/m', '', $body);
             // remove previous reply links to reduce clutter and confusion
             // remove "empty" lines at the end
             $lines = preg_split("/(\r\n|\n|\r)/", $body);
             $body = '';
             $len = count($lines) - 1;
             $found = false;
             for ($line = $len; $line >= 0; $line--) {
                 if ($found || !preg_match('/^\\s*\\>*\\s*[\\-]*\\s*$/', $lines[$line])) {
                     $body = "{$lines[$line]}\r\n{$body}";
                     $found = true;
                 }
             }
         }
         // Remove 're:' and [forum]. -rlpowell
         $title = trim(preg_replace("/[rR][eE]:/", "", preg_replace("/\\[[-A-Za-z _:]*\\]/", "", $output['header']['subject'])));
         $title = $mimelib->cleanQuotes($title);
         // trim off < and > from message-id
         $message_id = substr($output['header']["message-id"], 1, strlen($output['header']["message-id"]) - 2);
         if (isset($output['header']["in-reply-to"])) {
             $in_reply_to = substr($output['header']["in-reply-to"], 1, strlen($output['header']["in-reply-to"]) - 2);
         } else {
             $in_reply_to = '';
         }
         // Determine user from email
         $userName = $this->table('users_users')->fetchOne('login', array('email' => $email));
         //use anonomus name feature if we don't have a real name
         if (!$userName) {
             $anonName = $original_email;
         }
         //Todo: check permissions
         // Determine if the thread already exists first by looking for a mail this is a reply to.
         if (!empty($in_reply_to)) {
             $parentId = $this->table('tiki_comments')->fetchOne('threadId', array('object' => $forumId, 'objectType' => 'forum', 'message_id' => $in_reply_to));
         } else {
             $parentId = 0;
         }
         // if not, check if there's a topic with exactly this title
         if (!$parentId) {
             $parentId = $this->table('tiki_comments')->fetchOne('threadId', array('object' => $forumId, 'objectType' => 'forum', 'parentId' => 0, 'title' => $title));
         }
         if (!$parentId) {
             // create a thread to discuss a wiki page if the feature is on AND the page exists
             if ($prefs['feature_wiki_discuss'] === 'y' && TikiLib::lib('tiki')->page_exists($title)) {
                 // No thread already; create it.
                 $temp_msid = '';
                 $parentId = $this->post_new_comment('forum:' . $forumId, 0, $userName, $title, sprintf(tra("Use this thread to discuss the %s page."), "(({$title}))"), $temp_msid, $in_reply_to);
                 $this->register_forum_post($forumId, 0);
                 // First post is in reply to this one
                 $in_reply_to = $temp_msid;
             } else {
                 $parentId = 0;
             }
         }
         // post
         $threadid = $this->post_new_comment('forum:' . $forumId, $parentId, $userName, $title, $body, $message_id, $in_reply_to, 'n', '', '', '', $anonName, $postDate);
         $this->register_forum_post($forumId, $parentId);
         // Process attachments
         if (array_key_exists('parts', $output) && count($output['parts']) > 1) {
             $forum_info = $this->get_forum($forumId);
             if ($forum_info['att'] != 'att_no') {
                 $errors = array();
                 foreach ($output['parts'] as $part) {
                     if (array_key_exists('disposition', $part)) {
                         if ($part['disposition'] == 'attachment') {
                             if (!empty($part['d_parameters']['filename'])) {
                                 $part_name = $part['d_parameters']['filename'];
                             } else {
                                 if (preg_match('/filename=([^;]*)/', $part['d_parameters']['atend'], $mm)) {
                                     // not sure what this is but it seems to have the filename in it
                                     $part_name = $mm[1];
                                 } else {
                                     $part_name = "Unnamed File";
                                 }
                             }
                             $this->add_thread_attachment($forum_info, $threadid, $errors, $part_name, $part['type'], strlen($part['body']), 1, '', '', $part['body']);
                         } elseif ($part['disposition'] == 'inline') {
                             if (!empty($part['parts'])) {
                                 foreach ($part['parts'] as $p) {
                                     $this->add_thread_attachment($forum_info, $threadid, $errors, '-', $p['type'], strlen($p['body']), 1, '', '', $p['body']);
                                 }
                             } else {
                                 if (!empty($part['body'])) {
                                     $this->add_thread_attachment($forum_info, $threadid, $errors, '-', $part['type'], strlen($part['body']), 1, '', '', $part['body']);
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // Deal with mail notifications.
         if (array_key_exists('outbound_mails_reply_link', $info) && $info['outbound_mails_for_inbound_mails'] == 'y') {
             include_once 'lib/notifications/notificationemaillib.php';
             sendForumEmailNotification('forum_post_thread', $threadid, $info, $title, $body, $userName, $title, $message_id, $in_reply_to, $threadid, $parentId);
         }
         $pop3->deleteMsg($i);
     }
     $pop3->disconnect();
 }