We need the DAVPrincipal module here in some situations.
[davical.git] / inc / caldav-MKCOL.php
blob3ae1dc48a7f04595fd31b77df12660f8166cd0f3
1 <?php
2 /**
3 * CalDAV Server - handle MKCOL and MKCALENDAR method
5 * @package davical
6 * @subpackage caldav
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' ));
46 $failure_code = null;
48 $failure = array();
49 $dav_properties = array();
51 if ( isset($request->xml_tags) ) {
52 /**
53 * The MKCOL request may contain XML to set some DAV properties
55 $position = 0;
56 $xmltree = BuildXMLTree( $request->xml_tags, $position);
57 if ( $xmltree->GetTag() == 'DAV::mkcol' ) $request_type = 'extended-mkcol';
59 if ( $xmltree->GetTag() != '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->GetTag()) );
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->GetTag();
68 $content = $setting->RenderContent();
70 dbg_error_log( 'MKCOL', 'Processing tag "%s"', $tag);
72 switch( $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') )
84 ));
86 else {
87 $resourcetypes = $setting->GetPath('DAV::resourcetype/*');
88 $resourcetypes = str_replace( "\n", "", implode('',$resourcetypes));
89 $success[$tag] = 1;
91 break;
93 case 'DAV::displayname':
94 $displayname = $content;
95 /**
96 * @todo This is definitely a bug in SOHO Organizer and we probably should respond
97 * with an error, rather than silently doing what they *seem* to want us to do.
99 if ( preg_match( '/^SOHO.Organizer.6\./', $_SERVER['HTTP_USER_AGENT'] ) ) {
100 dbg_error_log( 'MKCOL', 'Displayname is "/" to "%s"', $request->path);
101 $parent_container = $request->path;
102 $request->path .= $content . '/';
104 $success[$tag] = 1;
105 break;
107 case 'urn:ietf:params:xml:ns:caldav:supported-calendar-component-set':
108 /** We allow this to be written as a dead property */
109 $dav_properties[$tag] = $content;
110 $success[$tag] = 1;
111 break;
113 case 'urn:ietf:params:xml:ns:caldav:supported-calendar-data': /** Ignored, since we will support iCalendar 2.0 */
114 case 'urn:ietf:params:xml:ns:caldav:calendar-data': /** Ignored, since we will support iCalendar 2.0 */
115 case 'urn:ietf:params:xml:ns:caldav:max-resource-size': /** Ignored, since we will support arbitrary size */
116 case 'urn:ietf:params:xml:ns:caldav:min-date-time': /** Ignored, since we will support arbitrary time */
117 case 'urn:ietf:params:xml:ns:caldav:max-date-time': /** Ignored, since we will support arbitrary time */
118 case 'urn:ietf:params:xml:ns:caldav:max-instances': /** Ignored, since we will support arbitrary instances */
119 $success[$tag] = 1;
120 break;
123 * The following properties are read-only, so they will cause the request to fail
125 case 'DAV::getetag':
126 case 'DAV::getcontentlength':
127 case 'DAV::getcontenttype':
128 case 'DAV::getlastmodified':
129 case 'DAV::creationdate':
130 case 'DAV::lockdiscovery':
131 case 'DAV::supportedlock':
132 $failure['set-'.$tag] = new XMLElement( 'propstat', array(
133 new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
134 new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
135 new XMLElement('responsedescription', translate('Property is read-only') )
137 if ( isset($failure_code) && $failure_code != 409 ) $failure_code = 207;
138 else if ( !isset($failure_code) ) $failure_code = 409;
139 break;
142 * If we don't have any special processing for the property, we just store it verbatim (which will be an XML fragment).
144 default:
145 $dav_properties[$tag] = $content;
146 $success[$tag] = 1;
147 break;
152 * If we have encountered any instances of failure, the whole damn thing fails.
154 if ( count($failure) > 0 ) {
155 $props = array();
156 $status = array();
157 foreach( $success AS $tag => $v ) {
158 // Unfortunately although these succeeded, we failed overall, so they didn't happen...
159 $props[] = new XMLElement($reply->Tag($tag));
162 $status[] = new XMLElement( 'propstat', array(
163 new XMLElement('prop', $props),
164 new XMLElement('status', 'HTTP/1.1 424 Failed Dependency' )
167 if ( $request_type == 'extended-mkcol' ) {
168 $request->DoResponse( $failure_code, $reply->Render('mkcol-response', array_merge( $status, $failure ), 'text/xml; charset="utf-8"' ) );
170 else {
171 array_unshift( $failure, $reply->href( ConstructURL($request->path) ) );
172 $failure[] = new XMLElement('responsedescription', translate('Some properties were not able to be set.') );
174 $request->DoResponse( 207, $reply->Render('multistatus', new XMLElement( 'response', $failure )), 'text/xml; charset="utf-8"' );
180 $sql = 'SELECT * FROM collection WHERE dav_name = :dav_name';
181 $qry = new AwlQuery( $sql, array( ':dav_name' => $request->path) );
182 if ( ! $qry->Exec('MKCOL',__LINE__,__FILE__) ) {
183 $request->DoResponse( 500, translate('Error querying database.') );
185 if ( $qry->rows() != 0 ) {
186 $request->DoResponse( 405, translate('A collection already exists at that location.') );
189 $qry = new AwlQuery();
190 $qry->Begin();
192 if ( ! $qry->QDo( 'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname,
193 is_calendar, is_addressbook, resourcetypes, created, modified )
194 VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname,
195 :is_calendar, :is_addressbook, :resourcetypes, current_timestamp, current_timestamp )',
196 array(
197 ':user_no' => $request->user_no,
198 ':parent_container' => $parent_container,
199 ':dav_name' => $request->path,
200 ':dav_etag' => md5($request->user_no. $request->path),
201 ':dav_displayname' => $displayname,
202 ':is_calendar' => ($is_calendar ? 't' : 'f'),
203 ':is_addressbook' => ($is_addressbook ? 't' : 'f'),
204 ':resourcetypes' => $resourcetypes
205 ) ) ) {
206 $request->DoResponse( 500, translate('Error writing calendar details to database.') );
208 foreach( $dav_properties AS $k => $v ) {
209 if ( ! $qry->QDo('SELECT set_dav_property( :dav_name, :user_no, :tag::text, :value::text )',
210 array( ':dav_name' => $request->path, ':user_no' => $request->user_no, ':tag' => $k, ':value' => $v) ) ) {
211 $request->DoResponse( 500, translate('Error writing calendar properties to database.') );
214 if ( !$qry->Commit() ) {
215 $request->DoResponse( 500, translate('Error writing calendar details to database.') );
217 dbg_error_log( 'MKCOL', 'New calendar "%s" created named "%s" for user "%d" in parent "%s"', $request->path, $displayname, $session->user_no, $parent_container);
218 header('Cache-Control: no-cache'); /** RFC4791 mandates this at 5.3.1 */
219 $request->DoResponse( 201, '' );
222 * @todo We could also respond to the request...
224 * <?xml version="1.0" encoding="utf-8" ?>
225 * <C:mkcalendar xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
226 * <D:set>
227 * <D:prop>
228 * <D:displayname>Lisa's Events</D:displayname>
229 * <C:calendar-description xml:lang="en">Calendar restricted to events.</C:calendar-description>
230 * <C:supported-calendar-component-set>
231 * <C:comp name="VEVENT"/>
232 * </C:supported-calendar-component-set>
233 * <C:calendar-timezone><![CDATA[BEGIN:VCALENDAR
234 * PRODID:-//Example Corp.//CalDAV Client//EN
235 * VERSION:2.0
236 * BEGIN:VTIMEZONE
237 * TZID:US-Eastern
238 * LAST-MODIFIED:19870101T000000Z
239 * BEGIN:STANDARD
240 * DTSTART:19671029T020000
241 * RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
242 * TZOFFSETFROM:-0400
243 * TZOFFSETTO:-0500
244 * TZNAME:Eastern Standard Time (US & Canada)
245 * END:STANDARD
246 * BEGIN:DAYLIGHT
247 * DTSTART:19870405T020000
248 * RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
249 * TZOFFSETFROM:-0500
250 * TZOFFSETTO:-0400
251 * TZNAME:Eastern Daylight Time (US & Canada)
252 * END:DAYLIGHT
253 * END:VTIMEZONE
254 * END:VCALENDAR
255 * ]]></C:calendar-timezone>
256 * </D:prop>
257 * </D:set>
258 * </C:mkcalendar>