document closed Debian bug
[davical.git] / inc / caldav-MOVE.php
blobad0e120eea8b5c55a539720eeb7ac29348165e38
1 <?php
2 /**
3 * CalDAV Server - handle MOVE method
5 * @package davical
6 * @subpackage caldav
7 * @author Andrew McMillan <andrew@morphoss.com>
8 * @copyright Morphoss Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
11 dbg_error_log("MOVE", "method handler");
13 require_once('DAVResource.php');
15 $request->NeedPrivilege('DAV::unbind');
17 if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['move']) && $c->dbg['move'])) ) {
18 $fh = fopen('/tmp/MOVE.txt','w');
19 if ( $fh ) {
20 fwrite($fh,$request->raw_post);
21 fclose($fh);
25 $lock_opener = $request->FailIfLocked();
27 $dest = new DAVResource($request->destination);
29 if ( $dest->dav_name() == '/' || $dest->IsPrincipal() ) {
30 $dest->NeedPrivilege('DAV::bind');
33 if ( ! $dest->ContainerExists() ) {
34 $request->DoResponse( 409, translate('Destination collection does not exist') );
37 if ( ! $request->overwrite && $dest->Exists() ) {
38 $request->DoResponse( 412, translate('Not overwriting existing destination resource') );
41 if ( isset($request->etag_none_match) && $request->etag_none_match != '*' ) {
42 $request->DoResponse( 412 ); /** request to move, but only if there is no source? WTF! */
45 $src = new DAVResource($request->path);
46 if ( ! $src->Exists() ) {
47 $request->DoResponse( 412, translate('Source resource does not exist.') );
50 if ( $src->IsCollection() ) {
51 switch( $dest->ContainerType() ) {
52 case 'calendar':
53 case 'addressbook':
54 case 'schedule-inbox':
55 case 'schedule-outbox':
56 $request->DoResponse( 412, translate('Special collections may not contain a calendar or other special collection.') );
59 else {
60 if ( (isset($request->etag_if_match) && $request->etag_if_match != '' )
61 || ( isset($request->etag_none_match) && $request->etag_none_match != '') ) {
63 /**
64 * RFC2068, 14.25:
65 * If none of the entity tags match, or if "*" is given and no current
66 * entity exists, the server MUST NOT perform the requested method, and
67 * MUST return a 412 (Precondition Failed) response.
69 * RFC2068, 14.26:
70 * If any of the entity tags match the entity tag of the entity that
71 * would have been returned in the response to a similar GET request
72 * (without the If-None-Match header) on that resource, or if "*" is
73 * given and any current entity exists for that resource, then the
74 * server MUST NOT perform the requested method.
76 $error = '';
77 if ( isset($request->etag_if_match) && $request->etag_if_match != $src->unique_tag() ) {
78 $error = translate( 'Existing resource does not match "If-Match" header - not accepted.');
80 else if ( isset($request->etag_none_match) && $request->etag_none_match != '' && $request->etag_none_match == $src->unique_tag() ) {
81 $error = translate( 'Existing resource matches "If-None-Match" header - not accepted.');
83 if ( $error != '' ) $request->DoResponse( 412, $error );
87 $src->NeedPrivilege('DAV::unbind');
88 $dest->NeedPrivilege('DAV::write-content');
89 if ( ! $dest->Exists() ) $dest->NeedPrivilege('DAV::bind');
92 function rollback( $response_code = 412 ) {
93 global $request;
94 $qry = new AwlQuery('ROLLBACK');
95 $qry->Exec('move'); // Just in case
96 $request->DoResponse( $response_code );
97 // And we don't return from that.
101 $qry = new AwlQuery('BEGIN');
102 if ( !$qry->Exec('move') ) rollback(500);
104 $src_name = $src->dav_name();
105 $dst_name = ($dest->IsBinding() ? $dest->bound_from() : $dest->dav_name());
106 $src_collection = $src->GetProperty('collection_id');
107 $dst_collection = $dest->GetProperty('collection_id');
108 $src_user_no = $src->GetProperty('user_no');
109 $dst_user_no = $dest->GetProperty('user_no');
111 $cache = getCacheInstance();
112 $cachekeys = array();
114 if ( $src->IsCollection() ) {
115 $cachekeys[] = ($src->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$src->parent_path();
116 $cachekeys[] = ($src->IsPrincipal() == 'principal' ? 'principal' : 'collection').'-'.$src->dav_name();
117 $cachekeys[] = ($src->IsPrincipal() ? 'principal' : 'collection').'-'.$dest->dav_name();
118 if ( $dest->Exists() ) {
119 $qry = new AwlQuery( 'DELETE FROM collection WHERE dav_name = :dst_name', array( ':dst_name' => $dst_name ) );
120 if ( !$qry->Exec('move') ) rollback(500);
122 /** @todo Need to confirm this will work correctly if we move this into another user's hierarchy. */
123 $sql = 'UPDATE collection SET dav_name = :dst_name ';
124 $params = array(':dst_name' => $dst_name);
125 if ( $src_user_no != $dst_user_no ) {
126 $sql .= ', user_no = :dst_user_no ';
127 $params[':dst_user_no'] = $dst_user_no;
129 if ( $src->parent_path() != $dest->parent_path() ) {
130 $sql .= ', parent_container=:parent ';
131 $params[':parent'] = $dest->parent_path();
132 $cachekeys[] = ($dest->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$dest->parent_path();
134 $sql .= 'WHERE collection_id = :src_collection';
135 $params[':src_collection'] = $src_collection;
136 $qry = new AwlQuery( $sql, $params );
137 if ( !$qry->Exec('move') ) rollback(500);
139 else {
140 if ( $dest->Exists() ) {
141 $qry = new AwlQuery( 'DELETE FROM caldav_data WHERE dav_name = :dst_name', array( ':dst_name' => $dst_name) );
142 if ( !$qry->Exec('move') ) rollback(500);
144 $cachekeys[] = ($src->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$src->parent_path();
146 $sql = 'UPDATE caldav_data SET dav_name = :dst_name';
147 $params = array( ':dst_name' => $dst_name );
148 if ( $src_user_no != $dst_user_no ) {
149 $sql .= ', user_no = :dst_user_no';
150 $params[':dst_user_no'] = $dst_user_no;
152 if ( $src_collection != $dst_collection ) {
153 $sql .= ', collection_id = :dst_collection';
154 $params[':dst_collection'] = $dst_collection;
155 $cachekeys[] = ($dest->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$dest->parent_path();
157 $sql .=' WHERE dav_name = :src_name';
158 $params[':src_name'] = $src_name;
159 $qry = new AwlQuery( $sql, $params );
160 if ( !$qry->Exec('move') ) rollback(500);
162 $qry = new AwlQuery( 'SELECT write_sync_change( :src_collection, 404, :src_name );', array(
163 ':src_name' => $src_name,
164 ':src_collection' => $src_collection
165 ) );
166 if ( !$qry->Exec('move') ) rollback(500);
167 if ( function_exists('log_caldav_action') ) {
168 log_caldav_action( 'DELETE', $src->GetProperty('uid'), $src_user_no, $src_collection, $src_name );
171 $qry = new AwlQuery( 'SELECT write_sync_change( :dst_collection, :sync_type, :dst_name );', array(
172 ':dst_name' => $dst_name,
173 ':dst_collection' => $dst_collection,
174 ':sync_type' => ( $dest->Exists() ? 200 : 201 )
175 ) );
176 if ( !$qry->Exec('move') ) rollback(500);
177 if ( function_exists('log_caldav_action') ) {
178 log_caldav_action( ( $dest->Exists() ? 'UPDATE' : 'INSERT' ), $src->GetProperty('uid'), $dst_user_no, $dst_collection, $dst_name );
183 $qry = new AwlQuery('COMMIT');
184 if ( !$qry->Exec('move') ) rollback(500);
186 // We need to delete from the cache *after* we commit the transaction :-)
187 foreach( $cachekeys AS $cache_ns ) $cache->delete( $cache_ns, null );
189 $request->DoResponse( 200 );