3 * CalDAV Server - handle MKCOL and MKCALENDAR method
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Catalyst IT Ltd, Morphoss Ltd - http://www.morphoss.com/
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
11 dbg_error_log('MKCOL', 'method handler');
12 require_once('AwlQuery.php');
14 $request->NeedPrivilege('DAV::bind');
15 $displayname = $request->path
;
17 // Enforce trailling '/' on collection name
18 if ( ! preg_match( '#/$#', $request->path
) ) {
19 dbg_error_log( 'MKCOL', 'Add trailling "/" to "%s"', $request->path
);
20 $request->path
.= '/';
23 $parent_container = '/';
24 if ( preg_match( '#^(.*/)([^/]+)(/)?$#', $request->path
, $matches ) ) {
25 $parent_container = $matches[1];
26 $displayname = $matches[2];
29 require_once('DAVResource.php');
30 $parent = new DAVResource( $parent_container );
31 if ( $parent->IsSchedulingCollection( 'inbox' ) ) {
32 $request->PreconditionFailed(403, 'urn:ietf:params:xml:ns:caldav:no-mkcol-in-inbox' );
36 $request_type = $request->method
;
37 $is_calendar = ($request_type == 'MKCALENDAR');
38 $is_addressbook = false;
40 $resourcetypes = '<DAV::collection/>';
41 if ($is_calendar) $resourcetypes .= '<urn:ietf:params:xml:ns:caldav:calendar/>';
43 require_once('XMLDocument.php');
44 $reply = new XMLDocument(array( 'DAV:' => '', 'urn:ietf:params:xml:ns:caldav' => 'C' ));
49 $dav_properties = array();
51 if ( isset($request->xml_tags
) ) {
53 * The MKCOL request may contain XML to set some DAV properties
56 $xmltree = BuildXMLTree( $request->xml_tags
, $position);
57 if ( $xmltree->GetNSTag() == 'DAV::mkcol' ) $request_type = 'extended-mkcol';
59 if ( $xmltree->GetNSTag() != 'urn:ietf:params:xml:ns:caldav:mkcalendar' && $request_type != 'extended-mkcol' ) {
60 $request->DoResponse( 406, sprintf('The XML is not a "DAV::mkcol" or "urn:ietf:params:xml:ns:caldav:mkcalendar" document (%s)', $xmltree->GetNSTag()) );
62 $setprops = $xmltree->GetContent(); // <set>
63 $setprops = $setprops[0]->GetContent(); // <prop>
64 $setprops = $setprops[0]->GetContent(); // the array of properties.
66 foreach( $setprops AS $k => $setting ) {
67 $tag = $setting->GetNSTag();
68 $content = $setting->RenderContent(0,null,true);
70 dbg_error_log( 'MKCOL', 'Processing tag "%s"', $tag);
74 case 'DAV::resourcetype':
75 /** Any value for resourcetype other than 'calendar' is ignored */
76 dbg_error_log( 'MKCOL', 'Extended MKCOL with resourcetype specified. "%s"', $content);
77 $is_addressbook = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:carddav:addressbook'));
78 $is_calendar = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:caldav:calendar'));
79 if ( $is_addressbook && $is_calendar ) {
80 $failure['set-'.$tag] = new XMLElement( 'propstat', array(
81 new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
82 new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
83 new XMLElement('responsedescription', translate('Collections may not be both CalDAV calendars and CardDAV addressbooks at the same time') )
87 $resourcetypes = $setting->GetPath('DAV::resourcetype/*');
89 foreach( $resourcetypes AS $k => $v ) {
90 $types .= '<'.$v->GetNSTag().'/>';
92 $resourcetypes = $types;
97 case 'DAV::displayname':
98 $displayname = $content;
100 * @todo This is definitely a bug in SOHO Organizer and we probably should respond
101 * with an error, rather than silently doing what they *seem* to want us to do.
103 if ( preg_match( '/^SOHO.Organizer.6\./', $_SERVER['HTTP_USER_AGENT'] ) ) {
104 dbg_error_log( 'MKCOL', 'Displayname is "/" to "%s"', $request->path
);
105 $parent_container = $request->path
;
106 $request->path
.= $content . '/';
111 case 'urn:ietf:params:xml:ns:caldav:supported-calendar-component-set':
112 /** We allow this to be written as a dead property */
113 $dav_properties[$tag] = $content;
117 case 'urn:ietf:params:xml:ns:caldav:supported-calendar-data': /** Ignored, since we will support iCalendar 2.0 */
118 case 'urn:ietf:params:xml:ns:caldav:calendar-data': /** Ignored, since we will support iCalendar 2.0 */
119 case 'urn:ietf:params:xml:ns:caldav:max-resource-size': /** Ignored, since we will support arbitrary size */
120 case 'urn:ietf:params:xml:ns:caldav:min-date-time': /** Ignored, since we will support arbitrary time */
121 case 'urn:ietf:params:xml:ns:caldav:max-date-time': /** Ignored, since we will support arbitrary time */
122 case 'urn:ietf:params:xml:ns:caldav:max-instances': /** Ignored, since we will support arbitrary instances */
127 * The following properties are read-only, so they will cause the request to fail
130 case 'DAV::getcontentlength':
131 case 'DAV::getcontenttype':
132 case 'DAV::getlastmodified':
133 case 'DAV::creationdate':
134 case 'DAV::lockdiscovery':
135 case 'DAV::supportedlock':
136 $failure['set-'.$tag] = new XMLElement( 'propstat', array(
137 new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
138 new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
139 new XMLElement('responsedescription', translate('Property is read-only') )
141 if ( isset($failure_code) && $failure_code != 409 ) $failure_code = 207;
142 else if ( !isset($failure_code) ) $failure_code = 409;
146 * If we don't have any special processing for the property, we just store it verbatim (which will be an XML fragment).
149 $dav_properties[$tag] = $content;
156 * If we have encountered any instances of failure, the whole damn thing fails.
158 if ( count($failure) > 0 ) {
161 foreach( $success AS $tag => $v ) {
162 // Unfortunately although these succeeded, we failed overall, so they didn't happen...
163 $props[] = new XMLElement($reply->Tag($tag));
166 $status[] = new XMLElement( 'propstat', array(
167 new XMLElement('prop', $props),
168 new XMLElement('status', 'HTTP/1.1 424 Failed Dependency' )
171 if ( $request_type == 'extended-mkcol' ) {
172 $request->DoResponse( $failure_code, $reply->Render('mkcol-response', array_merge( $status, $failure ), 'text/xml; charset="utf-8"' ) );
175 array_unshift( $failure, $reply->href( ConstructURL($request->path
) ) );
176 $failure[] = new XMLElement('responsedescription', translate('Some properties were not able to be set.') );
178 $request->DoResponse( 207, $reply->Render('multistatus', new XMLElement( 'response', $failure )), 'text/xml; charset="utf-8"' );
184 $sql = 'SELECT * FROM collection WHERE dav_name = :dav_name';
185 $qry = new AwlQuery( $sql, array( ':dav_name' => $request->path
) );
186 if ( ! $qry->Exec('MKCOL',__LINE__
,__FILE__
) ) {
187 $request->DoResponse( 500, translate('Error querying database.') );
189 if ( $qry->rows() != 0 ) {
190 $request->DoResponse( 405, translate('A collection already exists at that location.') );
193 $qry = new AwlQuery();
196 if ( ! $qry->QDo( 'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname,
197 is_calendar, is_addressbook, resourcetypes, created, modified )
198 VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname,
199 :is_calendar, :is_addressbook, :resourcetypes, current_timestamp, current_timestamp )',
201 ':user_no' => $request->user_no
,
202 ':parent_container' => $parent_container,
203 ':dav_name' => $request->path
,
204 ':dav_etag' => md5($request->user_no
. $request->path
),
205 ':dav_displayname' => $displayname,
206 ':is_calendar' => ($is_calendar ?
't' : 'f'),
207 ':is_addressbook' => ($is_addressbook ?
't' : 'f'),
208 ':resourcetypes' => $resourcetypes
210 $request->DoResponse( 500, translate('Error writing calendar details to database.') );
212 foreach( $dav_properties AS $k => $v ) {
213 if ( ! $qry->QDo('SELECT set_dav_property( :dav_name, :user_no::integer, :tag::text, :value::text )',
214 array( ':dav_name' => $request->path
, ':user_no' => $request->user_no
, ':tag' => $k, ':value' => $v) ) ) {
215 $request->DoResponse( 500, translate('Error writing calendar properties to database.') );
218 if ( !$qry->Commit() ) {
219 $request->DoResponse( 500, translate('Error writing calendar details to database.') );
221 dbg_error_log( 'MKCOL', 'New calendar "%s" created named "%s" for user "%d" in parent "%s"', $request->path
, $displayname, $session->user_no
, $parent_container);
222 header('Cache-Control: no-cache'); /** RFC4791 mandates this at 5.3.1 */
223 $request->DoResponse( 201, '' );
226 * @todo We could also respond to the request...
228 * <?xml version="1.0" encoding="utf-8" ?>
229 * <C:mkcalendar xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
232 * <D:displayname>Lisa's Events</D:displayname>
233 * <C:calendar-description xml:lang="en">Calendar restricted to events.</C:calendar-description>
234 * <C:supported-calendar-component-set>
235 * <C:comp name="VEVENT"/>
236 * </C:supported-calendar-component-set>
237 * <C:calendar-timezone><![CDATA[BEGIN:VCALENDAR
238 * PRODID:-//Example Corp.//CalDAV Client//EN
242 * LAST-MODIFIED:19870101T000000Z
244 * DTSTART:19671029T020000
245 * RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
248 * TZNAME:Eastern Standard Time (US & Canada)
251 * DTSTART:19870405T020000
252 * RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
255 * TZNAME:Eastern Daylight Time (US & Canada)
259 * ]]></C:calendar-timezone>