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

Revision 5711, 21.2 KB checked in by marcieli, 12 years ago (diff)

Ticket #2486 - Correção na adequação dos ids dos followupflagged.

Line 
1<?php
2
3include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php";
4require_once ROOTPATH.'/library/uuid/class.uuid.php';
5
6
7class ImapServiceAdapter extends imap_functions/* implements Service*/
8{
9    public function open( $config )
10    {
11                $this->init();
12    }
13
14//     public function connect( $config )
15//     {
16//                      $this->init();
17//     }
18       
19    public function find( $URI, $justthese = false, $criteria = false )
20        {
21                $context = $justthese['context'];
22                $URI = $URI['concept'];
23
24                switch( $URI )
25                {
26                        case 'folder':
27                        {
28                                $result = $this->to_utf8($this->get_folders_list());
29
30                                foreach ($result as $res) {
31
32                                        $response[] = array(
33                                                        'id' => $res['folder_id'],
34                                                        'commonName' => $res['folder_name'],
35                                                        'parentFolder' => $res['folder_parent'],
36                                                        'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
37                                                );
38                                }
39
40                                return $response;
41                        }
42                        case 'message':
43                        {
44                                //begin: for grid       
45                                $page  = $criteria['page']; //{1}    get the requested page
46                                $limit = $criteria['rows']; //{10}   get how many rows we want to have into the grid
47                                $sidx  = $criteria['sidx']; //{id}   get index row - i.e. user click to sort
48                                $sord  = $criteria['sord']; //{desc} get the direction
49
50                                $filter = $criteria['filter'];
51
52                                if( !$sidx ) $sidx = 1;
53
54                                $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] );
55                               
56                                $count = imap_num_msg( $this->open_mbox( $folder_name ) );
57
58                                $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
59
60                                if( $page > $total_pages )
61                                        $page = $total_pages;
62
63                                $start = $limit * $page - $limit;
64
65                                // do not put $limit*($page - 1)
66                                //end: for grid
67
68                                if( $filter )
69                                {
70                                        if( $filter[0] !== 'msgNumber' )
71                                        {
72                                        for( $i = 0; $i < count($filter); $i++ )
73                                        {
74                                                if( count( $filter[$i] ) === 4 )
75                                                $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' );
76
77                                                $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] );
78                                        }
79
80                                        return $this->searchSieveRule($criteria);
81                                        }
82
83                                        $msgNumber = array();
84
85                                        for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ )
86                                          $msgNumber[] = $filter[2][$i];
87
88                                        if( empty( $msgNumber ) )
89                                        return( false );
90
91                                        $result = $this->get_info_msgs( array( 'folder' => $folder_name,
92                                                                           'msgs_number' => implode( ',', $msgNumber ) ) );
93
94                                        foreach( $result as $i => $val )
95                                        $result[$i] = unserialize( $val );
96
97                                }
98                                else
99                                {
100                                        $result = $this->get_range_msgs2(
101                                                array(
102                                                        'folder' => $folder_name, //INBOX
103                                                        'msg_range_begin' => $start + 1, //??
104                                                        'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid
105                                                        'sort_box_type' => 'SORTARRIVAL',
106                                                        'search_box_type' => 'ALL',
107                                                        'sort_box_reverse' => 1
108                                                )
109                                        );
110                                }
111                                //return var_export($result);
112
113                                $response = array( "page" => $page, "total" => $total_pages, "records" => $count );
114                               
115                                for ($i=0; $i<count($result); $i++)
116                                {
117                                        $flags_enum = array('Recent', 'Unseen',  'Answered',  'Draft',  'Deleted', 'Flagged');
118
119                                        foreach ($flags_enum as $key => $flag)
120                                        {
121                                                if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '')
122                                                        unset($flags_enum[$key]);
123
124                                                unset($result[$i][$flag]);
125                                        }
126
127                                        if (array_key_exists($i, $result))
128                                        {
129                                                $response["rows"][$i] = $result[$i];
130                                                $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 );
131                                                $response["rows"][$i]['flags'] = implode(',', $flags_enum);
132                                                $response["rows"][$i]['size'] = $response["rows"][$i]['Size'];
133                                                //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 );
134                                                unset($response["rows"][$i]['Size']);
135                                        }
136                                 }
137
138                                return $this->to_utf8($response);
139                        }
140                       
141                        /**
142                         * Filtros suportados:
143                         * - ['=', 'folderName', $X]
144                         * - [
145                         *              'AND',
146                         *              [
147                         *                      'AND',
148                         *                      ['=', 'folderName', $X],
149                         *                      ['IN', 'messageNumber', $Ys]
150                         *              ],
151                         *              ['IN', 'labelId', $Zs]
152                         * ]
153                         * - ['=', 'labelId', $X]
154                         * - [
155                         *              'AND',
156                         *              ['=', 'folderName', $X],
157                         *              ['=', 'labelId', $Y]
158                         * ]
159                         * - ['IN', 'labelId', $Ys]
160                         * - [
161                         *              'AND',
162                         *              ['=', 'folderName', $X],
163                         *              ['IN', 'labelId', $Ys]
164                         * ]                   
165                         */
166                        case 'labeled':
167                        {
168                                $result = array ( );
169                                if (isset($criteria["filter"]) && is_array($criteria['filter'])) {
170                                        //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor
171                                        $map = array(
172                                                'id' => array(),
173                                                'folderName' => array(),
174                                                'messageNumber' => array(),
175                                                'labelId' => array()
176                                        );
177                                       
178                                        self::parseFilter($criteria["filter"], &$map);
179                                       
180                                        if (count($map['folderName']) == 0) {
181                                                $folders = $this->get_folders_list();
182                                                foreach ($folders as $folder)
183                                                        if (isset($folder['folder_id']))
184                                                                $map['folderName'][] = $folder['folder_id'];
185                                        }
186
187                                        foreach ($map['folderName'] as $folder) {
188                                                $this->mbox = $this->open_mbox($folder);
189                                               
190                                                foreach ($map['labelId'] as $label) {
191                                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$label.'"', SE_UID);
192                                                       
193                                                        foreach ($messagesLabeleds as $messageLabeled) {
194                                                                if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber']))
195                                                                        continue;
196                                                                       
197                                                                $result[] = array (
198                                                                        'id' => $folder . '/' . $messageLabeled . '#' . $label,
199                                                                        'folderName' => $folder,
200                                                                        'messageNumber' => $messageLabeled,
201                                                                        'labelId' => $label
202                                                                );
203                                                        }
204                                                }
205                                               
206                                                imap_close($this->mbox);
207                                                $this->mbox = false;
208                                        }
209                                }
210                               
211                                return $result;
212                        }
213                       
214                        case 'followupflagged':
215                        {
216                       
217                                $result = array ( );
218                                if (isset($criteria["filter"]) && is_array($criteria['filter'])) {
219                                        //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor
220                                        $map = array(
221                                                'id' => array(),
222                                                'folderName' => array(),
223                                                'messageNumber' => array()
224                                        );
225                                       
226                                        self::parseFilter($criteria["filter"], &$map);
227                                       
228                                        if (empty($map['folderName'])) {
229                                                $folders = $this->get_folders_list();
230                                                foreach ($folders as $folder)
231                                                        if (isset($folder['folder_id']))
232                                                                $map['folderName'][] = $folder['folder_id'];
233                                        }
234
235                                        foreach ($map['folderName'] as $folder) {
236                                                $this->mbox = $this->open_mbox($folder);
237                                               
238                                                /**
239                                                 * Caso não tenha sido passado id no filtro
240                                                 */
241                                                if (!$map['id']) {
242                                                        $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged"', SE_UID);
243
244                                                        foreach ($messagesFlaggeds as $messageFlagged) {
245                                                                if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber']))
246                                                                        continue;
247                                                                       
248                                                                $result[] = array (
249                                                                        'folderName' => $folder,
250                                                                        'messageNumber' => $messageFlagged
251                                                                );
252                                                        }
253                                                       
254                                                } else {
255                                                        foreach ($map['id'] as $followupflagged) {
256                                                                $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflagged.'"', SE_UID);
257                                                               
258                                                                foreach ($messagesFlaggeds as $messageFlagged) {
259                                                                        if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber']))
260                                                                                continue;
261                                                                               
262                                                                        $result[] = array (
263                                                                                'id' => $folder . '/' . $messageFlagged . '#' . $followupflagged,
264                                                                                'folderName' => $folder,
265                                                                                'messageNumber' => $messageFlagged
266                                                                        );
267                                                                }
268                                                        }
269                                                }
270                                               
271                                                imap_close($this->mbox);
272                                                $this->mbox = false;
273                                        }
274                                }
275                                               
276                                return $result;                         
277                        }
278                }
279    }
280
281    public function read( $URI, $justthese = false )
282    {
283
284                switch( $URI['concept'] )
285                {
286                        case 'message':
287                        {
288                                return $this->to_utf8(
289                                        $this->get_info_msg( array('msg_number'=>$URI['id'],
290                                        'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) )
291                                );
292                        }
293                        case 'labeled':
294                        {
295                                /**
296                                 * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId
297                                 */
298                                list($messageId, $labelId) = explode('#', $URI['id']);
299                                $folderName = basename($messageId);
300                                $messageNumber = dirname($messageId);
301                               
302                                $result = array();
303
304                                if ($folderName && $messageNumber && $labelId) {
305                                        $this->mbox = $this->open_mbox($folderName);
306                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID);
307                                       
308                                        if (in_array($messageNumber, $messagesLabeleds)) {
309                                                $result = array (
310                                                        'id' => $URI['id'],
311                                                        'folderName' => $folderName,
312                                                        'messageNumber' => $messageNumber,
313                                                        'labelId' => $labelId
314                                                );
315                                        }
316                                        imap_close($this->mbox);
317                                        $this->mbox = false;
318                                }
319                               
320                                return $result;
321                        }
322                       
323                        case 'followupflagged':
324                        {
325                                /**
326                                 * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#followupflaggedId
327                                 */
328                                list($messageId, $followupflaggedId) = explode('#', $URI['id']);
329                                $folderName = dirname($messageId);
330                                $messageNumber = basename($messageId);
331                               
332                                $result = array();
333
334                                if ($folderName && $messageNumber && $followupflaggedId) {
335                                        $this->mbox = $this->open_mbox($folderName);
336                                        $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflaggedId.'"', SE_UID);
337                                       
338                                        if (in_array($messageNumber, $messagesFlaggeds)) {
339                                                $result = array (
340                                                        'id' => $URI['id'],
341                                                        'folderName' => $folderName,
342                                                        'messageNumber' => $messageNumber
343                                                );
344                                        }
345                                        imap_close($this->mbox);
346                                        $this->mbox = false;
347                                }
348                               
349                                return $result;
350                        }
351                }
352    }
353
354    public function create( $URI, $data)
355    {               
356                switch( $URI['concept'] )
357                {
358                        case 'labeled':
359                        {
360                                if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) {
361                                        $this->mbox = $this->open_mbox($data['folderName']);
362                                        imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID);
363
364                                        imap_close($this->mbox);
365                                        $this->mbox = false;
366
367                                        return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']);
368                                }
369                                return array ();
370                        }
371                        case 'followupflagged':
372                        {
373                                //deve ser gravado no banco primeiro, obtido o id e, depois, gravado no imap passando o id no parametro $data
374                                if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['id'])) {
375                                        list($messageId, $followupflaggedId) = explode('#', $data['id']);
376                                       
377                                        $this->mbox = $this->open_mbox($data['folderName']);
378                                        $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged $Followupflag' . $followupflaggedId, ST_UID);
379                                                                               
380                                        imap_close($this->mbox);
381                                        $this->mbox = false;
382
383                                        return ($s) ? $data : array();
384
385                                }
386                                return array ();
387                        }
388                       
389                        case 'message':
390                        {
391                                $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True );
392                                $return = array();
393
394                                require_once dirname(__FILE__) . '/../../services/class.servicelocator.php';
395                                $mailService = ServiceLocator::getService('mail');
396
397                                $msg_uid = $data['msg_id'];
398                                $body = $data['body'];
399                                $body = str_replace("%nbsp;","&nbsp;",$body);
400                                $body = preg_replace("/\n/"," ",$body);
401                                $body = preg_replace("/\r/","" ,$body);
402                                $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );                                       
403
404                                $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8");
405                                $folder = @eregi_replace("INBOX[/.]", "INBOX".$this->imap_delimiter, $folder);
406
407                                                                /**
408                                                                 * Gera e preenche o field Message-Id do header
409                                                                 */
410                                                                $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft');
411                                                               
412                                $mailService->addTo($data['input_to']);
413                                $mailService->addCc( $data['input_cc']);
414                                $mailService->addBcc($data['input_cco']);
415                                $mailService->setSubject($data['input_subject']);
416                                if(isset($data['input_important_message']))
417                                        $mailService->addHeaderField('Importance','High');
418
419                                if(isset($data['input_return_receipt']))
420                                        $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail'));
421
422                                $isHTML = ( ( array_key_exists( 'type', $data ) && in_array( strtolower( $data[ 'type' ] ), array( 'html', 'plain' ) ) ) ? strtolower( $data[ 'type' ] ) != 'plain' : true );
423
424                                if (!$body) $body = ' ';
425                               
426
427                                $mbox_stream = $this->open_mbox($folder);
428
429                                $attachment = json_decode($data['attachments'],TRUE);
430                               
431
432                                foreach ($attachment as &$value)
433                                {
434
435                                    if((int)$value > 0) //BD attachment
436                                    {
437                                         $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment'));
438                                         
439                                         
440                                         if($att['disposition'] == 'embedded')
441                                         {
442                                             $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body);
443                                             $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']);
444                                         }
445                                         else
446                                             $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
447                                         
448                                         unset($att);
449                                    }
450                                    else
451                                    {
452                                        $value = json_decode($value, true);
453                                       
454                                        switch ($value['type']) {
455                                            case 'imapPart':
456                                                    $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']);
457                                                    if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG
458                                                    {   
459                                                        $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body);
460                                                        $mailService->addStringImage($att['source'], $att['type'], $att['name']);
461                                                    }
462                                                    else
463                                                        $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
464                                                    unset($att);
465                                                break;
466                                              case 'imapMSG':
467                                                    $sub =  $value['name'] ? $value['name'].'.eml' :'no title.eml';
468                                                    $mbox_stream = $this->open_mbox($value['folder']);
469                                                    $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']);
470                                                    $mailService->addStringAttachment($rawmsg, $sub, 'message/rfc822', '7bit', 'attachment' );
471                                                    unset($rawmsg);
472                                                break;
473
474                                            default:
475                                                break;
476                                        }
477                                    }
478
479                                }
480                               
481                                if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body);
482 
483                                if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft"))
484                                {
485                                    $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
486                                    $return['id'] = $status->uidnext - 1;
487                                       
488                                    if($data['uidsSave'] )
489                                        $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave']));
490                       
491                                }
492                 
493                                if($mbox_stream) imap_close($mbox_stream);
494
495                                return $return;
496                        }
497                }
498        }
499
500    public function delete( $URI, $justthese = false, $criteria = false )
501    {
502                switch( $URI['concept'] )
503                {
504                        case 'labeled':
505                        {
506                                list($messageId, $labelId) = explode('#', $URI['id']);
507                                $folderName = dirname($messageId);
508                                $messageNumber = basename($messageId);
509
510                                if ($folderName && $messageNumber && $labelId) {
511                                        $this->mbox = $this->open_mbox($folderName);
512                                        imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID);
513
514                                        imap_close($this->mbox);
515                                        $this->mbox = false;
516                                }
517                        }
518                        case 'followupflagged':
519                        {
520                                list($messageId, $followupflaggedId) = explode('#', $URI['id']);
521                               
522                                $folderName = dirname($messageId);
523                                $messageNumber = basename($messageId);
524                       
525                                if ($folderName && $messageNumber && $followupflaggedId) {
526
527                                        $this->mbox = $this->open_mbox($folderName);
528                                        $f1 = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflag' . $followupflaggedId, ST_UID);
529
530                                        /**
531                                         * implementa a possibilidade de listar todas as mensagens sinalizadas através da busca
532                                         */
533                                        $f2 = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflagged', ST_UID);
534
535                                        imap_close($this->mbox, CL_EXPUNGE);
536                                        $this->mbox = false;
537                                       
538                                        return ($f1 && $f2);
539                                }
540                               
541                                return false;
542                        }
543                }
544
545                //TODO - return
546        }
547
548    public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar
549    {}
550
551    public function update( $URI, $data, $criteria = false )
552    {
553                /**
554                 * Os únicos atributos que podem ser alterados no IMAP são folderName e messageId,
555                 * porém a operação de update desses atributos não faz sentido para o usuário da DataLayer,
556                 * pois na prática elas são executadas através das operações de CREATE e DELETE.
557                 * Assim, para os conceitos "labeled" e "followupflagged", só faz sentido o update de
558                 * atributos gravados no banco de dados e nunca no IMAP.
559                 */
560        }
561
562//     public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
563//     {
564//                      return $this->read( array( 'id' => $id,
565//                          'concept' => $concept,
566//                          'context' => $parents ), $justthese );
567//     }
568
569    public function replace( $URI, $data, $criteria = false )
570    {}
571
572    public function close()
573    {}
574
575    public function setup()
576    {}
577
578    public function commit( $uri )
579    { return( true ); }
580
581    public function rollback( $uri )
582    {}
583
584    public function begin( $uri )
585    {}
586
587
588    public function teardown()
589    {}
590
591    function to_utf8($in)
592    {
593                if (is_array($in)) {
594                        foreach ($in as $key => $value) {
595                                $out[$this->to_utf8($key)] = $this->to_utf8($value);
596                        }
597                } elseif(is_string($in)) {
598                                return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
599                } else {
600                        return $in;
601                }
602                return $out;
603    }
604       
605           
606    private static function parseFilter($filter ,&$map){
607               
608                if( !is_array( $filter ) || count($filter) <= 0) return null;
609                                       
610                $op = array_shift( $filter );
611                switch(strtolower($op))
612                {
613                        case 'and': {
614                                foreach ($filter as $term)
615                                        self::parseFilter($term ,&$map);
616                                return;
617                        }
618                        case 'in': {
619                                if(is_array($map[$filter[0]]) && is_array($filter[1]))
620                                        $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1]));
621                                return;
622                        }
623                        case '=': {
624                                $map[$filter[0]][] = $filter[1];
625                        }
626                }
627        }
628
629}
Note: See TracBrowser for help on using the repository browser.