[5136] | 1 | <?php |
---|
| 2 | |
---|
[5341] | 3 | include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php"; |
---|
[5697] | 4 | require_once ROOTPATH.'/library/uuid/class.uuid.php'; |
---|
[5136] | 5 | |
---|
[5697] | 6 | |
---|
[5136] | 7 | class ImapServiceAdapter extends imap_functions/* implements Service*/ |
---|
| 8 | { |
---|
| 9 | public function open( $config ) |
---|
| 10 | { |
---|
[5533] | 11 | $this->init(); |
---|
[5136] | 12 | } |
---|
| 13 | |
---|
[5341] | 14 | // public function connect( $config ) |
---|
| 15 | // { |
---|
[5533] | 16 | // $this->init(); |
---|
[5341] | 17 | // } |
---|
[5188] | 18 | |
---|
[5341] | 19 | public function find( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 20 | { |
---|
[5749] | 21 | //require_once dirname(__FILE__) . '/../api/controller.php'; |
---|
[5533] | 22 | $context = $justthese['context']; |
---|
| 23 | $URI = $URI['concept']; |
---|
[5136] | 24 | |
---|
[5533] | 25 | switch( $URI ) |
---|
| 26 | { |
---|
| 27 | case 'folder': |
---|
| 28 | { |
---|
| 29 | $result = $this->to_utf8($this->get_folders_list()); |
---|
[5136] | 30 | |
---|
[5533] | 31 | foreach ($result as $res) { |
---|
[5136] | 32 | |
---|
[5533] | 33 | $response[] = array( |
---|
| 34 | 'id' => $res['folder_id'], |
---|
| 35 | 'commonName' => $res['folder_name'], |
---|
| 36 | 'parentFolder' => $res['folder_parent'], |
---|
| 37 | 'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null) |
---|
| 38 | ); |
---|
| 39 | } |
---|
[5136] | 40 | |
---|
[5533] | 41 | return $response; |
---|
| 42 | } |
---|
| 43 | case 'message': |
---|
| 44 | { |
---|
| 45 | //begin: for grid |
---|
| 46 | $page = $criteria['page']; //{1} get the requested page |
---|
| 47 | $limit = $criteria['rows']; //{10} get how many rows we want to have into the grid |
---|
| 48 | $sidx = $criteria['sidx']; //{id} get index row - i.e. user click to sort |
---|
| 49 | $sord = $criteria['sord']; //{desc} get the direction |
---|
[5136] | 50 | |
---|
[5533] | 51 | $filter = $criteria['filter']; |
---|
[5341] | 52 | |
---|
[5533] | 53 | if( !$sidx ) $sidx = 1; |
---|
[5136] | 54 | |
---|
[5533] | 55 | $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] ); |
---|
| 56 | |
---|
| 57 | $count = imap_num_msg( $this->open_mbox( $folder_name ) ); |
---|
[5136] | 58 | |
---|
[5533] | 59 | $total_pages = $count > 0 ? ceil( $count/$limit ) : 0; |
---|
[5136] | 60 | |
---|
[5533] | 61 | if( $page > $total_pages ) |
---|
| 62 | $page = $total_pages; |
---|
[5136] | 63 | |
---|
[5533] | 64 | $start = $limit * $page - $limit; |
---|
[5341] | 65 | |
---|
[5533] | 66 | // do not put $limit*($page - 1) |
---|
| 67 | //end: for grid |
---|
[5136] | 68 | |
---|
[5533] | 69 | if( $filter ) |
---|
| 70 | { |
---|
| 71 | if( $filter[0] !== 'msgNumber' ) |
---|
| 72 | { |
---|
| 73 | for( $i = 0; $i < count($filter); $i++ ) |
---|
| 74 | { |
---|
| 75 | if( count( $filter[$i] ) === 4 ) |
---|
| 76 | $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' ); |
---|
[5341] | 77 | |
---|
[5533] | 78 | $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] ); |
---|
| 79 | } |
---|
[5341] | 80 | |
---|
[5533] | 81 | return $this->searchSieveRule($criteria); |
---|
| 82 | } |
---|
[5341] | 83 | |
---|
[5533] | 84 | $msgNumber = array(); |
---|
[5341] | 85 | |
---|
[5533] | 86 | for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ ) |
---|
| 87 | $msgNumber[] = $filter[2][$i]; |
---|
[5341] | 88 | |
---|
[5533] | 89 | if( empty( $msgNumber ) ) |
---|
| 90 | return( false ); |
---|
[5341] | 91 | |
---|
[5533] | 92 | $result = $this->get_info_msgs( array( 'folder' => $folder_name, |
---|
| 93 | 'msgs_number' => implode( ',', $msgNumber ) ) ); |
---|
[5341] | 94 | |
---|
[5533] | 95 | foreach( $result as $i => $val ) |
---|
[5749] | 96 | $result[$i] = unserialize( $val ); |
---|
[5533] | 97 | } |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | $result = $this->get_range_msgs2( |
---|
| 101 | array( |
---|
| 102 | 'folder' => $folder_name, //INBOX |
---|
| 103 | 'msg_range_begin' => $start + 1, //?? |
---|
| 104 | 'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid |
---|
| 105 | 'sort_box_type' => 'SORTARRIVAL', |
---|
| 106 | 'search_box_type' => 'ALL', |
---|
| 107 | 'sort_box_reverse' => 1 |
---|
| 108 | ) |
---|
| 109 | ); |
---|
| 110 | } |
---|
| 111 | //return var_export($result); |
---|
[5749] | 112 | |
---|
| 113 | $response = array( "page" => $page, "total" => $total_pages, "records" => $count ); |
---|
[5826] | 114 | |
---|
[5533] | 115 | for ($i=0; $i<count($result); $i++) |
---|
| 116 | { |
---|
[5749] | 117 | $flags_enum = array('Unseen'=> 1, 'Answered'=> 1, 'Forwarded'=> 1, 'Flagged'=> 1, 'Recent'=> 1, 'Draft'=> 1 ); |
---|
[5533] | 118 | |
---|
| 119 | foreach ($flags_enum as $key => $flag) |
---|
| 120 | { |
---|
[5749] | 121 | if ( !isset($result[$i][$key]) || !trim($result[$i][$key]) || trim($result[$i][$key]) == '') |
---|
| 122 | $flags_enum[$key] = 0; |
---|
| 123 | //unset($flags_enum[$key]); |
---|
[5533] | 124 | |
---|
| 125 | unset($result[$i][$flag]); |
---|
[5749] | 126 | } |
---|
[5533] | 127 | if (array_key_exists($i, $result)) |
---|
| 128 | { |
---|
| 129 | $response["rows"][$i] = $result[$i]; |
---|
| 130 | $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 ); |
---|
| 131 | $response["rows"][$i]['flags'] = implode(',', $flags_enum); |
---|
| 132 | $response["rows"][$i]['size'] = $response["rows"][$i]['Size']; |
---|
[5749] | 133 | $response["rows"][$i]['folder'] = $folder_name; |
---|
[5826] | 134 | |
---|
[5533] | 135 | //$response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() * 1000 ); |
---|
| 136 | unset($response["rows"][$i]['Size']); |
---|
| 137 | } |
---|
[5749] | 138 | } |
---|
[5533] | 139 | return $this->to_utf8($response); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Filtros suportados: |
---|
| 144 | * - ['=', 'folderName', $X] |
---|
| 145 | * - [ |
---|
| 146 | * 'AND', |
---|
| 147 | * [ |
---|
| 148 | * 'AND', |
---|
| 149 | * ['=', 'folderName', $X], |
---|
| 150 | * ['IN', 'messageNumber', $Ys] |
---|
| 151 | * ], |
---|
| 152 | * ['IN', 'labelId', $Zs] |
---|
| 153 | * ] |
---|
| 154 | * - ['=', 'labelId', $X] |
---|
| 155 | * - [ |
---|
| 156 | * 'AND', |
---|
| 157 | * ['=', 'folderName', $X], |
---|
| 158 | * ['=', 'labelId', $Y] |
---|
| 159 | * ] |
---|
| 160 | * - ['IN', 'labelId', $Ys] |
---|
| 161 | * - [ |
---|
| 162 | * 'AND', |
---|
| 163 | * ['=', 'folderName', $X], |
---|
| 164 | * ['IN', 'labelId', $Ys] |
---|
| 165 | * ] |
---|
| 166 | */ |
---|
| 167 | case 'labeled': |
---|
| 168 | { |
---|
| 169 | $result = array ( ); |
---|
| 170 | if (isset($criteria["filter"]) && is_array($criteria['filter'])) { |
---|
[5540] | 171 | //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor |
---|
[5533] | 172 | $map = array( |
---|
| 173 | 'id' => array(), |
---|
| 174 | 'folderName' => array(), |
---|
| 175 | 'messageNumber' => array(), |
---|
| 176 | 'labelId' => array() |
---|
| 177 | ); |
---|
| 178 | |
---|
| 179 | self::parseFilter($criteria["filter"], &$map); |
---|
| 180 | |
---|
| 181 | if (count($map['folderName']) == 0) { |
---|
| 182 | $folders = $this->get_folders_list(); |
---|
| 183 | foreach ($folders as $folder) |
---|
| 184 | if (isset($folder['folder_id'])) |
---|
| 185 | $map['folderName'][] = $folder['folder_id']; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | foreach ($map['folderName'] as $folder) { |
---|
| 189 | $this->mbox = $this->open_mbox($folder); |
---|
| 190 | |
---|
| 191 | foreach ($map['labelId'] as $label) { |
---|
| 192 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$label.'"', SE_UID); |
---|
| 193 | |
---|
| 194 | foreach ($messagesLabeleds as $messageLabeled) { |
---|
| 195 | if (count($map['messageNumber']) > 0 && !in_array($messageLabeled, $map['messageNumber'])) |
---|
| 196 | continue; |
---|
| 197 | |
---|
| 198 | $result[] = array ( |
---|
| 199 | 'id' => $folder . '/' . $messageLabeled . '#' . $label, |
---|
| 200 | 'folderName' => $folder, |
---|
| 201 | 'messageNumber' => $messageLabeled, |
---|
| 202 | 'labelId' => $label |
---|
| 203 | ); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | imap_close($this->mbox); |
---|
| 208 | $this->mbox = false; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | return $result; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | case 'followupflagged': |
---|
| 216 | { |
---|
| 217 | |
---|
| 218 | $result = array ( ); |
---|
| 219 | if (isset($criteria["filter"]) && is_array($criteria['filter'])) { |
---|
[5540] | 220 | //TODO - melhorar o tratamento do filter com a lista de todos os labelIds dado pelo interceptor |
---|
[5533] | 221 | $map = array( |
---|
| 222 | 'id' => array(), |
---|
| 223 | 'folderName' => array(), |
---|
| 224 | 'messageNumber' => array() |
---|
| 225 | ); |
---|
| 226 | |
---|
| 227 | self::parseFilter($criteria["filter"], &$map); |
---|
| 228 | |
---|
| 229 | if (empty($map['folderName'])) { |
---|
| 230 | $folders = $this->get_folders_list(); |
---|
| 231 | foreach ($folders as $folder) |
---|
| 232 | if (isset($folder['folder_id'])) |
---|
| 233 | $map['folderName'][] = $folder['folder_id']; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | foreach ($map['folderName'] as $folder) { |
---|
| 237 | $this->mbox = $this->open_mbox($folder); |
---|
| 238 | |
---|
[5708] | 239 | /** |
---|
| 240 | * Caso não tenha sido passado id no filtro |
---|
| 241 | */ |
---|
| 242 | if (!$map['id']) { |
---|
| 243 | $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflagged"', SE_UID); |
---|
| 244 | |
---|
[5533] | 245 | foreach ($messagesFlaggeds as $messageFlagged) { |
---|
| 246 | if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber'])) |
---|
| 247 | continue; |
---|
| 248 | |
---|
| 249 | $result[] = array ( |
---|
| 250 | 'folderName' => $folder, |
---|
| 251 | 'messageNumber' => $messageFlagged |
---|
| 252 | ); |
---|
| 253 | } |
---|
[5708] | 254 | |
---|
| 255 | } else { |
---|
| 256 | foreach ($map['id'] as $followupflagged) { |
---|
| 257 | $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflagged.'"', SE_UID); |
---|
| 258 | |
---|
| 259 | foreach ($messagesFlaggeds as $messageFlagged) { |
---|
| 260 | if (count($map['messageNumber']) > 0 && !in_array($messageFlagged, $map['messageNumber'])) |
---|
| 261 | continue; |
---|
| 262 | |
---|
| 263 | $result[] = array ( |
---|
| 264 | 'id' => $folder . '/' . $messageFlagged . '#' . $followupflagged, |
---|
| 265 | 'folderName' => $folder, |
---|
| 266 | 'messageNumber' => $messageFlagged |
---|
| 267 | ); |
---|
| 268 | } |
---|
| 269 | } |
---|
[5533] | 270 | } |
---|
| 271 | |
---|
| 272 | imap_close($this->mbox); |
---|
| 273 | $this->mbox = false; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | return $result; |
---|
| 278 | } |
---|
[5341] | 279 | } |
---|
[5533] | 280 | } |
---|
| 281 | |
---|
| 282 | public function read( $URI, $justthese = false ) |
---|
| 283 | { |
---|
| 284 | |
---|
| 285 | switch( $URI['concept'] ) |
---|
[5341] | 286 | { |
---|
[5533] | 287 | case 'message': |
---|
| 288 | { |
---|
| 289 | return $this->to_utf8( |
---|
| 290 | $this->get_info_msg( array('msg_number'=>$URI['id'], |
---|
| 291 | 'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) ) |
---|
| 292 | ); |
---|
| 293 | } |
---|
| 294 | case 'labeled': |
---|
| 295 | { |
---|
| 296 | /** |
---|
| 297 | * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#labelId |
---|
| 298 | */ |
---|
| 299 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
| 300 | $folderName = basename($messageId); |
---|
| 301 | $messageNumber = dirname($messageId); |
---|
| 302 | |
---|
| 303 | $result = array(); |
---|
| 304 | |
---|
| 305 | if ($folderName && $messageNumber && $labelId) { |
---|
| 306 | $this->mbox = $this->open_mbox($folderName); |
---|
| 307 | $messagesLabeleds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Label'.$labelId.'"', SE_UID); |
---|
| 308 | |
---|
| 309 | if (in_array($messageNumber, $messagesLabeleds)) { |
---|
| 310 | $result = array ( |
---|
| 311 | 'id' => $URI['id'], |
---|
| 312 | 'folderName' => $folderName, |
---|
| 313 | 'messageNumber' => $messageNumber, |
---|
| 314 | 'labelId' => $labelId |
---|
| 315 | ); |
---|
| 316 | } |
---|
| 317 | imap_close($this->mbox); |
---|
| 318 | $this->mbox = false; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | return $result; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | case 'followupflagged': |
---|
| 325 | { |
---|
| 326 | /** |
---|
| 327 | * id looks like 'folder/subfolder/subsubfolder/65#13', meaning messageId#followupflaggedId |
---|
| 328 | */ |
---|
| 329 | list($messageId, $followupflaggedId) = explode('#', $URI['id']); |
---|
[5708] | 330 | $folderName = dirname($messageId); |
---|
| 331 | $messageNumber = basename($messageId); |
---|
[5533] | 332 | |
---|
| 333 | $result = array(); |
---|
| 334 | |
---|
| 335 | if ($folderName && $messageNumber && $followupflaggedId) { |
---|
| 336 | $this->mbox = $this->open_mbox($folderName); |
---|
| 337 | $messagesFlaggeds = imap_search($this->mbox, 'UNDELETED KEYWORD "$Followupflag'.$followupflaggedId.'"', SE_UID); |
---|
| 338 | |
---|
| 339 | if (in_array($messageNumber, $messagesFlaggeds)) { |
---|
| 340 | $result = array ( |
---|
| 341 | 'id' => $URI['id'], |
---|
| 342 | 'folderName' => $folderName, |
---|
| 343 | 'messageNumber' => $messageNumber |
---|
| 344 | ); |
---|
| 345 | } |
---|
| 346 | imap_close($this->mbox); |
---|
| 347 | $this->mbox = false; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | return $result; |
---|
| 351 | } |
---|
[5341] | 352 | } |
---|
[5533] | 353 | } |
---|
[5136] | 354 | |
---|
[5533] | 355 | public function create( $URI, $data) |
---|
[5604] | 356 | { |
---|
[5533] | 357 | switch( $URI['concept'] ) |
---|
[5136] | 358 | { |
---|
[5533] | 359 | case 'labeled': |
---|
| 360 | { |
---|
| 361 | if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['labelId'])) { |
---|
| 362 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
| 363 | imap_setflag_full($this->mbox, $data['messageNumber'], '$Label' . $data['labelId'], ST_UID); |
---|
[5136] | 364 | |
---|
[5533] | 365 | imap_close($this->mbox); |
---|
| 366 | $this->mbox = false; |
---|
[5136] | 367 | |
---|
[5533] | 368 | return array ('id' => $data['folderName'].'/'.$data['messageNumber'].'#'.$data['labelId']); |
---|
| 369 | } |
---|
| 370 | return array (); |
---|
| 371 | } |
---|
| 372 | case 'followupflagged': |
---|
| 373 | { |
---|
[5540] | 374 | //deve ser gravado no banco primeiro, obtido o id e, depois, gravado no imap passando o id no parametro $data |
---|
[5533] | 375 | if (isset($data['folderName']) && isset($data['messageNumber']) && isset($data['id'])) { |
---|
| 376 | list($messageId, $followupflaggedId) = explode('#', $data['id']); |
---|
| 377 | |
---|
| 378 | $this->mbox = $this->open_mbox($data['folderName']); |
---|
[5708] | 379 | $s = imap_setflag_full($this->mbox, $data['messageNumber'], '$Followupflagged $Followupflag' . $followupflaggedId, ST_UID); |
---|
| 380 | |
---|
[5533] | 381 | imap_close($this->mbox); |
---|
| 382 | $this->mbox = false; |
---|
[5341] | 383 | |
---|
[5708] | 384 | return ($s) ? $data : array(); |
---|
| 385 | |
---|
[5533] | 386 | } |
---|
| 387 | return array (); |
---|
| 388 | } |
---|
[5697] | 389 | |
---|
| 390 | case 'message': |
---|
[5604] | 391 | { |
---|
| 392 | $GLOBALS['phpgw_info']['flags'] = array( 'noheader' => true, 'nonavbar' => true,'currentapp' => 'expressoMail1_2','enable_nextmatchs_class' => True ); |
---|
| 393 | $return = array(); |
---|
[5763] | 394 | |
---|
[5604] | 395 | require_once dirname(__FILE__) . '/../../services/class.servicelocator.php'; |
---|
| 396 | $mailService = ServiceLocator::getService('mail'); |
---|
| 397 | |
---|
| 398 | $msg_uid = $data['msg_id']; |
---|
| 399 | $body = $data['body']; |
---|
| 400 | $body = str_replace("%nbsp;"," ",$body); |
---|
| 401 | $body = html_entity_decode ( $body, ENT_QUOTES , 'ISO-8859-1' ); |
---|
| 402 | |
---|
| 403 | $folder = mb_convert_encoding($data['folder'], "UTF7-IMAP","ISO-8859-1, UTF-8"); |
---|
| 404 | $folder = @eregi_replace("INBOX[/.]", "INBOX".$this->imap_delimiter, $folder); |
---|
| 405 | |
---|
[5763] | 406 | /** |
---|
| 407 | * Gera e preenche o field Message-Id do header |
---|
| 408 | */ |
---|
| 409 | $mailService->addHeaderField('Message-Id', UUID::generate( UUID::UUID_RANDOM, UUID::FMT_STRING ) . '@Draft'); |
---|
[5697] | 410 | |
---|
[5763] | 411 | $mailService->addTo(mb_convert_encoding(($data['input_to']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 412 | $mailService->addCc( mb_convert_encoding(($data['input_cc']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 413 | $mailService->addBcc(mb_convert_encoding(($data['input_cco']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 414 | $mailService->setSubject(mb_convert_encoding(($data['input_subject']), 'ISO-8859-1', 'UTF-8,ISO-8859-1')); |
---|
| 415 | |
---|
[5604] | 416 | if(isset($data['input_important_message'])) |
---|
| 417 | $mailService->addHeaderField('Importance','High'); |
---|
| 418 | |
---|
| 419 | if(isset($data['input_return_receipt'])) |
---|
| 420 | $mailService->addHeaderField('Disposition-Notification-To', Config::me('mail')); |
---|
| 421 | |
---|
[5745] | 422 | $isHTML = ( isset($data['type']) && $data['type'] == 'html' )? true : false; |
---|
| 423 | |
---|
[5604] | 424 | if (!$body) $body = ' '; |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | $mbox_stream = $this->open_mbox($folder); |
---|
| 428 | |
---|
| 429 | $attachment = json_decode($data['attachments'],TRUE); |
---|
| 430 | |
---|
| 431 | |
---|
| 432 | foreach ($attachment as &$value) |
---|
| 433 | { |
---|
| 434 | |
---|
| 435 | if((int)$value > 0) //BD attachment |
---|
| 436 | { |
---|
| 437 | $att = Controller::read(array('id'=> $value , 'concept' => 'mailAttachment')); |
---|
| 438 | |
---|
[5620] | 439 | |
---|
[5604] | 440 | if($att['disposition'] == 'embedded') |
---|
| 441 | { |
---|
| 442 | $body = str_replace('"../prototype/getArchive.php?mailAttachment='.$att['id'].'"', $att['name'], $body); |
---|
| 443 | $mailService->addStringImage(base64_decode($att['source']), $att['type'], $att['name']); |
---|
| 444 | } |
---|
| 445 | else |
---|
| 446 | $mailService->addStringAttachment(base64_decode($att['source']), $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); |
---|
| 447 | |
---|
| 448 | unset($att); |
---|
| 449 | } |
---|
[5620] | 450 | else |
---|
[5604] | 451 | { |
---|
| 452 | $value = json_decode($value, true); |
---|
[5620] | 453 | |
---|
| 454 | switch ($value['type']) { |
---|
| 455 | case 'imapPart': |
---|
| 456 | $att = $this->getForwardingAttachment($value['folder'],$value['uid'], $value['part']); |
---|
| 457 | if(strstr($body,'<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />') !== false)//Embeded IMG |
---|
| 458 | { |
---|
| 459 | $body = str_ireplace('<img src="./inc/get_archive.php?msgFolder='.$value['folder'].'&msgNumber='.$value['uid'].'&indexPart='.$value['part'].'" />' , '<img src="'.$att['name'].'" />', $body); |
---|
| 460 | $mailService->addStringImage($att['source'], $att['type'], $att['name']); |
---|
| 461 | } |
---|
| 462 | else |
---|
| 463 | $mailService->addStringAttachment($att['source'], $att['name'], $att['type'], 'base64', isset($att['disposition']) ? $att['disposition'] :'attachment' ); |
---|
| 464 | unset($att); |
---|
| 465 | break; |
---|
| 466 | case 'imapMSG': |
---|
| 467 | $sub = $value['name'] ? $value['name'].'.eml' :'no title.eml'; |
---|
| 468 | $mbox_stream = $this->open_mbox($value['folder']); |
---|
| 469 | $rawmsg = $this->getRawHeader($value['uid']) . "\r\n\r\n" . $this->getRawBody($value['uid']); |
---|
| 470 | $mailService->addStringAttachment($rawmsg, $sub, 'message/rfc822', '7bit', 'attachment' ); |
---|
| 471 | unset($rawmsg); |
---|
| 472 | break; |
---|
| 473 | |
---|
| 474 | default: |
---|
| 475 | break; |
---|
| 476 | } |
---|
[5604] | 477 | } |
---|
| 478 | |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | if($isHTML) $mailService->setBodyHtml($body); else $mailService->setBodyText($body); |
---|
[5620] | 482 | |
---|
[5604] | 483 | if(imap_append($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, $mailService->getMessage(), "\\Seen \\Draft")) |
---|
| 484 | { |
---|
| 485 | $status = imap_status($mbox_stream, "{".$this->imap_server.":".$this->imap_port."}".$folder, SA_UIDNEXT); |
---|
| 486 | $return['id'] = $status->uidnext - 1; |
---|
| 487 | |
---|
| 488 | if($data['uidsSave'] ) |
---|
| 489 | $this->delete_msgs(array('folder'=> $folder , 'msgs_number' => $data['uidsSave'])); |
---|
| 490 | |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | if($mbox_stream) imap_close($mbox_stream); |
---|
[5620] | 494 | |
---|
[5604] | 495 | return $return; |
---|
| 496 | } |
---|
[5533] | 497 | } |
---|
[5136] | 498 | } |
---|
| 499 | |
---|
[5533] | 500 | public function delete( $URI, $justthese = false, $criteria = false ) |
---|
[5136] | 501 | { |
---|
[5533] | 502 | switch( $URI['concept'] ) |
---|
| 503 | { |
---|
| 504 | case 'labeled': |
---|
| 505 | { |
---|
| 506 | list($messageId, $labelId) = explode('#', $URI['id']); |
---|
[5586] | 507 | $folderName = dirname($messageId); |
---|
| 508 | $messageNumber = basename($messageId); |
---|
| 509 | |
---|
[5533] | 510 | if ($folderName && $messageNumber && $labelId) { |
---|
| 511 | $this->mbox = $this->open_mbox($folderName); |
---|
| 512 | imap_clearflag_full($this->mbox, $messageNumber, '$Label' . $labelId, ST_UID); |
---|
[5136] | 513 | |
---|
[5533] | 514 | imap_close($this->mbox); |
---|
| 515 | $this->mbox = false; |
---|
| 516 | } |
---|
| 517 | } |
---|
| 518 | case 'followupflagged': |
---|
| 519 | { |
---|
| 520 | list($messageId, $followupflaggedId) = explode('#', $URI['id']); |
---|
[5605] | 521 | |
---|
[5697] | 522 | $folderName = dirname($messageId); |
---|
| 523 | $messageNumber = basename($messageId); |
---|
[5533] | 524 | |
---|
| 525 | if ($folderName && $messageNumber && $followupflaggedId) { |
---|
[5697] | 526 | |
---|
[5533] | 527 | $this->mbox = $this->open_mbox($folderName); |
---|
[5711] | 528 | $f1 = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflag' . $followupflaggedId, ST_UID); |
---|
[5136] | 529 | |
---|
[5697] | 530 | /** |
---|
| 531 | * implementa a possibilidade de listar todas as mensagens sinalizadas através da busca |
---|
| 532 | */ |
---|
[5711] | 533 | $f2 = imap_clearflag_full($this->mbox, $messageNumber, '$Followupflagged', ST_UID); |
---|
[5697] | 534 | |
---|
| 535 | imap_close($this->mbox, CL_EXPUNGE); |
---|
[5533] | 536 | $this->mbox = false; |
---|
[5711] | 537 | |
---|
| 538 | return ($f1 && $f2); |
---|
[5533] | 539 | } |
---|
[5711] | 540 | |
---|
| 541 | return false; |
---|
[5533] | 542 | } |
---|
| 543 | } |
---|
[5136] | 544 | |
---|
[5533] | 545 | //TODO - return |
---|
| 546 | } |
---|
[5136] | 547 | |
---|
| 548 | public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar |
---|
| 549 | {} |
---|
| 550 | |
---|
| 551 | public function update( $URI, $data, $criteria = false ) |
---|
[5533] | 552 | { |
---|
[5570] | 553 | /** |
---|
| 554 | * Os únicos atributos que podem ser alterados no IMAP são folderName e messageId, |
---|
| 555 | * porém a operação de update desses atributos não faz sentido para o usuário da DataLayer, |
---|
| 556 | * pois na prática elas são executadas através das operações de CREATE e DELETE. |
---|
| 557 | * Assim, para os conceitos "labeled" e "followupflagged", só faz sentido o update de |
---|
| 558 | * atributos gravados no banco de dados e nunca no IMAP. |
---|
| 559 | */ |
---|
[5533] | 560 | } |
---|
[5136] | 561 | |
---|
[5533] | 562 | // public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false ) |
---|
| 563 | // { |
---|
| 564 | // return $this->read( array( 'id' => $id, |
---|
| 565 | // 'concept' => $concept, |
---|
| 566 | // 'context' => $parents ), $justthese ); |
---|
| 567 | // } |
---|
| 568 | |
---|
[5136] | 569 | public function replace( $URI, $data, $criteria = false ) |
---|
| 570 | {} |
---|
| 571 | |
---|
| 572 | public function close() |
---|
| 573 | {} |
---|
| 574 | |
---|
| 575 | public function setup() |
---|
| 576 | {} |
---|
| 577 | |
---|
[5341] | 578 | public function commit( $uri ) |
---|
| 579 | { return( true ); } |
---|
| 580 | |
---|
| 581 | public function rollback( $uri ) |
---|
| 582 | {} |
---|
| 583 | |
---|
| 584 | public function begin( $uri ) |
---|
| 585 | {} |
---|
| 586 | |
---|
| 587 | |
---|
[5136] | 588 | public function teardown() |
---|
| 589 | {} |
---|
| 590 | |
---|
| 591 | function to_utf8($in) |
---|
| 592 | { |
---|
[5533] | 593 | if (is_array($in)) { |
---|
| 594 | foreach ($in as $key => $value) { |
---|
| 595 | $out[$this->to_utf8($key)] = $this->to_utf8($value); |
---|
| 596 | } |
---|
| 597 | } elseif(is_string($in)) { |
---|
| 598 | return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' ); |
---|
| 599 | } else { |
---|
| 600 | return $in; |
---|
[5136] | 601 | } |
---|
[5533] | 602 | return $out; |
---|
[5204] | 603 | } |
---|
[5533] | 604 | |
---|
| 605 | |
---|
[5697] | 606 | private static function parseFilter($filter ,&$map){ |
---|
[5533] | 607 | |
---|
| 608 | if( !is_array( $filter ) || count($filter) <= 0) return null; |
---|
| 609 | |
---|
| 610 | $op = array_shift( $filter ); |
---|
| 611 | switch(strtolower($op)) |
---|
| 612 | { |
---|
| 613 | case 'and': { |
---|
| 614 | foreach ($filter as $term) |
---|
| 615 | self::parseFilter($term ,&$map); |
---|
| 616 | return; |
---|
| 617 | } |
---|
| 618 | case 'in': { |
---|
[5540] | 619 | if(is_array($map[$filter[0]]) && is_array($filter[1])) |
---|
| 620 | $map[$filter[0]] = array_unique(array_merge($map[$filter[0]], $filter[1])); |
---|
[5533] | 621 | return; |
---|
| 622 | } |
---|
| 623 | case '=': { |
---|
| 624 | $map[$filter[0]][] = $filter[1]; |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | } |
---|
[5697] | 628 | |
---|
[5136] | 629 | } |
---|