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

Revision 5826, 21.4 KB checked in by douglasz, 12 years ago (diff)

Ticket #2486 - Correção dos sinalizadore e marcadore na tela de anexar mensagens.

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                //require_once dirname(__FILE__) . '/../api/controller.php';
22                $context = $justthese['context'];
23                $URI = $URI['concept'];
24
25                switch( $URI )
26                {
27                        case 'folder':
28                        {
29                                $result = $this->to_utf8($this->get_folders_list());
30
31                                foreach ($result as $res) {
32
33                                        $response[] = array(
34                                                        'id' => $res['folder_id'],
35                                                        'commonName' => $res['folder_name'],
36                                                        'parentFolder' => $res['folder_parent'],
37                                                        'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
38                                                );
39                                }
40
41                                return $response;
42                        }
43                        case 'message':
44                        {
45                                //begin: for grid       
46                                $page  = $criteria['page']; //{1}    get the requested page
47                                $limit = $criteria['rows']; //{10}   get how many rows we want to have into the grid
48                                $sidx  = $criteria['sidx']; //{id}   get index row - i.e. user click to sort
49                                $sord  = $criteria['sord']; //{desc} get the direction
50
51                                $filter = $criteria['filter'];
52
53                                if( !$sidx ) $sidx = 1;
54
55                                $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] );
56                               
57                                $count = imap_num_msg( $this->open_mbox( $folder_name ) );
58
59                                $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
60
61                                if( $page > $total_pages )
62                                        $page = $total_pages;
63
64                                $start = $limit * $page - $limit;
65
66                                // do not put $limit*($page - 1)
67                                //end: for grid
68
69                                if( $filter )
70                                {
71                                        if( $filter[0] !== 'msgNumber' )
72                                        {
73                                        for( $i = 0; $i < count($filter); $i++ )
74                                        {
75                                                if( count( $filter[$i] ) === 4 )
76                                                $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' );
77
78                                                $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] );
79                                        }
80
81                                        return $this->searchSieveRule($criteria);
82                                        }
83
84                                        $msgNumber = array();
85
86                                        for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ )
87                                          $msgNumber[] = $filter[2][$i];
88
89                                        if( empty( $msgNumber ) )
90                                        return( false );
91
92                                        $result = $this->get_info_msgs( array( 'folder' => $folder_name,
93                                                                           'msgs_number' => implode( ',', $msgNumber ) ) );
94
95                                        foreach( $result as $i => $val )
96                                                $result[$i] = unserialize( $val );
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('Unseen'=> 1,  'Answered'=> 1, 'Forwarded'=> 1, 'Flagged'=> 1, 'Recent'=> 1, 'Draft'=> 1 );
118
119                                        foreach ($flags_enum as $key => $flag)
120                                        {
121                                                if ( !isset($result[$i][$key]) || !trim($result[$i][$key]) || trim($result[$i][$key]) == '')
122                                                        $flags_enum[$key] = 0;
123                                                        //unset($flags_enum[$key]);
124
125                                                unset($result[$i][$flag]);
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]['folder'] = $folder_name;
134                                               
135                                                //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 );
136                                                unset($response["rows"][$i]['Size']);
137                                        }
138                                 }                                       
139                                return $this->to_utf8($response);
140                        }
141                       
142                        /**
143                         * Filtros suportados:
144                         * - ['=', 'folderName', $X]
145                         * - [
146                         *              'AND',
147                         *              [
148                         *                      'AND',
149                         *                      ['=', 'folderName', $X],
150                         *                      ['IN', 'messageNumber', $Ys]
151                         *              ],
152                         *              ['IN', 'labelId', $Zs]
153                         * ]
154                         * - ['=', 'labelId', $X]
155                         * - [
156                         *              'AND',
157                         *              ['=', 'folderName', $X],
158                         *              ['=', 'labelId', $Y]
159                         * ]
160                         * - ['IN', 'labelId', $Ys]
161                         * - [
162                         *              'AND',
163                         *              ['=', 'folderName', $X],
164                         *              ['IN', 'labelId', $Ys]
165                         * ]                   
166                         */
167                        case 'labeled':
168                        {
169                                $result = array ( );
170                                if (isset($criteria["filter"]) && is_array($criteria['filter'])) {
171                                        //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor
172                                        $map = array(
173                                                'id' => array(),
174                                                'folderName' => array(),
175                                                'messageNumber' => array(),
176                                                'labelId' => array()
177                                        );
178                                       
179                                        self::parseFilter($criteria["filter"], &$map);
180                                       
181                                        if (count($map['folderName']) == 0) {
182                                                $folders = $this->get_folders_list();
183                                                foreach ($folders as $folder)
184                                                        if (isset($folder['folder_id']))
185                                                                $map['folderName'][] = $folder['folder_id'];
186                                        }
187
188                                        foreach ($map['folderName'] as $folder) {
189                                                $this->mbox = $this->open_mbox($folder);
190                                               
191                                                foreach ($map['labelId'] as $label) {
192                                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$label.'"', SE_UID);
193                                                       
194                                                        foreach ($messagesLabeleds as $messageLabeled) {
195                                                                if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber']))
196                                                                        continue;
197                                                                       
198                                                                $result[] = array (
199                                                                        'id' => $folder . '/' . $messageLabeled . '#' . $label,
200                                                                        'folderName' => $folder,
201                                                                        'messageNumber' => $messageLabeled,
202                                                                        'labelId' => $label
203                                                                );
204                                                        }
205                                                }
206                                               
207                                                imap_close($this->mbox);
208                                                $this->mbox = false;
209                                        }
210                                }
211                               
212                                return $result;
213                        }
214                       
215                        case 'followupflagged':
216                        {
217                       
218                                $result = array ( );
219                                if (isset($criteria["filter"]) && is_array($criteria['filter'])) {
220                                        //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor
221                                        $map = array(
222                                                'id' => array(),
223                                                'folderName' => array(),
224                                                'messageNumber' => array()
225                                        );
226                                       
227                                        self::parseFilter($criteria["filter"], &$map);
228                                       
229                                        if (empty($map['folderName'])) {
230                                                $folders = $this->get_folders_list();
231                                                foreach ($folders as $folder)
232                                                        if (isset($folder['folder_id']))
233                                                                $map['folderName'][] = $folder['folder_id'];
234                                        }
235
236                                        foreach ($map['folderName'] as $folder) {
237                                                $this->mbox = $this->open_mbox($folder);
238                                               
239                                                /**
240                                                 * Caso não tenha sido passado id no filtro
241                                                 */
242                                                if (!$map['id']) {
243                                                        $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged"', SE_UID);
244
245                                                        foreach ($messagesFlaggeds as $messageFlagged) {
246                                                                if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber']))
247                                                                        continue;
248                                                                       
249                                                                $result[] = array (
250                                                                        'folderName' => $folder,
251                                                                        'messageNumber' => $messageFlagged
252                                                                );
253                                                        }
254                                                       
255                                                } else {
256                                                        foreach ($map['id'] as $followupflagged) {
257                                                                $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflagged.'"', SE_UID);
258                                                               
259                                                                foreach ($messagesFlaggeds as $messageFlagged) {
260                                                                        if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber']))
261                                                                                continue;
262                                                                               
263                                                                        $result[] = array (
264                                                                                'id' => $folder . '/' . $messageFlagged . '#' . $followupflagged,
265                                                                                'folderName' => $folder,
266                                                                                'messageNumber' => $messageFlagged
267                                                                        );
268                                                                }
269                                                        }
270                                                }
271                                               
272                                                imap_close($this->mbox);
273                                                $this->mbox = false;
274                                        }
275                                }
276                                               
277                                return $result;                         
278                        }
279                }
280    }
281
282    public function read( $URI, $justthese = false )
283    {
284
285                switch( $URI['concept'] )
286                {
287                        case 'message':
288                        {
289                                return $this->to_utf8(
290                                        $this->get_info_msg( array('msg_number'=>$URI['id'],
291                                        'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) )
292                                );
293                        }
294                        case 'labeled':
295                        {
296                                /**
297                                 * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId
298                                 */
299                                list($messageId, $labelId) = explode('#', $URI['id']);
300                                $folderName = basename($messageId);
301                                $messageNumber = dirname($messageId);
302                               
303                                $result = array();
304
305                                if ($folderName && $messageNumber && $labelId) {
306                                        $this->mbox = $this->open_mbox($folderName);
307                                        $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID);
308                                       
309                                        if (in_array($messageNumber, $messagesLabeleds)) {
310                                                $result = array (
311                                                        'id' => $URI['id'],
312                                                        'folderName' => $folderName,
313                                                        'messageNumber' => $messageNumber,
314                                                        'labelId' => $labelId
315                                                );
316                                        }
317                                        imap_close($this->mbox);
318                                        $this->mbox = false;
319                                }
320                               
321                                return $result;
322                        }
323                       
324                        case 'followupflagged':
325                        {
326                                /**
327                                 * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#followupflaggedId
328                                 */
329                                list($messageId, $followupflaggedId) = explode('#', $URI['id']);
330                                $folderName = dirname($messageId);
331                                $messageNumber = basename($messageId);
332                               
333                                $result = array();
334
335                                if ($folderName && $messageNumber && $followupflaggedId) {
336                                        $this->mbox = $this->open_mbox($folderName);
337                                        $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflaggedId.'"', SE_UID);
338                                       
339                                        if (in_array($messageNumber, $messagesFlaggeds)) {
340                                                $result = array (
341                                                        'id' => $URI['id'],
342                                                        'folderName' => $folderName,
343                                                        'messageNumber' => $messageNumber
344                                                );
345                                        }
346                                        imap_close($this->mbox);
347                                        $this->mbox = false;
348                                }
349                               
350                                return $result;
351                        }
352                }
353    }
354
355    public function create( $URI, $data)
356    {               
357                switch( $URI['concept'] )
358                {
359                        case 'labeled':
360                        {
361                                if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) {
362                                        $this->mbox = $this->open_mbox($data['folderName']);
363                                        imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID);
364
365                                        imap_close($this->mbox);
366                                        $this->mbox = false;
367
368                                        return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']);
369                                }
370                                return array ();
371                        }
372                        case 'followupflagged':
373                        {
374                                //deve ser gravado no banco primeiro, obtido o id e, depois, gravado no imap passando o id no parametro $data
375                                if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['id'])) {
376                                        list($messageId, $followupflaggedId) = explode('#', $data['id']);
377                                       
378                                        $this->mbox = $this->open_mbox($data['folderName']);
379                                        $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged $Followupflag' . $followupflaggedId, ST_UID);
380                                                                               
381                                        imap_close($this->mbox);
382                                        $this->mbox = false;
383
384                                        return ($s) ? $data : array();
385
386                                }
387                                return array ();
388                        }
389                       
390                        case 'message':
391                        {
392                                $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True );
393                                $return = array();
394                               
395                                require_once dirname(__FILE__) . '/../../services/class.servicelocator.php';
396                                $mailService = ServiceLocator::getService('mail');
397
398                                $msg_uid = $data['msg_id'];
399                                $body = $data['body'];
400                                $body = str_replace("%nbsp;","&nbsp;",$body);
401                                $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );                                       
402
403                                $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8");
404                                $folder = @eregi_replace("INBOX[/.]", "INBOX".$this->imap_delimiter, $folder);
405
406                                /**
407                                 * Gera e preenche o field Message-Id do header
408                                 */
409                                $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft');
410                                                               
411                                $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
412                                $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
413                                $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
414                                $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
415                               
416                                if(isset($data['input_important_message']))
417                                        $mailService->addHeaderField('Importance','High');
418
419                                if(isset($data['input_return_receipt']))
420                                        $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail'));
421
422                                $isHTML = ( isset($data['type']) && $data['type'] == 'html' )?  true : false;
423                                                                 
424                                if (!$body) $body = ' ';
425                               
426
427                                $mbox_stream = $this->open_mbox($folder);
428
429                                $attachment = json_decode($data['attachments'],TRUE);
430                               
431
432                                foreach ($attachment as &$value)
433                                {
434
435                                    if((int)$value > 0) //BD attachment
436                                    {
437                                         $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment'));
438                                         
439                                         
440                                         if($att['disposition'] == 'embedded')
441                                         {
442                                             $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body);
443                                             $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']);
444                                         }
445                                         else
446                                             $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
447                                         
448                                         unset($att);
449                                    }
450                                    else
451                                    {
452                                        $value = json_decode($value, true);
453                                       
454                                        switch ($value['type']) {
455                                            case 'imapPart':
456                                                    $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']);
457                                                    if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG
458                                                    {   
459                                                        $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body);
460                                                        $mailService->addStringImage($att['source'], $att['type'], $att['name']);
461                                                    }
462                                                    else
463                                                        $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
464                                                    unset($att);
465                                                break;
466                                              case 'imapMSG':
467                                                    $sub =  $value['name'] ? $value['name'].'.eml' :'no title.eml';
468                                                    $mbox_stream = $this->open_mbox($value['folder']);
469                                                    $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']);
470                                                    $mailService->addStringAttachment($rawmsg, $sub, 'message/rfc822', '7bit', 'attachment' );
471                                                    unset($rawmsg);
472                                                break;
473
474                                            default:
475                                                break;
476                                        }
477                                    }
478
479                                }
480                               
481                                if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body);
482 
483                                if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft"))
484                                {
485                                    $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
486                                    $return['id'] = $status->uidnext - 1;
487                                       
488                                    if($data['uidsSave'] )
489                                        $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave']));
490                       
491                                }
492                 
493                                if($mbox_stream) imap_close($mbox_stream);
494
495                                return $return;
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.