DAViCal
Loading...
Searching...
No Matches
caldav-MKCOL.php
1<?php
11dbg_error_log('MKCOL', 'method handler');
12require_once('AwlQuery.php');
13
14$request->NeedPrivilege('DAV::bind');
15$displayname = $request->path;
16
17// Enforce trailling '/' on collection name
18if ( ! preg_match( '#/$#', $request->path ) ) {
19 dbg_error_log( 'MKCOL', 'Add trailling "/" to "%s"', $request->path);
20 $request->path .= '/';
21}
22
23$parent_container = '/';
24if ( preg_match( '#^(.*/)([^/]+)(/)?$#', $request->path, $matches ) ) {
25 $parent_container = $matches[1];
26 $displayname = $matches[2];
27}
28
29require_once('DAVResource.php');
30$parent = new DAVResource( $parent_container );
31if ( $parent->IsSchedulingCollection( 'inbox' ) ) {
32 $request->PreconditionFailed(403, 'urn:ietf:params:xml:ns:caldav:no-mkcol-in-inbox' );
33}
34
35
36$request_type = $request->method;
37$is_calendar = ($request_type == 'MKCALENDAR');
38$is_addressbook = false;
39
40$resourcetypes = '<DAV::collection/>';
41if ($is_calendar) $resourcetypes .= '<urn:ietf:params:xml:ns:caldav:calendar/>';
42
43require_once('XMLDocument.php');
44$reply = new XMLDocument(array( 'DAV:' => '', 'urn:ietf:params:xml:ns:caldav' => 'C' ));
45
46$failure_code = null;
47
48$failure = array();
49$dav_properties = array();
50
51if ( isset($request->xml_tags) ) {
55 $xmltree = BuildXMLTree( $request->xml_tags );
56 if ( $xmltree->GetNSTag() == 'DAV::mkcol' ) $request_type = 'extended-mkcol';
57
58 if ( $xmltree->GetNSTag() != 'urn:ietf:params:xml:ns:caldav:mkcalendar' && $request_type != 'extended-mkcol' ) {
59 $request->DoResponse( 406, sprintf('The XML is not a "DAV::mkcol" or "urn:ietf:params:xml:ns:caldav:mkcalendar" document (%s)', $xmltree->GetNSTag()) );
60 }
61 $setprops = $xmltree->GetContent(); // <set>
62 $setprops = $setprops[0]->GetContent(); // <prop>
63 $setprops = $setprops[0]->GetContent(); // the array of properties.
64
65 foreach( $setprops AS $k => $setting ) {
66 $tag = $setting->GetNSTag();
67 $content = $setting->RenderContent(0,null,true);
68
69 dbg_error_log( 'MKCOL', 'Processing tag "%s"', $tag);
70
71 switch( $tag ) {
72
73 case 'DAV::resourcetype':
75 dbg_error_log( 'MKCOL', 'Extended MKCOL with resourcetype specified. "%s"', $content);
76 $is_addressbook = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:carddav:addressbook'));
77 $is_calendar = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:caldav:calendar'));
78 if ( $is_addressbook && $is_calendar ) {
79 $failure['set-'.$tag] = new XMLElement( 'propstat', array(
80 new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
81 new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
82 new XMLElement('responsedescription', translate('Collections may not be both CalDAV calendars and CardDAV addressbooks at the same time') )
83 ));
84 }
85 else {
86 $resourcetypes = $setting->GetPath('DAV::resourcetype/*');
87 $types = '';
88 foreach( $resourcetypes AS $k => $v ) {
89 $types .= '<'.$v->GetNSTag().'/>';
90 }
91 $resourcetypes = $types;
92 $success[$tag] = 1;
93 }
94 break;
95
96 case 'DAV::displayname':
97 $displayname = $content;
102 if ( preg_match( '/^SOHO.Organizer.6\./', $_SERVER['HTTP_USER_AGENT'] ) ) {
103 dbg_error_log( 'MKCOL', 'Displayname is "/" to "%s"', $request->path);
104 $parent_container = $request->path;
105 $request->path .= $content . '/';
106 }
107 $success[$tag] = 1;
108 break;
109
110 case 'urn:ietf:params:xml:ns:caldav:supported-calendar-component-set':
112 $dav_properties[$tag] = $content;
113 $success[$tag] = 1;
114 break;
115
116 case 'urn:ietf:params:xml:ns:caldav:supported-calendar-data':
117 case 'urn:ietf:params:xml:ns:caldav:calendar-data':
118 case 'urn:ietf:params:xml:ns:caldav:max-resource-size':
119 case 'urn:ietf:params:xml:ns:caldav:min-date-time':
120 case 'urn:ietf:params:xml:ns:caldav:max-date-time':
121 case 'urn:ietf:params:xml:ns:caldav:max-instances':
122 $success[$tag] = 1;
123 break;
124
128 case 'DAV::getetag':
129 case 'DAV::getcontentlength':
130 case 'DAV::getcontenttype':
131 case 'DAV::getlastmodified':
132 case 'DAV::creationdate':
133 case 'DAV::lockdiscovery':
134 case 'DAV::supportedlock':
135 $failure['set-'.$tag] = new XMLElement( 'propstat', array(
136 new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
137 new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
138 new XMLElement('responsedescription', translate('Property is read-only') )
139 ));
140 if ( isset($failure_code) && $failure_code != 409 ) $failure_code = 207;
141 else if ( !isset($failure_code) ) $failure_code = 409;
142 break;
143
147 default:
148 $dav_properties[$tag] = $content;
149 $success[$tag] = 1;
150 break;
151 }
152 }
153
157 if ( count($failure) > 0 ) {
158 $props = array();
159 $status = array();
160 foreach( $success AS $tag => $v ) {
161 // Unfortunately although these succeeded, we failed overall, so they didn't happen...
162 $props[] = new XMLElement($reply->Tag($tag));
163 }
164
165 $status[] = new XMLElement( 'propstat', array(
166 new XMLElement('prop', $props),
167 new XMLElement('status', 'HTTP/1.1 424 Failed Dependency' )
168 ));
169
170 if ( $request_type == 'extended-mkcol' ) {
171 $request->DoResponse( $failure_code, $reply->Render('mkcol-response', array_merge( $status, $failure ), 'text/xml; charset="utf-8"' ) );
172 }
173 else {
174 array_unshift( $failure, $reply->href( ConstructURL($request->path) ) );
175 $failure[] = new XMLElement('responsedescription', translate('Some properties were not able to be set.') );
176
177 $request->DoResponse( 207, $reply->Render('multistatus', new XMLElement( 'response', $failure )), 'text/xml; charset="utf-8"' );
178 }
179
180 }
181}
182
183$sql = 'SELECT * FROM collection WHERE dav_name = :dav_name';
184$qry = new AwlQuery( $sql, array( ':dav_name' => $request->path) );
185if ( ! $qry->Exec('MKCOL',__LINE__,__FILE__) ) {
186 $request->DoResponse( 500, translate('Error querying database.') );
187}
188if ( $qry->rows() != 0 ) {
189 $request->DoResponse( 405, translate('A collection already exists at that location.') );
190}
191
192$qry = new AwlQuery();
193$qry->Begin();
194
195if ( ! $qry->QDo( 'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname,
196 is_calendar, is_addressbook, resourcetypes, created, modified )
197 VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname,
198 :is_calendar, :is_addressbook, :resourcetypes, current_timestamp, current_timestamp )',
199 array(
200 ':user_no' => $request->user_no,
201 ':parent_container' => $parent_container,
202 ':dav_name' => $request->path,
203 ':dav_etag' => md5($request->user_no. $request->path),
204 ':dav_displayname' => $displayname,
205 ':is_calendar' => ($is_calendar ? 't' : 'f'),
206 ':is_addressbook' => ($is_addressbook ? 't' : 'f'),
207 ':resourcetypes' => $resourcetypes
208 ) ) ) {
209 $request->DoResponse( 500, translate('Error writing calendar details to database.') );
210}
211foreach( $dav_properties AS $k => $v ) {
212 if ( ! $qry->QDo('SELECT set_dav_property( :dav_name, :user_no::integer, :tag::text, :value::text )',
213 array( ':dav_name' => $request->path, ':user_no' => $request->user_no, ':tag' => $k, ':value' => $v) ) ) {
214 $request->DoResponse( 500, translate('Error writing calendar properties to database.') );
215 }
216}
217if ( !$qry->Commit() ) {
218 $request->DoResponse( 500, translate('Error writing calendar details to database.') );
219}
220dbg_error_log( 'MKCOL', 'New calendar "%s" created named "%s" for user "%d" in parent "%s"', $request->path, $displayname, $session->user_no, $parent_container);
221header('Cache-Control: no-cache');
222$request->DoResponse( 201, '' );
223