[5136] | 1 | <?php |
---|
[6357] | 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 | * |
---|
[6115] | 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 |
---|
[6357] | 29 | * e-mail address prognus@prognus.com.br. |
---|
| 30 | * |
---|
[6115] | 31 | * Classe de abstração que faz uma adaptação para manipulação de informações |
---|
[6357] | 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 |
---|
[6115] | 40 | */ |
---|
[5136] | 41 | |
---|
[5341] | 42 | include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php"; |
---|
[5136] | 43 | |
---|
[6357] | 44 | use prototype\api\Config as Config; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * |
---|
| 48 | * @package Prototype (Mail) |
---|
| 49 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 50 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 51 | * @version 2.4 |
---|
| 52 | * @sponsor Caixa Econômica Federal |
---|
| 53 | * @since Classe disponibilizada na versão 2.4 |
---|
[6132] | 54 | */ |
---|
[5136] | 55 | class ImapServiceAdapter extends imap_functions/* implements Service*/ |
---|
| 56 | { |
---|
| 57 | public function open( $config ) |
---|
| 58 | { |
---|
[5533] | 59 | $this->init(); |
---|
[5136] | 60 | } |
---|
| 61 | |
---|
[5341] | 62 | // public function connect( $config ) |
---|
| 63 | // { |
---|
[5533] | 64 | // $this->init(); |
---|
[5341] | 65 | // } |
---|
[5188] | 66 | |
---|
[5341] | 67 | public function find( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 68 | { |
---|
[6212] | 69 | |
---|
[6171] | 70 | $context = isset($justthese['context']) ? $justthese['context'] : '' ; |
---|
[5136] | 71 | |
---|
[6171] | 72 | switch( $URI['concept'] ) |
---|
[5533] | 73 | { |
---|
| 74 | case 'folder': |
---|
| 75 | { |
---|
| 76 | $result = $this->to_utf8($this->get_folders_list()); |
---|
[5136] | 77 | |
---|
[5533] | 78 | foreach ($result as $res) { |
---|
[5136] | 79 | |
---|
[5533] | 80 | $response[] = array( |
---|
| 81 | 'id' => $res['folder_id'], |
---|
| 82 | 'commonName' => $res['folder_name'], |
---|
| 83 | 'parentFolder' => $res['folder_parent'], |
---|
| 84 | 'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null) |
---|
| 85 | ); |
---|
| 86 | } |
---|
[5136] | 87 | |
---|
[5533] | 88 | return $response; |
---|
| 89 | } |
---|
| 90 | case 'message': |
---|
| 91 | { |
---|
| 92 | //begin: for grid |
---|
[6171] | 93 | $page = isset($criteria['page']) ? $criteria['page'] : 1 ; //{1} get the requested page |
---|
| 94 | $limit = isset($criteria['rows']) ? $criteria['rows'] : 10 ; //{10} get how many rows we want to have into the grid |
---|
| 95 | $sidx = isset($criteria['sidx']) ? $criteria['sidx'] : 0; //{id} get index row - i.e. user click to sort |
---|
| 96 | $sord = isset($criteria['sord']) ? $criteria['sord'] : ''; //{desc} get the direction |
---|
[5136] | 97 | |
---|
[6212] | 98 | $filter = isset($criteria['filter']) ? $criteria['filter'] : ''; |
---|
[5341] | 99 | |
---|
[5533] | 100 | if( !$sidx ) $sidx = 1; |
---|
[5136] | 101 | |
---|
[6212] | 102 | $folder_name = isset($URI['folder']) ? $URI['folder'] : str_replace( '.', $this->imap_delimiter, isset($context['folder']) ? $context['folder'] : 'INBOX'); |
---|
[6171] | 103 | |
---|
[5533] | 104 | $count = imap_num_msg( $this->open_mbox( $folder_name ) ); |
---|
[5136] | 105 | |
---|
[5533] | 106 | $total_pages = $count > 0 ? ceil( $count/$limit ) : 0; |
---|
[5136] | 107 | |
---|
[5533] | 108 | if( $page > $total_pages ) |
---|
| 109 | $page = $total_pages; |
---|
[5136] | 110 | |
---|
[5533] | 111 | $start = $limit * $page - $limit; |
---|
| 112 | // do not put $limit*($page - 1) |
---|
| 113 | //end: for grid |
---|
[5959] | 114 | |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Trata o caso específico de retorno do atributo messageId |
---|
| 118 | * |
---|
| 119 | * TODO - refazer todo a operação find do conceito message, uma vez que esta |
---|
| 120 | * foi desenvolvida quando a nova API ainda era muito imatura e se encontra |
---|
| 121 | * muito acoplada à estrutura de retorno esperada pelo plugin jqGrid |
---|
| 122 | */ |
---|
| 123 | if ( $justthese ) |
---|
| 124 | { |
---|
[6212] | 125 | if (isset($justthese[0]) && $justthese[0] == 'messageId') { |
---|
[5959] | 126 | $map = array( |
---|
| 127 | 'folderName' => array(), |
---|
| 128 | 'messageNumber' => array() |
---|
| 129 | ); |
---|
| 130 | |
---|
| 131 | self::parseFilter($criteria["filter"], &$map); |
---|
| 132 | |
---|
| 133 | if (count($map['folderName']) == 0) { |
---|
| 134 | $folders = $this->get_folders_list(); |
---|
| 135 | foreach ($folders as $folder) |
---|
| 136 | if (isset($folder['folder_id'])) |
---|
| 137 | $map['folderName'][] = $folder['folder_id']; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | $result = array(); |
---|
| 141 | foreach ($map['folderName'] as $folder) { |
---|
| 142 | $this->mbox = $this->open_mbox($folder); |
---|
[5136] | 143 | |
---|
[5959] | 144 | /** |
---|
| 145 | * Se não foi passado messageNumber no filtro, |
---|
| 146 | * busca todas as mensagens de cada pasta |
---|
| 147 | */ |
---|
| 148 | $messages = empty($map['messageNumber']) ? '1:*' : implode(',', $map['messageNumber']); |
---|
| 149 | $sequenceType = empty($map['messageNumber']) ? 0 : FT_UID; |
---|
| 150 | |
---|
| 151 | $headers = imap_fetch_overview($this->mbox, $messages, $sequenceType); |
---|
| 152 | foreach ($headers as $h) { |
---|
[6171] | 153 | if(isset($h->message_id )) |
---|
| 154 | $result[] = array ( 'messageId' => $h->message_id ); |
---|
[5959] | 155 | } |
---|
[6171] | 156 | |
---|
[5959] | 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 | |
---|
[6212] | 288 | if(is_array($messagesLabeleds)) |
---|
[5533] | 289 | foreach ($messagesLabeleds as $messageLabeled) { |
---|
| 290 | if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber'])) |
---|
| 291 | continue; |
---|
| 292 | |
---|
| 293 | $result[] = array ( |
---|
| 294 | 'id' => $folder . '/' . $messageLabeled . '#' . $label, |
---|
| 295 | 'folderName' => $folder, |
---|
| 296 | 'messageNumber' => $messageLabeled, |
---|
| 297 | 'labelId' => $label |
---|
| 298 | ); |
---|
| 299 | } |
---|
| 300 | } |
---|
[6171] | 301 | |
---|
[5533] | 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | return $result; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | case 'followupflagged': |
---|
[6029] | 309 | { |
---|
[5533] | 310 | $result = array ( ); |
---|
| 311 | |
---|
[5959] | 312 | $map = array( |
---|
| 313 | //'id' => array(), |
---|
| 314 | 'folderName' => array(), |
---|
| 315 | 'messageNumber' => array(), |
---|
| 316 | 'messageId' => array() |
---|
| 317 | ); |
---|
| 318 | |
---|
| 319 | self::parseFilter($criteria["filter"], &$map); |
---|
| 320 | |
---|
| 321 | if (empty($map['folderName'])) { |
---|
| 322 | $folders = $this->get_folders_list(); |
---|
| 323 | foreach ($folders as $folder) |
---|
| 324 | if (isset($folder['folder_id'])) |
---|
| 325 | $map['folderName'][] = $folder['folder_id']; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | $messagesIds = $map['messageId']; |
---|
[5708] | 329 | |
---|
[5959] | 330 | foreach ($map['folderName'] as $folder) { |
---|
| 331 | $messages = array(); |
---|
| 332 | |
---|
| 333 | $this->mbox = $this->open_mbox($folder); |
---|
[6029] | 334 | |
---|
[5959] | 335 | /** |
---|
| 336 | * Se é uma busca por messageId |
---|
| 337 | */ |
---|
| 338 | if (!empty($map['messageId'])) { |
---|
[5708] | 339 | |
---|
[5959] | 340 | foreach ($messagesIds as $k => $v) { |
---|
[6029] | 341 | |
---|
[6076] | 342 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID); |
---|
[5959] | 343 | |
---|
| 344 | if ($r) { |
---|
[6029] | 345 | |
---|
| 346 | $messages = array_merge($messages, $r); |
---|
| 347 | unset($messagesIds[$k]); |
---|
| 348 | |
---|
[5708] | 349 | } |
---|
[5533] | 350 | } |
---|
[5959] | 351 | |
---|
| 352 | /** |
---|
| 353 | * Se é uma busca por messageNumber. |
---|
| 354 | * Lembrando que, neste caso, só deve ser suportada uma única pasta no filtro. |
---|
| 355 | */ |
---|
| 356 | } else { |
---|
[6076] | 357 | $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID); |
---|
[5533] | 358 | } |
---|
[5959] | 359 | |
---|
| 360 | /** |
---|
| 361 | * Se é uma busca por messageId, deve ser comparado com os messageNumbers |
---|
| 362 | * passados no filtro, se houverem. |
---|
| 363 | */ |
---|
[6171] | 364 | if (!empty($map['messageNumber']) && is_array($messages)) { |
---|
| 365 | |
---|
[5959] | 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 | */ |
---|
[6171] | 374 | if(is_array($messages)) |
---|
[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 | |
---|
| 386 | /** |
---|
| 387 | * Se é uma busca por messageId e todos os messageIds foram econstrados: |
---|
| 388 | * Stop searching in all folders |
---|
| 389 | */ |
---|
| 390 | if (!empty($map['messageId']) && empty($messagesIds)) |
---|
| 391 | break; |
---|
[5533] | 392 | } |
---|
[5959] | 393 | |
---|
| 394 | |
---|
| 395 | return $result; |
---|
| 396 | |
---|
| 397 | } //CASE 'followupflag' |
---|
[5341] | 398 | } |
---|
[5533] | 399 | } |
---|
| 400 | |
---|
| 401 | public function read( $URI, $justthese = false ) |
---|
| 402 | { |
---|
| 403 | |
---|
| 404 | switch( $URI['concept'] ) |
---|
[5341] | 405 | { |
---|
[5533] | 406 | case 'message': |
---|
| 407 | { |
---|
| 408 | return $this->to_utf8( |
---|
| 409 | $this->get_info_msg( array('msg_number'=>$URI['id'], |
---|
| 410 | 'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) ) |
---|
| 411 | ); |
---|
| 412 | } |
---|
| 413 | case 'labeled': |
---|
| 414 | { |
---|
| 415 | /** |
---|
| 416 | * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId |
---|
| 417 | */ |
---|
| 418 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
| 419 | $folderName = basename($messageId); |
---|
| 420 | $messageNumber = dirname($messageId); |
---|
| 421 | |
---|
| 422 | $result = array(); |
---|
| 423 | |
---|
| 424 | if ($folderName && $messageNumber && $labelId) { |
---|
| 425 | $this->mbox = $this->open_mbox($folderName); |
---|
| 426 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID); |
---|
| 427 | |
---|
| 428 | if (in_array($messageNumber, $messagesLabeleds)) { |
---|
| 429 | $result = array ( |
---|
| 430 | 'id' => $URI['id'], |
---|
| 431 | 'folderName' => $folderName, |
---|
| 432 | 'messageNumber' => $messageNumber, |
---|
| 433 | 'labelId' => $labelId |
---|
| 434 | ); |
---|
| 435 | } |
---|
[6171] | 436 | |
---|
[5533] | 437 | } |
---|
| 438 | |
---|
| 439 | return $result; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | case 'followupflagged': |
---|
| 443 | { |
---|
[5959] | 444 | |
---|
| 445 | /** |
---|
| 446 | * identifica se o formato de ID é "folder/subfolder/subsubfolder/<messageNumber>" ou "<message-id>" |
---|
[5533] | 447 | */ |
---|
[5959] | 448 | $folderName = $messageNumber = false; |
---|
| 449 | if(!($messageHasId = preg_match('/<.*>/', $URI['id']))) { |
---|
| 450 | $folderName = dirname($URI['id']); |
---|
| 451 | $messageNumber = basename($URI['id']); |
---|
| 452 | } |
---|
| 453 | |
---|
[5533] | 454 | $result = array(); |
---|
[5959] | 455 | if ($folderName && $messageNumber) { |
---|
[5533] | 456 | |
---|
| 457 | $this->mbox = $this->open_mbox($folderName); |
---|
[6076] | 458 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID); |
---|
[5959] | 459 | |
---|
| 460 | if (in_array($messageNumber, $r)) { |
---|
| 461 | $headers = imap_fetch_overview($this->mbox, $messageNumber, FT_UID); |
---|
| 462 | |
---|
[5533] | 463 | $result = array ( |
---|
[5959] | 464 | 'messageId' => $headers[0]->message_id, |
---|
| 465 | 'messageNumber' => $messageNumber, |
---|
| 466 | 'folderName' => $folderName |
---|
[5533] | 467 | ); |
---|
| 468 | } |
---|
[5959] | 469 | |
---|
| 470 | } else { |
---|
| 471 | /** |
---|
| 472 | * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela, |
---|
| 473 | * senão busca em todas. |
---|
| 474 | */ |
---|
| 475 | |
---|
| 476 | $folders = array (); |
---|
| 477 | if ($folderName) { |
---|
| 478 | $folders = array ($folderName); |
---|
| 479 | } else { |
---|
| 480 | $folder_list = $this->get_folders_list(); |
---|
| 481 | foreach ($folder_list as $folder) |
---|
| 482 | if (isset($folder['folder_id'])) |
---|
| 483 | $folders[] = $folder['folder_id']; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | foreach ($folders as $folder) { |
---|
| 487 | |
---|
| 488 | $this->mbox = $this->open_mbox($folder); |
---|
| 489 | |
---|
[6076] | 490 | if ($messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$URI['id'].'"', SE_UID)) { |
---|
[5959] | 491 | |
---|
| 492 | $result = array ( |
---|
| 493 | 'messageId' => $URI['id'], |
---|
| 494 | 'messageNumber' => $messages[0], |
---|
| 495 | 'folderName' => $folder |
---|
| 496 | ); |
---|
| 497 | |
---|
| 498 | /** |
---|
| 499 | * Stop searching in all folders |
---|
| 500 | */ |
---|
| 501 | break; |
---|
| 502 | } |
---|
[6171] | 503 | |
---|
[5959] | 504 | } |
---|
[5533] | 505 | } |
---|
| 506 | |
---|
[5959] | 507 | |
---|
[5533] | 508 | return $result; |
---|
| 509 | } |
---|
[5341] | 510 | } |
---|
[5533] | 511 | } |
---|
[5136] | 512 | |
---|
[5959] | 513 | public function create($URI, &$data) |
---|
[5604] | 514 | { |
---|
[5533] | 515 | switch( $URI['concept'] ) |
---|
[5136] | 516 | { |
---|
[5533] | 517 | case 'labeled': |
---|
| 518 | { |
---|
| 519 | if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) { |
---|
| 520 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
| 521 | imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID); |
---|
[5136] | 522 | |
---|
[5533] | 523 | return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']); |
---|
| 524 | } |
---|
| 525 | return array (); |
---|
| 526 | } |
---|
| 527 | case 'followupflagged': |
---|
| 528 | { |
---|
[5959] | 529 | //deve ser gravado primeiro no imap, obtido o message-id e, depois gravado no banco |
---|
| 530 | |
---|
| 531 | if (isset($data['folderName']) && isset($data['messageNumber'])) { |
---|
[5533] | 532 | |
---|
| 533 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
[5959] | 534 | $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged', ST_UID); |
---|
| 535 | |
---|
| 536 | $headers = imap_fetch_overview($this->mbox, $data['messageNumber'], FT_UID); |
---|
| 537 | |
---|
| 538 | $data['messageId'] = $headers[0]->message_id; |
---|
| 539 | |
---|
[5341] | 540 | |
---|
[5708] | 541 | return ($s) ? $data : array(); |
---|
| 542 | |
---|
[5959] | 543 | } else if (isset($data['messageId'])) { |
---|
| 544 | /** |
---|
| 545 | * Busca pela mensagem com o messageId dado. Se uma pasta foi passada, busca nela, |
---|
| 546 | * senão busca em todas. |
---|
| 547 | */ |
---|
| 548 | $folders = array (); |
---|
| 549 | if (isset($data['folderName'])) { |
---|
| 550 | $folders = array ($data['folderName']); |
---|
| 551 | } else { |
---|
| 552 | $folder_list = $this->get_folders_list(); |
---|
| 553 | foreach ($folder_list as $folder) |
---|
| 554 | if (isset($folder['folder_id'])) |
---|
| 555 | $folders[] = $folder['folder_id']; |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | foreach ($folders as $folder) { |
---|
| 559 | |
---|
| 560 | $this->mbox = $this->open_mbox($folder); |
---|
[6076] | 561 | if ($messages = imap_search($this->mbox, 'ALL TEXT "Message-Id: '.$data['messageId'].'"', SE_UID)) { |
---|
[5959] | 562 | |
---|
| 563 | $s = imap_setflag_full($this->mbox, $messages[0], '$Followupflagged', ST_UID); |
---|
[6171] | 564 | |
---|
[5959] | 565 | /** |
---|
| 566 | * Stop searching in all folders |
---|
| 567 | */ |
---|
| 568 | return $data; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | } |
---|
[5533] | 572 | } |
---|
| 573 | return array (); |
---|
| 574 | } |
---|
[5697] | 575 | |
---|
| 576 | case 'message': |
---|
[5604] | 577 | { |
---|
[5959] | 578 | require_once ROOTPATH.'/library/uuid/class.uuid.php'; |
---|
| 579 | |
---|
[5604] | 580 | $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True ); |
---|
[5959] | 581 | $return = array(); |
---|
[5604] | 582 | |
---|
[5959] | 583 | require_once dirname(__FILE__) . '/../../services/class.servicelocator.php'; |
---|
| 584 | $mailService = ServiceLocator::getService('mail'); |
---|
[5604] | 585 | |
---|
[5959] | 586 | $msg_uid = $data['msg_id']; |
---|
| 587 | $body = $data['body']; |
---|
| 588 | $body = str_replace("%nbsp;"," ",$body); |
---|
| 589 | $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' ); |
---|
[5604] | 590 | |
---|
[5959] | 591 | $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8"); |
---|
[6065] | 592 | $folder = @preg_replace('/INBOX[\/.]/i', "INBOX".$this->imap_delimiter, $folder); |
---|
[5959] | 593 | |
---|
[5763] | 594 | /** |
---|
[5959] | 595 | * Gera e preenche o field Message-Id do header |
---|
| 596 | */ |
---|
[5763] | 597 | $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft'); |
---|
[5604] | 598 | |
---|
[5960] | 599 | $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 600 | $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 601 | $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 602 | $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 603 | |
---|
[5959] | 604 | if(isset($data['input_important_message'])) |
---|
| 605 | $mailService->addHeaderField('Importance','High'); |
---|
[5604] | 606 | |
---|
[5959] | 607 | if(isset($data['input_return_receipt'])) |
---|
| 608 | $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail')); |
---|
[5604] | 609 | |
---|
[5960] | 610 | $isHTML = ( isset($data['type']) && $data['type'] == 'html' )? true : false; |
---|
[5604] | 611 | |
---|
[5959] | 612 | if (!$body) $body = ' '; |
---|
[5604] | 613 | |
---|
| 614 | |
---|
[5959] | 615 | $mbox_stream = $this->open_mbox($folder); |
---|
[5620] | 616 | |
---|
[5959] | 617 | $attachment = json_decode($data['attachments'],TRUE); |
---|
[5604] | 618 | |
---|
[5620] | 619 | |
---|
[5959] | 620 | foreach ($attachment as &$value) |
---|
| 621 | { |
---|
| 622 | if((int)$value > 0) //BD attachment |
---|
| 623 | { |
---|
| 624 | $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment')); |
---|
| 625 | |
---|
| 626 | if($att['disposition'] == 'embedded') |
---|
| 627 | { |
---|
| 628 | $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body); |
---|
| 629 | $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']); |
---|
| 630 | } |
---|
| 631 | else |
---|
| 632 | $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); |
---|
| 633 | |
---|
| 634 | unset($att); |
---|
| 635 | } |
---|
| 636 | else |
---|
| 637 | { |
---|
| 638 | $value = json_decode($value, true); |
---|
| 639 | |
---|
| 640 | switch ($value['type']) { |
---|
| 641 | case 'imapPart': |
---|
| 642 | $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']); |
---|
| 643 | if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG |
---|
| 644 | { |
---|
| 645 | $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body); |
---|
| 646 | $mailService->addStringImage($att['source'], $att['type'], $att['name']); |
---|
| 647 | } |
---|
| 648 | else |
---|
| 649 | $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); |
---|
| 650 | unset($att); |
---|
| 651 | break; |
---|
| 652 | case 'imapMSG': |
---|
| 653 | $sub = $value['name'] ? $value['name'].'.eml' :'no title.eml'; |
---|
| 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, $sub, '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'] ) |
---|
[5960] | 675 | $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave'])); |
---|
[5959] | 676 | } |
---|
| 677 | |
---|
| 678 | return $return; |
---|
[5604] | 679 | } |
---|
[5533] | 680 | } |
---|
[5136] | 681 | } |
---|
| 682 | |
---|
[5533] | 683 | public function delete( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 684 | { |
---|
[5533] | 685 | switch( $URI['concept'] ) |
---|
| 686 | { |
---|
| 687 | case 'labeled': |
---|
| 688 | { |
---|
| 689 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
[5586] | 690 | $folderName = dirname($messageId); |
---|
| 691 | $messageNumber = basename($messageId); |
---|
| 692 | |
---|
[5533] | 693 | if ($folderName && $messageNumber && $labelId) { |
---|
| 694 | $this->mbox = $this->open_mbox($folderName); |
---|
| 695 | imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID); |
---|
[5136] | 696 | |
---|
[5533] | 697 | } |
---|
| 698 | } |
---|
| 699 | case 'followupflagged': |
---|
| 700 | { |
---|
[5959] | 701 | $map = array( |
---|
| 702 | 'folderName' => array(), |
---|
| 703 | 'messageNumber' => array(), |
---|
| 704 | 'messageId' => array() |
---|
| 705 | ); |
---|
[5989] | 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 | } |
---|
[5697] | 718 | |
---|
[5989] | 719 | $messagesIds = $map['messageId']; |
---|
[5136] | 720 | |
---|
[5989] | 721 | foreach ($map['folderName'] as $folder) { |
---|
| 722 | $messages = array(); |
---|
[5711] | 723 | |
---|
[5989] | 724 | $this->mbox = $this->open_mbox($folder); |
---|
| 725 | |
---|
[5959] | 726 | /** |
---|
[5989] | 727 | * Se é uma busca por messageId |
---|
[5959] | 728 | */ |
---|
[5989] | 729 | if (!empty($map['messageId'])) { |
---|
| 730 | |
---|
| 731 | foreach ($messagesIds as $k => $v) { |
---|
[6076] | 732 | $r = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged" TEXT "Message-Id: '.$v.'"', SE_UID); |
---|
[5989] | 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 | */ |
---|
[5959] | 744 | } else { |
---|
[6076] | 745 | $messages = imap_search($this->mbox, 'ALL KEYWORD "$Followupflagged"', SE_UID); |
---|
[5959] | 746 | } |
---|
[5989] | 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; |
---|
[5533] | 769 | } |
---|
[5711] | 770 | |
---|
[5989] | 771 | return $s; |
---|
[5533] | 772 | } |
---|
| 773 | } |
---|
[5136] | 774 | |
---|
[5533] | 775 | //TODO - return |
---|
| 776 | } |
---|
[5136] | 777 | |
---|
[5989] | 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 | } |
---|
[5136] | 784 | |
---|
| 785 | public function update( $URI, $data, $criteria = false ) |
---|
[5533] | 786 | { |
---|
[5570] | 787 | /** |
---|
[5959] | 788 | * Os únicos atributos da sinalização presentes no IMAP são folderName, messageNumber e messageId, |
---|
[5570] | 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 | */ |
---|
[5533] | 794 | } |
---|
[5136] | 795 | |
---|
[5533] | 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 | |
---|
[5136] | 803 | public function replace( $URI, $data, $criteria = false ) |
---|
| 804 | {} |
---|
| 805 | |
---|
| 806 | public function close() |
---|
| 807 | {} |
---|
| 808 | |
---|
| 809 | public function setup() |
---|
| 810 | {} |
---|
| 811 | |
---|
[5341] | 812 | public function commit( $uri ) |
---|
| 813 | { return( true ); } |
---|
| 814 | |
---|
| 815 | public function rollback( $uri ) |
---|
| 816 | {} |
---|
| 817 | |
---|
| 818 | public function begin( $uri ) |
---|
| 819 | {} |
---|
| 820 | |
---|
| 821 | |
---|
[5136] | 822 | public function teardown() |
---|
| 823 | {} |
---|
| 824 | |
---|
| 825 | function to_utf8($in) |
---|
| 826 | { |
---|
[5533] | 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; |
---|
[5136] | 835 | } |
---|
[5533] | 836 | return $out; |
---|
[5204] | 837 | } |
---|
[5533] | 838 | |
---|
| 839 | |
---|
[5697] | 840 | private static function parseFilter($filter ,&$map){ |
---|
[5533] | 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': { |
---|
[5540] | 853 | if(is_array($map[$filter[0]]) && is_array($filter[1])) |
---|
| 854 | $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1])); |
---|
[5533] | 855 | return; |
---|
| 856 | } |
---|
| 857 | case '=': { |
---|
| 858 | $map[$filter[0]][] = $filter[1]; |
---|
| 859 | } |
---|
| 860 | } |
---|
| 861 | } |
---|
[5697] | 862 | |
---|
[5136] | 863 | } |
---|