1 | <?php
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | * Copyright (C) 2012 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify it under
|
---|
7 | * the terms of the GNU Affero General Public License version 3 as published by
|
---|
8 | * the Free Software Foundation with the addition of the following permission
|
---|
9 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
|
---|
10 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
|
---|
11 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
15 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
16 | * details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Affero General Public License
|
---|
19 | * along with this program; if not, see www.gnu.org/licenses or write to
|
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
---|
21 | * MA 02110-1301 USA.
|
---|
22 | *
|
---|
23 | * This code is based on the OpenXchange Connector and on the Prognus pSync
|
---|
24 | * Connector both developed by the community and licensed under the GPL
|
---|
25 | * version 2 or above as published by the Free Software Foundation.
|
---|
26 | *
|
---|
27 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
|
---|
28 | * 6731, PTI, Edifício do Saber, 3º floor, room 306, Foz do Iguaçu - PR - Brasil or at
|
---|
29 | * e-mail address prognus@prognus.com.br.
|
---|
30 | *
|
---|
31 | * Classe de abstração que faz uma adaptação para manipulação de informações
|
---|
32 | * no IMAP a partir de vários métodos.
|
---|
33 | *
|
---|
34 | * @package Prototype
|
---|
35 | * @license http://www.gnu.org/copyleft/gpl.html GPL
|
---|
36 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
|
---|
37 | * @version 2.4
|
---|
38 | * @sponsor Caixa Econômica Federal
|
---|
39 | * @since Arquivo disponibilizado na versão 2.4
|
---|
40 | */
|
---|
41 |
|
---|
42 | include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php";
|
---|
43 |
|
---|
44 | /**
|
---|
45 | *
|
---|
46 | * @package Prototype (Mail)
|
---|
47 | * @license http://www.gnu.org/copyleft/gpl.html GPL
|
---|
48 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
|
---|
49 | * @version 2.4
|
---|
50 | * @sponsor Caixa Econômica Federal
|
---|
51 | * @since Classe disponibilizada na versão 2.4
|
---|
52 | */
|
---|
53 | class ImapServiceAdapter extends imap_functions/* implements Service*/
|
---|
54 | {
|
---|
55 | public function open( $config )
|
---|
56 | {
|
---|
57 | $this->init();
|
---|
58 | }
|
---|
59 |
|
---|
60 | // public function connect( $config )
|
---|
61 | // {
|
---|
62 | // $this->init();
|
---|
63 | // }
|
---|
64 |
|
---|
65 | public function find( $URI, $justthese = false, $criteria = false )
|
---|
66 | {
|
---|
67 |
|
---|
68 | $context = isset($justthese['context']) ? $justthese['context'] : '' ;
|
---|
69 |
|
---|
70 | switch( $URI['concept'] )
|
---|
71 | {
|
---|
72 | case 'folder':
|
---|
73 | {
|
---|
74 | $result = $this->getFolders();
|
---|
75 |
|
---|
76 | foreach ($result as $res) {
|
---|
77 |
|
---|
78 | $response[] = array(
|
---|
79 | 'id' => mb_convert_encoding( $res['folder_id'], 'UTF-8', 'UTF7-IMAP' ),
|
---|
80 | 'commonName' => mb_convert_encoding( $res['folder_name'], 'UTF-8' , 'UTF7-IMAP' ),
|
---|
81 | 'parentFolder' => mb_convert_encoding( $res['folder_parent'], 'UTF-8' , 'UTF7-IMAP' ),
|
---|
82 | 'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
|
---|
83 | );
|
---|
84 | }
|
---|
85 |
|
---|
86 | return $response;
|
---|
87 | }
|
---|
88 | case 'message':
|
---|
89 | {
|
---|
90 | //begin: for grid
|
---|
91 | $page = isset($criteria['page']) ? $criteria['page'] : 1 ; //{1} get the requested page
|
---|
92 | $limit = isset($criteria['rows']) ? $criteria['rows'] : 10 ; //{10} get how many rows we want to have into the grid
|
---|
93 | $sidx = isset($criteria['sidx']) ? $criteria['sidx'] : 0; //{id} get index row - i.e. user click to sort
|
---|
94 | $sord = isset($criteria['sord']) ? $criteria['sord'] : ''; //{desc} get the direction
|
---|
95 |
|
---|
96 | $filter = isset($criteria['filter']) ? $criteria['filter'] : '';
|
---|
97 |
|
---|
98 | if( !$sidx ) $sidx = 1;
|
---|
99 |
|
---|
100 | $folder_name = isset($URI['folder']) ? $URI['folder'] : str_replace( '.', $this->imap_delimiter, isset($context['folder']) ? $context['folder'] : 'INBOX');
|
---|
101 |
|
---|
102 | $count = imap_num_msg( $this->open_mbox( $folder_name ) );
|
---|
103 |
|
---|
104 | $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
|
---|
105 |
|
---|
106 | if( $page > $total_pages )
|
---|
107 | $page = $total_pages;
|
---|
108 |
|
---|
109 | $start = $limit * $page - $limit;
|
---|
110 | // do not put $limit*($page - 1)
|
---|
111 | //end: for grid
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Trata o caso específico de retorno do atributo messageId
|
---|
116 | *
|
---|
117 | * TODO - refazer todo a operação find do conceito message, uma vez que esta
|
---|
118 | * foi desenvolvida quando a nova API ainda era muito imatura e se encontra
|
---|
119 | * muito acoplada à estrutura de retorno esperada pelo plugin jqGrid
|
---|
120 | */
|
---|
121 | if ( $justthese )
|
---|
122 | {
|
---|
123 | if (isset($justthese[0]) && $justthese[0] == 'messageId') {
|
---|
124 | $map = array(
|
---|
125 | 'folderName' => array(),
|
---|
126 | 'messageNumber' => array()
|
---|
127 | );
|
---|
128 |
|
---|
129 | self::parseFilter($criteria["filter"], &$map);
|
---|
130 |
|
---|
131 | if (count($map['folderName']) == 0) {
|
---|
132 | $folders = $this->get_folders_list();
|
---|
133 | foreach ($folders as $folder)
|
---|
134 | if (isset($folder['folder_id']))
|
---|
135 | $map['folderName'][] = $folder['folder_id'];
|
---|
136 | }
|
---|
137 |
|
---|
138 | $result = array();
|
---|
139 | foreach ($map['folderName'] as $folder) {
|
---|
140 | $this->mbox = $this->open_mbox($folder);
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Se não foi passado messageNumber no filtro,
|
---|
144 | * busca todas as mensagens de cada pasta
|
---|
145 | */
|
---|
146 | $messages = empty($map['messageNumber']) ? '1:*' : implode(',', $map['messageNumber']);
|
---|
147 | $sequenceType = empty($map['messageNumber']) ? 0 : FT_UID;
|
---|
148 |
|
---|
149 | $headers = imap_fetch_overview($this->mbox, $messages, $sequenceType);
|
---|
150 | foreach ($headers as $h) {
|
---|
151 | if(isset($h->message_id ))
|
---|
152 | $result[] = array ( 'messageId' => $h->message_id );
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|
156 | return $result;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | if( $filter )
|
---|
161 | {
|
---|
162 | if( $filter[0] !== 'msgNumber' )
|
---|
163 | {
|
---|
164 | for( $i = 0; $i < count($filter); $i++ )
|
---|
165 | {
|
---|
166 | if( count( $filter[$i] ) === 4 )
|
---|
167 | $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' );
|
---|
168 |
|
---|
169 | $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] );
|
---|
170 | }
|
---|
171 |
|
---|
172 | return $this->searchSieveRule($criteria);
|
---|
173 | }
|
---|
174 |
|
---|
175 | $msgNumber = array();
|
---|
176 |
|
---|
177 | for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ )
|
---|
178 | $msgNumber[] = $filter[2][$i];
|
---|
179 |
|
---|
180 | if( empty( $msgNumber ) )
|
---|
181 | return( false );
|
---|
182 |
|
---|
183 | $result = $this->get_info_msgs( array( 'folder' => $folder_name,
|
---|
184 | 'msgs_number' => implode( ',', $msgNumber ) ) );
|
---|
185 |
|
---|
186 | foreach( $result as $i => $val )
|
---|
187 | $result[$i] = unserialize( $val );
|
---|
188 |
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | $result = $this->get_range_msgs2(
|
---|
193 | array(
|
---|
194 | 'folder' => $folder_name, //INBOX
|
---|
195 | 'msg_range_begin' => $start + 1, //??
|
---|
196 | 'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid
|
---|
197 | 'sort_box_type' => 'SORTARRIVAL',
|
---|
198 | 'search_box_type' => 'ALL',
|
---|
199 | 'sort_box_reverse' => 1
|
---|
200 | )
|
---|
201 | );
|
---|
202 | }
|
---|
203 | //return var_export($result);
|
---|
204 |
|
---|
205 | $response = array( "page" => $page, "total" => $total_pages, "records" => $count );
|
---|
206 |
|
---|
207 | for ($i=0; $i<count($result); $i++)
|
---|
208 | {
|
---|
209 | $flags_enum = array('Unseen'=> 1, 'Answered'=> 1, 'Forwarded'=> 1, 'Flagged'=> 1, 'Recent'=> 1, 'Draft'=> 1 );
|
---|
210 |
|
---|
211 | foreach ($flags_enum as $key => $flag)
|
---|
212 | {
|
---|
213 | if ( !isset($result[$i][$key]) || !trim($result[$i][$key]) || trim($result[$i][$key]) == '')
|
---|
214 | $flags_enum[$key] = 0;
|
---|
215 |
|
---|
216 | unset($result[$i][$flag]);
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (array_key_exists($i, $result))
|
---|
220 | {
|
---|
221 | $response["rows"][$i] = $result[$i];
|
---|
222 | $response["rows"][$i]['timestamp'] = $result[$i]['udate'] * 1000;
|
---|
223 | $response["rows"][$i]['flags'] = implode(',', $flags_enum);
|
---|
224 | $response["rows"][$i]['size'] = $response["rows"][$i]['Size'];
|
---|
225 | $response["rows"][$i]['folder'] = $folder_name;
|
---|
226 | //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() * 1000 );
|
---|
227 | unset($response["rows"][$i]['Size']);
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | return $this->to_utf8($response);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Filtros suportados:
|
---|
236 | * - ['=', 'folderName', $X]
|
---|
237 | * - [
|
---|
238 | * 'AND',
|
---|
239 | * [
|
---|
240 | * 'AND',
|
---|
241 | * ['=', 'folderName', $X],
|
---|
242 | * ['IN', 'messageNumber', $Ys]
|
---|
243 | * ],
|
---|
244 | * ['IN', 'labelId', $Zs]
|
---|
245 | * ]
|
---|
246 | * - ['=', 'labelId', $X]
|
---|
247 | * - [
|
---|
248 | * 'AND',
|
---|
249 | * ['=', 'folderName', $X],
|
---|
250 | * ['=', 'labelId', $Y]
|
---|
251 | * ]
|
---|
252 | * - ['IN', 'labelId', $Ys]
|
---|
253 | * - [
|
---|
254 | * 'AND',
|
---|
255 | * ['=', 'folderName', $X],
|
---|
256 | * ['IN', 'labelId', $Ys]
|
---|
257 | * ]
|
---|
258 | */
|
---|
259 | case 'labeled':
|
---|
260 | {
|
---|
261 | $result = array ( );
|
---|
262 | if (isset($criteria["filter"]) && is_array($criteria['filter'])) {
|
---|
263 | //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor
|
---|
264 | $map = array(
|
---|
265 | 'id' => array(),
|
---|
266 | 'folderName' => array(),
|
---|
267 | 'messageNumber' => array(),
|
---|
268 | 'labelId' => array()
|
---|
269 | );
|
---|
270 |
|
---|
271 | self::parseFilter($criteria["filter"], &$map);
|
---|
272 |
|
---|
273 | if (count($map['folderName']) == 0) {
|
---|
274 | $folders = $this->get_folders_list();
|
---|
275 | foreach ($folders as $folder)
|
---|
276 | if (isset($folder['folder_id']))
|
---|
277 | $map['folderName'][] = $folder['folder_id'];
|
---|
278 | }
|
---|
279 |
|
---|
280 | foreach ($map['folderName'] as $folder) {
|
---|
281 | $this->mbox = $this->open_mbox($folder);
|
---|
282 |
|
---|
283 | foreach ($map['labelId'] as $label) {
|
---|
284 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$label.'"', SE_UID);
|
---|
285 |
|
---|
286 | if(is_array($messagesLabeleds))
|
---|
287 | foreach ($messagesLabeleds as $messageLabeled) {
|
---|
288 | if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber']))
|
---|
289 | continue;
|
---|
290 |
|
---|
291 | $result[] = array (
|
---|
292 | 'id' => $folder . '/' . $messageLabeled . '#' . $label,
|
---|
293 | 'folderName' => $folder,
|
---|
294 | 'messageNumber' => $messageLabeled,
|
---|
295 | 'labelId' => $label
|
---|
296 | );
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | return $result;
|
---|
304 | }
|
---|
305 |
|
---|
306 | case 'followupflagged':
|
---|
307 | {
|
---|
308 | $result = array ( );
|
---|
309 |
|
---|
310 | $map = array(
|
---|
311 | //'id' => array(),
|
---|
312 | 'folderName' => array(),
|
---|
313 | 'messageNumber' => array(),
|
---|
314 | 'messageId' => array()
|
---|
315 | );
|
---|
316 |
|
---|
317 | self::parseFilter($criteria["filter"], &$map);
|
---|
318 |
|
---|
319 | if (empty($map['folderName'])) {
|
---|
320 | $folders = $this->get_folders_list();
|
---|
321 | foreach ($folders as $folder)
|
---|
322 | if (isset($folder['folder_id']))
|
---|
323 | $map['folderName'][] = $folder['folder_id'];
|
---|
324 | }
|
---|
325 |
|
---|
326 | $messagesIds = $map['messageId'];
|
---|
327 |
|
---|
328 | foreach ($map['folderName'] as $folder) {
|
---|
329 | $messages = array();
|
---|
330 |
|
---|
331 | $this->mbox = $this->open_mbox($folder);
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Se é uma busca por messageId
|
---|
335 | */
|
---|
336 | if (!empty($map['messageId'])) {
|
---|
337 |
|
---|
338 | foreach ($messagesIds as $k => $v) {
|
---|
339 |
|
---|
340 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID);
|
---|
341 |
|
---|
342 | if ($r) {
|
---|
343 |
|
---|
344 | $messages = array_merge($messages, $r);
|
---|
345 | unset($messagesIds[$k]);
|
---|
346 |
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Se é uma busca por messageNumber.
|
---|
352 | * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro.
|
---|
353 | */
|
---|
354 | } else {
|
---|
355 | $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Se é uma busca por messageId, deve ser comparado com os messageNumbers
|
---|
360 | * passados no filtro, se houverem.
|
---|
361 | */
|
---|
362 | if (!empty($map['messageNumber']) && is_array($messages)) {
|
---|
363 |
|
---|
364 | foreach ($messages as $k => $m)
|
---|
365 | if (!in_array($m, $map['messageNumber']))
|
---|
366 | unset($messages[$k]);
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Adicionar demais atributos às mensagens para retorno
|
---|
371 | */
|
---|
372 | if(is_array($messages))
|
---|
373 | foreach ($messages as $k => $m) {
|
---|
374 | $headers = imap_fetch_overview($this->mbox, $m, FT_UID);
|
---|
375 |
|
---|
376 | $result[] = array (
|
---|
377 | 'messageId' => $headers[0]->message_id,
|
---|
378 | 'messageNumber' => $m,
|
---|
379 | 'folderName' => $folder
|
---|
380 | );
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Se é uma busca por messageId e todos os messageIds foram econstrados:
|
---|
386 | * Stop searching in all folders
|
---|
387 | */
|
---|
388 | if (!empty($map['messageId']) && empty($messagesIds))
|
---|
389 | break;
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | return $result;
|
---|
394 |
|
---|
395 | } //CASE 'followupflag'
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | public function read( $URI, $justthese = false )
|
---|
400 | {
|
---|
401 |
|
---|
402 | switch( $URI['concept'] )
|
---|
403 | {
|
---|
404 | case 'message':
|
---|
405 | {
|
---|
406 | return $this->to_utf8(
|
---|
407 | $this->get_info_msg( array('msg_number'=>$URI['id'],
|
---|
408 | 'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] ) ,
|
---|
409 | 'decoded' => true ) )
|
---|
410 | );
|
---|
411 | }
|
---|
412 | case 'labeled':
|
---|
413 | {
|
---|
414 | /**
|
---|
415 | * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId
|
---|
416 | */
|
---|
417 | list($messageId, $labelId) = explode('#', $URI['id']);
|
---|
418 | $folderName = basename($messageId);
|
---|
419 | $messageNumber = dirname($messageId);
|
---|
420 |
|
---|
421 | $result = array();
|
---|
422 |
|
---|
423 | if ($folderName && $messageNumber && $labelId) {
|
---|
424 | $this->mbox = $this->open_mbox($folderName);
|
---|
425 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID);
|
---|
426 |
|
---|
427 | if (in_array($messageNumber, $messagesLabeleds)) {
|
---|
428 | $result = array (
|
---|
429 | 'id' => $URI['id'],
|
---|
430 | 'folderName' => $folderName,
|
---|
431 | 'messageNumber' => $messageNumber,
|
---|
432 | 'labelId' => $labelId
|
---|
433 | );
|
---|
434 | }
|
---|
435 |
|
---|
436 | }
|
---|
437 |
|
---|
438 | return $result;
|
---|
439 | }
|
---|
440 |
|
---|
441 | case 'followupflagged':
|
---|
442 | {
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>"
|
---|
446 | */
|
---|
447 | $folderName = $messageNumber = false;
|
---|
448 | if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) {
|
---|
449 | $folderName = dirname($URI['id']);
|
---|
450 | $messageNumber = basename($URI['id']);
|
---|
451 | }
|
---|
452 |
|
---|
453 | $result = array();
|
---|
454 | if ($folderName && $messageNumber) {
|
---|
455 |
|
---|
456 | $this->mbox = $this->open_mbox($folderName);
|
---|
457 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
|
---|
458 |
|
---|
459 | if (in_array($messageNumber, $r)) {
|
---|
460 | $headers = imap_fetch_overview($this->mbox, $messageNumber, FT_UID);
|
---|
461 |
|
---|
462 | $result = array (
|
---|
463 | 'messageId' => $headers[0]->message_id,
|
---|
464 | 'messageNumber' => $messageNumber,
|
---|
465 | 'folderName' => $folderName
|
---|
466 | );
|
---|
467 | }
|
---|
468 |
|
---|
469 | } else {
|
---|
470 | /**
|
---|
471 | * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela,
|
---|
472 | * senão busca em todas.
|
---|
473 | */
|
---|
474 |
|
---|
475 | $folders = array ();
|
---|
476 | if ($folderName) {
|
---|
477 | $folders = array ($folderName);
|
---|
478 | } else {
|
---|
479 | $folder_list = $this->get_folders_list();
|
---|
480 | foreach ($folder_list as $folder)
|
---|
481 | if (isset($folder['folder_id']))
|
---|
482 | $folders[] = $folder['folder_id'];
|
---|
483 | }
|
---|
484 |
|
---|
485 | foreach ($folders as $folder) {
|
---|
486 |
|
---|
487 | $this->mbox = $this->open_mbox($folder);
|
---|
488 |
|
---|
489 | if ($messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$URI['id'].'"', SE_UID)) {
|
---|
490 |
|
---|
491 | $result = array (
|
---|
492 | 'messageId' => $URI['id'],
|
---|
493 | 'messageNumber' => $messages[0],
|
---|
494 | 'folderName' => $folder
|
---|
495 | );
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Stop searching in all folders
|
---|
499 | */
|
---|
500 | break;
|
---|
501 | }
|
---|
502 |
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | return $result;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | public function create($URI, &$data)
|
---|
513 | {
|
---|
514 | switch( $URI['concept'] )
|
---|
515 | {
|
---|
516 | case 'labeled':
|
---|
517 | {
|
---|
518 | if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) {
|
---|
519 | $this->mbox = $this->open_mbox($data['folderName']);
|
---|
520 | imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID);
|
---|
521 |
|
---|
522 | return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']);
|
---|
523 | }
|
---|
524 | return array ();
|
---|
525 | }
|
---|
526 | case 'followupflagged':
|
---|
527 | {
|
---|
528 | //deve ser gravado primeiro no imap, obtido o message-id e, depois gravado no banco
|
---|
529 |
|
---|
530 | if (isset($data['folderName']) && isset($data['messageNumber'])) {
|
---|
531 |
|
---|
532 | $this->mbox = $this->open_mbox($data['folderName']);
|
---|
533 | $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged', ST_UID);
|
---|
534 |
|
---|
535 | $headers = imap_fetch_overview($this->mbox, $data['messageNumber'], FT_UID);
|
---|
536 |
|
---|
537 | $data['messageId'] = $headers[0]->message_id;
|
---|
538 |
|
---|
539 |
|
---|
540 | return ($s) ? $data : array();
|
---|
541 |
|
---|
542 | } else if (isset($data['messageId'])) {
|
---|
543 | /**
|
---|
544 | * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela,
|
---|
545 | * senão busca em todas.
|
---|
546 | */
|
---|
547 | $folders = array ();
|
---|
548 | if (isset($data['folderName'])) {
|
---|
549 | $folders = array ($data['folderName']);
|
---|
550 | } else {
|
---|
551 | $folder_list = $this->get_folders_list();
|
---|
552 | foreach ($folder_list as $folder)
|
---|
553 | if (isset($folder['folder_id']))
|
---|
554 | $folders[] = $folder['folder_id'];
|
---|
555 | }
|
---|
556 |
|
---|
557 | foreach ($folders as $folder) {
|
---|
558 |
|
---|
559 | $this->mbox = $this->open_mbox($folder);
|
---|
560 | if ($messages = imap_search($this->mbox, 'ALL TEXT "Message-Id: '.$data['messageId'].'"', SE_UID)) {
|
---|
561 |
|
---|
562 | $s = imap_setflag_full($this->mbox, $messages[0], '$Followupflagged', ST_UID);
|
---|
563 |
|
---|
564 | /**
|
---|
565 | * Stop searching in all folders
|
---|
566 | */
|
---|
567 | return $data;
|
---|
568 | }
|
---|
569 |
|
---|
570 | }
|
---|
571 | }
|
---|
572 | return array ();
|
---|
573 | }
|
---|
574 |
|
---|
575 | case 'message':
|
---|
576 | {
|
---|
577 | require_once ROOTPATH.'/library/uuid/class.uuid.php';
|
---|
578 |
|
---|
579 | $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True );
|
---|
580 | $return = array();
|
---|
581 |
|
---|
582 | require_once dirname(__FILE__) . '/../../services/class.servicelocator.php';
|
---|
583 | $mailService = ServiceLocator::getService('mail');
|
---|
584 |
|
---|
585 | $msg_uid = $data['msg_id'];
|
---|
586 | $body = $data['body'];
|
---|
587 | $body = str_replace("%nbsp;"," ",$body);
|
---|
588 | $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' );
|
---|
589 |
|
---|
590 | $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8");
|
---|
591 | $folder = @preg_replace('/INBOX[\/.]/i', "INBOX".$this->imap_delimiter, $folder);
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Gera e preenche o field Message-Id do header
|
---|
595 | */
|
---|
596 | $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft');
|
---|
597 |
|
---|
598 | $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
|
---|
599 | $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
|
---|
600 | $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
|
---|
601 | $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1'));
|
---|
602 |
|
---|
603 | if(isset($data['input_important_message']))
|
---|
604 | $mailService->addHeaderField('Importance','High');
|
---|
605 |
|
---|
606 | if(isset($data['input_return_receipt']))
|
---|
607 | $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail'));
|
---|
608 |
|
---|
609 | $this->rfc2397ToEmbeddedAttachment($mailService , $body);
|
---|
610 |
|
---|
611 | $isHTML = ( isset($data['type']) && $data['type'] == 'html' )? true : false;
|
---|
612 |
|
---|
613 | if (!$body) $body = ' ';
|
---|
614 |
|
---|
615 |
|
---|
616 | $mbox_stream = $this->open_mbox($folder);
|
---|
617 |
|
---|
618 | $attachment = json_decode($data['attachments'],TRUE);
|
---|
619 |
|
---|
620 |
|
---|
621 | foreach ($attachment as &$value)
|
---|
622 | {
|
---|
623 | if((int)$value > 0) //BD attachment
|
---|
624 | {
|
---|
625 | $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment'));
|
---|
626 |
|
---|
627 | if($att['disposition'] == 'embedded')
|
---|
628 | {
|
---|
629 | $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body);
|
---|
630 | $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']);
|
---|
631 | }
|
---|
632 | else
|
---|
633 | $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
|
---|
634 |
|
---|
635 | unset($att);
|
---|
636 | }
|
---|
637 | else
|
---|
638 | {
|
---|
639 | $value = json_decode($value, true);
|
---|
640 |
|
---|
641 | switch ($value['type']) {
|
---|
642 | case 'imapPart':
|
---|
643 | $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']);
|
---|
644 | if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG
|
---|
645 | {
|
---|
646 | $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body);
|
---|
647 | $mailService->addStringImage($att['source'], $att['type'], $att['name']);
|
---|
648 | }
|
---|
649 | else
|
---|
650 | $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' );
|
---|
651 | unset($att);
|
---|
652 | break;
|
---|
653 | case 'imapMSG':
|
---|
654 | $mbox_stream = $this->open_mbox($value['folder']);
|
---|
655 | $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']);
|
---|
656 | $mailService->addStringAttachment($rawmsg, base64_decode($value['name']), 'message/rfc822', '7bit', 'attachment' );
|
---|
657 | unset($rawmsg);
|
---|
658 | break;
|
---|
659 |
|
---|
660 | default:
|
---|
661 | break;
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | }
|
---|
666 |
|
---|
667 | if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body);
|
---|
668 |
|
---|
669 | if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft"))
|
---|
670 | {
|
---|
671 | $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT);
|
---|
672 | $return['id'] = $status->uidnext - 1;
|
---|
673 |
|
---|
674 | if($data['uidsSave'] )
|
---|
675 | $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave']));
|
---|
676 | }
|
---|
677 |
|
---|
678 | return $return;
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | public function delete( $URI, $justthese = false, $criteria = false )
|
---|
684 | {
|
---|
685 | switch( $URI['concept'] )
|
---|
686 | {
|
---|
687 | case 'labeled':
|
---|
688 | {
|
---|
689 | list($messageId, $labelId) = explode('#', $URI['id']);
|
---|
690 | $folderName = dirname($messageId);
|
---|
691 | $messageNumber = basename($messageId);
|
---|
692 |
|
---|
693 | if ($folderName && $messageNumber && $labelId) {
|
---|
694 | $this->mbox = $this->open_mbox($folderName);
|
---|
695 | imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID);
|
---|
696 |
|
---|
697 | }
|
---|
698 | }
|
---|
699 | case 'followupflagged':
|
---|
700 | {
|
---|
701 | $map = array(
|
---|
702 | 'folderName' => array(),
|
---|
703 | 'messageNumber' => array(),
|
---|
704 | 'messageId' => array()
|
---|
705 | );
|
---|
706 |
|
---|
707 | self::parseFilter($criteria["filter"], &$map);
|
---|
708 |
|
---|
709 | if (!$map['folderName']) {
|
---|
710 | $folders = array ();
|
---|
711 |
|
---|
712 | $folder_list = $this->get_folders_list();
|
---|
713 | foreach ($folder_list as $folder)
|
---|
714 | if (isset($folder['folder_id']))
|
---|
715 | $folders[] = $folder['folder_id'];
|
---|
716 | $map['folderName'] = $folders;
|
---|
717 | }
|
---|
718 |
|
---|
719 | $messagesIds = $map['messageId'];
|
---|
720 |
|
---|
721 | foreach ($map['folderName'] as $folder) {
|
---|
722 | $messages = array();
|
---|
723 |
|
---|
724 | $this->mbox = $this->open_mbox($folder);
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * Se é uma busca por messageId
|
---|
728 | */
|
---|
729 | if (!empty($map['messageId'])) {
|
---|
730 |
|
---|
731 | foreach ($messagesIds as $k => $v) {
|
---|
732 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID);
|
---|
733 |
|
---|
734 | if ($r) {
|
---|
735 | $messages = $messages + $r;
|
---|
736 | unset($messagesIds[$k]);
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | /**
|
---|
741 | * Se é uma busca por messageNumber.
|
---|
742 | * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro.
|
---|
743 | */
|
---|
744 | } else {
|
---|
745 | $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID);
|
---|
746 | }
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Se é uma busca por messageId, deve ser comparado com os messageNumbers
|
---|
750 | * passados no filtro, se houverem.
|
---|
751 | */
|
---|
752 | if (!empty($map['messageNumber'])) {
|
---|
753 | foreach ($messages as $k => $m)
|
---|
754 | if (!in_array($m, $map['messageNumber']))
|
---|
755 | unset($messages[$k]);
|
---|
756 | }
|
---|
757 |
|
---|
758 | $s = true;
|
---|
759 | foreach ($messages as $k => $m) {
|
---|
760 | $s = imap_clearflag_full($this->mbox, $m, '$Followupflagged', ST_UID) && $s;
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Se é uma busca por messageId e todos os messageIds foram econstrados:
|
---|
765 | * Stop searching in all folders
|
---|
766 | */
|
---|
767 | if (!empty($map['messageId']) && empty($messagesIds))
|
---|
768 | break;
|
---|
769 | }
|
---|
770 |
|
---|
771 | return $s;
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | //TODO - return
|
---|
776 | }
|
---|
777 |
|
---|
778 | public function deleteAll( $URI, $justthese = false, $criteria = false )
|
---|
779 | {
|
---|
780 | /**
|
---|
781 | * TODO - implementar a deleção de todos os followupflaggeds conforme filtro
|
---|
782 | */
|
---|
783 | }
|
---|
784 |
|
---|
785 | public function update( $URI, $data, $criteria = false )
|
---|
786 | {
|
---|
787 | /**
|
---|
788 | * Os únicos atributos da sinalização presentes no IMAP são folderName, messageNumber e messageId,
|
---|
789 | * porém a operação de update desses atributos não faz sentido para o usuário da DataLayer,
|
---|
790 | * pois na prática elas são executadas através das operações de CREATE e DELETE.
|
---|
791 | * Assim, para os conceitos "labeled" e "followupflagged", só faz sentido o update de
|
---|
792 | * atributos gravados no banco de dados e nunca no IMAP.
|
---|
793 | */
|
---|
794 | }
|
---|
795 |
|
---|
796 | // public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
|
---|
797 | // {
|
---|
798 | // return $this->read( array( 'id' => $id,
|
---|
799 | // 'concept' => $concept,
|
---|
800 | // 'context' => $parents ), $justthese );
|
---|
801 | // }
|
---|
802 |
|
---|
803 | public function replace( $URI, $data, $criteria = false )
|
---|
804 | {}
|
---|
805 |
|
---|
806 | public function close()
|
---|
807 | {}
|
---|
808 |
|
---|
809 | public function setup()
|
---|
810 | {}
|
---|
811 |
|
---|
812 | public function commit( $uri )
|
---|
813 | { return( true ); }
|
---|
814 |
|
---|
815 | public function rollback( $uri )
|
---|
816 | {}
|
---|
817 |
|
---|
818 | public function begin( $uri )
|
---|
819 | {}
|
---|
820 |
|
---|
821 |
|
---|
822 | public function teardown()
|
---|
823 | {}
|
---|
824 |
|
---|
825 | function to_utf8($in)
|
---|
826 | {
|
---|
827 | if (is_array($in)) {
|
---|
828 | foreach ($in as $key => $value) {
|
---|
829 | $out[$this->to_utf8($key)] = $this->to_utf8($value);
|
---|
830 | }
|
---|
831 | } elseif(is_string($in)) {
|
---|
832 | return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
|
---|
833 | } else {
|
---|
834 | return $in;
|
---|
835 | }
|
---|
836 | return $out;
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | private static function parseFilter($filter ,&$map){
|
---|
841 |
|
---|
842 | if( !is_array( $filter ) || count($filter) <= 0) return null;
|
---|
843 |
|
---|
844 | $op = array_shift( $filter );
|
---|
845 | switch(strtolower($op))
|
---|
846 | {
|
---|
847 | case 'and': {
|
---|
848 | foreach ($filter as $term)
|
---|
849 | self::parseFilter($term ,&$map);
|
---|
850 | return;
|
---|
851 | }
|
---|
852 | case 'in': {
|
---|
853 | if(is_array($map[$filter[0]]) && is_array($filter[1]))
|
---|
854 | $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1]));
|
---|
855 | return;
|
---|
856 | }
|
---|
857 | case '=': {
|
---|
858 | $map[$filter[0]][] = $filter[1];
|
---|
859 | }
|
---|
860 | }
|
---|
861 | }
|
---|
862 |
|
---|
863 | }
|
---|