[7589] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
[7671] | 3 | * File : imap.php |
---|
| 4 | * Project : Z-Push |
---|
| 5 | * Descr : This backend is based on |
---|
| 6 | * 'BackendDiff' and implements an |
---|
| 7 | * IMAP interface |
---|
| 8 | * |
---|
| 9 | * Created : 10.10.2007 |
---|
| 10 | * |
---|
| 11 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
| 12 | * |
---|
| 13 | * This program is free software: you can redistribute it and/or modify |
---|
| 14 | * it under the terms of the GNU Affero General Public License, version 3, |
---|
| 15 | * as published by the Free Software Foundation with the following additional |
---|
| 16 | * term according to sec. 7: |
---|
| 17 | * |
---|
| 18 | * According to sec. 7 of the GNU Affero General Public License, version 3, |
---|
| 19 | * the terms of the AGPL are supplemented with the following terms: |
---|
| 20 | * |
---|
| 21 | * "Zarafa" is a registered trademark of Zarafa B.V. |
---|
| 22 | * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH |
---|
| 23 | * The licensing of the Program under the AGPL does not imply a trademark license. |
---|
| 24 | * Therefore any rights, title and interest in our trademarks remain entirely with us. |
---|
| 25 | * |
---|
| 26 | * However, if you propagate an unmodified version of the Program you are |
---|
| 27 | * allowed to use the term "Z-Push" to indicate that you distribute the Program. |
---|
| 28 | * Furthermore you may use our trademarks where it is necessary to indicate |
---|
| 29 | * the intended purpose of a product or service provided you use it in accordance |
---|
| 30 | * with honest practices in industrial or commercial matters. |
---|
| 31 | * If you want to propagate modified versions of the Program under the name "Z-Push", |
---|
| 32 | * you may only do so if you have a written permission by Zarafa Deutschland GmbH |
---|
| 33 | * (to acquire a permission please contact Zarafa at trademark@zarafa.com). |
---|
| 34 | * |
---|
| 35 | * This program is distributed in the hope that it will be useful, |
---|
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 38 | * GNU Affero General Public License for more details. |
---|
| 39 | * |
---|
| 40 | * You should have received a copy of the GNU Affero General Public License |
---|
| 41 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 42 | * |
---|
| 43 | * Consult LICENSE file for details |
---|
| 44 | ************************************************/ |
---|
[7589] | 45 | |
---|
| 46 | include_once(__DIR__.'/../../../lib/default/diffbackend/diffbackend.php'); |
---|
| 47 | include_once(__DIR__.'/../../../include/mimeDecode.php'); |
---|
| 48 | require_once(__DIR__.'/../../../include/z_RFC822.php'); |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | class ExpressoImapProvider extends BackendDiff { |
---|
| 52 | protected $wasteID; |
---|
| 53 | protected $sentID; |
---|
| 54 | protected $server; |
---|
| 55 | protected $mbox; |
---|
| 56 | protected $mboxFolder; |
---|
| 57 | protected $username; |
---|
| 58 | protected $domain; |
---|
| 59 | protected $serverdelimiter; |
---|
| 60 | protected $sinkfolders; |
---|
| 61 | protected $sinkstates; |
---|
[7671] | 62 | protected $excludedFolders; /* fmbiete's contribution r1527, ZP-319 */ |
---|
[7589] | 63 | |
---|
| 64 | /**---------------------------------------------------------------------------------------------------------- |
---|
| 65 | * default backend methods |
---|
| 66 | */ |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Authenticates the user |
---|
| 70 | * |
---|
| 71 | * @param string $username |
---|
| 72 | * @param string $domain |
---|
| 73 | * @param string $password |
---|
| 74 | * |
---|
| 75 | * @access public |
---|
| 76 | * @return boolean |
---|
| 77 | * @throws FatalException if php-imap module can not be found |
---|
| 78 | */ |
---|
| 79 | public function Logon($username, $domain, $password) { |
---|
| 80 | $this->wasteID = false; |
---|
| 81 | $this->sentID = false; |
---|
| 82 | $this->server = "{" . IMAP_SERVER . ":" . IMAP_PORT . "/imap" . IMAP_OPTIONS . "}"; |
---|
| 83 | |
---|
| 84 | if (!function_exists("imap_open")) |
---|
[7671] | 85 | throw new FatalException("BackendIMAP(): php-imap module is not installed", 0, null, LOGLEVEL_FATAL); |
---|
[7589] | 86 | |
---|
[7671] | 87 | /* BEGIN fmbiete's contribution r1527, ZP-319 */ |
---|
| 88 | $this->excludedFolders = array(); |
---|
| 89 | if (defined('IMAP_EXCLUDED_FOLDERS')) { |
---|
| 90 | $this->excludedFolders = explode("|", IMAP_EXCLUDED_FOLDERS); |
---|
| 91 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->Logon(): Excluding Folders (%s)", IMAP_EXCLUDED_FOLDERS)); |
---|
| 92 | } |
---|
| 93 | /* END fmbiete's contribution r1527, ZP-319 */ |
---|
| 94 | |
---|
[7589] | 95 | // open the IMAP-mailbox |
---|
| 96 | $this->mbox = @imap_open($this->server , $username, $password, OP_HALFOPEN); |
---|
| 97 | $this->mboxFolder = ""; |
---|
| 98 | |
---|
[7629] | 99 | |
---|
| 100 | $ldapConfig = parse_ini_file(EXPRESSO_PATH . '/prototype/config/OpenLDAP.srv' , true ); |
---|
| 101 | $ldapConfig = $ldapConfig['config']; |
---|
| 102 | $sr = ldap_search( $GLOBALS['connections']['ldap'] , $ldapConfig['context'] , "(uid=$username)" , array('uidNumber'), 0 , 1 ); |
---|
| 103 | if(!$sr) return false; |
---|
| 104 | |
---|
| 105 | $entries = ldap_get_entries( $GLOBALS['connections']['ldap'] , $sr ); |
---|
| 106 | $uidNumber = $entries[0]['uidnumber'][0]; |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | $rs = pg_query( $GLOBALS['connections']['db'], 'Select * FROM phpgw_preferences WHERE preference_owner IN (\'-2\',\'-1\',\''.$uidNumber.'\') AND preference_app = \'expressoMail\' ORDER BY preference_owner' ); |
---|
| 110 | |
---|
| 111 | $results = array(); |
---|
| 112 | while( $row = pg_fetch_assoc( $rs ) ) |
---|
| 113 | $results[] = $row; |
---|
| 114 | |
---|
| 115 | foreach($results as $result) |
---|
| 116 | { |
---|
| 117 | $ra = unserialize($result['preference_value']); |
---|
| 118 | if(isset($ra['save_in_folder'])) |
---|
| 119 | { |
---|
| 120 | $this->sentID = $ra['save_in_folder']; |
---|
| 121 | break; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
[7589] | 125 | if ($this->mbox) { |
---|
[7671] | 126 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->Logon(): User '%s' is authenticated on IMAP",$username)); |
---|
[7589] | 127 | $this->username = $username; |
---|
| 128 | $this->domain = $domain; |
---|
| 129 | // set serverdelimiter |
---|
| 130 | $this->serverdelimiter = $this->getServerDelimiter(); |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | else { |
---|
[7671] | 134 | ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->Logon(): can't connect: " . imap_last_error()); |
---|
[7589] | 135 | return false; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Logs off |
---|
| 141 | * Called before shutting down the request to close the IMAP connection |
---|
| 142 | * writes errors to the log |
---|
| 143 | * |
---|
| 144 | * @access public |
---|
| 145 | * @return boolean |
---|
| 146 | */ |
---|
| 147 | public function Logoff() { |
---|
| 148 | if ($this->mbox) { |
---|
| 149 | // list all errors |
---|
| 150 | $errors = imap_errors(); |
---|
| 151 | if (is_array($errors)) { |
---|
[7671] | 152 | foreach ($errors as $e) { |
---|
| 153 | if (stripos($e, "fail") !== false) { |
---|
[7589] | 154 | $level = LOGLEVEL_WARN; |
---|
[7671] | 155 | } |
---|
| 156 | else { |
---|
[7589] | 157 | $level = LOGLEVEL_DEBUG; |
---|
[7671] | 158 | } |
---|
| 159 | ZLog::Write($level, "BackendIMAP->Logoff(): IMAP said: " . $e); |
---|
| 160 | } |
---|
[7589] | 161 | } |
---|
| 162 | @imap_close($this->mbox); |
---|
[7671] | 163 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->Logoff(): IMAP connection closed"); |
---|
[7589] | 164 | } |
---|
| 165 | $this->SaveStorages(); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Sends an e-mail |
---|
| 170 | * This messages needs to be saved into the 'sent items' folder |
---|
| 171 | * |
---|
| 172 | * @param SyncSendMail $sm SyncSendMail object |
---|
| 173 | * |
---|
| 174 | * @access public |
---|
| 175 | * @return boolean |
---|
| 176 | * @throws StatusException |
---|
| 177 | */ |
---|
| 178 | // TODO implement , $saveInSent = true |
---|
| 179 | public function SendMail($sm) { |
---|
| 180 | $forward = $reply = (isset($sm->source->itemid) && $sm->source->itemid) ? $sm->source->itemid : false; |
---|
| 181 | $parent = false; |
---|
| 182 | |
---|
| 183 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("IMAPBackend->SendMail(): RFC822: %d bytes forward-id: '%s' reply-id: '%s' parent-id: '%s' SaveInSent: '%s' ReplaceMIME: '%s'", |
---|
[7671] | 184 | strlen($sm->mime), Utils::PrintAsString($sm->forwardflag), Utils::PrintAsString($sm->replyflag), |
---|
| 185 | Utils::PrintAsString((isset($sm->source->folderid) ? $sm->source->folderid : false)), |
---|
| 186 | Utils::PrintAsString(($sm->saveinsent)), Utils::PrintAsString(isset($sm->replacemime)) )); |
---|
[7589] | 187 | |
---|
| 188 | if (isset($sm->source->folderid) && $sm->source->folderid) |
---|
| 189 | // convert parent folder id back to work on an imap-id |
---|
| 190 | $parent = $this->getImapIdFromFolderId($sm->source->folderid); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | // by splitting the message in several lines we can easily grep later |
---|
| 194 | foreach(preg_split("/((\r)?\n)/", $sm->mime) as $rfc822line) |
---|
| 195 | ZLog::Write(LOGLEVEL_WBXML, "RFC822: ". $rfc822line); |
---|
| 196 | |
---|
| 197 | $mobj = new Mail_mimeDecode($sm->mime); |
---|
| 198 | $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); |
---|
| 199 | |
---|
| 200 | $Mail_RFC822 = new Mail_RFC822(); |
---|
| 201 | $toaddr = $ccaddr = $bccaddr = ""; |
---|
| 202 | if(isset($message->headers["to"])) |
---|
| 203 | $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); |
---|
| 204 | if(isset($message->headers["cc"])) |
---|
| 205 | $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); |
---|
| 206 | if(isset($message->headers["bcc"])) |
---|
| 207 | $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); |
---|
| 208 | |
---|
| 209 | // save some headers when forwarding mails (content type & transfer-encoding) |
---|
| 210 | $headers = ""; |
---|
| 211 | $forward_h_ct = ""; |
---|
| 212 | $forward_h_cte = ""; |
---|
| 213 | $envelopefrom = ""; |
---|
| 214 | |
---|
| 215 | $use_orgbody = false; |
---|
| 216 | |
---|
| 217 | // clean up the transmitted headers |
---|
| 218 | // remove default headers because we are using imap_mail |
---|
| 219 | $changedfrom = false; |
---|
| 220 | $returnPathSet = false; |
---|
| 221 | $body_base64 = false; |
---|
| 222 | $org_charset = ""; |
---|
| 223 | $org_boundary = false; |
---|
| 224 | $multipartmixed = false; |
---|
| 225 | foreach($message->headers as $k => $v) { |
---|
| 226 | if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") |
---|
| 227 | continue; |
---|
| 228 | |
---|
| 229 | if ($k == "content-type") { |
---|
| 230 | // if the message is a multipart message, then we should use the sent body |
---|
| 231 | if (preg_match("/multipart/i", $v)) { |
---|
| 232 | $use_orgbody = true; |
---|
| 233 | $org_boundary = $message->ctype_parameters["boundary"]; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | // save the original content-type header for the body part when forwarding |
---|
| 237 | if ($sm->forwardflag && !$use_orgbody) { |
---|
| 238 | $forward_h_ct = $v; |
---|
| 239 | continue; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | // set charset always to utf-8 |
---|
| 243 | $org_charset = $v; |
---|
| 244 | $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | if ($k == "content-transfer-encoding") { |
---|
| 248 | // if the content was base64 encoded, encode the body again when sending |
---|
| 249 | if (trim($v) == "base64") $body_base64 = true; |
---|
| 250 | |
---|
| 251 | // save the original encoding header for the body part when forwarding |
---|
| 252 | if ($sm->forwardflag) { |
---|
| 253 | $forward_h_cte = $v; |
---|
| 254 | continue; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // check if "from"-header is set, do nothing if it's set |
---|
| 259 | // else set it to IMAP_DEFAULTFROM |
---|
| 260 | if ($k == "from") { |
---|
| 261 | if (trim($v)) { |
---|
| 262 | $changedfrom = true; |
---|
| 263 | } elseif (! trim($v) && IMAP_DEFAULTFROM) { |
---|
| 264 | $changedfrom = true; |
---|
| 265 | if (IMAP_DEFAULTFROM == 'username') $v = $this->username; |
---|
| 266 | else if (IMAP_DEFAULTFROM == 'domain') $v = $this->domain; |
---|
| 267 | else $v = $this->username . IMAP_DEFAULTFROM; |
---|
| 268 | $envelopefrom = "-f$v"; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | // check if "Return-Path"-header is set |
---|
| 273 | if ($k == "return-path") { |
---|
| 274 | $returnPathSet = true; |
---|
| 275 | if (! trim($v) && IMAP_DEFAULTFROM) { |
---|
| 276 | if (IMAP_DEFAULTFROM == 'username') $v = $this->username; |
---|
| 277 | else if (IMAP_DEFAULTFROM == 'domain') $v = $this->domain; |
---|
| 278 | else $v = $this->username . IMAP_DEFAULTFROM; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | // all other headers stay |
---|
| 283 | if ($headers) $headers .= "\n"; |
---|
| 284 | $headers .= ucfirst($k) . ": ". $v; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | // set "From" header if not set on the device |
---|
| 288 | if(IMAP_DEFAULTFROM && !$changedfrom){ |
---|
| 289 | if (IMAP_DEFAULTFROM == 'username') $v = $this->username; |
---|
| 290 | else if (IMAP_DEFAULTFROM == 'domain') $v = $this->domain; |
---|
| 291 | else $v = $this->username . IMAP_DEFAULTFROM; |
---|
| 292 | if ($headers) $headers .= "\n"; |
---|
| 293 | $headers .= 'From: '.$v; |
---|
| 294 | $envelopefrom = "-f$v"; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // set "Return-Path" header if not set on the device |
---|
| 298 | if(IMAP_DEFAULTFROM && !$returnPathSet){ |
---|
| 299 | if (IMAP_DEFAULTFROM == 'username') $v = $this->username; |
---|
| 300 | else if (IMAP_DEFAULTFROM == 'domain') $v = $this->domain; |
---|
| 301 | else $v = $this->username . IMAP_DEFAULTFROM; |
---|
| 302 | if ($headers) $headers .= "\n"; |
---|
| 303 | $headers .= 'Return-Path: '.$v; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | // if this is a multipart message with a boundary, we must use the original body |
---|
| 307 | if ($use_orgbody) { |
---|
| 308 | list(,$body) = $mobj->_splitBodyHeader($sm->mime); |
---|
| 309 | $repl_body = $this->getBody($message); |
---|
| 310 | } |
---|
| 311 | else |
---|
| 312 | $body = $this->getBody($message); |
---|
| 313 | |
---|
| 314 | // reply |
---|
| 315 | if ($sm->replyflag && $parent) { |
---|
| 316 | $this->imap_reopenFolder($parent); |
---|
| 317 | // receive entire mail (header + body) to decode body correctly |
---|
| 318 | $origmail = @imap_fetchheader($this->mbox, $reply, FT_UID) . @imap_body($this->mbox, $reply, FT_PEEK | FT_UID); |
---|
| 319 | if (!$origmail) |
---|
[7671] | 320 | throw new StatusException(sprintf("BackendIMAP->SendMail(): Could not open message id '%s' in folder id '%s' to be replied: %s", $reply, $parent, imap_last_error()), SYNC_COMMONSTATUS_ITEMNOTFOUND); |
---|
[7589] | 321 | |
---|
| 322 | $mobj2 = new Mail_mimeDecode($origmail); |
---|
| 323 | // receive only body |
---|
| 324 | $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8'))); |
---|
| 325 | // unset mimedecoder & origmail - free memory |
---|
| 326 | unset($mobj2); |
---|
| 327 | unset($origmail); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | // encode the body to base64 if it was sent originally in base64 by the pda |
---|
| 331 | // contrib - chunk base64 encoded body |
---|
| 332 | if ($body_base64 && !$sm->forwardflag) $body = chunk_split(base64_encode($body)); |
---|
| 333 | |
---|
| 334 | |
---|
| 335 | // forward |
---|
| 336 | if ($sm->forwardflag && $parent) { |
---|
| 337 | $this->imap_reopenFolder($parent); |
---|
| 338 | // receive entire mail (header + body) |
---|
| 339 | $origmail = @imap_fetchheader($this->mbox, $forward, FT_UID) . @imap_body($this->mbox, $forward, FT_PEEK | FT_UID); |
---|
| 340 | |
---|
| 341 | if (!$origmail) |
---|
[7671] | 342 | throw new StatusException(sprintf("BackendIMAP->SendMail(): Could not open message id '%s' in folder id '%s' to be forwarded: %s", $forward, $parent, imap_last_error()), SYNC_COMMONSTATUS_ITEMNOTFOUND); |
---|
[7589] | 343 | |
---|
| 344 | if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) { |
---|
| 345 | // contrib - chunk base64 encoded body |
---|
| 346 | if ($body_base64) $body = chunk_split(base64_encode($body)); |
---|
| 347 | //use original boundary if it's set |
---|
| 348 | $boundary = ($org_boundary) ? $org_boundary : false; |
---|
| 349 | // build a new mime message, forward entire old mail as file |
---|
| 350 | list($aheader, $body) = $this->mail_attach("forwarded_message.eml",strlen($origmail),$origmail, $body, $forward_h_ct, $forward_h_cte,$boundary); |
---|
| 351 | // add boundary headers |
---|
| 352 | $headers .= "\n" . $aheader; |
---|
| 353 | |
---|
| 354 | } |
---|
| 355 | else { |
---|
| 356 | $mobj2 = new Mail_mimeDecode($origmail); |
---|
| 357 | $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); |
---|
| 358 | |
---|
| 359 | if (!$use_orgbody) |
---|
| 360 | $nbody = $body; |
---|
| 361 | else |
---|
| 362 | $nbody = $repl_body; |
---|
| 363 | |
---|
| 364 | $nbody .= "\r\n\r\n"; |
---|
| 365 | $nbody .= "-----Original Message-----\r\n"; |
---|
| 366 | if(isset($mess2->headers['from'])) |
---|
| 367 | $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; |
---|
| 368 | if(isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) |
---|
| 369 | $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; |
---|
| 370 | if(isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) |
---|
| 371 | $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; |
---|
| 372 | if(isset($mess2->headers['date'])) |
---|
| 373 | $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; |
---|
| 374 | if(isset($mess2->headers['subject'])) |
---|
| 375 | $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; |
---|
| 376 | $nbody .= "\r\n"; |
---|
| 377 | $nbody .= $this->getBody($mess2); |
---|
| 378 | |
---|
| 379 | if ($body_base64) { |
---|
| 380 | // contrib - chunk base64 encoded body |
---|
| 381 | $nbody = chunk_split(base64_encode($nbody)); |
---|
| 382 | if ($use_orgbody) |
---|
[7671] | 383 | // contrib - chunk base64 encoded body |
---|
[7589] | 384 | $repl_body = chunk_split(base64_encode($repl_body)); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | if ($use_orgbody) { |
---|
[7671] | 388 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): -------------------"); |
---|
| 389 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): old:\n'$repl_body'\nnew:\n'$nbody'\nund der body:\n'$body'"); |
---|
[7589] | 390 | //$body is quoted-printable encoded while $repl_body and $nbody are plain text, |
---|
| 391 | //so we need to decode $body in order replace to take place |
---|
| 392 | $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); |
---|
| 393 | } |
---|
| 394 | else |
---|
| 395 | $body = $nbody; |
---|
| 396 | |
---|
| 397 | |
---|
| 398 | if(isset($mess2->parts)) { |
---|
| 399 | $attached = false; |
---|
| 400 | |
---|
| 401 | if ($org_boundary) { |
---|
| 402 | $att_boundary = $org_boundary; |
---|
| 403 | // cut end boundary from body |
---|
| 404 | $body = substr($body, 0, strrpos($body, "--$att_boundary--")); |
---|
| 405 | } |
---|
| 406 | else { |
---|
| 407 | $att_boundary = strtoupper(md5(uniqid(time()))); |
---|
| 408 | // add boundary headers |
---|
| 409 | $headers .= "\n" . "Content-Type: multipart/mixed; boundary=$att_boundary"; |
---|
| 410 | $multipartmixed = true; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | foreach($mess2->parts as $part) { |
---|
| 414 | if(isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { |
---|
| 415 | |
---|
| 416 | if(isset($part->d_parameters['filename'])) |
---|
| 417 | $attname = $part->d_parameters['filename']; |
---|
| 418 | else if(isset($part->ctype_parameters['name'])) |
---|
| 419 | $attname = $part->ctype_parameters['name']; |
---|
| 420 | else if(isset($part->headers['content-description'])) |
---|
| 421 | $attname = $part->headers['content-description']; |
---|
| 422 | else $attname = "unknown attachment"; |
---|
| 423 | |
---|
| 424 | // ignore html content |
---|
| 425 | if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { |
---|
| 426 | continue; |
---|
| 427 | } |
---|
| 428 | // |
---|
| 429 | if ($use_orgbody || $attached) { |
---|
| 430 | $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); |
---|
| 431 | } |
---|
| 432 | // first attachment |
---|
| 433 | else { |
---|
| 434 | $encmail = $body; |
---|
| 435 | $attached = true; |
---|
| 436 | $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); |
---|
| 437 | $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | if ($multipartmixed && strpos(strtolower($mess2->headers['content-type']), "alternative") !== false) { |
---|
| 442 | //this happens if a multipart/alternative message is forwarded |
---|
| 443 | //then it's a multipart/mixed message which consists of: |
---|
| 444 | //1. text/plain part which was written on the mobile |
---|
| 445 | //2. multipart/alternative part which is the original message |
---|
| 446 | $body = "This is a message with multiple parts in MIME format.\n--". |
---|
[7671] | 447 | $att_boundary. |
---|
| 448 | "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". |
---|
| 449 | (($body_base64) ? chunk_split(base64_encode($message->body)) : rtrim($message->body)). |
---|
| 450 | "\n--".$att_boundary. |
---|
| 451 | "\nContent-Type: {$mess2->headers['content-type']}\n\n". |
---|
| 452 | @imap_body($this->mbox, $forward, FT_PEEK | FT_UID)."\n\n"; |
---|
[7589] | 453 | } |
---|
| 454 | $body .= "--$att_boundary--\n\n"; |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | unset($mobj2); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | // unset origmail - free memory |
---|
| 461 | unset($origmail); |
---|
| 462 | |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | // remove carriage-returns from body |
---|
| 466 | $body = str_replace("\r\n", "\n", $body); |
---|
| 467 | |
---|
| 468 | if (!$multipartmixed) { |
---|
| 469 | if (!empty($forward_h_ct)) $headers .= "\nContent-Type: $forward_h_ct"; |
---|
| 470 | if (!empty($forward_h_cte)) $headers .= "\nContent-Transfer-Encoding: $forward_h_cte"; |
---|
[7671] | 471 | // if body was quoted-printable, convert it again |
---|
[7589] | 472 | if (isset($message->headers["content-transfer-encoding"]) && strtolower($message->headers["content-transfer-encoding"]) == "quoted-printable") { |
---|
| 473 | $body = quoted_printable_encode($body); |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | // more debugging |
---|
[7671] | 478 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): parsed message: ". print_r($message,1)); |
---|
| 479 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): headers: $headers"); |
---|
| 480 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 481 | if (isset($message->headers["subject"])) { |
---|
| 482 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): subject: {$message->headers["subject"]}"); |
---|
| 483 | } |
---|
| 484 | else { |
---|
| 485 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): subject: no subject set. Set to empty."); |
---|
| 486 | $message->headers["subject"] = ""; // added by mku ZP-330 |
---|
| 487 | } |
---|
| 488 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
| 489 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): body: $body"); |
---|
[7589] | 490 | |
---|
| 491 | if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { |
---|
[7671] | 492 | // changed by mku ZP-330 |
---|
[7589] | 493 | $send = @imap_mail ( $toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); |
---|
| 494 | } |
---|
| 495 | else { |
---|
| 496 | if (!empty($ccaddr)) $headers .= "\nCc: $ccaddr"; |
---|
| 497 | if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; |
---|
[7671] | 498 | // changed by mku ZP-330 |
---|
[7589] | 499 | $send = @mail ( $toaddr, $message->headers["subject"], $body, $headers, $envelopefrom ); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | // email sent? |
---|
| 503 | if (!$send) |
---|
[7671] | 504 | throw new StatusException(sprintf("BackendIMAP->SendMail(): The email could not be sent. Last IMAP-error: %s", imap_last_error()), SYNC_COMMONSTATUS_MAILSUBMISSIONFAILED); |
---|
[7589] | 505 | |
---|
| 506 | // add message to the sent folder |
---|
| 507 | // build complete headers |
---|
| 508 | $headers .= "\nTo: $toaddr"; |
---|
[7671] | 509 | $headers .= "\nSubject: " . $message->headers["subject"]; // changed by mku ZP-330 |
---|
[7589] | 510 | |
---|
| 511 | if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { |
---|
| 512 | if (!empty($ccaddr)) $headers .= "\nCc: $ccaddr"; |
---|
| 513 | if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; |
---|
| 514 | } |
---|
[7671] | 515 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): complete headers: $headers"); |
---|
[7589] | 516 | |
---|
| 517 | $asf = false; |
---|
| 518 | if ($this->sentID) { |
---|
| 519 | $asf = $this->addSentMessage($this->sentID, $headers, $body); |
---|
| 520 | } |
---|
| 521 | else if (IMAP_SENTFOLDER) { |
---|
| 522 | $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); |
---|
[7671] | 523 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): Outgoing mail saved in configured 'Sent' folder '%s': %s", IMAP_SENTFOLDER, Utils::PrintAsString($asf))); |
---|
[7589] | 524 | } |
---|
| 525 | // No Sent folder set, try defaults |
---|
| 526 | else { |
---|
[7671] | 527 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): No Sent mailbox set"); |
---|
[7589] | 528 | if($this->addSentMessage("INBOX.Sent", $headers, $body)) { |
---|
[7671] | 529 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Outgoing mail saved in 'INBOX.Sent'"); |
---|
[7589] | 530 | $asf = true; |
---|
| 531 | } |
---|
| 532 | else if ($this->addSentMessage("Sent", $headers, $body)) { |
---|
[7671] | 533 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Outgoing mail saved in 'Sent'"); |
---|
[7589] | 534 | $asf = true; |
---|
| 535 | } |
---|
| 536 | else if ($this->addSentMessage("Sent Items", $headers, $body)) { |
---|
[7671] | 537 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail():IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); |
---|
[7589] | 538 | $asf = true; |
---|
| 539 | } |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | if (!$asf) { |
---|
[7671] | 543 | ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->SendMail(): The email could not be saved to Sent Items folder. Check your configuration."); |
---|
[7589] | 544 | } |
---|
| 545 | |
---|
| 546 | return $send; |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | /** |
---|
| 550 | * Returns the waste basket |
---|
| 551 | * |
---|
| 552 | * @access public |
---|
| 553 | * @return string |
---|
| 554 | */ |
---|
| 555 | public function GetWasteBasket() { |
---|
| 556 | // TODO this could be retrieved from the DeviceFolderCache |
---|
| 557 | if ($this->wasteID == false) { |
---|
| 558 | //try to get the waste basket without doing complete hierarchy sync |
---|
[7706] | 559 | $wastebaskt = @imap_getmailboxes($this->mbox, $this->server, IMAP_TRASHFOLDER); |
---|
[7589] | 560 | if (isset($wastebaskt[0])) { |
---|
| 561 | $this->wasteID = $this->convertImapId(substr($wastebaskt[0]->name, strlen($this->server))); |
---|
| 562 | return $this->wasteID; |
---|
| 563 | } |
---|
| 564 | //try get waste id from hierarchy if it wasn't possible with above for some reason |
---|
| 565 | $this->GetHierarchy(); |
---|
| 566 | } |
---|
| 567 | return $this->wasteID; |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | /** |
---|
| 571 | * Returns the content of the named attachment as stream. The passed attachment identifier is |
---|
| 572 | * the exact string that is returned in the 'AttName' property of an SyncAttachment. |
---|
| 573 | * Any information necessary to find the attachment must be encoded in that 'attname' property. |
---|
| 574 | * Data is written directly (with print $data;) |
---|
| 575 | * |
---|
| 576 | * @param string $attname |
---|
| 577 | * |
---|
| 578 | * @access public |
---|
| 579 | * @return SyncItemOperationsAttachment |
---|
| 580 | * @throws StatusException |
---|
| 581 | */ |
---|
| 582 | public function GetAttachmentData($attname) { |
---|
[7671] | 583 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetAttachmentData('%s')", $attname)); |
---|
[7589] | 584 | |
---|
| 585 | list($folderid, $id, $part) = explode(":", $attname); |
---|
| 586 | |
---|
| 587 | if (!$folderid || !$id || !$part) |
---|
[7671] | 588 | throw new StatusException(sprintf("BackendIMAP->GetAttachmentData('%s'): Error, attachment name key can not be parsed", $attname), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT); |
---|
[7589] | 589 | |
---|
| 590 | // convert back to work on an imap-id |
---|
| 591 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 592 | |
---|
| 593 | $this->imap_reopenFolder($folderImapid); |
---|
| 594 | $mail = @imap_fetchheader($this->mbox, $id, FT_UID) . @imap_body($this->mbox, $id, FT_PEEK | FT_UID); |
---|
| 595 | |
---|
| 596 | $mobj = new Mail_mimeDecode($mail); |
---|
| 597 | $message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); |
---|
| 598 | |
---|
[7671] | 599 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 600 | //trying parts |
---|
| 601 | $mparts = $message->parts; |
---|
| 602 | for ($i = 0; $i < count($mparts); $i++) { |
---|
| 603 | $auxpart = $mparts[$i]; |
---|
| 604 | //recursively add parts |
---|
| 605 | if($auxpart->ctype_primary == "multipart" && ($auxpart->ctype_secondary == "mixed" || $auxpart->ctype_secondary == "alternative" || $auxpart->ctype_secondary == "related")) { |
---|
| 606 | foreach($auxpart->parts as $spart) |
---|
| 607 | $mparts[] = $spart; |
---|
| 608 | } |
---|
| 609 | } |
---|
| 610 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
[7589] | 611 | |
---|
[7671] | 612 | if (!isset($mparts[$part]->body)) |
---|
| 613 | throw new StatusException(sprintf("BackendIMAP->GetAttachmentData('%s'): Error, requested part key can not be found: '%d'", $attname, $part), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT); |
---|
| 614 | |
---|
[7589] | 615 | // unset mimedecoder & mail |
---|
| 616 | unset($mobj); |
---|
| 617 | unset($mail); |
---|
| 618 | |
---|
| 619 | include_once('include/stringstreamwrapper.php'); |
---|
| 620 | $attachment = new SyncItemOperationsAttachment(); |
---|
[7671] | 621 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 622 | $attachment->data = StringStreamWrapper::Open($mparts[$part]->body); |
---|
| 623 | if (isset($mparts[$part]->ctype_primary) && isset($mparts[$part]->ctype_secondary)) |
---|
| 624 | $attachment->contenttype = $mparts[$part]->ctype_primary .'/'.$mparts[$part]->ctype_secondary; |
---|
[7589] | 625 | |
---|
[7671] | 626 | unset($mparts); |
---|
| 627 | unset($message); |
---|
| 628 | |
---|
| 629 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetAttachmentData contenttype %s", $attachment->contenttype)); |
---|
| 630 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
| 631 | |
---|
[7589] | 632 | return $attachment; |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | /** |
---|
| 636 | * Indicates if the backend has a ChangesSink. |
---|
| 637 | * A sink is an active notification mechanism which does not need polling. |
---|
| 638 | * The IMAP backend simulates a sink by polling status information of the folder |
---|
| 639 | * |
---|
| 640 | * @access public |
---|
| 641 | * @return boolean |
---|
| 642 | */ |
---|
| 643 | public function HasChangesSink() { |
---|
| 644 | $this->sinkfolders = array(); |
---|
| 645 | $this->sinkstates = array(); |
---|
| 646 | return true; |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | /** |
---|
| 650 | * The folder should be considered by the sink. |
---|
| 651 | * Folders which were not initialized should not result in a notification |
---|
| 652 | * of IBacken->ChangesSink(). |
---|
| 653 | * |
---|
| 654 | * @param string $folderid |
---|
| 655 | * |
---|
| 656 | * @access public |
---|
| 657 | * @return boolean false if found can not be found |
---|
| 658 | */ |
---|
| 659 | public function ChangesSinkInitialize($folderid) { |
---|
| 660 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("IMAPBackend->ChangesSinkInitialize(): folderid '%s'", $folderid)); |
---|
| 661 | |
---|
| 662 | $imapid = $this->getImapIdFromFolderId($folderid); |
---|
| 663 | |
---|
| 664 | if ($imapid) { |
---|
| 665 | $this->sinkfolders[] = $imapid; |
---|
| 666 | return true; |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | return false; |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | /** |
---|
| 673 | * The actual ChangesSink. |
---|
| 674 | * For max. the $timeout value this method should block and if no changes |
---|
| 675 | * are available return an empty array. |
---|
| 676 | * If changes are available a list of folderids is expected. |
---|
| 677 | * |
---|
| 678 | * @param int $timeout max. amount of seconds to block |
---|
| 679 | * |
---|
| 680 | * @access public |
---|
| 681 | * @return array |
---|
| 682 | */ |
---|
| 683 | public function ChangesSink($timeout = 30) { |
---|
| 684 | $notifications = array(); |
---|
| 685 | $stopat = time() + $timeout - 1; |
---|
| 686 | |
---|
| 687 | while($stopat > time() && empty($notifications)) { |
---|
| 688 | foreach ($this->sinkfolders as $imapid) { |
---|
| 689 | $this->imap_reopenFolder($imapid); |
---|
| 690 | |
---|
| 691 | // courier-imap only cleares the status cache after checking |
---|
| 692 | @imap_check($this->mbox); |
---|
| 693 | |
---|
| 694 | $status = @imap_status($this->mbox, $this->server . $imapid, SA_ALL); |
---|
| 695 | if (!$status) { |
---|
| 696 | ZLog::Write(LOGLEVEL_WARN, sprintf("ChangesSink: could not stat folder '%s': %s ", $this->getFolderIdFromImapId($imapid), imap_last_error())); |
---|
| 697 | } |
---|
| 698 | else { |
---|
| 699 | $newstate = "M:". $status->messages ."-R:". $status->recent ."-U:". $status->unseen; |
---|
| 700 | |
---|
| 701 | if (! isset($this->sinkstates[$imapid]) ) |
---|
| 702 | $this->sinkstates[$imapid] = $newstate; |
---|
| 703 | |
---|
| 704 | if ($this->sinkstates[$imapid] != $newstate) { |
---|
| 705 | $notifications[] = $this->getFolderIdFromImapId($imapid); |
---|
| 706 | $this->sinkstates[$imapid] = $newstate; |
---|
| 707 | } |
---|
| 708 | } |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | if (empty($notifications)) |
---|
| 712 | sleep(5); |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | return $notifications; |
---|
| 716 | } |
---|
| 717 | |
---|
| 718 | |
---|
| 719 | /**---------------------------------------------------------------------------------------------------------- |
---|
| 720 | * implemented DiffBackend methods |
---|
| 721 | */ |
---|
| 722 | |
---|
| 723 | |
---|
| 724 | /** |
---|
| 725 | * Returns a list (array) of folders. |
---|
| 726 | * |
---|
| 727 | * @access public |
---|
| 728 | * @return array/boolean false if the list could not be retrieved |
---|
| 729 | */ |
---|
| 730 | public function GetFolderList() { |
---|
| 731 | $folders = array(); |
---|
| 732 | |
---|
[7671] | 733 | $list = @imap_getmailboxes($this->mbox, $this->server, "*"); |
---|
[7589] | 734 | if (is_array($list)) { |
---|
| 735 | // reverse list to obtain folders in right order |
---|
| 736 | $list = array_reverse($list); |
---|
| 737 | |
---|
| 738 | foreach ($list as $val) { |
---|
[7671] | 739 | /* BEGIN fmbiete's contribution r1527, ZP-319 */ |
---|
| 740 | // don't return the excluded folders |
---|
| 741 | $notExcluded = true; |
---|
| 742 | for ($i = 0, $cnt = count($this->excludedFolders); $notExcluded && $i < $cnt; $i++) { // expr1, expr2 modified by mku ZP-329 |
---|
| 743 | // fix exclude folders with special chars by mku ZP-329 |
---|
| 744 | if (strpos(strtolower($val->name), strtolower(Utils::Utf7_iconv_encode(Utils::Utf8_to_utf7($this->excludedFolders[$i])))) !== false) { |
---|
| 745 | $notExcluded = false; |
---|
| 746 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("Pattern: <%s> found, excluding folder: '%s'", $this->excludedFolders[$i], $val->name)); // sprintf added by mku ZP-329 |
---|
| 747 | } |
---|
| 748 | } |
---|
[7589] | 749 | |
---|
[7671] | 750 | if ($notExcluded) { |
---|
| 751 | $box = array(); |
---|
| 752 | // cut off serverstring |
---|
| 753 | $imapid = substr($val->name, strlen($this->server)); |
---|
| 754 | $box["id"] = $this->convertImapId($imapid); |
---|
| 755 | |
---|
| 756 | $fhir = explode($val->delimiter, $imapid); |
---|
| 757 | if (count($fhir) > 1) { |
---|
| 758 | $this->getModAndParentNames($fhir, $box["mod"], $imapparent); |
---|
| 759 | $box["parent"] = $this->convertImapId($imapparent); |
---|
| 760 | } |
---|
| 761 | else { |
---|
| 762 | $box["mod"] = $imapid; |
---|
| 763 | $box["parent"] = "0"; |
---|
| 764 | } |
---|
| 765 | $folders[]=$box; |
---|
| 766 | /* END fmbiete's contribution r1527, ZP-319 */ |
---|
[7589] | 767 | } |
---|
| 768 | } |
---|
| 769 | } |
---|
| 770 | else { |
---|
[7671] | 771 | ZLog::Write(LOGLEVEL_WARN, "BackendIMAP->GetFolderList(): imap_list failed: " . imap_last_error()); |
---|
[7589] | 772 | return false; |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | return $folders; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | /** |
---|
| 779 | * Returns an actual SyncFolder object |
---|
| 780 | * |
---|
| 781 | * @param string $id id of the folder |
---|
| 782 | * |
---|
| 783 | * @access public |
---|
| 784 | * @return object SyncFolder with information |
---|
| 785 | */ |
---|
| 786 | public function GetFolder($id) { |
---|
| 787 | $folder = new SyncFolder(); |
---|
| 788 | $folder->serverid = $id; |
---|
| 789 | |
---|
| 790 | // convert back to work on an imap-id |
---|
| 791 | $imapid = $this->getImapIdFromFolderId($id); |
---|
| 792 | |
---|
| 793 | // explode hierarchy |
---|
| 794 | $fhir = explode($this->serverdelimiter, $imapid); |
---|
| 795 | |
---|
| 796 | // compare on lowercase strings |
---|
| 797 | $lid = strtolower($imapid); |
---|
| 798 | // TODO WasteID or SentID could be saved for later ussage |
---|
| 799 | if($lid == "inbox") { |
---|
| 800 | $folder->parentid = "0"; // Root |
---|
| 801 | $folder->displayname = "Inbox"; |
---|
| 802 | $folder->type = SYNC_FOLDER_TYPE_INBOX; |
---|
| 803 | } |
---|
[7706] | 804 | // // Zarafa IMAP-Gateway outputs |
---|
| 805 | // else if($lid == "drafts") { |
---|
| 806 | // $folder->parentid = "0"; |
---|
| 807 | // $folder->displayname = "Drafts"; |
---|
| 808 | // $folder->type = SYNC_FOLDER_TYPE_DRAFTS; |
---|
| 809 | // } |
---|
| 810 | // else if($lid == "trash") { |
---|
| 811 | // $folder->parentid = "0"; |
---|
| 812 | // $folder->displayname = "Trash"; |
---|
| 813 | // $folder->type = SYNC_FOLDER_TYPE_WASTEBASKET; |
---|
| 814 | // $this->wasteID = $id; |
---|
| 815 | // } |
---|
| 816 | // else if($lid == "sent" || $lid == "sent items" || $lid == IMAP_SENTFOLDER) { |
---|
| 817 | // $folder->parentid = "0"; |
---|
| 818 | // $folder->displayname = "Sent"; |
---|
| 819 | // $folder->type = SYNC_FOLDER_TYPE_SENTMAIL; |
---|
| 820 | // //$this->sentID = $id; |
---|
| 821 | // } |
---|
[7589] | 822 | // courier-imap outputs and cyrus-imapd outputs |
---|
[7706] | 823 | //else if( $lid == "inbox.drafts" || $lid == "inbox/drafts") { |
---|
[7709] | 824 | else if( $lid == strtolower(IMAP_DRAFTFOLDER) ) { |
---|
[7589] | 825 | $folder->parentid = $this->convertImapId($fhir[0]); |
---|
| 826 | $folder->displayname = "Drafts"; |
---|
| 827 | $folder->type = SYNC_FOLDER_TYPE_DRAFTS; |
---|
| 828 | } |
---|
[7709] | 829 | else if($lid == strtolower(IMAP_TRASHFOLDER) ) { |
---|
[7589] | 830 | $folder->parentid = $this->convertImapId($fhir[0]); |
---|
| 831 | $folder->displayname = "Trash"; |
---|
| 832 | $folder->type = SYNC_FOLDER_TYPE_WASTEBASKET; |
---|
| 833 | $this->wasteID = $id; |
---|
| 834 | } |
---|
[7709] | 835 | else if($lid == strtolower(IMAP_SENTFOLDER) ) { |
---|
[7589] | 836 | $folder->parentid = $this->convertImapId($fhir[0]); |
---|
| 837 | $folder->displayname = "Sent"; |
---|
| 838 | $folder->type = SYNC_FOLDER_TYPE_SENTMAIL; |
---|
[7629] | 839 | //$this->sentID = $id; |
---|
[7589] | 840 | } |
---|
| 841 | |
---|
| 842 | // define the rest as other-folders |
---|
| 843 | else { |
---|
| 844 | if (count($fhir) > 1) { |
---|
| 845 | $this->getModAndParentNames($fhir, $folder->displayname, $imapparent); |
---|
| 846 | $folder->parentid = $this->convertImapId($imapparent); |
---|
| 847 | $folder->displayname = Utils::Utf7_to_utf8(Utils::Utf7_iconv_decode($folder->displayname)); |
---|
| 848 | } |
---|
| 849 | else { |
---|
| 850 | $folder->displayname = Utils::Utf7_to_utf8(Utils::Utf7_iconv_decode($imapid)); |
---|
| 851 | $folder->parentid = "0"; |
---|
| 852 | } |
---|
[7671] | 853 | $folder->type = SYNC_FOLDER_TYPE_USER_MAIL; |
---|
[7589] | 854 | } |
---|
| 855 | |
---|
| 856 | //advanced debugging |
---|
[7671] | 857 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetFolder('%s'): '%s'", $id, $folder)); |
---|
[7589] | 858 | |
---|
| 859 | return $folder; |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | /** |
---|
| 863 | * Returns folder stats. An associative array with properties is expected. |
---|
| 864 | * |
---|
| 865 | * @param string $id id of the folder |
---|
| 866 | * |
---|
| 867 | * @access public |
---|
| 868 | * @return array |
---|
| 869 | */ |
---|
| 870 | public function StatFolder($id) { |
---|
| 871 | $folder = $this->GetFolder($id); |
---|
| 872 | |
---|
| 873 | $stat = array(); |
---|
| 874 | $stat["id"] = $id; |
---|
| 875 | $stat["parent"] = $folder->parentid; |
---|
| 876 | $stat["mod"] = $folder->displayname; |
---|
| 877 | |
---|
| 878 | return $stat; |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | /** |
---|
| 882 | * Creates or modifies a folder |
---|
| 883 | * The folder type is ignored in IMAP, as all folders are Email folders |
---|
| 884 | * |
---|
| 885 | * @param string $folderid id of the parent folder |
---|
| 886 | * @param string $oldid if empty -> new folder created, else folder is to be renamed |
---|
| 887 | * @param string $displayname new folder name (to be created, or to be renamed to) |
---|
| 888 | * @param int $type folder type |
---|
| 889 | * |
---|
| 890 | * @access public |
---|
| 891 | * @return boolean status |
---|
| 892 | * @throws StatusException could throw specific SYNC_FSSTATUS_* exceptions |
---|
| 893 | * |
---|
| 894 | */ |
---|
| 895 | public function ChangeFolder($folderid, $oldid, $displayname, $type){ |
---|
[7671] | 896 | ZLog::Write(LOGLEVEL_INFO, sprintf("BackendIMAP->ChangeFolder('%s','%s','%s','%s')", $folderid, $oldid, $displayname, $type)); |
---|
[7589] | 897 | |
---|
| 898 | // go to parent mailbox |
---|
| 899 | $this->imap_reopenFolder($folderid); |
---|
| 900 | |
---|
| 901 | // build name for new mailboxBackendMaildir |
---|
| 902 | $displayname = Utils::Utf7_iconv_encode(Utils::Utf8_to_utf7($displayname)); |
---|
| 903 | $newname = $this->server . $folderid . $this->serverdelimiter . $displayname; |
---|
| 904 | |
---|
| 905 | $csts = false; |
---|
| 906 | // if $id is set => rename mailbox, otherwise create |
---|
| 907 | if ($oldid) { |
---|
| 908 | // rename doesn't work properly with IMAP |
---|
| 909 | // the activesync client doesn't support a 'changing ID' |
---|
| 910 | // TODO this would be solved by implementing hex ids (Mantis #459) |
---|
| 911 | //$csts = imap_renamemailbox($this->mbox, $this->server . imap_utf7_encode(str_replace(".", $this->serverdelimiter, $oldid)), $newname); |
---|
| 912 | } |
---|
| 913 | else { |
---|
| 914 | $csts = @imap_createmailbox($this->mbox, $newname); |
---|
| 915 | } |
---|
| 916 | if ($csts) { |
---|
| 917 | return $this->StatFolder($folderid . $this->serverdelimiter . $displayname); |
---|
| 918 | } |
---|
| 919 | else |
---|
| 920 | return false; |
---|
| 921 | } |
---|
| 922 | |
---|
| 923 | /** |
---|
| 924 | * Deletes a folder |
---|
| 925 | * |
---|
| 926 | * @param string $id |
---|
| 927 | * @param string $parent is normally false |
---|
| 928 | * |
---|
| 929 | * @access public |
---|
| 930 | * @return boolean status - false if e.g. does not exist |
---|
| 931 | * @throws StatusException could throw specific SYNC_FSSTATUS_* exceptions |
---|
| 932 | * |
---|
| 933 | */ |
---|
| 934 | public function DeleteFolder($id, $parentid){ |
---|
| 935 | // TODO implement |
---|
| 936 | return false; |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | /** |
---|
| 940 | * Returns a list (array) of messages |
---|
| 941 | * |
---|
| 942 | * @param string $folderid id of the parent folder |
---|
| 943 | * @param long $cutoffdate timestamp in the past from which on messages should be returned |
---|
| 944 | * |
---|
| 945 | * @access public |
---|
| 946 | * @return array/false array with messages or false if folder is not available |
---|
| 947 | */ |
---|
| 948 | public function GetMessageList($folderid, $cutoffdate) { |
---|
[7671] | 949 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetMessageList('%s','%s')", $folderid, $cutoffdate)); |
---|
[7589] | 950 | |
---|
| 951 | $folderid = $this->getImapIdFromFolderId($folderid); |
---|
| 952 | |
---|
| 953 | if ($folderid == false) |
---|
| 954 | throw new StatusException("Folderid not found in cache", SYNC_STATUS_FOLDERHIERARCHYCHANGED); |
---|
| 955 | |
---|
| 956 | $messages = array(); |
---|
| 957 | $this->imap_reopenFolder($folderid, true); |
---|
| 958 | |
---|
| 959 | $sequence = "1:*"; |
---|
| 960 | if ($cutoffdate > 0) { |
---|
| 961 | $search = @imap_search($this->mbox, "SINCE ". date("d-M-Y", $cutoffdate)); |
---|
| 962 | if ($search !== false) |
---|
| 963 | $sequence = implode(",", $search); |
---|
| 964 | } |
---|
[7671] | 965 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetMessageList(): searching with sequence '%s'", $sequence)); |
---|
[7589] | 966 | $overviews = @imap_fetch_overview($this->mbox, $sequence); |
---|
| 967 | |
---|
| 968 | if (!$overviews || !is_array($overviews)) { |
---|
[7671] | 969 | ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->GetMessageList('%s','%s'): Failed to retrieve overview: %s",$folderid, $cutoffdate, imap_last_error())); |
---|
[7589] | 970 | return $messages; |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | foreach($overviews as $overview) { |
---|
| 974 | $date = ""; |
---|
| 975 | $vars = get_object_vars($overview); |
---|
| 976 | if (array_key_exists( "date", $vars)) { |
---|
| 977 | // message is out of range for cutoffdate, ignore it |
---|
| 978 | if ($this->cleanupDate($overview->date) < $cutoffdate) continue; |
---|
| 979 | $date = $overview->date; |
---|
| 980 | } |
---|
| 981 | |
---|
| 982 | // cut of deleted messages |
---|
[7671] | 983 | if (array_key_exists("deleted", $vars) && $overview->deleted) |
---|
[7589] | 984 | continue; |
---|
| 985 | |
---|
[7671] | 986 | if (array_key_exists("uid", $vars)) { |
---|
[7589] | 987 | $message = array(); |
---|
| 988 | $message["mod"] = $date; |
---|
| 989 | $message["id"] = $overview->uid; |
---|
| 990 | // 'seen' aka 'read' is the only flag we want to know about |
---|
| 991 | $message["flags"] = 0; |
---|
| 992 | |
---|
| 993 | if(array_key_exists( "seen", $vars) && $overview->seen) |
---|
| 994 | $message["flags"] = 1; |
---|
| 995 | |
---|
| 996 | array_push($messages, $message); |
---|
| 997 | } |
---|
| 998 | } |
---|
| 999 | return $messages; |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | /** |
---|
| 1003 | * Returns the actual SyncXXX object type. |
---|
| 1004 | * |
---|
| 1005 | * @param string $folderid id of the parent folder |
---|
| 1006 | * @param string $id id of the message |
---|
| 1007 | * @param ContentParameters $contentparameters parameters of the requested message (truncation, mimesupport etc) |
---|
| 1008 | * |
---|
| 1009 | * @access public |
---|
| 1010 | * @return object/false false if the message could not be retrieved |
---|
| 1011 | */ |
---|
| 1012 | public function GetMessage($folderid, $id, $contentparameters) { |
---|
| 1013 | $truncsize = Utils::GetTruncSize($contentparameters->GetTruncation()); |
---|
| 1014 | $mimesupport = $contentparameters->GetMimeSupport(); |
---|
[7671] | 1015 | $bodypreference = $contentparameters->GetBodyPreference(); /* fmbiete's contribution r1528, ZP-320 */ |
---|
| 1016 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetMessage('%s','%s')", $folderid, $id)); |
---|
[7589] | 1017 | |
---|
| 1018 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 1019 | |
---|
| 1020 | // Get flags, etc |
---|
| 1021 | $stat = $this->StatMessage($folderid, $id); |
---|
| 1022 | |
---|
| 1023 | if ($stat) { |
---|
| 1024 | $this->imap_reopenFolder($folderImapid); |
---|
| 1025 | $mail = @imap_fetchheader($this->mbox, $id, FT_UID) . @imap_body($this->mbox, $id, FT_PEEK | FT_UID); |
---|
| 1026 | |
---|
| 1027 | $mobj = new Mail_mimeDecode($mail); |
---|
| 1028 | $message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); |
---|
| 1029 | |
---|
[7686] | 1030 | |
---|
| 1031 | |
---|
| 1032 | |
---|
[7671] | 1033 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
[7589] | 1034 | $output = new SyncMail(); |
---|
| 1035 | |
---|
[7671] | 1036 | //Select body type preference |
---|
| 1037 | $bpReturnType = SYNC_BODYPREFERENCE_PLAIN; |
---|
| 1038 | if ($bodypreference !== false) { |
---|
| 1039 | $bpReturnType = Utils::GetBodyPreferenceBestMatch($bodypreference); // changed by mku ZP-330 |
---|
| 1040 | } |
---|
| 1041 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->GetMessage - getBodyPreferenceBestMatch: %d", $bpReturnType)); |
---|
[7589] | 1042 | |
---|
[7671] | 1043 | //Get body data |
---|
| 1044 | $this->getBodyRecursive($message, "plain", $plainBody); |
---|
| 1045 | $this->getBodyRecursive($message, "html", $htmlBody); |
---|
| 1046 | if ($plainBody == "") { |
---|
| 1047 | $plainBody = Utils::ConvertHtmlToText($htmlBody); |
---|
| 1048 | } |
---|
| 1049 | $htmlBody = str_replace("\n","\r\n", str_replace("\r","",$htmlBody)); |
---|
| 1050 | $plainBody = str_replace("\n","\r\n", str_replace("\r","",$plainBody)); |
---|
[7773] | 1051 | $plainBody = html_entity_decode($plainBody); |
---|
[7671] | 1052 | if (Request::GetProtocolVersion() >= 12.0) { |
---|
| 1053 | $output->asbody = new SyncBaseBody(); |
---|
| 1054 | |
---|
| 1055 | switch($bpReturnType) { |
---|
| 1056 | case SYNC_BODYPREFERENCE_PLAIN: |
---|
| 1057 | $output->asbody->data = $plainBody; |
---|
| 1058 | break; |
---|
| 1059 | case SYNC_BODYPREFERENCE_HTML: |
---|
| 1060 | if ($htmlBody == "") { |
---|
| 1061 | $output->asbody->data = $plainBody; |
---|
| 1062 | $bpReturnType = SYNC_BODYPREFERENCE_PLAIN; |
---|
| 1063 | } |
---|
| 1064 | else { |
---|
| 1065 | $output->asbody->data = $htmlBody; |
---|
| 1066 | } |
---|
| 1067 | break; |
---|
| 1068 | case SYNC_BODYPREFERENCE_MIME: |
---|
| 1069 | //We don't need to create a new MIME mail, we already have one!! |
---|
| 1070 | $output->asbody->data = $mail; |
---|
| 1071 | break; |
---|
| 1072 | case SYNC_BODYPREFERENCE_RTF: |
---|
| 1073 | ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->GetMessage RTF Format NOT CHECKED"); |
---|
| 1074 | $output->asbody->data = base64_encode($plainBody); |
---|
| 1075 | break; |
---|
| 1076 | } |
---|
| 1077 | // truncate body, if requested |
---|
| 1078 | if(strlen($output->asbody->data) > $truncsize) { |
---|
| 1079 | $output->asbody->data = Utils::Utf8_truncate($output->asbody->data, $truncsize); |
---|
| 1080 | $output->asbody->truncated = 1; |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | $output->asbody->type = $bpReturnType; |
---|
| 1084 | $output->nativebodytype = $bpReturnType; |
---|
| 1085 | $output->asbody->estimatedDataSize = strlen($output->asbody->data); |
---|
| 1086 | |
---|
| 1087 | $bpo = $contentparameters->BodyPreference($output->asbody->type); |
---|
| 1088 | if (Request::GetProtocolVersion() >= 14.0 && $bpo->GetPreview()) { |
---|
| 1089 | $output->asbody->preview = Utils::Utf8_truncate(Utils::ConvertHtmlToText($plainBody), $bpo->GetPreview()); |
---|
| 1090 | } |
---|
| 1091 | else { |
---|
| 1092 | $output->asbody->truncated = 0; |
---|
| 1093 | } |
---|
| 1094 | } |
---|
| 1095 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
| 1096 | else { // ASV_2.5 |
---|
[7589] | 1097 | $output->bodytruncated = 0; |
---|
[7671] | 1098 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 1099 | if ($bpReturnType == SYNC_BODYPREFERENCE_MIME) { |
---|
| 1100 | if (strlen($mail) > $truncsize) { |
---|
| 1101 | $output->mimedata = Utils::Utf8_truncate($mail, $truncsize); |
---|
| 1102 | $output->mimetruncated = 1; |
---|
| 1103 | } |
---|
| 1104 | else { |
---|
| 1105 | $output->mimetruncated = 0; |
---|
| 1106 | $output->mimedata = $mail; |
---|
| 1107 | } |
---|
| 1108 | $output->mimesize = strlen($output->mimedata); |
---|
| 1109 | } |
---|
| 1110 | else { |
---|
| 1111 | // truncate body, if requested |
---|
| 1112 | if (strlen($plainBody) > $truncsize) { |
---|
| 1113 | $output->body = Utils::Utf8_truncate($plainBody, $truncsize); |
---|
| 1114 | $output->bodytruncated = 1; |
---|
| 1115 | } |
---|
| 1116 | else { |
---|
| 1117 | $output->body = $plainBody; |
---|
| 1118 | $output->bodytruncated = 0; |
---|
| 1119 | } |
---|
| 1120 | $output->bodysize = strlen($output->body); |
---|
| 1121 | } |
---|
| 1122 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
[7589] | 1123 | } |
---|
| 1124 | |
---|
| 1125 | $output->datereceived = isset($message->headers["date"]) ? $this->cleanupDate($message->headers["date"]) : null; |
---|
| 1126 | $output->messageclass = "IPM.Note"; |
---|
| 1127 | $output->subject = isset($message->headers["subject"]) ? $message->headers["subject"] : ""; |
---|
| 1128 | $output->read = $stat["flags"]; |
---|
| 1129 | $output->from = isset($message->headers["from"]) ? $message->headers["from"] : null; |
---|
| 1130 | |
---|
[7671] | 1131 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 1132 | if (isset($message->headers["thread-topic"])) { |
---|
| 1133 | $output->threadtopic = $message->headers["thread-topic"]; |
---|
| 1134 | } |
---|
| 1135 | |
---|
| 1136 | // Language Code Page ID: http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756%28v=vs.85%29.aspx |
---|
| 1137 | $output->internetcpid = INTERNET_CPID_UTF8; |
---|
| 1138 | if (Request::GetProtocolVersion() >= 12.0) { |
---|
| 1139 | $output->contentclass = "urn:content-classes:message"; |
---|
| 1140 | } |
---|
| 1141 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
| 1142 | |
---|
[7589] | 1143 | $Mail_RFC822 = new Mail_RFC822(); |
---|
| 1144 | $toaddr = $ccaddr = $replytoaddr = array(); |
---|
| 1145 | if(isset($message->headers["to"])) |
---|
| 1146 | $toaddr = $Mail_RFC822->parseAddressList($message->headers["to"]); |
---|
| 1147 | if(isset($message->headers["cc"])) |
---|
| 1148 | $ccaddr = $Mail_RFC822->parseAddressList($message->headers["cc"]); |
---|
| 1149 | if(isset($message->headers["reply_to"])) |
---|
| 1150 | $replytoaddr = $Mail_RFC822->parseAddressList($message->headers["reply_to"]); |
---|
| 1151 | |
---|
| 1152 | $output->to = array(); |
---|
| 1153 | $output->cc = array(); |
---|
| 1154 | $output->reply_to = array(); |
---|
| 1155 | foreach(array("to" => $toaddr, "cc" => $ccaddr, "reply_to" => $replytoaddr) as $type => $addrlist) { |
---|
| 1156 | foreach($addrlist as $addr) { |
---|
| 1157 | $address = $addr->mailbox . "@" . $addr->host; |
---|
| 1158 | $name = $addr->personal; |
---|
| 1159 | |
---|
| 1160 | if (!isset($output->displayto) && $name != "") |
---|
| 1161 | $output->displayto = $name; |
---|
| 1162 | |
---|
| 1163 | if($name == "" || $name == $address) |
---|
| 1164 | $fulladdr = w2u($address); |
---|
| 1165 | else { |
---|
| 1166 | if (substr($name, 0, 1) != '"' && substr($name, -1) != '"') { |
---|
| 1167 | $fulladdr = "\"" . w2u($name) ."\" <" . w2u($address) . ">"; |
---|
| 1168 | } |
---|
| 1169 | else { |
---|
| 1170 | $fulladdr = w2u($name) ." <" . w2u($address) . ">"; |
---|
| 1171 | } |
---|
| 1172 | } |
---|
| 1173 | |
---|
| 1174 | array_push($output->$type, $fulladdr); |
---|
| 1175 | } |
---|
| 1176 | } |
---|
| 1177 | |
---|
| 1178 | // convert mime-importance to AS-importance |
---|
| 1179 | if (isset($message->headers["x-priority"])) { |
---|
| 1180 | $mimeImportance = preg_replace("/\D+/", "", $message->headers["x-priority"]); |
---|
[7671] | 1181 | //MAIL 1 - most important, 3 - normal, 5 - lowest |
---|
| 1182 | //AS 0 - low, 1 - normal, 2 - important |
---|
[7589] | 1183 | if ($mimeImportance > 3) |
---|
| 1184 | $output->importance = 0; |
---|
| 1185 | if ($mimeImportance == 3) |
---|
| 1186 | $output->importance = 1; |
---|
| 1187 | if ($mimeImportance < 3) |
---|
| 1188 | $output->importance = 2; |
---|
[7671] | 1189 | } else { /* fmbiete's contribution r1528, ZP-320 */ |
---|
| 1190 | $output->importance = 1; |
---|
[7589] | 1191 | } |
---|
| 1192 | |
---|
[7671] | 1193 | // Attachments are not needed for MIME messages |
---|
| 1194 | if($bpReturnType != SYNC_BODYPREFERENCE_MIME && isset($message->parts)) { |
---|
[7589] | 1195 | $mparts = $message->parts; |
---|
| 1196 | for ($i=0; $i<count($mparts); $i++) { |
---|
| 1197 | $part = $mparts[$i]; |
---|
| 1198 | //recursively add parts |
---|
| 1199 | if($part->ctype_primary == "multipart" && ($part->ctype_secondary == "mixed" || $part->ctype_secondary == "alternative" || $part->ctype_secondary == "related")) { |
---|
| 1200 | foreach($part->parts as $spart) |
---|
| 1201 | $mparts[] = $spart; |
---|
| 1202 | continue; |
---|
| 1203 | } |
---|
| 1204 | //add part as attachment if it's disposition indicates so or if it is not a text part |
---|
| 1205 | if ((isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) || |
---|
| 1206 | (isset($part->ctype_primary) && $part->ctype_primary != "text")) { |
---|
| 1207 | |
---|
| 1208 | if(isset($part->d_parameters['filename'])) |
---|
| 1209 | $attname = $part->d_parameters['filename']; |
---|
| 1210 | else if(isset($part->ctype_parameters['name'])) |
---|
| 1211 | $attname = $part->ctype_parameters['name']; |
---|
| 1212 | else if(isset($part->headers['content-description'])) |
---|
| 1213 | $attname = $part->headers['content-description']; |
---|
| 1214 | else $attname = "unknown attachment"; |
---|
| 1215 | |
---|
[7671] | 1216 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 1217 | if (Request::GetProtocolVersion() >= 12.0) { |
---|
| 1218 | if (!isset($output->asattachments) || !is_array($output->asattachments)) |
---|
| 1219 | $output->asattachments = array(); |
---|
| 1220 | |
---|
| 1221 | $attachment = new SyncBaseAttachment(); |
---|
| 1222 | |
---|
| 1223 | $attachment->estimatedDataSize = isset($part->d_parameters['size']) ? $part->d_parameters['size'] : isset($part->body) ? strlen($part->body) : 0; |
---|
| 1224 | |
---|
| 1225 | $attachment->displayname = $attname; |
---|
| 1226 | $attachment->filereference = $folderid . ":" . $id . ":" . $i; |
---|
| 1227 | $attachment->method = 1; //Normal attachment |
---|
| 1228 | $attachment->contentid = isset($part->headers['content-id']) ? str_replace("<", "", str_replace(">", "", $part->headers['content-id'])) : ""; |
---|
| 1229 | if (isset($part->disposition) && $part->disposition == "inline") { |
---|
| 1230 | $attachment->isinline = 1; |
---|
| 1231 | } |
---|
| 1232 | else { |
---|
| 1233 | $attachment->isinline = 0; |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | array_push($output->asattachments, $attachment); |
---|
| 1237 | } |
---|
| 1238 | else { //ASV_2.5 |
---|
| 1239 | if (!isset($output->attachments) || !is_array($output->attachments)) |
---|
| 1240 | $output->attachments = array(); |
---|
| 1241 | |
---|
| 1242 | $attachment = new SyncAttachment(); |
---|
| 1243 | |
---|
| 1244 | $attachment->attsize = isset($part->d_parameters['size']) ? $part->d_parameters['size'] : isset($part->body) ? strlen($part->body) : 0; |
---|
| 1245 | |
---|
| 1246 | $attachment->displayname = $attname; |
---|
| 1247 | $attachment->attname = $folderid . ":" . $id . ":" . $i; |
---|
| 1248 | $attachment->attmethod = 1; |
---|
| 1249 | $attachment->attoid = isset($part->headers['content-id']) ? str_replace("<", "", str_replace(">", "", $part->headers['content-id'])) : ""; |
---|
| 1250 | |
---|
| 1251 | array_push($output->attachments, $attachment); |
---|
| 1252 | } |
---|
| 1253 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
[7589] | 1254 | } |
---|
| 1255 | } |
---|
| 1256 | } |
---|
| 1257 | // unset mimedecoder & mail |
---|
| 1258 | unset($mobj); |
---|
| 1259 | unset($mail); |
---|
| 1260 | return $output; |
---|
| 1261 | } |
---|
| 1262 | |
---|
| 1263 | return false; |
---|
| 1264 | } |
---|
| 1265 | |
---|
| 1266 | /** |
---|
| 1267 | * Returns message stats, analogous to the folder stats from StatFolder(). |
---|
| 1268 | * |
---|
| 1269 | * @param string $folderid id of the folder |
---|
| 1270 | * @param string $id id of the message |
---|
| 1271 | * |
---|
| 1272 | * @access public |
---|
| 1273 | * @return array/boolean |
---|
| 1274 | */ |
---|
| 1275 | public function StatMessage($folderid, $id) { |
---|
[7671] | 1276 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->StatMessage('%s','%s')", $folderid, $id)); |
---|
[7589] | 1277 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 1278 | |
---|
| 1279 | $this->imap_reopenFolder($folderImapid); |
---|
| 1280 | $overview = @imap_fetch_overview( $this->mbox , $id , FT_UID); |
---|
| 1281 | |
---|
| 1282 | if (!$overview) { |
---|
[7671] | 1283 | ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->StatMessage('%s','%s'): Failed to retrieve overview: %s", $folderid, $id, imap_last_error())); |
---|
[7589] | 1284 | return false; |
---|
| 1285 | } |
---|
| 1286 | |
---|
| 1287 | // check if variables for this overview object are available |
---|
| 1288 | $vars = get_object_vars($overview[0]); |
---|
| 1289 | |
---|
| 1290 | // without uid it's not a valid message |
---|
| 1291 | if (! array_key_exists( "uid", $vars)) return false; |
---|
| 1292 | |
---|
| 1293 | $entry = array(); |
---|
| 1294 | $entry["mod"] = (array_key_exists( "date", $vars)) ? $overview[0]->date : ""; |
---|
| 1295 | $entry["id"] = $overview[0]->uid; |
---|
| 1296 | // 'seen' aka 'read' is the only flag we want to know about |
---|
| 1297 | $entry["flags"] = 0; |
---|
| 1298 | |
---|
| 1299 | if(array_key_exists( "seen", $vars) && $overview[0]->seen) |
---|
| 1300 | $entry["flags"] = 1; |
---|
| 1301 | |
---|
| 1302 | return $entry; |
---|
| 1303 | } |
---|
| 1304 | |
---|
| 1305 | /** |
---|
| 1306 | * Called when a message has been changed on the mobile. |
---|
[7671] | 1307 | * Added support for FollowUp flag |
---|
[7589] | 1308 | * |
---|
| 1309 | * @param string $folderid id of the folder |
---|
| 1310 | * @param string $id id of the message |
---|
| 1311 | * @param SyncXXX $message the SyncObject containing a message |
---|
| 1312 | * |
---|
| 1313 | * @access public |
---|
| 1314 | * @return array same return value as StatMessage() |
---|
| 1315 | * @throws StatusException could throw specific SYNC_STATUS_* exceptions |
---|
| 1316 | */ |
---|
| 1317 | public function ChangeMessage($folderid, $id, $message) { |
---|
[7671] | 1318 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->ChangeMessage('%s','%s','%s')", $folderid, $id, get_class($message))); |
---|
| 1319 | |
---|
| 1320 | /* BEGIN fmbiete's contribution r1529, ZP-321 */ |
---|
| 1321 | if (isset($message->flag)) { |
---|
| 1322 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->ChangeMessage('Setting flag')")); |
---|
| 1323 | |
---|
| 1324 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 1325 | |
---|
| 1326 | $this->imap_reopenFolder($folderImapid); |
---|
| 1327 | |
---|
| 1328 | if (isset($message->flag->flagstatus) && $message->flag->flagstatus == 2) { |
---|
| 1329 | ZLog::Write(LOGLEVEL_DEBUG, "Set On FollowUp -> IMAP Flagged"); |
---|
| 1330 | $status = @imap_setflag_full($this->mbox, $id, "\\Flagged",ST_UID); |
---|
| 1331 | } |
---|
| 1332 | else { |
---|
| 1333 | ZLog::Write(LOGLEVEL_DEBUG, "Clearing Flagged"); |
---|
| 1334 | $status = @imap_clearflag_full ( $this->mbox, $id, "\\Flagged", ST_UID); |
---|
| 1335 | } |
---|
| 1336 | |
---|
| 1337 | if ($status) { |
---|
| 1338 | ZLog::Write(LOGLEVEL_DEBUG, "Flagged changed"); |
---|
| 1339 | } |
---|
| 1340 | else { |
---|
| 1341 | ZLog::Write(LOGLEVEL_DEBUG, "Flagged failed"); |
---|
| 1342 | } |
---|
| 1343 | } |
---|
| 1344 | |
---|
| 1345 | return $this->StatMessage($folderid, $id); |
---|
| 1346 | /* END fmbiete's contribution r1529, ZP-321 */ |
---|
[7589] | 1347 | } |
---|
| 1348 | |
---|
| 1349 | /** |
---|
| 1350 | * Changes the 'read' flag of a message on disk |
---|
| 1351 | * |
---|
| 1352 | * @param string $folderid id of the folder |
---|
| 1353 | * @param string $id id of the message |
---|
| 1354 | * @param int $flags read flag of the message |
---|
| 1355 | * |
---|
| 1356 | * @access public |
---|
| 1357 | * @return boolean status of the operation |
---|
| 1358 | * @throws StatusException could throw specific SYNC_STATUS_* exceptions |
---|
| 1359 | */ |
---|
| 1360 | public function SetReadFlag($folderid, $id, $flags) { |
---|
[7671] | 1361 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SetReadFlag('%s','%s','%s')", $folderid, $id, $flags)); |
---|
[7589] | 1362 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 1363 | |
---|
| 1364 | $this->imap_reopenFolder($folderImapid); |
---|
| 1365 | |
---|
| 1366 | if ($flags == 0) { |
---|
| 1367 | // set as "Unseen" (unread) |
---|
| 1368 | $status = @imap_clearflag_full ( $this->mbox, $id, "\\Seen", ST_UID); |
---|
| 1369 | } else { |
---|
| 1370 | // set as "Seen" (read) |
---|
| 1371 | $status = @imap_setflag_full($this->mbox, $id, "\\Seen",ST_UID); |
---|
| 1372 | } |
---|
| 1373 | |
---|
| 1374 | return $status; |
---|
| 1375 | } |
---|
| 1376 | |
---|
| 1377 | /** |
---|
| 1378 | * Called when the user has requested to delete (really delete) a message |
---|
| 1379 | * |
---|
| 1380 | * @param string $folderid id of the folder |
---|
| 1381 | * @param string $id id of the message |
---|
| 1382 | * |
---|
| 1383 | * @access public |
---|
| 1384 | * @return boolean status of the operation |
---|
| 1385 | * @throws StatusException could throw specific SYNC_STATUS_* exceptions |
---|
| 1386 | */ |
---|
| 1387 | public function DeleteMessage($folderid, $id) { |
---|
[7671] | 1388 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->DeleteMessage('%s','%s')", $folderid, $id)); |
---|
[7589] | 1389 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 1390 | |
---|
| 1391 | $this->imap_reopenFolder($folderImapid); |
---|
| 1392 | $s1 = @imap_delete ($this->mbox, $id, FT_UID); |
---|
| 1393 | $s11 = @imap_setflag_full($this->mbox, $id, "\\Deleted", FT_UID); |
---|
| 1394 | $s2 = @imap_expunge($this->mbox); |
---|
| 1395 | |
---|
[7671] | 1396 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->DeleteMessage('%s','%s'): result: s-delete: '%s' s-expunge: '%s' setflag: '%s'", $folderid, $id, $s1, $s2, $s11)); |
---|
[7589] | 1397 | |
---|
| 1398 | return ($s1 && $s2 && $s11); |
---|
| 1399 | } |
---|
| 1400 | |
---|
| 1401 | /** |
---|
| 1402 | * Called when the user moves an item on the PDA from one folder to another |
---|
| 1403 | * |
---|
| 1404 | * @param string $folderid id of the source folder |
---|
| 1405 | * @param string $id id of the message |
---|
| 1406 | * @param string $newfolderid id of the destination folder |
---|
| 1407 | * |
---|
| 1408 | * @access public |
---|
| 1409 | * @return boolean status of the operation |
---|
| 1410 | * @throws StatusException could throw specific SYNC_MOVEITEMSSTATUS_* exceptions |
---|
| 1411 | */ |
---|
| 1412 | public function MoveMessage($folderid, $id, $newfolderid) { |
---|
[7671] | 1413 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->MoveMessage('%s','%s','%s')", $folderid, $id, $newfolderid)); |
---|
[7589] | 1414 | $folderImapid = $this->getImapIdFromFolderId($folderid); |
---|
| 1415 | $newfolderImapid = $this->getImapIdFromFolderId($newfolderid); |
---|
| 1416 | |
---|
| 1417 | |
---|
| 1418 | $this->imap_reopenFolder($folderImapid); |
---|
| 1419 | |
---|
| 1420 | // TODO this should throw a StatusExceptions on errors like SYNC_MOVEITEMSSTATUS_SAMESOURCEANDDEST,SYNC_MOVEITEMSSTATUS_INVALIDSOURCEID,SYNC_MOVEITEMSSTATUS_CANNOTMOVE |
---|
| 1421 | |
---|
| 1422 | // read message flags |
---|
| 1423 | $overview = @imap_fetch_overview ( $this->mbox , $id, FT_UID); |
---|
| 1424 | |
---|
| 1425 | if (!$overview) |
---|
[7671] | 1426 | throw new StatusException(sprintf("BackendIMAP->MoveMessage('%s','%s','%s'): Error, unable to retrieve overview of source message: %s", $folderid, $id, $newfolderid, imap_last_error()), SYNC_MOVEITEMSSTATUS_INVALIDSOURCEID); |
---|
[7589] | 1427 | else { |
---|
| 1428 | // get next UID for destination folder |
---|
| 1429 | // when moving a message we have to announce through ActiveSync the new messageID in the |
---|
| 1430 | // destination folder. This is a "guessing" mechanism as IMAP does not inform that value. |
---|
| 1431 | // when lots of simultaneous operations happen in the destination folder this could fail. |
---|
| 1432 | // in the worst case the moved message is displayed twice on the mobile. |
---|
| 1433 | $destStatus = imap_status($this->mbox, $this->server . $newfolderImapid, SA_ALL); |
---|
| 1434 | if (!$destStatus) |
---|
[7671] | 1435 | throw new StatusException(sprintf("BackendIMAP->MoveMessage('%s','%s','%s'): Error, unable to open destination folder: %s", $folderid, $id, $newfolderid, imap_last_error()), SYNC_MOVEITEMSSTATUS_INVALIDDESTID); |
---|
[7589] | 1436 | |
---|
| 1437 | $newid = $destStatus->uidnext; |
---|
| 1438 | |
---|
| 1439 | // move message |
---|
| 1440 | $s1 = imap_mail_move($this->mbox, $id, $newfolderImapid, CP_UID); |
---|
| 1441 | if (! $s1) |
---|
[7671] | 1442 | throw new StatusException(sprintf("BackendIMAP->MoveMessage('%s','%s','%s'): Error, copy to destination folder failed: %s", $folderid, $id, $newfolderid, imap_last_error()), SYNC_MOVEITEMSSTATUS_CANNOTMOVE); |
---|
[7589] | 1443 | |
---|
| 1444 | |
---|
| 1445 | // delete message in from-folder |
---|
| 1446 | $s2 = imap_expunge($this->mbox); |
---|
| 1447 | |
---|
| 1448 | // open new folder |
---|
| 1449 | $stat = $this->imap_reopenFolder($newfolderImapid); |
---|
| 1450 | if (! $s1) |
---|
[7671] | 1451 | throw new StatusException(sprintf("BackendIMAP->MoveMessage('%s','%s','%s'): Error, openeing the destination folder: %s", $folderid, $id, $newfolderid, imap_last_error()), SYNC_MOVEITEMSSTATUS_CANNOTMOVE); |
---|
[7589] | 1452 | |
---|
| 1453 | |
---|
| 1454 | // remove all flags |
---|
| 1455 | $s3 = @imap_clearflag_full ($this->mbox, $newid, "\\Seen \\Answered \\Flagged \\Deleted \\Draft", FT_UID); |
---|
| 1456 | $newflags = ""; |
---|
| 1457 | if ($overview[0]->seen) $newflags .= "\\Seen"; |
---|
| 1458 | if ($overview[0]->flagged) $newflags .= " \\Flagged"; |
---|
| 1459 | if ($overview[0]->answered) $newflags .= " \\Answered"; |
---|
| 1460 | $s4 = @imap_setflag_full ($this->mbox, $newid, $newflags, FT_UID); |
---|
| 1461 | |
---|
[7671] | 1462 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->MoveMessage('%s','%s','%s'): result s-move: '%s' s-expunge: '%s' unset-Flags: '%s' set-Flags: '%s'", $folderid, $id, $newfolderid, Utils::PrintAsString($s1), Utils::PrintAsString($s2), Utils::PrintAsString($s3), Utils::PrintAsString($s4))); |
---|
[7589] | 1463 | |
---|
| 1464 | // return the new id "as string"" |
---|
| 1465 | return $newid . ""; |
---|
| 1466 | } |
---|
| 1467 | } |
---|
| 1468 | |
---|
| 1469 | |
---|
| 1470 | /**---------------------------------------------------------------------------------------------------------- |
---|
| 1471 | * protected IMAP methods |
---|
| 1472 | */ |
---|
| 1473 | |
---|
| 1474 | /** |
---|
| 1475 | * Unmasks a hex folderid and returns the imap folder id |
---|
| 1476 | * |
---|
| 1477 | * @param string $folderid hex folderid generated by convertImapId() |
---|
| 1478 | * |
---|
| 1479 | * @access protected |
---|
| 1480 | * @return string imap folder id |
---|
| 1481 | */ |
---|
| 1482 | protected function getImapIdFromFolderId($folderid) { |
---|
| 1483 | $this->InitializePermanentStorage(); |
---|
| 1484 | |
---|
| 1485 | if (isset($this->permanentStorage->fmFidFimap)) { |
---|
| 1486 | if (isset($this->permanentStorage->fmFidFimap[$folderid])) { |
---|
| 1487 | $imapId = $this->permanentStorage->fmFidFimap[$folderid]; |
---|
[7671] | 1488 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getImapIdFromFolderId('%s') = %s", $folderid, $imapId)); |
---|
[7589] | 1489 | return $imapId; |
---|
| 1490 | } |
---|
| 1491 | else { |
---|
[7671] | 1492 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getImapIdFromFolderId('%s') = %s", $folderid, 'not found')); |
---|
[7589] | 1493 | return false; |
---|
| 1494 | } |
---|
| 1495 | } |
---|
[7671] | 1496 | ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getImapIdFromFolderId('%s') = %s", $folderid, 'not initialized!')); |
---|
[7589] | 1497 | return false; |
---|
| 1498 | } |
---|
| 1499 | |
---|
| 1500 | /** |
---|
| 1501 | * Retrieves a hex folderid previousily masked imap |
---|
| 1502 | * |
---|
| 1503 | * @param string $imapid Imap folder id |
---|
| 1504 | * |
---|
| 1505 | * @access protected |
---|
| 1506 | * @return string hex folder id |
---|
| 1507 | */ |
---|
| 1508 | protected function getFolderIdFromImapId($imapid) { |
---|
| 1509 | $this->InitializePermanentStorage(); |
---|
| 1510 | |
---|
| 1511 | if (isset($this->permanentStorage->fmFimapFid)) { |
---|
| 1512 | if (isset($this->permanentStorage->fmFimapFid[$imapid])) { |
---|
| 1513 | $folderid = $this->permanentStorage->fmFimapFid[$imapid]; |
---|
[7671] | 1514 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getFolderIdFromImapId('%s') = %s", $imapid, $folderid)); |
---|
[7589] | 1515 | return $folderid; |
---|
| 1516 | } |
---|
| 1517 | else { |
---|
[7671] | 1518 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getFolderIdFromImapId('%s') = %s", $imapid, 'not found')); |
---|
[7589] | 1519 | return false; |
---|
| 1520 | } |
---|
| 1521 | } |
---|
[7671] | 1522 | ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getFolderIdFromImapId('%s') = %s", $imapid, 'not initialized!')); |
---|
[7589] | 1523 | return false; |
---|
| 1524 | } |
---|
| 1525 | |
---|
| 1526 | /** |
---|
| 1527 | * Masks a imap folder id into a generated hex folderid |
---|
| 1528 | * The method getFolderIdFromImapId() is consulted so that an |
---|
| 1529 | * imapid always returns the same hex folder id |
---|
| 1530 | * |
---|
| 1531 | * @param string $imapid Imap folder id |
---|
| 1532 | * |
---|
| 1533 | * @access protected |
---|
| 1534 | * @return string hex folder id |
---|
| 1535 | */ |
---|
| 1536 | protected function convertImapId($imapid) { |
---|
| 1537 | $this->InitializePermanentStorage(); |
---|
| 1538 | |
---|
| 1539 | // check if this imap id was converted before |
---|
| 1540 | $folderid = $this->getFolderIdFromImapId($imapid); |
---|
| 1541 | |
---|
| 1542 | // nothing found, so generate a new id and put it in the cache |
---|
| 1543 | if (!$folderid) { |
---|
| 1544 | // generate folderid and add it to the mapping |
---|
| 1545 | $folderid = sprintf('%04x%04x', mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )); |
---|
| 1546 | |
---|
| 1547 | // folderId to folderImap mapping |
---|
| 1548 | if (!isset($this->permanentStorage->fmFidFimap)) |
---|
| 1549 | $this->permanentStorage->fmFidFimap = array(); |
---|
| 1550 | |
---|
| 1551 | $a = $this->permanentStorage->fmFidFimap; |
---|
| 1552 | $a[$folderid] = $imapid; |
---|
| 1553 | $this->permanentStorage->fmFidFimap = $a; |
---|
| 1554 | |
---|
| 1555 | // folderImap to folderid mapping |
---|
| 1556 | if (!isset($this->permanentStorage->fmFimapFid)) |
---|
| 1557 | $this->permanentStorage->fmFimapFid = array(); |
---|
| 1558 | |
---|
| 1559 | $b = $this->permanentStorage->fmFimapFid; |
---|
| 1560 | $b[$imapid] = $folderid; |
---|
| 1561 | $this->permanentStorage->fmFimapFid = $b; |
---|
| 1562 | } |
---|
| 1563 | |
---|
[7671] | 1564 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->convertImapId('%s') = %s", $imapid, $folderid)); |
---|
[7589] | 1565 | |
---|
| 1566 | return $folderid; |
---|
| 1567 | } |
---|
| 1568 | |
---|
| 1569 | |
---|
| 1570 | /** |
---|
| 1571 | * Parses the message and return only the plaintext body |
---|
| 1572 | * |
---|
| 1573 | * @param string $message html message |
---|
| 1574 | * |
---|
| 1575 | * @access protected |
---|
| 1576 | * @return string plaintext message |
---|
| 1577 | */ |
---|
| 1578 | protected function getBody($message) { |
---|
| 1579 | $body = ""; |
---|
| 1580 | $htmlbody = ""; |
---|
| 1581 | |
---|
| 1582 | $this->getBodyRecursive($message, "plain", $body); |
---|
| 1583 | |
---|
| 1584 | if($body === "") { |
---|
| 1585 | $this->getBodyRecursive($message, "html", $body); |
---|
| 1586 | } |
---|
| 1587 | |
---|
| 1588 | return $body; |
---|
| 1589 | } |
---|
| 1590 | |
---|
| 1591 | /** |
---|
| 1592 | * Get all parts in the message with specified type and concatenate them together, unless the |
---|
| 1593 | * Content-Disposition is 'attachment', in which case the text is apparently an attachment |
---|
| 1594 | * |
---|
| 1595 | * @param string $message mimedecode message(part) |
---|
| 1596 | * @param string $message message subtype |
---|
| 1597 | * @param string &$body body reference |
---|
| 1598 | * |
---|
| 1599 | * @access protected |
---|
| 1600 | * @return |
---|
| 1601 | */ |
---|
| 1602 | protected function getBodyRecursive($message, $subtype, &$body) { |
---|
| 1603 | if(!isset($message->ctype_primary)) return; |
---|
[7686] | 1604 | if(strcasecmp($message->ctype_primary,"text")==0 && strcasecmp($message->ctype_secondary,$subtype)==0 && isset($message->body)){ |
---|
[7698] | 1605 | |
---|
| 1606 | if(strtolower($message->ctype_parameters['charset']) == 'iso-8859-1') |
---|
| 1607 | { |
---|
| 1608 | $body .= mb_detect_encoding($message->body) == 'UTF-8' ? $message->body : mb_convert_encoding($message->body , 'ISO-8859-1'); |
---|
| 1609 | } |
---|
| 1610 | else |
---|
| 1611 | { |
---|
| 1612 | $body .= $message->body; |
---|
| 1613 | } |
---|
[7686] | 1614 | } |
---|
[7589] | 1615 | |
---|
| 1616 | if(strcasecmp($message->ctype_primary,"multipart")==0 && isset($message->parts) && is_array($message->parts)) { |
---|
| 1617 | foreach($message->parts as $part) { |
---|
| 1618 | if(!isset($part->disposition) || strcasecmp($part->disposition,"attachment")) { |
---|
| 1619 | $this->getBodyRecursive($part, $subtype, $body); |
---|
| 1620 | } |
---|
| 1621 | } |
---|
| 1622 | } |
---|
| 1623 | } |
---|
| 1624 | |
---|
| 1625 | /** |
---|
| 1626 | * Returns the serverdelimiter for folder parsing |
---|
| 1627 | * |
---|
| 1628 | * @access protected |
---|
| 1629 | * @return string delimiter |
---|
| 1630 | */ |
---|
| 1631 | protected function getServerDelimiter() { |
---|
[7671] | 1632 | $list = @imap_getmailboxes($this->mbox, $this->server, "INBOX*"); |
---|
[7589] | 1633 | if (is_array($list)) { |
---|
| 1634 | $val = $list[0]; |
---|
| 1635 | |
---|
| 1636 | return $val->delimiter; |
---|
| 1637 | } |
---|
| 1638 | return "."; // default "." |
---|
| 1639 | } |
---|
| 1640 | |
---|
| 1641 | /** |
---|
| 1642 | * Helper to re-initialize the folder to speed things up |
---|
| 1643 | * Remember what folder is currently open and only change if necessary |
---|
| 1644 | * |
---|
| 1645 | * @param string $folderid id of the folder |
---|
| 1646 | * @param boolean $force re-open the folder even if currently opened |
---|
| 1647 | * |
---|
| 1648 | * @access protected |
---|
| 1649 | * @return |
---|
| 1650 | */ |
---|
| 1651 | protected function imap_reopenFolder($folderid, $force = false) { |
---|
| 1652 | // to see changes, the folder has to be reopened! |
---|
[7671] | 1653 | if ($this->mboxFolder != $folderid || $force) { |
---|
| 1654 | $s = @imap_reopen($this->mbox, $this->server . $folderid); |
---|
| 1655 | // TODO throw status exception |
---|
| 1656 | if (!$s) { |
---|
| 1657 | ZLog::Write(LOGLEVEL_WARN, "BackendIMAP->imap_reopenFolder('%s'): failed to change folder: ",$folderid, implode(", ", imap_errors())); |
---|
[7589] | 1658 | return false; |
---|
[7671] | 1659 | } |
---|
[7589] | 1660 | $this->mboxFolder = $folderid; |
---|
| 1661 | } |
---|
| 1662 | } |
---|
| 1663 | |
---|
| 1664 | |
---|
| 1665 | /** |
---|
| 1666 | * Build a multipart RFC822, embedding body and one file (for attachments) |
---|
| 1667 | * |
---|
| 1668 | * @param string $filenm name of the file to be attached |
---|
| 1669 | * @param long $filesize size of the file to be attached |
---|
| 1670 | * @param string $file_cont content of the file |
---|
| 1671 | * @param string $body current body |
---|
| 1672 | * @param string $body_ct content-type |
---|
| 1673 | * @param string $body_cte content-transfer-encoding |
---|
| 1674 | * @param string $boundary optional existing boundary |
---|
| 1675 | * |
---|
| 1676 | * @access protected |
---|
| 1677 | * @return array with [0] => $mail_header and [1] => $mail_body |
---|
| 1678 | */ |
---|
| 1679 | protected function mail_attach($filenm,$filesize,$file_cont,$body, $body_ct, $body_cte, $boundary = false) { |
---|
| 1680 | if (!$boundary) $boundary = strtoupper(md5(uniqid(time()))); |
---|
| 1681 | |
---|
| 1682 | //remove the ending boundary because we will add it at the end |
---|
| 1683 | $body = str_replace("--$boundary--", "", $body); |
---|
| 1684 | |
---|
| 1685 | $mail_header = "Content-Type: multipart/mixed; boundary=$boundary\n"; |
---|
| 1686 | |
---|
| 1687 | // build main body with the sumitted type & encoding from the pda |
---|
| 1688 | $mail_body = $this->enc_multipart($boundary, $body, $body_ct, $body_cte); |
---|
| 1689 | $mail_body .= $this->enc_attach_file($boundary, $filenm, $filesize, $file_cont); |
---|
| 1690 | |
---|
| 1691 | $mail_body .= "--$boundary--\n\n"; |
---|
| 1692 | return array($mail_header, $mail_body); |
---|
| 1693 | } |
---|
| 1694 | |
---|
| 1695 | /** |
---|
| 1696 | * Helper for mail_attach() |
---|
| 1697 | * |
---|
| 1698 | * @param string $boundary boundary |
---|
| 1699 | * @param string $body current body |
---|
| 1700 | * @param string $body_ct content-type |
---|
| 1701 | * @param string $body_cte content-transfer-encoding |
---|
| 1702 | * |
---|
| 1703 | * @access protected |
---|
| 1704 | * @return string message body |
---|
| 1705 | */ |
---|
| 1706 | protected function enc_multipart($boundary, $body, $body_ct, $body_cte) { |
---|
| 1707 | $mail_body = "This is a multi-part message in MIME format\n\n"; |
---|
| 1708 | $mail_body .= "--$boundary\n"; |
---|
| 1709 | $mail_body .= "Content-Type: $body_ct\n"; |
---|
| 1710 | $mail_body .= "Content-Transfer-Encoding: $body_cte\n\n"; |
---|
| 1711 | $mail_body .= "$body\n\n"; |
---|
| 1712 | |
---|
| 1713 | return $mail_body; |
---|
| 1714 | } |
---|
| 1715 | |
---|
| 1716 | /** |
---|
| 1717 | * Helper for mail_attach() |
---|
| 1718 | * |
---|
| 1719 | * @param string $boundary boundary |
---|
| 1720 | * @param string $filenm name of the file to be attached |
---|
| 1721 | * @param long $filesize size of the file to be attached |
---|
| 1722 | * @param string $file_cont content of the file |
---|
| 1723 | * @param string $content_type optional content-type |
---|
| 1724 | * |
---|
| 1725 | * @access protected |
---|
| 1726 | * @return string message body |
---|
| 1727 | */ |
---|
| 1728 | protected function enc_attach_file($boundary, $filenm, $filesize, $file_cont, $content_type = "") { |
---|
| 1729 | if (!$content_type) $content_type = "text/plain"; |
---|
| 1730 | $mail_body = "--$boundary\n"; |
---|
| 1731 | $mail_body .= "Content-Type: $content_type; name=\"$filenm\"\n"; |
---|
| 1732 | $mail_body .= "Content-Transfer-Encoding: base64\n"; |
---|
| 1733 | $mail_body .= "Content-Disposition: attachment; filename=\"$filenm\"\n"; |
---|
| 1734 | $mail_body .= "Content-Description: $filenm\n\n"; |
---|
| 1735 | //contrib - chunk base64 encoded attachments |
---|
| 1736 | $mail_body .= chunk_split(base64_encode($file_cont)) . "\n\n"; |
---|
| 1737 | |
---|
| 1738 | return $mail_body; |
---|
| 1739 | } |
---|
| 1740 | |
---|
| 1741 | /** |
---|
| 1742 | * Adds a message with seen flag to a specified folder (used for saving sent items) |
---|
| 1743 | * |
---|
| 1744 | * @param string $folderid id of the folder |
---|
| 1745 | * @param string $header header of the message |
---|
| 1746 | * @param long $body body of the message |
---|
| 1747 | * |
---|
| 1748 | * @access protected |
---|
| 1749 | * @return boolean status |
---|
| 1750 | */ |
---|
| 1751 | protected function addSentMessage($folderid, $header, $body) { |
---|
| 1752 | $header_body = str_replace("\n", "\r\n", str_replace("\r", "", $header . "\n\n" . $body)); |
---|
| 1753 | |
---|
| 1754 | return @imap_append($this->mbox, $this->server . $folderid, $header_body, "\\Seen"); |
---|
| 1755 | } |
---|
| 1756 | |
---|
| 1757 | /** |
---|
| 1758 | * Parses an mimedecode address array back to a simple "," separated string |
---|
| 1759 | * |
---|
| 1760 | * @param array $ad addresses array |
---|
| 1761 | * |
---|
| 1762 | * @access protected |
---|
| 1763 | * @return string mail address(es) string |
---|
| 1764 | */ |
---|
| 1765 | protected function parseAddr($ad) { |
---|
| 1766 | $addr_string = ""; |
---|
| 1767 | if (isset($ad) && is_array($ad)) { |
---|
| 1768 | foreach($ad as $addr) { |
---|
| 1769 | if ($addr_string) $addr_string .= ","; |
---|
[7671] | 1770 | $addr_string .= $addr->mailbox . "@" . $addr->host; |
---|
[7589] | 1771 | } |
---|
| 1772 | } |
---|
| 1773 | return $addr_string; |
---|
| 1774 | } |
---|
| 1775 | |
---|
| 1776 | /** |
---|
| 1777 | * Recursive way to get mod and parent - repeat until only one part is left |
---|
| 1778 | * or the folder is identified as an IMAP folder |
---|
| 1779 | * |
---|
| 1780 | * @param string $fhir folder hierarchy string |
---|
| 1781 | * @param string &$displayname reference of the displayname |
---|
| 1782 | * @param long &$parent reference of the parent folder |
---|
| 1783 | * |
---|
| 1784 | * @access protected |
---|
| 1785 | * @return |
---|
| 1786 | */ |
---|
| 1787 | protected function getModAndParentNames($fhir, &$displayname, &$parent) { |
---|
| 1788 | // if mod is already set add the previous part to it as it might be a folder which has |
---|
| 1789 | // delimiter in its name |
---|
| 1790 | $displayname = (isset($displayname) && strlen($displayname) > 0) ? $displayname = array_pop($fhir).$this->serverdelimiter.$displayname : array_pop($fhir); |
---|
| 1791 | $parent = implode($this->serverdelimiter, $fhir); |
---|
| 1792 | |
---|
| 1793 | if (count($fhir) == 1 || $this->checkIfIMAPFolder($parent)) { |
---|
| 1794 | return; |
---|
| 1795 | } |
---|
| 1796 | //recursion magic |
---|
| 1797 | $this->getModAndParentNames($fhir, $displayname, $parent); |
---|
| 1798 | } |
---|
| 1799 | |
---|
| 1800 | /** |
---|
| 1801 | * Checks if a specified name is a folder in the IMAP store |
---|
| 1802 | * |
---|
| 1803 | * @param string $foldername a foldername |
---|
| 1804 | * |
---|
| 1805 | * @access protected |
---|
| 1806 | * @return boolean |
---|
| 1807 | */ |
---|
| 1808 | protected function checkIfIMAPFolder($folderName) { |
---|
| 1809 | $parent = imap_list($this->mbox, $this->server, $folderName); |
---|
| 1810 | if ($parent === false) return false; |
---|
| 1811 | return true; |
---|
| 1812 | } |
---|
| 1813 | |
---|
| 1814 | /** |
---|
| 1815 | * Removes parenthesis (comments) from the date string because |
---|
| 1816 | * strtotime returns false if received date has them |
---|
| 1817 | * |
---|
| 1818 | * @param string $receiveddate a date as a string |
---|
| 1819 | * |
---|
| 1820 | * @access protected |
---|
| 1821 | * @return string |
---|
| 1822 | */ |
---|
| 1823 | protected function cleanupDate($receiveddate) { |
---|
| 1824 | $receiveddate = strtotime(preg_replace("/\(.*\)/", "", $receiveddate)); |
---|
| 1825 | if ($receiveddate == false || $receiveddate == -1) { |
---|
| 1826 | debugLog("Received date is false. Message might be broken."); |
---|
| 1827 | return null; |
---|
| 1828 | } |
---|
| 1829 | |
---|
| 1830 | return $receiveddate; |
---|
| 1831 | } |
---|
| 1832 | |
---|
[7671] | 1833 | /* BEGIN fmbiete's contribution r1528, ZP-320 */ |
---|
| 1834 | /** |
---|
| 1835 | * Indicates which AS version is supported by the backend. |
---|
| 1836 | * |
---|
| 1837 | * @access public |
---|
| 1838 | * @return string AS version constant |
---|
| 1839 | */ |
---|
| 1840 | public function GetSupportedASVersion() { |
---|
| 1841 | return ZPush::ASV_14; |
---|
| 1842 | } |
---|
| 1843 | /* END fmbiete's contribution r1528, ZP-320 */ |
---|
| 1844 | }; |
---|
[7589] | 1845 | |
---|
[7671] | 1846 | ?> |
---|