Fix missing END:VCALENDAR.
[davical.git] / inc / caldav-POST.php
blobc8d9947bbab1e0a18841740dd9c6dac9edc7404e
1 <?php
2 /**
3 * CalDAV Server - handle PUT method
5 * @package davical
6 * @subpackage caldav
7 * @author Andrew McMillan <andrew@morphoss.com>
8 * @copyright Morphoss Ltd - http://www.morphoss.com/
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
11 dbg_error_log("POST", "method handler");
13 require_once("XMLDocument.php");
14 include_once('caldav-PUT-functions.php');
15 include_once('freebusy-functions.php');
17 if ( ! $request->AllowedTo("CALDAV:schedule-send-freebusy")
18 && ! $request->AllowedTo("CALDAV:schedule-send-invite")
19 && ! $request->AllowedTo("CALDAV:schedule-send-reply") ) {
20 // $request->DoResponse(403);
21 dbg_error_log( "WARN", ": POST: permissions not yet checked" );
24 if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || isset($c->dbg['post'])) ) {
25 $fh = fopen('/tmp/POST.txt','w');
26 if ( $fh ) {
27 fwrite($fh,$request->raw_post);
28 fclose($fh);
33 function handle_freebusy_request( $ic ) {
34 global $c, $session, $request;
36 $reply = new XMLDocument( array("DAV:" => "", "urn:ietf:params:xml:ns:caldav" => "C" ) );
37 $responses = array();
39 $fbq_start = $ic->GetPValue('DTSTART');
40 $fbq_end = $ic->GetPValue('DTEND');
41 if ( ! ( isset($fbq_start) || isset($fbq_end) ) ) {
42 $request->DoResponse( 400, 'All valid freebusy requests MUST contain a DTSTART and a DTEND' );
45 $range_start = new RepeatRuleDateTime($fbq_start);
46 $range_end = new RepeatRuleDateTime($fbq_end);
48 $attendees = $ic->GetProperties('ATTENDEE');
49 if ( preg_match( '# iCal/\d#', $_SERVER['HTTP_USER_AGENT']) ) {
50 dbg_error_log( "POST", "Non-compliant iCal request. Using X-WR-ATTENDEE property" );
51 $wr_attendees = $ic->GetProperties('X-WR-ATTENDEE');
52 foreach( $wr_attendees AS $k => $v ) {
53 $attendees[] = $v;
56 dbg_error_log( "POST", "Responding with free/busy for %d attendees", count($attendees) );
58 foreach( $attendees AS $k => $attendee ) {
59 $attendee_email = preg_replace( '/^mailto:/', '', $attendee->Value() );
60 dbg_error_log( "POST", "Calculating free/busy for %s", $attendee_email );
62 /** @todo Refactor this so we only do one query here and loop through the results */
63 $params = array( ':session_principal' => $session->principal_id, ':scan_depth' => $c->permission_scan_depth, ':email' => $attendee_email );
64 $qry = new AwlQuery('SELECT pprivs(:session_principal::int8,principal_id,:scan_depth::int) AS p, username FROM usr JOIN principal USING(user_no) WHERE lower(usr.email) = lower(:email)', $params );
65 if ( !$qry->Exec('POST',__LINE__,__FILE__) ) $request->DoResponse( 501, 'Database error');
66 if ( $qry->rows() > 1 ) {
67 // Unlikely, but if we get more than one result we'll do an exact match instead.
68 if ( !$qry->QDo('SELECT pprivs(:session_principal::int8,principal_id,:scan_depth::int) AS p, username FROM usr JOIN principal USING(user_no) WHERE usr.email = :email', $params ) )
69 $request->DoResponse( 501, 'Database error');
70 if ( $qry->rows() == 0 ) {
71 /** Sigh... Go back to the original case-insensitive match */
72 $qry->QDo('SELECT pprivs(:session_principal::int8,principal_id,:scan_depth::int) AS p, username FROM usr JOIN principal USING(user_no) WHERE lower(usr.email) = lower(:email)', $params );
76 $response = $reply->NewXMLElement("response", false, false, 'urn:ietf:params:xml:ns:caldav');
77 $reply->CalDAVElement($response, "recipient", $reply->href($attendee->Value()) );
79 if ( $qry->rows() == 0 ) {
80 $reply->CalDAVElement($response, "request-status", "3.7;Invalid Calendar User" );
81 $reply->CalDAVElement($response, "calendar-data" );
82 $responses[] = $response;
83 continue;
85 if ( ! $attendee_usr = $qry->Fetch() ) $request->DoResponse( 501, 'Database error');
86 if ( (privilege_to_bits('schedule-query-freebusy') & bindec($attendee_usr->p)) == 0 ) {
87 $reply->CalDAVElement($response, "request-status", "3.8;No authority" );
88 $reply->CalDAVElement($response, "calendar-data" );
89 $responses[] = $response;
90 continue;
92 $attendee_path_match = '^/'.$attendee_usr->username.'/';
93 $fb = get_freebusy( $attendee_path_match, $range_start, $range_end, bindec($attendee_usr->p) );
95 $fb->AddProperty( 'UID', $ic->GetPValue('UID') );
96 $fb->SetProperties( $ic->GetProperties('ORGANIZER'), 'ORGANIZER');
97 $fb->AddProperty( $attendee );
99 $vcal = new vCalendar( array('METHOD' => 'REPLY') );
100 $vcal->AddComponent( $fb );
102 $response = $reply->NewXMLElement( "response", false, false, 'urn:ietf:params:xml:ns:caldav' );
103 $reply->CalDAVElement($response, "recipient", $reply->href($attendee->Value()) );
104 $reply->CalDAVElement($response, "request-status", "2.0;Success" ); // Cargo-cult setting
105 $reply->CalDAVElement($response, "calendar-data", $vcal->Render() );
106 $responses[] = $response;
109 $response = $reply->NewXMLElement( "schedule-response", $responses, $reply->GetXmlNsArray(), 'urn:ietf:params:xml:ns:caldav' );
110 $request->XMLResponse( 200, $response );
114 function handle_cancel_request( $ic ) {
115 global $c, $session, $request;
117 $reply = new XMLDocument( array("DAV:" => "", "urn:ietf:params:xml:ns:caldav" => "C" ) );
119 $response = $reply->NewXMLElement( "response", false, false, 'urn:ietf:params:xml:ns:caldav' );
120 $reply->CalDAVElement($response, "request-status", "2.0;Success" ); // Cargo-cult setting
121 $response = $reply->NewXMLElement( "schedule-response", $response, $reply->GetXmlNsArray() );
122 $request->XMLResponse( 200, $response );
125 $ical = new vComponent( $request->raw_post );
126 $method = $ical->GetPValue('METHOD');
128 $resources = $ical->GetComponents('VTIMEZONE',false);
129 $first = $resources[0];
130 switch ( $method ) {
131 case 'REQUEST':
132 dbg_error_log('POST', 'Handling iTIP "REQUEST" method with "%s" component.', $method, $first->GetType() );
133 if ( $first->GetType() == 'VFREEBUSY' )
134 handle_freebusy_request( $first );
135 elseif ( $first->GetType() == 'VEVENT' ) {
136 handle_schedule_request( $ical );
138 else {
139 dbg_error_log('POST', 'Ignoring iTIP "REQUEST" with "%s" component.', $first->GetType() );
141 break;
142 case 'REPLY':
143 dbg_error_log('POST', 'Handling iTIP "REPLY" with "%s" component.', $first->GetType() );
144 handle_schedule_reply ( $ical );
145 break;
147 case 'CANCEL':
148 dbg_error_log("POST", "Handling iTIP 'CANCEL' method.", $method );
149 handle_cancel_request( $first );
150 break;
152 default:
153 dbg_error_log("POST", "Unhandled '%s' method in request.", $method );