Ignore:
Timestamp:
10/11/12 18:14:36 (12 years ago)
Author:
marcosw
Message:

Ticket #3088 - Realização de merge da melhoria do MailArchiver?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/expressoMail1_2/inc/class.imap_functions.inc.php

    r7413 r7414  
    31633163                $mailService->addHeaderField('Disposition-Notification-To', $_SESSION['phpgw_info']['expressomail']['user']['email']); 
    31643164 
     3165            $mailService->addHeaderField('Date', date("r")); 
    31653166 
    31663167            if ($folder != 'null') { 
     
    46604661 
    46614662    function insert_email($source,$folder,$timestamp,$flags){ 
    4662                  
     4663 
    46634664        $username = $_SESSION['phpgw_info']['expressomail']['user']['userid']; 
    46644665        $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; 
     
    46674668        $imap_options = '/notls/novalidate-cert'; 
    46684669 
    4669          
    46704670        $folder = mb_convert_encoding( $folder, "UTF7-IMAP","ISO-8859-1"); 
    4671  
    46724671        $mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$folder, $username, $password); 
    46734672         
    46744673        if(imap_last_error() === 'Mailbox already exists') 
    46754674            imap_createmailbox($mbox_stream,imap_utf7_encode("{".$imap_server."}".$folder)); 
     4675 
    46764676        if($timestamp){ 
    4677                         $pdate = date_parse(date('r')); // pega a data atual do servidor (TODO: pegar a data da mensagem local)  
    4678                         $timestamp += $pdate['zone']*(60); //converte a data da mensagem para o fuso horário GMT 0. Isto é feito devido ao Expresso Mail armazenar a data no fuso horário GMT 0 e para exibi-la converte ela para o fuso horário local.  
    4679                         /* TODO: o diretorio /tmp deve ser substituido pelo diretorio temporario configurado no setup */  
    4680                         $file = "/tmp/sess_".$_SESSION[ 'phpgw_session' ][ 'session_id' ];  
    4681                  
    4682                 $f = fopen($file,"w"); 
    4683                 fputs($f,base64_encode($source)); 
    4684             fclose($f); 
    4685             $command = "python ".dirname(__FILE__)."/../imap.py \"$imap_server\" \"$imap_port\" \"$username\" \"$password\" \"$timestamp\" \"$folder\" \"$file\""; 
    4686             $return['command']= exec($command); 
     4677            if(version_compare(PHP_VERSION, '5.3.2', '>=')){ 
     4678                $return['append'] = imap_append($mbox_stream, "{".$imap_server.":".$imap_port."}".mb_convert_encoding($folder, "UTF7-IMAP","ISO_8859-1"), $source,'',date('d-M-Y H:i:s O',$timestamp)); 
     4679            }else{ 
     4680                                $pdate = date_parse(date('r')); // pega a data atual do servidor (TODO: pegar a data da mensagem local)  
     4681                                $timestamp += $pdate['zone']*(60); //converte a data da mensagem para o fuso horário GMT 0. Isto é feito devido ao Expresso Mail armazenar a data no fuso horário GMT 0 e para exibi-la converte ela para o fuso horário local.  
     4682                                /* TODO: o diretorio /tmp deve ser substituido pelo diretorio temporario configurado no setup */  
     4683                                $file = "/tmp/sess_".$_SESSION[ 'phpgw_session' ][ 'session_id' ];  
     4684                         
     4685                        $f = fopen($file,"w"); 
     4686                        fputs($f,base64_encode($source)); 
     4687                    fclose($f); 
     4688                    $command = "python ".dirname(__FILE__)."/../imap.py \"$imap_server\" \"$imap_port\" \"$username\" \"$password\" \"$timestamp\" \"$folder\" \"$file\""; 
     4689                    $return['command']= exec($command); 
     4690                } 
    46874691        }else{ 
    46884692            $return['append'] = imap_append($mbox_stream, "{".$imap_server.":".$imap_port."}".$folder, $source, "\\Seen"); 
    46894693        } 
     4694 
     4695        if (!empty($return['command'])) 
     4696        { 
     4697            list ($result, $msg) = explode(':',$return['command']); 
     4698            if (strtoupper($result) === 'NO') 
     4699            { 
     4700                $return['error'] = $msg; 
     4701                return $return; 
     4702            } 
     4703        } 
     4704 
    46904705        $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT); 
    46914706                         
    46924707        $return['msg_no'] = $status->uidnext - 1; 
    4693         $return['error'] = imap_last_error(); 
    4694         if(!$return['error'] && $flags != '' ){ 
    4695  
    4696                   $flags_array=explode(':',$flags); 
    4697                   //"Answered","Draft","Flagged","Unseen" 
    4698                   $flags_fixed = ""; 
    4699                   if($flags_array[0] == 'A') 
    4700                         $flags_fixed.="\\Answered "; 
    4701                   if($flags_array[1] == 'X') 
    4702                         $flags_fixed.="\\Draft "; 
    4703                   if($flags_array[2] == 'F') 
    4704                         $flags_fixed.="\\Flagged "; 
    4705                   if($flags_array[3] != 'U') 
    4706                         $flags_fixed.="\\Seen "; 
    4707                   if($flags_array[4] == 'F') 
    4708                         $flags_fixed.="\\Answered \\Draft "; 
    4709                   imap_setflag_full($mbox_stream, $return['msg_no'], $flags_fixed, ST_UID); 
    4710                 } 
     4708                 
     4709        $return['error'] = ''; 
     4710                if(imap_last_error() && imap_last_error() != "SECURITY PROBLEM: insecure server advertised AUTH=PLAIN") 
     4711            $return['error'] = imap_last_error(); 
     4712 
     4713                if(!$return['error'] && $flags != '' ){ 
     4714                        $flags_array=explode(':',$flags); 
     4715                        //"Answered","Draft","Flagged","Unseen" 
     4716                        $flags_fixed = ""; 
     4717                        if($flags_array[0] == 'A') 
     4718                            $flags_fixed.="\\Answered "; 
     4719                        if($flags_array[1] == 'X') 
     4720                            $flags_fixed.="\\Draft "; 
     4721                        if($flags_array[2] == 'F') 
     4722                            $flags_fixed.="\\Flagged "; 
     4723                        if($flags_array[3] != 'U') 
     4724                            $flags_fixed.="\\Seen "; 
     4725                        if($flags_array[4] == 'F') 
     4726                            $flags_fixed.="\\Answered \\Draft "; 
     4727                        imap_setflag_full($mbox_stream, $return['msg_no'], $flags_fixed, ST_UID); 
     4728                } 
    47114729         
    47124730        //Ignorando erro de AUTH=Plain 
     
    47734791 
    47744792    function treat_base64_from_post($source){ 
    4775             $offset = 0; 
    4776             do 
     4793        $source = preg_replace('/(?<!\r)\n/', "\r\n", $source); 
     4794 
     4795        $offset = 0; 
     4796        do 
     4797        { 
     4798            if($inicio = strpos($source, 'Content-Transfer-Encoding: base64', $offset)) 
    47774799            { 
    4778                     if($inicio = strpos($source, 'Content-Transfer-Encoding: base64', $offset)) 
    4779                     { 
    4780                             $inicio = strpos($source, "\n\r", $inicio); 
    4781                             $fim = strpos($source, '--', $inicio); 
    4782                             if(!$fim) 
    4783                                     $fim = strpos($source,"\n\r", $inicio); 
    4784                             $length = $fim-$inicio; 
    4785                             $parte = substr( $source,$inicio,$length-1); 
    4786                             $parte = str_replace(" ", "+", $parte); 
    4787                             $source = substr_replace($source, $parte, $inicio, $length-1); 
    4788                     } 
    4789                     if($offset > $inicio) 
    4790                     $offset=FALSE; 
    4791                     else 
    4792                     $offset = $inicio; 
     4800                    $inicio = strpos($source, "\n\r", $inicio); 
     4801                    $fim = strpos($source, '--', $inicio); 
     4802                    if(!$fim) 
     4803                            $fim = strpos($source,"\n\r", $inicio); 
     4804                    $length = $fim-$inicio; 
     4805                    $parte = substr( $source,$inicio,$length-1); 
     4806                    $parte = str_replace(" ", "+", $parte); 
     4807                    $source = substr_replace($source, $parte, $inicio, $length-1); 
    47934808            } 
    4794             while($offset); 
    4795             return $source; 
     4809            if($offset > $inicio) 
     4810            $offset=FALSE; 
     4811            else 
     4812            $offset = $inicio; 
     4813        } 
     4814        while($offset); 
     4815        return $source; 
    47964816    } 
    47974817 
    4798 //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Recebe os fontes dos emails a serem desarquivados, separa e envia cada um para função insert_mail. 
     4818        //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Recebe os fontes dos emails a serem desarquivados, separa e envia cada um para função insert_mail. 
    47994819 
    48004820    function unarchive_mail($params) 
    4801     {            
    4802         $dest_folder = $params['folder']; 
     4821    {    
     4822        $dest_folder = urldecode($params['folder']); 
    48034823        $sources = explode("#@#@#@",$params['source']); 
    4804         //Add user timeszone 
    48054824        $timestamps = explode("#@#@#@",$params['timestamp']); 
    4806  
    4807  
    48084825        $flags = explode("#@#@#@",$params['flags']); 
    4809                  
    4810                 foreach($sources as $index=>$src) { 
    4811                         if($src!=""){ 
    4812                 $source = $this->treat_base64_from_post($src); 
    4813                 $timestampsactual = $timestamps[$index] + $this->functions->CalculateDateOffset();  
    4814                         $insert = $this->insert_email($source, mb_convert_encoding( $dest_folder,"ISO-8859-1","UTF-8"), $timestampsactual,$flags[$index]);  
     4826        $ids = explode("#@#@#@",$params['id']); 
     4827        $return = array(); 
     4828        $archived = array(); 
     4829        $error = array(); 
     4830 
     4831        foreach($sources as $index=>$src) 
     4832        { 
     4833            if($src!="") 
     4834            { 
     4835                //If it is a MailArchiver incomming data 
     4836                if($params['madata']) 
     4837                    $sourcedec = utf8_decode($src); 
     4838                //Default data 
     4839                else 
     4840                    $sourcedec = $src; 
     4841 
     4842                                $source = $this->treat_base64_from_post($sourcedec); 
     4843 
     4844                                $insert = $this->insert_email($source,$dest_folder,$timestamps[$index],$flags[$index]); 
     4845 
     4846                if($insert['error']) 
     4847                { 
     4848                    $error[] = $ids[$index]; 
     4849                } 
     4850                else  
     4851                { 
     4852                    $archived[] = $ids[$index]; 
     4853                } 
     4854            }else{ 
     4855                $error[] = $ids[$index]; 
    48154856            } 
    4816         } 
    4817         return $insert; 
     4857                } 
     4858         
     4859        if (!empty($error)) 
     4860        { 
     4861            $return['error'] = $error; 
     4862        } 
     4863        if (!empty($archived)) 
     4864        { 
     4865            $return['archived'] = $archived; 
     4866        } 
     4867         
     4868        return $return; 
    48184869    } 
    48194870 
     
    54845535        } 
    54855536 
     5537        //MailArchiver -> get offsettogmt as a global javascript variable, invoked at "main.js", init() 
     5538    function get_offset_gmt(){ 
     5539        return($this->functions->CalculateDateOffset()); 
     5540    } 
     5541 
     5542    //MailArchiver -> get message flags only, invoked at archive operation 
     5543    function get_msg_flags($args){   
     5544        $msg_folder = $args['folder']; 
     5545        $msg_n = $args['msg_number']; 
     5546        $arr_msg = explode(",", $msg_n); 
     5547        
     5548        for($i=0; $i<count($arr_msg); $i++){ 
     5549                         
     5550            if(!$this->mbox || !is_resource($this->mbox)) 
     5551                $this->mbox = $this->open_mbox($msg_folder); 
     5552         
     5553            if(!is_resource($this->mbox)) 
     5554                return(false); 
     5555                        
     5556            $msgno_imap = imap_msgno($this->mbox, $msg_n);           
     5557                $header = @imap_headerinfo($this->mbox, $msgno_imap, 80, 255); 
     5558                 
     5559            if (!is_object($header)) 
     5560                return false; 
     5561 
     5562            $taglist[$i]["msgid"] = $msg_n; 
     5563            $taglist[$i]["unseen"] = $header->Unseen; 
     5564            $taglist[$i]["recent"] = $header->Recent; 
     5565            $taglist[$i]["flagged"] = $header->Flagged; 
     5566            $taglist[$i]["draft"] = $header->Draft; 
     5567            $taglist[$i]["answered"] = $header->Answered; 
     5568            $taglist[$i]["deleted"] = $header->Deleted; 
     5569         
     5570            if($header->Answered =='A' && $header->Draft == 'X') 
     5571                $taglist[$i]['forwarded'] = 'F'; 
     5572            else 
     5573                $taglist[$i]['forwarded'] = ' '; 
     5574        } 
     5575 
     5576                return $taglist;         
     5577    }     
    54865578} 
    54875579?> 
Note: See TracChangeset for help on using the changeset viewer.