public function testFilterEmailExtractBody() { $body = <<<EOD body EOD; $this->assertEquals("body", Filter_Email::extractBody($body)); $body = <<<EOD test On Wed, 2011-03-02 at 08:26 +0100, Bibledit-Web wrote: > test notes three > test On Wed, 2011-03-02 at 08:26 +0100, Bibledit-Web wrote: > test notes three EOD; $this->assertEquals("test", Filter_Email::extractBody($body, "2011", "Bibledit-Web")); }
/** * handleEmailNew - handles an email received from $from with subject $subject and body $body. * Returns true if the mail was processed, else false. * The email is considered to have been processed if it created a new Consultation Note. */ public function handleEmailNew($from, $subject, $body) { // Store the original subject. $originalSubject = $subject; // Check that the subject indicates that a new consultation note is to be created. $pos = strpos(strtolower($subject), "new note"); if ($pos === false) { return false; } // There is a new note. Remove that bit from the $subject. $subject = substr($subject, 0, $pos) . substr($subject, $pos + 8); // Clean the subject line. $subject = trim($subject); $subject = str_replace(".", " ", $subject); $subject = str_replace(":", " ", $subject); $subject = str_replace(" ", " ", $subject); $subject = str_replace(" ", " ", $subject); $subject = str_replace(" ", " ", $subject); $subject = str_replace(" ", " ", $subject); // Check that the $from address of the email belongs to an existing user. $from = Filter_Email::extractEmail($from); $database_users = Database_Users::getInstance(); if (!$database_users->emailExists($from)) { return false; } $username = $database_users->getEmailToUser($from); // Extract book, chapter, verse, and note summary from the $subject $book = NULL; $chapter = NULL; $verse = NULL; $summary = NULL; $subject = explode(" ", $subject); if (count($subject) > 0) { $book = Filter_Books::interpretBook($subject[0]); } if (count($subject) > 1) { $chapter = Filter_Numeric::integer_in_string($subject[1]); } if (count($subject) > 2) { $verse = Filter_Numeric::integer_in_string($subject[2]); } unset($subject[0]); unset($subject[1]); unset($subject[2]); $summary = implode(" ", $subject); unset($subject); // Check book, chapter, verse, and summary. Give feedback if there's anything wrong. $noteCheck = ""; if (!(is_numeric($book) && $book > 0)) { $noteCheck .= Locale_Translate::_("Unknown book"); } if (!is_numeric($chapter)) { $noteCheck .= " " . Locale_Translate::_("Unknown chapter"); } if (!is_numeric($verse)) { $noteCheck .= " " . Locale_Translate::_("Unknown verse"); } if ($summary == NULL || $summary == "") { $noteCheck .= " " . Locale_Translate::_("Unknown summary"); } // Mail user if the note could not be posted. $database_mail = Database_Mail::getInstance(); if ($noteCheck != "") { $subject = Locale_Translate::_("Your new note could not be posted"); $database_mail->send($username, $subject . ": " . $originalSubject, $noteCheck); return false; } // Clean the email's body. $body = Filter_Email::extractBody($body); // Post the note. $session_logic = Session_Logic::getInstance(); $sessionuser = $session_logic->currentUser(); $session_logic->setUsername($username); $database_notes = Database_Notes::getInstance(); $identifier = $database_notes->storeNewNote("", $book, $chapter, $verse, $summary, $body, false); $this->handlerNewNote($identifier); $session_logic->setUsername($sessionuser); // Mail confirmation to the $username. $database_config_user = Database_Config_User::getInstance(); if ($database_config_user->getUserNotifyMeOfMyPosts($username)) { $subject = Locale_Translate::_("Your new note was posted"); $database_mail->send($username, $subject . ": " . $originalSubject, $body); } // Log operation. $database_logs = Database_Logs::getInstance(); $database_logs->log("New note posted" . ":" . " " . $body); // Job done. return true; }