]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/vCals/HTTP_WebDAV_Server_vCal.php
Release 6.5.6
[Github/sugarcrm.git] / modules / vCals / HTTP_WebDAV_Server_vCal.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41
42
43 require_once 'modules/Calendar/Calendar.php';
44
45 require_once 'include/HTTP_WebDAV_Server/Server.php';
46
47
48     /**
49      * Filesystem access using WebDAV
50      *
51      * @access public
52      */
53     class HTTP_WebDAV_Server_vCal extends HTTP_WebDAV_Server
54     {
55         /**
56          * Root directory for WebDAV access
57          *
58          * Defaults to webserver document root (set by ServeRequest)
59          *
60          * @access private
61          * @var    string
62          */
63         var $base = "";
64         var $vcal_focus;
65         var $vcal_type = "";
66         var $source = "";
67         var $publish_key = "";
68
69         function HTTP_WebDAV_Server_vCal()
70         {
71            $this->vcal_focus = new vCal();
72            $this->user_focus = new User();
73         }
74
75
76         /**
77          * Serve a webdav request
78          *
79          * @access public
80          * @param  string
81          */
82         function ServeRequest($base = false)
83         {
84
85             global $sugar_config,$current_language;
86
87             if (!empty($sugar_config['session_dir']))
88             {
89                session_save_path($sugar_config['session_dir']);
90             }
91
92             session_start();
93
94             // clean_incoming_data();
95
96
97             $current_language = $sugar_config['default_language'];
98
99             // special treatment for litmus compliance test
100             // reply on its identifier header
101             // not needed for the test itself but eases debugging
102 /*
103             foreach(apache_request_headers() as $key => $value) {
104                 if(stristr($key,"litmus")) {
105                     error_log("Litmus test $value");
106                     header("X-Litmus-reply: ".$value);
107                 }
108             }
109 */
110
111             // set root directory, defaults to webserver document root if not set
112             if ($base) {
113                 $this->base = realpath($base); // TODO throw if not a directory
114             } else if(!$this->base) {
115                 $this->base = $_SERVER['DOCUMENT_ROOT'];
116             }
117
118
119             $query_arr =  array();
120              // set path
121             if ( empty($_SERVER["PATH_INFO"]))
122             {
123                                 $this->path = "/";
124                                 if(strtolower($_SERVER["REQUEST_METHOD"]) == 'get'){
125                                         $query_arr = $_REQUEST;
126                                 }else{
127                                         parse_str($_REQUEST['parms'],$query_arr);
128                                 }
129             } else{
130               $this->path = $this->_urldecode( $_SERVER["PATH_INFO"]);
131
132               if(ini_get("magic_quotes_gpc")) {
133                $this->path = stripslashes($this->path);
134               }
135
136               $query_str = preg_replace('/^\//','',$this->path);
137               $query_arr =  array();
138               parse_str($query_str,$query_arr);
139             }
140
141
142             if ( ! empty($query_arr['type']))
143             {
144               $this->vcal_type = $query_arr['type'];
145             }
146             else {
147               $this->vcal_type = 'vfb';
148             }
149
150             if ( ! empty($query_arr['source']))
151             {
152               $this->source = $query_arr['source'];
153             }
154             else {
155               $this->source = 'outlook';
156             }
157
158             if ( ! empty($query_arr['key']))
159             {
160               $this->publish_key = $query_arr['key'];
161             }
162
163             // select user by email
164             if ( ! empty($query_arr['email']))
165             {
166
167
168               // clean the string!
169               $query_arr['email'] = clean_string($query_arr['email']);
170               //get user info
171               $this->user_focus->retrieve_by_email_address( $query_arr['email']);
172
173             }
174             // else select user by user_name
175             else if ( ! empty($query_arr['user_name']))
176             {
177               // clean the string!
178               $query_arr['user_name'] = clean_string($query_arr['user_name']);
179
180               //get user info
181               $arr = array('user_name'=>$query_arr['user_name']);
182               $this->user_focus->retrieve_by_string_fields($arr);
183             }
184             // else select user by user id
185             else if ( ! empty($query_arr['user_id']))
186             {
187                 $this->user_focus->retrieve($query_arr['user_id']);
188             }
189
190             // if we haven't found a user, then return 404
191             if ( empty($this->user_focus->id) || $this->user_focus->id == -1)
192             {
193                 $this->http_status("404 Not Found");
194                 return;
195             }
196
197 //            if(empty($this->user_focus->user_preferences))
198 //            {
199                      $this->user_focus->loadPreferences();
200 //            }
201
202             // let the base class do all the work
203             parent::ServeRequest();
204         }
205
206         /**
207          * No authentication is needed here
208          *
209          * @access private
210          * @param  string  HTTP Authentication type (Basic, Digest, ...)
211          * @param  string  Username
212          * @param  string  Password
213          * @return bool    true on successful authentication
214          */
215         function check_auth($type, $user, $pass)
216         {
217             if(isset($_SESSION['authenticated_user_id'])) {
218                 // allow logged in users access to freebusy info
219                 return true;
220             }
221             if(!empty($this->publish_key) && !empty($this->user_focus) && $this->user_focus->getPreference('calendar_publish_key' ) == $this->publish_key) {
222                 return true;
223             }
224             return false;
225         }
226
227
228         function GET()
229         {
230             return true;
231         }
232
233         // {{{ http_GET()
234
235         /**
236         * GET method handler
237         *
238         * @param void
239         * @returns void
240         */
241         function http_GET()
242         {
243
244            if ($this->vcal_type == 'vfb')
245            {
246              $this->http_status("200 OK");
247              echo $this->vcal_focus->get_vcal_freebusy($this->user_focus);
248            } else {
249              $this->http_status("404 Not Found");
250            }
251
252         }
253         // }}}
254
255
256         // {{{ http_PUT()
257
258         /**
259         * PUT method handler
260         *
261         * @param  void
262         * @return void
263         */
264         function http_PUT()
265         {
266             $options = Array();
267             $options["path"] = $this->path;
268             $options["content_length"] = $_SERVER["CONTENT_LENGTH"];
269
270             // get the Content-type
271             if (isset($_SERVER["CONTENT_TYPE"])) {
272                 // for now we do not support any sort of multipart requests
273                 if (!strncmp($_SERVER["CONTENT_TYPE"], "multipart/", 10)) {
274                     $this->http_status("501 not implemented");
275                     echo "The service does not support mulipart PUT requests";
276                     return;
277                 }
278                 $options["content_type"] = $_SERVER["CONTENT_TYPE"];
279             } else {
280                 // default content type if none given
281                 $options["content_type"] = "application/octet-stream";
282             }
283
284             /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT
285                ignore any Content-* (e.g. Content-Range) headers that it
286                does not understand or implement and MUST return a 501
287                (Not Implemented) response in such cases."
288             */
289             foreach ($_SERVER as $key => $val) {
290                 if (strncmp($key, "HTTP_CONTENT", 11)) continue;
291                 switch ($key) {
292                 case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11
293                     // TODO support this if ext/zlib filters are available
294                     $this->http_status("501 not implemented");
295                     echo "The service does not support '$val' content encoding";
296                     return;
297
298                 case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12
299                     // we assume it is not critical if this one is ignored
300                     // in the actual PUT implementation ...
301                     $options["content_language"] = $val;
302                     break;
303
304                 case 'HTTP_CONTENT_LOCATION': // RFC 2616 14.14
305                     /* The meaning of the Content-Location header in PUT
306                        or POST requests is undefined; servers are free
307                        to ignore it in those cases. */
308                     break;
309
310                 case 'HTTP_CONTENT_RANGE':    // RFC 2616 14.16
311                     // single byte range requests are NOT supported
312                     // the header format is also specified in RFC 2616 14.16
313                     // TODO we have to ensure that implementations support this or send 501 instead
314                         $this->http_status("400 bad request");
315                         echo "The service does only support single byte ranges";
316                         return;
317
318                 case 'HTTP_CONTENT_MD5':      // RFC 2616 14.15
319                     // TODO: maybe we can just pretend here?
320                     $this->http_status("501 not implemented");
321                     echo "The service does not support content MD5 checksum verification";
322                     return;
323
324                                 case 'HTTP_CONTENT_LENGTH': // RFC 2616 14.14
325                     /* The meaning of the Content-Location header in PUT
326                        or POST requests is undefined; servers are free
327                        to ignore it in those cases. */
328                     break;
329
330                 default:
331                     // any other unknown Content-* headers
332                     $this->http_status("501 not implemented");
333                     echo "The service does not support '$key'";
334                     return;
335                 }
336             }
337
338             // DO AUTHORIZATION for publishing Free/busy to Sugar:
339             if ( empty($this->publish_key) ||
340                 $this->publish_key != $this->user_focus->getPreference('calendar_publish_key' ))
341             {
342                     $this->http_status("401 not authorized");
343                     return;
344
345             }
346
347             // retrieve
348             $arr = array('user_id'=>$this->user_focus->id,'type'=>'vfb','source'=>$this->source);
349             $this->vcal_focus->retrieve_by_string_fields($arr);
350
351             $isUpdate  = false;
352
353             if ( ! empty($this->vcal_focus->user_id ) &&
354                 $this->vcal_focus->user_id != -1 )
355             {
356               $isUpdate  = true;
357             }
358
359             // open input stream
360             $options["stream"] = fopen("php://input", "r");
361             $content = '';
362
363             // read in input stream
364             while (!feof($options["stream"]))
365             {
366                $content .= fread($options["stream"], 4096);
367             }
368
369             // set freebusy members and save
370             $this->vcal_focus->content = $content;
371             $this->vcal_focus->type = 'vfb';
372             $this->vcal_focus->source = $this->source;
373             $focus->date_modified = null;
374             $this->vcal_focus->user_id = $this->user_focus->id;
375             $this->vcal_focus->save();
376
377             if ( $isUpdate )
378             {
379                $this->http_status("204 No Content");
380             } else {
381                $this->http_status("201 Created");
382             }
383         }
384
385         /**
386          * PUT method handler
387          *
388          * @param  array  parameter passing array
389          * @return bool   true on success
390          */
391         function PUT(&$options)
392         {
393
394         }
395
396         /**
397          * LOCK method handler
398          *
399          * @param  array  general parameter passing array
400          * @return bool   true on success
401          */
402         function lock(&$options)
403         {
404
405             $options["timeout"] = time()+300; // 5min. hardcoded
406             return true;
407         }
408
409         /**
410          * UNLOCK method handler
411          *
412          * @param  array  general parameter passing array
413          * @return bool   true on success
414          */
415         function unlock(&$options)
416         {
417
418             return "200 OK";
419         }
420
421
422         /**
423          * checkLock() helper
424          *
425          * @param  string resource path to check for locks
426          * @return bool   true on success
427          */
428         function checkLock($path)
429         {
430             return false;
431
432         }
433
434     }
435
436
437 ?>