source: sandbox/2.4.1-3/prototype/services/ImapServiceAdapter.php @ 6357

Revision 6357, 25.5 KB checked in by gustavo, 12 years ago (diff)

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