source: trunk/prototype/rest/mail/MailLastResource.php @ 6528

Revision 6528, 3.8 KB checked in by gustavo, 12 years ago (diff)

Ticket #2766 - Merge do branch das novas funcionalidaes para o trunk

  • Property svn:executable set to *
Line 
1<?php
2 
3use prototype\api\Config as Config;
4
5class MailLastResource extends Resource {
6 
7    /**
8    * Busca as últimas 20 menssagens não lidas do usuário
9    *
10    * @license    http://www.gnu.org/copyleft/gpl.html GPL
11    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
12    * @sponsor    Caixa EconÃŽmica Federal
13    * @author     Cristiano Corrêa Schmidt
14    * @return     Lista das ultimas 10 menssagens do usuário não lidas
15    * @access     public
16    **/
17    function get($request)
18    {
19        $this->secured();
20       
21        $response = new Response($request);
22        $response->code = Response::OK;
23        $response->addHeader('Content-type', 'aplication/json');
24               
25        $cyrus = Config::service('Cyrus', 'config');
26        $options = ($cyrus['tlsEncryption']) ? '/tls/novalidate-cert' : '/notls/novalidate-cert';
27        $mbox = imap_open( '{'.$cyrus['host'].":".$cyrus['port'].$options.'}INBOX' , Config::me('uid') , Config::me('password') );
28       
29        $msgIds = imap_sort( $mbox , SORTDATE , 1 , null , "UNSEEN" , 'UTF-8');
30        $msgIds = array_splice($msgIds , 0 , 20);
31       
32        $return = array();
33       
34        foreach ($msgIds as $key => &$value)
35        {   
36              $header = imap_headerinfo( $mbox, $value );
37              $return[$key]['subject'] = ( isset($header->subject) && trim($header->subject) !== '' ) ?  self::decodeMimeString($header->subject) : 'No Subject';
38              $return[$key]['date'] =  $header->udate;
39              $return[$key]['from'] =  (isset( $header->from[0] )) ? self::formatMailObject( $header->from[0] ) : array( 'name' => '' , 'mail' => '');
40        }
41
42        $response->body = json_encode($return);
43       
44        imap_close($mbox);
45        return $response;
46    }
47
48   
49   
50    /**
51    *  Decodifica os tokens encontrados na função decodeMimeString
52    *
53    * @license    http://www.gnu.org/copyleft/gpl.html GPL
54    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
55    * @sponsor    Caixa Económica Federal
56    * @author     Cristiano Corrêa Schmidt
57    * @return     bool
58    * @access     public
59    */
60    static private function decodeMimeStringCallback( $mathes )
61    {
62       $str = (strtolower($mathes[2]) == 'q') ?  quoted_printable_decode(str_replace('_','=20',$mathes[3])) : base64_decode( $mathes[3]) ;
63       return ( strtoupper($mathes[1]) == 'ISO-8859-1' ) ? mb_convert_encoding(  $str , 'UTF-8' , 'ISO-8859-1') : $str;
64    }
65
66    /**
67    *  Decodifica uma string no formato mime RFC2047
68    *
69    * @license    http://www.gnu.org/copyleft/gpl.html GPL
70    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
71    * @sponsor    Caixa Económica Federal
72    * @author     Cristiano Corrêa Schmidt
73    * @return     bool
74    * @access     public
75    */
76    static private function decodeMimeString( $string )
77    {
78      $string =  preg_replace('/\?\=(\s)*\=\?/', '?==?', $string);
79      return preg_replace_callback( '/\=\?([^\?]*)\?([qb])\?([^\?]*)\?=/i' ,array( 'self' , 'decodeMimeStringCallback'), $string);
80    }
81   
82     /**
83    *  Formata um mailObject para um array com name e email
84    *
85    * @license    http://www.gnu.org/copyleft/gpl.html GPL
86    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
87    * @sponsor    Caixa Económica Federal
88    * @author     Cristiano Corrêa Schmidt
89    * @return     bool
90    * @access     public
91    */
92    static private function formatMailObject( $obj )
93    {
94        $return = array();
95        $return['mail'] = self::decodeMimeString($obj->mailbox) . (( isset( $obj->host) && $obj->host != ('unspecified-domain') &&  $obj->host !=  '.SYNTAX-ERROR.')? '@'. $obj->host : '');
96        $return['name'] = ( isset( $obj->personal ) && trim($obj->personal) !== '' ) ? self::decodeMimeString($obj->personal) :  $return['mail'];
97        return $return;
98    }
99}
Note: See TracBrowser for help on using the repository browser.