Fix getctag replacement in this test.
[davical.git] / inc / caldav-LOCK.php
blob6ee2cfd87b1ebe849a1599519945bd48583fc9d6
1 <?php
2 /**
3 * We support both LOCK and UNLOCK methods in this function
4 */
6 require_once('XMLDocument.php');
7 $reply = new XMLDocument(array( 'DAV:' => '' ));
9 if ( ! $request->AllowedTo('write') ) {
10 $request->NeedPrivilege( 'write', $request->path );
13 if ( ! isset($request->xml_tags) ) {
14 if ( isset($request->lock_token) ) {
15 // It's OK for LOCK refresh requests to be empty.
16 $request->xml_tags = array();
18 else {
19 $request->XMLResponse( 400, new XMLElement( 'error', new XMLElement('missing-xml-for-request'), $reply->GetXmlNsArray() ) );
24 $unsupported = array();
25 $lockinfo = array();
26 $inside = array();
28 foreach( $request->xml_tags AS $k => $v ) {
30 $tag = $v['tag'];
31 dbg_error_log( "LOCK", " Handling Tag '%s' => '%s' ", $k, $v );
32 switch ( $tag ) {
33 case 'DAV::lockinfo':
34 dbg_error_log( "LOCK", ":Request: %s -> %s", $v['type'], $tag );
35 if ( $v['type'] == "open" ) {
36 $lockscope = "";
37 $locktype = "";
38 $lockowner = "";
39 $inside[$tag] = true;
41 else if ( $inside[$tag] && $v['type'] == "close" ) {
42 $lockinfo['scope'] = $lockscope; unset($lockscope);
43 $lockinfo['type'] = $locktype; unset($locktype);
44 $lockinfo['owner'] = $lockowner; unset($lockowner);
45 $inside[$tag] = false;
47 break;
49 case 'DAV::owner':
50 case 'DAV::locktype':
51 case 'DAV::lockscope':
52 dbg_error_log( "LOCK", ":Request: %s -> %s", $v['type'], $tag );
53 if ( $inside['DAV::lockinfo'] ) {
54 if ( $v['type'] == "open" ) {
55 $inside[$tag] = true;
57 else if ( $inside[$tag] && $v['type'] == "close" ) {
58 $inside[$tag] = false;
61 break;
63 /*case 'DAV::SHARED': */ /** Shared lock is not supported yet */
64 case 'DAV::exclusive':
65 dbg_error_log( "LOCK", ":Request: %s -> %s", $v['type'], $tag );
66 if ( $inside['DAV::lockscope'] && $v['type'] == "complete" ) {
67 $lockscope = strtolower(substr($tag,5));
69 break;
71 /* case 'DAV::READ': */ /** RFC2518 is pretty vague about read locks */
72 case 'DAV::write':
73 dbg_error_log( "LOCK", ":Request: %s -> %s", $v['type'], $tag );
74 if ( $inside['DAV::locktype'] && $v['type'] == "complete" ) {
75 $locktype = strtolower(substr($tag,5));
77 break;
79 case 'DAV::href':
80 dbg_error_log( "LOCK", ":Request: %s -> %s", $v['type'], $tag );
81 dbg_log_array( "LOCK", "DAV:href", $v, true );
82 if ( $inside['DAV::owner'] && $v['type'] == "complete" ) {
83 $lockowner = $v['value'];
85 break;
87 default:
88 if ( preg_match('/^(.*):([^:]+)$/', $tag, $matches) ) {
89 $unsupported[$matches[2]] = $matches[1];
91 else {
92 $unsupported[$tag] = "";
94 dbg_error_log( "LOCK", "Unhandled tag >>%s<<", $tag);
101 $request->UnsupportedRequest($unsupported); // Won't return if there was unsupported stuff.
103 $lock_opener = $request->FailIfLocked();
106 if ( $request->method == "LOCK" ) {
107 dbg_error_log( "LOCK", "Attempting to lock resource '%s'", $request->path);
108 $lock_timeout = (empty($request->timeout) ? 30 : intval($request->timeout) );
109 if ( $lock_timeout < 1 ) $lock_timeout = 30;
110 else if ( $lock_timeout > 300 ) $lock_timeout = 300;
111 if ( ($lock_token = $request->IsLocked()) ) { // NOTE Assignment in if() is expected here.
112 $sql = 'UPDATE locks SET start = current_timestamp WHERE opaquelocktoken = :lock_token';
113 $params = array( ':lock_token' => $lock_token);
115 else {
117 * A fresh lock
119 $lock_token = uuid();
120 $sql = 'INSERT INTO locks ( dav_name, opaquelocktoken, type, scope, depth, owner, timeout, start )
121 VALUES( :dav_name, :lock_token, :type, :scope, :request_depth, :owner, :timeout::interval, current_timestamp )';
122 $params = array(
123 ':dav_name' => $request->path,
124 ':lock_token' => $lock_token,
125 ':type' => $lockinfo['type'],
126 ':scope' => $lockinfo['scope'],
127 ':request_depth' => $request->depth,
128 ':owner' => $lockinfo['owner'],
129 ':timeout' => $lock_timeout.' seconds'
131 header( "Lock-Token: <opaquelocktoken:$lock_token>" );
133 $qry = new AwlQuery($sql, $params );
134 $qry->Exec("LOCK",__LINE__,__FILE__);
136 $lock_row = $request->GetLockRow($lock_token);
137 $activelock = array(
138 new XMLElement( 'locktype', new XMLElement( $lock_row->type )),
139 new XMLElement( 'lockscope', new XMLElement( $lock_row->scope )),
140 new XMLElement( 'depth', $request->GetDepthName() ),
141 new XMLElement( 'owner', new XMLElement( 'href', $lock_row->owner )),
142 new XMLElement( 'timeout', 'Second-'.$lock_timeout),
143 new XMLElement( 'locktoken', new XMLElement( 'href', 'opaquelocktoken:'.$lock_token ))
145 $response = new XMLElement("lockdiscovery", new XMLElement( "activelock", $activelock), array("xmlns" => "DAV:") );
147 elseif ( $request->method == "UNLOCK" ) {
149 * @todo respond with preconditionfailed(409,'lock-token-matches-request-uri') if
150 * there is no lock to be deleted.
152 dbg_error_log( "LOCK", "Attempting to unlock resource '%s'", $request->path);
153 if ( ($lock_token = $request->IsLocked()) ) { // NOTE Assignment in if() is expected here.
154 $sql = 'DELETE FROM locks WHERE opaquelocktoken = :lock_token';
155 $qry = new AwlQuery($sql, array( ':lock_token' => $lock_token) );
156 $qry->Exec("LOCK",__LINE__,__FILE__);
158 $request->DoResponse( 204 );
162 $prop = new XMLElement( "prop", $response, array('xmlns'=>'DAV:') );
163 // dbg_log_array( "LOCK", "XML", $response, true );
164 $xmldoc = $prop->Render(0,'<?xml version="1.0" encoding="utf-8" ?>');
165 $request->DoResponse( 200, $xmldoc, 'text/xml; charset="utf-8"' );