source: trunk/zpush/lib/request/itemoperations.php @ 7589

Revision 7589, 15.6 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      :   itemoperations.php
4* Project   :   Z-Push
5* Descr     :   Provides the ItemOperations command
6*
7* Created   :   16.02.2012
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 ItemOperations extends RequestProcessor {
45
46    /**
47     * Handles the ItemOperations command
48     * Provides batched online handling for Fetch, EmptyFolderContents and Move
49     *
50     * @param int       $commandCode
51     *
52     * @access public
53     * @return boolean
54     */
55    public function Handle($commandCode) {
56        // Parse input
57        if(!self::$decoder->getElementStartTag(SYNC_ITEMOPERATIONS_ITEMOPERATIONS))
58            return false;
59
60        $itemoperations = array();
61        //ItemOperations can either be Fetch, EmptyFolderContents or Move
62        while (1) {
63            //TODO check if multiple item operations are possible in one request
64            $el = self::$decoder->getElement();
65
66            if($el[EN_TYPE] != EN_TYPE_STARTTAG)
67                return false;
68
69            $fetch = $efc = $move = false;
70            $operation = array();
71            if($el[EN_TAG] == SYNC_ITEMOPERATIONS_FETCH) {
72                $fetch = true;
73                $operation['operation'] = SYNC_ITEMOPERATIONS_FETCH;
74                self::$topCollector->AnnounceInformation("Fetch", true);
75            }
76            else if($el[EN_TAG] == SYNC_ITEMOPERATIONS_EMPTYFOLDERCONTENTS) {
77                $efc = true;
78                $operation['operation'] = SYNC_ITEMOPERATIONS_EMPTYFOLDERCONTENTS;
79                self::$topCollector->AnnounceInformation("Empty Folder", true);
80            }
81            else if($el[EN_TAG] == SYNC_ITEMOPERATIONS_MOVE) {
82                $move = true;
83                $operation['operation'] = SYNC_ITEMOPERATIONS_MOVE;
84                self::$topCollector->AnnounceInformation("Move", true);
85            }
86
87            if(!$fetch && !$efc && !$move) {
88                ZLog::Write(LOGLEVEL_DEBUG, "Unknown item operation:".print_r($el, 1));
89                self::$topCollector->AnnounceInformation("Unknown operation", true);
90                return false;
91            }
92
93            if ($fetch) {
94                if(!self::$decoder->getElementStartTag(SYNC_ITEMOPERATIONS_STORE))
95                    return false;
96                $operation['store'] = self::$decoder->getElementContent();
97                if(!self::$decoder->getElementEndTag())
98                    return false;//SYNC_ITEMOPERATIONS_STORE
99
100                if(self::$decoder->getElementStartTag(SYNC_SEARCH_LONGID)) {
101                    $operation['longid'] = self::$decoder->getElementContent();
102                    if(!self::$decoder->getElementEndTag())
103                        return false;//SYNC_SEARCH_LONGID
104                }
105
106                if(self::$decoder->getElementStartTag(SYNC_FOLDERID)) {
107                    $operation['folderid'] = self::$decoder->getElementContent();
108                    if(!self::$decoder->getElementEndTag())
109                        return false;//SYNC_FOLDERID
110                }
111
112                if(self::$decoder->getElementStartTag(SYNC_SERVERENTRYID)) {
113                    $operation['serverid'] = self::$decoder->getElementContent();
114                    if(!self::$decoder->getElementEndTag())
115                        return false;//SYNC_SERVERENTRYID
116                }
117
118                if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_FILEREFERENCE)) {
119                    $operation['filereference'] = self::$decoder->getElementContent();
120                    if(!self::$decoder->getElementEndTag())
121                        return false;//SYNC_AIRSYNCBASE_FILEREFERENCE
122                }
123
124                if(self::$decoder->getElementStartTag(SYNC_ITEMOPERATIONS_OPTIONS)) {
125                    //TODO other options
126                    //schema
127                    //range
128                    //username
129                    //password
130                    //bodypartpreference
131                    //rm:RightsManagementSupport
132
133                    // Save all OPTIONS into a ContentParameters object
134                    $operation["cpo"] = new ContentParameters();
135                    while(1) {
136                        while (self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_BODYPREFERENCE)) {
137                            if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TYPE)) {
138                                $bptype = self::$decoder->getElementContent();
139                                $operation["cpo"]->BodyPreference($bptype);
140                                if(!self::$decoder->getElementEndTag()) {
141                                    return false;
142                                }
143                            }
144
145                            if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_TRUNCATIONSIZE)) {
146                                $operation["cpo"]->BodyPreference($bptype)->SetTruncationSize(self::$decoder->getElementContent());
147                                if(!self::$decoder->getElementEndTag())
148                                    return false;
149                            }
150
151                            if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_ALLORNONE)) {
152                                $operation["cpo"]->BodyPreference($bptype)->SetAllOrNone(self::$decoder->getElementContent());
153                                if(!self::$decoder->getElementEndTag())
154                                    return false;
155                            }
156
157                            if(self::$decoder->getElementStartTag(SYNC_AIRSYNCBASE_PREVIEW)) {
158                                $operation["cpo"]->BodyPreference($bptype)->SetPreview(self::$decoder->getElementContent());
159                                if(!self::$decoder->getElementEndTag())
160                                    return false;
161                            }
162
163                            if(!self::$decoder->getElementEndTag())
164                                return false;//SYNC_AIRSYNCBASE_BODYPREFERENCE
165                        }
166
167                        if(self::$decoder->getElementStartTag(SYNC_MIMESUPPORT)) {
168                            $operation["cpo"]->SetMimeSupport(self::$decoder->getElementContent());
169                            if(!self::$decoder->getElementEndTag())
170                                return false;
171                        }
172
173                        if(self::$decoder->getElementStartTag(SYNC_ITEMOPERATIONS_RANGE)) {
174                            $operation["range"] = self::$decoder->getElementContent();
175                            if(!self::$decoder->getElementEndTag())
176                                return false;
177                        }
178
179                        //break if it reached the endtag
180                        $e = self::$decoder->peek();
181                        if($e[EN_TYPE] == EN_TYPE_ENDTAG) {
182                            self::$decoder->getElementEndTag();
183                            break;
184                        }
185                    }
186                }
187            }
188
189            if ($efc) {
190                if(self::$decoder->getElementStartTag(SYNC_FOLDERID)) {
191                    $operation['folderid'] = self::$decoder->getElementContent();
192                    if(!self::$decoder->getElementEndTag())
193                        return false;//SYNC_FOLDERID
194                }
195                if(self::$decoder->getElementStartTag(SYNC_ITEMOPERATIONS_OPTIONS)) {
196                    if(self::$decoder->getElementStartTag(SYNC_ITEMOPERATIONS_DELETESUBFOLDERS)) {
197                        $operation['deletesubfolders'] = true;
198                        if (($dsf = self::$decoder->getElementContent()) !== false) {
199                            $operation['deletesubfolders'] = (boolean)$dsf;
200                            if(!self::$decoder->getElementEndTag())
201                                return false;
202                        }
203                    }
204                    self::$decoder->getElementEndTag();
205                }
206            }
207
208            //TODO move
209
210            if(!self::$decoder->getElementEndTag())
211                return false; //SYNC_ITEMOPERATIONS_FETCH or SYNC_ITEMOPERATIONS_EMPTYFOLDERCONTENTS or SYNC_ITEMOPERATIONS_MOVE
212
213            $itemoperations[] = $operation;
214            //break if it reached the endtag
215            $e = self::$decoder->peek();
216            if($e[EN_TYPE] == EN_TYPE_ENDTAG) {
217                self::$decoder->getElementEndTag(); //SYNC_ITEMOPERATIONS_ITEMOPERATIONS
218                break;
219            }
220
221        }
222
223//         if(!self::$decoder->getElementEndTag())
224//             return false;//SYNC_ITEMOPERATIONS_ITEMOPERATIONS
225
226        $status = SYNC_ITEMOPERATIONSSTATUS_SUCCESS;
227        //TODO status handling
228
229        self::$encoder->startWBXML();
230
231        self::$encoder->startTag(SYNC_ITEMOPERATIONS_ITEMOPERATIONS);
232
233        self::$encoder->startTag(SYNC_ITEMOPERATIONS_STATUS);
234        self::$encoder->content($status);
235        self::$encoder->endTag();//SYNC_ITEMOPERATIONS_STATUS
236
237        self::$encoder->startTag(SYNC_ITEMOPERATIONS_RESPONSE);
238
239        foreach ($itemoperations as $operation) {
240            // fetch response
241            if ($operation['operation'] == SYNC_ITEMOPERATIONS_FETCH) {
242                self::$encoder->startTag(SYNC_ITEMOPERATIONS_FETCH);
243
244                    self::$encoder->startTag(SYNC_ITEMOPERATIONS_STATUS);
245                    self::$encoder->content($status);
246                    self::$encoder->endTag();//SYNC_ITEMOPERATIONS_STATUS
247
248                    if (isset($operation['folderid']) && isset($operation['serverid'])) {
249                        self::$encoder->startTag(SYNC_FOLDERID);
250                        self::$encoder->content($operation['folderid']);
251                        self::$encoder->endTag(); // end SYNC_FOLDERID
252
253                        self::$encoder->startTag(SYNC_SERVERENTRYID);
254                        self::$encoder->content($operation['serverid']);
255                        self::$encoder->endTag(); // end SYNC_SERVERENTRYID
256
257                        self::$encoder->startTag(SYNC_FOLDERTYPE);
258                        self::$encoder->content("Email");
259                        self::$encoder->endTag();
260
261                        self::$topCollector->AnnounceInformation("Fetching data from backend with item and folder id");
262
263                        $data = self::$backend->Fetch($operation['folderid'], $operation['serverid'], $operation["cpo"]);
264                    }
265
266                    if (isset($longid)) {
267                        self::$encoder->startTag(SYNC_SEARCH_LONGID);
268                        self::$encoder->content($operation['longid']);
269                        self::$encoder->endTag(); // end SYNC_FOLDERID
270
271                        self::$encoder->startTag(SYNC_FOLDERTYPE);
272                        self::$encoder->content("Email");
273                        self::$encoder->endTag();
274
275                        $tmp = explode(":", $operation['longid']);
276
277                        self::$topCollector->AnnounceInformation("Fetching data from backend with long id");
278
279                        $data = self::$backend->Fetch($tmp[0], $tmp[1], $operation["cpo"]);
280                    }
281
282                    if (isset($operation['filereference'])) {
283                        self::$encoder->startTag(SYNC_AIRSYNCBASE_FILEREFERENCE);
284                        self::$encoder->content($operation['filereference']);
285                        self::$encoder->endTag(); // end SYNC_AIRSYNCBASE_FILEREFERENCE
286
287                        self::$topCollector->AnnounceInformation("Get attachment data from backend with file reference");
288
289                        $data = self::$backend->GetAttachmentData($operation['filereference']);
290                    }
291
292                    //TODO put it in try catch block
293
294                    if (isset($data)) {
295                        self::$topCollector->AnnounceInformation("Streaming data");
296
297                        self::$encoder->startTag(SYNC_ITEMOPERATIONS_PROPERTIES);
298                        $data->Encode(self::$encoder);
299                        self::$encoder->endTag(); //SYNC_ITEMOPERATIONS_PROPERTIES
300                    }
301
302                self::$encoder->endTag();//SYNC_ITEMOPERATIONS_FETCH
303            }
304            // empty folder contents operation
305            else if ($operation['operation'] == SYNC_ITEMOPERATIONS_EMPTYFOLDERCONTENTS) {
306                try {
307                    self::$topCollector->AnnounceInformation("Emptying folder");
308
309                    // send request to backend
310                    self::$backend->EmptyFolder($operation['folderid'], $operation['deletesubfolders']);
311                }
312                catch (StatusException $stex) {
313                   $status = $stex->getCode();
314                }
315
316                self::$encoder->startTag(SYNC_ITEMOPERATIONS_EMPTYFOLDERCONTENTS);
317
318                    self::$encoder->startTag(SYNC_ITEMOPERATIONS_STATUS);
319                    self::$encoder->content($status);
320                    self::$encoder->endTag();//SYNC_ITEMOPERATIONS_STATUS
321
322                    if (isset($operation['folderid'])) {
323                        self::$encoder->startTag(SYNC_FOLDERID);
324                        self::$encoder->content($operation['folderid']);
325                        self::$encoder->endTag(); // end SYNC_FOLDERID
326                    }
327                self::$encoder->endTag();//SYNC_ITEMOPERATIONS_EMPTYFOLDERCONTENTS
328            }
329            // TODO implement ItemOperations Move
330            // move operation
331            else {
332                self::$topCollector->AnnounceInformation("not implemented", true);
333
334                self::$encoder->startTag(SYNC_ITEMOPERATIONS_MOVE);
335                    self::$encoder->startTag(SYNC_ITEMOPERATIONS_STATUS);
336                    self::$encoder->content($status);
337                    self::$encoder->endTag();//SYNC_ITEMOPERATIONS_STATUS
338                self::$encoder->endTag();//SYNC_ITEMOPERATIONS_MOVE
339            }
340
341        }
342        self::$encoder->endTag();//SYNC_ITEMOPERATIONS_RESPONSE
343        self::$encoder->endTag();//SYNC_ITEMOPERATIONS_ITEMOPERATIONS
344
345        return true;
346    }
347}
348?>
Note: See TracBrowser for help on using the repository browser.