[5136] | 1 | <?php |
---|
[6115] | 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 | */ |
---|
[5136] | 41 | |
---|
[5341] | 42 | include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php"; |
---|
[5136] | 43 | |
---|
[6132] | 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 | */ |
---|
[5136] | 53 | class ImapServiceAdapter extends imap_functions/* implements Service*/ |
---|
| 54 | { |
---|
| 55 | public function open( $config ) |
---|
| 56 | { |
---|
[5533] | 57 | $this->init(); |
---|
[5136] | 58 | } |
---|
| 59 | |
---|
[5341] | 60 | // public function connect( $config ) |
---|
| 61 | // { |
---|
[5533] | 62 | // $this->init(); |
---|
[5341] | 63 | // } |
---|
[5188] | 64 | |
---|
[5341] | 65 | public function find( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 66 | { |
---|
[6076] | 67 | |
---|
[5533] | 68 | $context = $justthese['context']; |
---|
| 69 | $URI = $URI['concept']; |
---|
[5136] | 70 | |
---|
[5533] | 71 | switch( $URI ) |
---|
| 72 | { |
---|
| 73 | case 'folder': |
---|
| 74 | { |
---|
| 75 | $result = $this->to_utf8($this->get_folders_list()); |
---|
[5136] | 76 | |
---|
[5533] | 77 | foreach ($result as $res) { |
---|
[5136] | 78 | |
---|
[5533] | 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 | } |
---|
[5136] | 86 | |
---|
[5533] | 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 |
---|
[5136] | 96 | |
---|
[5533] | 97 | $filter = $criteria['filter']; |
---|
[5341] | 98 | |
---|
[5533] | 99 | if( !$sidx ) $sidx = 1; |
---|
[5136] | 100 | |
---|
[5533] | 101 | $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] ); |
---|
| 102 | |
---|
| 103 | $count = imap_num_msg( $this->open_mbox( $folder_name ) ); |
---|
[5136] | 104 | |
---|
[5533] | 105 | $total_pages = $count > 0 ? ceil( $count/$limit ) : 0; |
---|
[5136] | 106 | |
---|
[5533] | 107 | if( $page > $total_pages ) |
---|
| 108 | $page = $total_pages; |
---|
[5136] | 109 | |
---|
[5533] | 110 | $start = $limit * $page - $limit; |
---|
| 111 | // do not put $limit*($page - 1) |
---|
| 112 | //end: for grid |
---|
[5959] | 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); |
---|
[5136] | 142 | |
---|
[5959] | 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 | |
---|
[5533] | 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' ); |
---|
[5341] | 170 | |
---|
[5533] | 171 | $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] ); |
---|
| 172 | } |
---|
[5341] | 173 | |
---|
[5533] | 174 | return $this->searchSieveRule($criteria); |
---|
| 175 | } |
---|
[5341] | 176 | |
---|
[5533] | 177 | $msgNumber = array(); |
---|
[5341] | 178 | |
---|
[5533] | 179 | for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ ) |
---|
| 180 | $msgNumber[] = $filter[2][$i]; |
---|
[5341] | 181 | |
---|
[5533] | 182 | if( empty( $msgNumber ) ) |
---|
| 183 | return( false ); |
---|
[5341] | 184 | |
---|
[5533] | 185 | $result = $this->get_info_msgs( array( 'folder' => $folder_name, |
---|
| 186 | 'msgs_number' => implode( ',', $msgNumber ) ) ); |
---|
[5341] | 187 | |
---|
[5533] | 188 | foreach( $result as $i => $val ) |
---|
[5959] | 189 | $result[$i] = unserialize( $val ); |
---|
| 190 | |
---|
[5533] | 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); |
---|
[5959] | 206 | |
---|
| 207 | $response = array( "page" => $page, "total" => $total_pages, "records" => $count ); |
---|
[5749] | 208 | |
---|
[5533] | 209 | for ($i=0; $i<count($result); $i++) |
---|
| 210 | { |
---|
[5960] | 211 | $flags_enum = array('Unseen'=> 1, 'Answered'=> 1, 'Forwarded'=> 1, 'Flagged'=> 1, 'Recent'=> 1, 'Draft'=> 1 ); |
---|
[5533] | 212 | |
---|
| 213 | foreach ($flags_enum as $key => $flag) |
---|
| 214 | { |
---|
[5960] | 215 | if ( !isset($result[$i][$key]) || !trim($result[$i][$key]) || trim($result[$i][$key]) == '') |
---|
| 216 | $flags_enum[$key] = 0; |
---|
[5533] | 217 | |
---|
| 218 | unset($result[$i][$flag]); |
---|
[5959] | 219 | } |
---|
| 220 | |
---|
[5533] | 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']; |
---|
[5960] | 227 | $response["rows"][$i]['folder'] = $folder_name; |
---|
[5533] | 228 | //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() * 1000 ); |
---|
| 229 | unset($response["rows"][$i]['Size']); |
---|
| 230 | } |
---|
[5959] | 231 | } |
---|
| 232 | |
---|
[5533] | 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'])) { |
---|
[5540] | 265 | //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor |
---|
[5533] | 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': |
---|
[6029] | 310 | { |
---|
[5533] | 311 | $result = array ( ); |
---|
| 312 | |
---|
[5959] | 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']; |
---|
[5708] | 330 | |
---|
[5959] | 331 | foreach ($map['folderName'] as $folder) { |
---|
| 332 | $messages = array(); |
---|
| 333 | |
---|
| 334 | $this->mbox = $this->open_mbox($folder); |
---|
[6029] | 335 | |
---|
[5959] | 336 | /** |
---|
| 337 | * Se é uma busca por messageId |
---|
| 338 | */ |
---|
| 339 | if (!empty($map['messageId'])) { |
---|
[5708] | 340 | |
---|
[5959] | 341 | foreach ($messagesIds as $k => $v) { |
---|
[6029] | 342 | |
---|
[6076] | 343 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID); |
---|
[5959] | 344 | |
---|
| 345 | if ($r) { |
---|
[6029] | 346 | |
---|
| 347 | $messages = array_merge($messages, $r); |
---|
| 348 | unset($messagesIds[$k]); |
---|
| 349 | |
---|
[5708] | 350 | } |
---|
[5533] | 351 | } |
---|
[5959] | 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 { |
---|
[6076] | 358 | $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID); |
---|
[5533] | 359 | } |
---|
[5959] | 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 | */ |
---|
[6029] | 374 | |
---|
[5959] | 375 | foreach ($messages as $k => $m) { |
---|
| 376 | $headers = imap_fetch_overview($this->mbox, $m, FT_UID); |
---|
[6029] | 377 | |
---|
[5959] | 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; |
---|
[5533] | 394 | } |
---|
[5959] | 395 | |
---|
| 396 | if ($this->mbox) { |
---|
| 397 | imap_close($this->mbox); |
---|
| 398 | $this->mbox = false; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | return $result; |
---|
| 402 | |
---|
| 403 | } //CASE 'followupflag' |
---|
[5341] | 404 | } |
---|
[5533] | 405 | } |
---|
| 406 | |
---|
| 407 | public function read( $URI, $justthese = false ) |
---|
| 408 | { |
---|
| 409 | |
---|
| 410 | switch( $URI['concept'] ) |
---|
[5341] | 411 | { |
---|
[5533] | 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 | { |
---|
[5959] | 451 | |
---|
| 452 | /** |
---|
| 453 | * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>" |
---|
[5533] | 454 | */ |
---|
[5959] | 455 | $folderName = $messageNumber = false; |
---|
| 456 | if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) { |
---|
| 457 | $folderName = dirname($URI['id']); |
---|
| 458 | $messageNumber = basename($URI['id']); |
---|
| 459 | } |
---|
| 460 | |
---|
[5533] | 461 | $result = array(); |
---|
[5959] | 462 | if ($folderName && $messageNumber) { |
---|
[5533] | 463 | |
---|
| 464 | $this->mbox = $this->open_mbox($folderName); |
---|
[6076] | 465 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID); |
---|
[5959] | 466 | |
---|
| 467 | if (in_array($messageNumber, $r)) { |
---|
| 468 | $headers = imap_fetch_overview($this->mbox, $messageNumber, FT_UID); |
---|
| 469 | |
---|
[5533] | 470 | $result = array ( |
---|
[5959] | 471 | 'messageId' => $headers[0]->message_id, |
---|
| 472 | 'messageNumber' => $messageNumber, |
---|
| 473 | 'folderName' => $folderName |
---|
[5533] | 474 | ); |
---|
| 475 | } |
---|
[5959] | 476 | |
---|
[5533] | 477 | imap_close($this->mbox); |
---|
| 478 | $this->mbox = false; |
---|
[5959] | 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 | |
---|
[6076] | 500 | if ($messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$URI['id'].'"', SE_UID)) { |
---|
[5959] | 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 | } |
---|
[5533] | 517 | } |
---|
| 518 | |
---|
[5959] | 519 | if ($this->mbox) { |
---|
| 520 | imap_close($this->mbox); |
---|
| 521 | $this->mbox = false; |
---|
| 522 | } |
---|
| 523 | |
---|
[5533] | 524 | return $result; |
---|
| 525 | } |
---|
[5341] | 526 | } |
---|
[5533] | 527 | } |
---|
[5136] | 528 | |
---|
[5959] | 529 | public function create($URI, &$data) |
---|
[5604] | 530 | { |
---|
[5533] | 531 | switch( $URI['concept'] ) |
---|
[5136] | 532 | { |
---|
[5533] | 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); |
---|
[5136] | 538 | |
---|
[5533] | 539 | imap_close($this->mbox); |
---|
| 540 | $this->mbox = false; |
---|
[5136] | 541 | |
---|
[5533] | 542 | return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']); |
---|
| 543 | } |
---|
| 544 | return array (); |
---|
| 545 | } |
---|
| 546 | case 'followupflagged': |
---|
| 547 | { |
---|
[5959] | 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'])) { |
---|
[5533] | 551 | |
---|
| 552 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
[5959] | 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 | |
---|
[5533] | 559 | imap_close($this->mbox); |
---|
| 560 | $this->mbox = false; |
---|
[5341] | 561 | |
---|
[5708] | 562 | return ($s) ? $data : array(); |
---|
| 563 | |
---|
[5959] | 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); |
---|
[6076] | 582 | if ($messages = imap_search($this->mbox, 'ALL TEXT "Message-Id: '.$data['messageId'].'"', SE_UID)) { |
---|
[5959] | 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 | } |
---|
[5533] | 598 | } |
---|
| 599 | return array (); |
---|
| 600 | } |
---|
[5697] | 601 | |
---|
| 602 | case 'message': |
---|
[5604] | 603 | { |
---|
[5959] | 604 | require_once ROOTPATH.'/library/uuid/class.uuid.php'; |
---|
| 605 | |
---|
[5604] | 606 | $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True ); |
---|
[5959] | 607 | $return = array(); |
---|
[5604] | 608 | |
---|
[5959] | 609 | require_once dirname(__FILE__) . '/../../services/class.servicelocator.php'; |
---|
| 610 | $mailService = ServiceLocator::getService('mail'); |
---|
[5604] | 611 | |
---|
[5959] | 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' ); |
---|
[5604] | 616 | |
---|
[5959] | 617 | $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8"); |
---|
[6065] | 618 | $folder = @preg_replace('/INBOX[\/.]/i', "INBOX".$this->imap_delimiter, $folder); |
---|
[5959] | 619 | |
---|
[5763] | 620 | /** |
---|
[5959] | 621 | * Gera e preenche o field Message-Id do header |
---|
| 622 | */ |
---|
[5763] | 623 | $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft'); |
---|
[5604] | 624 | |
---|
[5960] | 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 | |
---|
[5959] | 630 | if(isset($data['input_important_message'])) |
---|
| 631 | $mailService->addHeaderField('Importance','High'); |
---|
[5604] | 632 | |
---|
[5959] | 633 | if(isset($data['input_return_receipt'])) |
---|
| 634 | $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail')); |
---|
[5604] | 635 | |
---|
[5960] | 636 | $isHTML = ( isset($data['type']) && $data['type'] == 'html' )? true : false; |
---|
[5604] | 637 | |
---|
[5959] | 638 | if (!$body) $body = ' '; |
---|
[5604] | 639 | |
---|
| 640 | |
---|
[5959] | 641 | $mbox_stream = $this->open_mbox($folder); |
---|
[5620] | 642 | |
---|
[5959] | 643 | $attachment = json_decode($data['attachments'],TRUE); |
---|
[5604] | 644 | |
---|
[5620] | 645 | |
---|
[5959] | 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'] ) |
---|
[5960] | 701 | $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave'])); |
---|
[5959] | 702 | } |
---|
| 703 | |
---|
| 704 | if($mbox_stream) imap_close($mbox_stream); |
---|
| 705 | |
---|
| 706 | return $return; |
---|
[5604] | 707 | } |
---|
[5533] | 708 | } |
---|
[5136] | 709 | } |
---|
| 710 | |
---|
[5533] | 711 | public function delete( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 712 | { |
---|
[5533] | 713 | switch( $URI['concept'] ) |
---|
| 714 | { |
---|
| 715 | case 'labeled': |
---|
| 716 | { |
---|
| 717 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
[5586] | 718 | $folderName = dirname($messageId); |
---|
| 719 | $messageNumber = basename($messageId); |
---|
| 720 | |
---|
[5533] | 721 | if ($folderName && $messageNumber && $labelId) { |
---|
| 722 | $this->mbox = $this->open_mbox($folderName); |
---|
| 723 | imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID); |
---|
[5136] | 724 | |
---|
[5533] | 725 | imap_close($this->mbox); |
---|
| 726 | $this->mbox = false; |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | case 'followupflagged': |
---|
| 730 | { |
---|
[5959] | 731 | $map = array( |
---|
| 732 | 'folderName' => array(), |
---|
| 733 | 'messageNumber' => array(), |
---|
| 734 | 'messageId' => array() |
---|
| 735 | ); |
---|
[5989] | 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 | } |
---|
[5697] | 748 | |
---|
[5989] | 749 | $messagesIds = $map['messageId']; |
---|
[5136] | 750 | |
---|
[5989] | 751 | foreach ($map['folderName'] as $folder) { |
---|
| 752 | $messages = array(); |
---|
[5711] | 753 | |
---|
[5989] | 754 | $this->mbox = $this->open_mbox($folder); |
---|
| 755 | |
---|
[5959] | 756 | /** |
---|
[5989] | 757 | * Se é uma busca por messageId |
---|
[5959] | 758 | */ |
---|
[5989] | 759 | if (!empty($map['messageId'])) { |
---|
| 760 | |
---|
| 761 | foreach ($messagesIds as $k => $v) { |
---|
[6076] | 762 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID); |
---|
[5989] | 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 | */ |
---|
[5959] | 774 | } else { |
---|
[6076] | 775 | $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID); |
---|
[5959] | 776 | } |
---|
[5989] | 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; |
---|
[5959] | 795 | |
---|
[5989] | 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; |
---|
[5533] | 802 | } |
---|
[5711] | 803 | |
---|
[5989] | 804 | return $s; |
---|
[5533] | 805 | } |
---|
| 806 | } |
---|
[5136] | 807 | |
---|
[5533] | 808 | //TODO - return |
---|
| 809 | } |
---|
[5136] | 810 | |
---|
[5989] | 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 | } |
---|
[5136] | 817 | |
---|
| 818 | public function update( $URI, $data, $criteria = false ) |
---|
[5533] | 819 | { |
---|
[5570] | 820 | /** |
---|
[5959] | 821 | * Os únicos atributos da sinalização presentes no IMAP são folderName, messageNumber e messageId, |
---|
[5570] | 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 | */ |
---|
[5533] | 827 | } |
---|
[5136] | 828 | |
---|
[5533] | 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 | |
---|
[5136] | 836 | public function replace( $URI, $data, $criteria = false ) |
---|
| 837 | {} |
---|
| 838 | |
---|
| 839 | public function close() |
---|
| 840 | {} |
---|
| 841 | |
---|
| 842 | public function setup() |
---|
| 843 | {} |
---|
| 844 | |
---|
[5341] | 845 | public function commit( $uri ) |
---|
| 846 | { return( true ); } |
---|
| 847 | |
---|
| 848 | public function rollback( $uri ) |
---|
| 849 | {} |
---|
| 850 | |
---|
| 851 | public function begin( $uri ) |
---|
| 852 | {} |
---|
| 853 | |
---|
| 854 | |
---|
[5136] | 855 | public function teardown() |
---|
| 856 | {} |
---|
| 857 | |
---|
| 858 | function to_utf8($in) |
---|
| 859 | { |
---|
[5533] | 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; |
---|
[5136] | 868 | } |
---|
[5533] | 869 | return $out; |
---|
[5204] | 870 | } |
---|
[5533] | 871 | |
---|
| 872 | |
---|
[5697] | 873 | private static function parseFilter($filter ,&$map){ |
---|
[5533] | 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': { |
---|
[5540] | 886 | if(is_array($map[$filter[0]]) && is_array($filter[1])) |
---|
| 887 | $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1])); |
---|
[5533] | 888 | return; |
---|
| 889 | } |
---|
| 890 | case '=': { |
---|
| 891 | $map[$filter[0]][] = $filter[1]; |
---|
| 892 | } |
---|
| 893 | } |
---|
| 894 | } |
---|
[5697] | 895 | |
---|
[5136] | 896 | } |
---|