[5136] | 1 | <?php |
---|
| 2 | |
---|
[5341] | 3 | include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php"; |
---|
[5136] | 4 | |
---|
| 5 | class ImapServiceAdapter extends imap_functions/* implements Service*/ |
---|
| 6 | { |
---|
| 7 | public function open( $config ) |
---|
| 8 | { |
---|
[5533] | 9 | $this->init(); |
---|
[5136] | 10 | } |
---|
| 11 | |
---|
[5341] | 12 | // public function connect( $config ) |
---|
| 13 | // { |
---|
[5533] | 14 | // $this->init(); |
---|
[5341] | 15 | // } |
---|
[5188] | 16 | |
---|
[5341] | 17 | public function find( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 18 | { |
---|
[5533] | 19 | $context = $justthese['context']; |
---|
| 20 | $URI = $URI['concept']; |
---|
[5136] | 21 | |
---|
[5533] | 22 | switch( $URI ) |
---|
| 23 | { |
---|
| 24 | case 'folder': |
---|
| 25 | { |
---|
| 26 | $result = $this->to_utf8($this->get_folders_list()); |
---|
[5136] | 27 | |
---|
[5533] | 28 | foreach ($result as $res) { |
---|
[5136] | 29 | |
---|
[5533] | 30 | $response[] = array( |
---|
| 31 | 'id' => $res['folder_id'], |
---|
| 32 | 'commonName' => $res['folder_name'], |
---|
| 33 | 'parentFolder' => $res['folder_parent'], |
---|
| 34 | 'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null) |
---|
| 35 | ); |
---|
| 36 | } |
---|
[5136] | 37 | |
---|
[5533] | 38 | return $response; |
---|
| 39 | } |
---|
| 40 | case 'message': |
---|
| 41 | { |
---|
| 42 | //begin: for grid |
---|
| 43 | $page = $criteria['page']; //{1} get the requested page |
---|
| 44 | $limit = $criteria['rows']; //{10} get how many rows we want to have into the grid |
---|
| 45 | $sidx = $criteria['sidx']; //{id} get index row - i.e. user click to sort |
---|
| 46 | $sord = $criteria['sord']; //{desc} get the direction |
---|
[5136] | 47 | |
---|
[5533] | 48 | $filter = $criteria['filter']; |
---|
[5341] | 49 | |
---|
[5533] | 50 | if( !$sidx ) $sidx = 1; |
---|
[5136] | 51 | |
---|
[5533] | 52 | $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] ); |
---|
| 53 | |
---|
| 54 | $count = imap_num_msg( $this->open_mbox( $folder_name ) ); |
---|
[5136] | 55 | |
---|
[5533] | 56 | $total_pages = $count > 0 ? ceil( $count/$limit ) : 0; |
---|
[5136] | 57 | |
---|
[5533] | 58 | if( $page > $total_pages ) |
---|
| 59 | $page = $total_pages; |
---|
[5136] | 60 | |
---|
[5533] | 61 | $start = $limit * $page - $limit; |
---|
[5341] | 62 | |
---|
[5533] | 63 | // do not put $limit*($page - 1) |
---|
| 64 | //end: for grid |
---|
[5136] | 65 | |
---|
[5533] | 66 | if( $filter ) |
---|
| 67 | { |
---|
| 68 | if( $filter[0] !== 'msgNumber' ) |
---|
| 69 | { |
---|
| 70 | for( $i = 0; $i < count($filter); $i++ ) |
---|
| 71 | { |
---|
| 72 | if( count( $filter[$i] ) === 4 ) |
---|
| 73 | $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' ); |
---|
[5341] | 74 | |
---|
[5533] | 75 | $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] ); |
---|
| 76 | } |
---|
[5341] | 77 | |
---|
[5533] | 78 | return $this->searchSieveRule($criteria); |
---|
| 79 | } |
---|
[5341] | 80 | |
---|
[5533] | 81 | $msgNumber = array(); |
---|
[5341] | 82 | |
---|
[5533] | 83 | for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ ) |
---|
| 84 | $msgNumber[] = $filter[2][$i]; |
---|
[5341] | 85 | |
---|
[5533] | 86 | if( empty( $msgNumber ) ) |
---|
| 87 | return( false ); |
---|
[5341] | 88 | |
---|
[5533] | 89 | $result = $this->get_info_msgs( array( 'folder' => $folder_name, |
---|
| 90 | 'msgs_number' => implode( ',', $msgNumber ) ) ); |
---|
[5341] | 91 | |
---|
[5533] | 92 | foreach( $result as $i => $val ) |
---|
| 93 | $result[$i] = unserialize( $val ); |
---|
[5341] | 94 | |
---|
[5533] | 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | $result = $this->get_range_msgs2( |
---|
| 99 | array( |
---|
| 100 | 'folder' => $folder_name, //INBOX |
---|
| 101 | 'msg_range_begin' => $start + 1, //?? |
---|
| 102 | 'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid |
---|
| 103 | 'sort_box_type' => 'SORTARRIVAL', |
---|
| 104 | 'search_box_type' => 'ALL', |
---|
| 105 | 'sort_box_reverse' => 1 |
---|
| 106 | ) |
---|
| 107 | ); |
---|
| 108 | } |
---|
| 109 | //return var_export($result); |
---|
| 110 | |
---|
| 111 | $response = array( "page" => $page, "total" => $total_pages, "records" => $count ); |
---|
| 112 | |
---|
| 113 | for ($i=0; $i<count($result); $i++) |
---|
| 114 | { |
---|
| 115 | $flags_enum = array('Recent', 'Unseen', 'Answered', 'Draft', 'Deleted', 'Flagged'); |
---|
| 116 | |
---|
| 117 | foreach ($flags_enum as $key => $flag) |
---|
| 118 | { |
---|
| 119 | if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '') |
---|
| 120 | unset($flags_enum[$key]); |
---|
| 121 | |
---|
| 122 | unset($result[$i][$flag]); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (array_key_exists($i, $result)) |
---|
| 126 | { |
---|
| 127 | $response["rows"][$i] = $result[$i]; |
---|
| 128 | $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 ); |
---|
| 129 | $response["rows"][$i]['flags'] = implode(',', $flags_enum); |
---|
| 130 | $response["rows"][$i]['size'] = $response["rows"][$i]['Size']; |
---|
| 131 | //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() * 1000 ); |
---|
| 132 | unset($response["rows"][$i]['Size']); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return $this->to_utf8($response); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Filtros suportados: |
---|
| 141 | * - ['=', 'folderName', $X] |
---|
| 142 | * - [ |
---|
| 143 | * 'AND', |
---|
| 144 | * [ |
---|
| 145 | * 'AND', |
---|
| 146 | * ['=', 'folderName', $X], |
---|
| 147 | * ['IN', 'messageNumber', $Ys] |
---|
| 148 | * ], |
---|
| 149 | * ['IN', 'labelId', $Zs] |
---|
| 150 | * ] |
---|
| 151 | * - ['=', 'labelId', $X] |
---|
| 152 | * - [ |
---|
| 153 | * 'AND', |
---|
| 154 | * ['=', 'folderName', $X], |
---|
| 155 | * ['=', 'labelId', $Y] |
---|
| 156 | * ] |
---|
| 157 | * - ['IN', 'labelId', $Ys] |
---|
| 158 | * - [ |
---|
| 159 | * 'AND', |
---|
| 160 | * ['=', 'folderName', $X], |
---|
| 161 | * ['IN', 'labelId', $Ys] |
---|
| 162 | * ] |
---|
| 163 | */ |
---|
| 164 | case 'labeled': |
---|
| 165 | { |
---|
| 166 | $result = array ( ); |
---|
| 167 | if (isset($criteria["filter"]) && is_array($criteria['filter'])) { |
---|
[5540] | 168 | //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor |
---|
[5533] | 169 | $map = array( |
---|
| 170 | 'id' => array(), |
---|
| 171 | 'folderName' => array(), |
---|
| 172 | 'messageNumber' => array(), |
---|
| 173 | 'labelId' => array() |
---|
| 174 | ); |
---|
| 175 | |
---|
| 176 | self::parseFilter($criteria["filter"], &$map); |
---|
| 177 | |
---|
| 178 | if (count($map['folderName']) == 0) { |
---|
| 179 | $folders = $this->get_folders_list(); |
---|
| 180 | foreach ($folders as $folder) |
---|
| 181 | if (isset($folder['folder_id'])) |
---|
| 182 | $map['folderName'][] = $folder['folder_id']; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | foreach ($map['folderName'] as $folder) { |
---|
| 186 | $this->mbox = $this->open_mbox($folder); |
---|
| 187 | |
---|
| 188 | foreach ($map['labelId'] as $label) { |
---|
| 189 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$label.'"', SE_UID); |
---|
| 190 | |
---|
| 191 | foreach ($messagesLabeleds as $messageLabeled) { |
---|
| 192 | if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber'])) |
---|
| 193 | continue; |
---|
| 194 | |
---|
| 195 | $result[] = array ( |
---|
| 196 | 'id' => $folder . '/' . $messageLabeled . '#' . $label, |
---|
| 197 | 'folderName' => $folder, |
---|
| 198 | 'messageNumber' => $messageLabeled, |
---|
| 199 | 'labelId' => $label |
---|
| 200 | ); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | imap_close($this->mbox); |
---|
| 205 | $this->mbox = false; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | return $result; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | case 'followupflagged': |
---|
| 213 | { |
---|
| 214 | |
---|
| 215 | $result = array ( ); |
---|
| 216 | if (isset($criteria["filter"]) && is_array($criteria['filter'])) { |
---|
[5540] | 217 | //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor |
---|
[5533] | 218 | $map = array( |
---|
| 219 | 'id' => array(), |
---|
| 220 | 'folderName' => array(), |
---|
| 221 | 'messageNumber' => array() |
---|
| 222 | ); |
---|
| 223 | |
---|
| 224 | self::parseFilter($criteria["filter"], &$map); |
---|
| 225 | |
---|
| 226 | if (empty($map['folderName'])) { |
---|
| 227 | $folders = $this->get_folders_list(); |
---|
| 228 | foreach ($folders as $folder) |
---|
| 229 | if (isset($folder['folder_id'])) |
---|
| 230 | $map['folderName'][] = $folder['folder_id']; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | foreach ($map['folderName'] as $folder) { |
---|
| 234 | $this->mbox = $this->open_mbox($folder); |
---|
| 235 | |
---|
| 236 | foreach ($map['id'] as $followupflagged) { |
---|
| 237 | $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflagged.'"', SE_UID); |
---|
| 238 | |
---|
| 239 | foreach ($messagesFlaggeds as $messageFlagged) { |
---|
| 240 | if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber'])) |
---|
| 241 | continue; |
---|
| 242 | |
---|
| 243 | $result[] = array ( |
---|
| 244 | 'id' => $folder . '/' . $messageFlagged . '#' . $followupflagged, |
---|
| 245 | 'folderName' => $folder, |
---|
| 246 | 'messageNumber' => $messageFlagged |
---|
| 247 | ); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | imap_close($this->mbox); |
---|
| 252 | $this->mbox = false; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | return $result; |
---|
| 257 | } |
---|
[5341] | 258 | } |
---|
[5533] | 259 | } |
---|
| 260 | |
---|
| 261 | public function read( $URI, $justthese = false ) |
---|
| 262 | { |
---|
| 263 | |
---|
| 264 | switch( $URI['concept'] ) |
---|
[5341] | 265 | { |
---|
[5533] | 266 | case 'message': |
---|
| 267 | { |
---|
| 268 | return $this->to_utf8( |
---|
| 269 | $this->get_info_msg( array('msg_number'=>$URI['id'], |
---|
| 270 | 'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) ) |
---|
| 271 | ); |
---|
| 272 | } |
---|
| 273 | case 'labeled': |
---|
| 274 | { |
---|
| 275 | /** |
---|
| 276 | * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId |
---|
| 277 | */ |
---|
| 278 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
| 279 | $folderName = basename($messageId); |
---|
| 280 | $messageNumber = dirname($messageId); |
---|
| 281 | |
---|
| 282 | $result = array(); |
---|
| 283 | |
---|
| 284 | if ($folderName && $messageNumber && $labelId) { |
---|
| 285 | $this->mbox = $this->open_mbox($folderName); |
---|
| 286 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID); |
---|
| 287 | |
---|
| 288 | if (in_array($messageNumber, $messagesLabeleds)) { |
---|
| 289 | $result = array ( |
---|
| 290 | 'id' => $URI['id'], |
---|
| 291 | 'folderName' => $folderName, |
---|
| 292 | 'messageNumber' => $messageNumber, |
---|
| 293 | 'labelId' => $labelId |
---|
| 294 | ); |
---|
| 295 | } |
---|
| 296 | imap_close($this->mbox); |
---|
| 297 | $this->mbox = false; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | return $result; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | case 'followupflagged': |
---|
| 304 | { |
---|
| 305 | /** |
---|
| 306 | * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#followupflaggedId |
---|
| 307 | */ |
---|
| 308 | list($messageId, $followupflaggedId) = explode('#', $URI['id']); |
---|
| 309 | $folderName = basename($messageId); |
---|
| 310 | $messageNumber = dirname($messageId); |
---|
| 311 | |
---|
| 312 | $result = array(); |
---|
| 313 | |
---|
| 314 | if ($folderName && $messageNumber && $followupflaggedId) { |
---|
| 315 | $this->mbox = $this->open_mbox($folderName); |
---|
| 316 | $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflaggedId.'"', SE_UID); |
---|
| 317 | |
---|
| 318 | if (in_array($messageNumber, $messagesFlaggeds)) { |
---|
| 319 | $result = array ( |
---|
| 320 | 'id' => $URI['id'], |
---|
| 321 | 'folderName' => $folderName, |
---|
| 322 | 'messageNumber' => $messageNumber |
---|
| 323 | ); |
---|
| 324 | } |
---|
| 325 | imap_close($this->mbox); |
---|
| 326 | $this->mbox = false; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | return $result; |
---|
| 330 | } |
---|
[5341] | 331 | } |
---|
[5533] | 332 | } |
---|
[5136] | 333 | |
---|
[5533] | 334 | public function create( $URI, $data) |
---|
| 335 | { |
---|
| 336 | switch( $URI['concept'] ) |
---|
[5136] | 337 | { |
---|
[5533] | 338 | case 'labeled': |
---|
| 339 | { |
---|
| 340 | if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) { |
---|
| 341 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
| 342 | imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID); |
---|
[5136] | 343 | |
---|
[5533] | 344 | imap_close($this->mbox); |
---|
| 345 | $this->mbox = false; |
---|
[5136] | 346 | |
---|
[5533] | 347 | return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']); |
---|
| 348 | } |
---|
| 349 | return array (); |
---|
| 350 | } |
---|
| 351 | case 'followupflagged': |
---|
| 352 | { |
---|
[5540] | 353 | //deve ser gravado no banco primeiro, obtido o id e, depois, gravado no imap passando o id no parametro $data |
---|
[5533] | 354 | if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['id'])) { |
---|
| 355 | list($messageId, $followupflaggedId) = explode('#', $data['id']); |
---|
| 356 | |
---|
| 357 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
| 358 | imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflag' . $followupflaggedId, ST_UID); |
---|
[5136] | 359 | |
---|
[5533] | 360 | imap_close($this->mbox); |
---|
| 361 | $this->mbox = false; |
---|
[5341] | 362 | |
---|
[5533] | 363 | return array ('id' => $data['id']); |
---|
| 364 | } |
---|
| 365 | return array (); |
---|
| 366 | } |
---|
| 367 | } |
---|
[5136] | 368 | } |
---|
| 369 | |
---|
[5533] | 370 | public function delete( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 371 | { |
---|
[5533] | 372 | switch( $URI['concept'] ) |
---|
| 373 | { |
---|
| 374 | case 'labeled': |
---|
| 375 | { |
---|
| 376 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
| 377 | $folderName = basename($messageId); |
---|
| 378 | $messageNumber = dirname($messageId); |
---|
| 379 | |
---|
| 380 | if ($folderName && $messageNumber && $labelId) { |
---|
| 381 | $this->mbox = $this->open_mbox($folderName); |
---|
| 382 | imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID); |
---|
[5136] | 383 | |
---|
[5533] | 384 | imap_close($this->mbox); |
---|
| 385 | $this->mbox = false; |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | case 'followupflagged': |
---|
| 389 | { |
---|
| 390 | list($messageId, $followupflaggedId) = explode('#', $URI['id']); |
---|
| 391 | $folderName = basename($messageId); |
---|
| 392 | $messageNumber = dirname($messageId); |
---|
| 393 | |
---|
| 394 | if ($folderName && $messageNumber && $followupflaggedId) { |
---|
| 395 | $this->mbox = $this->open_mbox($folderName); |
---|
| 396 | imap_clearflag_full($this->mbox, $messageNumber, '$Followupflag' . $followupflaggedId, ST_UID); |
---|
[5136] | 397 | |
---|
[5533] | 398 | imap_close($this->mbox); |
---|
| 399 | $this->mbox = false; |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | } |
---|
[5136] | 403 | |
---|
[5533] | 404 | //TODO - return |
---|
| 405 | } |
---|
[5136] | 406 | |
---|
| 407 | public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar |
---|
| 408 | {} |
---|
| 409 | |
---|
| 410 | public function update( $URI, $data, $criteria = false ) |
---|
[5533] | 411 | { |
---|
| 412 | //TODO - remove |
---|
| 413 | //TODO - create |
---|
| 414 | } |
---|
[5136] | 415 | |
---|
[5533] | 416 | // public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false ) |
---|
| 417 | // { |
---|
| 418 | // return $this->read( array( 'id' => $id, |
---|
| 419 | // 'concept' => $concept, |
---|
| 420 | // 'context' => $parents ), $justthese ); |
---|
| 421 | // } |
---|
| 422 | |
---|
[5136] | 423 | public function replace( $URI, $data, $criteria = false ) |
---|
| 424 | {} |
---|
| 425 | |
---|
| 426 | public function close() |
---|
| 427 | {} |
---|
| 428 | |
---|
| 429 | public function setup() |
---|
| 430 | {} |
---|
| 431 | |
---|
[5341] | 432 | public function commit( $uri ) |
---|
| 433 | { return( true ); } |
---|
| 434 | |
---|
| 435 | public function rollback( $uri ) |
---|
| 436 | {} |
---|
| 437 | |
---|
| 438 | public function begin( $uri ) |
---|
| 439 | {} |
---|
| 440 | |
---|
| 441 | |
---|
[5136] | 442 | public function teardown() |
---|
| 443 | {} |
---|
| 444 | |
---|
| 445 | function to_utf8($in) |
---|
| 446 | { |
---|
[5533] | 447 | if (is_array($in)) { |
---|
| 448 | foreach ($in as $key => $value) { |
---|
| 449 | $out[$this->to_utf8($key)] = $this->to_utf8($value); |
---|
| 450 | } |
---|
| 451 | } elseif(is_string($in)) { |
---|
| 452 | return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' ); |
---|
| 453 | } else { |
---|
| 454 | return $in; |
---|
[5136] | 455 | } |
---|
[5533] | 456 | return $out; |
---|
[5204] | 457 | } |
---|
[5533] | 458 | |
---|
| 459 | |
---|
| 460 | private static function parseFilter( $filter ,&$map){ |
---|
| 461 | |
---|
| 462 | if( !is_array( $filter ) || count($filter) <= 0) return null; |
---|
| 463 | |
---|
| 464 | $op = array_shift( $filter ); |
---|
| 465 | switch(strtolower($op)) |
---|
| 466 | { |
---|
| 467 | case 'and': { |
---|
| 468 | foreach ($filter as $term) |
---|
| 469 | self::parseFilter($term ,&$map); |
---|
| 470 | return; |
---|
| 471 | } |
---|
| 472 | case 'in': { |
---|
[5540] | 473 | if(is_array($map[$filter[0]]) && is_array($filter[1])) |
---|
| 474 | $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1])); |
---|
[5533] | 475 | return; |
---|
| 476 | } |
---|
| 477 | case '=': { |
---|
| 478 | $map[$filter[0]][] = $filter[1]; |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | } |
---|
| 482 | |
---|
[5136] | 483 | } |
---|