DAViCal
caldav-DELETE.php
1 <?php
11 dbg_error_log("delete", "DELETE method handler");
12 
13 require_once('DAVResource.php');
14 $dav_resource = new DAVResource($request->path);
15 $container = $dav_resource->GetParentContainer();
16 $container->NeedPrivilege('DAV::unbind');
17 
18 $lock_opener = $request->FailIfLocked();
19 
20 require_once('schedule-functions.php');
21 
22 function delete_collection( $id ) {
23  global $session, $request;
24 
25  $params = array( ':collection_id' => $id );
26  $qry = new AwlQuery('SELECT child.collection_id AS child_id FROM collection child JOIN collection parent ON (parent.dav_name = child.parent_container) WHERE parent.collection_id = :collection_id', $params );
27  if ( $qry->Exec('DELETE',__LINE__,__FILE__) && $qry->rows() > 0 ) {
28  while( $row = $qry->Fetch() ) {
29  delete_collection($row->child_id);
30  }
31  }
32 
33  if ( $qry->QDo("SELECT write_sync_change(collection_id, 404, caldav_data.dav_name) FROM caldav_data WHERE collection_id = :collection_id", $params )
34  && $qry->QDo("DELETE FROM property WHERE dav_name LIKE (SELECT dav_name FROM collection WHERE collection_id = :collection_id) || '%'", $params )
35  && $qry->QDo("DELETE FROM locks WHERE dav_name LIKE (SELECT dav_name FROM collection WHERE collection_id = :collection_id) || '%'", $params )
36  && $qry->QDo("DELETE FROM caldav_data WHERE collection_id = :collection_id", $params )
37  && $qry->QDo("DELETE FROM collection WHERE collection_id = :collection_id", $params ) ) {
38  @dbg_error_log( "DELETE", "DELETE (collection): User: %d, ETag: %s, Path: %s", $session->user_no, $request->etag_if_match, $request->path);
39  return true;
40  }
41  return false;
42 }
43 
44 
45 if ( !$dav_resource->Exists() )$request->DoResponse( 404 );
46 
47 if ( ! ( $dav_resource->resource_id() > 0 ) ) {
48  @dbg_error_log( "DELETE", ": failed: User: %d, ETag: %s, Path: %s, ResourceID: %d", $session->user_no, $request->etag_if_match, $request->path, $dav_resource->resource_id());
49  $request->DoResponse( 403 );
50 }
51 
52 $qry = new AwlQuery();
53 $qry->Begin();
54 
55 if ( $dav_resource->IsCollection() ) {
56  $cache = getCacheInstance();
57  $myLock = $cache->acquireLock('collection-'.$dav_resource->parent_path());
58  if ( $dav_resource->IsBinding() ) {
59  $params = array( ':dav_name' => $dav_resource->dav_name() );
60 
61  if ( $qry->QDo("DELETE FROM dav_binding WHERE dav_name = :dav_name", $params )
62  && $qry->Commit() ) {
63  $cache->delete( 'collection-'.$dav_resource->dav_name(), null );
64  $cache->delete( 'collection-'.$dav_resource->parent_path(), null );
65  $cache->releaseLock($myLock);
66  @dbg_error_log( "DELETE", "DELETE: Binding: %d, ETag: %s, Path: %s", $session->user_no, $request->etag_if_match, $request->path);
67  $request->DoResponse( 204 );
68  }
69  }
70  else {
71  if ( delete_collection( $dav_resource->resource_id() ) && $qry->Commit() ) {
72  // Uncache anything to do with the collection
73  $cache->delete( 'collection-'.$dav_resource->dav_name(), null );
74  $cache->delete( 'collection-'.$dav_resource->parent_path(), null );
75  $cache->releaseLock($myLock);
76  $request->DoResponse( 204 );
77  }
78  }
79  $cache->releaseLock($myLock);
80 }
81 else {
82  $request->CheckEtagMatch( $dav_resource->Exists(), $dav_resource->unique_tag() );
83 
84  // Check to see if we need to do any scheduling transactions for this one.
85  do_scheduling_for_delete($dav_resource);
86 
87  // We need to serialise access to this process just for this collection
88  $cache = getCacheInstance();
89  $myLock = $cache->acquireLock('collection-'.$dav_resource->parent_path());
90 
91  $collection_id = $dav_resource->GetProperty('collection_id');
92  $params = array( ':dav_id' => $dav_resource->resource_id() );
93  if ( $qry->QDo("DELETE FROM property WHERE dav_name = (SELECT dav_name FROM caldav_data WHERE dav_id = :dav_id)", $params )
94  && $qry->QDo("DELETE FROM locks WHERE dav_name = (SELECT dav_name FROM caldav_data WHERE dav_id = :dav_id)", $params )
95  && $qry->QDo("SELECT write_sync_change(collection_id, 404, caldav_data.dav_name) FROM caldav_data WHERE dav_id = :dav_id", $params )
96  && $qry->QDo("DELETE FROM caldav_data WHERE dav_id = :dav_id", $params ) ) {
97  if ( function_exists('log_caldav_action') ) {
98  log_caldav_action( 'DELETE', $dav_resource->GetProperty('uid'), $dav_resource->GetProperty('user_no'), $collection_id, $request->path );
99  }
100 
101  $qry->Commit();
102  @dbg_error_log( "DELETE", "DELETE: User: %d, ETag: %s, Path: %s", $session->user_no, $request->etag_if_match, $request->path);
103 
104  if ( function_exists('post_commit_action') ) {
105  post_commit_action( 'DELETE', $dav_resource->GetProperty('uid'), $dav_resource->GetProperty('user_no'), $collection_id, $request->path );
106  }
107 
108  $cache->delete( 'collection-'.$dav_resource->parent_path(), null );
109  $cache->releaseLock($myLock);
110  $request->DoResponse( 204 );
111  }
112  $cache->releaseLock($myLock);
113 }
114 
115 $request->DoResponse( 500 );