source: trunk/zpush/lib/core/streamimporter.php @ 7589

Revision 7589, 9.0 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      :   streamimporter.php
4* Project   :   Z-Push
5* Descr     :   sends changes directly to the wbxml stream
6*
7* Created   :   01.10.2007
8*
9* Copyright 2007 - 2012 Zarafa Deutschland GmbH
10*
11* This program is free software: you can redistribute it and/or modify
12* it under the terms of the GNU Affero General Public License, version 3,
13* as published by the Free Software Foundation with the following additional
14* term according to sec. 7:
15*
16* According to sec. 7 of the GNU Affero General Public License, version 3,
17* the terms of the AGPL are supplemented with the following terms:
18*
19* "Zarafa" is a registered trademark of Zarafa B.V.
20* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
21* The licensing of the Program under the AGPL does not imply a trademark license.
22* Therefore any rights, title and interest in our trademarks remain entirely with us.
23*
24* However, if you propagate an unmodified version of the Program you are
25* allowed to use the term "Z-Push" to indicate that you distribute the Program.
26* Furthermore you may use our trademarks where it is necessary to indicate
27* the intended purpose of a product or service provided you use it in accordance
28* with honest practices in industrial or commercial matters.
29* If you want to propagate modified versions of the Program under the name "Z-Push",
30* you may only do so if you have a written permission by Zarafa Deutschland GmbH
31* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
32*
33* This program is distributed in the hope that it will be useful,
34* but WITHOUT ANY WARRANTY; without even the implied warranty of
35* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36* GNU Affero General Public License for more details.
37*
38* You should have received a copy of the GNU Affero General Public License
39* along with this program.  If not, see <http://www.gnu.org/licenses/>.
40*
41* Consult LICENSE file for details
42************************************************/
43
44class ImportChangesStream implements IImportChanges {
45    private $encoder;
46    private $objclass;
47    private $seenObjects;
48    private $importedMsgs;
49    private $checkForIgnoredMessages;
50
51    /**
52     * Constructor of the StreamImporter
53     *
54     * @param WBXMLEncoder  $encoder        Objects are streamed to this encoder
55     * @param SyncObject    $class          SyncObject class (only these are accepted when streaming content messages)
56     *
57     * @access public
58     */
59    public function ImportChangesStream(&$encoder, $class) {
60        $this->encoder = &$encoder;
61        $this->objclass = $class;
62        $this->classAsString = (is_object($class))?get_class($class):'';
63        $this->seenObjects = array();
64        $this->importedMsgs = 0;
65        $this->checkForIgnoredMessages = true;
66    }
67
68    /**
69     * Implement interface - never used
70     */
71    public function Config($state, $flags = 0) { return true; }
72    public function GetState() { return false;}
73    public function LoadConflicts($contentparameters, $state) { return true; }
74
75    /**
76     * Imports a single message
77     *
78     * @param string        $id
79     * @param SyncObject    $message
80     *
81     * @access public
82     * @return boolean
83     */
84    public function ImportMessageChange($id, $message) {
85        // ignore other SyncObjects
86        if(!($message instanceof $this->classAsString))
87            return false;
88
89        // prevent sending the same object twice in one request
90        if (in_array($id, $this->seenObjects)) {
91            ZLog::Write(LOGLEVEL_DEBUG, sprintf("Object '%s' discarded! Object already sent in this request.", $id));
92            return true;
93        }
94
95        $this->importedMsgs++;
96        $this->seenObjects[] = $id;
97
98        // checks if the next message may cause a loop or is broken
99        if (ZPush::GetDeviceManager()->DoNotStreamMessage($id, $message)) {
100            ZLog::Write(LOGLEVEL_DEBUG, sprintf("ImportChangesStream->ImportMessageChange('%s'): message ignored and requested to be removed from mobile", $id));
101
102            // this is an internal operation & should not trigger an update in the device manager
103            $this->checkForIgnoredMessages = false;
104            $stat = $this->ImportMessageDeletion($id);
105            $this->checkForIgnoredMessages = true;
106
107            return $stat;
108        }
109
110        if ($message->flags === false || $message->flags === SYNC_NEWMESSAGE)
111            $this->encoder->startTag(SYNC_ADD);
112        else {
113            // on update of an SyncEmail we only export the flags
114            if($message instanceof SyncMail && isset($message->flag) && $message->flag instanceof SyncMailFlags) {
115                $newmessage = new SyncMail();
116                $newmessage->read = $message->read;
117                $newmessage->flag = $message->flag;
118                $message = $newmessage;
119                unset($newmessage);
120                ZLog::Write(LOGLEVEL_DEBUG, sprintf("ImportChangesStream->ImportMessageChange('%s'): SyncMail message updated. Message content is striped, only flags are streamed.", $id));
121            }
122
123            $this->encoder->startTag(SYNC_MODIFY);
124        }
125            $this->encoder->startTag(SYNC_SERVERENTRYID);
126                $this->encoder->content($id);
127            $this->encoder->endTag();
128            $this->encoder->startTag(SYNC_DATA);
129                $message->Encode($this->encoder);
130            $this->encoder->endTag();
131        $this->encoder->endTag();
132
133        return true;
134    }
135
136    /**
137     * Imports a deletion
138     *
139     * @param string        $id
140     *
141     * @access public
142     * @return boolean
143     */
144    public function ImportMessageDeletion($id) {
145        if ($this->checkForIgnoredMessages) {
146           ZPush::GetDeviceManager()->RemoveBrokenMessage($id);
147        }
148
149        $this->importedMsgs++;
150        $this->encoder->startTag(SYNC_REMOVE);
151            $this->encoder->startTag(SYNC_SERVERENTRYID);
152                $this->encoder->content($id);
153            $this->encoder->endTag();
154        $this->encoder->endTag();
155
156        return true;
157    }
158
159    /**
160     * Imports a change in 'read' flag
161     * Can only be applied to SyncMail (Email) requests
162     *
163     * @param string        $id
164     * @param int           $flags - read/unread
165     *
166     * @access public
167     * @return boolean
168     */
169    public function ImportMessageReadFlag($id, $flags) {
170        if(!($this->objclass instanceof SyncMail))
171            return false;
172
173        $this->importedMsgs++;
174
175        $this->encoder->startTag(SYNC_MODIFY);
176            $this->encoder->startTag(SYNC_SERVERENTRYID);
177                $this->encoder->content($id);
178            $this->encoder->endTag();
179            $this->encoder->startTag(SYNC_DATA);
180                $this->encoder->startTag(SYNC_POOMMAIL_READ);
181                    $this->encoder->content($flags);
182                $this->encoder->endTag();
183            $this->encoder->endTag();
184        $this->encoder->endTag();
185
186        return true;
187    }
188
189    /**
190     * ImportMessageMove is not implemented, as this operation can not be streamed to a WBXMLEncoder
191     *
192     * @param string        $id
193     * @param int           $flags      read/unread
194     *
195     * @access public
196     * @return boolean
197     */
198    public function ImportMessageMove($id, $newfolder) {
199        return true;
200    }
201
202    /**
203     * Imports a change on a folder
204     *
205     * @param object        $folder     SyncFolder
206     *
207     * @access public
208     * @return string       id of the folder
209     */
210    public function ImportFolderChange($folder) {
211        // checks if the next message may cause a loop or is broken
212        if (ZPush::GetDeviceManager(false) && ZPush::GetDeviceManager()->DoNotStreamMessage($folder->serverid, $folder)) {
213            ZLog::Write(LOGLEVEL_DEBUG, sprintf("ImportChangesStream->ImportFolderChange('%s'): folder ignored as requested by DeviceManager.", $folder->serverid));
214            return true;
215        }
216
217        // send a modify flag if the folder is already known on the device
218        if (isset($folder->flags) && $folder->flags === SYNC_NEWMESSAGE)
219            $this->encoder->startTag(SYNC_FOLDERHIERARCHY_ADD);
220        else
221            $this->encoder->startTag(SYNC_FOLDERHIERARCHY_UPDATE);
222
223        $folder->Encode($this->encoder);
224        $this->encoder->endTag();
225
226        return true;
227    }
228
229    /**
230     * Imports a folder deletion
231     *
232     * @param string        $id
233     * @param string        $parent id
234     *
235     * @access public
236     * @return boolean
237     */
238    public function ImportFolderDeletion($id, $parent = false) {
239        $this->encoder->startTag(SYNC_FOLDERHIERARCHY_REMOVE);
240            $this->encoder->startTag(SYNC_FOLDERHIERARCHY_SERVERENTRYID);
241                $this->encoder->content($id);
242            $this->encoder->endTag();
243        $this->encoder->endTag();
244
245        return true;
246    }
247
248    /**
249     * Returns the number of messages which were changed, deleted and had changed read status
250     *
251     * @access public
252     * @return int
253     */
254    public function GetImportedMessages() {
255        return $this->importedMsgs;
256    }
257}
258?>
Note: See TracBrowser for help on using the repository browser.