source: trunk/zpush/backend/expresso/expresso.php @ 7670

Revision 7670, 14.8 KB checked in by cristiano, 11 years ago (diff)

Ticket #3209 - Arrumado codificação dos eventos do calendario

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