PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/caldav-PUT-vcard.php

https://gitlab.com/tiggerben/davical
PHP | 171 lines | 134 code | 23 blank | 14 comment | 32 complexity | c8fd20f260e3a74e32c755f5e0f26587 MD5 | raw file
  1. <?php
  2. /**
  3. * CalDAV Server - handle PUT method on VCARD content-types
  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 or later
  10. */
  11. dbg_error_log("PUT", "method handler");
  12. require_once('DAVResource.php');
  13. if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['put']) && $c->dbg['put'])) ) {
  14. $fh = fopen('/var/log/davical/PUT.debug','w');
  15. if ( $fh ) {
  16. fwrite($fh,$request->raw_post);
  17. fclose($fh);
  18. }
  19. }
  20. $lock_opener = $request->FailIfLocked();
  21. require_once('vcard.php');
  22. $vcard = new vCard( $request->raw_post );
  23. $uid = $vcard->GetPValue('UID');
  24. if ( empty($uid) ) {
  25. $uid = uuid();
  26. $vcard->AddProperty('UID',$uid);
  27. }
  28. if ( $add_member ) {
  29. $request->path = $request->dav_name() . $uid . '.vcf';
  30. $dest = new DAVResource($request->path);
  31. if ( $dest->Exists() ) {
  32. $uid = uuid();
  33. $vcard->AddProperty('UID',$uid);
  34. $request->path = $request->dav_name() . $uid . '.vcf';
  35. $dest = new DAVResource($request->path);
  36. if ( $dest->Exists() ) throw new Exception("Failed to generate unique segment name for add-member!");
  37. }
  38. }
  39. else {
  40. $dest = new DAVResource($request->path);
  41. }
  42. $container = $dest->GetParentContainer();
  43. if ( ! $dest->Exists() ) {
  44. if ( $container->IsPrincipal() ) {
  45. $request->PreconditionFailed(405,'method-not-allowed',translate('A DAViCal principal collection may only contain collections'));
  46. }
  47. if ( ! $container->Exists() ) {
  48. $request->PreconditionFailed( 409, 'collection-must-exist',translate('The destination collection does not exist') );
  49. }
  50. $container->NeedPrivilege('DAV::bind');
  51. }
  52. else {
  53. if ( $dest->IsCollection() ) {
  54. if ( ! isset($c->readonly_webdav_collections) || $c->readonly_webdav_collections ) {
  55. $request->PreconditionFailed(405,'method-not-allowed',translate('You may not PUT to a collection URL'));
  56. }
  57. $request->DoResponse(403,translate('PUT on a collection is only allowed for text/vcard content against an addressbook collection'));
  58. }
  59. $dest->NeedPrivilege('DAV::write-content');
  60. }
  61. $request->CheckEtagMatch( $dest->Exists(), $dest->unique_tag() );
  62. $user_no = $dest->GetProperty('user_no');
  63. $collection_id = $container->GetProperty('collection_id');
  64. $original_etag = md5($request->raw_post);
  65. $qry = new AwlQuery();
  66. $qry->Begin();
  67. $uid = $vcard->GetPValue('UID');
  68. if ( empty($uid) ) {
  69. $uid = uuid();
  70. $vcard->AddProperty('UID',$uid);
  71. }
  72. $last_modified = $vcard->GetPValue('REV');
  73. if ( empty($last_modified) ) {
  74. $last_modified = gmdate( 'Ymd\THis\Z' );
  75. $vcard->AddProperty('REV',$last_modified);
  76. }
  77. elseif ( stripos($last_modified, 'TZ') ) {
  78. // At least one of my examples has this crap.
  79. $last_modified = str_replace('TZ','T000000Z',$last_modified);
  80. $vcard->ClearProperties('REV');
  81. $vcard->AddProperty('REV',$last_modified);
  82. }
  83. elseif( preg_match('{^(\d{8})(\d{6})Z?}', $last_modified, $matches) ) {
  84. // 'T' missing
  85. $last_modified = $matches[1] . 'T' . $matches[2] . 'Z';
  86. $vcard->ClearProperties('REV');
  87. $vcard->AddProperty('REV',$last_modified);
  88. }
  89. elseif( preg_match('{([0-9]{4})-([0-9]{2})-([0-9]{2}T)([0-9]{2}):([0-9]{2}):([0-9]{2})Z?}', $last_modified, $matches) ){
  90. // iOS sends these, see http://tools.ietf.org/html/rfc2426#section-3.6.4
  91. $last_modified = $matches[1] . $matches[2] .$matches[3] .$matches[4] .$matches[5]. $matches[6] . 'Z';
  92. $vcard->ClearProperties('REV');
  93. $vcard->AddProperty('REV', $last_modified);
  94. }
  95. elseif( !preg_match('{^\d{8}T\d{6}Z$}', $last_modified) ) {
  96. // reset to timestamp format, see https://tools.ietf.org/html/rfc6350#section-6.7.4
  97. $last_modified = gmdate( 'Ymd\THis\Z' );
  98. $vcard->ClearProperties('REV');
  99. $vcard->AddProperty('REV',$last_modified);
  100. }
  101. $rendered_card = $vcard->Render();
  102. $etag = md5($rendered_card);
  103. $params = array(
  104. ':user_no' => $user_no,
  105. ':dav_name' => $dest->bound_from(),
  106. ':etag' => $etag,
  107. ':dav_data' => $rendered_card,
  108. ':session_user' => $session->user_no,
  109. ':modified' => $last_modified
  110. );
  111. if ( $dest->Exists() ) {
  112. $sql = 'UPDATE caldav_data SET caldav_data=:dav_data, dav_etag=:etag, logged_user=:session_user,
  113. modified=:modified, user_no=:user_no, caldav_type=\'VCARD\' WHERE dav_name=:dav_name';
  114. $response_code = 200;
  115. $put_action_type = 'UPDATE';
  116. $qry->QDo( $sql, $params );
  117. $qry->QDo("SELECT dav_id FROM caldav_data WHERE dav_name = :dav_name ", array(':dav_name' => $params[':dav_name']) );
  118. }
  119. else {
  120. $sql = 'INSERT INTO caldav_data ( user_no, dav_name, dav_etag, caldav_data, caldav_type, logged_user, created, modified, collection_id )
  121. VALUES( :user_no, :dav_name, :etag, :dav_data, \'VCARD\', :session_user, current_timestamp, :modified, :collection_id )';
  122. $params[':collection_id'] = $collection_id;
  123. $response_code = 201;
  124. $qry->QDo( $sql, $params );
  125. $put_action_type = 'INSERT';
  126. $qry->QDo("SELECT currval('dav_id_seq') AS dav_id" );
  127. }
  128. $row = $qry->Fetch();
  129. $vcard->Write( $row->dav_id, $dest->Exists() );
  130. $qry->QDo("SELECT write_sync_change( $collection_id, $response_code, :dav_name)", array(':dav_name' => $dest->bound_from() ) );
  131. if ( function_exists('log_caldav_action') ) {
  132. log_caldav_action( $put_action_type, $uid, $user_no, $collection_id, $request->path );
  133. }
  134. else if ( isset($log_action) && $log_action ) {
  135. dbg_error_log( 'PUT', 'No log_caldav_action( %s, %s, %s, %s, %s) can be called.',
  136. $put_action_type, $uid, $user_no, $collection_id, $request->path );
  137. }
  138. if ( !$qry->Commit() ) {
  139. $qry->Rollback();
  140. $request->DoResponse( 500, "A database error occurred" );
  141. }
  142. // Uncache anything to do with the collection
  143. $cache = getCacheInstance();
  144. $cache->delete( 'collection-'.$container->dav_name(), null );
  145. if ( $add_member ) header('Location: '.$c->protocol_server_port_script.$request->path);
  146. if ( $etag == $original_etag ) header('ETag: "'. $etag . '"' ); // Only send the ETag if we didn't change what they gave us.
  147. if ( $response_code == 200 ) $response_code = 204;
  148. $request->DoResponse( $response_code );