Blame view

sources/lib/private/connector/sabre/server.php 8.77 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?php
  /**
   * ownCloud / SabreDAV
   *
   * @author Markus Goetz
   *
   * @copyright Copyright (C) 2007-2013 Rooftop Solutions. All rights reserved.
   * @author Evert Pot (http://www.rooftopsolutions.nl/)
   * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
   */
  
  /**
   * Class OC_Connector_Sabre_Server
   *
6d9380f96   Cédric Dupont   Update sources OC...
15
   * This class reimplements some methods from @see \Sabre\DAV\Server.
31b7f2792   Kload   Upgrade to ownclo...
16
17
18
19
20
21
22
23
24
25
26
   *
   * Basically we add handling of depth: infinity.
   *
   * The right way to handle this would have been to submit a patch to the upstream project
   * and grab the corresponding version one merged.
   *
   * Due to time constrains and the limitations where we don't want to upgrade 3rdparty code in
   * this stage of the release cycle we did choose this approach.
   *
   * For ownCloud 7 we will upgrade SabreDAV and submit the patch - if needed.
   *
6d9380f96   Cédric Dupont   Update sources OC...
27
   * @see \Sabre\DAV\Server
31b7f2792   Kload   Upgrade to ownclo...
28
   */
6d9380f96   Cédric Dupont   Update sources OC...
29
  class OC_Connector_Sabre_Server extends Sabre\DAV\Server {
31b7f2792   Kload   Upgrade to ownclo...
30
31
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  	 * @var string
  	 */
  	private $overLoadedUri = null;
  
  	public function getRequestUri() {
  
  		if (!is_null($this->overLoadedUri)) {
  			return $this->overLoadedUri;
  		}
  
  		return parent::getRequestUri();
  	}
  
  	public function checkPreconditions($handleAsGET = false) {
  		// chunked upload handling
  		if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  			$filePath = parent::getRequestUri();
  			list($path, $name) = \Sabre\DAV\URLUtil::splitPath($filePath);
  			$info = OC_FileChunking::decodeName($name);
  			if (!empty($info)) {
  				$filePath = $path . '/' . $info['name'];
  				$this->overLoadedUri = $filePath;
  			}
  		}
  
  		$result = parent::checkPreconditions($handleAsGET);
  		$this->overLoadedUri = null;
  		return $result;
  	}
  
  	/**
  	 * @see \Sabre\DAV\Server
31b7f2792   Kload   Upgrade to ownclo...
64
65
  	 */
  	protected function httpPropfind($uri) {
6d9380f96   Cédric Dupont   Update sources OC...
66
  		// $xml = new \Sabre\DAV\XMLReader(file_get_contents('php://input'));
31b7f2792   Kload   Upgrade to ownclo...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  		$requestedProperties = $this->parsePropFindRequest($this->httpRequest->getBody(true));
  
  		$depth = $this->getHTTPDepth(1);
  		// The only two options for the depth of a propfind is 0 or 1
  		// if ($depth!=0) $depth = 1;
  
  		$newProperties = $this->getPropertiesForPath($uri,$requestedProperties,$depth);
  
  		// This is a multi-status response
  		$this->httpResponse->sendStatus(207);
  		$this->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
  		$this->httpResponse->setHeader('Vary','Brief,Prefer');
  
  		// Normally this header is only needed for OPTIONS responses, however..
  		// iCal seems to also depend on these being set for PROPFIND. Since
  		// this is not harmful, we'll add it.
  		$features = array('1','3', 'extended-mkcol');
  		foreach($this->plugins as $plugin) {
  			$features = array_merge($features,$plugin->getFeatures());
  		}
  
  		$this->httpResponse->setHeader('DAV',implode(', ',$features));
  
  		$prefer = $this->getHTTPPrefer();
  		$minimal = $prefer['return-minimal'];
  
  		$data = $this->generateMultiStatus($newProperties, $minimal);
  		$this->httpResponse->sendBody($data);
  
  	}
  
  	/**
  	 * Small helper to support PROPFIND with DEPTH_INFINITY.
6d9380f96   Cédric Dupont   Update sources OC...
100
  	 * @param string $path
31b7f2792   Kload   Upgrade to ownclo...
101
102
103
104
  	 */
  	private function addPathNodesRecursively(&$nodes, $path) {
  		foreach($this->tree->getChildren($path) as $childNode) {
  			$nodes[$path . '/' . $childNode->getName()] = $childNode;
6d9380f96   Cédric Dupont   Update sources OC...
105
  			if ($childNode instanceof \Sabre\DAV\ICollection)
31b7f2792   Kload   Upgrade to ownclo...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  				$this->addPathNodesRecursively($nodes, $path . '/' . $childNode->getName());
  		}
  	}
  
  	public function getPropertiesForPath($path, $propertyNames = array(), $depth = 0) {
  
  		//	if ($depth!=0) $depth = 1;
  
  		$path = rtrim($path,'/');
  
  		$returnPropertyList = array();
  
  		$parentNode = $this->tree->getNodeForPath($path);
  		$nodes = array(
  			$path => $parentNode
  		);
6d9380f96   Cédric Dupont   Update sources OC...
122
  		if ($depth==1 && $parentNode instanceof \Sabre\DAV\ICollection) {
31b7f2792   Kload   Upgrade to ownclo...
123
124
  			foreach($this->tree->getChildren($path) as $childNode)
  				$nodes[$path . '/' . $childNode->getName()] = $childNode;
6d9380f96   Cédric Dupont   Update sources OC...
125
  		} else if ($depth == self::DEPTH_INFINITY && $parentNode instanceof \Sabre\DAV\ICollection) {
31b7f2792   Kload   Upgrade to ownclo...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  			$this->addPathNodesRecursively($nodes, $path);
  		}
  
  		// If the propertyNames array is empty, it means all properties are requested.
  		// We shouldn't actually return everything we know though, and only return a
  		// sensible list.
  		$allProperties = count($propertyNames)==0;
  
  		foreach($nodes as $myPath=>$node) {
  
  			$currentPropertyNames = $propertyNames;
  
  			$newProperties = array(
  				'200' => array(),
  				'404' => array(),
  			);
  
  			if ($allProperties) {
  				// Default list of propertyNames, when all properties were requested.
  				$currentPropertyNames = array(
  					'{DAV:}getlastmodified',
  					'{DAV:}getcontentlength',
  					'{DAV:}resourcetype',
  					'{DAV:}quota-used-bytes',
  					'{DAV:}quota-available-bytes',
  					'{DAV:}getetag',
  					'{DAV:}getcontenttype',
  				);
  			}
  
  			// If the resourceType was not part of the list, we manually add it
  			// and mark it for removal. We need to know the resourcetype in order
  			// to make certain decisions about the entry.
  			// WebDAV dictates we should add a / and the end of href's for collections
  			$removeRT = false;
  			if (!in_array('{DAV:}resourcetype',$currentPropertyNames)) {
  				$currentPropertyNames[] = '{DAV:}resourcetype';
  				$removeRT = true;
  			}
  
  			$result = $this->broadcastEvent('beforeGetProperties',array($myPath, $node, &$currentPropertyNames, &$newProperties));
  			// If this method explicitly returned false, we must ignore this
  			// node as it is inaccessible.
  			if ($result===false) continue;
  
  			if (count($currentPropertyNames) > 0) {
6d9380f96   Cédric Dupont   Update sources OC...
172
  				if ($node instanceof \Sabre\DAV\IProperties) {
31b7f2792   Kload   Upgrade to ownclo...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  					$nodeProperties = $node->getProperties($currentPropertyNames);
  
  					// The getProperties method may give us too much,
  					// properties, in case the implementor was lazy.
  					//
  					// So as we loop through this list, we will only take the
  					// properties that were actually requested and discard the
  					// rest.
  					foreach($currentPropertyNames as $k=>$currentPropertyName) {
  						if (isset($nodeProperties[$currentPropertyName])) {
  							unset($currentPropertyNames[$k]);
  							$newProperties[200][$currentPropertyName] = $nodeProperties[$currentPropertyName];
  						}
  					}
  
  				}
  
  			}
  
  			foreach($currentPropertyNames as $prop) {
  
  				if (isset($newProperties[200][$prop])) continue;
  
  				switch($prop) {
6d9380f96   Cédric Dupont   Update sources OC...
197
  					case '{DAV:}getlastmodified'       : if ($node->getLastModified()) $newProperties[200][$prop] = new \Sabre\DAV\Property\GetLastModified($node->getLastModified()); break;
31b7f2792   Kload   Upgrade to ownclo...
198
  					case '{DAV:}getcontentlength'      :
6d9380f96   Cédric Dupont   Update sources OC...
199
  						if ($node instanceof \Sabre\DAV\IFile) {
31b7f2792   Kload   Upgrade to ownclo...
200
201
  							$size = $node->getSize();
  							if (!is_null($size)) {
6d9380f96   Cédric Dupont   Update sources OC...
202
  								$newProperties[200][$prop] = 0 + $size;
31b7f2792   Kload   Upgrade to ownclo...
203
204
205
206
  							}
  						}
  						break;
  					case '{DAV:}quota-used-bytes'      :
6d9380f96   Cédric Dupont   Update sources OC...
207
  						if ($node instanceof \Sabre\DAV\IQuota) {
31b7f2792   Kload   Upgrade to ownclo...
208
209
210
211
212
  							$quotaInfo = $node->getQuotaInfo();
  							$newProperties[200][$prop] = $quotaInfo[0];
  						}
  						break;
  					case '{DAV:}quota-available-bytes' :
6d9380f96   Cédric Dupont   Update sources OC...
213
  						if ($node instanceof \Sabre\DAV\IQuota) {
31b7f2792   Kload   Upgrade to ownclo...
214
215
216
217
  							$quotaInfo = $node->getQuotaInfo();
  							$newProperties[200][$prop] = $quotaInfo[1];
  						}
  						break;
6d9380f96   Cédric Dupont   Update sources OC...
218
219
  					case '{DAV:}getetag'               : if ($node instanceof \Sabre\DAV\IFile && $etag = $node->getETag())  $newProperties[200][$prop] = $etag; break;
  					case '{DAV:}getcontenttype'        : if ($node instanceof \Sabre\DAV\IFile && $ct = $node->getContentType())  $newProperties[200][$prop] = $ct; break;
31b7f2792   Kload   Upgrade to ownclo...
220
221
222
223
224
  					case '{DAV:}supported-report-set'  :
  						$reports = array();
  						foreach($this->plugins as $plugin) {
  							$reports = array_merge($reports, $plugin->getSupportedReportSet($myPath));
  						}
6d9380f96   Cédric Dupont   Update sources OC...
225
  						$newProperties[200][$prop] = new \Sabre\DAV\Property\SupportedReportSet($reports);
31b7f2792   Kload   Upgrade to ownclo...
226
227
  						break;
  					case '{DAV:}resourcetype' :
6d9380f96   Cédric Dupont   Update sources OC...
228
  						$newProperties[200]['{DAV:}resourcetype'] = new \Sabre\DAV\Property\ResourceType();
31b7f2792   Kload   Upgrade to ownclo...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  						foreach($this->resourceTypeMapping as $className => $resourceType) {
  							if ($node instanceof $className) $newProperties[200]['{DAV:}resourcetype']->add($resourceType);
  						}
  						break;
  
  				}
  
  				// If we were unable to find the property, we will list it as 404.
  				if (!$allProperties && !isset($newProperties[200][$prop])) $newProperties[404][$prop] = null;
  
  			}
  
  			$this->broadcastEvent('afterGetProperties',array(trim($myPath,'/'),&$newProperties, $node));
  
  			$newProperties['href'] = trim($myPath,'/');
  
  			// Its is a WebDAV recommendation to add a trailing slash to collectionnames.
  			// Apple's iCal also requires a trailing slash for principals (rfc 3744), though this is non-standard.
  			if ($myPath!='' && isset($newProperties[200]['{DAV:}resourcetype'])) {
  				$rt = $newProperties[200]['{DAV:}resourcetype'];
  				if ($rt->is('{DAV:}collection') || $rt->is('{DAV:}principal')) {
  					$newProperties['href'] .='/';
  				}
  			}
  
  			// If the resourcetype property was manually added to the requested property list,
  			// we will remove it again.
  			if ($removeRT) unset($newProperties[200]['{DAV:}resourcetype']);
  
  			$returnPropertyList[] = $newProperties;
  
  		}
  
  		return $returnPropertyList;
  
  	}
  }