source: trunk/zpush/backend/zarafa/zarafa.php @ 7589

Revision 7589, 76.2 KB checked in by douglas, 11 years ago (diff)

Ticket #3209 - Integrar módulo de sincronização Z-push ao Expresso

Line 
1<?php
2/***********************************************
3* File      :   zarafa.php
4* Project   :   Z-Push
5* Descr     :   This is backend for the
6*               Zarafa Collaboration Platform (ZCP).
7*               It is an implementation of IBackend
8*               and also implements the ISearchProvider
9*               to search in the Zarafa system.
10*
11* Created   :   01.10.2011
12*
13* Copyright 2007 - 2012 Zarafa Deutschland GmbH
14*
15* This program is free software: you can redistribute it and/or modify
16* it under the terms of the GNU Affero General Public License, version 3,
17* as published by the Free Software Foundation with the following additional
18* term according to sec. 7:
19*
20* According to sec. 7 of the GNU Affero General Public License, version 3,
21* the terms of the AGPL are supplemented with the following terms:
22*
23* "Zarafa" is a registered trademark of Zarafa B.V.
24* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
25* The licensing of the Program under the AGPL does not imply a trademark license.
26* Therefore any rights, title and interest in our trademarks remain entirely with us.
27*
28* However, if you propagate an unmodified version of the Program you are
29* allowed to use the term "Z-Push" to indicate that you distribute the Program.
30* Furthermore you may use our trademarks where it is necessary to indicate
31* the intended purpose of a product or service provided you use it in accordance
32* with honest practices in industrial or commercial matters.
33* If you want to propagate modified versions of the Program under the name "Z-Push",
34* you may only do so if you have a written permission by Zarafa Deutschland GmbH
35* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
36*
37* This program is distributed in the hope that it will be useful,
38* but WITHOUT ANY WARRANTY; without even the implied warranty of
39* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40* GNU Affero General Public License for more details.
41*
42* You should have received a copy of the GNU Affero General Public License
43* along with this program.  If not, see <http://www.gnu.org/licenses/>.
44*
45* Consult LICENSE file for details
46*************************************************/
47
48// include PHP-MAPI classes
49include_once('backend/zarafa/mapi/mapi.util.php');
50include_once('backend/zarafa/mapi/mapidefs.php');
51include_once('backend/zarafa/mapi/mapitags.php');
52include_once('backend/zarafa/mapi/mapicode.php');
53include_once('backend/zarafa/mapi/mapiguid.php');
54include_once('backend/zarafa/mapi/class.baseexception.php');
55include_once('backend/zarafa/mapi/class.mapiexception.php');
56include_once('backend/zarafa/mapi/class.baserecurrence.php');
57include_once('backend/zarafa/mapi/class.taskrecurrence.php');
58include_once('backend/zarafa/mapi/class.recurrence.php');
59include_once('backend/zarafa/mapi/class.meetingrequest.php');
60include_once('backend/zarafa/mapi/class.freebusypublish.php');
61
62// processing of RFC822 messages
63include_once('include/mimeDecode.php');
64require_once('include/z_RFC822.php');
65
66// components of Zarafa backend
67include_once('backend/zarafa/mapiutils.php');
68include_once('backend/zarafa/mapimapping.php');
69include_once('backend/zarafa/mapiprovider.php');
70include_once('backend/zarafa/mapiphpwrapper.php');
71include_once('backend/zarafa/mapistreamwrapper.php');
72include_once('backend/zarafa/importer.php');
73include_once('backend/zarafa/exporter.php');
74
75
76class BackendZarafa implements IBackend, ISearchProvider {
77    private $mainUser;
78    private $session;
79    private $defaultstore;
80    private $store;
81    private $storeName;
82    private $storeCache;
83    private $importedFolders;
84    private $notifications;
85    private $changesSink;
86    private $changesSinkFolders;
87    private $changesSinkStores;
88    private $wastebasket;
89
90    /**
91     * Constructor of the Zarafa Backend
92     *
93     * @access public
94     */
95    public function BackendZarafa() {
96        $this->session = false;
97        $this->store = false;
98        $this->storeName = false;
99        $this->storeCache = array();
100        $this->importedFolders = array();
101        $this->notifications = false;
102        $this->changesSink = false;
103        $this->changesSinkFolders = array();
104        $this->changesSinkStores = array();
105        $this->wastebasket = false;
106        $this->session = false;
107
108        ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa using PHP-MAPI version: %s", phpversion("mapi")));
109    }
110
111    /**
112     * Indicates which StateMachine should be used
113     *
114     * @access public
115     * @return boolean      ZarafaBackend uses the default FileStateMachine
116     */
117    public function GetStateMachine() {
118        return false;
119    }
120
121    /**
122     * Returns the ZarafaBackend as it implements the ISearchProvider interface
123     * This could be overwritten by the global configuration
124     *
125     * @access public
126     * @return object       Implementation of ISearchProvider
127     */
128    public function GetSearchProvider() {
129        return $this;
130    }
131
132    /**
133     * Indicates which AS version is supported by the backend.
134     *
135     * @access public
136     * @return string       AS version constant
137     */
138    public function GetSupportedASVersion() {
139        return ZPush::ASV_14;
140    }
141
142    /**
143     * Authenticates the user with the configured Zarafa server
144     *
145     * @param string        $username
146     * @param string        $domain
147     * @param string        $password
148     *
149     * @access public
150     * @return boolean
151     * @throws AuthenticationRequiredException
152     */
153    public function Logon($user, $domain, $pass) {
154        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->Logon(): Trying to authenticate user '%s'..", $user));
155        $this->mainUser = strtolower($user);
156
157        try {
158            // check if notifications are available in php-mapi
159            if(function_exists('mapi_feature') && mapi_feature('LOGONFLAGS')) {
160                $this->session = @mapi_logon_zarafa($user, $pass, MAPI_SERVER, null, null, 0);
161                $this->notifications = true;
162            }
163            // old fashioned session
164            else {
165                $this->session = @mapi_logon_zarafa($user, $pass, MAPI_SERVER);
166                $this->notifications = false;
167            }
168
169            if (mapi_last_hresult()) {
170                ZLog::Write(LOGLEVEL_ERROR, sprintf("ZarafaBackend->Logon(): login failed with error code: 0x%X", mapi_last_hresult()));
171                if (mapi_last_hresult() == MAPI_E_NETWORK_ERROR)
172                    throw new HTTPReturnCodeException("Error connecting to ZCP (login)", 503, null, LOGLEVEL_INFO);
173            }
174        }
175        catch (MAPIException $ex) {
176            throw new AuthenticationRequiredException($ex->getDisplayMessage());
177        }
178
179        if(!$this->session) {
180            ZLog::Write(LOGLEVEL_WARN, sprintf("ZarafaBackend->Logon(): logon failed for user '%s'", $user));
181            $this->defaultstore = false;
182            return false;
183        }
184
185        // Get/open default store
186        $this->defaultstore = $this->openMessageStore($this->mainUser);
187
188        if (mapi_last_hresult() == MAPI_E_FAILONEPROVIDER)
189            throw new HTTPReturnCodeException("Error connecting to ZCP (open store)", 503, null, LOGLEVEL_INFO);
190
191        if($this->defaultstore === false)
192            throw new AuthenticationRequiredException(sprintf("ZarafaBackend->Logon(): User '%s' has no default store", $user));
193
194        $this->store = $this->defaultstore;
195        $this->storeName = $this->mainUser;
196
197        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->Logon(): User '%s' is authenticated",$user));
198
199        // check if this is a Zarafa 7 store with unicode support
200        MAPIUtils::IsUnicodeStore($this->store);
201        return true;
202    }
203
204    /**
205     * Setup the backend to work on a specific store or checks ACLs there.
206     * If only the $store is submitted, all Import/Export/Fetch/Etc operations should be
207     * performed on this store (switch operations store).
208     * If the ACL check is enabled, this operation should just indicate the ACL status on
209     * the submitted store, without changing the store for operations.
210     * For the ACL status, the currently logged on user MUST have access rights on
211     *  - the entire store - admin access if no folderid is sent, or
212     *  - on a specific folderid in the store (secretary/full access rights)
213     *
214     * The ACLcheck MUST fail if a folder of the authenticated user is checked!
215     *
216     * @param string        $store              target store, could contain a "domain\user" value
217     * @param boolean       $checkACLonly       if set to true, Setup() should just check ACLs
218     * @param string        $folderid           if set, only ACLs on this folderid are relevant
219     *
220     * @access public
221     * @return boolean
222     */
223    public function Setup($store, $checkACLonly = false, $folderid = false) {
224        list($user, $domain) = Utils::SplitDomainUser($store);
225
226        if (!isset($this->mainUser))
227            return false;
228
229        if ($user === false)
230            $user = $this->mainUser;
231
232        // This is a special case. A user will get it's entire folder structure by the foldersync by default.
233        // The ACL check is executed when an additional folder is going to be sent to the mobile.
234        // Configured that way the user could receive the same folderid twice, with two different names.
235        if ($this->mainUser == $user && $checkACLonly && $folderid) {
236            ZLog::Write(LOGLEVEL_DEBUG, "ZarafaBackend->Setup(): Checking ACLs for folder of the users defaultstore. Fail is forced to avoid folder duplications on mobile.");
237            return false;
238        }
239
240        // get the users store
241        $userstore = $this->openMessageStore($user);
242
243        // only proceed if a store was found, else return false
244        if ($userstore) {
245            // only check permissions
246            if ($checkACLonly == true) {
247                // check for admin rights
248                if (!$folderid) {
249                    if ($user != $this->mainUser) {
250                        $zarafauserinfo = @mapi_zarafa_getuser_by_name($this->defaultstore, $this->mainUser);
251                        $admin = (isset($zarafauserinfo['admin']) && $zarafauserinfo['admin'])?true:false;
252                    }
253                    // the user has always full access to his own store
254                    else
255                        $admin = true;
256
257                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->Setup(): Checking for admin ACLs on store '%s': '%s'", $user, Utils::PrintAsString($admin)));
258                    return $admin;
259                }
260                // check 'secretary' permissions on this folder
261                else {
262                    $rights = $this->hasSecretaryACLs($userstore, $folderid);
263                    ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->Setup(): Checking for secretary ACLs on '%s' of store '%s': '%s'", $folderid, $user, Utils::PrintAsString($rights)));
264                    return $rights;
265                }
266            }
267
268            // switch operations store
269            // this should also be done if called with user = mainuser or user = false
270            // which means to switch back to the default store
271            else {
272                // switch active store
273                $this->store = $userstore;
274                $this->storeName = $user;
275                return true;
276            }
277        }
278        return false;
279    }
280
281    /**
282     * Logs off
283     * Free/Busy information is updated for modified calendars
284     * This is done after the synchronization process is completed
285     *
286     * @access public
287     * @return boolean
288     */
289    public function Logoff() {
290        // update if the calendar which received incoming changes
291        foreach($this->importedFolders as $folderid => $store) {
292            // open the root of the store
293            $storeprops = mapi_getprops($store, array(PR_USER_ENTRYID));
294            $root = mapi_msgstore_openentry($store);
295            if (!$root)
296                continue;
297
298            // get the entryid of the main calendar of the store and the calendar to be published
299            $rootprops = mapi_getprops($root, array(PR_IPM_APPOINTMENT_ENTRYID));
300            $entryid = mapi_msgstore_entryidfromsourcekey($store, hex2bin($folderid));
301
302            // only publish free busy for the main calendar
303            if(isset($rootprops[PR_IPM_APPOINTMENT_ENTRYID]) && $rootprops[PR_IPM_APPOINTMENT_ENTRYID] == $entryid) {
304                ZLog::Write(LOGLEVEL_INFO, sprintf("ZarafaBackend->Logoff(): Updating freebusy information on folder id '%s'", $folderid));
305                $calendar = mapi_msgstore_openentry($store, $entryid);
306
307                $pub = new FreeBusyPublish($this->session, $store, $calendar, $storeprops[PR_USER_ENTRYID]);
308                $pub->publishFB(time() - (7 * 24 * 60 * 60), 6 * 30 * 24 * 60 * 60); // publish from one week ago, 6 months ahead
309            }
310        }
311
312        return true;
313    }
314
315    /**
316     * Returns an array of SyncFolder types with the entire folder hierarchy
317     * on the server (the array itself is flat, but refers to parents via the 'parent' property
318     *
319     * provides AS 1.0 compatibility
320     *
321     * @access public
322     * @return array SYNC_FOLDER
323     */
324    public function GetHierarchy() {
325        $folders = array();
326        $importer = false;
327        $mapiprovider = new MAPIProvider($this->session, $this->store);
328
329        $rootfolder = mapi_msgstore_openentry($this->store);
330        $rootfolderprops = mapi_getprops($rootfolder, array(PR_SOURCE_KEY));
331        $rootfoldersourcekey = bin2hex($rootfolderprops[PR_SOURCE_KEY]);
332
333        $hierarchy =  mapi_folder_gethierarchytable($rootfolder, CONVENIENT_DEPTH);
334        $rows = mapi_table_queryallrows($hierarchy, array(PR_ENTRYID));
335
336        foreach ($rows as $row) {
337            $mapifolder = mapi_msgstore_openentry($this->store, $row[PR_ENTRYID]);
338            $folder = $mapiprovider->GetFolder($mapifolder);
339
340            if (isset($folder->parentid) && $folder->parentid != $rootfoldersourcekey)
341                $folders[] = $folder;
342        }
343
344        return $folders;
345    }
346
347    /**
348     * Returns the importer to process changes from the mobile
349     * If no $folderid is given, hierarchy importer is expected
350     *
351     * @param string        $folderid (opt)
352     *
353     * @access public
354     * @return object(ImportChanges)
355     */
356    public function GetImporter($folderid = false) {
357        ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->GetImporter() folderid: '%s'", Utils::PrintAsString($folderid)));
358        if($folderid !== false) {
359            // check if the user of the current store has permissions to import to this folderid
360            if ($this->storeName != $this->mainUser && !$this->hasSecretaryACLs($this->store, $folderid)) {
361                ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->GetImporter(): missing permissions on folderid: '%s'.", Utils::PrintAsString($folderid)));
362                return false;
363            }
364            $this->importedFolders[$folderid] = $this->store;
365            return new ImportChangesICS($this->session, $this->store, hex2bin($folderid));
366        }
367        else
368            return new ImportChangesICS($this->session, $this->store);
369    }
370
371    /**
372     * Returns the exporter to send changes to the mobile
373     * If no $folderid is given, hierarchy exporter is expected
374     *
375     * @param string        $folderid (opt)
376     *
377     * @access public
378     * @return object(ExportChanges)
379     * @throws StatusException
380     */
381    public function GetExporter($folderid = false) {
382        if($folderid !== false) {
383            // check if the user of the current store has permissions to export from this folderid
384            if ($this->storeName != $this->mainUser && !$this->hasSecretaryACLs($this->store, $folderid)) {
385                ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->GetExporter(): missing permissions on folderid: '%s'.", Utils::PrintAsString($folderid)));
386                return false;
387            }
388            return new ExportChangesICS($this->session, $this->store, hex2bin($folderid));
389        }
390        else
391            return new ExportChangesICS($this->session, $this->store);
392    }
393
394    /**
395     * Sends an e-mail
396     * This messages needs to be saved into the 'sent items' folder
397     *
398     * @param SyncSendMail  $sm     SyncSendMail object
399     *
400     * @access public
401     * @return boolean
402     * @throws StatusException
403     */
404    public function SendMail($sm) {
405        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->SendMail(): RFC822: %d bytes  forward-id: '%s' reply-id: '%s' parent-id: '%s' SaveInSent: '%s' ReplaceMIME: '%s'",
406                                            strlen($sm->mime), Utils::PrintAsString($sm->forwardflag), Utils::PrintAsString($sm->replyflag),
407                                            Utils::PrintAsString((isset($sm->source->folderid) ? $sm->source->folderid : false)),
408                                            Utils::PrintAsString(($sm->saveinsent)), Utils::PrintAsString(isset($sm->replacemime)) ));
409
410        // by splitting the message in several lines we can easily grep later
411        foreach(preg_split("/((\r)?\n)/", $sm->mime) as $rfc822line)
412            ZLog::Write(LOGLEVEL_WBXML, "RFC822: ". $rfc822line);
413
414        $mimeParams = array('decode_headers' => true,
415                            'decode_bodies' => true,
416                            'include_bodies' => true,
417                            'charset' => 'utf-8');
418
419        $mimeObject = new Mail_mimeDecode($sm->mime);
420        $message = $mimeObject->decode($mimeParams);
421
422        $sendMailProps = MAPIMapping::GetSendMailProperties();
423        $sendMailProps = getPropIdsFromStrings($this->store, $sendMailProps);
424
425        // Open the outbox and create the message there
426        $storeprops = mapi_getprops($this->store, array($sendMailProps["outboxentryid"], $sendMailProps["ipmsentmailentryid"]));
427        if(isset($storeprops[$sendMailProps["outboxentryid"]]))
428            $outbox = mapi_msgstore_openentry($this->store, $storeprops[$sendMailProps["outboxentryid"]]);
429
430        if(!$outbox)
431            throw new StatusException(sprintf("ZarafaBackend->SendMail(): No Outbox found or unable to create message: 0x%X", mapi_last_hresult()), SYNC_COMMONSTATUS_SERVERERROR);
432
433        $mapimessage = mapi_folder_createmessage($outbox);
434
435        //message properties to be set
436        $mapiprops = array();
437        // only save the outgoing in sent items folder if the mobile requests it
438        $mapiprops[$sendMailProps["sentmailentryid"]] = $storeprops[$sendMailProps["ipmsentmailentryid"]];
439
440        // Check if imtomapi function is available and use it to send the mime message.
441        // It is available since ZCP 7.0.6
442        // @see http://jira.zarafa.com/browse/ZCP-9508
443        if(function_exists('mapi_feature') && mapi_feature('INETMAPI_IMTOMAPI')) {
444            ZLog::Write(LOGLEVEL_DEBUG, "Use the mapi_inetmapi_imtomapi function");
445            $ab = mapi_openaddressbook($this->session);
446            mapi_inetmapi_imtomapi($this->session, $this->store, $ab, $mapimessage, $sm->mime, array());
447
448            // Set the appSeqNr so that tracking tab can be updated for meeting request updates
449            // @see http://jira.zarafa.com/browse/ZP-68
450            $meetingRequestProps = MAPIMapping::GetMeetingRequestProperties();
451            $meetingRequestProps = getPropIdsFromStrings($this->store, $meetingRequestProps);
452            $props = mapi_getprops($mapimessage, array(PR_MESSAGE_CLASS, $meetingRequestProps["goidtag"]));
453            if (stripos($props[PR_MESSAGE_CLASS], "IPM.Schedule.Meeting.Resp.") === 0) {
454                // search for calendar items using goid
455                $mr = new Meetingrequest($this->store, $mapimessage);
456                $appointments = $mr->findCalendarItems($props[$meetingRequestProps["goidtag"]]);
457                if (is_array($appointments) && !empty($appointments)) {
458                    $app = mapi_msgstore_openentry($this->store, $appointments[0]);
459                    $appprops = mapi_getprops($app, array($meetingRequestProps["appSeqNr"]));
460                    if (isset($appprops[$meetingRequestProps["appSeqNr"]]) && $appprops[$meetingRequestProps["appSeqNr"]]) {
461                        $mapiprops[$meetingRequestProps["appSeqNr"]] = $appprops[$meetingRequestProps["appSeqNr"]];
462                        ZLog::Write(LOGLEVEL_DEBUG, sprintf("Set sequence number to:%d", $appprops[$meetingRequestProps["appSeqNr"]]));
463                    }
464                }
465            }
466
467            // Delete the PR_SENT_REPRESENTING_* properties because some android devices
468            // do not send neither From nor Sender header causing empty PR_SENT_REPRESENTING_NAME and
469            // PR_SENT_REPRESENTING_EMAIL_ADDRESS properties and "broken" PR_SENT_REPRESENTING_ENTRYID
470            // which results in spooler not being able to send the message.
471            // @see http://jira.zarafa.com/browse/ZP-85
472            mapi_deleteprops($mapimessage,
473                array(  $sendMailProps["sentrepresentingname"], $sendMailProps["sentrepresentingemail"], $sendMailProps["representingentryid"],
474                        $sendMailProps["sentrepresentingaddt"], $sendMailProps["sentrepresentinsrchk"]));
475
476            if(isset($sm->source->itemid) && $sm->source->itemid) {
477                $entryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($sm->source->folderid), hex2bin($sm->source->itemid));
478                if ($entryid)
479                    $fwmessage = mapi_msgstore_openentry($this->store, $entryid);
480
481                if(!isset($fwmessage) || !$fwmessage)
482                    throw new StatusException(sprintf("ZarafaBackend->SendMail(): Could not open message id '%s' in folder id '%s' to be replied/forwarded: 0x%X", $sm->source->itemid, $sm->source->folderid, mapi_last_hresult()), SYNC_COMMONSTATUS_ITEMNOTFOUND);
483
484                //update icon when forwarding or replying message
485                if ($sm->forwardflag) mapi_setprops($fwmessage, array(PR_ICON_INDEX=>262));
486                elseif ($sm->replyflag) mapi_setprops($fwmessage, array(PR_ICON_INDEX=>261));
487                mapi_savechanges($fwmessage);
488
489                // only attach the original message if the mobile does not send it itself
490                if (!isset($sm->replacemime)) {
491                    // get message's body in order to append forward or reply text
492                    $body = MAPIUtils::readPropStream($mapimessage, PR_BODY);
493                    $bodyHtml = MAPIUtils::readPropStream($mapimessage, PR_HTML);
494                    $cpid = mapi_getprops($fwmessage, array($sendMailProps["internetcpid"]));
495                    if($sm->forwardflag) {
496                        // attach the original attachments to the outgoing message
497                        $this->copyAttachments($mapimessage, $fwmessage);
498                    }
499
500                    if (strlen($body) > 0) {
501                        $fwbody = MAPIUtils::readPropStream($fwmessage, PR_BODY);
502                        $fwbody = (isset($cpid[$sendMailProps["internetcpid"]])) ? Utils::ConvertCodepageStringToUtf8($cpid[$sendMailProps["internetcpid"]], $fwbody) : w2u($fwbody);
503                        $mapiprops[$sendMailProps["body"]] = $body."\r\n\r\n".$fwbody;
504                    }
505
506                    if (strlen($bodyHtml) > 0) {
507                        $fwbodyHtml = MAPIUtils::readPropStream($fwmessage, PR_HTML);
508                        $fwbodyHtml = (isset($cpid[$sendMailProps["internetcpid"]])) ? Utils::ConvertCodepageStringToUtf8($cpid[$sendMailProps["internetcpid"]], $fwbodyHtml) : w2u($fwbodyHtml);
509                        $mapiprops[$sendMailProps["html"]] = $bodyHtml."<br><br>".$fwbodyHtml;
510                    }
511                }
512            }
513
514            mapi_setprops($mapimessage, $mapiprops);
515            mapi_message_savechanges($mapimessage);
516            mapi_message_submitmessage($mapimessage);
517            $hr = mapi_last_hresult();
518
519            if ($hr)
520                throw new StatusException(sprintf("ZarafaBackend->SendMail(): Error saving/submitting the message to the Outbox: 0x%X", mapi_last_hresult()), SYNC_COMMONSTATUS_MAILSUBMISSIONFAILED);
521
522            ZLog::Write(LOGLEVEL_DEBUG, "ZarafaBackend->SendMail(): email submitted");
523            return true;
524        }
525
526        $mapiprops[$sendMailProps["subject"]] = u2wi(isset($message->headers["subject"])?$message->headers["subject"]:"");
527        $mapiprops[$sendMailProps["messageclass"]] = "IPM.Note";
528        $mapiprops[$sendMailProps["deliverytime"]] = time();
529
530        if(isset($message->headers["x-priority"])) {
531            $this->getImportanceAndPriority($message->headers["x-priority"], $mapiprops, $sendMailProps);
532        }
533
534        $this->addRecipients($message->headers, $mapimessage);
535
536        // Loop through message subparts.
537        $body = "";
538        $body_html = "";
539        if($message->ctype_primary == "multipart" && ($message->ctype_secondary == "mixed" || $message->ctype_secondary == "alternative")) {
540            $mparts = $message->parts;
541            for($i=0; $i<count($mparts); $i++) {
542                $part = $mparts[$i];
543
544                // palm pre & iPhone send forwarded messages in another subpart which are also parsed
545                if($part->ctype_primary == "multipart" && ($part->ctype_secondary == "mixed" || $part->ctype_secondary == "alternative"  || $part->ctype_secondary == "related")) {
546                    foreach($part->parts as $spart)
547                        $mparts[] = $spart;
548                    continue;
549                }
550
551                // standard body
552                if($part->ctype_primary == "text" && $part->ctype_secondary == "plain" && isset($part->body) && (!isset($part->disposition) || $part->disposition != "attachment")) {
553                    $body .= u2wi($part->body); // assume only one text body
554                }
555                // html body
556                elseif($part->ctype_primary == "text" && $part->ctype_secondary == "html") {
557                    $body_html .= u2wi($part->body);
558                }
559                // TNEF
560                elseif($part->ctype_primary == "ms-tnef" || $part->ctype_secondary == "ms-tnef") {
561                    if (!isset($tnefAndIcalProps)) {
562                        $tnefAndIcalProps = MAPIMapping::GetTnefAndIcalProperties();
563                        $tnefAndIcalProps = getPropIdsFromStrings($this->store, $tnefAndIcalProps);
564                    }
565
566                    require_once('tnefparser.php');
567                    $zptnef = new TNEFParser($this->store, $tnefAndIcalProps);
568
569                    $zptnef->ExtractProps($part->body, $mapiprops);
570                    if (is_array($mapiprops) && !empty($mapiprops)) {
571                        //check if it is a recurring item
572                        if (isset($mapiprops[$tnefAndIcalProps["tnefrecurr"]])) {
573                            MAPIUtils::handleRecurringItem($mapiprops, $tnefAndIcalProps);
574                        }
575                    }
576                    else ZLog::Write(LOGLEVEL_WARN, "ZarafaBackend->Sendmail(): TNEFParser: Mapi property array was empty");
577                }
578                // iCalendar
579                elseif($part->ctype_primary == "text" && $part->ctype_secondary == "calendar") {
580                    if (!isset($tnefAndIcalProps)) {
581                        $tnefAndIcalProps = MAPIMapping::GetTnefAndIcalProperties();
582                        $tnefAndIcalProps = getPropIdsFromStrings($this->store, $tnefAndIcalProps);
583                    }
584
585                    require_once('icalparser.php');
586                    $zpical = new ICalParser($this->store, $tnefAndIcalProps);
587                    $zpical->ExtractProps($part->body, $mapiprops);
588
589                    // iPhone sends a second ICS which we ignore if we can
590                    if (!isset($mapiprops[PR_MESSAGE_CLASS]) && strlen(trim($body)) == 0) {
591                        ZLog::Write(LOGLEVEL_WARN, "ZarafaBackend->Sendmail(): Secondary iPhone response is being ignored!! Mail dropped!");
592                        return true;
593                    }
594
595                    if (! Utils::CheckMapiExtVersion("6.30") && is_array($mapiprops) && !empty($mapiprops)) {
596                        mapi_setprops($mapimessage, $mapiprops);
597                    }
598                    else {
599                        // store ics as attachment
600                        //see Utils::IcalTimezoneFix() in utils.php for more information
601                        $part->body = Utils::IcalTimezoneFix($part->body);
602                        MAPIUtils::StoreAttachment($mapimessage, $part);
603                        ZLog::Write(LOGLEVEL_INFO, "ZarafaBackend->Sendmail(): Sending ICS file as attachment");
604                    }
605                }
606                // any other type, store as attachment
607                else
608                    MAPIUtils::StoreAttachment($mapimessage, $part);
609            }
610        }
611        // html main body
612        else if($message->ctype_primary == "text" && $message->ctype_secondary == "html") {
613            $body_html .= u2wi($message->body);
614        }
615        // standard body
616        else {
617            $body = u2wi($message->body);
618        }
619
620        // some devices only transmit a html body
621        if (strlen($body) == 0 && strlen($body_html) > 0) {
622            ZLog::Write(LOGLEVEL_WARN, "ZarafaBackend->SendMail(): only html body sent, transformed into plain text");
623            $body = strip_tags($body_html);
624        }
625
626        if(isset($sm->source->itemid) && $sm->source->itemid) {
627            // Append the original text body for reply/forward
628
629            $entryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($sm->source->folderid), hex2bin($sm->source->itemid));
630            if ($entryid)
631                $fwmessage = mapi_msgstore_openentry($this->store, $entryid);
632
633            if(!isset($fwmessage) || !$fwmessage)
634                throw new StatusException(sprintf("ZarafaBackend->SendMail(): Could not open message id '%s' in folder id '%s' to be replied/forwarded: 0x%X", $sm->source->itemid, $sm->source->folderid, mapi_last_hresult()), SYNC_COMMONSTATUS_ITEMNOTFOUND);
635
636            //update icon when forwarding or replying message
637            if ($sm->forwardflag) mapi_setprops($fwmessage, array(PR_ICON_INDEX=>262));
638            elseif ($sm->replyflag) mapi_setprops($fwmessage, array(PR_ICON_INDEX=>261));
639            mapi_savechanges($fwmessage);
640
641            // only attach the original message if the mobile does not send it itself
642            if (!isset($sm->replacemime)) {
643                $fwbody = MAPIUtils::readPropStream($fwmessage, PR_BODY);
644                $fwbodyHtml = MAPIUtils::readPropStream($fwmessage, PR_HTML);
645
646                if($sm->forwardflag) {
647                    // During a forward, we have to add the forward header ourselves. This is because
648                    // normally the forwarded message is added as an attachment. However, we don't want this
649                    // because it would be rather complicated to copy over the entire original message due
650                    // to the lack of IMessage::CopyTo ..
651                    $fwheader = $this->getForwardHeaders($fwmessage);
652
653                    // add fwheader to body and body_html
654                    $body .= $fwheader;
655                    if (strlen($body_html) > 0)
656                        $body_html .= str_ireplace("\r\n", "<br>", $fwheader);
657
658                    // attach the original attachments to the outgoing message
659                    $this->copyAttachments($mapimessage, $fwmessage);
660                }
661
662                if(strlen($body) > 0)
663                    $body .= $fwbody;
664
665                if (strlen($body_html) > 0)
666                    $body_html .= $fwbodyHtml;
667            }
668        }
669
670        //set PR_INTERNET_CPID to 65001 (utf-8) if store supports it and to 1252 otherwise
671        $internetcpid = INTERNET_CPID_WINDOWS1252;
672        if (defined('STORE_SUPPORTS_UNICODE') && STORE_SUPPORTS_UNICODE == true) {
673            $internetcpid = INTERNET_CPID_UTF8;
674        }
675
676        $mapiprops[$sendMailProps["body"]] = $body;
677        $mapiprops[$sendMailProps["internetcpid"]] = $internetcpid;
678
679
680        if(strlen($body_html) > 0){
681            $mapiprops[$sendMailProps["html"]] = $body_html;
682        }
683
684        //TODO if setting all properties fails, try setting them infividually like in mapiprovider
685        mapi_setprops($mapimessage, $mapiprops);
686
687        mapi_savechanges($mapimessage);
688        mapi_message_submitmessage($mapimessage);
689
690        if(mapi_last_hresult())
691            throw new StatusException(sprintf("ZarafaBackend->SendMail(): Error saving/submitting the message to the Outbox: 0x%X", mapi_last_hresult()), SYNC_COMMONSTATUS_MAILSUBMISSIONFAILED);
692
693        ZLog::Write(LOGLEVEL_DEBUG, "ZarafaBackend->SendMail(): email submitted");
694        return true;
695    }
696
697    /**
698     * Returns all available data of a single message
699     *
700     * @param string            $folderid
701     * @param string            $id
702     * @param ContentParameters $contentparameters flag
703     *
704     * @access public
705     * @return object(SyncObject)
706     * @throws StatusException
707     */
708    public function Fetch($folderid, $id, $contentparameters) {
709        // get the entry id of the message
710        $entryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid), hex2bin($id));
711        if(!$entryid)
712            throw new StatusException(sprintf("BackendZarafa->Fetch('%s','%s'): Error getting entryid: 0x%X", $folderid, $id, mapi_last_hresult()), SYNC_STATUS_OBJECTNOTFOUND);
713
714        // open the message
715        $message = mapi_msgstore_openentry($this->store, $entryid);
716        if(!$message)
717            throw new StatusException(sprintf("BackendZarafa->Fetch('%s','%s'): Error, unable to open message: 0x%X", $folderid, $id, mapi_last_hresult()), SYNC_STATUS_OBJECTNOTFOUND);
718
719        // convert the mapi message into a SyncObject and return it
720        $mapiprovider = new MAPIProvider($this->session, $this->store);
721
722        // override truncation
723        $contentparameters->SetTruncation(SYNC_TRUNCATION_ALL);
724        // TODO check for body preferences
725        return $mapiprovider->GetMessage($message, $contentparameters);
726    }
727
728    /**
729     * Returns the waste basket
730     *
731     * @access public
732     * @return string
733     */
734    public function GetWasteBasket() {
735        if ($this->wastebasket) {
736            return $this->wastebasket;
737        }
738
739        $storeprops = mapi_getprops($this->defaultstore, array(PR_IPM_WASTEBASKET_ENTRYID));
740        if (isset($storeprops[PR_IPM_WASTEBASKET_ENTRYID])) {
741            $wastebasket = mapi_msgstore_openentry($this->store, $storeprops[PR_IPM_WASTEBASKET_ENTRYID]);
742            $wastebasketprops = mapi_getprops($wastebasket, array(PR_SOURCE_KEY));
743            if (isset($wastebasketprops[PR_SOURCE_KEY])) {
744                $this->wastebasket = bin2hex($wastebasketprops[PR_SOURCE_KEY]);
745                ZLog::Write(LOGLEVEL_DEBUG, sprintf("Got waste basket with id '%s'", $this->wastebasket));
746                return $this->wastebasket;
747            }
748        }
749        return false;
750    }
751
752    /**
753     * Returns the content of the named attachment as stream
754     *
755     * @param string        $attname
756     * @access public
757     * @return SyncItemOperationsAttachment
758     * @throws StatusException
759     */
760    public function GetAttachmentData($attname) {
761        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->GetAttachmentData('%s')", $attname));
762        list($id, $attachnum) = explode(":", $attname);
763
764        if(!isset($id) || !isset($attachnum))
765            throw new StatusException(sprintf("ZarafaBackend->GetAttachmentData('%s'): Error, attachment requested for non-existing item", $attname), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
766
767        $entryid = hex2bin($id);
768        $message = mapi_msgstore_openentry($this->store, $entryid);
769        if(!$message)
770            throw new StatusException(sprintf("ZarafaBackend->GetAttachmentData('%s'): Error, unable to open item for attachment data for id '%s' with: 0x%X", $attname, $id, mapi_last_hresult()), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
771
772        $attach = mapi_message_openattach($message, $attachnum);
773        if(!$attach)
774            throw new StatusException(sprintf("ZarafaBackend->GetAttachmentData('%s'): Error, unable to open attachment number '%s' with: 0x%X", $attname, $attachnum, mapi_last_hresult()), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
775
776        $stream = mapi_openpropertytostream($attach, PR_ATTACH_DATA_BIN);
777        if(!$stream)
778            throw new StatusException(sprintf("ZarafaBackend->GetAttachmentData('%s'): Error, unable to open attachment data stream: 0x%X", $attname, mapi_last_hresult()), SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
779
780        //get the mime type of the attachment
781        $contenttype = mapi_getprops($attach, array(PR_ATTACH_MIME_TAG, PR_ATTACH_MIME_TAG_W));
782        $attachment = new SyncItemOperationsAttachment();
783        // put the mapi stream into a wrapper to get a standard stream
784        $attachment->data = MapiStreamWrapper::Open($stream);
785        if (isset($contenttype[PR_ATTACH_MIME_TAG]))
786            $attachment->contenttype = $contenttype[PR_ATTACH_MIME_TAG];
787        elseif (isset($contenttype[PR_ATTACH_MIME_TAG_W]))
788            $attachment->contenttype = $contenttype[PR_ATTACH_MIME_TAG_W];
789            //TODO default contenttype
790        return $attachment;
791    }
792
793
794    /**
795     * Deletes all contents of the specified folder.
796     * This is generally used to empty the trash (wastebasked), but could also be used on any
797     * other folder.
798     *
799     * @param string        $folderid
800     * @param boolean       $includeSubfolders      (opt) also delete sub folders, default true
801     *
802     * @access public
803     * @return boolean
804     * @throws StatusException
805     */
806    public function EmptyFolder($folderid, $includeSubfolders = true) {
807        $folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
808        if (!$folderentryid)
809            throw new StatusException(sprintf("BackendZarafa->EmptyFolder('%s','%s'): Error, unable to open folder (no entry id)", $folderid, Utils::PrintAsString($includeSubfolders)), SYNC_ITEMOPERATIONSSTATUS_SERVERERROR);
810        $folder = mapi_msgstore_openentry($this->store, $folderentryid);
811
812        if (!$folder)
813            throw new StatusException(sprintf("BackendZarafa->EmptyFolder('%s','%s'): Error, unable to open parent folder (open entry)", $folderid, Utils::PrintAsString($includeSubfolders)), SYNC_ITEMOPERATIONSSTATUS_SERVERERROR);
814
815        $flags = 0;
816        if ($includeSubfolders)
817            $flags = DEL_ASSOCIATED;
818
819        ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->EmptyFolder('%s','%s'): emptying folder",$folderid, Utils::PrintAsString($includeSubfolders)));
820
821        // empty folder!
822        mapi_folder_emptyfolder($folder, $flags);
823        if (mapi_last_hresult())
824            throw new StatusException(sprintf("BackendZarafa->EmptyFolder('%s','%s'): Error, mapi_folder_emptyfolder() failed: 0x%X", $folderid, Utils::PrintAsString($includeSubfolders), mapi_last_hresult()), SYNC_ITEMOPERATIONSSTATUS_SERVERERROR);
825
826        return true;
827    }
828
829    /**
830     * Processes a response to a meeting request.
831     * CalendarID is a reference and has to be set if a new calendar item is created
832     *
833     * @param string        $requestid      id of the object containing the request
834     * @param string        $folderid       id of the parent folder of $requestid
835     * @param string        $response
836     *
837     * @access public
838     * @return string       id of the created/updated calendar obj
839     * @throws StatusException
840     */
841    public function MeetingResponse($requestid, $folderid, $response) {
842        // Use standard meeting response code to process meeting request
843        $reqentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid), hex2bin($requestid));
844        if (!$reqentryid)
845            throw new StatusException(sprintf("BackendZarafa->MeetingResponse('%s', '%s', '%s'): Error, unable to entryid of the message 0x%X", $requestid, $folderid, $response, mapi_last_hresult()), SYNC_MEETRESPSTATUS_INVALIDMEETREQ);
846
847        $mapimessage = mapi_msgstore_openentry($this->store, $reqentryid);
848        if(!$mapimessage)
849            throw new StatusException(sprintf("BackendZarafa->MeetingResponse('%s','%s', '%s'): Error, unable to open request message for response 0x%X", $requestid, $folderid, $response, mapi_last_hresult()), SYNC_MEETRESPSTATUS_INVALIDMEETREQ);
850
851        $meetingrequest = new Meetingrequest($this->store, $mapimessage);
852
853        if(!$meetingrequest->isMeetingRequest())
854            throw new StatusException(sprintf("BackendZarafa->MeetingResponse('%s','%s', '%s'): Error, attempt to respond to non-meeting request", $requestid, $folderid, $response), SYNC_MEETRESPSTATUS_INVALIDMEETREQ);
855
856        if($meetingrequest->isLocalOrganiser())
857            throw new StatusException(sprintf("BackendZarafa->MeetingResponse('%s','%s', '%s'): Error, attempt to response to meeting request that we organized", $requestid, $folderid, $response), SYNC_MEETRESPSTATUS_INVALIDMEETREQ);
858
859        // Process the meeting response. We don't have to send the actual meeting response
860        // e-mail, because the device will send it itself.
861        switch($response) {
862            case 1:     // accept
863            default:
864                $entryid = $meetingrequest->doAccept(false, false, false, false, false, false, true); // last true is the $userAction
865                break;
866            case 2:        // tentative
867                $entryid = $meetingrequest->doAccept(true, false, false, false, false, false, true); // last true is the $userAction
868                break;
869            case 3:        // decline
870                $meetingrequest->doDecline(false);
871                break;
872        }
873
874        // F/B will be updated on logoff
875
876        // We have to return the ID of the new calendar item, so do that here
877        $calendarid = "";
878        if (isset($entryid)) {
879            $newitem = mapi_msgstore_openentry($this->store, $entryid);
880            $newprops = mapi_getprops($newitem, array(PR_SOURCE_KEY));
881            $calendarid = bin2hex($newprops[PR_SOURCE_KEY]);
882        }
883
884        // on recurring items, the MeetingRequest class responds with a wrong entryid
885        if ($requestid == $calendarid) {
886            ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->MeetingResponse('%s','%s', '%s'): returned calender id is the same as the requestid - re-searching", $requestid, $folderid, $response));
887
888            $props = MAPIMapping::GetMeetingRequestProperties();
889            $props = getPropIdsFromStrings($this->store, $props);
890
891            $messageprops = mapi_getprops($mapimessage, Array($props["goidtag"]));
892            $goid = $messageprops[$props["goidtag"]];
893
894            $items = $meetingrequest->findCalendarItems($goid);
895
896            if (is_array($items)) {
897               $newitem = mapi_msgstore_openentry($this->store, $items[0]);
898               $newprops = mapi_getprops($newitem, array(PR_SOURCE_KEY));
899               $calendarid = bin2hex($newprops[PR_SOURCE_KEY]);
900               ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->MeetingResponse('%s','%s', '%s'): found other calendar entryid", $requestid, $folderid, $response));
901            }
902
903            if ($requestid == $calendarid)
904                throw new StatusException(sprintf("BackendZarafa->MeetingResponse('%s','%s', '%s'): Error finding the accepted meeting response in the calendar", $requestid, $folderid, $response), SYNC_MEETRESPSTATUS_INVALIDMEETREQ);
905        }
906
907        // delete meeting request from Inbox
908        $folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
909        $folder = mapi_msgstore_openentry($this->store, $folderentryid);
910        mapi_folder_deletemessages($folder, array($reqentryid), 0);
911
912        return $calendarid;
913    }
914
915    /**
916     * Indicates if the backend has a ChangesSink.
917     * A sink is an active notification mechanism which does not need polling.
918     * Since Zarafa 7.0.5 such a sink is available.
919     * The Zarafa backend uses this method to initialize the sink with mapi.
920     *
921     * @access public
922     * @return boolean
923     */
924    public function HasChangesSink() {
925        if (!$this->notifications) {
926            ZLog::Write(LOGLEVEL_DEBUG, "ZarafaBackend->HasChangesSink(): sink is not available");
927            return false;
928        }
929
930        $this->changesSink = @mapi_sink_create();
931
932        if (! $this->changesSink || mapi_last_hresult()) {
933            ZLog::Write(LOGLEVEL_WARN, sprintf("ZarafaBackend->HasChangesSink(): sink could not be created with  0x%X", mapi_last_hresult()));
934            return false;
935        }
936
937        ZLog::Write(LOGLEVEL_DEBUG, "ZarafaBackend->HasChangesSink(): created");
938
939        // advise the main store and also to check if the connection supports it
940        return $this->adviseStoreToSink($this->defaultstore);
941    }
942
943    /**
944     * The folder should be considered by the sink.
945     * Folders which were not initialized should not result in a notification
946     * of IBackend->ChangesSink().
947     *
948     * @param string        $folderid
949     *
950     * @access public
951     * @return boolean      false if entryid can not be found for that folder
952     */
953    public function ChangesSinkInitialize($folderid) {
954        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->ChangesSinkInitialize(): folderid '%s'", $folderid));
955
956        $entryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
957        if (!$entryid)
958            return false;
959
960        // add entryid to the monitored folders
961        $this->changesSinkFolders[$entryid] = $folderid;
962
963        // advise the current store to the sink
964        return $this->adviseStoreToSink($this->store);
965    }
966
967    /**
968     * The actual ChangesSink.
969     * For max. the $timeout value this method should block and if no changes
970     * are available return an empty array.
971     * If changes are available a list of folderids is expected.
972     *
973     * @param int           $timeout        max. amount of seconds to block
974     *
975     * @access public
976     * @return array
977     */
978    public function ChangesSink($timeout = 30) {
979        $notifications = array();
980        $sinkresult = @mapi_sink_timedwait($this->changesSink, $timeout * 1000);
981        foreach ($sinkresult as $sinknotif) {
982            // check if something in the monitored folders changed
983            if (isset($sinknotif['parentid']) && array_key_exists($sinknotif['parentid'], $this->changesSinkFolders)) {
984                $notifications[] = $this->changesSinkFolders[$sinknotif['parentid']];
985            }
986            // deletes and moves
987            if (isset($sinknotif['oldparentid']) && array_key_exists($sinknotif['oldparentid'], $this->changesSinkFolders)) {
988                $notifications[] = $this->changesSinkFolders[$sinknotif['oldparentid']];
989            }
990        }
991        return $notifications;
992    }
993
994    /**
995     * Applies settings to and gets informations from the device
996     *
997     * @param SyncObject        $settings (SyncOOF or SyncUserInformation possible)
998     *
999     * @access public
1000     * @return SyncObject       $settings
1001     */
1002    public function Settings($settings) {
1003        if ($settings instanceof SyncOOF) {
1004            $this->settingsOOF($settings);
1005        }
1006
1007        if ($settings instanceof SyncUserInformation) {
1008            $this->settingsUserInformation($settings);
1009        }
1010
1011        return $settings;
1012    }
1013
1014
1015    /**----------------------------------------------------------------------------------------------------------
1016     * Implementation of the ISearchProvider interface
1017     */
1018
1019    /**
1020     * Indicates if a search type is supported by this SearchProvider
1021     * Currently only the type ISearchProvider::SEARCH_GAL (Global Address List) is implemented
1022     *
1023     * @param string        $searchtype
1024     *
1025     * @access public
1026     * @return boolean
1027     */
1028    public function SupportsType($searchtype) {
1029        return ($searchtype == ISearchProvider::SEARCH_GAL) || ($searchtype == ISearchProvider::SEARCH_MAILBOX);
1030    }
1031
1032    /**
1033     * Searches the GAB of Zarafa
1034     * Can be overwitten globally by configuring a SearchBackend
1035     *
1036     * @param string        $searchquery
1037     * @param string        $searchrange
1038     *
1039     * @access public
1040     * @return array
1041     * @throws StatusException
1042     */
1043    public function GetGALSearchResults($searchquery, $searchrange){
1044        // only return users from who the displayName or the username starts with $name
1045        //TODO: use PR_ANR for this restriction instead of PR_DISPLAY_NAME and PR_ACCOUNT
1046        $addrbook = mapi_openaddressbook($this->session);
1047        if ($addrbook)
1048            $ab_entryid = mapi_ab_getdefaultdir($addrbook);
1049        if ($ab_entryid)
1050            $ab_dir = mapi_ab_openentry($addrbook, $ab_entryid);
1051        if ($ab_dir)
1052            $table = mapi_folder_getcontentstable($ab_dir);
1053
1054        if (!$table)
1055            throw new StatusException(sprintf("ZarafaBackend->GetGALSearchResults(): could not open addressbook: 0x%X", mapi_last_hresult()), SYNC_SEARCHSTATUS_STORE_CONNECTIONFAILED);
1056
1057        $restriction = MAPIUtils::GetSearchRestriction(u2w($searchquery));
1058        mapi_table_restrict($table, $restriction);
1059        mapi_table_sort($table, array(PR_DISPLAY_NAME => TABLE_SORT_ASCEND));
1060
1061        if (mapi_last_hresult())
1062            throw new StatusException(sprintf("ZarafaBackend->GetGALSearchResults(): could not apply restriction: 0x%X", mapi_last_hresult()), SYNC_SEARCHSTATUS_STORE_TOOCOMPLEX);
1063
1064        //range for the search results, default symbian range end is 50, wm 99,
1065        //so we'll use that of nokia
1066        $rangestart = 0;
1067        $rangeend = 50;
1068
1069        if ($searchrange != '0') {
1070            $pos = strpos($searchrange, '-');
1071            $rangestart = substr($searchrange, 0, $pos);
1072            $rangeend = substr($searchrange, ($pos + 1));
1073        }
1074        $items = array();
1075
1076        $querycnt = mapi_table_getrowcount($table);
1077        //do not return more results as requested in range
1078        $querylimit = (($rangeend + 1) < $querycnt) ? ($rangeend + 1) : $querycnt;
1079        $items['range'] = ($querylimit > 0) ? $rangestart.'-'.($querylimit - 1) : '0-0';
1080        $items['searchtotal'] = $querycnt;
1081        if ($querycnt > 0)
1082            $abentries = mapi_table_queryrows($table, array(PR_ACCOUNT, PR_DISPLAY_NAME, PR_SMTP_ADDRESS, PR_BUSINESS_TELEPHONE_NUMBER, PR_GIVEN_NAME, PR_SURNAME, PR_MOBILE_TELEPHONE_NUMBER, PR_HOME_TELEPHONE_NUMBER, PR_TITLE, PR_COMPANY_NAME, PR_OFFICE_LOCATION), $rangestart, $querylimit);
1083
1084        for ($i = 0; $i < $querylimit; $i++) {
1085            $items[$i][SYNC_GAL_DISPLAYNAME] = w2u($abentries[$i][PR_DISPLAY_NAME]);
1086
1087            if (strlen(trim($items[$i][SYNC_GAL_DISPLAYNAME])) == 0)
1088                $items[$i][SYNC_GAL_DISPLAYNAME] = w2u($abentries[$i][PR_ACCOUNT]);
1089
1090            $items[$i][SYNC_GAL_ALIAS] = w2u($abentries[$i][PR_ACCOUNT]);
1091            //it's not possible not get first and last name of an user
1092            //from the gab and user functions, so we just set lastname
1093            //to displayname and leave firstname unset
1094            //this was changed in Zarafa 6.40, so we try to get first and
1095            //last name and fall back to the old behaviour if these values are not set
1096            if (isset($abentries[$i][PR_GIVEN_NAME]))
1097                $items[$i][SYNC_GAL_FIRSTNAME] = w2u($abentries[$i][PR_GIVEN_NAME]);
1098            if (isset($abentries[$i][PR_SURNAME]))
1099                $items[$i][SYNC_GAL_LASTNAME] = w2u($abentries[$i][PR_SURNAME]);
1100
1101            if (!isset($items[$i][SYNC_GAL_LASTNAME])) $items[$i][SYNC_GAL_LASTNAME] = $items[$i][SYNC_GAL_DISPLAYNAME];
1102
1103            $items[$i][SYNC_GAL_EMAILADDRESS] = w2u($abentries[$i][PR_SMTP_ADDRESS]);
1104            //check if an user has an office number or it might produce warnings in the log
1105            if (isset($abentries[$i][PR_BUSINESS_TELEPHONE_NUMBER]))
1106                $items[$i][SYNC_GAL_PHONE] = w2u($abentries[$i][PR_BUSINESS_TELEPHONE_NUMBER]);
1107            //check if an user has a mobile number or it might produce warnings in the log
1108            if (isset($abentries[$i][PR_MOBILE_TELEPHONE_NUMBER]))
1109                $items[$i][SYNC_GAL_MOBILEPHONE] = w2u($abentries[$i][PR_MOBILE_TELEPHONE_NUMBER]);
1110            //check if an user has a home number or it might produce warnings in the log
1111            if (isset($abentries[$i][PR_HOME_TELEPHONE_NUMBER]))
1112                $items[$i][SYNC_GAL_HOMEPHONE] = w2u($abentries[$i][PR_HOME_TELEPHONE_NUMBER]);
1113
1114            if (isset($abentries[$i][PR_COMPANY_NAME]))
1115                $items[$i][SYNC_GAL_COMPANY] = w2u($abentries[$i][PR_COMPANY_NAME]);
1116
1117            if (isset($abentries[$i][PR_TITLE]))
1118                $items[$i][SYNC_GAL_TITLE] = w2u($abentries[$i][PR_TITLE]);
1119
1120            if (isset($abentries[$i][PR_OFFICE_LOCATION]))
1121                $items[$i][SYNC_GAL_OFFICE] = w2u($abentries[$i][PR_OFFICE_LOCATION]);
1122        }
1123        return $items;
1124    }
1125
1126    /**
1127     * Searches for the emails on the server
1128     *
1129     * @param ContentParameter $cpo
1130     *
1131     * @return array
1132     */
1133    public function GetMailboxSearchResults($cpo) {
1134        $searchFolder = $this->getSearchFolder();
1135        $searchRestriction = $this->getSearchRestriction($cpo);
1136        $searchRange = explode('-', $cpo->GetSearchRange());
1137        $searchFolderId = $cpo->GetSearchFolderid();
1138        $searchFolders = array();
1139        // search only in required folders
1140        if (!empty($searchFolderId)) {
1141            $searchFolderEntryId = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($searchFolderId));
1142            $searchFolders[] = $searchFolderEntryId;
1143        }
1144        // if no folder was required then search in the entire store
1145        else {
1146            $tmp = mapi_getprops($this->store, array(PR_ENTRYID,PR_DISPLAY_NAME,PR_IPM_SUBTREE_ENTRYID));
1147            $searchFolders[] = $tmp[PR_IPM_SUBTREE_ENTRYID];
1148        }
1149        $items = array();
1150        $flags = 0;
1151        // if subfolders are required, do a recursive search
1152        if ($cpo->GetSearchDeepTraversal()) {
1153            $flags |= SEARCH_RECURSIVE;
1154        }
1155
1156        mapi_folder_setsearchcriteria($searchFolder, $searchRestriction, $searchFolders, $flags);
1157
1158        $table = mapi_folder_getcontentstable($searchFolder);
1159        $searchStart = time();
1160        // do the search and wait for all the results available
1161        while (time() - $searchStart < SEARCH_WAIT) {
1162            $searchcriteria = mapi_folder_getsearchcriteria($searchFolder);
1163            if(($searchcriteria["searchstate"] & SEARCH_REBUILD) == 0)
1164                break; // Search is done
1165            sleep(1);
1166        }
1167
1168        // if the search range is set limit the result to it, otherwise return all found messages
1169        $rows = (is_array($searchRange) && isset($searchRange[0]) && isset($searchRange[1])) ?
1170            mapi_table_queryrows($table, array(PR_SOURCE_KEY, PR_PARENT_SOURCE_KEY), $searchRange[0], $searchRange[1] - $searchRange[0] + 1) :
1171            mapi_table_queryrows($table, array(PR_SOURCE_KEY, PR_PARENT_SOURCE_KEY), 0, SEARCH_MAXRESULTS);
1172
1173        $cnt = count($rows);
1174        $items['searchtotal'] = $cnt;
1175        $items["range"] = $cpo->GetSearchRange();
1176        for ($i = 0; $i < $cnt; $i++) {
1177            $items[$i]['class'] = 'Email';
1178            $items[$i]['longid'] = bin2hex($rows[$i][PR_PARENT_SOURCE_KEY]) . ":" . bin2hex($rows[$i][PR_SOURCE_KEY]);
1179            $items[$i]['folderid'] = bin2hex($rows[$i][PR_PARENT_SOURCE_KEY]);
1180        }
1181        return $items;
1182    }
1183
1184    /**
1185    * Terminates a search for a given PID
1186    *
1187    * @param int $pid
1188    *
1189    * @return boolean
1190    */
1191    public function TerminateSearch($pid) {
1192        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->TerminateSearch(): terminating search for pid %d", $pid));
1193        $storeProps = mapi_getprops($this->store, array(PR_STORE_SUPPORT_MASK, PR_FINDER_ENTRYID));
1194        if (($storeProps[PR_STORE_SUPPORT_MASK] & STORE_SEARCH_OK) != STORE_SEARCH_OK) {
1195            ZLog::Write(LOGLEVEL_WARN, "Store doesn't support search folders. Public store doesn't have FINDER_ROOT folder");
1196            return false;
1197        }
1198
1199        $finderfolder = mapi_msgstore_openentry($this->store, $storeProps[PR_FINDER_ENTRYID]);
1200        if(mapi_last_hresult() != NOERROR) {
1201            ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to open search folder (0x%X)", mapi_last_hresult()));
1202            return false;
1203        }
1204
1205        $hierarchytable = mapi_folder_gethierarchytable($finderfolder);
1206        mapi_table_restrict($hierarchytable,
1207            array(RES_CONTENT,
1208                array(
1209                    FUZZYLEVEL      => FL_PREFIX,
1210                    ULPROPTAG       => PR_DISPLAY_NAME,
1211                    VALUE           => array(PR_DISPLAY_NAME=>"Z-Push Search Folder ".$pid)
1212                )
1213            ),
1214            TBL_BATCH);
1215
1216        $folders = mapi_table_queryallrows($hierarchytable, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_LAST_MODIFICATION_TIME));
1217        foreach($folders as $folder) {
1218            mapi_folder_deletefolder($finderfolder, $folder[PR_ENTRYID]);
1219        }
1220        return true;
1221    }
1222
1223    /**
1224     * Disconnects from the current search provider
1225     *
1226     * @access public
1227     * @return boolean
1228     */
1229    public function Disconnect() {
1230        return true;
1231    }
1232
1233    /**
1234     * Returns the MAPI store ressource for a folderid
1235     * This is not part of IBackend but necessary for the ImportChangesICS->MoveMessage() operation if
1236     * the destination folder is not in the default store
1237     * Note: The current backend store might be changed as IBackend->Setup() is executed
1238     *
1239     * @param string        $store              target store, could contain a "domain\user" value - if emtpy default store is returned
1240     * @param string        $folderid
1241     *
1242     * @access public
1243     * @return Ressource/boolean
1244     */
1245    public function GetMAPIStoreForFolderId($store, $folderid) {
1246        if ($store == false) {
1247            ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->GetMAPIStoreForFolderId('%s', '%s'): no store specified, returning default store", $store, $folderid));
1248            return $this->defaultstore;
1249        }
1250
1251        // setup the correct store
1252        if ($this->Setup($store, false, $folderid)) {
1253            return $this->store;
1254        }
1255        else {
1256            ZLog::Write(LOGLEVEL_WARN, sprintf("ZarafaBackend->GetMAPIStoreForFolderId('%s', '%s'): store is not available", $store, $folderid));
1257            return false;
1258        }
1259    }
1260
1261
1262    /**----------------------------------------------------------------------------------------------------------
1263     * Private methods
1264     */
1265
1266    /**
1267     * Advises a store to the changes sink
1268     *
1269     * @param mapistore $store              store to be advised
1270     *
1271     * @access private
1272     * @return boolean
1273     */
1274    private function adviseStoreToSink($store) {
1275        // check if we already advised the store
1276        if (!in_array($store, $this->changesSinkStores)) {
1277            mapi_msgstore_advise($this->store, null, fnevObjectModified | fnevObjectCreated | fnevObjectMoved | fnevObjectDeleted, $this->changesSink);
1278            $this->changesSinkStores[] = $store;
1279
1280            if (mapi_last_hresult()) {
1281                ZLog::Write(LOGLEVEL_WARN, sprintf("ZarafaBackend->adviseStoreToSink(): failed to advised store '%s' with code 0x%X. Polling will be performed.", $this->store, mapi_last_hresult()));
1282                return false;
1283            }
1284            else
1285                ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->adviseStoreToSink(): advised store '%s'", $this->store));
1286        }
1287        return true;
1288    }
1289
1290    /**
1291     * Open the store marked with PR_DEFAULT_STORE = TRUE
1292     * if $return_public is set, the public store is opened
1293     *
1294     * @param string    $user               User which store should be opened
1295     *
1296     * @access public
1297     * @return boolean
1298     */
1299    private function openMessageStore($user) {
1300        // During PING requests the operations store has to be switched constantly
1301        // the cache prevents the same store opened several times
1302        if (isset($this->storeCache[$user]))
1303           return  $this->storeCache[$user];
1304
1305        $entryid = false;
1306        $return_public = false;
1307
1308        if (strtoupper($user) == 'SYSTEM')
1309            $return_public = true;
1310
1311        // loop through the storestable if authenticated user of public folder
1312        if ($user == $this->mainUser || $return_public === true) {
1313            // Find the default store
1314            $storestables = mapi_getmsgstorestable($this->session);
1315            $result = mapi_last_hresult();
1316
1317            if ($result == NOERROR){
1318                $rows = mapi_table_queryallrows($storestables, array(PR_ENTRYID, PR_DEFAULT_STORE, PR_MDB_PROVIDER));
1319
1320                foreach($rows as $row) {
1321                    if(!$return_public && isset($row[PR_DEFAULT_STORE]) && $row[PR_DEFAULT_STORE] == true) {
1322                        $entryid = $row[PR_ENTRYID];
1323                        break;
1324                    }
1325                    if ($return_public && isset($row[PR_MDB_PROVIDER]) && $row[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) {
1326                        $entryid = $row[PR_ENTRYID];
1327                        break;
1328                    }
1329                }
1330            }
1331        }
1332        else
1333            $entryid = @mapi_msgstore_createentryid($this->defaultstore, $user);
1334
1335        if($entryid) {
1336            $store = @mapi_openmsgstore($this->session, $entryid);
1337
1338            if (!$store) {
1339                ZLog::Write(LOGLEVEL_WARN, sprintf("ZarafaBackend->openMessageStore('%s'): Could not open store", $user));
1340                return false;
1341            }
1342
1343            // add this store to the cache
1344            if (!isset($this->storeCache[$user]))
1345                $this->storeCache[$user] = $store;
1346
1347            ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZarafaBackend->openMessageStore('%s'): Found '%s' store: '%s'", $user, (($return_public)?'PUBLIC':'DEFAULT'),$store));
1348            return $store;
1349        }
1350        else {
1351            ZLog::Write(LOGLEVEL_WARN, sprintf("ZarafaBackend->openMessageStore('%s'): No store found for this user", $user));
1352            return false;
1353        }
1354    }
1355
1356    private function hasSecretaryACLs($store, $folderid) {
1357        $entryid = mapi_msgstore_entryidfromsourcekey($store, hex2bin($folderid));
1358        if (!$entryid)  return false;
1359
1360        $folder = mapi_msgstore_openentry($store, $entryid);
1361        if (!$folder) return false;
1362
1363        $props = mapi_getprops($folder, array(PR_RIGHTS));
1364        if (isset($props[PR_RIGHTS]) &&
1365            ($props[PR_RIGHTS] & ecRightsReadAny) &&
1366            ($props[PR_RIGHTS] & ecRightsCreate) &&
1367            ($props[PR_RIGHTS] & ecRightsEditOwned) &&
1368            ($props[PR_RIGHTS] & ecRightsDeleteOwned) &&
1369            ($props[PR_RIGHTS] & ecRightsEditAny) &&
1370            ($props[PR_RIGHTS] & ecRightsDeleteAny) &&
1371            ($props[PR_RIGHTS] & ecRightsFolderVisible) ) {
1372            return true;
1373        }
1374        return false;
1375    }
1376
1377    /**
1378     * The meta function for out of office settings.
1379     *
1380     * @param SyncObject $oof
1381     *
1382     * @access private
1383     * @return void
1384     */
1385    private function settingsOOF(&$oof) {
1386        //if oof state is set it must be set of oof and get otherwise
1387        if (isset($oof->oofstate)) {
1388            $this->settingsOOFSEt($oof);
1389        }
1390        else {
1391            $this->settingsOOFGEt($oof);
1392        }
1393    }
1394
1395    /**
1396     * Gets the out of office settings
1397     *
1398     * @param SyncObject $oof
1399     *
1400     * @access private
1401     * @return void
1402     */
1403    private function settingsOOFGEt(&$oof) {
1404        $oofprops = mapi_getprops($this->defaultstore, array(PR_EC_OUTOFOFFICE, PR_EC_OUTOFOFFICE_MSG, PR_EC_OUTOFOFFICE_SUBJECT));
1405        $oof->oofstate = SYNC_SETTINGSOOF_DISABLED;
1406        $oof->Status = SYNC_SETTINGSSTATUS_SUCCESS;
1407        if ($oofprops != false) {
1408            $oof->oofstate = isset($oofprops[PR_EC_OUTOFOFFICE]) ? ($oofprops[PR_EC_OUTOFOFFICE] ? SYNC_SETTINGSOOF_GLOBAL : SYNC_SETTINGSOOF_DISABLED) : SYNC_SETTINGSOOF_DISABLED;
1409            //TODO external and external unknown
1410            $oofmessage = new SyncOOFMessage();
1411            $oofmessage->appliesToInternal = "";
1412            $oofmessage->enabled = $oof->oofstate;
1413            $oofmessage->replymessage = (isset($oofprops[PR_EC_OUTOFOFFICE_MSG])) ? w2u($oofprops[PR_EC_OUTOFOFFICE_MSG]) : "";
1414            $oofmessage->bodytype = $oof->bodytype;
1415            unset($oofmessage->appliesToExternal, $oofmessage->appliesToExternalUnknown);
1416            $oof->oofmessage[] = $oofmessage;
1417        }
1418        else {
1419            ZLog::Write(LOGLEVEL_WARN, "Unable to get out of office information");
1420        }
1421
1422        //unset body type for oof in order not to stream it
1423        unset($oof->bodytype);
1424    }
1425
1426    /**
1427     * Sets the out of office settings.
1428     *
1429     * @param SyncObject $oof
1430     *
1431     * @access private
1432     * @return void
1433     */
1434    private function settingsOOFSEt(&$oof) {
1435        $oof->Status = SYNC_SETTINGSSTATUS_SUCCESS;
1436        $props = array();
1437        if ($oof->oofstate == SYNC_SETTINGSOOF_GLOBAL || $oof->oofstate == SYNC_SETTINGSOOF_TIMEBASED) {
1438            $props[PR_EC_OUTOFOFFICE] = true;
1439            foreach ($oof->oofmessage as $oofmessage) {
1440                if (isset($oofmessage->appliesToInternal)) {
1441                    $props[PR_EC_OUTOFOFFICE_MSG] = isset($oofmessage->replymessage) ? u2w($oofmessage->replymessage) : "";
1442                    $props[PR_EC_OUTOFOFFICE_SUBJECT] = "Out of office";
1443                }
1444            }
1445        }
1446        elseif($oof->oofstate == SYNC_SETTINGSOOF_DISABLED) {
1447            $props[PR_EC_OUTOFOFFICE] = false;
1448        }
1449
1450        if (!empty($props)) {
1451            @mapi_setprops($this->defaultstore, $props);
1452            $result = mapi_last_hresult();
1453            if ($result != NOERROR) {
1454                ZLog::Write(LOGLEVEL_ERROR, sprintf("Setting oof information failed (%X)", $result));
1455                return false;
1456            }
1457        }
1458
1459        return true;
1460    }
1461
1462    /**
1463     * Gets the user's email address from server
1464     *
1465     * @param SyncObject $userinformation
1466     *
1467     * @access private
1468     * @return void
1469     */
1470    private function settingsUserInformation(&$userinformation) {
1471        if (!isset($this->defaultstore) || !isset($this->mainUser)) {
1472            ZLog::Write(LOGLEVEL_ERROR, "The store or user are not available for getting user information");
1473            return false;
1474        }
1475        $user = mapi_zarafa_getuser($this->defaultstore, $this->mainUser);
1476        if ($user != false) {
1477            $userinformation->Status = SYNC_SETTINGSSTATUS_USERINFO_SUCCESS;
1478            $userinformation->emailaddresses[] = $user["emailaddress"];
1479            return true;
1480        }
1481        ZLog::Write(LOGLEVEL_ERROR, sprintf("Getting user information failed: mapi_zarafa_getuser(%X)", mapi_last_hresult()));
1482        return false;
1483    }
1484
1485    /**
1486     * Sets the importance and priority of a message from a RFC822 message headers.
1487     *
1488     * @param int $xPriority
1489     * @param array $mapiprops
1490     *
1491     * @return void
1492     */
1493    private function getImportanceAndPriority($xPriority, &$mapiprops, $sendMailProps) {
1494        switch($xPriority) {
1495            case 1:
1496            case 2:
1497                $priority = PRIO_URGENT;
1498                $importance = IMPORTANCE_HIGH;
1499                break;
1500            case 4:
1501            case 5:
1502                $priority = PRIO_NONURGENT;
1503                $importance = IMPORTANCE_LOW;
1504                break;
1505            case 3:
1506            default:
1507                $priority = PRIO_NORMAL;
1508                $importance = IMPORTANCE_NORMAL;
1509                break;
1510        }
1511        $mapiprops[$sendMailProps["importance"]] = $importance;
1512        $mapiprops[$sendMailProps["priority"]] = $priority;
1513    }
1514
1515    /**
1516     * Adds the recipients to an email message from a RFC822 message headers.
1517     *
1518     * @param MIMEMessageHeader $headers
1519     * @param MAPIMessage $mapimessage
1520     */
1521    private function addRecipients($headers, &$mapimessage) {
1522        $toaddr = $ccaddr = $bccaddr = array();
1523
1524        $Mail_RFC822 = new Mail_RFC822();
1525        if(isset($headers["to"]))
1526            $toaddr = $Mail_RFC822->parseAddressList($headers["to"]);
1527        if(isset($headers["cc"]))
1528            $ccaddr = $Mail_RFC822->parseAddressList($headers["cc"]);
1529        if(isset($headers["bcc"]))
1530            $bccaddr = $Mail_RFC822->parseAddressList($headers["bcc"]);
1531
1532        if(empty($toaddr))
1533            throw new StatusException(sprintf("ZarafaBackend->SendMail(): 'To' address in RFC822 message not found or unparsable. To header: '%s'", ((isset($headers["to"]))?$headers["to"]:'')), SYNC_COMMONSTATUS_MESSHASNORECIP);
1534
1535        // Add recipients
1536        $recips = array();
1537        foreach(array(MAPI_TO => $toaddr, MAPI_CC => $ccaddr, MAPI_BCC => $bccaddr) as $type => $addrlist) {
1538            foreach($addrlist as $addr) {
1539                $mapirecip[PR_ADDRTYPE] = "SMTP";
1540                $mapirecip[PR_EMAIL_ADDRESS] = $addr->mailbox . "@" . $addr->host;
1541                if(isset($addr->personal) && strlen($addr->personal) > 0)
1542                    $mapirecip[PR_DISPLAY_NAME] = u2wi($addr->personal);
1543                else
1544                    $mapirecip[PR_DISPLAY_NAME] = $mapirecip[PR_EMAIL_ADDRESS];
1545
1546                $mapirecip[PR_RECIPIENT_TYPE] = $type;
1547                $mapirecip[PR_ENTRYID] = mapi_createoneoff($mapirecip[PR_DISPLAY_NAME], $mapirecip[PR_ADDRTYPE], $mapirecip[PR_EMAIL_ADDRESS]);
1548
1549                array_push($recips, $mapirecip);
1550            }
1551        }
1552
1553        mapi_message_modifyrecipients($mapimessage, 0, $recips);
1554    }
1555
1556    /**
1557     * Get headers for the forwarded message
1558     *
1559     * @param MAPIMessage $fwmessage
1560     *
1561     * @return string
1562     */
1563    private function getForwardHeaders($message) {
1564        $messageprops = mapi_getprops($message, array(PR_SENT_REPRESENTING_NAME, PR_DISPLAY_TO, PR_DISPLAY_CC, PR_SUBJECT, PR_CLIENT_SUBMIT_TIME));
1565
1566        $fwheader = "\r\n\r\n";
1567        $fwheader .= "-----Original Message-----\r\n";
1568        if(isset($messageprops[PR_SENT_REPRESENTING_NAME]))
1569            $fwheader .= "From: " . $messageprops[PR_SENT_REPRESENTING_NAME] . "\r\n";
1570        if(isset($messageprops[PR_DISPLAY_TO]) && strlen($messageprops[PR_DISPLAY_TO]) > 0)
1571            $fwheader .= "To: " . $messageprops[PR_DISPLAY_TO] . "\r\n";
1572        if(isset($messageprops[PR_DISPLAY_CC]) && strlen($messageprops[PR_DISPLAY_CC]) > 0)
1573            $fwheader .= "Cc: " . $messageprops[PR_DISPLAY_CC] . "\r\n";
1574        if(isset($messageprops[PR_CLIENT_SUBMIT_TIME]))
1575            $fwheader .= "Sent: " . strftime("%x %X", $messageprops[PR_CLIENT_SUBMIT_TIME]) . "\r\n";
1576        if(isset($messageprops[PR_SUBJECT]))
1577            $fwheader .= "Subject: " . $messageprops[PR_SUBJECT] . "\r\n";
1578
1579        return $fwheader."\r\n";
1580    }
1581
1582    /**
1583     * Copies attachments from one message to another.
1584     *
1585     * @param MAPIMessage $toMessage
1586     * @param MAPIMessage $fromMessage
1587     *
1588     * @return void
1589     */
1590    private function copyAttachments(&$toMessage, $fromMessage) {
1591        $attachtable = mapi_message_getattachmenttable($fromMessage);
1592        $rows = mapi_table_queryallrows($attachtable, array(PR_ATTACH_NUM));
1593
1594        foreach($rows as $row) {
1595            if(isset($row[PR_ATTACH_NUM])) {
1596                $attach = mapi_message_openattach($fromMessage, $row[PR_ATTACH_NUM]);
1597
1598                $newattach = mapi_message_createattach($toMessage);
1599
1600                // Copy all attachments from old to new attachment
1601                $attachprops = mapi_getprops($attach);
1602                mapi_setprops($newattach, $attachprops);
1603
1604                if(isset($attachprops[mapi_prop_tag(PT_ERROR, mapi_prop_id(PR_ATTACH_DATA_BIN))])) {
1605                    // Data is in a stream
1606                    $srcstream = mapi_openpropertytostream($attach, PR_ATTACH_DATA_BIN);
1607                    $dststream = mapi_openpropertytostream($newattach, PR_ATTACH_DATA_BIN, MAPI_MODIFY | MAPI_CREATE);
1608
1609                    while(1) {
1610                        $data = mapi_stream_read($srcstream, 4096);
1611                        if(strlen($data) == 0)
1612                            break;
1613
1614                        mapi_stream_write($dststream, $data);
1615                    }
1616
1617                    mapi_stream_commit($dststream);
1618                }
1619                mapi_savechanges($newattach);
1620            }
1621        }
1622    }
1623
1624   /**
1625    * Function will create a search folder in FINDER_ROOT folder
1626    * if folder exists then it will open it
1627    *
1628    * @see createSearchFolder($store, $openIfExists = true) function in the webaccess
1629    *
1630    * @return mapiFolderObject $folder created search folder
1631    */
1632    private function getSearchFolder() {
1633        // create new or open existing search folder
1634        $searchFolderRoot = $this->getSearchFoldersRoot($this->store);
1635        if($searchFolderRoot === false) {
1636            // error in finding search root folder
1637            // or store doesn't support search folders
1638            return false;
1639        }
1640
1641        $searchFolder = $this->createSearchFolder($searchFolderRoot);
1642
1643        if($searchFolder !== false && mapi_last_hresult() == NOERROR) {
1644            return $searchFolder;
1645        }
1646        return false;
1647    }
1648
1649   /**
1650    * Function will open FINDER_ROOT folder in root container
1651    * public folder's don't have FINDER_ROOT folder
1652    *
1653    * @see getSearchFoldersRoot($store) function in the webaccess
1654    *
1655    * @return mapiFolderObject root folder for search folders
1656    */
1657    private function getSearchFoldersRoot() {
1658        // check if we can create search folders
1659        $storeProps = mapi_getprops($this->store, array(PR_STORE_SUPPORT_MASK, PR_FINDER_ENTRYID));
1660        if(($storeProps[PR_STORE_SUPPORT_MASK] & STORE_SEARCH_OK) != STORE_SEARCH_OK) {
1661            ZLog::Write(LOGLEVEL_WARN, "Store doesn't support search folders. Public store doesn't have FINDER_ROOT folder");
1662            return false;
1663        }
1664
1665        // open search folders root
1666        $searchRootFolder = mapi_msgstore_openentry($this->store, $storeProps[PR_FINDER_ENTRYID]);
1667        if(mapi_last_hresult() != NOERROR) {
1668            ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to open search folder (0x%X)", mapi_last_hresult()));
1669            return false;
1670        }
1671
1672        return $searchRootFolder;
1673    }
1674
1675
1676    /**
1677     * Creates a search folder if it not exists or opens an existing one
1678     * and returns it.
1679     *
1680     * @param mapiFolderObject $searchFolderRoot
1681     *
1682     * @return mapiFolderObject
1683     */
1684    private function createSearchFolder($searchFolderRoot) {
1685        $folderName = "Z-Push Search Folder ".@getmypid();
1686        $searchFolders = mapi_folder_gethierarchytable($searchFolderRoot);
1687        $restriction = array(
1688            RES_CONTENT,
1689            array(
1690                    FUZZYLEVEL      => FL_PREFIX,
1691                    ULPROPTAG       => PR_DISPLAY_NAME,
1692                    VALUE           => array(PR_DISPLAY_NAME=>$folderName)
1693            )
1694        );
1695        //restrict the hierarchy to the z-push search folder only
1696        mapi_table_restrict($searchFolders, $restriction);
1697        if (mapi_table_getrowcount($searchFolders)) {
1698            $searchFolder = mapi_table_queryrows($searchFolders, array(PR_ENTRYID), 0, 1);
1699
1700            return mapi_msgstore_openentry($this->store, $searchFolder[0][PR_ENTRYID]);
1701        }
1702        return mapi_folder_createfolder($searchFolderRoot, $folderName, null, 0, FOLDER_SEARCH);
1703    }
1704
1705    /**
1706     * Creates a search restriction
1707     *
1708     * @param ContentParameter $cpo
1709     * @return array
1710     */
1711    private function getSearchRestriction($cpo) {
1712        $searchText = $cpo->GetSearchFreeText();
1713
1714        $searchGreater = strtotime($cpo->GetSearchValueGreater());
1715        $searchLess = strtotime($cpo->GetSearchValueLess());
1716
1717        // split the search on whitespache and look for every word
1718        $searchText = preg_split("/\W+/", $searchText);
1719        $searchProps = array(PR_BODY, PR_SUBJECT, PR_DISPLAY_TO, PR_DISPLAY_CC, PR_SENDER_NAME, PR_SENDER_EMAIL_ADDRESS, PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_EMAIL_ADDRESS);
1720        $resAnd = array();
1721        foreach($searchText as $term) {
1722            $resOr = array();
1723
1724            foreach($searchProps as $property) {
1725                array_push($resOr,
1726                    array(RES_CONTENT,
1727                        array(
1728                            FUZZYLEVEL => FL_SUBSTRING|FL_IGNORECASE,
1729                            ULPROPTAG => $property,
1730                            VALUE => u2w($term)
1731                        )
1732                    )
1733                );
1734            }
1735            array_push($resAnd, array(RES_OR, $resOr));
1736        }
1737
1738        // add time range restrictions
1739        if ($searchGreater) {
1740            array_push($resAnd, array(RES_PROPERTY, array(RELOP => RELOP_GE, ULPROPTAG => PR_MESSAGE_DELIVERY_TIME, VALUE => array(PR_MESSAGE_DELIVERY_TIME => $searchGreater)))); // RES_AND;
1741        }
1742        if ($searchLess) {
1743            array_push($resAnd, array(RES_PROPERTY, array(RELOP => RELOP_LE, ULPROPTAG => PR_MESSAGE_DELIVERY_TIME, VALUE => array(PR_MESSAGE_DELIVERY_TIME => $searchLess))));
1744        }
1745        $mapiquery = array(RES_AND, $resAnd);
1746
1747        return $mapiquery;
1748    }
1749}
1750
1751/**
1752 * DEPRECATED legacy class
1753 */
1754class BackendICS extends BackendZarafa {}
1755
1756?>
Note: See TracBrowser for help on using the repository browser.