PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/vCals/HTTP_WebDAV_Server_vCal.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 430 lines | 201 code | 75 blank | 154 comment | 40 complexity | 5a6b18b368756d72906cb30a6a8c5bbe MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. require_once 'modules/Calendar/Calendar.php';
  38. require_once 'include/HTTP_WebDAV_Server/Server.php';
  39. /**
  40. * Filesystem access using WebDAV
  41. *
  42. * @access public
  43. */
  44. class HTTP_WebDAV_Server_vCal extends HTTP_WebDAV_Server
  45. {
  46. /**
  47. * Root directory for WebDAV access
  48. *
  49. * Defaults to webserver document root (set by ServeRequest)
  50. *
  51. * @access private
  52. * @var string
  53. */
  54. var $base = "";
  55. var $vcal_focus;
  56. var $vcal_type = "";
  57. var $source = "";
  58. var $publish_key = "";
  59. function HTTP_WebDAV_Server_vCal()
  60. {
  61. $this->vcal_focus = new vCal();
  62. $this->user_focus = new User();
  63. }
  64. /**
  65. * Serve a webdav request
  66. *
  67. * @access public
  68. * @param string
  69. */
  70. function ServeRequest($base = false)
  71. {
  72. global $sugar_config,$current_language;
  73. if (!empty($sugar_config['session_dir']))
  74. {
  75. session_save_path($sugar_config['session_dir']);
  76. }
  77. session_start();
  78. // clean_incoming_data();
  79. $current_language = $sugar_config['default_language'];
  80. // special treatment for litmus compliance test
  81. // reply on its identifier header
  82. // not needed for the test itself but eases debugging
  83. /*
  84. foreach(apache_request_headers() as $key => $value) {
  85. if(stristr($key,"litmus")) {
  86. error_log("Litmus test $value");
  87. header("X-Litmus-reply: ".$value);
  88. }
  89. }
  90. */
  91. // set root directory, defaults to webserver document root if not set
  92. if ($base) {
  93. $this->base = realpath($base); // TODO throw if not a directory
  94. } else if(!$this->base) {
  95. $this->base = $_SERVER['DOCUMENT_ROOT'];
  96. }
  97. $query_arr = array();
  98. // set path
  99. if ( empty($_SERVER["PATH_INFO"]))
  100. {
  101. $this->path = "/";
  102. if(strtolower($_SERVER["REQUEST_METHOD"]) == 'get'){
  103. $query_arr = $_REQUEST;
  104. }else{
  105. parse_str($_REQUEST['parms'],$query_arr);
  106. }
  107. } else{
  108. $this->path = $this->_urldecode( $_SERVER["PATH_INFO"]);
  109. if(ini_get("magic_quotes_gpc")) {
  110. $this->path = stripslashes($this->path);
  111. }
  112. $query_str = preg_replace('/^\//','',$this->path);
  113. $query_arr = array();
  114. parse_str($query_str,$query_arr);
  115. }
  116. if ( ! empty($query_arr['type']))
  117. {
  118. $this->vcal_type = $query_arr['type'];
  119. }
  120. else {
  121. $this->vcal_type = 'vfb';
  122. }
  123. if ( ! empty($query_arr['source']))
  124. {
  125. $this->source = $query_arr['source'];
  126. }
  127. else {
  128. $this->source = 'outlook';
  129. }
  130. if ( ! empty($query_arr['key']))
  131. {
  132. $this->publish_key = $query_arr['key'];
  133. }
  134. // select user by email
  135. if ( ! empty($query_arr['email']))
  136. {
  137. // clean the string!
  138. $query_arr['email'] = clean_string($query_arr['email']);
  139. //get user info
  140. $this->user_focus->retrieve_by_email_address( $query_arr['email']);
  141. }
  142. // else select user by user_name
  143. else if ( ! empty($query_arr['user_name']))
  144. {
  145. // clean the string!
  146. $query_arr['user_name'] = clean_string($query_arr['user_name']);
  147. //get user info
  148. $arr = array('user_name'=>$query_arr['user_name']);
  149. $this->user_focus->retrieve_by_string_fields($arr);
  150. }
  151. // else select user by user id
  152. else if ( ! empty($query_arr['user_id']))
  153. {
  154. $this->user_focus->retrieve($query_arr['user_id']);
  155. }
  156. // if we haven't found a user, then return 404
  157. if ( empty($this->user_focus->id) || $this->user_focus->id == -1)
  158. {
  159. $this->http_status("404 Not Found");
  160. return;
  161. }
  162. // if(empty($this->user_focus->user_preferences))
  163. // {
  164. $this->user_focus->loadPreferences();
  165. // }
  166. // let the base class do all the work
  167. parent::ServeRequest();
  168. }
  169. /**
  170. * No authentication is needed here
  171. *
  172. * @access private
  173. * @param string HTTP Authentication type (Basic, Digest, ...)
  174. * @param string Username
  175. * @param string Password
  176. * @return bool true on successful authentication
  177. */
  178. function check_auth($type, $user, $pass)
  179. {
  180. return true;
  181. }
  182. function GET()
  183. {
  184. return true;
  185. }
  186. // {{{ http_GET()
  187. /**
  188. * GET method handler
  189. *
  190. * @param void
  191. * @returns void
  192. */
  193. function http_GET()
  194. {
  195. if ($this->vcal_type == 'vfb')
  196. {
  197. $this->http_status("200 OK");
  198. echo $this->vcal_focus->get_vcal_freebusy($this->user_focus);
  199. } else {
  200. $this->http_status("404 Not Found");
  201. }
  202. }
  203. // }}}
  204. // {{{ http_PUT()
  205. /**
  206. * PUT method handler
  207. *
  208. * @param void
  209. * @return void
  210. */
  211. function http_PUT()
  212. {
  213. $options = Array();
  214. $options["path"] = $this->path;
  215. $options["content_length"] = $_SERVER["CONTENT_LENGTH"];
  216. // get the Content-type
  217. if (isset($_SERVER["CONTENT_TYPE"])) {
  218. // for now we do not support any sort of multipart requests
  219. if (!strncmp($_SERVER["CONTENT_TYPE"], "multipart/", 10)) {
  220. $this->http_status("501 not implemented");
  221. echo "The service does not support mulipart PUT requests";
  222. return;
  223. }
  224. $options["content_type"] = $_SERVER["CONTENT_TYPE"];
  225. } else {
  226. // default content type if none given
  227. $options["content_type"] = "application/octet-stream";
  228. }
  229. /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT
  230. ignore any Content-* (e.g. Content-Range) headers that it
  231. does not understand or implement and MUST return a 501
  232. (Not Implemented) response in such cases."
  233. */
  234. foreach ($_SERVER as $key => $val) {
  235. if (strncmp($key, "HTTP_CONTENT", 11)) continue;
  236. switch ($key) {
  237. case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11
  238. // TODO support this if ext/zlib filters are available
  239. $this->http_status("501 not implemented");
  240. echo "The service does not support '$val' content encoding";
  241. return;
  242. case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12
  243. // we assume it is not critical if this one is ignored
  244. // in the actual PUT implementation ...
  245. $options["content_language"] = $val;
  246. break;
  247. case 'HTTP_CONTENT_LOCATION': // RFC 2616 14.14
  248. /* The meaning of the Content-Location header in PUT
  249. or POST requests is undefined; servers are free
  250. to ignore it in those cases. */
  251. break;
  252. case 'HTTP_CONTENT_RANGE': // RFC 2616 14.16
  253. // single byte range requests are NOT supported
  254. // the header format is also specified in RFC 2616 14.16
  255. // TODO we have to ensure that implementations support this or send 501 instead
  256. $this->http_status("400 bad request");
  257. echo "The service does only support single byte ranges";
  258. return;
  259. case 'HTTP_CONTENT_MD5': // RFC 2616 14.15
  260. // TODO: maybe we can just pretend here?
  261. $this->http_status("501 not implemented");
  262. echo "The service does not support content MD5 checksum verification";
  263. return;
  264. case 'HTTP_CONTENT_LENGTH': // RFC 2616 14.14
  265. /* The meaning of the Content-Location header in PUT
  266. or POST requests is undefined; servers are free
  267. to ignore it in those cases. */
  268. break;
  269. default:
  270. // any other unknown Content-* headers
  271. $this->http_status("501 not implemented");
  272. echo "The service does not support '$key'";
  273. return;
  274. }
  275. }
  276. // DO AUTHORIZATION for publishing Free/busy to Sugar:
  277. if ( $this->user_focus->getPreference('calendar_publish_key') &&
  278. $this->publish_key != $this->user_focus->getPreference('calendar_publish_key' ))
  279. {
  280. $this->http_status("401 not authorized");
  281. return;
  282. }
  283. // retrieve
  284. $arr = array('user_id'=>$this->user_focus->id,'type'=>'vfb','source'=>$this->source);
  285. $this->vcal_focus->retrieve_by_string_fields($arr);
  286. $isUpdate = false;
  287. if ( ! empty($this->vcal_focus->user_id ) &&
  288. $this->vcal_focus->user_id != -1 )
  289. {
  290. $isUpdate = true;
  291. }
  292. // open input stream
  293. $options["stream"] = fopen("php://input", "r");
  294. $content = '';
  295. // read in input stream
  296. while (!feof($options["stream"]))
  297. {
  298. $content .= fread($options["stream"], 4096);
  299. }
  300. // set freebusy members and save
  301. $this->vcal_focus->content = $content;
  302. $this->vcal_focus->type = 'vfb';
  303. $this->vcal_focus->source = $this->source;
  304. $focus->date_modified = null;
  305. $this->vcal_focus->user_id = $this->user_focus->id;
  306. $this->vcal_focus->save();
  307. if ( $isUpdate )
  308. {
  309. $this->http_status("204 No Content");
  310. } else {
  311. $this->http_status("201 Created");
  312. }
  313. }
  314. /**
  315. * PUT method handler
  316. *
  317. * @param array parameter passing array
  318. * @return bool true on success
  319. */
  320. function PUT(&$options)
  321. {
  322. }
  323. /**
  324. * LOCK method handler
  325. *
  326. * @param array general parameter passing array
  327. * @return bool true on success
  328. */
  329. function lock(&$options)
  330. {
  331. $options["timeout"] = time()+300; // 5min. hardcoded
  332. return true;
  333. }
  334. /**
  335. * UNLOCK method handler
  336. *
  337. * @param array general parameter passing array
  338. * @return bool true on success
  339. */
  340. function unlock(&$options)
  341. {
  342. return "200 OK";
  343. }
  344. /**
  345. * checkLock() helper
  346. *
  347. * @param string resource path to check for locks
  348. * @return bool true on success
  349. */
  350. function checkLock($path)
  351. {
  352. return false;
  353. }
  354. }
  355. ?>