source: sandbox/2.4-expresso-api/prototype/rest/mail/MailLastResource.php @ 5888

Revision 5888, 3.8 KB checked in by cristiano, 12 years ago (diff)

Ticket #2598 - implementação base REST + oauth

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