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

Revision 7644, 14.6 KB checked in by cristiano, 11 years ago (diff)

Ticket #3209 - Adicionado suporte de imagens nos contatos, Desabilitados Contas compartilhadas no email

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