Handle HTTP date formatting for non-english locales (force English names).
[davical.git] / inc / caldav-PUT-vcard.php
blob7988cbc1e67b4144c36509299407c1e24e035efb
1 <?php
2 /**
3 * CalDAV Server - handle PUT method on VCARD content-types
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 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');
17 if ( $fh ) {
18 fwrite($fh,$request->raw_post);
19 fclose($fh);
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');
37 else {
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 $original_etag = md5($request->raw_post);
59 require_once('vcard.php');
60 $vcard = new vCard( $request->raw_post );
62 $qry = new AwlQuery();
63 $qry->Begin();
65 $uid = $vcard->GetPValue('UID');
66 if ( empty($uid) ) {
67 $uid = uuid();
68 $vcard->AddProperty('UID',$uid);
71 $last_modified = $vcard->GetPValue('REV');
72 if ( empty($last_modified) ) {
73 $last_modified = gmdate( 'Ymd\THis\Z' );
74 $vcard->AddProperty('REV',$last_modified);
76 elseif ( stripos($last_modified, 'TZ') ) {
77 // At least one of my examples has this crap.
78 $last_modified = str_replace('TZ','T000000Z',$last_modified);
79 $vcard->ClearProperties('REV');
80 $vcard->AddProperty('REV',$last_modified);
82 elseif( preg_match('{^(\d{8})(\d{6})Z?}', $last_modified, $matches) ) {
83 $last_modified = $matches[1] . 'T' . $matches[2] . 'Z';
84 $vcard->ClearProperties('REV');
85 $vcard->AddProperty('REV',$last_modified);
88 $rendered_card = $vcard->Render();
89 $etag = md5($rendered_card);
90 $params = array(
91 ':user_no' => $dest->GetProperty('user_no'),
92 ':dav_name' => $dest->bound_from(),
93 ':etag' => $etag,
94 ':dav_data' => $rendered_card,
95 ':session_user' => $session->user_no,
96 ':modified' => $last_modified
98 if ( $dest->Exists() ) {
99 $sql = 'UPDATE caldav_data SET caldav_data=:dav_data, dav_etag=:etag, logged_user=:session_user,
100 modified=:modified, user_no=:user_no, caldav_type=\'VCARD\' WHERE dav_name=:dav_name';
101 $response_code = 200;
102 $qry->QDo( $sql, $params );
104 $qry->QDo("SELECT dav_id FROM caldav_data WHERE dav_name = :dav_name ", array(':dav_name' => $params[':dav_name']) );
106 else {
107 $sql = 'INSERT INTO caldav_data ( user_no, dav_name, dav_etag, caldav_data, caldav_type, logged_user, created, modified, collection_id )
108 VALUES( :user_no, :dav_name, :etag, :dav_data, \'VCARD\', :session_user, current_timestamp, :modified, :collection_id )';
109 $params[':collection_id'] = $collection_id;
110 $response_code = 201;
111 $qry->QDo( $sql, $params );
113 $qry->QDo("SELECT currval('dav_id_seq') AS dav_id" );
115 $row = $qry->Fetch();
117 $vcard->Write( $row->dav_id, $dest->Exists() );
119 $qry->QDo("SELECT write_sync_change( $collection_id, $response_code, :dav_name)", array(':dav_name' => $dest->bound_from() ) );
121 if ( !$qry->Commit() ) {
122 $qry->Rollback();
123 $request->DoResponse( 500, "A database error occurred" );
126 // Uncache anything to do with the collection
127 $cache = getCacheInstance();
128 $cache->delete( 'collection-'.$container->dav_name(), null );
130 if ( $etag == $original_etag ) header('ETag: "'. $etag . '"' ); // Only send the ETag if we didn't change what they gave us.
131 if ( $response_code == 200 ) $response_code = 204;
132 $request->DoResponse( $response_code );