source: branches/2.5/zpush/backend/expresso/expresso.php @ 8232

Revision 8232, 14.8 KB checked in by douglas, 11 years ago (diff)

Ticket #0000 - Copiadas as alterações do Trunk. Versão final 2.5.1.

Line 
1<?php
2require_once __DIR__ . '/../../lib/default/diffbackend/diffbackend.php';
3require_once __DIR__ . '/providers/imapProvider.php';
4require_once __DIR__ . '/providers/contactProvider.php';
5require_once __DIR__ . '/providers/calendarProvider.php';
6
7class BackendExpresso extends BackendDiff
8{
9    var $providers = array('Contact','Imap','Calendar');
10    var $providerInstances;
11    var $providersFolderMap;
12    var $sendMailProvider = 'Imap';
13    var $cacheFolders = array();
14
15    function __construct()
16    {
17        foreach($this->providers as $provider)
18        {
19            $providerClass = 'Expresso'.$provider.'Provider';
20            $this->providerInstances[$provider] = new $providerClass();
21        }
22    }
23
24    private function getProvider( $folderId )
25    {
26
27
28        foreach($this->providers as $provider)
29        {
30            if(!isset($this->cacheFolders[$provider]))
31                $this->cacheFolders[$provider] =  $this->providerInstances[$provider]->GetFolderList();
32
33            foreach($this->cacheFolders[$provider] as $folder)
34            {
35                if($folder['id'] == $folderId && is_object($this->providerInstances[$provider]))
36                    return $this->providerInstances[$provider];
37            }
38        }
39
40        throw new FatalException("BackendExpresso->getProvider(): Provide not found", 0, null, LOGLEVEL_FATAL);
41    }
42
43    /**
44     * Returns a list (array) of folders, each entry being an associative array
45     * with the same entries as StatFolder(). This method should return stable information; ie
46     * if nothing has changed, the items in the array must be exactly the same. The order of
47     * the items within the array is not important though.
48     *
49     * @access protected
50     * @return array/boolean        false if the list could not be retrieved
51     */
52    public function GetFolderList()
53    {
54        $return = array();
55
56        foreach($this->providers as $provider)
57             $return = array_merge($return , $this->providerInstances[$provider]->GetFolderList());
58
59
60        return $return;
61    }
62
63    /**
64     * Returns an actual SyncFolder object with all the properties set. Folders
65     * are pretty simple, having only a type, a name, a parent and a server ID.
66     *
67     * @param string        $id           id of the folder
68     *
69     * @access public
70     * @return object   SyncFolder with information
71     */
72    public function GetFolder($id)
73    {
74       return $this->getProvider($id)->GetFolder($id);
75    }
76
77    /**
78     * Returns folder stats. An associative array with properties is expected.
79     *
80     * @param string        $id             id of the folder
81     *
82     * @access public
83     * @return array
84     *          Associative array(
85     *              string  "id"            The server ID that will be used to identify the folder. It must be unique, and not too long
86     *                                      How long exactly is not known, but try keeping it under 20 chars or so. It must be a string.
87     *              string  "parent"        The server ID of the parent of the folder. Same restrictions as 'id' apply.
88     *              long    "mod"           This is the modification signature. It is any arbitrary string which is constant as long as
89     *                                      the folder has not changed. In practice this means that 'mod' can be equal to the folder name
90     *                                      as this is the only thing that ever changes in folders. (the type is normally constant)
91     *          )
92     */
93    public function StatFolder($id)
94    {
95        return $this->getProvider($id)->StatFolder($id);
96    }
97
98    /**
99     * Creates or modifies a folder
100     *
101     * @param string        $folderid       id of the parent folder
102     * @param string        $oldid          if empty -> new folder created, else folder is to be renamed
103     * @param string        $displayname    new folder name (to be created, or to be renamed to)
104     * @param int           $type           folder type
105     *
106     * @access public
107     * @return boolean                      status
108     * @throws StatusException              could throw specific SYNC_FSSTATUS_* exceptions
109     *
110     */
111    public function ChangeFolder($folderid, $oldid, $displayname, $type)
112    {
113        return $this->getProvider($oldid ? $oldid: $folderid)->ChangeFolder($folderid, $oldid, $displayname, $type);
114    }
115
116    /**
117     * Deletes a folder
118     *
119     * @param string        $id
120     * @param string        $parent         is normally false
121     *
122     * @access public
123     * @return boolean                      status - false if e.g. does not exist
124     * @throws StatusException              could throw specific SYNC_FSSTATUS_* exceptions
125     */
126    public function DeleteFolder($id, $parentid)
127    {
128        return $this->getProvider($id)->DeleteFolder($id, $parentid);
129    }
130
131    /**
132     * Returns a list (array) of messages, each entry being an associative array
133     * with the same entries as StatMessage(). This method should return stable information; ie
134     * if nothing has changed, the items in the array must be exactly the same. The order of
135     * the items within the array is not important though.
136     *
137     * The $cutoffdate is a date in the past, representing the date since which items should be shown.
138     * This cutoffdate is determined by the user's setting of getting 'Last 3 days' of e-mail, etc. If
139     * the cutoffdate is ignored, the user will not be able to select their own cutoffdate, but all
140     * will work OK apart from that.
141     *
142     * @param string        $folderid       id of the parent folder
143     * @param long          $cutoffdate     timestamp in the past from which on messages should be returned
144     *
145     * @access public
146     * @return array/false                  array with messages or false if folder is not available
147     */
148    public function GetMessageList($folderid, $cutoffdate)
149    {
150        return $this->getProvider($folderid)->GetMessageList($folderid, $cutoffdate);
151    }
152
153    /**
154     * Returns the actual SyncXXX object type. The '$folderid' of parent folder can be used.
155     * Mixing item types returned is illegal and will be blocked by the engine; ie returning an Email object in a
156     * Tasks folder will not do anything. The SyncXXX objects should be filled with as much information as possible,
157     * but at least the subject, body, to, from, etc.
158     *
159     * @param string            $folderid           id of the parent folder
160     * @param string            $id                 id of the message
161     * @param ContentParameters $contentparameters  parameters of the requested message (truncation, mimesupport etc)
162     *
163     * @access public
164     * @return object/false                 false if the message could not be retrieved
165     */
166    public function GetMessage($folderid, $id, $contentparameters)
167    {
168        return $this->getProvider($folderid)->GetMessage($folderid, $id, $contentparameters);
169    }
170
171    /**
172     * Returns message stats, analogous to the folder stats from StatFolder().
173     *
174     * @param string        $folderid       id of the folder
175     * @param string        $id             id of the message
176     *
177     * @access public
178     * @return array or boolean if fails
179     *          Associative array(
180     *              string  "id"            Server unique identifier for the message. Again, try to keep this short (under 20 chars)
181     *              int     "flags"         simply '0' for unread, '1' for read
182     *              long    "mod"           This is the modification signature. It is any arbitrary string which is constant as long as
183     *                                      the message has not changed. As soon as this signature changes, the item is assumed to be completely
184     *                                      changed, and will be sent to the PDA as a whole. Normally you can use something like the modification
185     *                                      time for this field, which will change as soon as the contents have changed.
186     *          )
187     */
188    public function StatMessage($folderid, $id)
189    {
190        return $this->getProvider($folderid)->StatMessage($folderid, $id);
191    }
192
193    /**
194     * Called when a message has been changed on the mobile. The new message must be saved to disk.
195     * The return value must be whatever would be returned from StatMessage() after the message has been saved.
196     * This way, the 'flags' and the 'mod' properties of the StatMessage() item may change via ChangeMessage().
197     * This method will never be called on E-mail items as it's not 'possible' to change e-mail items. It's only
198     * possible to set them as 'read' or 'unread'.
199     *
200     * @param string        $folderid       id of the folder
201     * @param string        $id             id of the message
202     * @param SyncXXX       $message        the SyncObject containing a message
203     *
204     * @access public
205     * @return array                        same return value as StatMessage()
206     * @throws StatusException              could throw specific SYNC_STATUS_* exceptions
207     */
208    public function ChangeMessage($folderid, $id, $message)
209    {
210       return $this->getProvider($folderid)->ChangeMessage($folderid, $id, $message);
211    }
212
213    /**
214     * Changes the 'read' flag of a message on disk. The $flags
215     * parameter can only be '1' (read) or '0' (unread). After a call to
216     * SetReadFlag(), GetMessageList() should return the message with the
217     * new 'flags' but should not modify the 'mod' parameter. If you do
218     * change 'mod', simply setting the message to 'read' on the mobile will trigger
219     * a full resync of the item from the server.
220     *
221     * @param string        $folderid       id of the folder
222     * @param string        $id             id of the message
223     * @param int           $flags          read flag of the message
224     *
225     * @access public
226     * @return boolean                      status of the operation
227     * @throws StatusException              could throw specific SYNC_STATUS_* exceptions
228     */
229    public function SetReadFlag($folderid, $id, $flags)
230    {
231        return $this->getProvider($folderid)->SetReadFlag($folderid,$id, $flags);
232    }
233
234    /**
235     * Called when the user has requested to delete (really delete) a message. Usually
236     * this means just unlinking the file its in or somesuch. After this call has succeeded, a call to
237     * GetMessageList() should no longer list the message. If it does, the message will be re-sent to the mobile
238     * as it will be seen as a 'new' item. This means that if this method is not implemented, it's possible to
239     * delete messages on the PDA, but as soon as a sync is done, the item will be resynched to the mobile
240     *
241     * @param string        $folderid       id of the folder
242     * @param string        $id             id of the message
243     *
244     * @access public
245     * @return boolean                      status of the operation
246     * @throws StatusException              could throw specific SYNC_STATUS_* exceptions
247     */
248    public function DeleteMessage($folderid, $id)
249    {
250        return $this->getProvider($folderid)->DeleteMessage($folderid, $id);
251    }
252
253    /**
254     * Called when the user moves an item on the PDA from one folder to another. Whatever is needed
255     * to move the message on disk has to be done here. After this call, StatMessage() and GetMessageList()
256     * should show the items to have a new parent. This means that it will disappear from GetMessageList()
257     * of the sourcefolder and the destination folder will show the new message
258     *
259     * @param string        $folderid       id of the source folder
260     * @param string        $id             id of the message
261     * @param string        $newfolderid    id of the destination folder
262     *
263     * @access public
264     * @return boolean                      status of the operation
265     * @throws StatusException              could throw specific SYNC_MOVEITEMSSTATUS_* exceptions
266     */
267    public function MoveMessage($folderid, $id, $newfolderid)
268    {
269        return $this->getProvider($folderid)->MoveMessage($folderid, $id , $newfolderid);
270    }
271
272    /**
273     * Authenticates the user
274     *
275     * @param string        $username
276     * @param string        $domain
277     * @param string        $password
278     *
279     * @access public
280     * @return boolean
281     * @throws FatalException   e.g. some required libraries are unavailable
282     */
283    public function Logon($username, $domain, $password)
284    {
285        ZLog::Write(LOGLEVEL_DEBUG, sprintf("ExpressoBackend->Logon(): Trying to authenticate user '%s'..", $username));
286
287        foreach($this->providers as $provider)
288        {
289            if( !$this->providerInstances[$provider]->Logon($username, $domain, $password) )
290            {
291                ZLog::Write(LOGLEVEL_ERROR, 'ExpressoBackend->Logon(): login failed provide :'.$provider);
292                return false;
293            }
294        }
295
296        return true;
297    }
298
299    /**
300     * Logs off
301     * non critical operations closing the session should be done here
302     *
303     * @access public
304     * @return boolean
305     */
306    public function Logoff()
307    {
308        foreach($this->providers as $provider)
309           $this->providerInstances[$provider]->Logoff();
310
311        return true;
312    }
313
314    /**
315     * Sends an e-mail
316     * This messages needs to be saved into the 'sent items' folder
317     *
318     * Basically two things can be done
319     *      1) Send the message to an SMTP server as-is
320     *      2) Parse the message, and send it some other way
321     *
322     * @param SyncSendMail        $sm         SyncSendMail object
323     *
324     * @access public
325     * @return boolean
326     * @throws StatusException
327     */
328    public function SendMail($sm)
329    {
330        return $this->providerInstances[$this->sendMailProvider]->SendMail($sm);
331    }
332
333    /**
334     * Returns the waste basket
335     *
336     * The waste basked is used when deleting items; if this function returns a valid folder ID,
337     * then all deletes are handled as moves and are sent to the backend as a move.
338     * If it returns FALSE, then deletes are handled as real deletes
339     *
340     * @access public
341     * @return string
342     */
343    public function GetWasteBasket()
344    {
345      return $this->providerInstances['Imap']->GetWasteBasket();
346    }
347
348    /**
349     * Returns the content of the named attachment as stream. The passed attachment identifier is
350     * the exact string that is returned in the 'AttName' property of an SyncAttachment.
351     * Any information necessary to locate the attachment must be encoded in that 'attname' property.
352     * Data is written directly - 'print $data;'
353     *
354     * @param string        $attname
355     *
356     * @access public
357     * @return SyncItemOperationsAttachment
358     * @throws StatusException
359     */
360    public function GetAttachmentData($attname)
361    {
362        list($folderid, $id, $part) = explode(":", $attname);
363        return $this->getProvider($folderid)->GetAttachmentData($attname);
364    }
365
366}
Note: See TracBrowser for help on using the repository browser.