3 * CalDAV Server - handle PUT method on unknown (arbitrary) content-types
7 * @author Andrew McMillan <andrew@morphoss.com>
8 * @copyright Morphoss Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
11 dbg_error_log("PUT", "method handler");
13 require_once('DAVResource.php');
15 if ( ! ini_get('open_basedir') && (isset($c->dbg
['ALL']) ||
(isset($c->dbg
['put']) && $c->dbg
['put'])) ) {
16 $fh = fopen('/tmp/PUT.txt','w');
18 fwrite($fh,$request->raw_post
);
23 $lock_opener = $request->FailIfLocked();
25 $dest = new DAVResource($request->path
);
27 $container = $dest->GetParentContainer();
28 if ( $container->IsCalendar() ) {
29 $request->PreconditionFailed(412,'urn:ietf:params:xml:ns:caldav:supported-calendar-data',
30 translate('Incorrect content type for calendar: ') . $request->content_type
);
32 else if ( $container->IsAddressbook() ) {
33 $request->PreconditionFailed(412,'urn:ietf:params:xml:ns:carddav:supported-address-data',
34 translate('Incorrect content type for addressbook: ') . $request->content_type
);
36 if ( ! $dest->Exists() ) {
37 if ( $container->IsPrincipal() ) {
38 $request->DoResponse(403,translate('A DAViCal principal collection may only contain collections'));
40 if ( ! $container->Exists() ) {
41 $request->DoResponse( 409, translate('Destination collection does not exist') );
43 $container->NeedPrivilege('DAV::bind');
46 if ( $dest->IsCollection() ) {
47 if ( ! isset($c->readonly_webdav_collections
) ||
$c->readonly_webdav_collections
) {
48 $request->DoResponse(403,translate('You may not PUT to a collection URL'));
50 $request->DoResponse(403,translate('PUT on a collection is only allowed for text/calendar content against a calendar collection'));
52 $dest->NeedPrivilege('DAV::write-content');
55 if ( isset($request->etag_none_match
) && $request->etag_none_match
!= '*' && $dest->Exists() ) {
56 $request->DoResponse(412);
59 if ( isset($request->etag_if_match
) && $request->etag_if_match
!= $dest->unique_tag() ) {
60 $request->DoResponse(412);
63 $collection_id = $container->GetProperty('collection_id');
65 $qry = new AwlQuery();
68 $etag = md5($request->raw_post
);
70 ':user_no' => $dest->GetProperty('user_no'),
71 ':dav_name' => $dest->bound_from(),
73 ':dav_data' => $request->raw_post
,
74 ':session_user' => $session->user_no
76 if ( $dest->Exists() ) {
77 $sql = 'UPDATE caldav_data SET caldav_data=:dav_data, dav_etag=:etag, logged_user=:session_user,
78 modified=current_timestamp WHERE user_no=:user_no AND dav_name=:dav_name';
82 $sql = 'INSERT INTO caldav_data ( user_no, dav_name, dav_etag, caldav_data, logged_user, created, modified, collection_id )
83 VALUES( :user_no, :dav_name, :etag, :dav_data, :session_user, current_timestamp, current_timestamp, :collection_id )';
84 $params[':collection_id'] = $collection_id;
87 $qry->QDo( $sql, $params );
89 $qry->QDo("SELECT write_sync_change( $collection_id, $response_code, :dav_name)", array(':dav_name' => $dest->bound_from() ) );
91 $qry = new AwlQuery('COMMIT');
92 if ( !$qry->Exec('move') ) rollback(500);
94 // Uncache anything to do with the collection
95 $cache = getCacheInstance();
96 $cache->delete( 'collection-'.$container->dav_name(), null );
98 header('ETag: "'. $etag . '"' );
99 if ( $response_code == 200 ) $response_code = 204;
100 $request->DoResponse( $response_code );