[4414] | 1 | <?php |
---|
[4436] | 2 | /** |
---|
| 3 | * Classe que manipula e gerencia toda a parte de anexos. |
---|
| 4 | * |
---|
| 5 | * @package ExpressoMail |
---|
| 6 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 7 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 8 | * @author Cristiano Correa |
---|
| 9 | */ |
---|
[4414] | 10 | |
---|
| 11 | class attachment |
---|
| 12 | { |
---|
| 13 | /* |
---|
| 14 | * Globals variables |
---|
| 15 | */ |
---|
| 16 | var $mbox; |
---|
| 17 | var $imap_port; |
---|
| 18 | var $has_cid; |
---|
| 19 | var $imap_options; |
---|
| 20 | var $imap_sentfolder; |
---|
| 21 | var $msgNumber; |
---|
| 22 | var $folder; |
---|
| 23 | var $structure; |
---|
| 24 | var $decodeConf; |
---|
| 25 | //------------------------------------------// |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Constructor |
---|
[4436] | 29 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 30 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
[4414] | 31 | * @param string $folder Imap folder name |
---|
| 32 | * @param integer $msgNumber Imap menssagem number |
---|
| 33 | */ |
---|
| 34 | function attachment($rootPath = false) |
---|
| 35 | { |
---|
| 36 | /* |
---|
| 37 | * Requires |
---|
| 38 | */ |
---|
| 39 | if($rootPath) |
---|
| 40 | { |
---|
| 41 | require_once $rootPath.'/library/mime/mimePart.php'; |
---|
| 42 | require_once $rootPath.'/library/mime/mimeDecode.php'; |
---|
| 43 | } |
---|
| 44 | else |
---|
| 45 | { |
---|
| 46 | require_once $_SESSION['rootPath'].'/library/mime/mimePart.php'; |
---|
| 47 | require_once $_SESSION['rootPath'].'/library/mime/mimeDecode.php'; |
---|
| 48 | } |
---|
| 49 | //----------------------------------------------------------// |
---|
| 50 | |
---|
| 51 | $this->decodeConf['include_bodies'] = true; |
---|
| 52 | $this->decodeConf['decode_bodies'] = true; |
---|
| 53 | $this->decodeConf['decode_headers'] = true; |
---|
| 54 | } |
---|
| 55 | //----------------------------------------------------------------------------// |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Open mail from Imap and parse structure |
---|
[4436] | 61 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 62 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 63 | * @return string menssagem |
---|
[4414] | 64 | */ |
---|
| 65 | public function setStructureFromMail($folder,$msgNumber) |
---|
| 66 | { |
---|
| 67 | $this->folder = mb_convert_encoding($folder, 'UTF7-IMAP',mb_detect_encoding($folder.'x', 'UTF-8, ISO-8859-1')); |
---|
| 68 | $this->msgNumber = $msgNumber; |
---|
| 69 | $this->username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; |
---|
| 70 | $this->password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
| 71 | $this->imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer']; |
---|
| 72 | $this->imap_port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort']; |
---|
| 73 | $this->imap_delimiter = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDelimiter']; |
---|
| 74 | $this->imap_sentfolder = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder'] ? $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder'] : str_replace("*","", $this->functions->getLang("Sent")); |
---|
| 75 | $this->has_cid = false; |
---|
| 76 | |
---|
| 77 | if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes') |
---|
| 78 | $this->imap_options = '/tls/novalidate-cert'; |
---|
| 79 | else |
---|
| 80 | $this->imap_options = '/notls/novalidate-cert'; |
---|
| 81 | |
---|
| 82 | $this->mbox = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$this->folder , $this->username, $this->password) or die('Error'); |
---|
| 83 | |
---|
| 84 | $rawMessageData = $this->_getRaw(); |
---|
| 85 | $decoder = new Mail_mimeDecode($rawMessageData); |
---|
| 86 | $this->structure = $decoder->decode($this->decodeConf); |
---|
| 87 | |
---|
| 88 | /* |
---|
| 89 | * Clean memory and close imap connection |
---|
| 90 | */ |
---|
| 91 | $rawMessageData = null; |
---|
| 92 | $decoder = null; |
---|
| 93 | @imap_close($this->mbox); |
---|
| 94 | //-----------------------------------------// |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
[4436] | 98 | * Set Stucture from Mail_mimeDecode Structure |
---|
| 99 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 100 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 101 | * @param Mail_mimeDecode $structure |
---|
[4414] | 102 | */ |
---|
| 103 | public function setStructure($structure) |
---|
| 104 | { |
---|
| 105 | $this->structure = $structure; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Set Stucture from raw mail code |
---|
[4436] | 110 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 111 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
[4414] | 112 | * @param Mail_mimeDecode $structure |
---|
| 113 | */ |
---|
| 114 | public function setStructureFromRawMail($rawMail) |
---|
| 115 | { |
---|
| 116 | $decoder = new Mail_mimeDecode($rawMail); |
---|
| 117 | $this->structure = $decoder->decode($this->decodeConf); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Returns Attachment Decoded |
---|
[4436] | 122 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 123 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 124 | * @param string $partNumber Index part |
---|
[4414] | 125 | * @return string Attachment Decoded |
---|
| 126 | */ |
---|
| 127 | public function getAttachment($partNumber) |
---|
| 128 | { |
---|
| 129 | $partContent = ''; |
---|
| 130 | $this->_getPartContent($this->structure, $partNumber, $partContent); |
---|
| 131 | return $partContent; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * Returns EmbeddedImages Infos |
---|
[4436] | 136 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 137 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 138 | * @return array EmbeddedImages |
---|
[4414] | 139 | */ |
---|
| 140 | public function getEmbeddedImagesInfo() |
---|
| 141 | { |
---|
| 142 | $imagesEmbedded = array(); |
---|
| 143 | $this->_getEmbeddedImagesInfo($this->structure,$imagesEmbedded); |
---|
| 144 | return $imagesEmbedded; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Returns Attachments Infos |
---|
[4436] | 149 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 150 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 151 | * @return array |
---|
[4414] | 152 | */ |
---|
| 153 | public function getAttachmentsInfo() |
---|
| 154 | { |
---|
| 155 | $attachments = array(); |
---|
| 156 | $this->_getAttachmentsInfo($this->structure,$attachments); |
---|
| 157 | return $attachments; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * Returns Attachment Info |
---|
[4436] | 162 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 163 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 164 | * @param string $partNumber Index part |
---|
[4414] | 165 | * @return array |
---|
| 166 | */ |
---|
| 167 | public function getAttachmentInfo($partNumber) |
---|
| 168 | { |
---|
| 169 | $attachment = array(); |
---|
| 170 | $this->_getPartInfo($this->structure,$partNumber,$attachment); |
---|
| 171 | return $attachment; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * returns the source code menssagem |
---|
[4436] | 176 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 177 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 178 | * @return string menssagem |
---|
[4414] | 179 | */ |
---|
| 180 | private function _getRaw() |
---|
| 181 | { |
---|
| 182 | return imap_fetchheader($this->mbox, $this->msgNumber, FT_UID).imap_body($this->mbox, $this->msgNumber, FT_UID); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * Returns content from the searched |
---|
[4436] | 187 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 188 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 189 | * @param pointer $structure Structure object |
---|
[4414] | 190 | * @param string $soughtIndex |
---|
| 191 | * @param pointer $body Content |
---|
| 192 | * @param string $pIndex |
---|
| 193 | */ |
---|
| 194 | private function _getPartContent(&$structure, $soughtIndex,&$body,$pIndex = '0') |
---|
| 195 | { |
---|
| 196 | if($structure->parts) |
---|
| 197 | { |
---|
| 198 | foreach ($structure->parts as $index => $part) |
---|
| 199 | { |
---|
| 200 | if(strtolower($part->ctype_primary) == 'multipart') |
---|
| 201 | $this->_getPartContent($part,$soughtIndex,$body,$pIndex.'.'.$index); |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) |
---|
| 205 | $this->_getPartContent($part,$soughtIndex,$body,$pIndex.'.'.$index); |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | $currentIndex = $pIndex.'.'.$index; |
---|
| 209 | if($currentIndex == $soughtIndex) |
---|
| 210 | { |
---|
| 211 | $body = $part->body; |
---|
| 212 | break; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | else if($soughtIndex == '0') |
---|
| 219 | $body = $structure->body; |
---|
| 220 | } |
---|
| 221 | |
---|
[4436] | 222 | /** |
---|
| 223 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 224 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 225 | */ |
---|
[4414] | 226 | private function _getPartInfo(&$structure, $soughtIndex,&$info,$pIndex = '0') |
---|
| 227 | { |
---|
| 228 | |
---|
| 229 | if($structure->parts) |
---|
| 230 | { |
---|
| 231 | foreach ($structure->parts as $index => $part) |
---|
| 232 | { |
---|
| 233 | if(strtolower($part->ctype_primary) == 'multipart') |
---|
| 234 | $this->_getPartInfo($part,$soughtIndex,$info,$pIndex.'.'.$index); |
---|
| 235 | else |
---|
| 236 | { |
---|
| 237 | if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) |
---|
| 238 | $this->_getPartInfo($part,$soughtIndex,$info,$pIndex.'.'.$index); |
---|
| 239 | else |
---|
| 240 | { |
---|
| 241 | $currentIndex = $pIndex.'.'.$index; |
---|
| 242 | if($currentIndex == $soughtIndex) |
---|
| 243 | { |
---|
[4774] | 244 | $this->_pushAttachmentsInfo($part,$info,$currentIndex); |
---|
[4414] | 245 | break; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | }else if($soughtIndex == '0') |
---|
[4774] | 251 | $this->_pushAttachmentsInfo($structure,$info); |
---|
[4414] | 252 | } |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | /** |
---|
| 257 | * Write in $attachments, array with the information of attachments |
---|
| 258 | * @param <type> $structure |
---|
| 259 | * @param <type> $attachments |
---|
| 260 | */ |
---|
| 261 | private function _getAttachmentsInfo($structure, &$attachments, $pIndex = '0') |
---|
| 262 | { |
---|
| 263 | |
---|
| 264 | if($structure->parts) |
---|
| 265 | { |
---|
| 266 | foreach ($structure->parts as $index => $part) |
---|
| 267 | { |
---|
| 268 | if(strtolower($part->ctype_primary) == 'multipart') |
---|
| 269 | $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index); |
---|
| 270 | else |
---|
| 271 | { |
---|
| 272 | if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) |
---|
| 273 | $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index); |
---|
| 274 | else |
---|
| 275 | { |
---|
[4710] | 276 | if(!$part->headers['content-transfer-encoding']) //Caso não esteja especificado a codificação |
---|
| 277 | $part->headers['content-transfer-encoding'] = mb_detect_encoding ($part->body,array('BASE64','Quoted-Printable','7bit','8bit','ASCII')); |
---|
[4774] | 278 | if($part->headers['content-transfer-encoding'] === ('ASCII' || false)) //Caso a codificação retorne ascii ou false especifica como base64 |
---|
[4710] | 279 | $part->headers['content-transfer-encoding'] = 'base64'; |
---|
| 280 | |
---|
[4774] | 281 | $this->_pushAttachmentsInfo($part,$attachments,$pIndex.'.'.$index); |
---|
[4414] | 282 | |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | else |
---|
[4774] | 288 | $this->_pushAttachmentsInfo($structure,$attachments); |
---|
[4414] | 289 | |
---|
| 290 | } |
---|
| 291 | |
---|
[4774] | 292 | |
---|
[5079] | 293 | /** |
---|
| 294 | * Write in $$attachments, array with the information of attachment |
---|
| 295 | * |
---|
| 296 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 297 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 298 | * @sponsor Caixa Econômica Federal |
---|
| 299 | * @param $structure |
---|
| 300 | * @param $attachments |
---|
| 301 | * @param $pIndex |
---|
| 302 | */ |
---|
[4774] | 303 | private function _pushAttachmentsInfo(&$structure, &$attachments, $pIndex = '0') |
---|
| 304 | { |
---|
| 305 | $name = false; |
---|
| 306 | if($structure->d_parameters['filename']) $name = $structure->d_parameters['filename']; |
---|
| 307 | else if($structure->ctype_parameters['name']) $name = $structure->ctype_parameters['name']; |
---|
| 308 | else if(strtolower($structure->ctype_primary) == 'text' && strtolower($structure->ctype_secondary) == 'calendar') $name = 'calendar.ics'; |
---|
| 309 | |
---|
| 310 | if($name) |
---|
| 311 | { |
---|
| 312 | $codificao = mb_detect_encoding($name.'x', 'UTF-8, ISO-8859-1'); |
---|
| 313 | if($codificao == 'UTF-8') $name = utf8_decode($name); |
---|
| 314 | |
---|
| 315 | $definition['pid'] = $pIndex; |
---|
| 316 | $definition['name'] = addslashes(mb_convert_encoding($name, "ISO-8859-1")); |
---|
| 317 | $definition['encoding'] = $structure->headers['content-transfer-encoding']; |
---|
| 318 | $definition['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']); |
---|
| 319 | |
---|
| 320 | array_push($attachments, $definition); |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
[4414] | 329 | /** |
---|
| 330 | * Write in $images, array with the information of Embedded Images |
---|
| 331 | * @param <type> $structure |
---|
| 332 | * @param <type> $attachments |
---|
| 333 | */ |
---|
| 334 | private function _getEmbeddedImagesInfo($structure, &$images, $pIndex = '0') |
---|
| 335 | { |
---|
| 336 | foreach ($structure->parts as $index => $part) |
---|
| 337 | { |
---|
| 338 | if(strtolower($part->ctype_primary) == 'multipart') |
---|
| 339 | $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index); |
---|
| 340 | else |
---|
| 341 | { |
---|
| 342 | if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) |
---|
| 343 | $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index); |
---|
| 344 | else |
---|
| 345 | { |
---|
| 346 | if(!$part->ctype_parameters['name']) |
---|
| 347 | $name = $part->d_parameters['filename']; |
---|
| 348 | else |
---|
| 349 | $name = $part->ctype_parameters['name']; |
---|
| 350 | |
---|
| 351 | $type = strtolower(substr($name, -4)); |
---|
| 352 | $ctype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary); |
---|
| 353 | if(strtolower($part->ctype_primary) == 'image' || ($ctype == 'application/octet-stream' && ($type == '.png' || $type == '.jpg' || $type == '.gif'))) |
---|
| 354 | { |
---|
| 355 | $definition['pid'] = $pIndex.'.'.$index; |
---|
[4729] | 356 | $definition['name'] = addslashes($name); |
---|
[4414] | 357 | $definition['type'] = 'image/'.strtolower($part->ctype_secondary); |
---|
| 358 | $definition['encoding'] = $part->headers['content-transfer-encoding']; |
---|
| 359 | $definition['cid'] = $part->headers['content-id']; |
---|
| 360 | array_push($images, $definition); |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | ?> |
---|