3 * CalDAV Server - handle PUT method on VCARD 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 ( ! $dest->Exists() ) {
29 if ( $container->IsPrincipal() ) {
30 $request->PreconditionFailed(405,'method-not-allowed',translate('A DAViCal principal collection may only contain collections'));
32 if ( ! $container->Exists() ) {
33 $request->PreconditionFailed( 409, 'collection-must-exist',translate('The destination collection does not exist') );
35 $container->NeedPrivilege('DAV::bind');
38 if ( $dest->IsCollection() ) {
39 if ( ! isset($c->readonly_webdav_collections
) ||
$c->readonly_webdav_collections
) {
40 $request->PreconditionFailed(405,'method-not-allowed',translate('You may not PUT to a collection URL'));
42 $request->DoResponse(403,translate('PUT on a collection is only allowed for text/vcard content against an addressbook collection'));
44 $dest->NeedPrivilege('DAV::write-content');
47 if ( isset($request->etag_none_match
) && $request->etag_none_match
!= '*' && $dest->Exists() ) {
48 $request->PreconditionFailed(412,'if-none-match', translate('A resource already exists at the destination.'));
51 if ( isset($request->etag_if_match
) && $request->etag_if_match
!= $dest->unique_tag() ) {
52 $request->PreconditionFailed(412,'if-match',sprintf('Existing resource ETag of "%s" does not match "%s"', $dest->unique_tag(), $request->etag_if_match
) );
55 $collection_id = $container->GetProperty('collection_id');
57 $qry = new AwlQuery();
60 $etag = md5($request->raw_post
);
62 ':user_no' => $dest->GetProperty('user_no'),
63 ':dav_name' => $dest->bound_from(),
65 ':dav_data' => $request->raw_post
,
66 ':session_user' => $session->user_no
68 if ( $dest->Exists() ) {
69 $sql = 'UPDATE caldav_data SET caldav_data=:dav_data, dav_etag=:etag, logged_user=:session_user,
70 modified=current_timestamp, user_no=:user_no, caldav_type=\'VCARD\' WHERE dav_name=:dav_name';
72 $qry->QDo( $sql, $params );
74 $qry->QDo("SELECT dav_id FROM caldav_data WHERE dav_name = :dav_name ", array(':dav_name' => $params[':dav_name']) );
77 $sql = 'INSERT INTO caldav_data ( user_no, dav_name, dav_etag, caldav_data, caldav_type, logged_user, created, modified, collection_id )
78 VALUES( :user_no, :dav_name, :etag, :dav_data, \'VCARD\', :session_user, current_timestamp, current_timestamp, :collection_id )';
79 $params[':collection_id'] = $collection_id;
81 $qry->QDo( $sql, $params );
83 $qry->QDo("SELECT currval('dav_id_seq') AS dav_id" );
87 require_once('vcard.php');
89 $vcard = new vCard( $request->raw_post
);
90 $vcard->Write( $row->dav_id
, $dest->Exists() );
92 $qry->QDo("SELECT write_sync_change( $collection_id, $response_code, :dav_name)", array(':dav_name' => $dest->bound_from() ) );
94 if ( !$qry->Commit() ) {
96 $request->DoResponse( 500, "A database error occurred" );
99 // Uncache anything to do with the collection
100 $cache = getCacheInstance();
101 $cache->delete( 'collection-'.$container->dav_name(), null );
103 header('ETag: "'. $etag . '"' );
104 if ( $response_code == 200 ) $response_code = 204;
105 $request->DoResponse( $response_code );