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(
'/var/log/davical/MOVE.debug',
'w');
20 fwrite($fh,$request->raw_post);
25 $lock_opener = $request->FailIfLocked();
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 );
46 if ( ! $src->Exists() ) {
47 $request->DoResponse( 412, translate(
'Source resource does not exist.') );
50 if ( $src->IsCollection() ) {
51 switch( $dest->ContainerType() ) {
54 case 'schedule-inbox':
55 case 'schedule-outbox':
56 $request->DoResponse( 412, translate(
'Special collections may not contain a calendar or other special collection.') );
60 $request->CheckEtagMatch( $src->Exists(), $src->unique_tag() );
63 $src->NeedPrivilege(
'DAV::unbind');
64 $dest->NeedPrivilege(
'DAV::write-content');
65 if ( ! $dest->Exists() ) $dest->NeedPrivilege(
'DAV::bind');
68 function rollback( $response_code = 412 ) {
70 $qry =
new AwlQuery(
'ROLLBACK');
72 $request->DoResponse( $response_code );
77 $qry =
new AwlQuery(
'BEGIN');
78 if ( !$qry->Exec(
'move') ) rollback(500);
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');
87 $cache = getCacheInstance();
90 if ( $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);
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;
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();
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);
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);
120 $cachekeys[] = ($src->ContainerType() ==
'principal' ?
'principal' :
'collection').
'-'.$src->parent_path();
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;
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();
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);
138 $qry =
new AwlQuery(
'SELECT write_sync_change( :src_collection, 404, :src_name );', array(
139 ':src_name' => $src_name,
140 ':src_collection' => $src_collection
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 );
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 )
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 );
159 $qry =
new AwlQuery(
'COMMIT');
160 if ( !$qry->Exec(
'move') ) rollback(500);
163 foreach( $cachekeys AS $cache_ns ) $cache->delete( $cache_ns, null );
165 $request->DoResponse( 200 );