Ignore:
Timestamp:
04/17/12 16:09:39 (12 years ago)
Author:
adriano
Message:

Ticket #2633 - alteracoes de backend e interceptadores para a funcionalidade de sinalizadores

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/prototype/services/ImapServiceAdapter.php

    r5826 r5959  
    22 
    33include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php"; 
    4 require_once ROOTPATH.'/library/uuid/class.uuid.php'; 
    5  
    64 
    75class ImapServiceAdapter extends imap_functions/* implements Service*/ 
     
    1917    public function find( $URI, $justthese = false, $criteria = false ) 
    2018        {  
    21                 //require_once dirname(__FILE__) . '/../api/controller.php'; 
    2219                $context = $justthese['context']; 
    2320                $URI = $URI['concept']; 
     
    6360 
    6461                                $start = $limit * $page - $limit; 
    65  
    6662                                // do not put $limit*($page - 1) 
    6763                                //end: for grid 
    68  
     64                                 
     65                                 
     66                                /** 
     67                                 * Trata o caso específico de retorno do atributo messageId 
     68                                 * 
     69                                 * TODO - refazer todo a operação find do conceito message, uma vez que esta  
     70                                 * foi desenvolvida quando a nova API ainda era muito imatura e se encontra  
     71                                 * muito acoplada à estrutura de retorno esperada pelo plugin jqGrid 
     72                                 */ 
     73                                if ( $justthese )  
     74                                { 
     75                                        if ($justthese[0] == 'messageId') { 
     76                                                $map = array( 
     77                                                        'folderName' => array(), 
     78                                                        'messageNumber' => array() 
     79                                                ); 
     80                                                 
     81                                                self::parseFilter($criteria["filter"], &$map); 
     82                                                 
     83                                                if (count($map['folderName']) == 0) { 
     84                                                        $folders = $this->get_folders_list(); 
     85                                                        foreach ($folders as $folder) 
     86                                                                if (isset($folder['folder_id']))  
     87                                                                        $map['folderName'][] = $folder['folder_id']; 
     88                                                } 
     89                                                 
     90                                                $result = array(); 
     91                                                foreach ($map['folderName'] as $folder) { 
     92                                                        $this->mbox = $this->open_mbox($folder); 
     93 
     94                                                        /** 
     95                                                         * Se não foi passado messageNumber no filtro,  
     96                                                         * busca todas as mensagens de cada pasta 
     97                                                         */ 
     98                                                        $messages = empty($map['messageNumber']) ? '1:*' : implode(',', $map['messageNumber']); 
     99                                                        $sequenceType = empty($map['messageNumber']) ? 0 : FT_UID; 
     100 
     101                                                        $headers = imap_fetch_overview($this->mbox, $messages, $sequenceType); 
     102                                                        foreach ($headers as $h) { 
     103                                                                $result[] = array ( 'messageId' => $h->message_id ); 
     104                                                        } 
     105                                                                                                                 
     106                                                        imap_close($this->mbox); 
     107                                                        $this->mbox = false; 
     108                                                } 
     109                                                return $result; 
     110                                        } 
     111                                } 
     112                                 
    69113                                if( $filter ) 
    70114                                { 
     
    94138 
    95139                                        foreach( $result as $i => $val ) 
    96                                                 $result[$i] = unserialize( $val ); 
     140                                        $result[$i] = unserialize( $val ); 
     141 
    97142                                } 
    98143                                else 
     
    110155                                } 
    111156                                //return var_export($result); 
    112                                  
     157 
    113158                                $response = array( "page" => $page, "total" => $total_pages, "records" => $count ); 
    114                                                                  
     159                                 
    115160                                for ($i=0; $i<count($result); $i++) 
    116161                                { 
    117                                         $flags_enum = array('Unseen'=> 1,  'Answered'=> 1, 'Forwarded'=> 1, 'Flagged'=> 1, 'Recent'=> 1, 'Draft'=> 1 ); 
     162                                        $flags_enum = array('Recent', 'Unseen',  'Answered',  'Draft',  'Deleted', 'Flagged'); 
    118163 
    119164                                        foreach ($flags_enum as $key => $flag) 
    120165                                        { 
    121                                                 if ( !isset($result[$i][$key]) || !trim($result[$i][$key]) || trim($result[$i][$key]) == '')  
    122                                                         $flags_enum[$key] = 0; 
    123                                                         //unset($flags_enum[$key]); 
     166                                                if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '')  
     167                                                        unset($flags_enum[$key]); 
    124168 
    125169                                                unset($result[$i][$flag]); 
    126                                         }                                        
     170                                        } 
     171 
    127172                                        if (array_key_exists($i, $result)) 
    128173                                        { 
     
    131176                                                $response["rows"][$i]['flags'] = implode(',', $flags_enum); 
    132177                                                $response["rows"][$i]['size'] = $response["rows"][$i]['Size']; 
    133                                                 $response["rows"][$i]['folder'] = $folder_name; 
    134                                                  
    135178                                                //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 ); 
    136179                                                unset($response["rows"][$i]['Size']); 
    137180                                        } 
    138                                  }                                        
     181                                 } 
     182 
    139183                                return $this->to_utf8($response); 
    140184                        } 
     
    214258                         
    215259                        case 'followupflagged': 
    216                         { 
    217                          
     260                        {                        
    218261                                $result = array ( ); 
    219                                 if (isset($criteria["filter"]) && is_array($criteria['filter'])) { 
    220                                         //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor 
    221                                         $map = array( 
    222                                                 'id' => array(), 
    223                                                 'folderName' => array(), 
    224                                                 'messageNumber' => array() 
    225                                         ); 
    226                                          
    227                                         self::parseFilter($criteria["filter"], &$map); 
    228                                          
    229                                         if (empty($map['folderName'])) { 
    230                                                 $folders = $this->get_folders_list(); 
    231                                                 foreach ($folders as $folder) 
    232                                                         if (isset($folder['folder_id']))  
    233                                                                 $map['folderName'][] = $folder['folder_id']; 
    234                                         } 
    235  
    236                                         foreach ($map['folderName'] as $folder) { 
    237                                                 $this->mbox = $this->open_mbox($folder); 
    238                                                  
    239                                                 /** 
    240                                                  * Caso não tenha sido passado id no filtro 
    241                                                  */ 
    242                                                 if (!$map['id']) { 
    243                                                         $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged"', SE_UID); 
    244  
    245                                                         foreach ($messagesFlaggeds as $messageFlagged) { 
    246                                                                 if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber'])) 
    247                                                                         continue; 
    248                                                                          
    249                                                                 $result[] = array ( 
    250                                                                         'folderName' => $folder, 
    251                                                                         'messageNumber' => $messageFlagged 
    252                                                                 ); 
    253                                                         } 
    254                                                          
    255                                                 } else { 
    256                                                         foreach ($map['id'] as $followupflagged) { 
    257                                                                 $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflagged.'"', SE_UID); 
    258                                                                  
    259                                                                 foreach ($messagesFlaggeds as $messageFlagged) { 
    260                                                                         if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber'])) 
    261                                                                                 continue; 
    262                                                                                  
    263                                                                         $result[] = array ( 
    264                                                                                 'id' => $folder . '/' . $messageFlagged . '#' . $followupflagged,  
    265                                                                                 'folderName' => $folder, 
    266                                                                                 'messageNumber' => $messageFlagged 
    267                                                                         ); 
    268                                                                 } 
     262 
     263                                $map = array( 
     264                                        //'id' => array(), 
     265                                        'folderName' => array(), 
     266                                        'messageNumber' => array(), 
     267                                        'messageId' => array() 
     268                                ); 
     269                                 
     270                                self::parseFilter($criteria["filter"], &$map); 
     271         
     272                                if (empty($map['folderName'])) { 
     273                                        $folders = $this->get_folders_list(); 
     274                                        foreach ($folders as $folder) 
     275                                                if (isset($folder['folder_id']))  
     276                                                        $map['folderName'][] = $folder['folder_id']; 
     277                                } 
     278                                 
     279                                $messagesIds = $map['messageId']; 
     280 
     281                                foreach ($map['folderName'] as $folder) { 
     282                                        $messages = array(); 
     283                                         
     284                                        $this->mbox = $this->open_mbox($folder); 
     285                                 
     286                                        /** 
     287                                         * Se é uma busca por messageId 
     288                                         */ 
     289                                        if (!empty($map['messageId'])) { 
     290                                                         
     291                                                foreach ($messagesIds as $k => $v) { 
     292                                                        $r = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged" TEXT "'.$v.'"', SE_UID); 
     293 
     294                                                        if ($r) { 
     295                                                                $messages = $messages + $r; 
     296                                                                unset($messagesIds[$k]);         
    269297                                                        } 
    270298                                                } 
    271                                                  
    272                                                 imap_close($this->mbox); 
    273                                                 $this->mbox = false; 
    274                                         } 
    275                                 } 
    276                                                  
    277                                 return $result;                          
    278                         } 
     299 
     300                                        /** 
     301                                         * Se é uma busca por messageNumber. 
     302                                         * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro. 
     303                                         */ 
     304                                        } else { 
     305                                                $messages = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged"', SE_UID); 
     306                                        } 
     307 
     308                                        /** 
     309                                         * Se é uma busca por messageId, deve ser comparado com os messageNumbers  
     310                                         * passados no filtro, se houverem. 
     311                                         */ 
     312                                        if (!empty($map['messageNumber'])) { 
     313                                                foreach ($messages as $k => $m) 
     314                                                        if (!in_array($m, $map['messageNumber'])) 
     315                                                                unset($messages[$k]); 
     316                                        } 
     317 
     318                                        /** 
     319                                         * Adicionar demais atributos às mensagens para retorno 
     320                                         */ 
     321                                        foreach ($messages as $k => $m) { 
     322                                                $headers = imap_fetch_overview($this->mbox, $m, FT_UID); 
     323                                                         
     324                                                $result[] = array ( 
     325                                                        'messageId' => $headers[0]->message_id, 
     326                                                        'messageNumber' => $m, 
     327                                                        'folderName' => $folder 
     328                                                ); 
     329                                        } 
     330 
     331                                        imap_close($this->mbox); 
     332                                        $this->mbox = false; 
     333                                         
     334                                        /** 
     335                                         * Se é uma busca por messageId e todos os messageIds foram econstrados: 
     336                                         * Stop searching in all folders 
     337                                         */ 
     338                                        if (!empty($map['messageId']) && empty($messagesIds)) 
     339                                                break; 
     340                                } 
     341                                 
     342                                if ($this->mbox) { 
     343                                        imap_close($this->mbox); 
     344                                        $this->mbox = false; 
     345                                } 
     346 
     347                                return $result; 
     348                                 
     349                        } //CASE 'followupflag' 
    279350                } 
    280351    } 
     
    324395                        case 'followupflagged': 
    325396                        { 
    326                                 /** 
    327                                  * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#followupflaggedId 
     397                         
     398                                /**  
     399                                 * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>" 
    328400                                 */ 
    329                                 list($messageId, $followupflaggedId) = explode('#', $URI['id']); 
    330                                 $folderName = dirname($messageId); 
    331                                 $messageNumber = basename($messageId); 
    332                                  
     401                                $folderName = $messageNumber = false; 
     402                                if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) { 
     403                                        $folderName = dirname($URI['id']); 
     404                                        $messageNumber = basename($URI['id']); 
     405                                } 
     406 
    333407                                $result = array(); 
    334  
    335                                 if ($folderName && $messageNumber && $followupflaggedId) { 
     408                                if ($folderName && $messageNumber) { 
     409 
    336410                                        $this->mbox = $this->open_mbox($folderName); 
    337                                         $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflaggedId.'"', SE_UID); 
    338                                          
    339                                         if (in_array($messageNumber, $messagesFlaggeds)) { 
     411                                        $r = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged"', SE_UID); 
     412 
     413                                        if (in_array($messageNumber, $r)) { 
     414                                                $headers = imap_fetch_overview($this->mbox, $messageNumber, FT_UID); 
     415                                                         
    340416                                                $result = array ( 
    341                                                         'id' => $URI['id'],  
    342                                                         'folderName' => $folderName, 
    343                                                         'messageNumber' => $messageNumber 
     417                                                        'messageId' => $headers[0]->message_id, 
     418                                                        'messageNumber' => $messageNumber, 
     419                                                        'folderName' => $folderName 
    344420                                                ); 
    345421                                        } 
     422                                         
    346423                                        imap_close($this->mbox); 
    347424                                        $this->mbox = false; 
     425                                 
     426                                } else { 
     427                                        /** 
     428                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela, 
     429                                         * senão busca em todas. 
     430                                         */ 
     431                                         
     432                                        $folders = array (); 
     433                                        if ($folderName) { 
     434                                                $folders = array ($folderName); 
     435                                        } else { 
     436                                                $folder_list = $this->get_folders_list(); 
     437                                                foreach ($folder_list as $folder) 
     438                                                        if (isset($folder['folder_id']))  
     439                                                                $folders[] = $folder['folder_id']; 
     440                                        } 
     441                                         
     442                                        foreach ($folders as $folder) { 
     443                                                 
     444                                                $this->mbox = $this->open_mbox($folder); 
     445                                                 
     446                                                if ($messages = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged" TEXT "'.$URI['id'].'"', SE_UID)) { 
     447                                 
     448                                                        $result = array ( 
     449                                                                'messageId' => $URI['id'], 
     450                                                                'messageNumber' => $messages[0], 
     451                                                                'folderName' => $folder 
     452                                                        ); 
     453                                                         
     454                                                        /** 
     455                                                         * Stop searching in all folders 
     456                                                         */ 
     457                                                        break; 
     458                                                } 
     459                                                 
     460                                                imap_close($this->mbox); 
     461                                                $this->mbox = false; 
     462                                        } 
     463                                } 
     464                                 
     465                                if ($this->mbox) { 
     466                                        imap_close($this->mbox); 
     467                                        $this->mbox = false; 
    348468                                } 
    349469                                 
     
    353473    } 
    354474 
    355     public function create( $URI, $data) 
     475    public function create($URI, &$data) 
    356476    {                
    357477                switch( $URI['concept'] ) 
     
    372492                        case 'followupflagged': 
    373493                        { 
    374                                 //deve ser gravado no banco primeiro, obtido o id e, depois, gravado no imap passando o id no parametro $data 
    375                                 if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['id'])) { 
    376                                         list($messageId, $followupflaggedId) = explode('#', $data['id']); 
     494                                //deve ser gravado primeiro no imap, obtido o message-id e, depois gravado no banco 
     495                                 
     496                                if (isset($data['folderName']) && isset($data['messageNumber'])) { 
    377497                                         
    378498                                        $this->mbox = $this->open_mbox($data['folderName']); 
    379                                         $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged $Followupflag' . $followupflaggedId, ST_UID); 
    380                                                                                  
     499                                        $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged', ST_UID); 
     500                                         
     501                                        $headers = imap_fetch_overview($this->mbox, $data['messageNumber'], FT_UID); 
     502                                         
     503                                        $data['messageId'] = $headers[0]->message_id; 
     504                                                         
    381505                                        imap_close($this->mbox); 
    382506                                        $this->mbox = false; 
     
    384508                                        return ($s) ? $data : array(); 
    385509 
     510                                } else if (isset($data['messageId'])) { 
     511                                        /** 
     512                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela, 
     513                                         * senão busca em todas. 
     514                                         */ 
     515                                        $folders = array (); 
     516                                        if (isset($data['folderName'])) { 
     517                                                $folders = array ($data['folderName']); 
     518                                        } else { 
     519                                                $folder_list = $this->get_folders_list(); 
     520                                                foreach ($folder_list as $folder) 
     521                                                        if (isset($folder['folder_id']))  
     522                                                                $folders[] = $folder['folder_id']; 
     523                                        } 
     524                                         
     525                                        foreach ($folders as $folder) { 
     526                                                 
     527                                                $this->mbox = $this->open_mbox($folder); 
     528                                                if ($messages = imap_search($this->mbox, 'TEXT "'.$data['messageId'].'"', SE_UID)) { 
     529                                                         
     530                                                        $s = imap_setflag_full($this->mbox, $messages[0], '$Followupflagged', ST_UID); 
     531                                                         
     532                                                        imap_close($this->mbox); 
     533                                                        $this->mbox = false; 
     534                                                         
     535                                                        /** 
     536                                                         * Stop searching in all folders 
     537                                                         */ 
     538                                                        return $data; 
     539                                                } 
     540                                                 
     541                                                imap_close($this->mbox); 
     542                                                $this->mbox = false; 
     543                                        } 
    386544                                } 
    387545                                return array (); 
     
    390548                        case 'message': 
    391549                        { 
     550                                require_once ROOTPATH.'/library/uuid/class.uuid.php'; 
     551                                 
    392552                                $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True ); 
    393                                 $return = array(); 
    394                                  
    395                                 require_once dirname(__FILE__) . '/../../services/class.servicelocator.php'; 
    396                                 $mailService = ServiceLocator::getService('mail'); 
    397  
    398                                 $msg_uid = $data['msg_id']; 
    399                                 $body = $data['body']; 
    400                                 $body = str_replace("%nbsp;","&nbsp;",$body); 
    401                                 $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );                                         
    402  
    403                                 $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8"); 
    404                                 $folder = @eregi_replace("INBOX[/.]", "INBOX".$this->imap_delimiter, $folder); 
     553                                $return = array(); 
     554 
     555                                require_once dirname(__FILE__) . '/../../services/class.servicelocator.php'; 
     556                                $mailService = ServiceLocator::getService('mail'); 
     557 
     558                                $msg_uid = $data['msg_id']; 
     559                                $body = $data['body']; 
     560                                $body = str_replace("%nbsp;","&nbsp;",$body); 
     561                                $body = preg_replace("/\n/"," ",$body); 
     562                                $body = preg_replace("/\r/","" ,$body); 
     563                                $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );                                         
     564 
     565                                $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8"); 
     566                                $folder = @eregi_replace("INBOX[/.]", "INBOX".$this->imap_delimiter, $folder); 
    405567 
    406568                                /** 
    407                                  * Gera e preenche o field Message-Id do header 
    408                                  */ 
     569                                * Gera e preenche o field Message-Id do header 
     570                                */ 
    409571                                $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft'); 
    410                                                                  
    411                                 $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); 
    412                                 $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); 
    413                                 $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); 
    414                                 $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); 
    415                                  
    416                                 if(isset($data['input_important_message'])) 
    417                                         $mailService->addHeaderField('Importance','High'); 
    418  
    419                                 if(isset($data['input_return_receipt'])) 
    420                                         $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail')); 
    421  
    422                                 $isHTML = ( isset($data['type']) && $data['type'] == 'html' )?  true : false; 
    423                                                                    
    424                                 if (!$body) $body = ' '; 
    425                                  
    426  
    427                                 $mbox_stream = $this->open_mbox($folder); 
    428  
    429                                 $attachment = json_decode($data['attachments'],TRUE); 
    430                                  
    431  
    432                                 foreach ($attachment as &$value)  
    433                                 { 
    434  
    435                                     if((int)$value > 0) //BD attachment 
    436                                     { 
    437                                          $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment')); 
    438                                           
    439                                           
    440                                          if($att['disposition'] == 'embedded') 
    441                                          { 
    442                                              $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body); 
    443                                              $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']); 
    444                                          } 
    445                                          else 
    446                                              $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); 
    447                                           
    448                                          unset($att); 
    449                                     } 
    450                                     else 
    451                                     { 
    452                                         $value = json_decode($value, true); 
    453                                          
    454                                         switch ($value['type']) { 
    455                                             case 'imapPart': 
    456                                                     $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']); 
    457                                                     if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG 
    458                                                     {     
    459                                                         $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body); 
    460                                                         $mailService->addStringImage($att['source'], $att['type'], $att['name']); 
    461                                                     } 
    462                                                     else 
    463                                                         $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); 
    464                                                     unset($att); 
    465                                                 break; 
    466                                               case 'imapMSG': 
    467                                                     $sub =  $value['name'] ? $value['name'].'.eml' :'no title.eml'; 
    468                                                     $mbox_stream = $this->open_mbox($value['folder']); 
    469                                                     $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']); 
    470                                                     $mailService->addStringAttachment($rawmsg, $sub, 'message/rfc822', '7bit', 'attachment' ); 
    471                                                     unset($rawmsg); 
    472                                                 break; 
    473  
    474                                             default: 
    475                                                 break; 
    476                                         } 
    477                                     } 
    478  
    479                                 } 
    480                                  
    481                                 if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body); 
    482    
    483                                 if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft")) 
    484                                 { 
    485                                     $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT); 
    486                                     $return['id'] = $status->uidnext - 1; 
    487                                          
    488                                     if($data['uidsSave'] ) 
    489                                         $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave'])); 
    490                         
    491                                 } 
    492                   
    493                                 if($mbox_stream) imap_close($mbox_stream); 
    494  
    495                                 return $return; 
     572 
     573                                $mailService->addTo($data['input_to']); 
     574                                $mailService->addCc( $data['input_cc']); 
     575                                $mailService->addBcc($data['input_cco']); 
     576                                $mailService->setSubject($data['input_subject']); 
     577                                if(isset($data['input_important_message'])) 
     578                                        $mailService->addHeaderField('Importance','High'); 
     579 
     580                                if(isset($data['input_return_receipt'])) 
     581                                        $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail')); 
     582 
     583                                $isHTML = ( ( array_key_exists( 'type', $data ) && in_array( strtolower( $data[ 'type' ] ), array( 'html', 'plain' ) ) ) ? strtolower( $data[ 'type' ] ) != 'plain' : true ); 
     584 
     585                                if (!$body) $body = ' '; 
     586 
     587 
     588                                $mbox_stream = $this->open_mbox($folder); 
     589 
     590                                $attachment = json_decode($data['attachments'],TRUE); 
     591 
     592 
     593                                foreach ($attachment as &$value)  
     594                                { 
     595                                        if((int)$value > 0) //BD attachment 
     596                                        { 
     597                                                $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment')); 
     598 
     599                                                if($att['disposition'] == 'embedded') 
     600                                                { 
     601                                                        $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body); 
     602                                                        $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']); 
     603                                                } 
     604                                                else 
     605                                                        $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); 
     606 
     607                                                unset($att); 
     608                                        } 
     609                                        else 
     610                                        { 
     611                                                $value = json_decode($value, true); 
     612 
     613                                                switch ($value['type']) { 
     614                                                        case 'imapPart': 
     615                                                                $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']); 
     616                                                                if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG 
     617                                                                {     
     618                                                                        $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body); 
     619                                                                        $mailService->addStringImage($att['source'], $att['type'], $att['name']); 
     620                                                                } 
     621                                                                else 
     622                                                                        $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); 
     623                                                                unset($att); 
     624                                                                break; 
     625                                                        case 'imapMSG': 
     626                                                                $sub =  $value['name'] ? $value['name'].'.eml' :'no title.eml'; 
     627                                                                $mbox_stream = $this->open_mbox($value['folder']); 
     628                                                                $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']); 
     629                                                                $mailService->addStringAttachment($rawmsg, $sub, 'message/rfc822', '7bit', 'attachment' ); 
     630                                                                unset($rawmsg); 
     631                                                                break; 
     632 
     633                                                        default: 
     634                                                        break; 
     635                                                } 
     636                                        } 
     637 
     638                                } 
     639 
     640                                if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body); 
     641 
     642                                if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft")) 
     643                                { 
     644                                        $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT); 
     645                                        $return['id'] = $status->uidnext - 1; 
     646 
     647                                        if($data['uidsSave'] ) 
     648                                        $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave'])); 
     649                                } 
     650 
     651                                if($mbox_stream) imap_close($mbox_stream); 
     652 
     653                                return $return; 
    496654                        } 
    497655                } 
     
    518676                        case 'followupflagged': 
    519677                        { 
    520                                 list($messageId, $followupflaggedId) = explode('#', $URI['id']); 
    521                                  
    522                                 $folderName = dirname($messageId); 
    523                                 $messageNumber = basename($messageId); 
    524678                         
    525                                 if ($folderName && $messageNumber && $followupflaggedId) { 
     679                                /**  
     680                                 * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>" 
     681                                 */ 
     682                                $folderName = $messageNumber = false; 
     683                                if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) { 
     684                                        $folderName = dirname($URI['id']); 
     685                                        $messageNumber = basename($URI['id']); 
     686                                } 
     687                                 
     688                                /** 
     689                                 * TODO - efetuar o parsing do $criteria e obter a pasta passada 
     690                                 */ 
     691                                $map = array( 
     692                                        'folderName' => array(), 
     693                                        'messageNumber' => array(), 
     694                                        'messageId' => array() 
     695                                ); 
     696                         
     697                                if ($folderName && $messageNumber) { 
    526698 
    527699                                        $this->mbox = $this->open_mbox($folderName); 
    528                                         $f1 = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflag' . $followupflaggedId, ST_UID); 
    529  
    530                                         /** 
    531                                          * implementa a possibilidade de listar todas as mensagens sinalizadas através da busca 
    532                                          */ 
    533                                         $f2 = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflagged', ST_UID); 
     700                                        $s = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflagged', ST_UID); 
    534701 
    535702                                        imap_close($this->mbox, CL_EXPUNGE); 
    536703                                        $this->mbox = false; 
    537704                                         
    538                                         return ($f1 && $f2); 
     705                                        return $s; 
     706                                } else { 
     707                                        /** 
     708                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela, 
     709                                         * senão busca em todas. 
     710                                         */ 
     711                                         
     712                                        $folders = array (); 
     713                                        if (isset($map['folderName'])) { 
     714                                                $folders = $map['folderName']; 
     715                                        } else { 
     716                                                $folder_list = $this->get_folders_list(); 
     717                                                foreach ($folder_list as $folder) 
     718                                                        if (isset($folder['folder_id']))  
     719                                                                $folders[] = $folder['folder_id']; 
     720                                        } 
     721                                         
     722                                        foreach ($folders as $folder) { 
     723                                                 
     724                                                $this->mbox = $this->open_mbox($folder); 
     725                                                if ($messages = imap_search($this->mbox, 'TEXT "'.$URI['id'].'"', SE_UID)) { 
     726                                                         
     727                                                        $s = imap_clearflag_full($this->mbox, $messages[0], '$Followupflagged', ST_UID); 
     728                                                         
     729                                                        imap_close($this->mbox, CL_EXPUNGE); 
     730                                                        $this->mbox = false; 
     731                                                         
     732                                                        /** 
     733                                                         * Stop searching in all folders 
     734                                                         */ 
     735                                                        return $s; 
     736                                                } 
     737                                                 
     738                                                imap_close($this->mbox); 
     739                                                $this->mbox = false; 
     740                                        } 
    539741                                } 
    540742                                 
     
    552754    { 
    553755                /** 
    554                  * Os únicos atributos que podem ser alterados no IMAP são folderName e messageId, 
     756                 * Os únicos atributos da sinalização presentes no IMAP são folderName, messageNumber e messageId, 
    555757                 * porém a operação de update desses atributos não faz sentido para o usuário da DataLayer, 
    556758                 * pois na prática elas são executadas através das operações de CREATE e DELETE. 
Note: See TracChangeset for help on using the changeset viewer.