| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the ZBateson\MailMimeParser project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license http://opensource.org/licenses/bsd-license.php BSD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace ZBateson\MailMimeParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ZBateson\MailMimeParser\Header\HeaderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * A parsed mime message with optional mime parts depending on its type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * A mime message may have any number of mime parts, and each part may have any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * number of sub-parts, etc... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * A message is a specialized "mime part". Namely the message keeps hold of text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * versus HTML parts (and associated streams for easy access), holds a stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * for the entire message and all its parts, and maintains parts and their | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * relationships. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Zaahid Bateson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class Message extends MimePart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var string unique ID used to identify the object to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      *      $this->partStreamRegistry when registering the stream.  The ID is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      *      used for opening stream parts with the mmp-mime-message "protocol". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @see \ZBateson\MailMimeParser\SimpleDi::registerStreamExtensions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @see \ZBateson\MailMimeParser\PartStream::stream_open | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $objectId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var \ZBateson\MailMimeParser\MimePart The plain text part or null if | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *      there isn't one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $textPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var \ZBateson\MailMimeParser\MimePart The HTML part stream or null if | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      *      there isn't one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $htmlPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var \ZBateson\MailMimeParser\MimePart[] array of parts in this message  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $parts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Constructs a Message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param HeaderFactory $headerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function __construct(HeaderFactory $headerFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         parent::__construct($headerFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->objectId = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Returns the unique object ID registered with the PartStreamRegistry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * service object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function getObjectId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $this->objectId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Returns true if the $part should be assigned as this message's main | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * text part content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param \ZBateson\MailMimeParser\MimePart $part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     private function isMessageTextPart(MimePart $part) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $type = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return ($type === 'text/plain' && empty($this->textPart)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Returns true if the $part should be assigned as this message's main | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * html part content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param \ZBateson\MailMimeParser\MimePart $part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function isMessageHtmlPart(MimePart $part) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $type = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return ($type === 'text/html' && empty($this->htmlPart)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Either adds the passed part to $this->textPart if its content type is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * text/plain, to $this->htmlPart if it's text/html, or adds the part to the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * parts array otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param \ZBateson\MailMimeParser\MimePart $part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function addPart(MimePart $part) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($this->isMessageTextPart($part)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $this->textPart = $part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } elseif ($this->isMessageHtmlPart($part)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $this->htmlPart = $part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->parts[] = $part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Returns the text part (or null if none is set.) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return \ZBateson\MailMimeParser\MimePart | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     public function getTextPart() | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         return $this->textPart; | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Returns the HTML part (or null if none is set.) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return \ZBateson\MailMimeParser\MimePart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function getHtmlPart() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this->htmlPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * Returns the non-text, non-HTML part at the given 0-based index, or null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * if none is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param int $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return \ZBateson\MailMimeParser\MimePart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function getAttachmentPart($index) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         if (!isset($this->parts[$index])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return $this->parts[$index]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * Returns all attachment parts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @return \ZBateson\MailMimeParser\MimePart[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     public function getAllAttachmentParts() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $this->parts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * Returns the number of attachments available. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     public function getAttachmentCount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return count($this->parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Returns a resource handle where the text content can be read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     public function getTextStream() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         if (!empty($this->textPart)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             return $this->textPart->getContentResourceHandle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * Returns a resource handle where the HTML content can be read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     public function getHtmlStream() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         if (!empty($this->htmlPart)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             return $this->htmlPart->getContentResourceHandle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 199 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |  |