1 : <?php
2 :
3 : class 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 0 : $this->secured();
18 :
19 0 : $response = new Response($request);
20 0 : $response->code = Response::OK;
21 0 : $response->addHeader('Content-type', 'aplication/json');
22 :
23 0 : $cyrus = Config::service('Cyrus', 'config');
24 0 : $options = ($cyrus['tlsEncryption']) ? '/tls/novalidate-cert' : '/notls/novalidate-cert';
25 0 : $mbox = imap_open( '{'.$cyrus['host'].":".$cyrus['port'].$options.'}INBOX' , Config::me('uid') , Config::me('password') );
26 :
27 0 : $msgIds = imap_sort( $mbox , SORTDATE , 1 , null , "UNSEEN" , 'UTF-8');
28 0 : $msgIds = array_splice($msgIds , 0 , 20);
29 :
30 0 : $return = array();
31 :
32 0 : foreach ($msgIds as $key => &$value)
33 : {
34 0 : $header = imap_headerinfo( $mbox, $value );
35 0 : $return[$key]['subject'] = ( isset($header->subject) && trim($header->subject) !== '' ) ? self::decodeMimeString($header->subject) : 'No Subject';
36 0 : $return[$key]['date'] = $header->udate;
37 0 : $return[$key]['from'] = (isset( $header->from[0] )) ? self::formatMailObject( $header->from[0] ) : array( 'name' => '' , 'mail' => '');
38 0 : }
39 :
40 0 : $response->body = json_encode($return);
41 :
42 0 : imap_close($mbox);
43 0 : 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 0 : $str = (strtolower($mathes[2]) == 'q') ? quoted_printable_decode(str_replace('_','=20',$mathes[3])) : base64_decode( $mathes[3]) ;
61 0 : 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 0 : $string = preg_replace('/\?\=(\s)*\=\?/', '?==?', $string);
77 0 : 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 0 : $return = array();
93 0 : $return['mail'] = self::decodeMimeString($obj->mailbox) . (( isset( $obj->host) && $obj->host != ('unspecified-domain') && $obj->host != '.SYNTAX-ERROR.')? '@'. $obj->host : '');
94 0 : $return['name'] = ( isset( $obj->personal ) && trim($obj->personal) !== '' ) ? self::decodeMimeString($obj->personal) : $return['mail'];
95 0 : return $return;
96 : }
|