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