2
/***********************************************
5
* Descr : This backend is based on
6
* 'BackendDiff' which handles the
7
* intricacies of generating
8
* differentials from static
9
* snapshots. This means that the
10
* implementation here needs no
11
* state information, and can simply
12
* return the current state of the
13
* messages. The diffbackend will
14
* then compare the current state
15
* to the known last state of the PDA
16
* and generate change increments
19
* Created : 01.10.2007
21
* Copyright 2007 - 2012 Zarafa Deutschland GmbH
23
* This program is free software: you can redistribute it and/or modify
24
* it under the terms of the GNU Affero General Public License, version 3,
25
* as published by the Free Software Foundation with the following additional
26
* term according to sec. 7:
28
* According to sec. 7 of the GNU Affero General Public License, version 3,
29
* the terms of the AGPL are supplemented with the following terms:
31
* "Zarafa" is a registered trademark of Zarafa B.V.
32
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
33
* The licensing of the Program under the AGPL does not imply a trademark license.
34
* Therefore any rights, title and interest in our trademarks remain entirely with us.
36
* However, if you propagate an unmodified version of the Program you are
37
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
38
* Furthermore you may use our trademarks where it is necessary to indicate
39
* the intended purpose of a product or service provided you use it in accordance
40
* with honest practices in industrial or commercial matters.
41
* If you want to propagate modified versions of the Program under the name "Z-Push",
42
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
43
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
45
* This program is distributed in the hope that it will be useful,
46
* but WITHOUT ANY WARRANTY; without even the implied warranty of
47
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48
* GNU Affero General Public License for more details.
50
* You should have received a copy of the GNU Affero General Public License
51
* along with this program. If not, see <http://www.gnu.org/licenses/>.
53
* Consult LICENSE file for details
54
************************************************/
56
include_once('lib/default/diffbackend/diffbackend.php');
57
include_once('include/mimeDecode.php');
58
require_once('include/z_RFC822.php');
60
class BackendMaildir extends BackendDiff {
61
/**----------------------------------------------------------------------------------------------------------
62
* default backend methods
66
* Authenticates the user - NOT EFFECTIVELY IMPLEMENTED
67
* Normally some kind of password check would be done here.
68
* Alternatively, the password could be ignored and an Apache
69
* authentication via mod_auth_* could be done
71
* @param string $username
72
* @param string $domain
73
* @param string $password
78
public function Logon($username, $domain, $password) {
88
public function Logoff() {
94
* Not implemented here
96
* @param SyncSendMail $sm SyncSendMail object
100
* @throws StatusException
102
public function SendMail($sm) {
107
* Returns the waste basket
112
public function GetWasteBasket() {
117
* Returns the content of the named attachment as stream. The passed attachment identifier is
118
* the exact string that is returned in the 'AttName' property of an SyncAttachment.
119
* Any information necessary to find the attachment must be encoded in that 'attname' property.
120
* Data is written directly (with print $data;)
122
* @param string $attname
125
* @return SyncItemOperationsAttachment
126
* @throws StatusException
128
public function GetAttachmentData($attname) {
129
list($id, $part) = explode(":", $attname);
131
$fn = $this->findMessage($id);
133
throw new StatusException(sprintf("BackendMaildir->GetAttachmentData('%s'): Error, requested message/attachment can not be found", $attname), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
136
$rfc822 = file_get_contents($this->getPath() . "/$fn");
138
$message = Mail_mimeDecode::decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\n", 'charset' => 'utf-8'));
140
include_once('include/stringstreamwrapper.php');
141
$attachment = new SyncItemOperationsAttachment();
142
$attachment->data = StringStreamWrapper::Open($message->parts[$part]->body);
143
if (isset($message->parts[$part]->ctype_primary) && isset($message->parts[$part]->ctype_secondary))
144
$attachment->contenttype = $message->parts[$part]->ctype_primary .'/'.$message->parts[$part]->ctype_secondary;
149
/**----------------------------------------------------------------------------------------------------------
150
* implemented DiffBackend methods
155
* Returns a list (array) of folders.
156
* In simple implementations like this one, probably just one folder is returned.
161
public function GetFolderList() {
165
$inbox["id"] = "root";
166
$inbox["parent"] = "0";
167
$inbox["mod"] = "Inbox";
173
$sub["parent"] = "root";
182
* Returns an actual SyncFolder object
184
* @param string $id id of the folder
187
* @return object SyncFolder with information
189
public function GetFolder($id) {
191
$inbox = new SyncFolder();
193
$inbox->serverid = $id;
194
$inbox->parentid = "0"; // Root
195
$inbox->displayname = "Inbox";
196
$inbox->type = SYNC_FOLDER_TYPE_INBOX;
199
} else if($id == "sub") {
200
$inbox = new SyncFolder();
201
$inbox->serverid = $id;
202
$inbox->parentid = "root";
203
$inbox->displayname = "Sub";
204
$inbox->type = SYNC_FOLDER_TYPE_OTHER;
214
* Returns folder stats. An associative array with properties is expected.
216
* @param string $id id of the folder
221
public function StatFolder($id) {
222
$folder = $this->GetFolder($id);
226
$stat["parent"] = $folder->parentid;
227
$stat["mod"] = $folder->displayname;
234
* Creates or modifies a folder
237
* @param string $folderid id of the parent folder
238
* @param string $oldid if empty -> new folder created, else folder is to be renamed
239
* @param string $displayname new folder name (to be created, or to be renamed to)
240
* @param int $type folder type
243
* @return boolean status
244
* @throws StatusException could throw specific SYNC_FSSTATUS_* exceptions
247
public function ChangeFolder($folderid, $oldid, $displayname, $type){
255
* @param string $parent is normally false
258
* @return boolean status - false if e.g. does not exist
259
* @throws StatusException could throw specific SYNC_FSSTATUS_* exceptions
262
public function DeleteFolder($id, $parentid){
267
* Returns a list (array) of messages
269
* @param string $folderid id of the parent folder
270
* @param long $cutoffdate timestamp in the past from which on messages should be returned
273
* @return array/false array with messages or false if folder is not available
275
public function GetMessageList($folderid, $cutoffdate) {
276
$this->moveNewToCur();
278
if($folderid != "root")
281
// return stats of all messages in a dir. We can do this faster than
282
// just calling statMessage() on each message; We still need fstat()
283
// information though, so listing 10000 messages is going to be
284
// rather slow (depending on filesystem, etc)
286
// we also have to filter by the specified cutoffdate so only the
287
// last X days are retrieved. Normally, this would mean that we'd
288
// have to open each message, get the Received: header, and check
289
// whether that is in the filter range. Because this is much too slow, we
290
// are depending on the creation date of the message instead, which should
291
// normally be just about the same, unless you just did some kind of import.
294
$dirname = $this->getPath();
296
$dir = opendir($dirname);
301
while($entry = readdir($dir)) {
307
$stat = stat("$dirname/$entry");
309
if($stat["mtime"] < $cutoffdate) {
310
// message is out of range for curoffdate, ignore it
314
$message["mod"] = $stat["mtime"];
318
// Flags according to http://cr.yp.to/proto/maildir.html (pretty authoritative - qmail author's website)
319
if(!preg_match("/([^:]+):2,([PRSTDF]*)/",$entry,$matches))
321
$message["id"] = $matches[1];
322
$message["flags"] = 0;
324
if(strpos($matches[2],"S") !== false) {
325
$message["flags"] |= 1; // 'seen' aka 'read' is the only flag we want to know about
328
array_push($messages, $message);
335
* Returns the actual SyncXXX object type.
337
* @param string $folderid id of the parent folder
338
* @param string $id id of the message
339
* @param ContentParameters $contentparameters parameters of the requested message (truncation, mimesupport etc)
342
* @return object/false false if the message could not be retrieved
344
public function GetMessage($folderid, $id, $truncsize, $mimesupport = 0) {
345
if($folderid != 'root')
348
$fn = $this->findMessage($id);
351
$stat = $this->StatMessage($folderid, $id);
354
$rfc822 = file_get_contents($this->getPath() . "/" . $fn);
356
$message = Mail_mimeDecode::decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\n", 'charset' => 'utf-8'));
358
$output = new SyncMail();
360
$output->body = str_replace("\n", "\r\n", $this->getBody($message));
361
$output->bodysize = strlen($output->body);
362
$output->bodytruncated = 0; // We don't implement truncation in this backend
363
$output->datereceived = $this->parseReceivedDate($message->headers["received"][0]);
364
$output->messageclass = "IPM.Note";
365
$output->subject = $message->headers["subject"];
366
$output->read = $stat["flags"];
367
$output->from = $message->headers["from"];
369
$Mail_RFC822 = new Mail_RFC822();
370
$toaddr = $ccaddr = $replytoaddr = array();
371
if(isset($message->headers["to"]))
372
$toaddr = $Mail_RFC822->parseAddressList($message->headers["to"]);
373
if(isset($message->headers["cc"]))
374
$ccaddr = $Mail_RFC822->parseAddressList($message->headers["cc"]);
375
if(isset($message->headers["reply_to"]))
376
$replytoaddr = $Mail_RFC822->parseAddressList($message->headers["reply_to"]);
378
$output->to = array();
379
$output->cc = array();
380
$output->reply_to = array();
381
foreach(array("to" => $toaddr, "cc" => $ccaddr, "reply_to" => $replytoaddr) as $type => $addrlist) {
382
foreach($addrlist as $addr) {
383
$address = $addr->mailbox . "@" . $addr->host;
384
$name = $addr->personal;
386
if (!isset($output->displayto) && $name != "")
387
$output->displayto = $name;
389
if($name == "" || $name == $address)
390
$fulladdr = w2u($address);
392
if (substr($name, 0, 1) != '"' && substr($name, -1) != '"') {
393
$fulladdr = "\"" . w2u($name) ."\" <" . w2u($address) . ">";
396
$fulladdr = w2u($name) ." <" . w2u($address) . ">";
400
array_push($output->$type, $fulladdr);
404
// convert mime-importance to AS-importance
405
if (isset($message->headers["x-priority"])) {
406
$mimeImportance = preg_replace("/\D+/", "", $message->headers["x-priority"]);
407
if ($mimeImportance > 3)
408
$output->importance = 0;
409
if ($mimeImportance == 3)
410
$output->importance = 1;
411
if ($mimeImportance < 3)
412
$output->importance = 2;
415
// Attachments are only searched in the top-level part
417
if(isset($message->parts)) {
418
foreach($message->parts as $part) {
419
if($part->ctype_primary == "application") {
420
$attachment = new SyncAttachment();
421
$attachment->attsize = strlen($part->body);
423
if(isset($part->d_parameters['filename']))
424
$attname = $part->d_parameters['filename'];
425
else if(isset($part->ctype_parameters['name']))
426
$attname = $part->ctype_parameters['name'];
427
else if(isset($part->headers['content-description']))
428
$attname = $part->headers['content-description'];
429
else $attname = "unknown attachment";
431
$attachment->displayname = $attname;
432
$attachment->attname = $id . ":" . $n;
433
$attachment->attmethod = 1;
434
$attachment->attoid = isset($part->headers['content-id']) ? $part->headers['content-id'] : "";
436
array_push($output->attachments, $attachment);
446
* Returns message stats, analogous to the folder stats from StatFolder().
448
* @param string $folderid id of the folder
449
* @param string $id id of the message
454
public function StatMessage($folderid, $id) {
455
$dirname = $this->getPath();
456
$fn = $this->findMessage($id);
460
$stat = stat("$dirname/$fn");
467
$entry["flags"] |= 1;
468
$entry["mod"] = $stat["mtime"];
474
* Called when a message has been changed on the mobile.
475
* This functionality is not available for emails.
477
* @param string $folderid id of the folder
478
* @param string $id id of the message
479
* @param SyncXXX $message the SyncObject containing a message
482
* @return array same return value as StatMessage()
483
* @throws StatusException could throw specific SYNC_STATUS_* exceptions
485
public function ChangeMessage($folderid, $id, $message) {
490
* Changes the 'read' flag of a message on disk
492
* @param string $folderid id of the folder
493
* @param string $id id of the message
494
* @param int $flags read flag of the message
497
* @return boolean status of the operation
498
* @throws StatusException could throw specific SYNC_STATUS_* exceptions
500
public function SetReadFlag($folderid, $id, $flags) {
501
if($folderid != 'root')
504
$fn = $this->findMessage($id);
507
return true; // message may have been deleted
509
if(!preg_match("/([^:]+):2,([PRSTDF]*)/",$fn,$matches))
512
// remove 'seen' (S) flag
514
$newflags = str_replace("S","",$matches[2]);
516
// make sure we don't double add the 'S' flag
517
$newflags = str_replace("S","",$matches[2]) . "S";
520
$newfn = $matches[1] . ":2," . $newflags;
521
// rename if required
523
rename($this->getPath() ."/$fn", $this->getPath() . "/$newfn");
529
* Called when the user has requested to delete (really delete) a message
531
* @param string $folderid id of the folder
532
* @param string $id id of the message
535
* @return boolean status of the operation
536
* @throws StatusException could throw specific SYNC_STATUS_* exceptions
538
public function DeleteMessage($folderid, $id) {
539
if($folderid != 'root')
542
$fn = $this->findMessage($id);
545
return true; // success because message has been deleted already
547
if(!unlink($this->getPath() . "/$fn")) {
548
return true; // success - message may have been deleted in the mean time (since findMessage)
555
* Called when the user moves an item on the PDA from one folder to another
558
* @param string $folderid id of the source folder
559
* @param string $id id of the message
560
* @param string $newfolderid id of the destination folder
563
* @return boolean status of the operation
564
* @throws StatusException could throw specific SYNC_MOVEITEMSSTATUS_* exceptions
566
public function MoveMessage($folderid, $id, $newfolderid) {
571
/**----------------------------------------------------------------------------------------------------------
572
* private maildir-specific internals
576
* Searches for the message
578
* @param string $id id of the message
583
private function findMessage($id) {
584
// We could use 'this->folderid' for path info but we currently
585
// only support a single INBOX. We also have to use a glob '*'
586
// because we don't know the flags of the message we're looking for.
588
$dirname = $this->getPath();
589
$dir = opendir($dirname);
591
while($entry = readdir($dir)) {
592
if(strpos($entry,$id) === 0)
595
return false; // not found
599
* Parses the message and return only the plaintext body
601
* @param string $message html message
604
* @return string plaintext message
606
private function getBody($message) {
610
$this->getBodyRecursive($message, "plain", $body);
612
if(!isset($body) || $body === "") {
613
$this->getBodyRecursive($message, "html", $body);
614
// remove css-style tags
615
$body = preg_replace("/<style.*?<\/style>/is", "", $body);
616
// remove all other html
617
$body = strip_tags($body);
624
* Get all parts in the message with specified type and concatenate them together, unless the
625
* Content-Disposition is 'attachment', in which case the text is apparently an attachment
627
* @param string $message mimedecode message(part)
628
* @param string $message message subtype
629
* @param string &$body body reference
634
private function getBodyRecursive($message, $subtype, &$body) {
635
if(!isset($message->ctype_primary)) return;
636
if(strcasecmp($message->ctype_primary,"text")==0 && strcasecmp($message->ctype_secondary,$subtype)==0 && isset($message->body))
637
$body .= $message->body;
639
if(strcasecmp($message->ctype_primary,"multipart")==0 && isset($message->parts) && is_array($message->parts)) {
640
foreach($message->parts as $part) {
641
if(!isset($part->disposition) || strcasecmp($part->disposition,"attachment")) {
642
$this->getBodyRecursive($part, $subtype, $body);
649
* Parses the received date
651
* @param string $received received date string
656
private function parseReceivedDate($received) {
657
$pos = strpos($received, ";");
661
$datestr = substr($received, $pos+1);
662
$datestr = ltrim($datestr);
664
return strtotime($datestr);
668
* Moves everything in Maildir/new/* to Maildir/cur/
673
private function moveNewToCur() {
674
$newdirname = MAILDIR_BASE . "/" . $this->store . "/" . MAILDIR_SUBDIR . "/new";
676
$newdir = opendir($newdirname);
678
while($newentry = readdir($newdir)) {
679
if($newentry{0} == ".")
682
// link/unlink == move. This is the way to move the message according to cr.yp.to
683
link($newdirname . "/" . $newentry, $this->getPath() . "/" . $newentry . ":2,");
684
unlink($newdirname . "/" . $newentry);
689
* The path we're working on
694
private function getPath() {
695
return MAILDIR_BASE . "/" . $this->store . "/" . MAILDIR_SUBDIR . "/cur";
b'\\ No newline at end of file'