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