Ignore:
Timestamp:
08/03/11 12:00:08 (13 years ago)
Author:
thiagoaos
Message:

Ticket #2180 - Adicionado código fonte completo do zpush

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/z-push/backend/imap.php

    r4619 r4898  
    99 * Created   :   10.10.2007 
    1010 * 
    11  * ï¿œ Zarafa Deutschland GmbH, www.zarafaserver.de 
     11 * Zarafa Deutschland GmbH, www.zarafaserver.de 
    1212 * This file is distributed under GPL v2. 
    1313 * Consult LICENSE file for details 
     
    9191         * the new message as any other new message in a folder. 
    9292         */ 
    93     function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { 
    94         debugLog("IMAP-SendMail: for: $forward   reply: $reply   parent: $parent  RFC822:  \n". $rfc822 ); 
    95  
    96         $mobj = new Mail_mimeDecode($rfc822); 
    97         $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); 
    98          
    99         $Mail_RFC822 = new Mail_RFC822(); 
    100         $toaddr = $ccaddr = $bccaddr = ""; 
    101         if(isset($message->headers["to"])) 
    102             $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); 
    103         if(isset($message->headers["cc"])) 
    104             $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); 
    105         if(isset($message->headers["bcc"])) 
    106             $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); 
    107  
    108         // save some headers when forwarding mails (content type & transfer-encoding) 
    109         $headers = ""; 
    110         $forward_h_ct = ""; 
    111         $forward_h_cte = ""; 
    112         $envelopefrom = ""; 
    113  
    114         $use_orgbody = false; 
    115          
    116         // clean up the transmitted headers 
    117         // remove default headers because we are using imap_mail 
    118         $changedfrom = false; 
    119         $returnPathSet = false; 
    120         $body_base64 = false; 
    121         $org_charset = ""; 
    122         $org_boundary = false; 
    123         $multipartmixed = false; 
    124         foreach($message->headers as $k => $v) { 
    125             if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") 
    126                 continue; 
    127  
    128             if ($k == "content-type") { 
    129                 // if the message is a multipart message, then we should use the sent body 
    130                 if (preg_match("/multipart/i", $v)) { 
    131                     $use_orgbody = true; 
    132                     $org_boundary = $message->ctype_parameters["boundary"]; 
    133                 } 
    134  
    135                 // save the original content-type header for the body part when forwarding 
    136                 if ($forward && !$use_orgbody) { 
    137                     $forward_h_ct = $v; 
    138                     continue; 
    139                 } 
    140  
    141                 // set charset always to utf-8 
    142                 $org_charset = $v; 
    143                 $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); 
    144             } 
    145  
    146             if ($k == "content-transfer-encoding") { 
    147                 // if the content was base64 encoded, encode the body again when sending 
    148                 if (trim($v) == "base64") $body_base64 = true; 
    149  
    150                 // save the original encoding header for the body part when forwarding 
    151                 if ($forward) { 
    152                     $forward_h_cte = $v; 
    153                     continue; 
    154                 } 
    155             } 
    156  
    157             // check if "from"-header is set, do nothing if it's set 
    158             // else set it to IMAP_DEFAULTFROM 
    159             if ($k == "from") { 
    160                 if (trim($v)) { 
    161                     $changedfrom = true; 
    162                 } elseif (! trim($v) && IMAP_DEFAULTFROM) { 
    163                     $changedfrom = true; 
    164                     if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    165                     else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    166                     else $v = $this->_username . IMAP_DEFAULTFROM; 
    167                     $envelopefrom = "-f$v"; 
    168                 } 
    169             } 
    170  
    171             // check if "Return-Path"-header is set 
    172             if ($k == "return-path") { 
    173                 $returnPathSet = true; 
    174                 if (! trim($v) && IMAP_DEFAULTFROM) { 
    175                     if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    176                     else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    177                     else $v = $this->_username . IMAP_DEFAULTFROM; 
    178                 } 
    179             } 
    180  
    181             // all other headers stay 
    182             if ($headers) $headers .= "\n"; 
    183             $headers .= ucfirst($k) . ": ". $v; 
    184         } 
    185  
    186         // set "From" header if not set on the device 
    187         if(IMAP_DEFAULTFROM && !$changedfrom){ 
    188             if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    189             else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    190             else $v = $this->_username . IMAP_DEFAULTFROM; 
    191             if ($headers) $headers .= "\n"; 
    192             $headers .= 'From: '.$v; 
    193             $envelopefrom = "-f$v"; 
    194         } 
    195  
    196         // set "Return-Path" header if not set on the device 
    197         if(IMAP_DEFAULTFROM && !$returnPathSet){ 
    198             if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    199             else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    200             else $v = $this->_username . IMAP_DEFAULTFROM; 
    201             if ($headers) $headers .= "\n"; 
    202             $headers .= 'Return-Path: '.$v; 
    203         } 
    204  
    205         // if this is a multipart message with a boundary, we must use the original body 
    206         if ($use_orgbody) { 
    207             list(,$body) = $mobj->_splitBodyHeader($rfc822);  
    208             $repl_body = $this->getBody($message);  
    209             if ($message->parts[0]->headers["content-transfer-encoding"] == "base64") $multipart_text_cte_base64 = true; 
    210             else $multipart_text_cte_base64 = false; 
    211         } 
    212         else 
    213             $body = $this->getBody($message); 
    214  
    215         // reply 
    216         if ($reply && $parent) { 
    217             $this->imap_reopenFolder($parent); 
    218             // receive entire mail (header + body) to decode body correctly 
    219             $origmail = @imap_fetchheader($this->_mbox, $reply, FT_UID) . @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); 
    220  
    221             $mobj2 = new Mail_mimeDecode($origmail); 
    222             // receive only body 
    223             $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8'))); 
    224             // unset mimedecoder & origmail - free memory 
    225             unset($mobj2); 
    226             unset($origmail); 
    227         } 
    228  
    229         // encode the body to base64 if it was sent originally in base64 by the pda 
    230         // contrib - chunk base64 encoded body 
    231         if ($body_base64 && !$forward) $body = chunk_split(base64_encode($body)); 
    232  
    233  
    234         // forward 
    235         if ($forward && $parent) { 
    236             $this->imap_reopenFolder($parent); 
    237             // receive entire mail (header + body) 
    238             $origmail = @imap_fetchheader($this->_mbox, $forward, FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); 
    239  
    240             //if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) { 
    241             if (defined('IMAP_INLINE_FORWARD') && IMAP_INLINE_FORWARD === false) { 
    242                 // contrib - chunk base64 encoded body 
    243                 if ($body_base64) $body = chunk_split(base64_encode($body)); 
    244                 //use original boundary if it's set 
    245                 $boundary = ($org_boundary) ? $org_boundary : false; 
    246                 // build a new mime message, forward entire old mail as file 
    247                 list($aheader, $body) = $this->mail_attach("forwarded_message.eml",strlen($origmail),$origmail, $body, $forward_h_ct, $forward_h_cte,$boundary); 
    248                 // add boundary headers 
    249                 $headers .= "\n" . $aheader; 
    250  
    251             } 
    252             else { 
    253                 $mobj2 = new Mail_mimeDecode($origmail); 
    254                 $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); 
    255  
    256                 if (!$use_orgbody) 
    257                     $nbody = $body; 
    258                 else 
    259                     $nbody = $repl_body; 
    260  
    261                 $nbody .= "\r\n\r\n"; 
    262                 $nbody .= "-----Original Message-----\r\n"; 
    263                 if(isset($mess2->headers['from'])) 
    264                     $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; 
    265                 if(isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) 
    266                     $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; 
    267                 if(isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) 
    268                     $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; 
    269                 if(isset($mess2->headers['date'])) 
    270                     $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; 
    271                 if(isset($mess2->headers['subject'])) 
    272                     $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; 
    273                 $nbody .= "\r\n"; 
    274                 $nbody .= $this->getBody($mess2); 
    275  
    276                 if ($body_base64) { 
    277                     // contrib - chunk base64 encoded body 
    278                     $nbody = chunk_split(base64_encode($nbody)); 
    279                     if ($use_orgbody) 
    280                     // contrib - chunk base64 encoded body 
    281                         $repl_body = chunk_split(base64_encode($repl_body)); 
    282                 } 
    283  
    284                 if ($use_orgbody) { 
    285                     debugLog("-------------------"); 
    286                     debugLog("old:\n'$repl_body'\nnew:\n'$nbody'\nund der body:\n'$body'"); 
    287                     //$body is quoted-printable encoded while $repl_body and $nbody are plain text, 
    288                     //so we need to decode $body in order replace to take place 
    289                     if (!$multipart_text_cte_base64) { 
    290                        $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); 
    291                     } else { 
    292                        $body = str_replace(base64_encode($repl_body), base64_encode($nbody), $body);                    } 
    293                 } 
    294                 else 
    295                     $body = $nbody; 
    296  
    297  
    298                 if(isset($mess2->parts)) { 
    299                     $attached = false; 
    300  
    301                     if ($org_boundary) { 
    302                         $att_boundary = $org_boundary; 
    303                         // cut end boundary from body 
    304                         $body = substr($body, 0, strrpos($body, "--$att_boundary--")); 
    305                     } 
    306                     else { 
    307                         $att_boundary = strtoupper(md5(uniqid(time()))); 
    308                         // add boundary headers 
    309                         $headers .= "\n" . "Content-Type: multipart/mixed; boundary=$att_boundary"; 
    310                         $multipartmixed = true; 
    311                     } 
    312  
    313                     foreach($mess2->parts as $part) { 
    314                         if(isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { 
    315  
    316                             if(isset($part->d_parameters['filename'])) 
    317                                 $attname = $part->d_parameters['filename']; 
    318                             else if(isset($part->ctype_parameters['name'])) 
    319                                 $attname = $part->ctype_parameters['name']; 
    320                             else if(isset($part->headers['content-description'])) 
    321                                 $attname = $part->headers['content-description']; 
    322                             else $attname = "unknown attachment"; 
    323  
    324                             // ignore html content 
    325                             if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { 
    326                                 continue; 
    327                             } 
    328                             // 
    329                             if ($use_orgbody || $attached) { 
    330                                 $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); 
    331                             } 
    332                             // first attachment 
    333                             else { 
    334                                 $encmail = $body; 
    335                                 $attached = true; 
    336                                 $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); 
    337                                 $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); 
    338                             } 
    339                         } 
    340                     } 
    341                     if ($multipartmixed) { 
    342                         //this happens if a multipart/alternative message is forwarded 
    343                         //then it's a multipart/mixed message which consists of: 
    344                         //1. text/plain part which was written on the mobile 
    345                         //2. multipart/alternative part which is the original message 
    346                         //$body = "This is a message with multiple parts in MIME format.\n--". 
    347                         //        $att_boundary. 
    348                         //        "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". 
    349                         //        (($body_base64) ? chunk_split(base64_encode($message->body)) : rtrim($message->body)). 
    350                         //        "\n--".$att_boundary. 
    351                         //        "\nContent-Type: {$mess2->headers['content-type']}\n\n". 
    352                         //        @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID)."\n\n"; 
    353                         $body = "\n--". 
    354                                 $att_boundary. 
    355                                 "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". 
    356                                 $body; 
    357                     } 
    358  
    359                     $body .= "--$att_boundary--\n\n"; 
    360                 } 
    361  
    362                 unset($mobj2); 
    363             } 
    364  
    365             // unset origmail - free memory 
    366             unset($origmail); 
    367  
    368         } 
    369  
    370         // remove carriage-returns from body 
    371         $body = str_replace("\r\n", "\n", $body); 
    372  
    373         if (!$multipartmixed) { 
    374             if (!empty($forward_h_ct)) $headers .= "\nContent-Type: $forward_h_ct"; 
    375             if (!empty($forward_h_cte)) $headers .= "\nContent-Transfer-Encoding: $forward_h_cte"; 
    376         } 
    377         //advanced debugging 
    378         debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); 
    379         debugLog("IMAP-SendMail: headers: $headers"); 
    380         debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); 
    381         debugLog("IMAP-SendMail: body: $body"); 
    382  
    383         if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { 
    384             $send =  @imap_mail ( $toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); 
    385         } 
    386         else { 
    387             if (!empty($ccaddr))  $headers .= "\nCc: $ccaddr"; 
    388             if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; 
    389             $send =  @mail ( $toaddr, $message->headers["subject"], $body, $headers, $envelopefrom ); 
    390         } 
    391  
    392         // email sent? 
    393         if (!$send) { 
    394             debugLog("The email could not be sent. Last-IMAP-error: ". imap_last_error()); 
    395         } 
    396  
    397         // add message to the sent folder 
    398         // build complete headers 
    399         $headers .= "\nTo: $toaddr"; 
    400         $headers .= "\nSubject: " . $message->headers["subject"]; 
    401  
    402         if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { 
    403             if (!empty($ccaddr))  $headers .= "\nCc: $ccaddr"; 
    404             if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; 
    405         } 
    406         debugLog("IMAP-SendMail: complete headers: $headers"); 
    407  
    408         $asf = false; 
    409         if ($this->_sentID) { 
    410             $asf = $this->addSentMessage($this->_sentID, $headers, $body); 
    411         } 
    412         else if (IMAP_SENTFOLDER) { 
    413             $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); 
    414             debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '".IMAP_SENTFOLDER."': ". (($asf)?"success":"failed")); 
    415         } 
    416         // No Sent folder set, try defaults 
    417         else { 
    418             debugLog("IMAP-SendMail: No Sent mailbox set"); 
    419             if($this->addSentMessage("INBOX.Sent", $headers, $body)) { 
    420                 debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); 
    421                 $asf = true; 
    422             } 
    423             else if ($this->addSentMessage("Sent", $headers, $body)) { 
    424                 debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); 
    425                 $asf = true; 
    426             } 
    427             else if ($this->addSentMessage("Sent Items", $headers, $body)) { 
    428                 debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); 
    429                 $asf = true; 
    430             } 
    431         } 
    432  
    433         // unset mimedecoder - free memory 
    434         unset($mobj); 
    435         return ($send && $asf); 
    436     } 
    437          
     93        function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { 
     94                debugLog("IMAP-SendMail: for: $forward   reply: $reply   parent: $parent  RFC822:  \n". $rfc822 ); 
     95 
     96                $mobj = new Mail_mimeDecode($rfc822); 
     97                $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); 
     98 
     99                $Mail_RFC822 = new Mail_RFC822(); 
     100                $toaddr = $ccaddr = $bccaddr = ""; 
     101                if(isset($message->headers["to"])) 
     102                        $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); 
     103                if(isset($message->headers["cc"])) 
     104                        $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); 
     105                if(isset($message->headers["bcc"])) 
     106                        $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); 
     107 
     108                // save some headers when forwarding mails (content type & transfer-encoding) 
     109                $headers = ""; 
     110                $forward_h_ct = ""; 
     111                $forward_h_cte = ""; 
     112                $envelopefrom = ""; 
     113 
     114                $use_orgbody = false; 
     115 
     116                // clean up the transmitted headers 
     117                // remove default headers because we are using imap_mail 
     118                $changedfrom = false; 
     119                $returnPathSet = false; 
     120                $body_base64 = false; 
     121                $org_charset = ""; 
     122                $org_boundary = false; 
     123                $multipartmixed = false; 
     124                 
     125                foreach($message->headers as $k => $v) { 
     126                        if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") 
     127                                continue; 
     128 
     129                        if ($k == "content-type") { 
     130                                // if the message is a multipart message, then we should use the sent body 
     131                                if (preg_match("/multipart/i", $v)) { 
     132                                        $use_orgbody = true; 
     133                                        $org_boundary = $message->ctype_parameters["boundary"]; 
     134                                } 
     135 
     136                                // save the original content-type header for the body part when forwarding 
     137                                if ($forward && !$use_orgbody) { 
     138                                        $forward_h_ct = $v; 
     139                                        continue; 
     140                                } 
     141 
     142                                // set charset always to utf-8 
     143                                $org_charset = $v; 
     144                                $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); 
     145                        } 
     146 
     147                        if ($k == "content-transfer-encoding") { 
     148                                // if the content was base64 encoded, encode the body again when sending 
     149                                if (trim($v) == "base64") $body_base64 = true; 
     150 
     151                                // save the original encoding header for the body part when forwarding 
     152                                if ($forward) { 
     153                                        $forward_h_cte = $v; 
     154                                        continue; 
     155                                } 
     156                        } 
     157 
     158                        // check if "from"-header is set, do nothing if it's set 
     159                        // else set it to IMAP_DEFAULTFROM 
     160                        if ($k == "from") { 
     161                                if (trim($v)) { 
     162                                        $changedfrom = true; 
     163                                } elseif (! trim($v) && IMAP_DEFAULTFROM) { 
     164                                        $changedfrom = true; 
     165                                        if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     166                                        else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     167                                        else $v = $this->_username . IMAP_DEFAULTFROM; 
     168                                        $envelopefrom = "-f$v"; 
     169                                } 
     170                        } 
     171 
     172                        // check if "Return-Path"-header is set 
     173                        if ($k == "return-path") { 
     174                                $returnPathSet = true; 
     175                                if (! trim($v) && IMAP_DEFAULTFROM) { 
     176                                        if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     177                                        else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     178                                        else $v = $this->_username . IMAP_DEFAULTFROM; 
     179                                } 
     180                        } 
     181 
     182                        // all other headers stay 
     183                        if ($headers) $headers .= "\n"; 
     184                        $headers .= ucfirst($k) . ": ". $v; 
     185                } 
     186 
     187                // set "From" header if not set on the device 
     188                if(IMAP_DEFAULTFROM && !$changedfrom){ 
     189                        if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     190                        else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     191                        else $v = $this->_username . IMAP_DEFAULTFROM; 
     192                        if ($headers) $headers .= "\n"; 
     193                        $headers .= 'From: '.$v; 
     194                        $envelopefrom = "-f$v"; 
     195                } 
     196 
     197                // set "Return-Path" header if not set on the device 
     198                if(IMAP_DEFAULTFROM && !$returnPathSet){ 
     199                        if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     200                        else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     201                        else $v = $this->_username . IMAP_DEFAULTFROM; 
     202                        if ($headers) $headers .= "\n"; 
     203                        $headers .= 'Return-Path: '.$v; 
     204                } 
     205 
     206                // if this is a multipart message with a boundary, we must use the original body 
     207                if ($use_orgbody) { 
     208                        list(,$body) = $mobj->_splitBodyHeader($rfc822); 
     209                        $repl_body = $this->getBody($message);  
     210                        if ($message->parts[0]->headers["content-transfer-encoding"] == "base64") $multipart_text_cte_base64 = true; 
     211                        else $multipart_text_cte_base64 = false; 
     212                } 
     213                else 
     214                        $body = $this->getBody($message); 
     215 
     216                // reply 
     217                if ($reply && $parent) { 
     218                        $this->imap_reopenFolder($parent); 
     219                        // receive entire mail (header + body) to decode body correctly 
     220                        $origmail = @imap_fetchheader($this->_mbox, $reply, FT_UID) . @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); 
     221 
     222                        $mobj2 = new Mail_mimeDecode($origmail); 
     223                        // receive only body 
     224                        $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8'))); 
     225                        // unset mimedecoder & origmail - free memory 
     226                        unset($mobj2); 
     227                        unset($origmail); 
     228                } 
     229 
     230                // encode the body to base64 if it was sent originally in base64 by the pda 
     231                // contrib - chunk base64 encoded body 
     232                if ($body_base64 && !$forward) $body = chunk_split(base64_encode($body)); 
     233 
     234 
     235                // forward 
     236                if ($forward && $parent) { 
     237                        $this->imap_reopenFolder($parent); 
     238                        // receive entire mail (header + body) 
     239                        $origmail = @imap_fetchheader($this->_mbox, $forward, FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); 
     240 
     241                                if (defined('IMAP_INLINE_FORWARD') && IMAP_INLINE_FORWARD === false) { 
     242                                        // contrib - chunk base64 encoded body 
     243                                        if ($body_base64) $body = chunk_split(base64_encode($body)); 
     244                                        //use original boundary if it's set 
     245                                        $boundary = ($org_boundary) ? $org_boundary : false; 
     246                                        // build a new mime message, forward entire old mail as file 
     247                                        list($aheader, $body) = $this->mail_attach("forwarded_message.eml",strlen($origmail),$origmail, $body, $forward_h_ct, $forward_h_cte,$boundary); 
     248                                        // add boundary headers 
     249                                        $headers .= "\n" . $aheader; 
     250                                }  
     251                                else { 
     252                                        $mobj2 = new Mail_mimeDecode($origmail); 
     253                                        $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); 
     254 
     255                                        if (!$use_orgbody) 
     256                                                $nbody = $body; 
     257                                        else 
     258                                                $nbody = $repl_body; 
     259 
     260                                        $nbody .= "\r\n\r\n"; 
     261                                        $nbody .= "-----Original Message-----\r\n"; 
     262                                        if(isset($mess2->headers['from'])) 
     263                                                $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; 
     264                                        if(isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) 
     265                                                $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; 
     266                                        if(isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) 
     267                                                $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; 
     268                                        if(isset($mess2->headers['date'])) 
     269                                                $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; 
     270                                        if(isset($mess2->headers['subject'])) 
     271                                                $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; 
     272                                         
     273                                        $nbody .= "\r\n"; 
     274                                        $nbody .= $this->getBody($mess2); 
     275 
     276                                        if ($body_base64) { 
     277                                                // contrib - chunk base64 encoded body 
     278                                                $nbody = chunk_split(base64_encode($nbody)); 
     279                                                if ($use_orgbody) 
     280                                                        // contrib - chunk base64 encoded body 
     281                                                        $repl_body = chunk_split(base64_encode($repl_body)); 
     282                                        } 
     283 
     284                                        if ($use_orgbody) { 
     285                                                debugLog("-------------------"); 
     286                                                debugLog("old:\n'$repl_body'\nnew:\n'$nbody'\nund der body:\n'$body'"); 
     287                                                //$body is quoted-printable encoded while $repl_body and $nbody are plain text, 
     288                                                //so we need to decode $body in order replace to take place 
     289                                                if (!$multipart_text_cte_base64) { 
     290                                                        $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); 
     291                                                } else { 
     292                                                        $body = str_replace(base64_encode($repl_body), base64_encode($nbody), $body);                     
     293                                                } 
     294                                        } 
     295                                        else 
     296                                                $body = $nbody; 
     297 
     298 
     299                                        if(isset($mess2->parts)) { 
     300                                                $attached = false; 
     301 
     302                                                if ($org_boundary) { 
     303                                                        $att_boundary = $org_boundary; 
     304                                                        // cut end boundary from body 
     305                                                        $body = substr($body, 0, strrpos($body, "--$att_boundary--")); 
     306                                                } 
     307                                                else { 
     308                                                        $att_boundary = strtoupper(md5(uniqid(time()))); 
     309                                                        // add boundary headers 
     310                                                        $headers .= "\n" . "Content-Type: multipart/mixed; boundary=$att_boundary"; 
     311                                                        $multipartmixed = true; 
     312                                                } 
     313 
     314                                                foreach($mess2->parts as $part) { 
     315                                                        if(isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { 
     316 
     317                                                        if(isset($part->d_parameters['filename'])) 
     318                                                                $attname = $part->d_parameters['filename']; 
     319                                                        else if(isset($part->ctype_parameters['name'])) 
     320                                                                $attname = $part->ctype_parameters['name']; 
     321                                                        else if(isset($part->headers['content-description'])) 
     322                                                                $attname = $part->headers['content-description']; 
     323                                                        else $attname = "unknown attachment"; 
     324 
     325                                                        // ignore html content 
     326                                                        if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { 
     327                                                                continue; 
     328                                                        } 
     329                                                        // 
     330                                                        if ($use_orgbody || $attached) { 
     331                                                                $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); 
     332                                                        } 
     333                                                        // first attachment 
     334                                                        else { 
     335                                                                $encmail = $body; 
     336                                                                $attached = true; 
     337                                                                $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); 
     338                                                                $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); 
     339                                                        } 
     340                                                } 
     341                                        } 
     342                                        if ($multipartmixed) { 
     343                                                //this happens if a multipart/alternative message is forwarded 
     344                                                //then it's a multipart/mixed message which consists of: 
     345                                                //1. text/plain part which was written on the mobile 
     346                                                //2. multipart/alternative part which is the original message 
     347                                                //$body = "This is a message with multiple parts in MIME format.\n--". 
     348                                                //        $att_boundary. 
     349                                                //        "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". 
     350                                                //        (($body_base64) ? chunk_split(base64_encode($message->body)) : rtrim($message->body)). 
     351                                                //        "\n--".$att_boundary. 
     352                                                //        "\nContent-Type: {$mess2->headers['content-type']}\n\n". 
     353                                                //        @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID)."\n\n"; 
     354                                                $body = "\n--". 
     355                                                        $att_boundary. 
     356                                                        "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". 
     357                                                        $body; 
     358                                        } 
     359 
     360                                        $body .= "--$att_boundary--\n\n"; 
     361                                } 
     362 
     363                                unset($mobj2); 
     364                        } 
     365 
     366                        // unset origmail - free memory 
     367                        unset($origmail); 
     368 
     369                } 
     370 
     371                // remove carriage-returns from body 
     372                $body = str_replace("\r\n", "\n", $body); 
     373 
     374                if (!$multipartmixed) { 
     375                        if (!empty($forward_h_ct)) $headers .= "\nContent-Type: $forward_h_ct"; 
     376                        if (!empty($forward_h_cte)) $headers .= "\nContent-Transfer-Encoding: $forward_h_cte"; 
     377                } 
     378                //advanced debugging 
     379                debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); 
     380                debugLog("IMAP-SendMail: headers: $headers"); 
     381                debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); 
     382                debugLog("IMAP-SendMail: body: $body"); 
     383 
     384                if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { 
     385                        $send =  @imap_mail ( $toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); 
     386                } 
     387                else { 
     388                        if (!empty($ccaddr))  $headers .= "\nCc: $ccaddr"; 
     389                        if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; 
     390                        $send =  @mail ( $toaddr, $message->headers["subject"], $body, $headers, $envelopefrom ); 
     391                } 
     392 
     393                // email sent? 
     394                if (!$send) { 
     395                        debugLog("The email could not be sent. Last-IMAP-error: ". imap_last_error()); 
     396                } 
     397 
     398                // add message to the sent folder 
     399                // build complete headers 
     400                $headers .= "\nTo: $toaddr"; 
     401                $headers .= "\nSubject: " . $message->headers["subject"]; 
     402 
     403                if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { 
     404                        if (!empty($ccaddr))  $headers .= "\nCc: $ccaddr"; 
     405                        if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; 
     406                } 
     407                debugLog("IMAP-SendMail: complete headers: $headers"); 
     408 
     409                $asf = false; 
     410                if ($this->_sentID) { 
     411                        $asf = $this->addSentMessage($this->_sentID, $headers, $body); 
     412                } 
     413                else if(defined("IMAP_SENTFOLDER")) { 
     414                        $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); 
     415                        debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '".IMAP_SENTFOLDER."': ". (($asf)?"success":"failed")); 
     416                } 
     417                // No Sent folder set, try defaults 
     418                else { 
     419                        debugLog("IMAP-SendMail: No Sent mailbox set"); 
     420                        if($this->addSentMessage("INBOX.Sent", $headers, $body)) { 
     421                                debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); 
     422                                $asf = true; 
     423                        } 
     424                        else if ($this->addSentMessage("Sent", $headers, $body)) { 
     425                                debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); 
     426                                $asf = true; 
     427                        } 
     428                        else if ($this->addSentMessage("Sent Items", $headers, $body)) { 
     429                                debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); 
     430                                $asf = true; 
     431                        } 
     432                } 
     433 
     434                // unset mimedecoder - free memory 
     435                unset($mobj); 
     436                return ($send && $asf); 
     437        } 
    438438 
    439439        /* Should return a wastebasket folder if there is one. This is used when deleting 
     
    468468                        if ($search !== false) 
    469469                        $sequence = implode(",", $search); 
    470                         if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> A função @imap_search leu as seguintes SEQUÊNCIAS do servidor IMAP com base no filtro de data: '.$sequence); 
     470                        if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> A função @imap_search leu as seguintes SEQUÊNCIAS do servidor IMAP com base no filtro de data: '.$sequence); 
    471471                } 
    472472 
     
    479479                                $date = ""; 
    480480                                $vars = get_object_vars($overview); 
    481                                 if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> A função @imap_fetch_overview leu mais detalhes da mensagem: '. print_r($overview,1)); 
     481                                if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> A função @imap_fetch_overview leu mais detalhes da mensagem: '. print_r($overview,1)); 
    482482                                if (array_key_exists( "date", $vars)) { 
    483483                                        // message is out of range for cutoffdate, ignore it 
    484484                                        if(strtotime(preg_replace("/\(.*\)/", "", $overview->date)) < $cutoffdate) { // emerson-faria.nobre@serpro.gov.br - 07/feb/2011 
    485                                                 if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> O overview->date: '.$overview->date.' (overview->date_timestamp: '.strtotime(preg_replace("/\(.*\)/", "", $overview->date)).') é menor que o $cutoffdate: '.date("d/m/Y G:i:s",$cutoffdate).' ($cutoffdate_timestamp: '.$cutoffdate.') ou a função "strtotime" gerou um ERRO porque não conseguiu retornar um valor para o overview->date_timestamp. A mensagem será descartada da sincronização.'); 
     485                                                if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> O overview->date: '.$overview->date.' (overview->date_timestamp: '.strtotime(preg_replace("/\(.*\)/", "", $overview->date)).') é menor que o $cutoffdate: '.date("d/m/Y G:i:s",$cutoffdate).' ($cutoffdate_timestamp: '.$cutoffdate.') ou a função "strtotime" gerou um ERRO porque não conseguiu retornar um valor para o overview->date_timestamp. A mensagem será descartada da sincronização.'); 
    486486                                                continue; 
    487487                                        } 
    488488                                        //if(strtotime($overview->date) < $cutoffdate) continue; 
    489489                                        $date = $overview->date; 
    490                                 } else if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> ERRO: O campo date não existe no overview da mensagem.'); 
     490                                } else if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> ERRO: O campo date não existe no overview da mensagem.'); 
    491491 
    492492                                // cut of deleted messages 
    493493                                if (array_key_exists( "deleted", $vars) && $overview->deleted) { 
    494                                         if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> A mensagem está com o flag deleted ativo e será descartada da sincronização'); 
    495                                         continue; 
     494                                        if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> A mensagem está com o flag deleted ativo e será descartada da sincronização'); 
     495                                continue; 
    496496                                } 
    497497 
     
    506506                                        $message["flags"] = 1; 
    507507                                        array_push($messages, $message); 
    508                                 } else if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> ERRO: O campo uid não existe no overview da mensagem.'); 
    509                         } 
    510                 } 
     508                                } else if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetMessageList-> ERRO: O campo uid não existe no overview da mensagem.'); 
     509                                } 
     510                        } 
    511511                return $messages; 
    512512        } 
     
    523523                        $list = array_reverse($list); 
    524524                        foreach ($list as $val) { 
    525                                 if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetFolderList-> Pasta lida usando a função @imap_getmailboxes: '.print_r($val,1)); 
     525                                if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetFolderList-> Pasta lida usando a função @imap_getmailboxes: '.print_r($val,1)); 
    526526                                $box = array(); 
    527527 
     
    542542                                        $box["parent"] = "0"; 
    543543                                } 
    544                                 if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetFolderList-> Parâmetros da pasta após decodificação: '.print_r($box,1)); 
     544                                if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog('IMAP::GetFolderList-> Parâmetros da pasta após decodificação: '.print_r($box,1)); 
    545545                                $folders[]=$box; 
    546546                        } 
     
    699699                if (isset($message->parts[$part]->body)) 
    700700                print $message->parts[$part]->body; 
    701                  
     701 
    702702                // unset mimedecoder & mail 
    703703                unset($mobj); 
     
    729729                        $vars = get_object_vars($overview[0]); 
    730730 
    731                         if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL') and (! array_key_exists( "uid", $vars))) debugLog('IMAP::StatMessage-> ERRO: A mensagem será desconsiderada porque não tem o campo "uid"'); 
     731                        if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL') and (! array_key_exists( "uid", $vars))) debugLog('IMAP::StatMessage-> ERRO: A mensagem será desconsiderada porque não tem o campo "uid"'); 
    732732                        // without uid it's not a valid message 
    733733                        if (! array_key_exists( "uid", $vars)) return false; 
     
    768768                        $mail = @imap_fetchheader($this->_mbox, $id, FT_PREFETCHTEXT | FT_UID) . @imap_body($this->_mbox, $id, FT_PEEK | FT_UID); 
    769769 
    770                         if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog("IMAP-GetMessage: Mensagem lida do servidor IMAP através da função @imap_fetchheader: ". print_r($mail,1)); 
     770                        if (TRACE_UID !== false and (TRACE_TYPE == 'IMAP' or TRACE_TYPE == 'ALL')) traceLog("IMAP-GetMessage: Mensagem lida do servidor IMAP através da função @imap_fetchheader: ". print_r($mail,1)); 
    771771 
    772772                        $mobj = new Mail_mimeDecode($mail); 
     
    999999                        // Remove the HTML tags using the 'html2text' - emerson-faria.nobre@serpro.gov.br 
    10001000                        // The 'html2text' (http://www.mbayer.de/html2text) must be installed in Z-Push server. 
    1001  
     1001                                 
    10021002                        // Advanced debug 
    10031003                        // debugLog("IMAP-getBody: subject: " . $message->headers["subject"]); 
    1004                         $body = utf8_encode($body); 
     1004                        $body = utf8_encode($body);      
    10051005                        libxml_use_internal_errors(true); 
    10061006                        try { 
     
    10541054 
    10551055                if(strcasecmp($message->ctype_primary,"multipart")==0 && isset($message->parts) && is_array($message->parts)) { 
    1056                 foreach($message->parts as $part) { 
     1056                        foreach($message->parts as $part) { 
    10571057                                if(!isset($part->disposition) || strcasecmp($part->disposition,"attachment"))  { 
    10581058                                        $this->getBodyRecursive($part, $subtype, $body); 
     
    10861086 
    10871087        // build a multipart email, embedding body and one file (for attachments) 
    1088    function mail_attach($filenm,$filesize,$file_cont,$body, $body_ct, $body_cte, $boundary = false) { 
    1089         if (!$boundary) $boundary = strtoupper(md5(uniqid(time()))); 
    1090  
    1091         //remove the ending boundary because we will add it at the end 
    1092         $body = str_replace("--$boundary--", "", $body); 
    1093  
    1094         $mail_header = "Content-Type: multipart/mixed; boundary=$boundary\n"; 
    1095  
    1096         // build main body with the sumitted type & encoding from the pda 
    1097         $mail_body  = $this->enc_multipart($boundary, $body, $body_ct, $body_cte); 
    1098         $mail_body .= $this->enc_attach_file($boundary, $filenm, $filesize, $file_cont); 
    1099  
    1100         $mail_body .= "--$boundary--\n\n"; 
    1101         return array($mail_header, $mail_body); 
    1102     } 
    1103  
    1104     function enc_multipart($boundary, $body, $body_ct, $body_cte) { 
     1088        function mail_attach($filenm,$filesize,$file_cont,$body, $body_ct, $body_cte, $boundary = false) { 
     1089                if (!$boundary) $boundary = strtoupper(md5(uniqid(time()))); 
     1090 
     1091                //remove the ending boundary because we will add it at the end 
     1092                $body = str_replace("--$boundary--", "", $body); 
     1093 
     1094                $mail_header = "Content-Type: multipart/mixed; boundary=$boundary\n"; 
     1095 
     1096                // build main body with the sumitted type & encoding from the pda 
     1097                $mail_body  = $this->enc_multipart($boundary, $body, $body_ct, $body_cte); 
     1098                $mail_body .= $this->enc_attach_file($boundary, $filenm, $filesize, $file_cont); 
     1099 
     1100                $mail_body .= "--$boundary--\n\n"; 
     1101                 
     1102                return array($mail_header, $mail_body); 
     1103        } 
     1104 
     1105        function enc_multipart($boundary, $body, $body_ct, $body_cte) { 
    11051106//        $mail_body = "This is a multi-part message in MIME format\n\n";    
    11061107//        $mail_body .= "--$boundary\n"; 
    11071108//        $mail_body .= "Content-Type: $body_ct\n"; 
    11081109//        $mail_body .= "Content-Transfer-Encoding: $body_cte\n\n"; 
    1109         $mail_body = "$body\n\n"; 
    1110  
    1111         return $mail_body; 
    1112     } 
     1110        $mail_body = "$body\n\n"; 
     1111 
     1112        return $mail_body; 
     1113        } 
    11131114     
    1114     function enc_attach_file($boundary, $filenm, $filesize, $file_cont, $content_type = "") { 
    1115         if (!$content_type) $content_type = "text/plain"; 
    1116         $mail_body = "--$boundary\n"; 
    1117         $mail_body .= "Content-Type: $content_type; name=\"$filenm\"\n"; 
    1118         $mail_body .= "Content-Transfer-Encoding: base64\n"; 
    1119         $mail_body .= "Content-Disposition: attachment; filename=\"$filenm\"\n"; 
    1120         $mail_body .= "Content-Description: $filenm\n\n"; 
    1121         //contrib - chunk base64 encoded attachments 
    1122         $mail_body .= chunk_split(base64_encode($file_cont)) . "\n\n"; 
    1123  
    1124         return $mail_body; 
    1125     } 
    1126          
     1115        function enc_attach_file($boundary, $filenm, $filesize, $file_cont, $content_type = "") { 
     1116                if (!$content_type) $content_type = "text/plain"; 
     1117                $mail_body = "--$boundary\n"; 
     1118                $mail_body .= "Content-Type: $content_type; name=\"$filenm\"\n"; 
     1119                $mail_body .= "Content-Transfer-Encoding: base64\n"; 
     1120                $mail_body .= "Content-Disposition: attachment; filename=\"$filenm\"\n"; 
     1121                $mail_body .= "Content-Description: $filenm\n\n"; 
     1122                //contrib - chunk base64 encoded attachments 
     1123                $mail_body .= chunk_split(base64_encode($file_cont)) . "\n\n"; 
     1124 
     1125                return $mail_body; 
     1126        } 
     1127 
    11271128        // adds a message as seen to a specified folder (used for saving sent mails) 
    11281129        function addSentMessage($folderid, $header, $body) { 
Note: See TracChangeset for help on using the changeset viewer.