DAViCal
caldav-MOVE.php
1<?php
11dbg_error_log("MOVE", "method handler");
12
13require_once('DAVResource.php');
14
15$request->NeedPrivilege('DAV::unbind');
16
17if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['move']) && $c->dbg['move'])) ) {
18 $fh = fopen('/var/log/davical/MOVE.debug','w');
19 if ( $fh ) {
20 fwrite($fh,$request->raw_post);
21 fclose($fh);
22 }
23}
24
25$lock_opener = $request->FailIfLocked();
26
27$dest = new DAVResource($request->destination);
28
29if ( $dest->dav_name() == '/' || $dest->IsPrincipal() ) {
30 $dest->NeedPrivilege('DAV::bind');
31}
32
33if ( ! $dest->ContainerExists() ) {
34 $request->DoResponse( 409, translate('Destination collection does not exist') );
35}
36
37if ( ! $request->overwrite && $dest->Exists() ) {
38 $request->DoResponse( 412, translate('Not overwriting existing destination resource') );
39}
40
41if ( isset($request->etag_none_match) && $request->etag_none_match != '*' ) {
42 $request->DoResponse( 412 );
43}
44
45$src = new DAVResource($request->path);
46if ( ! $src->Exists() ) {
47 $request->DoResponse( 412, translate('Source resource does not exist.') );
48}
49
50if ( $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.') );
57 };
58}
59else {
60 $request->CheckEtagMatch( $src->Exists(), $src->unique_tag() );
61}
62
63$src->NeedPrivilege('DAV::unbind');
64$dest->NeedPrivilege('DAV::write-content');
65if ( ! $dest->Exists() ) $dest->NeedPrivilege('DAV::bind');
66
67
68function rollback( $response_code = 412 ) {
69 global $request;
70 $qry = new AwlQuery('ROLLBACK');
71 $qry->Exec('move'); // Just in case
72 $request->DoResponse( $response_code );
73 // And we don't return from that.
74}
75
76
77$qry = new AwlQuery('BEGIN');
78if ( !$qry->Exec('move') ) rollback(500);
79
80$src_name = $src->dav_name();
81$dst_name = ($dest->IsBinding() ? $dest->bound_from() : $dest->dav_name());
82$src_collection = $src->GetProperty('collection_id');
83$dst_collection = $dest->GetProperty('collection_id');
84$src_user_no = $src->GetProperty('user_no');
85$dst_user_no = $dest->GetProperty('user_no');
86
87$cache = getCacheInstance();
88$cachekeys = array();
89
90if ( $src->IsCollection() ) {
91 $cachekeys[] = ($src->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$src->parent_path();
92 $cachekeys[] = ($src->IsPrincipal() == 'principal' ? 'principal' : 'collection').'-'.$src->dav_name();
93 $cachekeys[] = ($src->IsPrincipal() ? 'principal' : 'collection').'-'.$dest->dav_name();
94 if ( $dest->Exists() ) {
95 $qry = new AwlQuery( 'DELETE FROM collection WHERE dav_name = :dst_name', array( ':dst_name' => $dst_name ) );
96 if ( !$qry->Exec('move') ) rollback(500);
97 }
99 $sql = 'UPDATE collection SET dav_name = :dst_name ';
100 $params = array(':dst_name' => $dst_name);
101 if ( $src_user_no != $dst_user_no ) {
102 $sql .= ', user_no = :dst_user_no ';
103 $params[':dst_user_no'] = $dst_user_no;
104 }
105 if ( $src->parent_path() != $dest->parent_path() ) {
106 $sql .= ', parent_container=:parent ';
107 $params[':parent'] = $dest->parent_path();
108 $cachekeys[] = ($dest->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$dest->parent_path();
109 }
110 $sql .= 'WHERE collection_id = :src_collection';
111 $params[':src_collection'] = $src_collection;
112 $qry = new AwlQuery( $sql, $params );
113 if ( !$qry->Exec('move') ) rollback(500);
114}
115else {
116 if ( $dest->Exists() ) {
117 $qry = new AwlQuery( 'DELETE FROM caldav_data WHERE dav_name = :dst_name', array( ':dst_name' => $dst_name) );
118 if ( !$qry->Exec('move') ) rollback(500);
119 }
120 $cachekeys[] = ($src->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$src->parent_path();
121
122 $sql = 'UPDATE caldav_data SET dav_name = :dst_name';
123 $params = array( ':dst_name' => $dst_name );
124 if ( $src_user_no != $dst_user_no ) {
125 $sql .= ', user_no = :dst_user_no';
126 $params[':dst_user_no'] = $dst_user_no;
127 }
128 if ( $src_collection != $dst_collection ) {
129 $sql .= ', collection_id = :dst_collection';
130 $params[':dst_collection'] = $dst_collection;
131 $cachekeys[] = ($dest->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$dest->parent_path();
132 }
133 $sql .=' WHERE dav_name = :src_name';
134 $params[':src_name'] = $src_name;
135 $qry = new AwlQuery( $sql, $params );
136 if ( !$qry->Exec('move') ) rollback(500);
137
138 $qry = new AwlQuery( 'SELECT write_sync_change( :src_collection, 404, :src_name );', array(
139 ':src_name' => $src_name,
140 ':src_collection' => $src_collection
141 ) );
142 if ( !$qry->Exec('move') ) rollback(500);
143 if ( function_exists('log_caldav_action') ) {
144 log_caldav_action( 'DELETE', $src->GetProperty('uid'), $src_user_no, $src_collection, $src_name );
145 }
146
147 $qry = new AwlQuery( 'SELECT write_sync_change( :dst_collection, :sync_type, :dst_name );', array(
148 ':dst_name' => $dst_name,
149 ':dst_collection' => $dst_collection,
150 ':sync_type' => ( $dest->Exists() ? 200 : 201 )
151 ) );
152 if ( !$qry->Exec('move') ) rollback(500);
153 if ( function_exists('log_caldav_action') ) {
154 log_caldav_action( ( $dest->Exists() ? 'UPDATE' : 'INSERT' ), $src->GetProperty('uid'), $dst_user_no, $dst_collection, $dst_name );
155 }
156
157}
158
159$qry = new AwlQuery('COMMIT');
160if ( !$qry->Exec('move') ) rollback(500);
161
162// We need to delete from the cache *after* we commit the transaction :-)
163foreach( $cachekeys AS $cache_ns ) $cache->delete( $cache_ns, null );
164
165$request->DoResponse( 200 );