Merge branch 'master' of github.com:DAViCal/davical into github
[davical.git] / inc / caldav-PUT-vcalendar.php
blob65251ab7fcb0fdc1db858d4f7d92a8ee471e787e
1 <?php
2 /**
3 * CalDAV Server - handle PUT method
5 * @package davical
6 * @subpackage caldav
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 include_once('caldav-PUT-functions.php');
17 $vcalendar = new vCalendar( $request->raw_post );
18 $uid = $vcalendar->GetUID();
19 if ( empty($uid) ) {
20 $uid = uuid();
21 $vcalendar->SetUID($uid);
24 if ( $add_member ) {
25 $request->path = $request->dav_name() . $uid . '.ics';
26 $dav_resource = new DAVResource($request->path);
27 if ( $dav_resource->Exists() ) {
28 $uid = uuid();
29 $vcalendar->SetUID($uid);
30 $request->path = $request->dav_name() . $uid . '.ics';
31 $dav_resource = new DAVResource($request->path);
33 if ( $dav_resource->Exists() ) throw new Exception("Failed to generate unique segment name for add-member!");
36 else {
37 $dav_resource = new DAVResource($request->path);
39 if ( ! $dav_resource->HavePrivilegeTo('DAV::write-content') ) {
40 $request->DoResponse(403,'No write permission');
43 if ( ! $dav_resource->Exists() && ! $dav_resource->HavePrivilegeTo('DAV::bind') ) {
44 $request->DoResponse(403,'No bind permission.');
47 if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['put']) && $c->dbg['put'])) ) {
48 $fh = fopen('/var/log/davical/PUT.debug','w');
49 if ( $fh ) {
50 fwrite($fh,$request->raw_post);
51 fclose($fh);
55 controlRequestContainer( $dav_resource->GetProperty('username'), $dav_resource->GetProperty('user_no'), $dav_resource->bound_from(), true);
57 $lock_opener = $request->FailIfLocked();
60 if ( $dav_resource->IsCollection() ) {
61 if ( $dav_resource->IsPrincipal() || $dav_resource->IsBinding() || !isset($c->readonly_webdav_collections) || $c->readonly_webdav_collections == true ) {
62 $request->DoResponse( 405 ); // Method not allowed
63 return;
66 $appending = (isset($_GET['mode']) && $_GET['mode'] == 'append' );
68 /**
69 * CalDAV does not define the result of a PUT on a collection. We treat that
70 * as an import. The code is in caldav-PUT-functions.php
72 import_collection($request->raw_post,$request->user_no,$request->path,true, $appending);
73 $request->DoResponse( 200 );
74 return;
77 $etag = md5($request->raw_post);
79 $request->CheckEtagMatch( $dav_resource->Exists(), $dav_resource->unique_tag() );
81 $put_action_type = ($dav_resource->Exists() ? 'UPDATE' : 'INSERT');
82 $collection = $dav_resource->GetParentContainer();
84 write_resource( $dav_resource, $request->raw_post, $collection, $session->user_no, $etag,
85 $put_action_type, true, true );
87 if ( isset($etag) ) header(sprintf('ETag: "%s"', $etag) );
89 $request->DoResponse( ($dav_resource->Exists() ? 204 : 201) );