Skip to content

Commit a425f04

Browse files
committed
Added the TemporaryFileFilter tree-class
1 parent 928c432 commit a425f04

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

Sabre/DAV/FilterTree.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @author Evert Pot (http://www.rooftopsolutions.nl/)
1616
* @license licence http://www.freebsd.org/copyright/license.html BSD License (4 Clause)
1717
*/
18-
class Sabre_DAV_FilterTree extends Sabre_DAV_Tree {
18+
abstract class Sabre_DAV_FilterTree extends Sabre_DAV_Tree {
1919

2020
/**
2121
* subject
@@ -32,7 +32,7 @@ class Sabre_DAV_FilterTree extends Sabre_DAV_Tree {
3232
*/
3333
public function __construct(Sabre_DAV_Tree $subject) {
3434

35-
$this->tempDir = $tempDir;
35+
$this->subject = $subject;
3636

3737
}
3838

@@ -144,7 +144,7 @@ public function move($sourcePath, $destinationPath) {
144144
*/
145145
public function supportsLocks() {
146146

147-
return $this->subject->supportLocks();
147+
return $this->subject->supportsLocks();
148148

149149
}
150150

Sabre/DAV/TemporaryFileFilter.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
require_once 'Sabre/DAV/FilterTree.php';
4+
5+
class Sabre_DAV_TemporaryFileFilter extends Sabre_DAV_FilterTree {
6+
7+
private $dataDir = null;
8+
9+
const FINDER_FORK =
10+
'H4sIAAAAAAAAA+3XMWsCMRQH8Beh2Fu0OHQqR+Yicji6FV0cRKiHlG7xfNJwuSTkIuK36mdy6rfQiGepwtHNQd6PhHCP9w+5bIGH5yY0ACYi49MZ/+CVYw2iMPsAre+whu/WDoDFcPYENd7S9D0snVMCfsL8vGppVPXXMDJT9IS1CnsFerEUXgzyyWjssRiXs8wh6qGwfu3wFGVhX0gAuvW5i1S6tSG5sEqWPkmix2oXVp2EXZ0sOnfON1Ivzea//nbdLRBCCCGEEEIIuTUGf55tcfolS+6wNGuXIV8Zl3OpPWovjRZKbbnClecLJXR+fAffgcv//y2/QLzfHwDNa116ABAAAA==';
11+
12+
public function setDataDir($path) {
13+
14+
$this->dataDir = $path;
15+
16+
}
17+
18+
public function isTempFile($path) {
19+
20+
$tempPath = basename($path);
21+
22+
$tempFiles = array(
23+
'/^._(.*)$/', // OS/X resource forks
24+
'/^.DS_Store$/', // OS/X custom folder settings
25+
'/^desktop.ini$/', // Windows custom folder settings
26+
'/^Thumbs.db$/', // Windows thumbnail cache
27+
'/^.(.*).swp$/', // ViM temporary files
28+
);
29+
30+
$match = false;
31+
foreach($tempFiles as $tempFile) {
32+
33+
if (preg_match($tempFile,$tempPath)) $match = true;
34+
35+
}
36+
37+
if ($match) {
38+
$dataDir = (is_null($this->dataDir)?ini_get('session.save_path').'/sabredav/':$this->dataDir);
39+
return $dataDir . '/sabredav_' . md5($path) . '.tempfile';
40+
} else {
41+
return false;
42+
}
43+
44+
}
45+
46+
public function put($path,$data) {
47+
48+
if ($tempPath = $this->isTempFile($path)) {
49+
50+
file_put_contents($tempPath,$data);
51+
52+
} else return parent::put($path,$data);
53+
54+
}
55+
56+
public function createFile($path,$data) {
57+
58+
if ($tempPath = $this->isTempFile($path)) {
59+
60+
file_put_contents($tempPath,$data);
61+
62+
} else return parent::createFile($path,$data);
63+
64+
}
65+
66+
public function get($path) {
67+
68+
if ($tempPath = $this->isTempFile($path)) {
69+
70+
if (!file_exists($tempPath)) {
71+
if (strpos(basename($path),'._')===0) echo gzdecode(base64_decode(self::FINDER_FORK));
72+
else throw new Sabre_DAV_FileNotFoundException();
73+
} else {
74+
return file_get_contents($tempPath);
75+
}
76+
77+
} else return parent::get($path);
78+
79+
}
80+
81+
public function delete($path) {
82+
83+
if ($tempPath = $this->isTempFile($path)) {
84+
85+
return(file_exists($tempPath) && unlink($tempPath));
86+
87+
} else return parent::delete($path);
88+
89+
}
90+
91+
public function getNodeInfo($path,$depth=0) {
92+
93+
if (($tempPath = $this->isTempFile($path)) && !$depth) {
94+
95+
//echo $tempPath;
96+
if (!file_exists($tempPath)) {
97+
if (strpos(basename($path),'._')===0) {
98+
return array(array(
99+
'name' => '',
100+
'type' => Sabre_DAV_Server::NODE_FILE,
101+
'lastmodified' => filemtime(__FILE__),
102+
'size' => 4096,
103+
));
104+
105+
} else {
106+
throw new Sabre_DAV_FileNotFoundException();
107+
}
108+
}
109+
$props = array(
110+
'name' => '',
111+
'type' => Sabre_DAV_Server::NODE_FILE,
112+
'lastmodified' => filemtime($tempPath),
113+
'size' => filesize($tempPath),
114+
);
115+
116+
return array($props);
117+
118+
} else return parent::getNodeInfo($path,$depth);
119+
120+
}
121+
122+
}
123+
124+
?>

0 commit comments

Comments
 (0)