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

Revision 5416, 5.2 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2463 - Correção no imap service da nova API.

Line 
1<?php
2
3include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php";
4
5class ImapServiceAdapter extends imap_functions/* implements Service*/
6{
7    public function open( $config )
8    {
9        $this->init();
10    }
11
12//     public function connect( $config )
13//     {
14//      $this->init();
15//     }
16       
17    public function find( $URI, $justthese = false, $criteria = false )
18        {
19        $context = $justthese['context'];
20            $URI = $URI['concept'];
21
22        switch( $URI )
23        {
24            case 'folder':
25            {
26                $result = $this->to_utf8($this->get_folders_list());
27
28                foreach ($result as $res) {
29
30                    $response[] = array(
31                                    'id' => $res['folder_id'],
32                                    'commonName' => $res['folder_name'],
33                                    'parentFolder' => $res['folder_parent'],
34                                    'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
35                            );
36                }
37
38                break;
39            }
40            case 'message':
41            {
42                //begin: for grid       
43                $page  = $criteria['page']; //{1}    get the requested page
44                $limit = $criteria['rows']; //{10}   get how many rows we want to have into the grid
45                $sidx  = $criteria['sidx']; //{id}   get index row - i.e. user click to sort
46                $sord  = $criteria['sord']; //{desc} get the direction
47
48                $filter = $criteria['filter'];
49
50                if( !$sidx ) $sidx = 1;
51
52                $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] );
53               
54                $count = imap_num_msg( $this->open_mbox( $folder_name ) );
55
56                $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
57
58                if( $page > $total_pages )
59                    $page = $total_pages;
60
61                $start = $limit * $page - $limit;
62
63                // do not put $limit*($page - 1)
64                //end: for grid
65
66                if( $filter )
67                {
68                    if( $filter[0] !== 'msgNumber' )
69                    {
70                        for( $i = 0; $i < count($filter); $i++ )
71                        {
72                            if( count( $filter[$i] ) === 4 )
73                                $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' );
74
75                            $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] );
76                        }
77
78                        return $this->searchSieveRule($criteria);
79                    }
80
81                    $msgNumber = array();
82
83                    for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ )
84                          $msgNumber[] = $filter[2][$i];
85
86                    if( empty( $msgNumber ) )
87                        return( false );
88
89                    $result = $this->get_info_msgs( array( 'folder' => $folder_name,
90                                                           'msgs_number' => implode( ',', $msgNumber ) ) );
91
92                    foreach( $result as $i => $val )
93                        $result[$i] = unserialize( $val );
94
95                }
96                else
97                {
98                    $result = $this->get_range_msgs2( array( 'folder' => $folder_name, //INBOX
99                                         'msg_range_begin' => $start + 1, //??
100                                         'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid
101                                         'sort_box_type' => 'SORTARRIVAL',
102                                         'search_box_type' => 'ALL',
103                                                             'sort_box_reverse' => 1 ) );
104                }
105                //return var_export($result);
106
107                $response = array( "page" => $page, "total" => $total_pages, "records" => $count );
108               
109                for ($i=0; $i<count($result); $i++)
110                {
111                    $flags_enum = array('Recent', 'Unseen',  'Answered',  'Draft',  'Deleted', 'Flagged');
112
113                    foreach ($flags_enum as $key => $flag)
114                    {
115                        if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '')
116                                unset($flags_enum[$key]);
117                        unset($result[$i][$flag]);
118                    }
119
120                    if (array_key_exists($i, $result))
121                    {
122                                $response["rows"][$i] = $result[$i];
123                        $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 );
124                                $response["rows"][$i]['flags'] = implode(',', $flags_enum);
125                                $response["rows"][$i]['size'] = $response["rows"][$i]['Size'];
126//                      $response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 );
127                                unset($response["rows"][$i]['Size']);
128                    }
129                 }
130
131                $response = $this->to_utf8($response);
132
133                break;
134            }
135        }
136        return $response;
137    }
138
139//     public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
140//     {
141//      return $this->read( array( 'id' => $id,
142//                          'concept' => $concept,
143//                          'context' => $parents ), $justthese );
144//     }
145
146    public function read( $URI, $justthese = false )
147    {
148
149        return $this->to_utf8( $this->get_info_msg( array('msg_number'=>$URI['id'],
150                                                                                                          'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) ) );
151    }
152
153    public function create( $URI, $data )
154    {}
155
156    public function delete( $URI, $justthese = false, $criteria = false )
157    {}
158
159    public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar
160    {}
161
162    public function update( $URI, $data, $criteria = false )
163    {}
164
165    public function replace( $URI, $data, $criteria = false )
166    {}
167
168    public function close()
169    {}
170
171    public function setup()
172    {}
173
174    public function commit( $uri )
175    { return( true ); }
176
177    public function rollback( $uri )
178    {}
179
180    public function begin( $uri )
181    {}
182
183
184    public function teardown()
185    {}
186
187    function to_utf8($in)
188    {
189        if (is_array($in)) {
190                foreach ($in as $key => $value) {
191                        $out[$this->to_utf8($key)] = $this->to_utf8($value);
192                }
193        } elseif(is_string($in)) {
194                        return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
195        } else {
196                return $in;
197        }
198        return $out;
199    }
200
201}
Note: See TracBrowser for help on using the repository browser.