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

Revision 7589, 14.5 KB checked in by douglas, 11 years ago (diff)

Ticket #3209 - Integrar módulo de sincronização Z-push ao Expresso

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