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

Revision 6171, 25.4 KB checked in by cristiano, 12 years ago (diff)

Ticket #2314 - Reutilização da conexão IMAP

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->to_utf8($this->get_folders_list());
75
76                                foreach ($result as $res) {
77
78                                        $response[] = array(
79                                                        'id' => $res['folder_id'],
80                                                        'commonName' => $res['folder_name'],
81                                                        'parentFolder' => $res['folder_parent'],
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 = $criteria['filter'];
97
98                                if( !$sidx ) $sidx = 1;
99
100                                $folder_name =  isset($URI['folder']) ?  $URI['folder'] : str_replace( '.', $this->imap_delimiter, $context );
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 ($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 "$Label'.$label.'"', SE_UID);
285                                                       
286                                                        foreach ($messagesLabeleds as $messageLabeled) {
287                                                                if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber']))
288                                                                        continue;
289                                                                       
290                                                                $result[] = array (
291                                                                        'id' => $folder . '/' . $messageLabeled . '#' . $label,
292                                                                        'folderName' => $folder,
293                                                                        'messageNumber' => $messageLabeled,
294                                                                        'labelId' => $label
295                                                                );
296                                                        }
297                                                }
298
299                                        }
300                                }
301                               
302                                return $result;
303                        }
304                       
305                        case 'followupflagged':
306                        {                                       
307                                $result = array ( );
308
309                                $map = array(
310                                        //'id' => array(),
311                                        'folderName' => array(),
312                                        'messageNumber' => array(),
313                                        'messageId' => array()
314                                );
315                               
316                                self::parseFilter($criteria["filter"], &$map);
317       
318                                if (empty($map['folderName'])) {
319                                        $folders = $this->get_folders_list();
320                                        foreach ($folders as $folder)
321                                                if (isset($folder['folder_id']))
322                                                        $map['folderName'][] = $folder['folder_id'];
323                                }
324                               
325                                $messagesIds = $map['messageId'];
326
327                                foreach ($map['folderName'] as $folder) {
328                                        $messages = array();
329                                       
330                                        $this->mbox = $this->open_mbox($folder);
331
332                                        /**
333                                         * Se é uma busca por messageId
334                                         */
335                                        if (!empty($map['messageId'])) {
336                                                       
337                                                foreach ($messagesIds as $k => $v) {
338
339                                                        $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID);
340
341                                                        if ($r) {
342
343                                                                $messages = array_merge($messages, $r);
344                                                                unset($messagesIds[$k]);
345                                                               
346                                                        }
347                                                }
348
349                                        /**
350                                         * Se é uma busca por messageNumber.
351                                         * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro.
352                                         */
353                                        } else {
354                                                $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
355                                        }
356
357                                        /**
358                                         * Se é uma busca por messageId, deve ser comparado com os messageNumbers
359                                         * passados no filtro, se houverem.
360                                         */
361                                        if (!empty($map['messageNumber']) && is_array($messages)) {
362                                                                                               
363                                                foreach ($messages as $k => $m)
364                                                        if (!in_array($m, $map['messageNumber']))
365                                                                unset($messages[$k]);
366                                        }
367
368                                        /**
369                                         * Adicionar demais atributos às mensagens para retorno
370                                         */
371                                        if(is_array($messages))
372                                        foreach ($messages as $k => $m) {
373                                                $headers = imap_fetch_overview($this->mbox, $m, FT_UID);
374
375                                                $result[] = array (
376                                                        'messageId' => $headers[0]->message_id,
377                                                        'messageNumber' => $m,
378                                                        'folderName' => $folder
379                                                );
380                                        }
381
382                                       
383                                        /**
384                                         * Se é uma busca por messageId e todos os messageIds foram econstrados:
385                                         * Stop searching in all folders
386                                         */
387                                        if (!empty($map['messageId']) && empty($messagesIds))
388                                                break;
389                                }
390                               
391
392                                return $result;
393                               
394                        } //CASE 'followupflag'
395                }
396    }
397
398    public function read( $URI, $justthese = false )
399    {
400
401                switch( $URI['concept'] )
402                {
403                        case 'message':
404                        {
405                                return $this->to_utf8(
406                                        $this->get_info_msg( array('msg_number'=>$URI['id'],
407                                        'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) )
408                                );
409                        }
410                        case 'labeled':
411                        {
412                                /**
413                                 * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId
414                                 */
415                                list($messageId, $labelId) = explode('#', $URI['id']);
416                                $folderName = basename($messageId);
417                                $messageNumber = dirname($messageId);
418                               
419                                $result = array();
420
421                                if ($folderName && $messageNumber && $labelId) {
422                                        $this->mbox = $this->open_mbox($folderName);
423                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID);
424                                       
425                                        if (in_array($messageNumber, $messagesLabeleds)) {
426                                                $result = array (
427                                                        'id' => $URI['id'],
428                                                        'folderName' => $folderName,
429                                                        'messageNumber' => $messageNumber,
430                                                        'labelId' => $labelId
431                                                );
432                                        }
433
434                                }
435                               
436                                return $result;
437                        }
438                       
439                        case 'followupflagged':
440                        {
441                       
442                                /**
443                                 * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>"
444                                 */
445                                $folderName = $messageNumber = false;
446                                if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) {
447                                        $folderName = dirname($URI['id']);
448                                        $messageNumber = basename($URI['id']);
449                                }
450
451                                $result = array();
452                                if ($folderName && $messageNumber) {
453
454                                        $this->mbox = $this->open_mbox($folderName);
455                                        $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
456
457                                        if (in_array($messageNumber, $r)) {
458                                                $headers = imap_fetch_overview($this->mbox, $messageNumber, FT_UID);
459                                                       
460                                                $result = array (
461                                                        'messageId' => $headers[0]->message_id,
462                                                        'messageNumber' => $messageNumber,
463                                                        'folderName' => $folderName
464                                                );
465                                        }
466                               
467                                } else {
468                                        /**
469                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela,
470                                         * senão busca em todas.
471                                         */
472                                       
473                                        $folders = array ();
474                                        if ($folderName) {
475                                                $folders = array ($folderName);
476                                        } else {
477                                                $folder_list = $this->get_folders_list();
478                                                foreach ($folder_list as $folder)
479                                                        if (isset($folder['folder_id']))
480                                                                $folders[] = $folder['folder_id'];
481                                        }
482                                       
483                                        foreach ($folders as $folder) {
484                                               
485                                                $this->mbox = $this->open_mbox($folder);
486                                               
487                                                if ($messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$URI['id'].'"', SE_UID)) {
488                               
489                                                        $result = array (
490                                                                'messageId' => $URI['id'],
491                                                                'messageNumber' => $messages[0],
492                                                                'folderName' => $folder
493                                                        );
494                                                       
495                                                        /**
496                                                         * Stop searching in all folders
497                                                         */
498                                                        break;
499                                                }
500       
501                                        }
502                                }
503                               
504                               
505                                return $result;
506                        }
507                }
508    }
509
510    public function create($URI, &$data)
511    {               
512                switch( $URI['concept'] )
513                {
514                        case 'labeled':
515                        {
516                                if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) {
517                                        $this->mbox = $this->open_mbox($data['folderName']);
518                                        imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID);
519
520                                        return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']);
521                                }
522                                return array ();
523                        }
524                        case 'followupflagged':
525                        {
526                                //deve ser gravado primeiro no imap, obtido o message-id e, depois gravado no banco
527                               
528                                if (isset($data['folderName']) && isset($data['messageNumber'])) {
529                                       
530                                        $this->mbox = $this->open_mbox($data['folderName']);
531                                        $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged', ST_UID);
532                                       
533                                        $headers = imap_fetch_overview($this->mbox, $data['messageNumber'], FT_UID);
534                                       
535                                        $data['messageId'] = $headers[0]->message_id;
536                                                       
537
538                                        return ($s) ? $data : array();
539
540                                } else if (isset($data['messageId'])) {
541                                        /**
542                                         * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela,
543                                         * senão busca em todas.
544                                         */
545                                        $folders = array ();
546                                        if (isset($data['folderName'])) {
547                                                $folders = array ($data['folderName']);
548                                        } else {
549                                                $folder_list = $this->get_folders_list();
550                                                foreach ($folder_list as $folder)
551                                                        if (isset($folder['folder_id']))
552                                                                $folders[] = $folder['folder_id'];
553                                        }
554                                       
555                                        foreach ($folders as $folder) {
556                                               
557                                                $this->mbox = $this->open_mbox($folder);
558                                                if ($messages = imap_search($this->mbox, 'ALL TEXT "Message-Id: '.$data['messageId'].'"', SE_UID)) {
559                                                       
560                                                        $s = imap_setflag_full($this->mbox, $messages[0], '$Followupflagged', ST_UID);
561                                               
562                                                        /**
563                                                         * Stop searching in all folders
564                                                         */
565                                                        return $data;
566                                                }
567                                               
568                                        }
569                                }
570                                return array ();
571                        }
572                       
573                        case 'message':
574                        {
575                                require_once ROOTPATH.'/library/uuid/class.uuid.php';
576                               
577                                $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True );
578                                $return = array();
579
580                                require_once dirname(__FILE__) . '/../../services/class.servicelocator.php';
581                                $mailService = ServiceLocator::getService('mail');
582
583                                $msg_uid = $data['msg_id'];
584                                $body = $data['body'];
585                                $body = str_replace("%nbsp;","&nbsp;",$body);
586                                $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );                                       
587
588                                $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8");
589                                $folder = @preg_replace('/INBOX[\/.]/i', "INBOX".$this->imap_delimiter, $folder);
590
591                                /**
592                                * Gera e preenche o field Message-Id do header
593                                */
594                                $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft');
595
596                                $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
597                                $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
598                                $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
599                                $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
600                               
601                                if(isset($data['input_important_message']))
602                                        $mailService->addHeaderField('Importance','High');
603
604                                if(isset($data['input_return_receipt']))
605                                        $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail'));
606
607                                $isHTML = ( isset($data['type']) && $data['type'] == 'html' )?  true : false;
608
609                                if (!$body) $body = ' ';
610
611
612                                $mbox_stream = $this->open_mbox($folder);
613
614                                $attachment = json_decode($data['attachments'],TRUE);
615
616
617                                foreach ($attachment as &$value)
618                                {
619                                        if((int)$value > 0) //BD attachment
620                                        {
621                                                $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment'));
622
623                                                if($att['disposition'] == 'embedded')
624                                                {
625                                                        $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body);
626                                                        $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']);
627                                                }
628                                                else
629                                                        $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
630
631                                                unset($att);
632                                        }
633                                        else
634                                        {
635                                                $value = json_decode($value, true);
636
637                                                switch ($value['type']) {
638                                                        case 'imapPart':
639                                                                $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']);
640                                                                if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG
641                                                                {   
642                                                                        $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body);
643                                                                        $mailService->addStringImage($att['source'], $att['type'], $att['name']);
644                                                                }
645                                                                else
646                                                                        $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
647                                                                unset($att);
648                                                                break;
649                                                        case 'imapMSG':
650                                                                $sub =  $value['name'] ? $value['name'].'.eml' :'no title.eml';
651                                                                $mbox_stream = $this->open_mbox($value['folder']);
652                                                                $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']);
653                                                                $mailService->addStringAttachment($rawmsg, $sub, 'message/rfc822', '7bit', 'attachment' );
654                                                                unset($rawmsg);
655                                                                break;
656
657                                                        default:
658                                                        break;
659                                                }
660                                        }
661
662                                }
663
664                                if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body);
665
666                                if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft"))
667                                {
668                                        $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
669                                        $return['id'] = $status->uidnext - 1;
670
671                                        if($data['uidsSave'] )
672                                                $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave']));
673                                }
674
675                                return $return;
676                        }
677                }
678        }
679
680    public function delete( $URI, $justthese = false, $criteria = false )
681    {
682                switch( $URI['concept'] )
683                {
684                        case 'labeled':
685                        {
686                                list($messageId, $labelId) = explode('#', $URI['id']);
687                                $folderName = dirname($messageId);
688                                $messageNumber = basename($messageId);
689
690                                if ($folderName && $messageNumber && $labelId) {
691                                        $this->mbox = $this->open_mbox($folderName);
692                                        imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID);
693
694                                }
695                        }
696                        case 'followupflagged':
697                        {
698                                $map = array(
699                                        'folderName' => array(),
700                                        'messageNumber' => array(),
701                                        'messageId' => array()
702                                );
703                               
704                                self::parseFilter($criteria["filter"], &$map);
705                               
706                                if (!$map['folderName']) {
707                                        $folders = array ();
708                                       
709                                        $folder_list = $this->get_folders_list();
710                                        foreach ($folder_list as $folder)
711                                                if (isset($folder['folder_id']))
712                                                        $folders[] = $folder['folder_id'];
713                                        $map['folderName'] = $folders;
714                                }
715
716                                $messagesIds = $map['messageId'];
717
718                                foreach ($map['folderName'] as $folder) {
719                                        $messages = array();
720                                       
721                                        $this->mbox = $this->open_mbox($folder);
722                               
723                                        /**
724                                         * Se é uma busca por messageId
725                                         */
726                                        if (!empty($map['messageId'])) {
727                                                       
728                                                foreach ($messagesIds as $k => $v) {
729                                                        $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID);
730
731                                                        if ($r) {
732                                                                $messages = $messages + $r;
733                                                                unset($messagesIds[$k]);       
734                                                        }
735                                                }
736
737                                        /**
738                                         * Se é uma busca por messageNumber.
739                                         * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro.
740                                         */
741                                        } else {
742                                                $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
743                                        }
744
745                                        /**
746                                         * Se é uma busca por messageId, deve ser comparado com os messageNumbers
747                                         * passados no filtro, se houverem.
748                                         */
749                                        if (!empty($map['messageNumber'])) {
750                                                foreach ($messages as $k => $m)
751                                                        if (!in_array($m, $map['messageNumber']))
752                                                                unset($messages[$k]);
753                                        }
754
755                                        $s = true;
756                                        foreach ($messages as $k => $m) {                                               
757                                                $s = imap_clearflag_full($this->mbox, $m, '$Followupflagged', ST_UID) && $s;
758                                        }
759
760                                        /**
761                                         * Se é uma busca por messageId e todos os messageIds foram econstrados:
762                                         * Stop searching in all folders
763                                         */
764                                        if (!empty($map['messageId']) && empty($messagesIds))
765                                                break;
766                                }
767                               
768                                return $s;
769                        }
770                }
771
772                //TODO - return
773        }
774
775    public function deleteAll( $URI, $justthese = false, $criteria = false )
776    {
777                /**
778                 * TODO - implementar a deleção de todos os followupflaggeds conforme filtro
779                 */
780        }
781
782    public function update( $URI, $data, $criteria = false )
783    {
784                /**
785                 * Os únicos atributos da sinalização presentes no IMAP são folderName, messageNumber e messageId,
786                 * porém a operação de update desses atributos não faz sentido para o usuário da DataLayer,
787                 * pois na prática elas são executadas através das operações de CREATE e DELETE.
788                 * Assim, para os conceitos "labeled" e "followupflagged", só faz sentido o update de
789                 * atributos gravados no banco de dados e nunca no IMAP.
790                 */
791        }
792
793//     public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
794//     {
795//                      return $this->read( array( 'id' => $id,
796//                          'concept' => $concept,
797//                          'context' => $parents ), $justthese );
798//     }
799
800    public function replace( $URI, $data, $criteria = false )
801    {}
802
803    public function close()
804    {}
805
806    public function setup()
807    {}
808
809    public function commit( $uri )
810    { return( true ); }
811
812    public function rollback( $uri )
813    {}
814
815    public function begin( $uri )
816    {}
817
818
819    public function teardown()
820    {}
821
822    function to_utf8($in)
823    {
824                if (is_array($in)) {
825                        foreach ($in as $key => $value) {
826                                $out[$this->to_utf8($key)] = $this->to_utf8($value);
827                        }
828                } elseif(is_string($in)) {
829                                return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
830                } else {
831                        return $in;
832                }
833                return $out;
834    }
835       
836           
837    private static function parseFilter($filter ,&$map){
838               
839                if( !is_array( $filter ) || count($filter) <= 0) return null;
840                                       
841                $op = array_shift( $filter );
842                switch(strtolower($op))
843                {
844                        case 'and': {
845                                foreach ($filter as $term)
846                                        self::parseFilter($term ,&$map);
847                                return;
848                        }
849                        case 'in': {
850                                if(is_array($map[$filter[0]]) && is_array($filter[1]))
851                                        $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1]));
852                                return;
853                        }
854                        case '=': {
855                                $map[$filter[0]][] = $filter[1];
856                        }
857                }
858        }
859
860}
Note: See TracBrowser for help on using the repository browser.