3 * CalDAV Server - handle PUT method
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Catalyst .Net Ltd, Morphoss Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
11 dbg_error_log("PUT", "method handler");
13 require_once('DAVResource.php');
15 $dav_resource = new DAVResource($request->path
);
16 if ( ! $dav_resource->HavePrivilegeTo('DAV::write-content') ) {
17 $request->DoResponse(403);
20 if ( ! $dav_resource->Exists() && ! $dav_resource->HavePrivilegeTo('DAV::bind') ) {
21 $request->DoResponse(403);
24 if ( ! ini_get('open_basedir') && (isset($c->dbg
['ALL']) ||
(isset($c->dbg
['put']) && $c->dbg
['put'])) ) {
25 $fh = fopen('/tmp/PUT.txt','w');
27 fwrite($fh,$request->raw_post
);
32 include_once('caldav-PUT-functions.php');
33 controlRequestContainer( $dav_resource->GetProperty('username'), $dav_resource->GetProperty('user_no'), $dav_resource->bound_from(), true);
35 $lock_opener = $request->FailIfLocked();
38 if ( $dav_resource->IsCollection() ) {
39 if ( $dav_resource->IsPrincipal() ||
$dav_resource->IsBinding() ||
!isset($c->readonly_webdav_collections
) ||
$c->readonly_webdav_collections
== true ) {
40 $request->DoResponse( 405 ); // Method not allowed
44 $appending = (isset($_GET['mode']) && $_GET['mode'] == 'append' );
47 * CalDAV does not define the result of a PUT on a collection. We treat that
48 * as an import. The code is in caldav-PUT-functions.php
50 import_collection($request->raw_post
,$request->user_no
,$request->path
,true, $appending);
51 $request->DoResponse( 200 );
55 $etag = md5($request->raw_post
);
57 if ( ! $dav_resource->Exists() && (isset($request->etag_if_match
) && $request->etag_if_match
!= '') ) {
60 * If none of the entity tags match, or if "*" is given and no current
61 * entity exists, the server MUST NOT perform the requested method, and
62 * MUST return a 412 (Precondition Failed) response.
64 $request->PreconditionFailed(412,'if-match');
67 if ( $dav_resource->Exists() ) {
68 if ( isset($request->etag_if_match
) && $request->etag_if_match
!= '' && $request->etag_if_match
!= $dav_resource->unique_tag() ) {
71 * If none of the entity tags match, or if "*" is given and no current
72 * entity exists, the server MUST NOT perform the requested method, and
73 * MUST return a 412 (Precondition Failed) response.
75 $request->PreconditionFailed(412,'if-match',sprintf('Existing resource ETag of "%s" does not match "%s"', $dav_resource->unique_tag(), $request->etag_if_match
) );
77 else if ( isset($request->etag_none_match
) && $request->etag_none_match
!= ''
78 && ($request->etag_none_match
== $dav_resource->unique_tag() ||
$request->etag_none_match
== '*') ) {
81 * If any of the entity tags match the entity tag of the entity that
82 * would have been returned in the response to a similar GET request
83 * (without the If-None-Match header) on that resource, or if "*" is
84 * given and any current entity exists for that resource, then the
85 * server MUST NOT perform the requested method.
87 $request->PreconditionFailed(412,'if-none-match', translate( 'Existing resource matches "If-None-Match" header - not accepted.'));
91 $put_action_type = ($dav_resource->Exists() ?
'UPDATE' : 'INSERT');
92 $collection = $dav_resource->GetParentContainer();
94 write_resource( $dav_resource, $request->raw_post
, $collection, $session->user_no
, $etag,
95 $put_action_type, true, true );
97 if ( isset($etag) ) header(sprintf('ETag: "%s"', $etag) );
99 $request->DoResponse( ($dav_resource->Exists() ?
204 : 201) );