Author: Alexandru Stanoi Date: 2007-01-24 13:09:48 +0100 (Wed, 24 Jan 2007) New Revision: 4555
Log: - Implemented feature request #8419: added the property size to ezcMailPart, which is set when parsing a mail. Modified: trunk/Mail/ChangeLog trunk/Mail/src/interfaces/part.php trunk/Mail/src/parser/parser.php trunk/Mail/src/parser/parts/delivery_status_parser.php trunk/Mail/src/parser/parts/file_parser.php trunk/Mail/src/parser/parts/multipart_alternative_parser.php trunk/Mail/src/parser/parts/multipart_digest_parser.php trunk/Mail/src/parser/parts/multipart_mixed_parser.php trunk/Mail/src/parser/parts/multipart_parser.php trunk/Mail/src/parser/parts/multipart_related_parser.php trunk/Mail/src/parser/parts/multipart_report_parser.php trunk/Mail/src/parser/parts/rfc822_digest_parser.php trunk/Mail/src/parser/parts/rfc822_parser.php trunk/Mail/src/parser/parts/text_parser.php trunk/Mail/src/parser/rfc2231_implementation.php trunk/Mail/tests/parser/parser_test.php trunk/Mail/tests/transports/transport_imap_test.php trunk/Mail/tests/transports/transport_pop3_test.php Modified: trunk/Mail/ChangeLog =================================================================== --- trunk/Mail/ChangeLog 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/ChangeLog 2007-01-24 12:09:48 UTC (rev 4555) @@ -15,6 +15,8 @@ - Added header folding for the Content-Disposition header. - Fixed an issue with ezcMailHeaderFolder::foldAny() where notices were thrown if the header contained a too long string without any white spaces. +- Implemented feature request #8419: added the property size to ezcMailPart, + which is set when parsing a mail. 1.2.1 - [RELEASEDATE] Modified: trunk/Mail/src/interfaces/part.php =================================================================== --- trunk/Mail/src/interfaces/part.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/interfaces/part.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -24,6 +24,9 @@ * on outgoing mail. Note that the ezcMailFile part sets the * Content-Disposition field itself based on it's own properties * when sending mail. + * @property int $size + * The size of the mail part in bytes. It is set when parsing a + * mail [EMAIL PROTECTED] ezcMailParser->parseMail()}. * @property-read ezcMailHeadersHolder $headers * Contains the header holder object, taking care of the * headers of this part. Can be retreived for reasons of @@ -79,6 +82,7 @@ switch ( $name ) { case 'contentDisposition': + case 'size': $this->properties[$name] = $value; break; @@ -104,6 +108,7 @@ switch ( $name ) { case 'contentDisposition': + case 'size': return isset( $this->properties[$name] ) ? $this->properties[$name] : null; case "headers": @@ -127,6 +132,7 @@ switch ( $name ) { case 'contentDisposition': + case 'size': return isset( $this->properties[$name] ); case "headers": Modified: trunk/Mail/src/parser/parser.php =================================================================== --- trunk/Mail/src/parser/parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -66,11 +66,22 @@ { $this->partParser = new ezcMailRfc822Parser(); $data = ""; + $lastData = ""; + $size = 0; while ( ( $data = $set->getNextLine() ) !== null ) { $this->partParser->parseBody( $data ); + $size += strlen( $data ); + $lastData = $data; } - $mail[] = $this->partParser->finish( $class ); + $part = $this->partParser->finish( $class ); + $part->size = $size; + if ( trim( $lastData ) === ')' ) + { + // IMAP: don't consider the last line: ) CR LF + $part->size = $part->size - 3; + } + $mail[] = $part; } while ( $set->nextMail() ); return $mail; } Modified: trunk/Mail/src/parser/parts/delivery_status_parser.php =================================================================== --- trunk/Mail/src/parser/parts/delivery_status_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/delivery_status_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -35,6 +35,13 @@ private $section; /** + * Holds the size of the mail part. + * + * @var int + */ + private $size; + + /** * Constructs a new ezcMailDeliveryStatusParser with additional headers $headers. * * @param ezcMailHeadersHolder $headers @@ -44,6 +51,7 @@ $this->headers = $headers; $this->section = 0; $this->part = new ezcMailDeliveryStatus(); + $this->size = 0; } /** @@ -54,6 +62,7 @@ public function parseBody( $line ) { $this->parseHeader( $line, $this->headers ); + $this->size += strlen( $line ); } /** @@ -99,6 +108,7 @@ public function finish() { unset( $this->part->recipients[$this->section - 1] ); // because one extra recipient is created in parseHeader() + $this->part->size = $this->size; return $this->part; } } Modified: trunk/Mail/src/parser/parts/file_parser.php =================================================================== --- trunk/Mail/src/parser/parts/file_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/file_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -274,7 +274,7 @@ { $filePart->dispositionType = ezcMailFile::DISPLAY_INLINE; } - + $filePart->size = filesize( $this->fileName ); return $filePart; } } Modified: trunk/Mail/src/parser/parts/multipart_alternative_parser.php =================================================================== --- trunk/Mail/src/parser/parts/multipart_alternative_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/multipart_alternative_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -55,6 +55,12 @@ */ public function finishMultipart() { + $size = 0; + foreach ( $this->part->getParts() as $part ) + { + $size += $part->size; + } + $this->part->size = $size; return $this->part; } } Modified: trunk/Mail/src/parser/parts/multipart_digest_parser.php =================================================================== --- trunk/Mail/src/parser/parts/multipart_digest_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/multipart_digest_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -55,6 +55,12 @@ */ public function finishMultipart() { + $size = 0; + foreach ( $this->part->getParts() as $part ) + { + $size += $part->size; + } + $this->part->size = $size; return $this->part; } } Modified: trunk/Mail/src/parser/parts/multipart_mixed_parser.php =================================================================== --- trunk/Mail/src/parser/parts/multipart_mixed_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/multipart_mixed_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -55,6 +55,12 @@ */ public function finishMultipart() { + $size = 0; + foreach ( $this->part->getParts() as $part ) + { + $size += $part->size; + } + $this->part->size = $size; return $this->part; } } Modified: trunk/Mail/src/parser/parts/multipart_parser.php =================================================================== --- trunk/Mail/src/parser/parts/multipart_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/multipart_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -226,5 +226,4 @@ */ abstract public function finishMultipart(); } - ?> Modified: trunk/Mail/src/parser/parts/multipart_related_parser.php =================================================================== --- trunk/Mail/src/parser/parts/multipart_related_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/multipart_related_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -61,6 +61,12 @@ */ public function finishMultipart() { + $size = $this->part->getMainPart()->size; + foreach ( $this->part->getRelatedParts() as $part ) + { + $size += $part->size; + } + $this->part->size = $size; return $this->part; } } Modified: trunk/Mail/src/parser/parts/multipart_report_parser.php =================================================================== --- trunk/Mail/src/parser/parts/multipart_report_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/multipart_report_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -82,6 +82,12 @@ { $this->report->setOriginalPart( $this->parts[2] ); } + $size = 0; + foreach ( $this->report->getParts() as $part ) + { + $size += $part->size; + } + $this->report->size = $size; return $this->report; } } Modified: trunk/Mail/src/parser/parts/rfc822_digest_parser.php =================================================================== --- trunk/Mail/src/parser/parts/rfc822_digest_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/rfc822_digest_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -35,6 +35,13 @@ private $mailParser = null; /** + * Holds the size of the digest. + * + * @var int + */ + private $size; + + /** * Constructs a new digest parser with the headers $headers. * * @param ezcMailHeadersHolder $headers @@ -43,6 +50,7 @@ { $this->headers = $headers; $this->mailParser = new ezcMailRfc822Parser(); + $this->size = 0; } /** @@ -55,6 +63,7 @@ public function parseBody( $line ) { $this->mailParser->parseBody( $line ); + $this->size += strlen( $line ); } /** @@ -66,9 +75,8 @@ { $digest = new ezcMailRfc822Digest( $this->mailParser->finish() ); ezcMailPartParser::parsePartHeaders( $this->headers, $digest ); + $digest->size = $this->size; return $digest; } - } - ?> Modified: trunk/Mail/src/parser/parts/rfc822_parser.php =================================================================== --- trunk/Mail/src/parser/parts/rfc822_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/rfc822_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -54,11 +54,19 @@ private $bodyParser = null; /** + * Hold the size of the mail part. + * + * @var int + */ + private $size; + + /** * Constructs a new ezcMailRfc822Parser. */ public function __construct() { $this->headers = new ezcMailHeadersHolder(); + $this->size = 0; } /** @@ -71,6 +79,7 @@ public function parseBody( $origLine ) { $line = rtrim( $origLine, "\r\n" ); + $this->size += strlen( $line ); if ( $this->parserState == self::PARSE_STATE_HEADERS && $line == '' ) { $this->parserState = self::PARSE_STATE_BODY; @@ -159,6 +168,7 @@ { $mail->body = $this->bodyParser->finish(); } + $mail->size = $this->size; return $mail; } } Modified: trunk/Mail/src/parser/parts/text_parser.php =================================================================== --- trunk/Mail/src/parser/parts/text_parser.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/parts/text_parser.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -105,7 +105,7 @@ $part->subType = $this->subType; $part->setHeaders( $this->headers->getCaseSensitiveArray() ); ezcMailPartParser::parsePartHeaders( $this->headers, $part ); - + $part->size = strlen( $this->text ); return $part; } } Modified: trunk/Mail/src/parser/rfc2231_implementation.php =================================================================== --- trunk/Mail/src/parser/rfc2231_implementation.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/src/parser/rfc2231_implementation.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -12,7 +12,7 @@ /** * This class parses header fields that conform to RFC2231. * - * Headers confirming to this specification are Content-Type and Content-Disposition. + * Headers conforming to this specification are Content-Type and Content-Disposition. * * @package Mail * @version //autogen// Modified: trunk/Mail/tests/parser/parser_test.php =================================================================== --- trunk/Mail/tests/parser/parser_test.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/tests/parser/parser_test.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -1137,5 +1137,20 @@ $original = $report->getOriginalPart(); $this->assertEquals( "[original message goes here]", trim( $original->mail->body->text ) ); } + + public function testMessageSize() + { + $parser = new ezcMailParser(); + $set = new SingleFileSet( 'various/test-html-text-and-attachment' ); + $mail = $parser->parseMail( $set ); + $mail = $mail[0]; + $this->assertEquals( 7646, $mail->size ); + $expected = array( 93, 115, 2313, 822 ); + $parts = $mail->fetchParts(); + for ( $i = 0; $i < count( $parts ); $i++ ) + { + $this->assertequals( $expected[$i], $parts[$i]->size ); + } + } } ?> Modified: trunk/Mail/tests/transports/transport_imap_test.php =================================================================== --- trunk/Mail/tests/transports/transport_imap_test.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/tests/transports/transport_imap_test.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -1302,6 +1302,23 @@ } } + public function testMessageSize() + { + $imap = new ezcMailImapTransport( "dolly.ez.no" ); + $imap->authenticate( "ezcomponents", "ezcomponents" ); + $imap->selectMailbox( 'inbox' ); + $set = $imap->fetchAll(); + $parser = new ezcMailParser(); + $mail = $parser->parseMail( $set ); + $expected = array( 1542, '1539', '1383', '63913' ); + for ( $i = 0; $i < count( $mail ); $i++ ) + { + $this->assertequals( $expected[$i], $mail[$i]->size ); + } + $parts = $mail[3]->fetchParts(); + $this->assertEquals( '45177', $parts[1]->size ); + } + public static function suite() { self::$ids = array( 15, 16, 17, 18 ); Modified: trunk/Mail/tests/transports/transport_pop3_test.php =================================================================== --- trunk/Mail/tests/transports/transport_pop3_test.php 2007-01-24 10:45:35 UTC (rev 4554) +++ trunk/Mail/tests/transports/transport_pop3_test.php 2007-01-24 12:09:48 UTC (rev 4555) @@ -384,6 +384,22 @@ } } + public function testMessageSize() + { + $pop3 = new ezcMailPop3Transport( "dolly.ez.no" ); + $pop3->authenticate( "ezcomponents", "ezcomponents" ); + $set = $pop3->fetchAll(); + $parser = new ezcMailParser(); + $mail = $parser->parseMail( $set ); + $expected = array( 1542, '1539', '1383', '63913' ); + for ( $i = 0; $i < count( $mail ); $i++ ) + { + $this->assertequals( $expected[$i], $mail[$i]->size ); + } + $parts = $mail[3]->fetchParts(); + $this->assertEquals( '45177', $parts[1]->size ); + } + public static function suite() { // small hack because the message IDs keep increasing everyday by 4 on the server -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components