|
| 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