source: trunk/prototype/services/ImapServiceAdapter.php @ 6303

Revision 6303, 26.4 KB checked in by cristiano, 12 years ago (diff)

Ticket #2799 - Problema no preview de mensagens (anexar mensagem)

Line 
1<?php
2/**
3 *
4 * Copyright (C) 2012 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
5 *
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by
8 * the Free Software Foundation with the addition of the following permission
9 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
10 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
11 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program; if not, see www.gnu.org/licenses or write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301 USA.
22 *
23 * This code is based on the OpenXchange Connector and on the Prognus pSync
24 * Connector both developed by the community and licensed under the GPL
25 * version 2 or above as published by the Free Software Foundation.
26 *
27 * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
28 * 6731, PTI, Edifício do Saber, 3º floor, room 306, Foz do Iguaçu - PR - Brasil or at
29 * e-mail address prognus@prognus.com.br.
30 *
31 * Classe de abstração que faz uma adaptação para manipulação de informações
32 * no IMAP a partir de vários métodos.
33 *
34 * @package    Prototype
35 * @license    http://www.gnu.org/copyleft/gpl.html GPL
36 * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
37 * @version    2.4
38 * @sponsor    Caixa Econômica Federal
39 * @since      Arquivo disponibilizado na versão 2.4
40 */
41
42include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php";
43
44/**
45 *
46 * @package    Prototype (Mail)
47 * @license    http://www.gnu.org/copyleft/gpl.html GPL
48 * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
49 * @version    2.4
50 * @sponsor    Caixa Econômica Federal
51 * @since      Classe disponibilizada na versão 2.4
52 */
53class ImapServiceAdapter extends imap_functions/* implements Service*/
54{
55    public function open( $config )
56    {
57                $this->init();
58    }
59
60//     public function connect( $config )
61//     {
62//                      $this->init();
63//     }
64       
65    public function find( $URI, $justthese = false, $criteria = false )
66        {
67
68                $context = isset($justthese['context']) ? $justthese['context'] : '' ;
69
70                switch( $URI['concept'] )
71                {
72                        case 'folder':
73                        {
74                                $result = $this->getFolders();
75
76                                foreach ($result as $res) {
77
78                                        $response[] = array(
79                                                        'id' => mb_convert_encoding( $res['folder_id'], 'UTF-8', 'UTF7-IMAP' ),
80                                                        'commonName' => mb_convert_encoding( $res['folder_name'], 'UTF-8' , 'UTF7-IMAP' ),
81                                                        'parentFolder' => mb_convert_encoding( $res['folder_parent'], 'UTF-8' , 'UTF7-IMAP' ),
82                                                        'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
83                                                );
84                                }
85
86                                return $response;
87                        }
88                        case 'message':
89                        {
90                                //begin: for grid       
91                                $page  = isset($criteria['page']) ? $criteria['page'] : 1 ; //{1}    get the requested page
92                                $limit = isset($criteria['rows']) ? $criteria['rows'] : 10 ; //{10}   get how many rows we want to have into the grid
93                                $sidx  = isset($criteria['sidx']) ? $criteria['sidx'] : 0; //{id}   get index row - i.e. user click to sort
94                                $sord  = isset($criteria['sord']) ? $criteria['sord'] : ''; //{desc} get the direction
95
96                                $filter =  isset($criteria['filter']) ? $criteria['filter'] : '';
97
98                                if( !$sidx ) $sidx = 1;
99
100                                $folder_name =  isset($URI['folder']) ?  $URI['folder'] : str_replace( '.', $this->imap_delimiter, isset($context['folder']) ?  $context['folder'] : 'INBOX');
101                       
102                                $count = imap_num_msg( $this->open_mbox( $folder_name ) );
103
104                                $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
105
106                                if( $page > $total_pages )
107                                        $page = $total_pages;
108
109                                $start = $limit * $page - $limit;
110                                // do not put $limit*($page - 1)
111                                //end: for grid
112                               
113                               
114                                /**
115                                 * Trata o caso específico de retorno do atributo messageId
116                                 *
117                                 * TODO - refazer todo a operação find do conceito message, uma vez que esta
118                                 * foi desenvolvida quando a nova API ainda era muito imatura e se encontra
119                                 * muito acoplada à estrutura de retorno esperada pelo plugin jqGrid
120                                 */
121                                if ( $justthese )
122                                {
123                                        if (isset($justthese[0]) && $justthese[0] == 'messageId') {
124                                                $map = array(
125                                                        'folderName' => array(),
126                                                        'messageNumber' => array()
127                                                );
128                                               
129                                                self::parseFilter($criteria["filter"], &$map);
130                                               
131                                                if (count($map['folderName']) == 0) {
132                                                        $folders = $this->get_folders_list();
133                                                        foreach ($folders as $folder)
134                                                                if (isset($folder['folder_id']))
135                                                                        $map['folderName'][] = $folder['folder_id'];
136                                                }
137                                               
138                                                $result = array();
139                                                foreach ($map['folderName'] as $folder) {
140                                                        $this->mbox = $this->open_mbox($folder);
141
142                                                        /**
143                                                         * Se não foi passado messageNumber no filtro,
144                                                         * busca todas as mensagens de cada pasta
145                                                         */
146                                                        $messages = empty($map['messageNumber']) ? '1:*' : implode(',', $map['messageNumber']);
147                                                        $sequenceType = empty($map['messageNumber']) ? 0 : FT_UID;
148
149                                                        $headers = imap_fetch_overview($this->mbox, $messages, $sequenceType);
150                                                        foreach ($headers as $h) {
151                                                                if(isset($h->message_id ))
152                                                                        $result[] = array ( 'messageId' => $h->message_id );
153                                                        }
154       
155                                                }
156                                                return $result;
157                                        }
158                                }
159                               
160                                if( $filter )
161                                {
162                                        if( $filter[0] !== 'msgNumber' )
163                                        {
164                                        for( $i = 0; $i < count($filter); $i++ )
165                                        {
166                                                if( count( $filter[$i] ) === 4 )
167                                                $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' );
168
169                                                $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] );
170                                        }
171
172                                        return $this->searchSieveRule($criteria);
173                                        }
174
175                                        $msgNumber = array();
176
177                                        for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ )
178                                          $msgNumber[] = $filter[2][$i];
179
180                                        if( empty( $msgNumber ) )
181                                        return( false );
182
183                                        $result = $this->get_info_msgs( array( 'folder' => $folder_name,
184                                                                           'msgs_number' => implode( ',', $msgNumber ) ) );
185
186                                        foreach( $result as $i => $val )
187                                        $result[$i] = unserialize( $val );
188
189                                }
190                                else
191                                {
192                                        $result = $this->get_range_msgs2(
193                                                array(
194                                                        'folder' => $folder_name, //INBOX
195                                                        'msg_range_begin' => $start + 1, //??
196                                                        'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid
197                                                        'sort_box_type' => 'SORTARRIVAL',
198                                                        'search_box_type' => 'ALL',
199                                                        'sort_box_reverse' => 1
200                                                )
201                                        );
202                                }
203                                //return var_export($result);
204
205                                $response = array( "page" => $page, "total" => $total_pages, "records" => $count );
206                               
207                                for ($i=0; $i<count($result); $i++)
208                                {
209                                        $flags_enum = array('Unseen'=> 1,  'Answered'=> 1, 'Forwarded'=> 1, 'Flagged'=> 1, 'Recent'=> 1, 'Draft'=> 1 );
210
211                                        foreach ($flags_enum as $key => $flag)
212                                        {
213                                                if ( !isset($result[$i][$key]) || !trim($result[$i][$key]) || trim($result[$i][$key]) == '') 
214                                                        $flags_enum[$key] = 0;
215
216                                                unset($result[$i][$flag]);
217                                        }
218
219                                        if (array_key_exists($i, $result))
220                                        {
221                                                $response["rows"][$i] = $result[$i];
222                                                $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 );
223                                                $response["rows"][$i]['flags'] = implode(',', $flags_enum);
224                                                $response["rows"][$i]['size'] = $response["rows"][$i]['Size'];
225                                                $response["rows"][$i]['folder'] = $folder_name;
226                                                //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 );
227                                                unset($response["rows"][$i]['Size']);
228                                        }
229                                 }
230
231                                return $this->to_utf8($response);
232                        }
233                       
234                        /**
235                         * Filtros suportados:
236                         * - ['=', 'folderName', $X]
237                         * - [
238                         *              'AND',
239                         *              [
240                         *                      'AND',
241                         *                      ['=', 'folderName', $X],
242                         *                      ['IN', 'messageNumber', $Ys]
243                         *              ],
244                         *              ['IN', 'labelId', $Zs]
245                         * ]
246                         * - ['=', 'labelId', $X]
247                         * - [
248                         *              'AND',
249                         *              ['=', 'folderName', $X],
250                         *              ['=', 'labelId', $Y]
251                         * ]
252                         * - ['IN', 'labelId', $Ys]
253                         * - [
254                         *              'AND',
255                         *              ['=', 'folderName', $X],
256                         *              ['IN', 'labelId', $Ys]
257                         * ]                   
258                         */
259                        case 'labeled':
260                        {
261                                $result = array ( );
262                                if (isset($criteria["filter"]) && is_array($criteria['filter'])) {
263                                        //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor
264                                        $map = array(
265                                                'id' => array(),
266                                                'folderName' => array(),
267                                                'messageNumber' => array(),
268                                                'labelId' => array()
269                                        );
270                                       
271                                        self::parseFilter($criteria["filter"], &$map);
272                                       
273                                        if (count($map['folderName']) == 0) {
274                                                $folders = $this->get_folders_list();
275                                                foreach ($folders as $folder)
276                                                        if (isset($folder['folder_id']))
277                                                                $map['folderName'][] = $folder['folder_id'];
278                                        }
279
280                                        foreach ($map['folderName'] as $folder) {
281                                                $this->mbox = $this->open_mbox($folder);
282                                               
283                                                foreach ($map['labelId'] as $label) {
284                                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$ExpressoLabel'.$label.'"', SE_UID);
285                                                       
286                                                        if(is_array($messagesLabeleds))
287                                                        foreach ($messagesLabeleds as $messageLabeled) {
288                                                                if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber']))
289                                                                        continue;
290                                                                       
291                                                                $result[] = array (
292                                                                        'id' => $folder . '/' . $messageLabeled . '#' . $label,
293                                                                        'folderName' => $folder,
294                                                                        'messageNumber' => $messageLabeled,
295                                                                        'labelId' => $label
296                                                                );
297                                                        }
298                                                }
299
300                                        }
301                                }
302                               
303                                return $result;
304                        }
305                       
306                        case 'followupflagged':
307                        {                                       
308                                $result = array ( );
309
310                                $map = array(
311                                        //'id' => array(),
312                                        'folderName' => array(),
313                                        'messageNumber' => array(),
314                                        'messageId' => array()
315                                );
316                               
317                                self::parseFilter($criteria["filter"], &$map);
318       
319                                if (empty($map['folderName'])) {
320                                        $folders = $this->get_folders_list();
321                                        foreach ($folders as $folder)
322                                                if (isset($folder['folder_id']))
323                                                        $map['folderName'][] = $folder['folder_id'];
324                                }
325                               
326                                $messagesIds = $map['messageId'];
327
328                                foreach ($map['folderName'] as $folder) {
329                                        $messages = array();
330                                       
331                                        $this->mbox = $this->open_mbox($folder);
332
333                                        /**
334                                         * Se é uma busca por messageId
335                                         */
336                                        if (!empty($map['messageId'])) {
337                                                       
338                                                foreach ($messagesIds as $k => $v) {
339
340                                                        $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID);
341
342                                                        if ($r) {
343
344                                                                $messages = array_merge($messages, $r);
345                                                                unset($messagesIds[$k]);
346                                                               
347                                                        }
348                                                }
349
350                                        /**
351                                         * Se é uma busca por messageNumber.
352                                         * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro.
353                                         */
354                                        } else {
355                                                $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
356                                        }
357
358                                        /**
359                                         * Se é uma busca por messageId, deve ser comparado com os messageNumbers
360                                         * passados no filtro, se houverem.
361                                         */
362                                        if (!empty($map['messageNumber']) && is_array($messages)) {
363                                                                                               
364                                                foreach ($messages as $k => $m)
365                                                        if (!in_array($m, $map['messageNumber']))
366                                                                unset($messages[$k]);
367                                        }
368
369                                        /**
370                                         * Adicionar demais atributos às mensagens para retorno
371                                         */
372                                        if(is_array($messages))
373                                        foreach ($messages as $k => $m) {
374                                                $headers = imap_fetch_overview($this->mbox, $m, FT_UID);
375
376                                                $result[] = array (
377                                                        'messageId' => $headers[0]->message_id,
378                                                        'messageNumber' => $m,
379                                                        'folderName' => $folder
380                                                );
381                                        }
382
383                                       
384                                        /**
385                                         * Se é uma busca por messageId e todos os messageIds foram econstrados:
386                                         * Stop searching in all folders
387                                         */
388                                        if (!empty($map['messageId']) && empty($messagesIds))
389                                                break;
390                                }
391                               
392
393                                return $result;
394                               
395                        } //CASE 'followupflag'
396                }
397    }
398
399    public function read( $URI, $justthese = false )
400    {
401
402                switch( $URI['concept'] )
403                {
404                        case 'message':
405                        {       
406                                return $this->to_utf8(
407                                        $this->get_info_msg( array('msg_number'=>$URI['id'],
408                                        'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] ) ,
409                                        'decoded' => true ) )
410                                );
411                        }
412                        case 'labeled':
413                        {
414                                /**
415                                 * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId
416                                 */
417                                list($messageId, $labelId) = explode('#', $URI['id']);
418                                $folderName = basename($messageId);
419                                $messageNumber = dirname($messageId);
420                               
421                                $result = array();
422
423                                if ($folderName && $messageNumber && $labelId) {
424                                        $this->mbox = $this->open_mbox($folderName);
425                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$ExpressoLabel'.$labelId.'"', SE_UID);
426                                       
427                                        if (in_array($messageNumber, $messagesLabeleds)) {
428                                                $result = array (
429                                                        'id' => $URI['id'],
430                                                        'folderName' => $folderName,
431                                                        'messageNumber' => $messageNumber,
432                                                        'labelId' => $labelId
433                                                );
434                                        }
435
436                                }
437                               
438                                return $result;
439                        }
440                       
441                        case 'followupflagged':
442                        {
443                       
444                                /**
445                                 * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>"
446                                 */
447                                $folderName = $messageNumber = false;
448                                if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) {
449                                        $folderName = dirname($URI['id']);
450                                        $messageNumber = basename($URI['id']);
451                                }
452
453                                $result = array();
454                                if ($folderName && $messageNumber) {
455
456                                        $this->mbox = $this->open_mbox($folderName);
457                                        $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
458
459                                        if (in_array($messageNumber, $r)) {
460                                                $headers = imap_fetch_overview($this->mbox, $messageNumber, FT_UID);
461                                                       
462                                                $result = array (
463                                                        'messageId' => $headers[0]->message_id,
464                                                        'messageNumber' => $messageNumber,
465                                                        'folderName' => $folderName
466                                                );
467                                        }
468                               
469                                } else {
470                                        /**
471                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela,
472                                         * senão busca em todas.
473                                         */
474                                       
475                                        $folders = array ();
476                                        if ($folderName) {
477                                                $folders = array ($folderName);
478                                        } else {
479                                                $folder_list = $this->get_folders_list();
480                                                foreach ($folder_list as $folder)
481                                                        if (isset($folder['folder_id']))
482                                                                $folders[] = $folder['folder_id'];
483                                        }
484                                       
485                                        foreach ($folders as $folder) {
486                                               
487                                                $this->mbox = $this->open_mbox($folder);
488                                               
489                                                if ($messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$URI['id'].'"', SE_UID)) {
490                               
491                                                        $result = array (
492                                                                'messageId' => $URI['id'],
493                                                                'messageNumber' => $messages[0],
494                                                                'folderName' => $folder
495                                                        );
496                                                       
497                                                        /**
498                                                         * Stop searching in all folders
499                                                         */
500                                                        break;
501                                                }
502       
503                                        }
504                                }
505                               
506                               
507                                return $result;
508                        }
509                }
510    }
511
512    public function create($URI, &$data)
513    {               
514                switch( $URI['concept'] )
515                {
516                        case 'labeled':
517                        {
518                                if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) {
519                                        $this->mbox = $this->open_mbox($data['folderName']);
520                                        imap_setflag_full($this->mbox, $data['messageNumber'], '$ExpressoLabel' . $data['labelId'], ST_UID);
521
522                                        return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']);
523                                }
524                                return array ();
525                        }
526                        case 'followupflagged':
527                        {
528                                //deve ser gravado primeiro no imap, obtido o message-id e, depois gravado no banco
529                               
530                                if (isset($data['folderName']) && isset($data['messageNumber'])) {
531                                       
532                                        $this->mbox = $this->open_mbox($data['folderName']);
533                                        $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged', ST_UID);
534                                       
535                                        $headers = imap_fetch_overview($this->mbox, $data['messageNumber'], FT_UID);
536                                       
537                                        $data['messageId'] = $headers[0]->message_id;
538                                                       
539
540                                        return ($s) ? $data : array();
541
542                                } else if (isset($data['messageId'])) {
543                                        /**
544                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela,
545                                         * senão busca em todas.
546                                         */
547                                        $folders = array ();
548                                        if (isset($data['folderName'])) {
549                                                $folders = array ($data['folderName']);
550                                        } else {
551                                                $folder_list = $this->get_folders_list();
552                                                foreach ($folder_list as $folder)
553                                                        if (isset($folder['folder_id']))
554                                                                $folders[] = $folder['folder_id'];
555                                        }
556                                       
557                                        foreach ($folders as $folder) {
558                                               
559                                                $this->mbox = $this->open_mbox($folder);
560                                                if ($messages = imap_search($this->mbox, 'ALL TEXT "Message-Id: '.$data['messageId'].'"', SE_UID)) {
561                                                       
562                                                        $s = imap_setflag_full($this->mbox, $messages[0], '$Followupflagged', ST_UID);
563                                               
564                                                        /**
565                                                         * Stop searching in all folders
566                                                         */
567                                                        return $data;
568                                                }
569                                               
570                                        }
571                                }
572                                return array ();
573                        }
574                       
575                        case 'message':
576                        {
577                                require_once ROOTPATH.'/library/uuid/class.uuid.php';
578                               
579                                $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True );
580                                $return = array();
581
582                                require_once dirname(__FILE__) . '/../../services/class.servicelocator.php';
583                                $mailService = ServiceLocator::getService('mail');
584
585                                $msg_uid = $data['msg_id'];
586                                $body = $data['body'];
587                                $body = str_replace("%nbsp;","&nbsp;",$body);
588                                $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );                                       
589
590                                $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8");
591                                $folder = @preg_replace('/INBOX[\/.]/i', "INBOX".$this->imap_delimiter, $folder);
592
593                                /**
594                                * Gera e preenche o field Message-Id do header
595                                */
596                                $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft');
597
598                                $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
599                                $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
600                                $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
601                                $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
602                               
603                                if(isset($data['input_important_message']))
604                                        $mailService->addHeaderField('Importance','High');
605
606                                if(isset($data['input_return_receipt']))
607                                        $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail'));
608
609                                $isHTML = ( isset($data['type']) && $data['type'] == 'html' )?  true : false;
610
611                                if (!$body) $body = ' ';
612
613
614                                $mbox_stream = $this->open_mbox($folder);
615
616                                $attachment = json_decode($data['attachments'],TRUE);
617
618
619                                foreach ($attachment as &$value)
620                                {
621                                        if((int)$value > 0) //BD attachment
622                                        {
623                                                $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment'));
624
625                                                if($att['disposition'] == 'embedded')
626                                                {
627                                                        $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body);
628                                                        $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']);
629                                                }
630                                                else
631                                                        $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
632
633                                                unset($att);
634                                        }
635                                        else
636                                        {
637                                                $value = json_decode($value, true);
638                                               
639                                                switch ($value['type']) {
640                                                        case 'imapPart':
641                                                                $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']);
642                                                                if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG
643                                                                {   
644                                                                        $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body);
645                                                                        $mailService->addStringImage($att['source'], $att['type'], $att['name']);
646                                                                }
647                                                                else
648                                                                        $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
649                                                                unset($att);
650                                                                break;
651                                                        case 'imapMSG':
652                                                                $mbox_stream = $this->open_mbox($value['folder']);
653                                                                $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']);
654                                                                $mailService->addStringAttachment($rawmsg, base64_decode($value['name']), 'message/rfc822', '7bit', 'attachment' );
655                                                                unset($rawmsg);
656                                                                break;
657
658                                                        default:
659                                                        break;
660                                                }
661                                        }
662
663                                }
664
665                                if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body);
666
667                                if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft"))
668                                {
669                                        $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
670                                        $return['id'] = $status->uidnext - 1;
671
672                                        if($data['uidsSave'] )
673                                                $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave']));
674                                }
675
676                                return $return;
677                        }
678                }
679        }
680
681    public function delete( $URI, $justthese = false, $criteria = false )
682    {
683                switch( $URI['concept'] )
684                {
685                        case 'labeled':
686                        {
687                                list($messageId, $labelId) = explode('#', $URI['id']);
688                                $folderName = dirname($messageId);
689                                $messageNumber = basename($messageId);
690
691                                if ($folderName && $messageNumber && $labelId) {
692                                        $this->mbox = $this->open_mbox($folderName);
693                                        imap_clearflag_full($this->mbox, $messageNumber, '$ExpressoLabel' . $labelId, ST_UID);
694
695                                }
696                        }
697                        case 'followupflagged':
698                        {
699                                $map = array(
700                                        'folderName' => array(),
701                                        'messageNumber' => array(),
702                                        'messageId' => array()
703                                );
704                               
705                                self::parseFilter($criteria["filter"], &$map);
706                               
707                                if (!$map['folderName']) {
708                                        $folders = array ();
709                                       
710                                        $folder_list = $this->get_folders_list();
711                                        foreach ($folder_list as $folder)
712                                                if (isset($folder['folder_id']))
713                                                        $folders[] = $folder['folder_id'];
714                                        $map['folderName'] = $folders;
715                                }
716
717                                $messagesIds = $map['messageId'];
718
719                                foreach ($map['folderName'] as $folder) {
720                                        $messages = array();
721                                       
722                                        $this->mbox = $this->open_mbox($folder);
723                               
724                                        /**
725                                         * Se é uma busca por messageId
726                                         */
727                                        if (!empty($map['messageId'])) {
728                                                       
729                                                foreach ($messagesIds as $k => $v) {
730                                                        $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID);
731
732                                                        if ($r) {
733                                                                $messages = $messages + $r;
734                                                                unset($messagesIds[$k]);       
735                                                        }
736                                                }
737
738                                        /**
739                                         * Se é uma busca por messageNumber.
740                                         * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro.
741                                         */
742                                        } else {
743                                                $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
744                                        }
745
746                                        /**
747                                         * Se é uma busca por messageId, deve ser comparado com os messageNumbers
748                                         * passados no filtro, se houverem.
749                                         */
750                                        if (!empty($map['messageNumber'])) {
751                                                foreach ($messages as $k => $m)
752                                                        if (!in_array($m, $map['messageNumber']))
753                                                                unset($messages[$k]);
754                                        }
755
756                                        $s = true;
757                                        foreach ($messages as $k => $m) {                                               
758                                                $s = imap_clearflag_full($this->mbox, $m, '$Followupflagged', ST_UID) && $s;
759                                        }
760
761                                        /**
762                                         * Se é uma busca por messageId e todos os messageIds foram econstrados:
763                                         * Stop searching in all folders
764                                         */
765                                        if (!empty($map['messageId']) && empty($messagesIds))
766                                                break;
767                                }
768                               
769                                return $s;
770                        }
771                }
772
773                //TODO - return
774        }
775
776    public function deleteAll( $URI, $justthese = false, $criteria = false )
777    {
778                /**
779                 * TODO - implementar a deleção de todos os followupflaggeds conforme filtro
780                 */
781        }
782
783    public function update( $URI, $data, $criteria = false )
784    {
785                /**
786                 * Os únicos atributos da sinalização presentes no IMAP são folderName, messageNumber e messageId,
787                 * porém a operação de update desses atributos não faz sentido para o usuário da DataLayer,
788                 * pois na prática elas são executadas através das operações de CREATE e DELETE.
789                 * Assim, para os conceitos "labeled" e "followupflagged", só faz sentido o update de
790                 * atributos gravados no banco de dados e nunca no IMAP.
791                 */
792        }
793
794//     public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
795//     {
796//                      return $this->read( array( 'id' => $id,
797//                          'concept' => $concept,
798//                          'context' => $parents ), $justthese );
799//     }
800
801    public function replace( $URI, $data, $criteria = false )
802    {}
803
804    public function close()
805    {}
806
807    public function setup()
808    {}
809
810    public function commit( $uri )
811    { return( true ); }
812
813    public function rollback( $uri )
814    {}
815
816    public function begin( $uri )
817    {}
818
819
820    public function teardown()
821    {}
822
823    function to_utf8($in)
824    {
825                if (is_array($in)) {
826                        foreach ($in as $key => $value) {
827                                $out[$this->to_utf8($key)] = $this->to_utf8($value);
828                        }
829                } elseif(is_string($in)) {
830                                return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
831                } else {
832                        return $in;
833                }
834                return $out;
835    }
836       
837           
838    private static function parseFilter($filter ,&$map){
839               
840                if( !is_array( $filter ) || count($filter) <= 0) return null;
841                                       
842                $op = array_shift( $filter );
843                switch(strtolower($op))
844                {
845                        case 'and': {
846                                foreach ($filter as $term)
847                                        self::parseFilter($term ,&$map);
848                                return;
849                        }
850                        case 'in': {
851                                if(is_array($map[$filter[0]]) && is_array($filter[1]))
852                                        $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1]));
853                                return;
854                        }
855                        case '=': {
856                                $map[$filter[0]][] = $filter[1];
857                        }
858                }
859        }
860
861}
Note: See TracBrowser for help on using the repository browser.