Loading...
Searching...
No Matches
caldav-MKCOL.php
<?php
dbg_error_log('MKCOL', 'method handler');
require_once('AwlQuery.php');
$request->NeedPrivilege('DAV::bind');
$displayname = $request->path;
// Enforce trailling '/' on collection name
if ( ! preg_match( '#/$#', $request->path ) ) {
dbg_error_log( 'MKCOL', 'Add trailling "/" to "%s"', $request->path);
$request->path .= '/';
}
$parent_container = '/';
if ( preg_match( '#^(.*/)([^/]+)(/)?$#', $request->path, $matches ) ) {
$parent_container = $matches[1];
$displayname = $matches[2];
}
require_once('DAVResource.php');
$parent = new DAVResource( $parent_container );
if ( $parent->IsSchedulingCollection( 'inbox' ) ) {
$request->PreconditionFailed(403, 'urn:ietf:params:xml:ns:caldav:no-mkcol-in-inbox' );
}
$request_type = $request->method;
$is_calendar = ($request_type == 'MKCALENDAR');
$is_addressbook = false;
$resourcetypes = '<DAV::collection/>';
if ($is_calendar) $resourcetypes .= '<urn:ietf:params:xml:ns:caldav:calendar/>';
require_once('XMLDocument.php');
$reply = new XMLDocument(array( 'DAV:' => '', 'urn:ietf:params:xml:ns:caldav' => 'C' ));
$failure_code = null;
$failure = array();
$dav_properties = array();
if ( isset($request->xml_tags) ) {
$xmltree = BuildXMLTree( $request->xml_tags );
if ( $xmltree->GetNSTag() == 'DAV::mkcol' ) $request_type = 'extended-mkcol';
if ( $xmltree->GetNSTag() != 'urn:ietf:params:xml:ns:caldav:mkcalendar' && $request_type != 'extended-mkcol' ) {
$request->DoResponse( 406, sprintf('The XML is not a "DAV::mkcol" or "urn:ietf:params:xml:ns:caldav:mkcalendar" document (%s)', $xmltree->GetNSTag()) );
}
$setprops = $xmltree->GetContent(); // <set>
$setprops = $setprops[0]->GetContent(); // <prop>
$setprops = $setprops[0]->GetContent(); // the array of properties.
foreach( $setprops AS $k => $setting ) {
$tag = $setting->GetNSTag();
$content = $setting->RenderContent(0,null,true);
dbg_error_log( 'MKCOL', 'Processing tag "%s"', $tag);
switch( $tag ) {
case 'DAV::resourcetype':
dbg_error_log( 'MKCOL', 'Extended MKCOL with resourcetype specified. "%s"', $content);
$is_addressbook = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:carddav:addressbook'));
$is_calendar = count($setting->GetPath('DAV::resourcetype/urn:ietf:params:xml:ns:caldav:calendar'));
if ( $is_addressbook && $is_calendar ) {
$failure['set-'.$tag] = new XMLElement( 'propstat', array(
new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
new XMLElement('responsedescription', translate('Collections may not be both CalDAV calendars and CardDAV addressbooks at the same time') )
));
}
else {
$resourcetypes = $setting->GetPath('DAV::resourcetype/*');
$types = '';
foreach( $resourcetypes AS $k => $v ) {
$types .= '<'.$v->GetNSTag().'/>';
}
$resourcetypes = $types;
$success[$tag] = 1;
}
break;
case 'DAV::displayname':
$displayname = $content;
if ( preg_match( '/^SOHO.Organizer.6\./', $_SERVER['HTTP_USER_AGENT'] ) ) {
dbg_error_log( 'MKCOL', 'Displayname is "/" to "%s"', $request->path);
$parent_container = $request->path;
$request->path .= $content . '/';
}
$success[$tag] = 1;
break;
case 'urn:ietf:params:xml:ns:caldav:supported-calendar-component-set':
$dav_properties[$tag] = $content;
$success[$tag] = 1;
break;
case 'urn:ietf:params:xml:ns:caldav:supported-calendar-data':
case 'urn:ietf:params:xml:ns:caldav:calendar-data':
case 'urn:ietf:params:xml:ns:caldav:max-resource-size':
case 'urn:ietf:params:xml:ns:caldav:min-date-time':
case 'urn:ietf:params:xml:ns:caldav:max-date-time':
case 'urn:ietf:params:xml:ns:caldav:max-instances':
$success[$tag] = 1;
break;
case 'DAV::getetag':
case 'DAV::getcontentlength':
case 'DAV::getcontenttype':
case 'DAV::getlastmodified':
case 'DAV::creationdate':
case 'DAV::lockdiscovery':
case 'DAV::supportedlock':
$failure['set-'.$tag] = new XMLElement( 'propstat', array(
new XMLElement( 'prop', new XMLElement($reply->Tag($tag))),
new XMLElement( 'status', 'HTTP/1.1 409 Conflict' ),
new XMLElement('responsedescription', translate('Property is read-only') )
));
if ( isset($failure_code) && $failure_code != 409 ) $failure_code = 207;
else if ( !isset($failure_code) ) $failure_code = 409;
break;
default:
$dav_properties[$tag] = $content;
$success[$tag] = 1;
break;
}
}
if ( count($failure) > 0 ) {
$props = array();
$status = array();
foreach( $success AS $tag => $v ) {
// Unfortunately although these succeeded, we failed overall, so they didn't happen...
$props[] = new XMLElement($reply->Tag($tag));
}
$status[] = new XMLElement( 'propstat', array(
new XMLElement('prop', $props),
new XMLElement('status', 'HTTP/1.1 424 Failed Dependency' )
));
if ( $request_type == 'extended-mkcol' ) {
$request->DoResponse( $failure_code, $reply->Render('mkcol-response', array_merge( $status, $failure ), 'text/xml; charset="utf-8"' ) );
}
else {
array_unshift( $failure, $reply->href( ConstructURL($request->path) ) );
$failure[] = new XMLElement('responsedescription', translate('Some properties were not able to be set.') );
$request->DoResponse( 207, $reply->Render('multistatus', new XMLElement( 'response', $failure )), 'text/xml; charset="utf-8"' );
}
}
}
$sql = 'SELECT * FROM collection WHERE dav_name = :dav_name';
$qry = new AwlQuery( $sql, array( ':dav_name' => $request->path) );
if ( ! $qry->Exec('MKCOL',__LINE__,__FILE__) ) {
$request->DoResponse( 500, translate('Error querying database.') );
}
if ( $qry->rows() != 0 ) {
$request->DoResponse( 405, translate('A collection already exists at that location.') );
}
$qry = new AwlQuery();
$qry->Begin();
if ( ! $qry->QDo( 'INSERT INTO collection ( user_no, parent_container, dav_name, dav_etag, dav_displayname,
is_calendar, is_addressbook, resourcetypes, created, modified )
VALUES( :user_no, :parent_container, :dav_name, :dav_etag, :dav_displayname,
:is_calendar, :is_addressbook, :resourcetypes, current_timestamp, current_timestamp )',
array(
':user_no' => $request->user_no,
':parent_container' => $parent_container,
':dav_name' => $request->path,
':dav_etag' => md5($request->user_no. $request->path),
':dav_displayname' => $displayname,
':is_calendar' => ($is_calendar ? 't' : 'f'),
':is_addressbook' => ($is_addressbook ? 't' : 'f'),
':resourcetypes' => $resourcetypes
) ) ) {
$request->DoResponse( 500, translate('Error writing calendar details to database.') );
}
foreach( $dav_properties AS $k => $v ) {
if ( ! $qry->QDo('SELECT set_dav_property( :dav_name, :user_no::integer, :tag::text, :value::text )',
array( ':dav_name' => $request->path, ':user_no' => $request->user_no, ':tag' => $k, ':value' => $v) ) ) {
$request->DoResponse( 500, translate('Error writing calendar properties to database.') );
}
}
if ( !$qry->Commit() ) {
$request->DoResponse( 500, translate('Error writing calendar details to database.') );
}
dbg_error_log( 'MKCOL', 'New calendar "%s" created named "%s" for user "%d" in parent "%s"', $request->path, $displayname, $session->user_no, $parent_container);
header('Cache-Control: no-cache');
$request->DoResponse( 201, '' );
DAVResource
Definition DAVResource.php:25