From 5935bb9b34d1c54232730fca340099d6fd46e00a Mon Sep 17 00:00:00 2001 From: Ilija Studen Date: Wed, 22 Apr 2015 08:24:55 +0200 Subject: [PATCH] Minor tweaks and fixes --- src/ActiveCollab/EmailReplyExtractor.php | 23 +++--- .../Extractor/Extractor.php | 73 +++++++++---------- .../ExampleMessagesTest.php | 2 +- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/ActiveCollab/EmailReplyExtractor.php b/src/ActiveCollab/EmailReplyExtractor.php index 445f3c6..ae0e18e 100644 --- a/src/ActiveCollab/EmailReplyExtractor.php +++ b/src/ActiveCollab/EmailReplyExtractor.php @@ -17,25 +17,20 @@ final class EmailReplyExtractor const OUTLOOK = 'Outlook'; const YAHOO = 'Yahoo'; - public static function extract($path) + /** + * Parse input file and return reply + * + * @param string $path + * @return string + */ + public static function extractReply($path) { $parser = new Parser(); $parser->setPath($path); $extractor = self::getExtractor(self::detectMailer(self::getHeadersRelevantForMailerDetection($parser)), $parser); - print get_class($extractor) . " (" . basename($path) . "):\n"; - print (string) $extractor . "\n-------------------------\n"; - } - - public static function extractReply($path) - { - - } - - public static function extractForward($path) - { - + return (string) $extractor; } /** @@ -116,7 +111,7 @@ private static function getHeadersRelevantForMailerDetection(Parser &$parser) * @param string $niddle * @return boolean */ - public static function str_starts_with($string, $niddle) { + public static function strStartsWith($string, $niddle) { return substr($string, 0, strlen($niddle)) == $niddle; } } \ No newline at end of file diff --git a/src/ActiveCollab/EmailReplyExtractor/Extractor/Extractor.php b/src/ActiveCollab/EmailReplyExtractor/Extractor/Extractor.php index 5c0649d..047374d 100644 --- a/src/ActiveCollab/EmailReplyExtractor/Extractor/Extractor.php +++ b/src/ActiveCollab/EmailReplyExtractor/Extractor/Extractor.php @@ -86,8 +86,9 @@ public function joinLines() } /** - * Return splitters + * Return original message splitters * + * @todo * @return array */ protected function getOriginalMessageSplitters() @@ -95,8 +96,11 @@ protected function getOriginalMessageSplitters() return [ '-- REPLY ABOVE THIS LINE --', '-- REPLY ABOVE THIS LINE', + 'REPLY ABOVE THIS LINE --', '-- Reply above this line --', '-----Original Message-----', + '----- Original Message -----', + '-- ODGOVORI ODJE --', '-------- Original message --------' ]; } @@ -285,43 +289,40 @@ protected function &getParser() } /** - * Convert HTML to plain text (email style) - * - * @param string $html - * @param boolean $clean + * @param string $html * @return string */ - static function toPlainText($html, $clean = false) { + static function toPlainText($html) { $plain = (string) $html; // strip slashes $plain = (string) trim(stripslashes($plain)); // strip unnecessary characters - $plain = (string) preg_replace(array( - "/\r/", // strip carriage returns - "/]*>.*?<\/script>/si", // strip immediately, because we don't need any data from it - "/]*>.*?<\/style>/is", // strip immediately, because we don't need any data from it - "/style=\".*?\"/" //was: '/style=\"[^\"]*/' - ), "", $plain); + $plain = (string) preg_replace([ + "/\r/", // strip carriage returns + "/]*>.*?<\/script>/si", // strip immediately, because we don't need any data from it + "/]*>.*?<\/style>/is", // strip immediately, because we don't need any data from it + "/style=\".*?\"/" //was: '/style=\"[^\"]*/' + ], "", $plain); // entities to convert (this is not a definite list) - $entities = array( - ' ' => array(' ', ' '), - '"' => array('"', '”', '“', '“', '”', '“', '”'), - '\'' => array(''', '’', '‘', '‘', '’'), - '>' => array('>'), - '<' => array('<'), - '&' => array('&', '&'), - '(c)' => array('©', '©'), - '(R)' => array('®', '®'), - '(tm)' => array('™', '™', '™'), - '--' => array('—', '—', '—'), - '-' => array('–', '−', '–', '−'), - '*' => array('•', '•', '•'), - '�' => array('£', '£'), - 'EUR' => array('€', '€') - ); + $entities = [ + ' ' => [ ' ', ' ' ], + '"' => [ '"', '”', '“', '“', '”', '“', '”' ], + '\'' => [ ''', '’', '‘', '‘', '’' ], + '>' => [ '>' ], + '<' => [ '<' ], + '&' => [ '&', '&' ], + '(c)' => [ '©', '©' ], + '(R)' => [ '®', '®' ], + '(tm)' => [ '™', '™', '™' ], + '--' => [ '—', '—', '—' ], + '-' => [ '–', '−', '–', '−' ], + '*' => [ '•', '•', '•' ], + '�' => [ '£', '£' ], + 'EUR' => [ '€', '€' ] + ]; // convert specified entities foreach ($entities as $character => $entity) { @@ -329,9 +330,7 @@ static function toPlainText($html, $clean = false) { } // strip other not previously converted entities - $plain = (string) preg_replace(array( - '/&[^&;]+;/si', - ), "", $plain); + $plain = (string) preg_replace([ '/&[^&;]+;/si' ], "", $plain); //

converts to 2 newlines $plain = (string) preg_replace('/]*>/i', "\n\n", $plain); //

@@ -341,8 +340,8 @@ static function toPlainText($html, $clean = false) { return "\n\n" . mb_strtoupper($matches[1]) . "\n\n"; }, $plain); // - $plain = (string) preg_replace_callback(array('/]*>(.*?)<\/b>/i', '/]*>(.*?)<\/strong>/i'), function($matches) { - return mb_strtoupper($matches[1]); + $plain = (string) preg_replace_callback([ '/]*>(.*?)<\/b>/i', '/]*>(.*?)<\/strong>/i' ], function($matches) { + return $matches[1]; }, $plain); // // deal with italic elements @@ -372,9 +371,9 @@ static function toPlainText($html, $clean = false) { $url = $matches[1]; $text = $matches[2]; - if (EmailReplyExtractor::str_starts_with($url, 'http://') || EmailReplyExtractor::str_starts_with($url, 'https://')) { + if (EmailReplyExtractor::strStartsWith($url, 'http://') || EmailReplyExtractor::strStartsWith($url, 'https://')) { return "$text [$url]"; - } else if (EmailReplyExtractor::str_starts_with($url, 'mailto:')) { + } else if (EmailReplyExtractor::strStartsWith($url, 'mailto:')) { return $text . ' [' . substr($url, 7) . ']'; } else { return $text; @@ -390,8 +389,8 @@ static function toPlainText($html, $clean = false) { if (!empty($lines)) { foreach ($lines as $line) { $return[] = '> ' . $line; - } // if - } // if + } + } return "\n\n" . implode("\n", $return) . "\n\n"; }, $plain); diff --git a/test/src/ActiveCollab/EmailReplyExtractor/ExampleMessagesTest.php b/test/src/ActiveCollab/EmailReplyExtractor/ExampleMessagesTest.php index 45493cb..4674e30 100644 --- a/test/src/ActiveCollab/EmailReplyExtractor/ExampleMessagesTest.php +++ b/test/src/ActiveCollab/EmailReplyExtractor/ExampleMessagesTest.php @@ -20,7 +20,7 @@ public function testExampleMessages() } if ($file->isFile() && $file->getExtension() == 'eml') { - EmailReplyExtractor::extract($file->getPathname()); + $this->assertEquals('Email Reply', EmailReplyExtractor::extractReply($file->getPathname())); } } }