]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/iCals/HTTP_WebDAV_Server_iCal.php
Release 6.5.6
[Github/sugarcrm.git] / modules / iCals / HTTP_WebDAV_Server_iCal.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 require_once 'modules/Calendar/Calendar.php';
41 require_once 'modules/iCals/iCal.php';
42 require_once 'modules/vCals/HTTP_WebDAV_Server_vCal.php';
43
44     /**
45      * Calendar access using WebDAV
46      *
47      * @access public
48      */
49     class HTTP_WebDAV_Server_iCal extends HTTP_WebDAV_Server_vCal
50     {
51
52         var $cal_encoding = "";
53         var $cal_charset = "";
54         var $http_spec = "";
55
56         /**
57         * Constructor for the WebDAV srver
58         */
59         public function __construct()
60         {
61            $this->vcal_focus = new iCal();
62            $this->user_focus = new User();
63         }
64
65         /**
66          * Serve a webdav request
67          *
68          * @access public
69          * @param  string
70          */
71         public function ServeICalRequest($base = false)
72         {
73             if (empty($_REQUEST['type'])) {
74               $_REQUEST['type'] = 'ics';
75             }
76
77             if (empty($_REQUEST['encoding'])) {
78                 $this->cal_encoding = 'utf-8';
79             } else {
80                 $this->cal_encoding = $_REQUEST['encoding'];
81             }
82
83             if (empty($_REQUEST['cal_charset'])) {
84                 $this->cal_charset = 'utf-8';
85             } else {
86                 $this->cal_charset = $_REQUEST['cal_charset'];
87             }
88
89             if (empty($_REQUEST['http_spec'])) {
90                 $this->http_spec = '1.1';
91             } else {
92                 $this->http_spec = $_REQUEST['http_spec'];
93             }
94
95             // check the HTTP auth headers for a user
96             if (empty($_REQUEST['user_name']) && !empty($_SERVER['PHP_AUTH_USER'])) {
97               $_REQUEST['user_name'] = $_SERVER['PHP_AUTH_USER'];
98               $_REQUEST['key'] = $_SERVER['PHP_AUTH_PW'];
99             }
100
101             // if username is still empty, then return 401
102             if (empty($_REQUEST['user_name']) && empty($_REQUEST['email'])) {
103                 if ($_REQUEST['type'] == 'ics') {
104                     $this->http_status("401 not authorized");
105                     header('WWW-Authenticate: Basic realm="SugarCRM iCal"');
106                     ob_end_clean();
107                     echo 'Authorization required';
108                 } else {
109                     $this->http_status("404 Not Found");
110                     ob_end_clean();
111                 }
112                 return;
113             }
114             parent::ServeRequest();
115         }
116
117
118         /**
119         * GET method handler
120         *
121         * @param void
122         * @returns void
123         */
124         public function http_GET()
125         {
126            if ($this->vcal_type == 'vfb') {
127              $this->http_status("200 OK");
128              ob_end_clean();
129              echo $this->vcal_focus->get_vcal_freebusy($this->user_focus);
130            } else if ($this->vcal_type == 'ics') {
131              // DO HTTP AUTHORIZATION for iCal:
132              if ( empty($this->publish_key ) ||
133                 $this->publish_key != $this->user_focus->getPreference('calendar_publish_key' )) {
134                     $this->http_status("401 not authorized");
135                     header('WWW-Authenticate: Basic realm="SugarCRM iCal"');
136                     echo 'Authorization required';
137                     return;
138              }
139
140              $this->http_status("200 OK");
141              header('Content-Type: text/calendar; charset="' . $this->cal_charset . '"');
142              $result = mb_convert_encoding(html_entity_decode($this->vcal_focus->getVcalIcal($this->user_focus, $_REQUEST['num_months']), ENT_QUOTES, $this->cal_charset), $this->cal_encoding);
143                 ob_end_clean();
144              echo $result;
145            } else {
146              $this->http_status("404 Not Found");
147              ob_end_clean();
148            }
149
150         }
151
152         /**
153          * set HTTP return status and mirror it in a private header
154          *
155          * @param  string  status code and message
156          * @return void
157          */
158         public function http_status($status)
159         {
160             // simplified success case
161             if($status === true) {
162                 $status = "200 OK";
163             }
164
165             // remember status
166             $this->_http_status = $status;
167
168             // generate HTTP status response
169             header("HTTP/$this->http_spec $status");
170             header("X-WebDAV-Status: $status", true);
171         }
172
173     }
174
175 ?>