1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: dbg_error_log("MOVE", "method handler");
12:
13: require_once('DAVResource.php');
14:
15: $request->NeedPrivilege('DAV::unbind');
16:
17: if ( ! 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:
29: if ( $dest->dav_name() == '/' || $dest->IsPrincipal() ) {
30: $dest->NeedPrivilege('DAV::bind');
31: }
32:
33: if ( ! $dest->ContainerExists() ) {
34: $request->DoResponse( 409, translate('Destination collection does not exist') );
35: }
36:
37: if ( ! $request->overwrite && $dest->Exists() ) {
38: $request->DoResponse( 412, translate('Not overwriting existing destination resource') );
39: }
40:
41: if ( isset($request->etag_none_match) && $request->etag_none_match != '*' ) {
42: $request->DoResponse( 412 );
43: }
44:
45: $src = new DAVResource($request->path);
46: if ( ! $src->Exists() ) {
47: $request->DoResponse( 412, translate('Source resource does not exist.') );
48: }
49:
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.') );
57: };
58: }
59: else {
60: if ( (isset($request->etag_if_match) && $request->etag_if_match != '' )
61: || ( isset($request->etag_none_match) && $request->etag_none_match != '') ) {
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75:
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.');
79: }
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.');
82: }
83: if ( $error != '' ) $request->DoResponse( 412, $error );
84: }
85: }
86:
87: $src->NeedPrivilege('DAV::unbind');
88: $dest->NeedPrivilege('DAV::write-content');
89: if ( ! $dest->Exists() ) $dest->NeedPrivilege('DAV::bind');
90:
91:
92: function rollback( $response_code = 412 ) {
93: global $request;
94: $qry = new AwlQuery('ROLLBACK');
95: $qry->Exec('move');
96: $request->DoResponse( $response_code );
97:
98: }
99:
100:
101: $qry = new AwlQuery('BEGIN');
102: if ( !$qry->Exec('move') ) rollback(500);
103:
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');
110:
111: $cache = getCacheInstance();
112: $cachekeys = array();
113:
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);
121: }
122:
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;
128: }
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();
133: }
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);
138: }
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);
143: }
144: $cachekeys[] = ($src->ContainerType() == 'principal' ? 'principal' : 'collection').'-'.$src->parent_path();
145:
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;
151: }
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();
156: }
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);
161:
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 );
169: }
170:
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 );
179: }
180:
181: }
182:
183: $qry = new AwlQuery('COMMIT');
184: if ( !$qry->Exec('move') ) rollback(500);
185:
186:
187: foreach( $cachekeys AS $cache_ns ) $cache->delete( $cache_ns, null );
188:
189: $request->DoResponse( 200 );
190: