/usr/share/php/sabre21/Sabre/DAV/TemporaryFileFilterPlugin.php is in php-sabre-dav-2.1 2.1.10-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 64 65 66 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | <?php
namespace Sabre\DAV;
use
Sabre\HTTP\RequestInterface,
Sabre\HTTP\ResponseInterface;
/**
* Temporary File Filter Plugin
*
* The purpose of this filter is to intercept some of the garbage files
* operation systems and applications tend to generate when mounting
* a WebDAV share as a disk.
*
* It will intercept these files and place them in a separate directory.
* these files are not deleted automatically, so it is adviceable to
* delete these after they are not accessed for 24 hours.
*
* Currently it supports:
* * OS/X style resource forks and .DS_Store
* * desktop.ini and Thumbs.db (windows)
* * .*.swp (vim temporary files)
* * .dat.* (smultron temporary files)
*
* Additional patterns can be added, by adding on to the
* temporaryFilePatterns property.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class TemporaryFileFilterPlugin extends ServerPlugin {
/**
* This is the list of patterns we intercept.
* If new patterns are added, they must be valid patterns for preg_match.
*
* @var array
*/
public $temporaryFilePatterns = [
'/^\._(.*)$/', // OS/X resource forks
'/^.DS_Store$/', // OS/X custom folder settings
'/^desktop.ini$/', // Windows custom folder settings
'/^Thumbs.db$/', // Windows thumbnail cache
'/^.(.*).swp$/', // ViM temporary files
'/^\.dat(.*)$/', // Smultron seems to create these
'/^~lock.(.*)#$/', // Windows 7 lockfiles
];
/**
* A reference to the main Server class
*
* @var Sabre\DAV\Server
*/
protected $server;
/**
* This is the directory where this plugin
* will store it's files.
*
* @var string
*/
private $dataDir;
/**
* Creates the plugin.
*
* Make sure you specify a directory for your files. If you don't, we
* will use PHP's directory for session-storage instead, and you might
* not want that.
*
* @param string|null $dataDir
*/
function __construct($dataDir = null) {
if (!$dataDir) $dataDir = ini_get('session.save_path').'/sabredav/';
if (!is_dir($dataDir)) mkdir($dataDir);
$this->dataDir = $dataDir;
}
/**
* Initialize the plugin
*
* This is called automatically be the Server class after this plugin is
* added with Sabre\DAV\Server::addPlugin()
*
* @param Server $server
* @return void
*/
function initialize(Server $server) {
$this->server = $server;
$server->on('beforeMethod', [$this,'beforeMethod']);
$server->on('beforeCreateFile',[$this,'beforeCreateFile']);
}
/**
* This method is called before any HTTP method handler
*
* This method intercepts any GET, DELETE, PUT and PROPFIND calls to
* filenames that are known to match the 'temporary file' regex.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
*/
function beforeMethod(RequestInterface $request, ResponseInterface $response) {
if (!$tempLocation = $this->isTempFile($request->getPath()))
return true;
switch($request->getMethod()) {
case 'GET' :
return $this->httpGet($request, $response, $tempLocation);
case 'PUT' :
return $this->httpPut($request, $response, $tempLocation);
case 'PROPFIND' :
return $this->httpPropfind($request, $response, $tempLocation);
case 'DELETE' :
return $this->httpDelete($request, $response, $tempLocation);
}
return true;
}
/**
* This method is invoked if some subsystem creates a new file.
*
* This is used to deal with HTTP LOCK requests which create a new
* file.
*
* @param string $uri
* @param resource $data
* @return bool
*/
function beforeCreateFile($uri,$data) {
if ($tempPath = $this->isTempFile($uri)) {
$hR = $this->server->httpResponse;
$hR->setHeader('X-Sabre-Temp','true');
file_put_contents($tempPath,$data);
return false;
}
return true;
}
/**
* This method will check if the url matches the temporary file pattern
* if it does, it will return an path based on $this->dataDir for the
* temporary file storage.
*
* @param string $path
* @return boolean|string
*/
protected function isTempFile($path) {
// We're only interested in the basename.
list(, $tempPath) = URLUtil::splitPath($path);
foreach($this->temporaryFilePatterns as $tempFile) {
if (preg_match($tempFile,$tempPath)) {
return $this->getDataDir() . '/sabredav_' . md5($path) . '.tempfile';
}
}
return false;
}
/**
* This method handles the GET method for temporary files.
* If the file doesn't exist, it will return false which will kick in
* the regular system for the GET method.
*
* @param RequestInterface $request
* @param ResponseInterface $hR
* @param string $tempLocation
* @return bool
*/
function httpGet(RequestInterface $request, ResponseInterface $hR, $tempLocation) {
if (!file_exists($tempLocation)) return true;
$hR->setHeader('Content-Type','application/octet-stream');
$hR->setHeader('Content-Length',filesize($tempLocation));
$hR->setHeader('X-Sabre-Temp','true');
$hR->setStatus(200);
$hR->setBody(fopen($tempLocation,'r'));
return false;
}
/**
* This method handles the PUT method.
*
* @param RequestInterface $request
* @param ResponseInterface $hR
* @param string $tempLocation
* @return bool
*/
function httpPut(RequestInterface $request, ResponseInterface $hR, $tempLocation) {
$hR->setHeader('X-Sabre-Temp','true');
$newFile = !file_exists($tempLocation);
if (!$newFile && ($this->server->httpRequest->getHeader('If-None-Match'))) {
throw new Exception\PreconditionFailed('The resource already exists, and an If-None-Match header was supplied');
}
file_put_contents($tempLocation,$this->server->httpRequest->getBody());
$hR->setStatus($newFile?201:200);
return false;
}
/**
* This method handles the DELETE method.
*
* If the file didn't exist, it will return false, which will make the
* standard HTTP DELETE handler kick in.
*
* @param RequestInterface $request
* @param ResponseInterface $hR
* @param string $tempLocation
* @return bool
*/
function httpDelete(RequestInterface $request, ResponseInterface $hR, $tempLocation) {
if (!file_exists($tempLocation)) return true;
unlink($tempLocation);
$hR->setHeader('X-Sabre-Temp','true');
$hR->setStatus(204);
return false;
}
/**
* This method handles the PROPFIND method.
*
* It's a very lazy method, it won't bother checking the request body
* for which properties were requested, and just sends back a default
* set of properties.
*
* @param RequestInterface $request
* @param ResponseInterface $hR
* @param string $tempLocation
* @return bool
*/
function httpPropfind(RequestInterface $request, ResponseInterface $hR, $tempLocation) {
if (!file_exists($tempLocation)) return true;
$hR->setHeader('X-Sabre-Temp','true');
$hR->setStatus(207);
$hR->setHeader('Content-Type','application/xml; charset=utf-8');
$this->server->parsePropFindRequest($request->getBodyAsString());
$properties = [
'href' => $request->getPath(),
200 => [
'{DAV:}getlastmodified' => new Property\GetLastModified(filemtime($tempLocation)),
'{DAV:}getcontentlength' => filesize($tempLocation),
'{DAV:}resourcetype' => new Property\ResourceType(null),
'{'.Server::NS_SABREDAV.'}tempFile' => true,
],
];
$data = $this->server->generateMultiStatus([$properties]);
$hR->setBody($data);
return false;
}
/**
* This method returns the directory where the temporary files should be stored.
*
* @return string
*/
protected function getDataDir()
{
return $this->dataDir;
}
}
|