]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/vCals/HTTP_WebDAV_Server_vCal.php
Release 6.5.0
[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             return true;
218         }
219
220
221         function GET()
222         {
223             return true;
224         }
225
226         // {{{ http_GET()
227
228         /**
229         * GET method handler
230         *
231         * @param void
232         * @returns void
233         */
234         function http_GET()
235         {
236
237            if ($this->vcal_type == 'vfb')
238            {
239              $this->http_status("200 OK");
240              echo $this->vcal_focus->get_vcal_freebusy($this->user_focus); 
241            } else {
242              $this->http_status("404 Not Found");
243            }
244
245         }
246         // }}}
247
248
249         // {{{ http_PUT()
250
251         /**
252         * PUT method handler
253         *
254         * @param  void
255         * @return void
256         */
257         function http_PUT()
258         {
259             $options = Array();
260             $options["path"] = $this->path;
261             $options["content_length"] = $_SERVER["CONTENT_LENGTH"];
262
263             // get the Content-type
264             if (isset($_SERVER["CONTENT_TYPE"])) {
265                 // for now we do not support any sort of multipart requests
266                 if (!strncmp($_SERVER["CONTENT_TYPE"], "multipart/", 10)) {
267                     $this->http_status("501 not implemented");
268                     echo "The service does not support mulipart PUT requests";
269                     return;
270                 }
271                 $options["content_type"] = $_SERVER["CONTENT_TYPE"];
272             } else {
273                 // default content type if none given
274                 $options["content_type"] = "application/octet-stream";
275             }
276
277             /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT
278                ignore any Content-* (e.g. Content-Range) headers that it
279                does not understand or implement and MUST return a 501
280                (Not Implemented) response in such cases."
281             */
282             foreach ($_SERVER as $key => $val) {
283                 if (strncmp($key, "HTTP_CONTENT", 11)) continue;
284                 switch ($key) {
285                 case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11
286                     // TODO support this if ext/zlib filters are available
287                     $this->http_status("501 not implemented");
288                     echo "The service does not support '$val' content encoding";
289                     return;
290
291                 case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12
292                     // we assume it is not critical if this one is ignored
293                     // in the actual PUT implementation ...
294                     $options["content_language"] = $val;
295                     break;
296
297                 case 'HTTP_CONTENT_LOCATION': // RFC 2616 14.14
298                     /* The meaning of the Content-Location header in PUT
299                        or POST requests is undefined; servers are free
300                        to ignore it in those cases. */
301                     break;
302
303                 case 'HTTP_CONTENT_RANGE':    // RFC 2616 14.16
304                     // single byte range requests are NOT supported
305                     // the header format is also specified in RFC 2616 14.16
306                     // TODO we have to ensure that implementations support this or send 501 instead
307                         $this->http_status("400 bad request");
308                         echo "The service does only support single byte ranges";
309                         return;
310
311                 case 'HTTP_CONTENT_MD5':      // RFC 2616 14.15
312                     // TODO: maybe we can just pretend here?
313                     $this->http_status("501 not implemented");
314                     echo "The service does not support content MD5 checksum verification";
315                     return;
316
317                                 case 'HTTP_CONTENT_LENGTH': // RFC 2616 14.14
318                     /* The meaning of the Content-Location header in PUT
319                        or POST requests is undefined; servers are free
320                        to ignore it in those cases. */
321                     break;
322
323                 default:
324                     // any other unknown Content-* headers
325                     $this->http_status("501 not implemented");
326                     echo "The service does not support '$key'";
327                     return;
328                 }
329             }
330
331             // DO AUTHORIZATION for publishing Free/busy to Sugar:
332             if ( $this->user_focus->getPreference('calendar_publish_key') && 
333                 $this->publish_key != $this->user_focus->getPreference('calendar_publish_key' ))
334             {
335                     $this->http_status("401 not authorized");
336                     return;
337                 
338             }
339
340             // retrieve
341             $arr = array('user_id'=>$this->user_focus->id,'type'=>'vfb','source'=>$this->source);
342             $this->vcal_focus->retrieve_by_string_fields($arr); 
343
344             $isUpdate  = false;
345
346             if ( ! empty($this->vcal_focus->user_id ) && 
347                 $this->vcal_focus->user_id != -1 ) 
348             {
349               $isUpdate  = true;
350             }
351
352             // open input stream
353             $options["stream"] = fopen("php://input", "r");
354             $content = '';
355
356             // read in input stream
357             while (!feof($options["stream"])) 
358             {
359                $content .= fread($options["stream"], 4096);
360             }
361
362             // set freebusy members and save
363             $this->vcal_focus->content = $content;
364             $this->vcal_focus->type = 'vfb';
365             $this->vcal_focus->source = $this->source;
366             $focus->date_modified = null;
367             $this->vcal_focus->user_id = $this->user_focus->id;
368             $this->vcal_focus->save();
369
370             if ( $isUpdate )
371             {
372                $this->http_status("204 No Content");
373             } else {
374                $this->http_status("201 Created");
375             }
376         }
377
378         /**
379          * PUT method handler
380          * 
381          * @param  array  parameter passing array
382          * @return bool   true on success
383          */
384         function PUT(&$options) 
385         {
386
387         }
388
389         /**
390          * LOCK method handler
391          *
392          * @param  array  general parameter passing array
393          * @return bool   true on success
394          */
395         function lock(&$options)
396         {
397
398             $options["timeout"] = time()+300; // 5min. hardcoded
399             return true;
400         }
401
402         /**
403          * UNLOCK method handler
404          *
405          * @param  array  general parameter passing array
406          * @return bool   true on success
407          */
408         function unlock(&$options)
409         {
410
411             return "200 OK";
412         }
413
414
415         /**
416          * checkLock() helper
417          *
418          * @param  string resource path to check for locks
419          * @return bool   true on success
420          */
421         function checkLock($path)
422         {
423             return false;
424
425         }
426
427     }
428
429
430 ?>