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.
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.
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
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
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.
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.
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 ********************************************************************************/
43 require_once 'modules/Calendar/Calendar.php';
45 require_once 'include/HTTP_WebDAV_Server/Server.php';
49 * Filesystem access using WebDAV
53 class HTTP_WebDAV_Server_vCal extends HTTP_WebDAV_Server
56 * Root directory for WebDAV access
58 * Defaults to webserver document root (set by ServeRequest)
67 var $publish_key = "";
69 function HTTP_WebDAV_Server_vCal()
71 $this->vcal_focus = new vCal();
72 $this->user_focus = new User();
77 * Serve a webdav request
82 function ServeRequest($base = false)
85 global $sugar_config,$current_language;
87 if (!empty($sugar_config['session_dir']))
89 session_save_path($sugar_config['session_dir']);
94 // clean_incoming_data();
97 $current_language = $sugar_config['default_language'];
99 // special treatment for litmus compliance test
100 // reply on its identifier header
101 // not needed for the test itself but eases debugging
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);
111 // set root directory, defaults to webserver document root if not set
113 $this->base = realpath($base); // TODO throw if not a directory
114 } else if(!$this->base) {
115 $this->base = $_SERVER['DOCUMENT_ROOT'];
119 $query_arr = array();
121 if ( empty($_SERVER["PATH_INFO"]))
124 if(strtolower($_SERVER["REQUEST_METHOD"]) == 'get'){
125 $query_arr = $_REQUEST;
127 parse_str($_REQUEST['parms'],$query_arr);
130 $this->path = $this->_urldecode( $_SERVER["PATH_INFO"]);
132 if(ini_get("magic_quotes_gpc")) {
133 $this->path = stripslashes($this->path);
136 $query_str = preg_replace('/^\//','',$this->path);
137 $query_arr = array();
138 parse_str($query_str,$query_arr);
142 if ( ! empty($query_arr['type']))
144 $this->vcal_type = $query_arr['type'];
147 $this->vcal_type = 'vfb';
150 if ( ! empty($query_arr['source']))
152 $this->source = $query_arr['source'];
155 $this->source = 'outlook';
158 if ( ! empty($query_arr['key']))
160 $this->publish_key = $query_arr['key'];
163 // select user by email
164 if ( ! empty($query_arr['email']))
169 $query_arr['email'] = clean_string($query_arr['email']);
171 $this->user_focus->retrieve_by_email_address( $query_arr['email']);
174 // else select user by user_name
175 else if ( ! empty($query_arr['user_name']))
178 $query_arr['user_name'] = clean_string($query_arr['user_name']);
181 $arr = array('user_name'=>$query_arr['user_name']);
182 $this->user_focus->retrieve_by_string_fields($arr);
184 // else select user by user id
185 else if ( ! empty($query_arr['user_id']))
187 $this->user_focus->retrieve($query_arr['user_id']);
190 // if we haven't found a user, then return 404
191 if ( empty($this->user_focus->id) || $this->user_focus->id == -1)
193 $this->http_status("404 Not Found");
197 // if(empty($this->user_focus->user_preferences))
199 $this->user_focus->loadPreferences();
202 // let the base class do all the work
203 parent::ServeRequest();
207 * No authentication is needed here
210 * @param string HTTP Authentication type (Basic, Digest, ...)
211 * @param string Username
212 * @param string Password
213 * @return bool true on successful authentication
215 function check_auth($type, $user, $pass)
237 if ($this->vcal_type == 'vfb')
239 $this->http_status("200 OK");
240 echo $this->vcal_focus->get_vcal_freebusy($this->user_focus);
242 $this->http_status("404 Not Found");
260 $options["path"] = $this->path;
261 $options["content_length"] = $_SERVER["CONTENT_LENGTH"];
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";
271 $options["content_type"] = $_SERVER["CONTENT_TYPE"];
273 // default content type if none given
274 $options["content_type"] = "application/octet-stream";
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."
282 foreach ($_SERVER as $key => $val) {
283 if (strncmp($key, "HTTP_CONTENT", 11)) continue;
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";
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;
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. */
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";
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";
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. */
324 // any other unknown Content-* headers
325 $this->http_status("501 not implemented");
326 echo "The service does not support '$key'";
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' ))
335 $this->http_status("401 not authorized");
341 $arr = array('user_id'=>$this->user_focus->id,'type'=>'vfb','source'=>$this->source);
342 $this->vcal_focus->retrieve_by_string_fields($arr);
346 if ( ! empty($this->vcal_focus->user_id ) &&
347 $this->vcal_focus->user_id != -1 )
353 $options["stream"] = fopen("php://input", "r");
356 // read in input stream
357 while (!feof($options["stream"]))
359 $content .= fread($options["stream"], 4096);
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();
372 $this->http_status("204 No Content");
374 $this->http_status("201 Created");
381 * @param array parameter passing array
382 * @return bool true on success
384 function PUT(&$options)
390 * LOCK method handler
392 * @param array general parameter passing array
393 * @return bool true on success
395 function lock(&$options)
398 $options["timeout"] = time()+300; // 5min. hardcoded
403 * UNLOCK method handler
405 * @param array general parameter passing array
406 * @return bool true on success
408 function unlock(&$options)
418 * @param string resource path to check for locks
419 * @return bool true on success
421 function checkLock($path)