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