Overview

Packages

  • awl
    • caldav-client-v2
    • RRule
  • davical
    • authentication
      • drivers
    • caldav
    • DAViCalSession
    • DAVTicket
    • external-bind
    • feed
    • HTTPAuthSession
    • iSchedule
    • iSchedule-POST
    • logging
    • metrics
    • Principal
    • propfind
    • PublicSession
    • Request
    • Resource
    • tzservice
  • None
  • PHP

Functions

  • add_failure
  • check_for_expansion
  • component_to_xml
  • controlRequestContainer
  • delete_collection
  • deliverItipCancel
  • display_status
  • do_scheduling_for_delete
  • do_scheduling_reply
  • do_scheduling_requests
  • doImipMessage
  • doItipAttendeeReply
  • doItipOrganizerCancel
  • export_iCalendar
  • GetItip
  • GetTZID
  • handle_cancel_request
  • handle_freebusy_request
  • handle_schedule_reply
  • handle_schedule_request
  • import_addressbook_collection
  • import_calendar_collection
  • import_collection
  • late_catch_fatal_error
  • logRequestHeaders
  • obfuscated_event
  • process_ace
  • processItipCancel
  • property_response
  • public_events_only
  • rollback
  • rollback_on_error
  • send_dav_header
  • simple_write_resource
  • write_alarms
  • write_attendees
  • write_resource
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3: * CalDAV Server - handle MOVE method
  4: *
  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
 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 );  /** request to move, but only if there is no source? WTF! */
 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:     * 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.
 68:     *
 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.
 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'); // Just in case
 96:   $request->DoResponse( $response_code );
 97:   // And we don't return from that.
 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:   /** @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;
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: // We need to delete from the cache *after* we commit the transaction :-)
187: foreach( $cachekeys AS $cache_ns ) $cache->delete( $cache_ns, null );
188: 
189: $request->DoResponse( 200 );
190: 
DAViCal API documentation generated by ApiGen 2.8.0