@@ -49,438 +49,438 @@ |
||
49 | 49 | * |
50 | 50 | */ |
51 | 51 | class OC_Files { |
52 | - const FILE = 1; |
|
53 | - const ZIP_FILES = 2; |
|
54 | - const ZIP_DIR = 3; |
|
55 | - |
|
56 | - const UPLOAD_MIN_LIMIT_BYTES = 1048576; // 1 MiB |
|
57 | - |
|
58 | - |
|
59 | - private static $multipartBoundary = ''; |
|
60 | - |
|
61 | - /** |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - private static function getBoundary() { |
|
65 | - if (empty(self::$multipartBoundary)) { |
|
66 | - self::$multipartBoundary = md5(mt_rand()); |
|
67 | - } |
|
68 | - return self::$multipartBoundary; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param string $filename |
|
73 | - * @param string $name |
|
74 | - * @param array $rangeArray ('from'=>int,'to'=>int), ... |
|
75 | - */ |
|
76 | - private static function sendHeaders($filename, $name, array $rangeArray) { |
|
77 | - OC_Response::setContentDispositionHeader($name, 'attachment'); |
|
78 | - header('Content-Transfer-Encoding: binary', true); |
|
79 | - header('Pragma: public');// enable caching in IE |
|
80 | - header('Expires: 0'); |
|
81 | - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
82 | - $fileSize = \OC\Files\Filesystem::filesize($filename); |
|
83 | - $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); |
|
84 | - if ($fileSize > -1) { |
|
85 | - if (!empty($rangeArray)) { |
|
86 | - header('HTTP/1.1 206 Partial Content', true); |
|
87 | - header('Accept-Ranges: bytes', true); |
|
88 | - if (count($rangeArray) > 1) { |
|
89 | - $type = 'multipart/byteranges; boundary='.self::getBoundary(); |
|
90 | - // no Content-Length header here |
|
91 | - } |
|
92 | - else { |
|
93 | - header(sprintf('Content-Range: bytes %d-%d/%d', $rangeArray[0]['from'], $rangeArray[0]['to'], $fileSize), true); |
|
94 | - OC_Response::setContentLengthHeader($rangeArray[0]['to'] - $rangeArray[0]['from'] + 1); |
|
95 | - } |
|
96 | - } |
|
97 | - else { |
|
98 | - OC_Response::setContentLengthHeader($fileSize); |
|
99 | - } |
|
100 | - } |
|
101 | - header('Content-Type: '.$type, true); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * return the content of a file or return a zip file containing multiple files |
|
106 | - * |
|
107 | - * @param string $dir |
|
108 | - * @param string $files ; separated list of files to download |
|
109 | - * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header |
|
110 | - */ |
|
111 | - public static function get($dir, $files, $params = null) { |
|
112 | - |
|
113 | - $view = \OC\Files\Filesystem::getView(); |
|
114 | - $getType = self::FILE; |
|
115 | - $filename = $dir; |
|
116 | - try { |
|
117 | - |
|
118 | - if (is_array($files) && count($files) === 1) { |
|
119 | - $files = $files[0]; |
|
120 | - } |
|
121 | - |
|
122 | - if (!is_array($files)) { |
|
123 | - $filename = $dir . '/' . $files; |
|
124 | - if (!$view->is_dir($filename)) { |
|
125 | - self::getSingleFile($view, $dir, $files, is_null($params) ? array() : $params); |
|
126 | - return; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - $name = 'download'; |
|
131 | - if (is_array($files)) { |
|
132 | - $getType = self::ZIP_FILES; |
|
133 | - $basename = basename($dir); |
|
134 | - if ($basename) { |
|
135 | - $name = $basename; |
|
136 | - } |
|
137 | - |
|
138 | - $filename = $dir . '/' . $name; |
|
139 | - } else { |
|
140 | - $filename = $dir . '/' . $files; |
|
141 | - $getType = self::ZIP_DIR; |
|
142 | - // downloading root ? |
|
143 | - if ($files !== '') { |
|
144 | - $name = $files; |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - self::lockFiles($view, $dir, $files); |
|
149 | - |
|
150 | - /* Calculate filesize and number of files */ |
|
151 | - if ($getType === self::ZIP_FILES) { |
|
152 | - $fileInfos = array(); |
|
153 | - $fileSize = 0; |
|
154 | - foreach ($files as $file) { |
|
155 | - $fileInfo = \OC\Files\Filesystem::getFileInfo($dir . '/' . $file); |
|
156 | - $fileSize += $fileInfo->getSize(); |
|
157 | - $fileInfos[] = $fileInfo; |
|
158 | - } |
|
159 | - $numberOfFiles = self::getNumberOfFiles($fileInfos); |
|
160 | - } elseif ($getType === self::ZIP_DIR) { |
|
161 | - $fileInfo = \OC\Files\Filesystem::getFileInfo($dir . '/' . $files); |
|
162 | - $fileSize = $fileInfo->getSize(); |
|
163 | - $numberOfFiles = self::getNumberOfFiles(array($fileInfo)); |
|
164 | - } |
|
165 | - |
|
166 | - $streamer = new Streamer(\OC::$server->getRequest(), $fileSize, $numberOfFiles); |
|
167 | - OC_Util::obEnd(); |
|
168 | - |
|
169 | - $streamer->sendHeaders($name); |
|
170 | - $executionTime = (int)OC::$server->getIniWrapper()->getNumeric('max_execution_time'); |
|
171 | - if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
172 | - @set_time_limit(0); |
|
173 | - } |
|
174 | - ignore_user_abort(true); |
|
175 | - |
|
176 | - if ($getType === self::ZIP_FILES) { |
|
177 | - foreach ($files as $file) { |
|
178 | - $file = $dir . '/' . $file; |
|
179 | - if (\OC\Files\Filesystem::is_file($file)) { |
|
180 | - $fileSize = \OC\Files\Filesystem::filesize($file); |
|
181 | - $fileTime = \OC\Files\Filesystem::filemtime($file); |
|
182 | - $fh = \OC\Files\Filesystem::fopen($file, 'r'); |
|
183 | - $streamer->addFileFromStream($fh, basename($file), $fileSize, $fileTime); |
|
184 | - fclose($fh); |
|
185 | - } elseif (\OC\Files\Filesystem::is_dir($file)) { |
|
186 | - $streamer->addDirRecursive($file); |
|
187 | - } |
|
188 | - } |
|
189 | - } elseif ($getType === self::ZIP_DIR) { |
|
190 | - $file = $dir . '/' . $files; |
|
191 | - $streamer->addDirRecursive($file); |
|
192 | - } |
|
193 | - $streamer->finalize(); |
|
194 | - set_time_limit($executionTime); |
|
195 | - self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
196 | - } catch (\OCP\Lock\LockedException $ex) { |
|
197 | - self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
198 | - OC::$server->getLogger()->logException($ex); |
|
199 | - $l = \OC::$server->getL10N('core'); |
|
200 | - $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; |
|
201 | - \OC_Template::printErrorPage($l->t('File is currently busy, please try again later'), $hint); |
|
202 | - } catch (\OCP\Files\ForbiddenException $ex) { |
|
203 | - self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
204 | - OC::$server->getLogger()->logException($ex); |
|
205 | - $l = \OC::$server->getL10N('core'); |
|
206 | - \OC_Template::printErrorPage($l->t('Can\'t read file'), $ex->getMessage()); |
|
207 | - } catch (\Exception $ex) { |
|
208 | - self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
209 | - OC::$server->getLogger()->logException($ex); |
|
210 | - $l = \OC::$server->getL10N('core'); |
|
211 | - $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; |
|
212 | - \OC_Template::printErrorPage($l->t('Can\'t read file'), $hint); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @param string $rangeHeaderPos |
|
218 | - * @param int $fileSize |
|
219 | - * @return array $rangeArray ('from'=>int,'to'=>int), ... |
|
220 | - */ |
|
221 | - private static function parseHttpRangeHeader($rangeHeaderPos, $fileSize) { |
|
222 | - $rArray=explode(',', $rangeHeaderPos); |
|
223 | - $minOffset = 0; |
|
224 | - $ind = 0; |
|
225 | - |
|
226 | - $rangeArray = array(); |
|
227 | - |
|
228 | - foreach ($rArray as $value) { |
|
229 | - $ranges = explode('-', $value); |
|
230 | - if (is_numeric($ranges[0])) { |
|
231 | - if ($ranges[0] < $minOffset) { // case: bytes=500-700,601-999 |
|
232 | - $ranges[0] = $minOffset; |
|
233 | - } |
|
234 | - if ($ind > 0 && $rangeArray[$ind-1]['to']+1 == $ranges[0]) { // case: bytes=500-600,601-999 |
|
235 | - $ind--; |
|
236 | - $ranges[0] = $rangeArray[$ind]['from']; |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - if (is_numeric($ranges[0]) && is_numeric($ranges[1]) && $ranges[0] < $fileSize && $ranges[0] <= $ranges[1]) { |
|
241 | - // case: x-x |
|
242 | - if ($ranges[1] >= $fileSize) { |
|
243 | - $ranges[1] = $fileSize-1; |
|
244 | - } |
|
245 | - $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $ranges[1], 'size' => $fileSize ); |
|
246 | - $minOffset = $ranges[1] + 1; |
|
247 | - if ($minOffset >= $fileSize) { |
|
248 | - break; |
|
249 | - } |
|
250 | - } |
|
251 | - elseif (is_numeric($ranges[0]) && $ranges[0] < $fileSize) { |
|
252 | - // case: x- |
|
253 | - $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $fileSize-1, 'size' => $fileSize ); |
|
254 | - break; |
|
255 | - } |
|
256 | - elseif (is_numeric($ranges[1])) { |
|
257 | - // case: -x |
|
258 | - if ($ranges[1] > $fileSize) { |
|
259 | - $ranges[1] = $fileSize; |
|
260 | - } |
|
261 | - $rangeArray[$ind++] = array( 'from' => $fileSize-$ranges[1], 'to' => $fileSize-1, 'size' => $fileSize ); |
|
262 | - break; |
|
263 | - } |
|
264 | - } |
|
265 | - return $rangeArray; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * @param View $view |
|
270 | - * @param string $name |
|
271 | - * @param string $dir |
|
272 | - * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header |
|
273 | - */ |
|
274 | - private static function getSingleFile($view, $dir, $name, $params) { |
|
275 | - $filename = $dir . '/' . $name; |
|
276 | - OC_Util::obEnd(); |
|
277 | - $view->lockFile($filename, ILockingProvider::LOCK_SHARED); |
|
52 | + const FILE = 1; |
|
53 | + const ZIP_FILES = 2; |
|
54 | + const ZIP_DIR = 3; |
|
55 | + |
|
56 | + const UPLOAD_MIN_LIMIT_BYTES = 1048576; // 1 MiB |
|
57 | + |
|
58 | + |
|
59 | + private static $multipartBoundary = ''; |
|
60 | + |
|
61 | + /** |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + private static function getBoundary() { |
|
65 | + if (empty(self::$multipartBoundary)) { |
|
66 | + self::$multipartBoundary = md5(mt_rand()); |
|
67 | + } |
|
68 | + return self::$multipartBoundary; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param string $filename |
|
73 | + * @param string $name |
|
74 | + * @param array $rangeArray ('from'=>int,'to'=>int), ... |
|
75 | + */ |
|
76 | + private static function sendHeaders($filename, $name, array $rangeArray) { |
|
77 | + OC_Response::setContentDispositionHeader($name, 'attachment'); |
|
78 | + header('Content-Transfer-Encoding: binary', true); |
|
79 | + header('Pragma: public');// enable caching in IE |
|
80 | + header('Expires: 0'); |
|
81 | + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
82 | + $fileSize = \OC\Files\Filesystem::filesize($filename); |
|
83 | + $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); |
|
84 | + if ($fileSize > -1) { |
|
85 | + if (!empty($rangeArray)) { |
|
86 | + header('HTTP/1.1 206 Partial Content', true); |
|
87 | + header('Accept-Ranges: bytes', true); |
|
88 | + if (count($rangeArray) > 1) { |
|
89 | + $type = 'multipart/byteranges; boundary='.self::getBoundary(); |
|
90 | + // no Content-Length header here |
|
91 | + } |
|
92 | + else { |
|
93 | + header(sprintf('Content-Range: bytes %d-%d/%d', $rangeArray[0]['from'], $rangeArray[0]['to'], $fileSize), true); |
|
94 | + OC_Response::setContentLengthHeader($rangeArray[0]['to'] - $rangeArray[0]['from'] + 1); |
|
95 | + } |
|
96 | + } |
|
97 | + else { |
|
98 | + OC_Response::setContentLengthHeader($fileSize); |
|
99 | + } |
|
100 | + } |
|
101 | + header('Content-Type: '.$type, true); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * return the content of a file or return a zip file containing multiple files |
|
106 | + * |
|
107 | + * @param string $dir |
|
108 | + * @param string $files ; separated list of files to download |
|
109 | + * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header |
|
110 | + */ |
|
111 | + public static function get($dir, $files, $params = null) { |
|
112 | + |
|
113 | + $view = \OC\Files\Filesystem::getView(); |
|
114 | + $getType = self::FILE; |
|
115 | + $filename = $dir; |
|
116 | + try { |
|
117 | + |
|
118 | + if (is_array($files) && count($files) === 1) { |
|
119 | + $files = $files[0]; |
|
120 | + } |
|
121 | + |
|
122 | + if (!is_array($files)) { |
|
123 | + $filename = $dir . '/' . $files; |
|
124 | + if (!$view->is_dir($filename)) { |
|
125 | + self::getSingleFile($view, $dir, $files, is_null($params) ? array() : $params); |
|
126 | + return; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + $name = 'download'; |
|
131 | + if (is_array($files)) { |
|
132 | + $getType = self::ZIP_FILES; |
|
133 | + $basename = basename($dir); |
|
134 | + if ($basename) { |
|
135 | + $name = $basename; |
|
136 | + } |
|
137 | + |
|
138 | + $filename = $dir . '/' . $name; |
|
139 | + } else { |
|
140 | + $filename = $dir . '/' . $files; |
|
141 | + $getType = self::ZIP_DIR; |
|
142 | + // downloading root ? |
|
143 | + if ($files !== '') { |
|
144 | + $name = $files; |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + self::lockFiles($view, $dir, $files); |
|
149 | + |
|
150 | + /* Calculate filesize and number of files */ |
|
151 | + if ($getType === self::ZIP_FILES) { |
|
152 | + $fileInfos = array(); |
|
153 | + $fileSize = 0; |
|
154 | + foreach ($files as $file) { |
|
155 | + $fileInfo = \OC\Files\Filesystem::getFileInfo($dir . '/' . $file); |
|
156 | + $fileSize += $fileInfo->getSize(); |
|
157 | + $fileInfos[] = $fileInfo; |
|
158 | + } |
|
159 | + $numberOfFiles = self::getNumberOfFiles($fileInfos); |
|
160 | + } elseif ($getType === self::ZIP_DIR) { |
|
161 | + $fileInfo = \OC\Files\Filesystem::getFileInfo($dir . '/' . $files); |
|
162 | + $fileSize = $fileInfo->getSize(); |
|
163 | + $numberOfFiles = self::getNumberOfFiles(array($fileInfo)); |
|
164 | + } |
|
165 | + |
|
166 | + $streamer = new Streamer(\OC::$server->getRequest(), $fileSize, $numberOfFiles); |
|
167 | + OC_Util::obEnd(); |
|
168 | + |
|
169 | + $streamer->sendHeaders($name); |
|
170 | + $executionTime = (int)OC::$server->getIniWrapper()->getNumeric('max_execution_time'); |
|
171 | + if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
172 | + @set_time_limit(0); |
|
173 | + } |
|
174 | + ignore_user_abort(true); |
|
175 | + |
|
176 | + if ($getType === self::ZIP_FILES) { |
|
177 | + foreach ($files as $file) { |
|
178 | + $file = $dir . '/' . $file; |
|
179 | + if (\OC\Files\Filesystem::is_file($file)) { |
|
180 | + $fileSize = \OC\Files\Filesystem::filesize($file); |
|
181 | + $fileTime = \OC\Files\Filesystem::filemtime($file); |
|
182 | + $fh = \OC\Files\Filesystem::fopen($file, 'r'); |
|
183 | + $streamer->addFileFromStream($fh, basename($file), $fileSize, $fileTime); |
|
184 | + fclose($fh); |
|
185 | + } elseif (\OC\Files\Filesystem::is_dir($file)) { |
|
186 | + $streamer->addDirRecursive($file); |
|
187 | + } |
|
188 | + } |
|
189 | + } elseif ($getType === self::ZIP_DIR) { |
|
190 | + $file = $dir . '/' . $files; |
|
191 | + $streamer->addDirRecursive($file); |
|
192 | + } |
|
193 | + $streamer->finalize(); |
|
194 | + set_time_limit($executionTime); |
|
195 | + self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
196 | + } catch (\OCP\Lock\LockedException $ex) { |
|
197 | + self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
198 | + OC::$server->getLogger()->logException($ex); |
|
199 | + $l = \OC::$server->getL10N('core'); |
|
200 | + $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; |
|
201 | + \OC_Template::printErrorPage($l->t('File is currently busy, please try again later'), $hint); |
|
202 | + } catch (\OCP\Files\ForbiddenException $ex) { |
|
203 | + self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
204 | + OC::$server->getLogger()->logException($ex); |
|
205 | + $l = \OC::$server->getL10N('core'); |
|
206 | + \OC_Template::printErrorPage($l->t('Can\'t read file'), $ex->getMessage()); |
|
207 | + } catch (\Exception $ex) { |
|
208 | + self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); |
|
209 | + OC::$server->getLogger()->logException($ex); |
|
210 | + $l = \OC::$server->getL10N('core'); |
|
211 | + $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; |
|
212 | + \OC_Template::printErrorPage($l->t('Can\'t read file'), $hint); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @param string $rangeHeaderPos |
|
218 | + * @param int $fileSize |
|
219 | + * @return array $rangeArray ('from'=>int,'to'=>int), ... |
|
220 | + */ |
|
221 | + private static function parseHttpRangeHeader($rangeHeaderPos, $fileSize) { |
|
222 | + $rArray=explode(',', $rangeHeaderPos); |
|
223 | + $minOffset = 0; |
|
224 | + $ind = 0; |
|
225 | + |
|
226 | + $rangeArray = array(); |
|
227 | + |
|
228 | + foreach ($rArray as $value) { |
|
229 | + $ranges = explode('-', $value); |
|
230 | + if (is_numeric($ranges[0])) { |
|
231 | + if ($ranges[0] < $minOffset) { // case: bytes=500-700,601-999 |
|
232 | + $ranges[0] = $minOffset; |
|
233 | + } |
|
234 | + if ($ind > 0 && $rangeArray[$ind-1]['to']+1 == $ranges[0]) { // case: bytes=500-600,601-999 |
|
235 | + $ind--; |
|
236 | + $ranges[0] = $rangeArray[$ind]['from']; |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + if (is_numeric($ranges[0]) && is_numeric($ranges[1]) && $ranges[0] < $fileSize && $ranges[0] <= $ranges[1]) { |
|
241 | + // case: x-x |
|
242 | + if ($ranges[1] >= $fileSize) { |
|
243 | + $ranges[1] = $fileSize-1; |
|
244 | + } |
|
245 | + $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $ranges[1], 'size' => $fileSize ); |
|
246 | + $minOffset = $ranges[1] + 1; |
|
247 | + if ($minOffset >= $fileSize) { |
|
248 | + break; |
|
249 | + } |
|
250 | + } |
|
251 | + elseif (is_numeric($ranges[0]) && $ranges[0] < $fileSize) { |
|
252 | + // case: x- |
|
253 | + $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $fileSize-1, 'size' => $fileSize ); |
|
254 | + break; |
|
255 | + } |
|
256 | + elseif (is_numeric($ranges[1])) { |
|
257 | + // case: -x |
|
258 | + if ($ranges[1] > $fileSize) { |
|
259 | + $ranges[1] = $fileSize; |
|
260 | + } |
|
261 | + $rangeArray[$ind++] = array( 'from' => $fileSize-$ranges[1], 'to' => $fileSize-1, 'size' => $fileSize ); |
|
262 | + break; |
|
263 | + } |
|
264 | + } |
|
265 | + return $rangeArray; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * @param View $view |
|
270 | + * @param string $name |
|
271 | + * @param string $dir |
|
272 | + * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header |
|
273 | + */ |
|
274 | + private static function getSingleFile($view, $dir, $name, $params) { |
|
275 | + $filename = $dir . '/' . $name; |
|
276 | + OC_Util::obEnd(); |
|
277 | + $view->lockFile($filename, ILockingProvider::LOCK_SHARED); |
|
278 | 278 | |
279 | - $rangeArray = array(); |
|
279 | + $rangeArray = array(); |
|
280 | 280 | |
281 | - if (isset($params['range']) && substr($params['range'], 0, 6) === 'bytes=') { |
|
282 | - $rangeArray = self::parseHttpRangeHeader(substr($params['range'], 6), |
|
283 | - \OC\Files\Filesystem::filesize($filename)); |
|
284 | - } |
|
281 | + if (isset($params['range']) && substr($params['range'], 0, 6) === 'bytes=') { |
|
282 | + $rangeArray = self::parseHttpRangeHeader(substr($params['range'], 6), |
|
283 | + \OC\Files\Filesystem::filesize($filename)); |
|
284 | + } |
|
285 | 285 | |
286 | - if (\OC\Files\Filesystem::isReadable($filename)) { |
|
287 | - self::sendHeaders($filename, $name, $rangeArray); |
|
288 | - } elseif (!\OC\Files\Filesystem::file_exists($filename)) { |
|
289 | - header("HTTP/1.1 404 Not Found"); |
|
290 | - $tmpl = new OC_Template('', '404', 'guest'); |
|
291 | - $tmpl->printPage(); |
|
292 | - exit(); |
|
293 | - } else { |
|
294 | - header("HTTP/1.1 403 Forbidden"); |
|
295 | - die('403 Forbidden'); |
|
296 | - } |
|
297 | - if (isset($params['head']) && $params['head']) { |
|
298 | - return; |
|
299 | - } |
|
300 | - if (!empty($rangeArray)) { |
|
301 | - try { |
|
302 | - if (count($rangeArray) == 1) { |
|
303 | - $view->readfilePart($filename, $rangeArray[0]['from'], $rangeArray[0]['to']); |
|
304 | - } |
|
305 | - else { |
|
306 | - // check if file is seekable (if not throw UnseekableException) |
|
307 | - // we have to check it before body contents |
|
308 | - $view->readfilePart($filename, $rangeArray[0]['size'], $rangeArray[0]['size']); |
|
309 | - |
|
310 | - $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); |
|
311 | - |
|
312 | - foreach ($rangeArray as $range) { |
|
313 | - echo "\r\n--".self::getBoundary()."\r\n". |
|
314 | - "Content-type: ".$type."\r\n". |
|
315 | - "Content-range: bytes ".$range['from']."-".$range['to']."/".$range['size']."\r\n\r\n"; |
|
316 | - $view->readfilePart($filename, $range['from'], $range['to']); |
|
317 | - } |
|
318 | - echo "\r\n--".self::getBoundary()."--\r\n"; |
|
319 | - } |
|
320 | - } catch (\OCP\Files\UnseekableException $ex) { |
|
321 | - // file is unseekable |
|
322 | - header_remove('Accept-Ranges'); |
|
323 | - header_remove('Content-Range'); |
|
324 | - header("HTTP/1.1 200 OK"); |
|
325 | - self::sendHeaders($filename, $name, array()); |
|
326 | - $view->readfile($filename); |
|
327 | - } |
|
328 | - } |
|
329 | - else { |
|
330 | - $view->readfile($filename); |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Returns the total (recursive) number of files and folders in the given |
|
336 | - * FileInfos. |
|
337 | - * |
|
338 | - * @param \OCP\Files\FileInfo[] $fileInfos the FileInfos to count |
|
339 | - * @return int the total number of files and folders |
|
340 | - */ |
|
341 | - private static function getNumberOfFiles($fileInfos) { |
|
342 | - $numberOfFiles = 0; |
|
343 | - |
|
344 | - $view = new View(); |
|
345 | - |
|
346 | - while ($fileInfo = array_pop($fileInfos)) { |
|
347 | - $numberOfFiles++; |
|
348 | - |
|
349 | - if ($fileInfo->getType() === \OCP\Files\FileInfo::TYPE_FOLDER) { |
|
350 | - $fileInfos = array_merge($fileInfos, $view->getDirectoryContent($fileInfo->getPath())); |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - return $numberOfFiles; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * @param View $view |
|
359 | - * @param string $dir |
|
360 | - * @param string[]|string $files |
|
361 | - */ |
|
362 | - public static function lockFiles($view, $dir, $files) { |
|
363 | - if (!is_array($files)) { |
|
364 | - $file = $dir . '/' . $files; |
|
365 | - $files = [$file]; |
|
366 | - } |
|
367 | - foreach ($files as $file) { |
|
368 | - $file = $dir . '/' . $file; |
|
369 | - $view->lockFile($file, ILockingProvider::LOCK_SHARED); |
|
370 | - if ($view->is_dir($file)) { |
|
371 | - $contents = $view->getDirectoryContent($file); |
|
372 | - $contents = array_map(function($fileInfo) use ($file) { |
|
373 | - /** @var \OCP\Files\FileInfo $fileInfo */ |
|
374 | - return $file . '/' . $fileInfo->getName(); |
|
375 | - }, $contents); |
|
376 | - self::lockFiles($view, $dir, $contents); |
|
377 | - } |
|
378 | - } |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * set the maximum upload size limit for apache hosts using .htaccess |
|
383 | - * |
|
384 | - * @param int $size file size in bytes |
|
385 | - * @param array $files override '.htaccess' and '.user.ini' locations |
|
386 | - * @return bool|int false on failure, size on success |
|
387 | - */ |
|
388 | - public static function setUploadLimit($size, $files = []) { |
|
389 | - //don't allow user to break his config |
|
390 | - $size = (int)$size; |
|
391 | - if ($size < self::UPLOAD_MIN_LIMIT_BYTES) { |
|
392 | - return false; |
|
393 | - } |
|
394 | - $size = OC_Helper::phpFileSize($size); |
|
395 | - |
|
396 | - $phpValueKeys = array( |
|
397 | - 'upload_max_filesize', |
|
398 | - 'post_max_size' |
|
399 | - ); |
|
400 | - |
|
401 | - // default locations if not overridden by $files |
|
402 | - $files = array_merge([ |
|
403 | - '.htaccess' => OC::$SERVERROOT . '/.htaccess', |
|
404 | - '.user.ini' => OC::$SERVERROOT . '/.user.ini' |
|
405 | - ], $files); |
|
406 | - |
|
407 | - $updateFiles = [ |
|
408 | - $files['.htaccess'] => [ |
|
409 | - 'pattern' => '/php_value %1$s (\S)*/', |
|
410 | - 'setting' => 'php_value %1$s %2$s' |
|
411 | - ], |
|
412 | - $files['.user.ini'] => [ |
|
413 | - 'pattern' => '/%1$s=(\S)*/', |
|
414 | - 'setting' => '%1$s=%2$s' |
|
415 | - ] |
|
416 | - ]; |
|
417 | - |
|
418 | - $success = true; |
|
419 | - |
|
420 | - foreach ($updateFiles as $filename => $patternMap) { |
|
421 | - // suppress warnings from fopen() |
|
422 | - $handle = @fopen($filename, 'r+'); |
|
423 | - if (!$handle) { |
|
424 | - \OCP\Util::writeLog('files', |
|
425 | - 'Can\'t write upload limit to ' . $filename . '. Please check the file permissions', |
|
426 | - ILogger::WARN); |
|
427 | - $success = false; |
|
428 | - continue; // try to update as many files as possible |
|
429 | - } |
|
430 | - |
|
431 | - $content = ''; |
|
432 | - while (!feof($handle)) { |
|
433 | - $content .= fread($handle, 1000); |
|
434 | - } |
|
435 | - |
|
436 | - foreach ($phpValueKeys as $key) { |
|
437 | - $pattern = vsprintf($patternMap['pattern'], [$key]); |
|
438 | - $setting = vsprintf($patternMap['setting'], [$key, $size]); |
|
439 | - $hasReplaced = 0; |
|
440 | - $newContent = preg_replace($pattern, $setting, $content, 2, $hasReplaced); |
|
441 | - if ($newContent !== null) { |
|
442 | - $content = $newContent; |
|
443 | - } |
|
444 | - if ($hasReplaced === 0) { |
|
445 | - $content .= "\n" . $setting; |
|
446 | - } |
|
447 | - } |
|
448 | - |
|
449 | - // write file back |
|
450 | - ftruncate($handle, 0); |
|
451 | - rewind($handle); |
|
452 | - fwrite($handle, $content); |
|
453 | - |
|
454 | - fclose($handle); |
|
455 | - } |
|
456 | - |
|
457 | - if ($success) { |
|
458 | - return OC_Helper::computerFileSize($size); |
|
459 | - } |
|
460 | - return false; |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * @param string $dir |
|
465 | - * @param $files |
|
466 | - * @param integer $getType |
|
467 | - * @param View $view |
|
468 | - * @param string $filename |
|
469 | - */ |
|
470 | - private static function unlockAllTheFiles($dir, $files, $getType, $view, $filename) { |
|
471 | - if ($getType === self::FILE) { |
|
472 | - $view->unlockFile($filename, ILockingProvider::LOCK_SHARED); |
|
473 | - } |
|
474 | - if ($getType === self::ZIP_FILES) { |
|
475 | - foreach ($files as $file) { |
|
476 | - $file = $dir . '/' . $file; |
|
477 | - $view->unlockFile($file, ILockingProvider::LOCK_SHARED); |
|
478 | - } |
|
479 | - } |
|
480 | - if ($getType === self::ZIP_DIR) { |
|
481 | - $file = $dir . '/' . $files; |
|
482 | - $view->unlockFile($file, ILockingProvider::LOCK_SHARED); |
|
483 | - } |
|
484 | - } |
|
286 | + if (\OC\Files\Filesystem::isReadable($filename)) { |
|
287 | + self::sendHeaders($filename, $name, $rangeArray); |
|
288 | + } elseif (!\OC\Files\Filesystem::file_exists($filename)) { |
|
289 | + header("HTTP/1.1 404 Not Found"); |
|
290 | + $tmpl = new OC_Template('', '404', 'guest'); |
|
291 | + $tmpl->printPage(); |
|
292 | + exit(); |
|
293 | + } else { |
|
294 | + header("HTTP/1.1 403 Forbidden"); |
|
295 | + die('403 Forbidden'); |
|
296 | + } |
|
297 | + if (isset($params['head']) && $params['head']) { |
|
298 | + return; |
|
299 | + } |
|
300 | + if (!empty($rangeArray)) { |
|
301 | + try { |
|
302 | + if (count($rangeArray) == 1) { |
|
303 | + $view->readfilePart($filename, $rangeArray[0]['from'], $rangeArray[0]['to']); |
|
304 | + } |
|
305 | + else { |
|
306 | + // check if file is seekable (if not throw UnseekableException) |
|
307 | + // we have to check it before body contents |
|
308 | + $view->readfilePart($filename, $rangeArray[0]['size'], $rangeArray[0]['size']); |
|
309 | + |
|
310 | + $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); |
|
311 | + |
|
312 | + foreach ($rangeArray as $range) { |
|
313 | + echo "\r\n--".self::getBoundary()."\r\n". |
|
314 | + "Content-type: ".$type."\r\n". |
|
315 | + "Content-range: bytes ".$range['from']."-".$range['to']."/".$range['size']."\r\n\r\n"; |
|
316 | + $view->readfilePart($filename, $range['from'], $range['to']); |
|
317 | + } |
|
318 | + echo "\r\n--".self::getBoundary()."--\r\n"; |
|
319 | + } |
|
320 | + } catch (\OCP\Files\UnseekableException $ex) { |
|
321 | + // file is unseekable |
|
322 | + header_remove('Accept-Ranges'); |
|
323 | + header_remove('Content-Range'); |
|
324 | + header("HTTP/1.1 200 OK"); |
|
325 | + self::sendHeaders($filename, $name, array()); |
|
326 | + $view->readfile($filename); |
|
327 | + } |
|
328 | + } |
|
329 | + else { |
|
330 | + $view->readfile($filename); |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Returns the total (recursive) number of files and folders in the given |
|
336 | + * FileInfos. |
|
337 | + * |
|
338 | + * @param \OCP\Files\FileInfo[] $fileInfos the FileInfos to count |
|
339 | + * @return int the total number of files and folders |
|
340 | + */ |
|
341 | + private static function getNumberOfFiles($fileInfos) { |
|
342 | + $numberOfFiles = 0; |
|
343 | + |
|
344 | + $view = new View(); |
|
345 | + |
|
346 | + while ($fileInfo = array_pop($fileInfos)) { |
|
347 | + $numberOfFiles++; |
|
348 | + |
|
349 | + if ($fileInfo->getType() === \OCP\Files\FileInfo::TYPE_FOLDER) { |
|
350 | + $fileInfos = array_merge($fileInfos, $view->getDirectoryContent($fileInfo->getPath())); |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + return $numberOfFiles; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * @param View $view |
|
359 | + * @param string $dir |
|
360 | + * @param string[]|string $files |
|
361 | + */ |
|
362 | + public static function lockFiles($view, $dir, $files) { |
|
363 | + if (!is_array($files)) { |
|
364 | + $file = $dir . '/' . $files; |
|
365 | + $files = [$file]; |
|
366 | + } |
|
367 | + foreach ($files as $file) { |
|
368 | + $file = $dir . '/' . $file; |
|
369 | + $view->lockFile($file, ILockingProvider::LOCK_SHARED); |
|
370 | + if ($view->is_dir($file)) { |
|
371 | + $contents = $view->getDirectoryContent($file); |
|
372 | + $contents = array_map(function($fileInfo) use ($file) { |
|
373 | + /** @var \OCP\Files\FileInfo $fileInfo */ |
|
374 | + return $file . '/' . $fileInfo->getName(); |
|
375 | + }, $contents); |
|
376 | + self::lockFiles($view, $dir, $contents); |
|
377 | + } |
|
378 | + } |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * set the maximum upload size limit for apache hosts using .htaccess |
|
383 | + * |
|
384 | + * @param int $size file size in bytes |
|
385 | + * @param array $files override '.htaccess' and '.user.ini' locations |
|
386 | + * @return bool|int false on failure, size on success |
|
387 | + */ |
|
388 | + public static function setUploadLimit($size, $files = []) { |
|
389 | + //don't allow user to break his config |
|
390 | + $size = (int)$size; |
|
391 | + if ($size < self::UPLOAD_MIN_LIMIT_BYTES) { |
|
392 | + return false; |
|
393 | + } |
|
394 | + $size = OC_Helper::phpFileSize($size); |
|
395 | + |
|
396 | + $phpValueKeys = array( |
|
397 | + 'upload_max_filesize', |
|
398 | + 'post_max_size' |
|
399 | + ); |
|
400 | + |
|
401 | + // default locations if not overridden by $files |
|
402 | + $files = array_merge([ |
|
403 | + '.htaccess' => OC::$SERVERROOT . '/.htaccess', |
|
404 | + '.user.ini' => OC::$SERVERROOT . '/.user.ini' |
|
405 | + ], $files); |
|
406 | + |
|
407 | + $updateFiles = [ |
|
408 | + $files['.htaccess'] => [ |
|
409 | + 'pattern' => '/php_value %1$s (\S)*/', |
|
410 | + 'setting' => 'php_value %1$s %2$s' |
|
411 | + ], |
|
412 | + $files['.user.ini'] => [ |
|
413 | + 'pattern' => '/%1$s=(\S)*/', |
|
414 | + 'setting' => '%1$s=%2$s' |
|
415 | + ] |
|
416 | + ]; |
|
417 | + |
|
418 | + $success = true; |
|
419 | + |
|
420 | + foreach ($updateFiles as $filename => $patternMap) { |
|
421 | + // suppress warnings from fopen() |
|
422 | + $handle = @fopen($filename, 'r+'); |
|
423 | + if (!$handle) { |
|
424 | + \OCP\Util::writeLog('files', |
|
425 | + 'Can\'t write upload limit to ' . $filename . '. Please check the file permissions', |
|
426 | + ILogger::WARN); |
|
427 | + $success = false; |
|
428 | + continue; // try to update as many files as possible |
|
429 | + } |
|
430 | + |
|
431 | + $content = ''; |
|
432 | + while (!feof($handle)) { |
|
433 | + $content .= fread($handle, 1000); |
|
434 | + } |
|
435 | + |
|
436 | + foreach ($phpValueKeys as $key) { |
|
437 | + $pattern = vsprintf($patternMap['pattern'], [$key]); |
|
438 | + $setting = vsprintf($patternMap['setting'], [$key, $size]); |
|
439 | + $hasReplaced = 0; |
|
440 | + $newContent = preg_replace($pattern, $setting, $content, 2, $hasReplaced); |
|
441 | + if ($newContent !== null) { |
|
442 | + $content = $newContent; |
|
443 | + } |
|
444 | + if ($hasReplaced === 0) { |
|
445 | + $content .= "\n" . $setting; |
|
446 | + } |
|
447 | + } |
|
448 | + |
|
449 | + // write file back |
|
450 | + ftruncate($handle, 0); |
|
451 | + rewind($handle); |
|
452 | + fwrite($handle, $content); |
|
453 | + |
|
454 | + fclose($handle); |
|
455 | + } |
|
456 | + |
|
457 | + if ($success) { |
|
458 | + return OC_Helper::computerFileSize($size); |
|
459 | + } |
|
460 | + return false; |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * @param string $dir |
|
465 | + * @param $files |
|
466 | + * @param integer $getType |
|
467 | + * @param View $view |
|
468 | + * @param string $filename |
|
469 | + */ |
|
470 | + private static function unlockAllTheFiles($dir, $files, $getType, $view, $filename) { |
|
471 | + if ($getType === self::FILE) { |
|
472 | + $view->unlockFile($filename, ILockingProvider::LOCK_SHARED); |
|
473 | + } |
|
474 | + if ($getType === self::ZIP_FILES) { |
|
475 | + foreach ($files as $file) { |
|
476 | + $file = $dir . '/' . $file; |
|
477 | + $view->unlockFile($file, ILockingProvider::LOCK_SHARED); |
|
478 | + } |
|
479 | + } |
|
480 | + if ($getType === self::ZIP_DIR) { |
|
481 | + $file = $dir . '/' . $files; |
|
482 | + $view->unlockFile($file, ILockingProvider::LOCK_SHARED); |
|
483 | + } |
|
484 | + } |
|
485 | 485 | |
486 | 486 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | private static function sendHeaders($filename, $name, array $rangeArray) { |
77 | 77 | OC_Response::setContentDispositionHeader($name, 'attachment'); |
78 | 78 | header('Content-Transfer-Encoding: binary', true); |
79 | - header('Pragma: public');// enable caching in IE |
|
79 | + header('Pragma: public'); // enable caching in IE |
|
80 | 80 | header('Expires: 0'); |
81 | 81 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
82 | 82 | $fileSize = \OC\Files\Filesystem::filesize($filename); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | } |
121 | 121 | |
122 | 122 | if (!is_array($files)) { |
123 | - $filename = $dir . '/' . $files; |
|
123 | + $filename = $dir.'/'.$files; |
|
124 | 124 | if (!$view->is_dir($filename)) { |
125 | 125 | self::getSingleFile($view, $dir, $files, is_null($params) ? array() : $params); |
126 | 126 | return; |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | $name = $basename; |
136 | 136 | } |
137 | 137 | |
138 | - $filename = $dir . '/' . $name; |
|
138 | + $filename = $dir.'/'.$name; |
|
139 | 139 | } else { |
140 | - $filename = $dir . '/' . $files; |
|
140 | + $filename = $dir.'/'.$files; |
|
141 | 141 | $getType = self::ZIP_DIR; |
142 | 142 | // downloading root ? |
143 | 143 | if ($files !== '') { |
@@ -152,13 +152,13 @@ discard block |
||
152 | 152 | $fileInfos = array(); |
153 | 153 | $fileSize = 0; |
154 | 154 | foreach ($files as $file) { |
155 | - $fileInfo = \OC\Files\Filesystem::getFileInfo($dir . '/' . $file); |
|
155 | + $fileInfo = \OC\Files\Filesystem::getFileInfo($dir.'/'.$file); |
|
156 | 156 | $fileSize += $fileInfo->getSize(); |
157 | 157 | $fileInfos[] = $fileInfo; |
158 | 158 | } |
159 | 159 | $numberOfFiles = self::getNumberOfFiles($fileInfos); |
160 | 160 | } elseif ($getType === self::ZIP_DIR) { |
161 | - $fileInfo = \OC\Files\Filesystem::getFileInfo($dir . '/' . $files); |
|
161 | + $fileInfo = \OC\Files\Filesystem::getFileInfo($dir.'/'.$files); |
|
162 | 162 | $fileSize = $fileInfo->getSize(); |
163 | 163 | $numberOfFiles = self::getNumberOfFiles(array($fileInfo)); |
164 | 164 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | OC_Util::obEnd(); |
168 | 168 | |
169 | 169 | $streamer->sendHeaders($name); |
170 | - $executionTime = (int)OC::$server->getIniWrapper()->getNumeric('max_execution_time'); |
|
170 | + $executionTime = (int) OC::$server->getIniWrapper()->getNumeric('max_execution_time'); |
|
171 | 171 | if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
172 | 172 | @set_time_limit(0); |
173 | 173 | } |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | |
176 | 176 | if ($getType === self::ZIP_FILES) { |
177 | 177 | foreach ($files as $file) { |
178 | - $file = $dir . '/' . $file; |
|
178 | + $file = $dir.'/'.$file; |
|
179 | 179 | if (\OC\Files\Filesystem::is_file($file)) { |
180 | 180 | $fileSize = \OC\Files\Filesystem::filesize($file); |
181 | 181 | $fileTime = \OC\Files\Filesystem::filemtime($file); |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | } |
188 | 188 | } |
189 | 189 | } elseif ($getType === self::ZIP_DIR) { |
190 | - $file = $dir . '/' . $files; |
|
190 | + $file = $dir.'/'.$files; |
|
191 | 191 | $streamer->addDirRecursive($file); |
192 | 192 | } |
193 | 193 | $streamer->finalize(); |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | * @return array $rangeArray ('from'=>int,'to'=>int), ... |
220 | 220 | */ |
221 | 221 | private static function parseHttpRangeHeader($rangeHeaderPos, $fileSize) { |
222 | - $rArray=explode(',', $rangeHeaderPos); |
|
222 | + $rArray = explode(',', $rangeHeaderPos); |
|
223 | 223 | $minOffset = 0; |
224 | 224 | $ind = 0; |
225 | 225 | |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | if ($ranges[0] < $minOffset) { // case: bytes=500-700,601-999 |
232 | 232 | $ranges[0] = $minOffset; |
233 | 233 | } |
234 | - if ($ind > 0 && $rangeArray[$ind-1]['to']+1 == $ranges[0]) { // case: bytes=500-600,601-999 |
|
234 | + if ($ind > 0 && $rangeArray[$ind - 1]['to'] + 1 == $ranges[0]) { // case: bytes=500-600,601-999 |
|
235 | 235 | $ind--; |
236 | 236 | $ranges[0] = $rangeArray[$ind]['from']; |
237 | 237 | } |
@@ -240,9 +240,9 @@ discard block |
||
240 | 240 | if (is_numeric($ranges[0]) && is_numeric($ranges[1]) && $ranges[0] < $fileSize && $ranges[0] <= $ranges[1]) { |
241 | 241 | // case: x-x |
242 | 242 | if ($ranges[1] >= $fileSize) { |
243 | - $ranges[1] = $fileSize-1; |
|
243 | + $ranges[1] = $fileSize - 1; |
|
244 | 244 | } |
245 | - $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $ranges[1], 'size' => $fileSize ); |
|
245 | + $rangeArray[$ind++] = array('from' => $ranges[0], 'to' => $ranges[1], 'size' => $fileSize); |
|
246 | 246 | $minOffset = $ranges[1] + 1; |
247 | 247 | if ($minOffset >= $fileSize) { |
248 | 248 | break; |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | } |
251 | 251 | elseif (is_numeric($ranges[0]) && $ranges[0] < $fileSize) { |
252 | 252 | // case: x- |
253 | - $rangeArray[$ind++] = array( 'from' => $ranges[0], 'to' => $fileSize-1, 'size' => $fileSize ); |
|
253 | + $rangeArray[$ind++] = array('from' => $ranges[0], 'to' => $fileSize - 1, 'size' => $fileSize); |
|
254 | 254 | break; |
255 | 255 | } |
256 | 256 | elseif (is_numeric($ranges[1])) { |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | if ($ranges[1] > $fileSize) { |
259 | 259 | $ranges[1] = $fileSize; |
260 | 260 | } |
261 | - $rangeArray[$ind++] = array( 'from' => $fileSize-$ranges[1], 'to' => $fileSize-1, 'size' => $fileSize ); |
|
261 | + $rangeArray[$ind++] = array('from' => $fileSize - $ranges[1], 'to' => $fileSize - 1, 'size' => $fileSize); |
|
262 | 262 | break; |
263 | 263 | } |
264 | 264 | } |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | * @param array $params ; 'head' boolean to only send header of the request ; 'range' http range header |
273 | 273 | */ |
274 | 274 | private static function getSingleFile($view, $dir, $name, $params) { |
275 | - $filename = $dir . '/' . $name; |
|
275 | + $filename = $dir.'/'.$name; |
|
276 | 276 | OC_Util::obEnd(); |
277 | 277 | $view->lockFile($filename, ILockingProvider::LOCK_SHARED); |
278 | 278 | |
@@ -361,17 +361,17 @@ discard block |
||
361 | 361 | */ |
362 | 362 | public static function lockFiles($view, $dir, $files) { |
363 | 363 | if (!is_array($files)) { |
364 | - $file = $dir . '/' . $files; |
|
364 | + $file = $dir.'/'.$files; |
|
365 | 365 | $files = [$file]; |
366 | 366 | } |
367 | 367 | foreach ($files as $file) { |
368 | - $file = $dir . '/' . $file; |
|
368 | + $file = $dir.'/'.$file; |
|
369 | 369 | $view->lockFile($file, ILockingProvider::LOCK_SHARED); |
370 | 370 | if ($view->is_dir($file)) { |
371 | 371 | $contents = $view->getDirectoryContent($file); |
372 | 372 | $contents = array_map(function($fileInfo) use ($file) { |
373 | 373 | /** @var \OCP\Files\FileInfo $fileInfo */ |
374 | - return $file . '/' . $fileInfo->getName(); |
|
374 | + return $file.'/'.$fileInfo->getName(); |
|
375 | 375 | }, $contents); |
376 | 376 | self::lockFiles($view, $dir, $contents); |
377 | 377 | } |
@@ -387,7 +387,7 @@ discard block |
||
387 | 387 | */ |
388 | 388 | public static function setUploadLimit($size, $files = []) { |
389 | 389 | //don't allow user to break his config |
390 | - $size = (int)$size; |
|
390 | + $size = (int) $size; |
|
391 | 391 | if ($size < self::UPLOAD_MIN_LIMIT_BYTES) { |
392 | 392 | return false; |
393 | 393 | } |
@@ -400,8 +400,8 @@ discard block |
||
400 | 400 | |
401 | 401 | // default locations if not overridden by $files |
402 | 402 | $files = array_merge([ |
403 | - '.htaccess' => OC::$SERVERROOT . '/.htaccess', |
|
404 | - '.user.ini' => OC::$SERVERROOT . '/.user.ini' |
|
403 | + '.htaccess' => OC::$SERVERROOT.'/.htaccess', |
|
404 | + '.user.ini' => OC::$SERVERROOT.'/.user.ini' |
|
405 | 405 | ], $files); |
406 | 406 | |
407 | 407 | $updateFiles = [ |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | $handle = @fopen($filename, 'r+'); |
423 | 423 | if (!$handle) { |
424 | 424 | \OCP\Util::writeLog('files', |
425 | - 'Can\'t write upload limit to ' . $filename . '. Please check the file permissions', |
|
425 | + 'Can\'t write upload limit to '.$filename.'. Please check the file permissions', |
|
426 | 426 | ILogger::WARN); |
427 | 427 | $success = false; |
428 | 428 | continue; // try to update as many files as possible |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | $content = $newContent; |
443 | 443 | } |
444 | 444 | if ($hasReplaced === 0) { |
445 | - $content .= "\n" . $setting; |
|
445 | + $content .= "\n".$setting; |
|
446 | 446 | } |
447 | 447 | } |
448 | 448 | |
@@ -473,12 +473,12 @@ discard block |
||
473 | 473 | } |
474 | 474 | if ($getType === self::ZIP_FILES) { |
475 | 475 | foreach ($files as $file) { |
476 | - $file = $dir . '/' . $file; |
|
476 | + $file = $dir.'/'.$file; |
|
477 | 477 | $view->unlockFile($file, ILockingProvider::LOCK_SHARED); |
478 | 478 | } |
479 | 479 | } |
480 | 480 | if ($getType === self::ZIP_DIR) { |
481 | - $file = $dir . '/' . $files; |
|
481 | + $file = $dir.'/'.$files; |
|
482 | 482 | $view->unlockFile($file, ILockingProvider::LOCK_SHARED); |
483 | 483 | } |
484 | 484 | } |
@@ -66,1440 +66,1440 @@ |
||
66 | 66 | use OCP\IUser; |
67 | 67 | |
68 | 68 | class OC_Util { |
69 | - public static $scripts = array(); |
|
70 | - public static $styles = array(); |
|
71 | - public static $headers = array(); |
|
72 | - private static $rootMounted = false; |
|
73 | - private static $fsSetup = false; |
|
74 | - |
|
75 | - /** @var array Local cache of version.php */ |
|
76 | - private static $versionCache = null; |
|
77 | - |
|
78 | - protected static function getAppManager() { |
|
79 | - return \OC::$server->getAppManager(); |
|
80 | - } |
|
81 | - |
|
82 | - private static function initLocalStorageRootFS() { |
|
83 | - // mount local file backend as root |
|
84 | - $configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data"); |
|
85 | - //first set up the local "root" storage |
|
86 | - \OC\Files\Filesystem::initMountManager(); |
|
87 | - if (!self::$rootMounted) { |
|
88 | - \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/'); |
|
89 | - self::$rootMounted = true; |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * mounting an object storage as the root fs will in essence remove the |
|
95 | - * necessity of a data folder being present. |
|
96 | - * TODO make home storage aware of this and use the object storage instead of local disk access |
|
97 | - * |
|
98 | - * @param array $config containing 'class' and optional 'arguments' |
|
99 | - * @suppress PhanDeprecatedFunction |
|
100 | - */ |
|
101 | - private static function initObjectStoreRootFS($config) { |
|
102 | - // check misconfiguration |
|
103 | - if (empty($config['class'])) { |
|
104 | - \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
105 | - } |
|
106 | - if (!isset($config['arguments'])) { |
|
107 | - $config['arguments'] = array(); |
|
108 | - } |
|
109 | - |
|
110 | - // instantiate object store implementation |
|
111 | - $name = $config['class']; |
|
112 | - if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { |
|
113 | - $segments = explode('\\', $name); |
|
114 | - OC_App::loadApp(strtolower($segments[1])); |
|
115 | - } |
|
116 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
117 | - // mount with plain / root object store implementation |
|
118 | - $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; |
|
119 | - |
|
120 | - // mount object storage as root |
|
121 | - \OC\Files\Filesystem::initMountManager(); |
|
122 | - if (!self::$rootMounted) { |
|
123 | - \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); |
|
124 | - self::$rootMounted = true; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * mounting an object storage as the root fs will in essence remove the |
|
130 | - * necessity of a data folder being present. |
|
131 | - * |
|
132 | - * @param array $config containing 'class' and optional 'arguments' |
|
133 | - * @suppress PhanDeprecatedFunction |
|
134 | - */ |
|
135 | - private static function initObjectStoreMultibucketRootFS($config) { |
|
136 | - // check misconfiguration |
|
137 | - if (empty($config['class'])) { |
|
138 | - \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
139 | - } |
|
140 | - if (!isset($config['arguments'])) { |
|
141 | - $config['arguments'] = array(); |
|
142 | - } |
|
143 | - |
|
144 | - // instantiate object store implementation |
|
145 | - $name = $config['class']; |
|
146 | - if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { |
|
147 | - $segments = explode('\\', $name); |
|
148 | - OC_App::loadApp(strtolower($segments[1])); |
|
149 | - } |
|
150 | - |
|
151 | - if (!isset($config['arguments']['bucket'])) { |
|
152 | - $config['arguments']['bucket'] = ''; |
|
153 | - } |
|
154 | - // put the root FS always in first bucket for multibucket configuration |
|
155 | - $config['arguments']['bucket'] .= '0'; |
|
156 | - |
|
157 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
158 | - // mount with plain / root object store implementation |
|
159 | - $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; |
|
160 | - |
|
161 | - // mount object storage as root |
|
162 | - \OC\Files\Filesystem::initMountManager(); |
|
163 | - if (!self::$rootMounted) { |
|
164 | - \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); |
|
165 | - self::$rootMounted = true; |
|
166 | - } |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Can be set up |
|
171 | - * |
|
172 | - * @param string $user |
|
173 | - * @return boolean |
|
174 | - * @description configure the initial filesystem based on the configuration |
|
175 | - * @suppress PhanDeprecatedFunction |
|
176 | - * @suppress PhanAccessMethodInternal |
|
177 | - */ |
|
178 | - public static function setupFS($user = '') { |
|
179 | - //setting up the filesystem twice can only lead to trouble |
|
180 | - if (self::$fsSetup) { |
|
181 | - return false; |
|
182 | - } |
|
183 | - |
|
184 | - \OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem'); |
|
185 | - |
|
186 | - // If we are not forced to load a specific user we load the one that is logged in |
|
187 | - if ($user === null) { |
|
188 | - $user = ''; |
|
189 | - } else if ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) { |
|
190 | - $user = OC_User::getUser(); |
|
191 | - } |
|
192 | - |
|
193 | - // load all filesystem apps before, so no setup-hook gets lost |
|
194 | - OC_App::loadApps(array('filesystem')); |
|
195 | - |
|
196 | - // the filesystem will finish when $user is not empty, |
|
197 | - // mark fs setup here to avoid doing the setup from loading |
|
198 | - // OC_Filesystem |
|
199 | - if ($user != '') { |
|
200 | - self::$fsSetup = true; |
|
201 | - } |
|
202 | - |
|
203 | - \OC\Files\Filesystem::initMountManager(); |
|
204 | - |
|
205 | - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
206 | - \OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
|
207 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) { |
|
208 | - /** @var \OC\Files\Storage\Common $storage */ |
|
209 | - $storage->setMountOptions($mount->getOptions()); |
|
210 | - } |
|
211 | - return $storage; |
|
212 | - }); |
|
213 | - |
|
214 | - \OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
|
215 | - if (!$mount->getOption('enable_sharing', true)) { |
|
216 | - return new \OC\Files\Storage\Wrapper\PermissionsMask([ |
|
217 | - 'storage' => $storage, |
|
218 | - 'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE |
|
219 | - ]); |
|
220 | - } |
|
221 | - return $storage; |
|
222 | - }); |
|
223 | - |
|
224 | - // install storage availability wrapper, before most other wrappers |
|
225 | - \OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, \OCP\Files\Storage\IStorage $storage) { |
|
226 | - if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { |
|
227 | - return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]); |
|
228 | - } |
|
229 | - return $storage; |
|
230 | - }); |
|
231 | - |
|
232 | - \OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
|
233 | - if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { |
|
234 | - return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]); |
|
235 | - } |
|
236 | - return $storage; |
|
237 | - }); |
|
238 | - |
|
239 | - \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { |
|
240 | - // set up quota for home storages, even for other users |
|
241 | - // which can happen when using sharing |
|
242 | - |
|
243 | - /** |
|
244 | - * @var \OC\Files\Storage\Storage $storage |
|
245 | - */ |
|
246 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
247 | - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
248 | - ) { |
|
249 | - /** @var \OC\Files\Storage\Home $storage */ |
|
250 | - if (is_object($storage->getUser())) { |
|
251 | - $user = $storage->getUser()->getUID(); |
|
252 | - $quota = OC_Util::getUserQuota($user); |
|
253 | - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
254 | - return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); |
|
255 | - } |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - return $storage; |
|
260 | - }); |
|
261 | - |
|
262 | - OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); |
|
263 | - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true); |
|
264 | - |
|
265 | - //check if we are using an object storage |
|
266 | - $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null); |
|
267 | - $objectStoreMultibucket = \OC::$server->getSystemConfig()->getValue('objectstore_multibucket', null); |
|
268 | - |
|
269 | - // use the same order as in ObjectHomeMountProvider |
|
270 | - if (isset($objectStoreMultibucket)) { |
|
271 | - self::initObjectStoreMultibucketRootFS($objectStoreMultibucket); |
|
272 | - } elseif (isset($objectStore)) { |
|
273 | - self::initObjectStoreRootFS($objectStore); |
|
274 | - } else { |
|
275 | - self::initLocalStorageRootFS(); |
|
276 | - } |
|
277 | - |
|
278 | - if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) { |
|
279 | - \OC::$server->getEventLogger()->end('setup_fs'); |
|
280 | - return false; |
|
281 | - } |
|
282 | - |
|
283 | - //if we aren't logged in, there is no use to set up the filesystem |
|
284 | - if ($user != "") { |
|
285 | - |
|
286 | - $userDir = '/' . $user . '/files'; |
|
287 | - |
|
288 | - //jail the user into his "home" directory |
|
289 | - \OC\Files\Filesystem::init($user, $userDir); |
|
290 | - |
|
291 | - OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir)); |
|
292 | - } |
|
293 | - \OC::$server->getEventLogger()->end('setup_fs'); |
|
294 | - return true; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * check if a password is required for each public link |
|
299 | - * |
|
300 | - * @return boolean |
|
301 | - * @suppress PhanDeprecatedFunction |
|
302 | - */ |
|
303 | - public static function isPublicLinkPasswordRequired() { |
|
304 | - $enforcePassword = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_links_password', 'no'); |
|
305 | - return $enforcePassword === 'yes'; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * check if sharing is disabled for the current user |
|
310 | - * @param IConfig $config |
|
311 | - * @param IGroupManager $groupManager |
|
312 | - * @param IUser|null $user |
|
313 | - * @return bool |
|
314 | - */ |
|
315 | - public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { |
|
316 | - if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
317 | - $groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
318 | - $excludedGroups = json_decode($groupsList); |
|
319 | - if (is_null($excludedGroups)) { |
|
320 | - $excludedGroups = explode(',', $groupsList); |
|
321 | - $newValue = json_encode($excludedGroups); |
|
322 | - $config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
323 | - } |
|
324 | - $usersGroups = $groupManager->getUserGroupIds($user); |
|
325 | - if (!empty($usersGroups)) { |
|
326 | - $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
327 | - // if the user is only in groups which are disabled for sharing then |
|
328 | - // sharing is also disabled for the user |
|
329 | - if (empty($remainingGroups)) { |
|
330 | - return true; |
|
331 | - } |
|
332 | - } |
|
333 | - } |
|
334 | - return false; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * check if share API enforces a default expire date |
|
339 | - * |
|
340 | - * @return boolean |
|
341 | - * @suppress PhanDeprecatedFunction |
|
342 | - */ |
|
343 | - public static function isDefaultExpireDateEnforced() { |
|
344 | - $isDefaultExpireDateEnabled = \OC::$server->getConfig()->getAppValue('core', 'shareapi_default_expire_date', 'no'); |
|
345 | - $enforceDefaultExpireDate = false; |
|
346 | - if ($isDefaultExpireDateEnabled === 'yes') { |
|
347 | - $value = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_expire_date', 'no'); |
|
348 | - $enforceDefaultExpireDate = $value === 'yes'; |
|
349 | - } |
|
350 | - |
|
351 | - return $enforceDefaultExpireDate; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get the quota of a user |
|
356 | - * |
|
357 | - * @param string $userId |
|
358 | - * @return float Quota bytes |
|
359 | - */ |
|
360 | - public static function getUserQuota($userId) { |
|
361 | - $user = \OC::$server->getUserManager()->get($userId); |
|
362 | - if (is_null($user)) { |
|
363 | - return \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
364 | - } |
|
365 | - $userQuota = $user->getQuota(); |
|
366 | - if($userQuota === 'none') { |
|
367 | - return \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
368 | - } |
|
369 | - return OC_Helper::computerFileSize($userQuota); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * copies the skeleton to the users /files |
|
374 | - * |
|
375 | - * @param String $userId |
|
376 | - * @param \OCP\Files\Folder $userDirectory |
|
377 | - * @throws \RuntimeException |
|
378 | - * @suppress PhanDeprecatedFunction |
|
379 | - */ |
|
380 | - public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { |
|
381 | - |
|
382 | - $plainSkeletonDirectory = \OC::$server->getConfig()->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton'); |
|
383 | - $userLang = \OC::$server->getL10NFactory()->findLanguage(); |
|
384 | - $skeletonDirectory = str_replace('{lang}', $userLang, $plainSkeletonDirectory); |
|
385 | - |
|
386 | - if (!file_exists($skeletonDirectory)) { |
|
387 | - $dialectStart = strpos($userLang, '_'); |
|
388 | - if ($dialectStart !== false) { |
|
389 | - $skeletonDirectory = str_replace('{lang}', substr($userLang, 0, $dialectStart), $plainSkeletonDirectory); |
|
390 | - } |
|
391 | - if ($dialectStart === false || !file_exists($skeletonDirectory)) { |
|
392 | - $skeletonDirectory = str_replace('{lang}', 'default', $plainSkeletonDirectory); |
|
393 | - } |
|
394 | - if (!file_exists($skeletonDirectory)) { |
|
395 | - $skeletonDirectory = ''; |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - $instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', ''); |
|
400 | - |
|
401 | - if ($instanceId === null) { |
|
402 | - throw new \RuntimeException('no instance id!'); |
|
403 | - } |
|
404 | - $appdata = 'appdata_' . $instanceId; |
|
405 | - if ($userId === $appdata) { |
|
406 | - throw new \RuntimeException('username is reserved name: ' . $appdata); |
|
407 | - } |
|
408 | - |
|
409 | - if (!empty($skeletonDirectory)) { |
|
410 | - \OCP\Util::writeLog( |
|
411 | - 'files_skeleton', |
|
412 | - 'copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'), |
|
413 | - ILogger::DEBUG |
|
414 | - ); |
|
415 | - self::copyr($skeletonDirectory, $userDirectory); |
|
416 | - // update the file cache |
|
417 | - $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); |
|
418 | - } |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * copies a directory recursively by using streams |
|
423 | - * |
|
424 | - * @param string $source |
|
425 | - * @param \OCP\Files\Folder $target |
|
426 | - * @return void |
|
427 | - */ |
|
428 | - public static function copyr($source, \OCP\Files\Folder $target) { |
|
429 | - $logger = \OC::$server->getLogger(); |
|
430 | - |
|
431 | - // Verify if folder exists |
|
432 | - $dir = opendir($source); |
|
433 | - if($dir === false) { |
|
434 | - $logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']); |
|
435 | - return; |
|
436 | - } |
|
437 | - |
|
438 | - // Copy the files |
|
439 | - while (false !== ($file = readdir($dir))) { |
|
440 | - if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
441 | - if (is_dir($source . '/' . $file)) { |
|
442 | - $child = $target->newFolder($file); |
|
443 | - self::copyr($source . '/' . $file, $child); |
|
444 | - } else { |
|
445 | - $child = $target->newFile($file); |
|
446 | - $sourceStream = fopen($source . '/' . $file, 'r'); |
|
447 | - if($sourceStream === false) { |
|
448 | - $logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']); |
|
449 | - closedir($dir); |
|
450 | - return; |
|
451 | - } |
|
452 | - stream_copy_to_stream($sourceStream, $child->fopen('w')); |
|
453 | - } |
|
454 | - } |
|
455 | - } |
|
456 | - closedir($dir); |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * @return void |
|
461 | - * @suppress PhanUndeclaredMethod |
|
462 | - */ |
|
463 | - public static function tearDownFS() { |
|
464 | - \OC\Files\Filesystem::tearDown(); |
|
465 | - \OC::$server->getRootFolder()->clearCache(); |
|
466 | - self::$fsSetup = false; |
|
467 | - self::$rootMounted = false; |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * get the current installed version of ownCloud |
|
472 | - * |
|
473 | - * @return array |
|
474 | - */ |
|
475 | - public static function getVersion() { |
|
476 | - OC_Util::loadVersion(); |
|
477 | - return self::$versionCache['OC_Version']; |
|
478 | - } |
|
479 | - |
|
480 | - /** |
|
481 | - * get the current installed version string of ownCloud |
|
482 | - * |
|
483 | - * @return string |
|
484 | - */ |
|
485 | - public static function getVersionString() { |
|
486 | - OC_Util::loadVersion(); |
|
487 | - return self::$versionCache['OC_VersionString']; |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * @deprecated the value is of no use anymore |
|
492 | - * @return string |
|
493 | - */ |
|
494 | - public static function getEditionString() { |
|
495 | - return ''; |
|
496 | - } |
|
497 | - |
|
498 | - /** |
|
499 | - * @description get the update channel of the current installed of ownCloud. |
|
500 | - * @return string |
|
501 | - */ |
|
502 | - public static function getChannel() { |
|
503 | - OC_Util::loadVersion(); |
|
504 | - return \OC::$server->getConfig()->getSystemValue('updater.release.channel', self::$versionCache['OC_Channel']); |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * @description get the build number of the current installed of ownCloud. |
|
509 | - * @return string |
|
510 | - */ |
|
511 | - public static function getBuild() { |
|
512 | - OC_Util::loadVersion(); |
|
513 | - return self::$versionCache['OC_Build']; |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * @description load the version.php into the session as cache |
|
518 | - * @suppress PhanUndeclaredVariable |
|
519 | - */ |
|
520 | - private static function loadVersion() { |
|
521 | - if (self::$versionCache !== null) { |
|
522 | - return; |
|
523 | - } |
|
524 | - |
|
525 | - $timestamp = filemtime(OC::$SERVERROOT . '/version.php'); |
|
526 | - require OC::$SERVERROOT . '/version.php'; |
|
527 | - /** @var $timestamp int */ |
|
528 | - self::$versionCache['OC_Version_Timestamp'] = $timestamp; |
|
529 | - /** @var $OC_Version string */ |
|
530 | - self::$versionCache['OC_Version'] = $OC_Version; |
|
531 | - /** @var $OC_VersionString string */ |
|
532 | - self::$versionCache['OC_VersionString'] = $OC_VersionString; |
|
533 | - /** @var $OC_Build string */ |
|
534 | - self::$versionCache['OC_Build'] = $OC_Build; |
|
535 | - |
|
536 | - /** @var $OC_Channel string */ |
|
537 | - self::$versionCache['OC_Channel'] = $OC_Channel; |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * generates a path for JS/CSS files. If no application is provided it will create the path for core. |
|
542 | - * |
|
543 | - * @param string $application application to get the files from |
|
544 | - * @param string $directory directory within this application (css, js, vendor, etc) |
|
545 | - * @param string $file the file inside of the above folder |
|
546 | - * @return string the path |
|
547 | - */ |
|
548 | - private static function generatePath($application, $directory, $file) { |
|
549 | - if (is_null($file)) { |
|
550 | - $file = $application; |
|
551 | - $application = ""; |
|
552 | - } |
|
553 | - if (!empty($application)) { |
|
554 | - return "$application/$directory/$file"; |
|
555 | - } else { |
|
556 | - return "$directory/$file"; |
|
557 | - } |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * add a javascript file |
|
562 | - * |
|
563 | - * @param string $application application id |
|
564 | - * @param string|null $file filename |
|
565 | - * @param bool $prepend prepend the Script to the beginning of the list |
|
566 | - * @return void |
|
567 | - */ |
|
568 | - public static function addScript($application, $file = null, $prepend = false) { |
|
569 | - $path = OC_Util::generatePath($application, 'js', $file); |
|
570 | - |
|
571 | - // core js files need separate handling |
|
572 | - if ($application !== 'core' && $file !== null) { |
|
573 | - self::addTranslations ( $application ); |
|
574 | - } |
|
575 | - self::addExternalResource($application, $prepend, $path, "script"); |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * add a javascript file from the vendor sub folder |
|
580 | - * |
|
581 | - * @param string $application application id |
|
582 | - * @param string|null $file filename |
|
583 | - * @param bool $prepend prepend the Script to the beginning of the list |
|
584 | - * @return void |
|
585 | - */ |
|
586 | - public static function addVendorScript($application, $file = null, $prepend = false) { |
|
587 | - $path = OC_Util::generatePath($application, 'vendor', $file); |
|
588 | - self::addExternalResource($application, $prepend, $path, "script"); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * add a translation JS file |
|
593 | - * |
|
594 | - * @param string $application application id |
|
595 | - * @param string|null $languageCode language code, defaults to the current language |
|
596 | - * @param bool|null $prepend prepend the Script to the beginning of the list |
|
597 | - */ |
|
598 | - public static function addTranslations($application, $languageCode = null, $prepend = false) { |
|
599 | - if (is_null($languageCode)) { |
|
600 | - $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); |
|
601 | - } |
|
602 | - if (!empty($application)) { |
|
603 | - $path = "$application/l10n/$languageCode"; |
|
604 | - } else { |
|
605 | - $path = "l10n/$languageCode"; |
|
606 | - } |
|
607 | - self::addExternalResource($application, $prepend, $path, "script"); |
|
608 | - } |
|
609 | - |
|
610 | - /** |
|
611 | - * add a css file |
|
612 | - * |
|
613 | - * @param string $application application id |
|
614 | - * @param string|null $file filename |
|
615 | - * @param bool $prepend prepend the Style to the beginning of the list |
|
616 | - * @return void |
|
617 | - */ |
|
618 | - public static function addStyle($application, $file = null, $prepend = false) { |
|
619 | - $path = OC_Util::generatePath($application, 'css', $file); |
|
620 | - self::addExternalResource($application, $prepend, $path, "style"); |
|
621 | - } |
|
622 | - |
|
623 | - /** |
|
624 | - * add a css file from the vendor sub folder |
|
625 | - * |
|
626 | - * @param string $application application id |
|
627 | - * @param string|null $file filename |
|
628 | - * @param bool $prepend prepend the Style to the beginning of the list |
|
629 | - * @return void |
|
630 | - */ |
|
631 | - public static function addVendorStyle($application, $file = null, $prepend = false) { |
|
632 | - $path = OC_Util::generatePath($application, 'vendor', $file); |
|
633 | - self::addExternalResource($application, $prepend, $path, "style"); |
|
634 | - } |
|
635 | - |
|
636 | - /** |
|
637 | - * add an external resource css/js file |
|
638 | - * |
|
639 | - * @param string $application application id |
|
640 | - * @param bool $prepend prepend the file to the beginning of the list |
|
641 | - * @param string $path |
|
642 | - * @param string $type (script or style) |
|
643 | - * @return void |
|
644 | - */ |
|
645 | - private static function addExternalResource($application, $prepend, $path, $type = "script") { |
|
646 | - |
|
647 | - if ($type === "style") { |
|
648 | - if (!in_array($path, self::$styles)) { |
|
649 | - if ($prepend === true) { |
|
650 | - array_unshift ( self::$styles, $path ); |
|
651 | - } else { |
|
652 | - self::$styles[] = $path; |
|
653 | - } |
|
654 | - } |
|
655 | - } elseif ($type === "script") { |
|
656 | - if (!in_array($path, self::$scripts)) { |
|
657 | - if ($prepend === true) { |
|
658 | - array_unshift ( self::$scripts, $path ); |
|
659 | - } else { |
|
660 | - self::$scripts [] = $path; |
|
661 | - } |
|
662 | - } |
|
663 | - } |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * Add a custom element to the header |
|
668 | - * If $text is null then the element will be written as empty element. |
|
669 | - * So use "" to get a closing tag. |
|
670 | - * @param string $tag tag name of the element |
|
671 | - * @param array $attributes array of attributes for the element |
|
672 | - * @param string $text the text content for the element |
|
673 | - */ |
|
674 | - public static function addHeader($tag, $attributes, $text=null) { |
|
675 | - self::$headers[] = array( |
|
676 | - 'tag' => $tag, |
|
677 | - 'attributes' => $attributes, |
|
678 | - 'text' => $text |
|
679 | - ); |
|
680 | - } |
|
681 | - |
|
682 | - /** |
|
683 | - * check if the current server configuration is suitable for ownCloud |
|
684 | - * |
|
685 | - * @param \OC\SystemConfig $config |
|
686 | - * @return array arrays with error messages and hints |
|
687 | - */ |
|
688 | - public static function checkServer(\OC\SystemConfig $config) { |
|
689 | - $l = \OC::$server->getL10N('lib'); |
|
690 | - $errors = array(); |
|
691 | - $CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data'); |
|
692 | - |
|
693 | - if (!self::needUpgrade($config) && $config->getValue('installed', false)) { |
|
694 | - // this check needs to be done every time |
|
695 | - $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY); |
|
696 | - } |
|
697 | - |
|
698 | - // Assume that if checkServer() succeeded before in this session, then all is fine. |
|
699 | - if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) { |
|
700 | - return $errors; |
|
701 | - } |
|
702 | - |
|
703 | - $webServerRestart = false; |
|
704 | - $setup = new \OC\Setup( |
|
705 | - $config, |
|
706 | - \OC::$server->getIniWrapper(), |
|
707 | - \OC::$server->getL10N('lib'), |
|
708 | - \OC::$server->query(\OCP\Defaults::class), |
|
709 | - \OC::$server->getLogger(), |
|
710 | - \OC::$server->getSecureRandom(), |
|
711 | - \OC::$server->query(\OC\Installer::class) |
|
712 | - ); |
|
713 | - |
|
714 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
715 | - |
|
716 | - $availableDatabases = $setup->getSupportedDatabases(); |
|
717 | - if (empty($availableDatabases)) { |
|
718 | - $errors[] = array( |
|
719 | - 'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'), |
|
720 | - 'hint' => '' //TODO: sane hint |
|
721 | - ); |
|
722 | - $webServerRestart = true; |
|
723 | - } |
|
724 | - |
|
725 | - // Check if config folder is writable. |
|
726 | - if(!OC_Helper::isReadOnlyConfigEnabled()) { |
|
727 | - if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) { |
|
728 | - $errors[] = array( |
|
729 | - 'error' => $l->t('Cannot write into "config" directory'), |
|
730 | - 'hint' => $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', |
|
731 | - [$urlGenerator->linkToDocs('admin-dir_permissions')]) |
|
732 | - ); |
|
733 | - } |
|
734 | - } |
|
735 | - |
|
736 | - // Check if there is a writable install folder. |
|
737 | - if ($config->getValue('appstoreenabled', true)) { |
|
738 | - if (OC_App::getInstallPath() === null |
|
739 | - || !is_writable(OC_App::getInstallPath()) |
|
740 | - || !is_readable(OC_App::getInstallPath()) |
|
741 | - ) { |
|
742 | - $errors[] = array( |
|
743 | - 'error' => $l->t('Cannot write into "apps" directory'), |
|
744 | - 'hint' => $l->t('This can usually be fixed by giving the webserver write access to the apps directory' |
|
745 | - . ' or disabling the appstore in the config file. See %s', |
|
746 | - [$urlGenerator->linkToDocs('admin-dir_permissions')]) |
|
747 | - ); |
|
748 | - } |
|
749 | - } |
|
750 | - // Create root dir. |
|
751 | - if ($config->getValue('installed', false)) { |
|
752 | - if (!is_dir($CONFIG_DATADIRECTORY)) { |
|
753 | - $success = @mkdir($CONFIG_DATADIRECTORY); |
|
754 | - if ($success) { |
|
755 | - $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); |
|
756 | - } else { |
|
757 | - $errors[] = [ |
|
758 | - 'error' => $l->t('Cannot create "data" directory'), |
|
759 | - 'hint' => $l->t('This can usually be fixed by giving the webserver write access to the root directory. See %s', |
|
760 | - [$urlGenerator->linkToDocs('admin-dir_permissions')]) |
|
761 | - ]; |
|
762 | - } |
|
763 | - } else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) { |
|
764 | - //common hint for all file permissions error messages |
|
765 | - $permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.', |
|
766 | - [$urlGenerator->linkToDocs('admin-dir_permissions')]); |
|
767 | - $errors[] = [ |
|
768 | - 'error' => 'Your data directory is not writable', |
|
769 | - 'hint' => $permissionsHint |
|
770 | - ]; |
|
771 | - } else { |
|
772 | - $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); |
|
773 | - } |
|
774 | - } |
|
775 | - |
|
776 | - if (!OC_Util::isSetLocaleWorking()) { |
|
777 | - $errors[] = array( |
|
778 | - 'error' => $l->t('Setting locale to %s failed', |
|
779 | - array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' |
|
780 | - . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')), |
|
781 | - 'hint' => $l->t('Please install one of these locales on your system and restart your webserver.') |
|
782 | - ); |
|
783 | - } |
|
784 | - |
|
785 | - // Contains the dependencies that should be checked against |
|
786 | - // classes = class_exists |
|
787 | - // functions = function_exists |
|
788 | - // defined = defined |
|
789 | - // ini = ini_get |
|
790 | - // If the dependency is not found the missing module name is shown to the EndUser |
|
791 | - // When adding new checks always verify that they pass on Travis as well |
|
792 | - // for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini |
|
793 | - $dependencies = array( |
|
794 | - 'classes' => array( |
|
795 | - 'ZipArchive' => 'zip', |
|
796 | - 'DOMDocument' => 'dom', |
|
797 | - 'XMLWriter' => 'XMLWriter', |
|
798 | - 'XMLReader' => 'XMLReader', |
|
799 | - ), |
|
800 | - 'functions' => [ |
|
801 | - 'xml_parser_create' => 'libxml', |
|
802 | - 'mb_strcut' => 'mb multibyte', |
|
803 | - 'ctype_digit' => 'ctype', |
|
804 | - 'json_encode' => 'JSON', |
|
805 | - 'gd_info' => 'GD', |
|
806 | - 'gzencode' => 'zlib', |
|
807 | - 'iconv' => 'iconv', |
|
808 | - 'simplexml_load_string' => 'SimpleXML', |
|
809 | - 'hash' => 'HASH Message Digest Framework', |
|
810 | - 'curl_init' => 'cURL', |
|
811 | - 'openssl_verify' => 'OpenSSL', |
|
812 | - ], |
|
813 | - 'defined' => array( |
|
814 | - 'PDO::ATTR_DRIVER_NAME' => 'PDO' |
|
815 | - ), |
|
816 | - 'ini' => [ |
|
817 | - 'default_charset' => 'UTF-8', |
|
818 | - ], |
|
819 | - ); |
|
820 | - $missingDependencies = array(); |
|
821 | - $invalidIniSettings = []; |
|
822 | - $moduleHint = $l->t('Please ask your server administrator to install the module.'); |
|
823 | - |
|
824 | - /** |
|
825 | - * FIXME: The dependency check does not work properly on HHVM on the moment |
|
826 | - * and prevents installation. Once HHVM is more compatible with our |
|
827 | - * approach to check for these values we should re-enable those |
|
828 | - * checks. |
|
829 | - */ |
|
830 | - $iniWrapper = \OC::$server->getIniWrapper(); |
|
831 | - if (!self::runningOnHhvm()) { |
|
832 | - foreach ($dependencies['classes'] as $class => $module) { |
|
833 | - if (!class_exists($class)) { |
|
834 | - $missingDependencies[] = $module; |
|
835 | - } |
|
836 | - } |
|
837 | - foreach ($dependencies['functions'] as $function => $module) { |
|
838 | - if (!function_exists($function)) { |
|
839 | - $missingDependencies[] = $module; |
|
840 | - } |
|
841 | - } |
|
842 | - foreach ($dependencies['defined'] as $defined => $module) { |
|
843 | - if (!defined($defined)) { |
|
844 | - $missingDependencies[] = $module; |
|
845 | - } |
|
846 | - } |
|
847 | - foreach ($dependencies['ini'] as $setting => $expected) { |
|
848 | - if (is_bool($expected)) { |
|
849 | - if ($iniWrapper->getBool($setting) !== $expected) { |
|
850 | - $invalidIniSettings[] = [$setting, $expected]; |
|
851 | - } |
|
852 | - } |
|
853 | - if (is_int($expected)) { |
|
854 | - if ($iniWrapper->getNumeric($setting) !== $expected) { |
|
855 | - $invalidIniSettings[] = [$setting, $expected]; |
|
856 | - } |
|
857 | - } |
|
858 | - if (is_string($expected)) { |
|
859 | - if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) { |
|
860 | - $invalidIniSettings[] = [$setting, $expected]; |
|
861 | - } |
|
862 | - } |
|
863 | - } |
|
864 | - } |
|
865 | - |
|
866 | - foreach($missingDependencies as $missingDependency) { |
|
867 | - $errors[] = array( |
|
868 | - 'error' => $l->t('PHP module %s not installed.', array($missingDependency)), |
|
869 | - 'hint' => $moduleHint |
|
870 | - ); |
|
871 | - $webServerRestart = true; |
|
872 | - } |
|
873 | - foreach($invalidIniSettings as $setting) { |
|
874 | - if(is_bool($setting[1])) { |
|
875 | - $setting[1] = $setting[1] ? 'on' : 'off'; |
|
876 | - } |
|
877 | - $errors[] = [ |
|
878 | - 'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]), |
|
879 | - 'hint' => $l->t('Adjusting this setting in php.ini will make Nextcloud run again') |
|
880 | - ]; |
|
881 | - $webServerRestart = true; |
|
882 | - } |
|
883 | - |
|
884 | - /** |
|
885 | - * The mbstring.func_overload check can only be performed if the mbstring |
|
886 | - * module is installed as it will return null if the checking setting is |
|
887 | - * not available and thus a check on the boolean value fails. |
|
888 | - * |
|
889 | - * TODO: Should probably be implemented in the above generic dependency |
|
890 | - * check somehow in the long-term. |
|
891 | - */ |
|
892 | - if($iniWrapper->getBool('mbstring.func_overload') !== null && |
|
893 | - $iniWrapper->getBool('mbstring.func_overload') === true) { |
|
894 | - $errors[] = array( |
|
895 | - 'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]), |
|
896 | - 'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini') |
|
897 | - ); |
|
898 | - } |
|
899 | - |
|
900 | - if(function_exists('xml_parser_create') && |
|
901 | - LIBXML_LOADED_VERSION < 20700 ) { |
|
902 | - $version = LIBXML_LOADED_VERSION; |
|
903 | - $major = floor($version/10000); |
|
904 | - $version -= ($major * 10000); |
|
905 | - $minor = floor($version/100); |
|
906 | - $version -= ($minor * 100); |
|
907 | - $patch = $version; |
|
908 | - $errors[] = array( |
|
909 | - 'error' => $l->t('libxml2 2.7.0 is at least required. Currently %s is installed.', [$major . '.' . $minor . '.' . $patch]), |
|
910 | - 'hint' => $l->t('To fix this issue update your libxml2 version and restart your web server.') |
|
911 | - ); |
|
912 | - } |
|
913 | - |
|
914 | - if (!self::isAnnotationsWorking()) { |
|
915 | - $errors[] = array( |
|
916 | - 'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'), |
|
917 | - 'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.') |
|
918 | - ); |
|
919 | - } |
|
920 | - |
|
921 | - if (!\OC::$CLI && $webServerRestart) { |
|
922 | - $errors[] = array( |
|
923 | - 'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'), |
|
924 | - 'hint' => $l->t('Please ask your server administrator to restart the web server.') |
|
925 | - ); |
|
926 | - } |
|
927 | - |
|
928 | - $errors = array_merge($errors, self::checkDatabaseVersion()); |
|
929 | - |
|
930 | - // Cache the result of this function |
|
931 | - \OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0); |
|
932 | - |
|
933 | - return $errors; |
|
934 | - } |
|
935 | - |
|
936 | - /** |
|
937 | - * Check the database version |
|
938 | - * |
|
939 | - * @return array errors array |
|
940 | - */ |
|
941 | - public static function checkDatabaseVersion() { |
|
942 | - $l = \OC::$server->getL10N('lib'); |
|
943 | - $errors = array(); |
|
944 | - $dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite'); |
|
945 | - if ($dbType === 'pgsql') { |
|
946 | - // check PostgreSQL version |
|
947 | - try { |
|
948 | - $result = \OC_DB::executeAudited('SHOW SERVER_VERSION'); |
|
949 | - $data = $result->fetchRow(); |
|
950 | - if (isset($data['server_version'])) { |
|
951 | - $version = $data['server_version']; |
|
952 | - if (version_compare($version, '9.0.0', '<')) { |
|
953 | - $errors[] = array( |
|
954 | - 'error' => $l->t('PostgreSQL >= 9 required'), |
|
955 | - 'hint' => $l->t('Please upgrade your database version') |
|
956 | - ); |
|
957 | - } |
|
958 | - } |
|
959 | - } catch (\Doctrine\DBAL\DBALException $e) { |
|
960 | - $logger = \OC::$server->getLogger(); |
|
961 | - $logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9'); |
|
962 | - $logger->logException($e); |
|
963 | - } |
|
964 | - } |
|
965 | - return $errors; |
|
966 | - } |
|
967 | - |
|
968 | - /** |
|
969 | - * Check for correct file permissions of data directory |
|
970 | - * |
|
971 | - * @param string $dataDirectory |
|
972 | - * @return array arrays with error messages and hints |
|
973 | - */ |
|
974 | - public static function checkDataDirectoryPermissions($dataDirectory) { |
|
975 | - if(\OC::$server->getConfig()->getSystemValue('check_data_directory_permissions', true) === false) { |
|
976 | - return []; |
|
977 | - } |
|
978 | - $l = \OC::$server->getL10N('lib'); |
|
979 | - $errors = []; |
|
980 | - $permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory' |
|
981 | - . ' cannot be listed by other users.'); |
|
982 | - $perms = substr(decoct(@fileperms($dataDirectory)), -3); |
|
983 | - if (substr($perms, -1) !== '0') { |
|
984 | - chmod($dataDirectory, 0770); |
|
985 | - clearstatcache(); |
|
986 | - $perms = substr(decoct(@fileperms($dataDirectory)), -3); |
|
987 | - if ($perms[2] !== '0') { |
|
988 | - $errors[] = [ |
|
989 | - 'error' => $l->t('Your data directory is readable by other users'), |
|
990 | - 'hint' => $permissionsModHint |
|
991 | - ]; |
|
992 | - } |
|
993 | - } |
|
994 | - return $errors; |
|
995 | - } |
|
996 | - |
|
997 | - /** |
|
998 | - * Check that the data directory exists and is valid by |
|
999 | - * checking the existence of the ".ocdata" file. |
|
1000 | - * |
|
1001 | - * @param string $dataDirectory data directory path |
|
1002 | - * @return array errors found |
|
1003 | - */ |
|
1004 | - public static function checkDataDirectoryValidity($dataDirectory) { |
|
1005 | - $l = \OC::$server->getL10N('lib'); |
|
1006 | - $errors = []; |
|
1007 | - if ($dataDirectory[0] !== '/') { |
|
1008 | - $errors[] = [ |
|
1009 | - 'error' => $l->t('Your data directory must be an absolute path'), |
|
1010 | - 'hint' => $l->t('Check the value of "datadirectory" in your configuration') |
|
1011 | - ]; |
|
1012 | - } |
|
1013 | - if (!file_exists($dataDirectory . '/.ocdata')) { |
|
1014 | - $errors[] = [ |
|
1015 | - 'error' => $l->t('Your data directory is invalid'), |
|
1016 | - 'hint' => $l->t('Ensure there is a file called ".ocdata"' . |
|
1017 | - ' in the root of the data directory.') |
|
1018 | - ]; |
|
1019 | - } |
|
1020 | - return $errors; |
|
1021 | - } |
|
1022 | - |
|
1023 | - /** |
|
1024 | - * Check if the user is logged in, redirects to home if not. With |
|
1025 | - * redirect URL parameter to the request URI. |
|
1026 | - * |
|
1027 | - * @return void |
|
1028 | - */ |
|
1029 | - public static function checkLoggedIn() { |
|
1030 | - // Check if we are a user |
|
1031 | - if (!\OC::$server->getUserSession()->isLoggedIn()) { |
|
1032 | - header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute( |
|
1033 | - 'core.login.showLoginForm', |
|
1034 | - [ |
|
1035 | - 'redirect_url' => \OC::$server->getRequest()->getRequestUri(), |
|
1036 | - ] |
|
1037 | - ) |
|
1038 | - ); |
|
1039 | - exit(); |
|
1040 | - } |
|
1041 | - // Redirect to 2FA challenge selection if 2FA challenge was not solved yet |
|
1042 | - if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
1043 | - header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
1044 | - exit(); |
|
1045 | - } |
|
1046 | - } |
|
1047 | - |
|
1048 | - /** |
|
1049 | - * Check if the user is a admin, redirects to home if not |
|
1050 | - * |
|
1051 | - * @return void |
|
1052 | - */ |
|
1053 | - public static function checkAdminUser() { |
|
1054 | - OC_Util::checkLoggedIn(); |
|
1055 | - if (!OC_User::isAdminUser(OC_User::getUser())) { |
|
1056 | - header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); |
|
1057 | - exit(); |
|
1058 | - } |
|
1059 | - } |
|
1060 | - |
|
1061 | - /** |
|
1062 | - * Check if the user is a subadmin, redirects to home if not |
|
1063 | - * |
|
1064 | - * @return null|boolean $groups where the current user is subadmin |
|
1065 | - */ |
|
1066 | - public static function checkSubAdminUser() { |
|
1067 | - OC_Util::checkLoggedIn(); |
|
1068 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
1069 | - $isSubAdmin = false; |
|
1070 | - if($userObject !== null) { |
|
1071 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
1072 | - } |
|
1073 | - |
|
1074 | - if (!$isSubAdmin) { |
|
1075 | - header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); |
|
1076 | - exit(); |
|
1077 | - } |
|
1078 | - return true; |
|
1079 | - } |
|
1080 | - |
|
1081 | - /** |
|
1082 | - * Returns the URL of the default page |
|
1083 | - * based on the system configuration and |
|
1084 | - * the apps visible for the current user |
|
1085 | - * |
|
1086 | - * @return string URL |
|
1087 | - * @suppress PhanDeprecatedFunction |
|
1088 | - */ |
|
1089 | - public static function getDefaultPageUrl() { |
|
1090 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
1091 | - // Deny the redirect if the URL contains a @ |
|
1092 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
1093 | - if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) { |
|
1094 | - $location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); |
|
1095 | - } else { |
|
1096 | - $defaultPage = \OC::$server->getConfig()->getAppValue('core', 'defaultpage'); |
|
1097 | - if ($defaultPage) { |
|
1098 | - $location = $urlGenerator->getAbsoluteURL($defaultPage); |
|
1099 | - } else { |
|
1100 | - $appId = 'files'; |
|
1101 | - $config = \OC::$server->getConfig(); |
|
1102 | - $defaultApps = explode(',', $config->getSystemValue('defaultapp', 'files')); |
|
1103 | - // find the first app that is enabled for the current user |
|
1104 | - foreach ($defaultApps as $defaultApp) { |
|
1105 | - $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); |
|
1106 | - if (static::getAppManager()->isEnabledForUser($defaultApp)) { |
|
1107 | - $appId = $defaultApp; |
|
1108 | - break; |
|
1109 | - } |
|
1110 | - } |
|
1111 | - |
|
1112 | - if($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') { |
|
1113 | - $location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/'); |
|
1114 | - } else { |
|
1115 | - $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/'); |
|
1116 | - } |
|
1117 | - } |
|
1118 | - } |
|
1119 | - return $location; |
|
1120 | - } |
|
1121 | - |
|
1122 | - /** |
|
1123 | - * Redirect to the user default page |
|
1124 | - * |
|
1125 | - * @return void |
|
1126 | - */ |
|
1127 | - public static function redirectToDefaultPage() { |
|
1128 | - $location = self::getDefaultPageUrl(); |
|
1129 | - header('Location: ' . $location); |
|
1130 | - exit(); |
|
1131 | - } |
|
1132 | - |
|
1133 | - /** |
|
1134 | - * get an id unique for this instance |
|
1135 | - * |
|
1136 | - * @return string |
|
1137 | - */ |
|
1138 | - public static function getInstanceId() { |
|
1139 | - $id = \OC::$server->getSystemConfig()->getValue('instanceid', null); |
|
1140 | - if (is_null($id)) { |
|
1141 | - // We need to guarantee at least one letter in instanceid so it can be used as the session_name |
|
1142 | - $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
1143 | - \OC::$server->getSystemConfig()->setValue('instanceid', $id); |
|
1144 | - } |
|
1145 | - return $id; |
|
1146 | - } |
|
1147 | - |
|
1148 | - /** |
|
1149 | - * Public function to sanitize HTML |
|
1150 | - * |
|
1151 | - * This function is used to sanitize HTML and should be applied on any |
|
1152 | - * string or array of strings before displaying it on a web page. |
|
1153 | - * |
|
1154 | - * @param string|array $value |
|
1155 | - * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
1156 | - */ |
|
1157 | - public static function sanitizeHTML($value) { |
|
1158 | - if (is_array($value)) { |
|
1159 | - $value = array_map(function($value) { |
|
1160 | - return self::sanitizeHTML($value); |
|
1161 | - }, $value); |
|
1162 | - } else { |
|
1163 | - // Specify encoding for PHP<5.4 |
|
1164 | - $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); |
|
1165 | - } |
|
1166 | - return $value; |
|
1167 | - } |
|
1168 | - |
|
1169 | - /** |
|
1170 | - * Public function to encode url parameters |
|
1171 | - * |
|
1172 | - * This function is used to encode path to file before output. |
|
1173 | - * Encoding is done according to RFC 3986 with one exception: |
|
1174 | - * Character '/' is preserved as is. |
|
1175 | - * |
|
1176 | - * @param string $component part of URI to encode |
|
1177 | - * @return string |
|
1178 | - */ |
|
1179 | - public static function encodePath($component) { |
|
1180 | - $encoded = rawurlencode($component); |
|
1181 | - $encoded = str_replace('%2F', '/', $encoded); |
|
1182 | - return $encoded; |
|
1183 | - } |
|
1184 | - |
|
1185 | - |
|
1186 | - public function createHtaccessTestFile(\OCP\IConfig $config) { |
|
1187 | - // php dev server does not support htaccess |
|
1188 | - if (php_sapi_name() === 'cli-server') { |
|
1189 | - return false; |
|
1190 | - } |
|
1191 | - |
|
1192 | - // testdata |
|
1193 | - $fileName = '/htaccesstest.txt'; |
|
1194 | - $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.'; |
|
1195 | - |
|
1196 | - // creating a test file |
|
1197 | - $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; |
|
1198 | - |
|
1199 | - if (file_exists($testFile)) {// already running this test, possible recursive call |
|
1200 | - return false; |
|
1201 | - } |
|
1202 | - |
|
1203 | - $fp = @fopen($testFile, 'w'); |
|
1204 | - if (!$fp) { |
|
1205 | - throw new OC\HintException('Can\'t create test file to check for working .htaccess file.', |
|
1206 | - 'Make sure it is possible for the webserver to write to ' . $testFile); |
|
1207 | - } |
|
1208 | - fwrite($fp, $testContent); |
|
1209 | - fclose($fp); |
|
1210 | - |
|
1211 | - return $testContent; |
|
1212 | - } |
|
1213 | - |
|
1214 | - /** |
|
1215 | - * Check if the .htaccess file is working |
|
1216 | - * @param \OCP\IConfig $config |
|
1217 | - * @return bool |
|
1218 | - * @throws Exception |
|
1219 | - * @throws \OC\HintException If the test file can't get written. |
|
1220 | - */ |
|
1221 | - public function isHtaccessWorking(\OCP\IConfig $config) { |
|
1222 | - |
|
1223 | - if (\OC::$CLI || !$config->getSystemValue('check_for_working_htaccess', true)) { |
|
1224 | - return true; |
|
1225 | - } |
|
1226 | - |
|
1227 | - $testContent = $this->createHtaccessTestFile($config); |
|
1228 | - if ($testContent === false) { |
|
1229 | - return false; |
|
1230 | - } |
|
1231 | - |
|
1232 | - $fileName = '/htaccesstest.txt'; |
|
1233 | - $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; |
|
1234 | - |
|
1235 | - // accessing the file via http |
|
1236 | - $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName); |
|
1237 | - try { |
|
1238 | - $content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody(); |
|
1239 | - } catch (\Exception $e) { |
|
1240 | - $content = false; |
|
1241 | - } |
|
1242 | - |
|
1243 | - // cleanup |
|
1244 | - @unlink($testFile); |
|
1245 | - |
|
1246 | - /* |
|
69 | + public static $scripts = array(); |
|
70 | + public static $styles = array(); |
|
71 | + public static $headers = array(); |
|
72 | + private static $rootMounted = false; |
|
73 | + private static $fsSetup = false; |
|
74 | + |
|
75 | + /** @var array Local cache of version.php */ |
|
76 | + private static $versionCache = null; |
|
77 | + |
|
78 | + protected static function getAppManager() { |
|
79 | + return \OC::$server->getAppManager(); |
|
80 | + } |
|
81 | + |
|
82 | + private static function initLocalStorageRootFS() { |
|
83 | + // mount local file backend as root |
|
84 | + $configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data"); |
|
85 | + //first set up the local "root" storage |
|
86 | + \OC\Files\Filesystem::initMountManager(); |
|
87 | + if (!self::$rootMounted) { |
|
88 | + \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/'); |
|
89 | + self::$rootMounted = true; |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * mounting an object storage as the root fs will in essence remove the |
|
95 | + * necessity of a data folder being present. |
|
96 | + * TODO make home storage aware of this and use the object storage instead of local disk access |
|
97 | + * |
|
98 | + * @param array $config containing 'class' and optional 'arguments' |
|
99 | + * @suppress PhanDeprecatedFunction |
|
100 | + */ |
|
101 | + private static function initObjectStoreRootFS($config) { |
|
102 | + // check misconfiguration |
|
103 | + if (empty($config['class'])) { |
|
104 | + \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
105 | + } |
|
106 | + if (!isset($config['arguments'])) { |
|
107 | + $config['arguments'] = array(); |
|
108 | + } |
|
109 | + |
|
110 | + // instantiate object store implementation |
|
111 | + $name = $config['class']; |
|
112 | + if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { |
|
113 | + $segments = explode('\\', $name); |
|
114 | + OC_App::loadApp(strtolower($segments[1])); |
|
115 | + } |
|
116 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
117 | + // mount with plain / root object store implementation |
|
118 | + $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; |
|
119 | + |
|
120 | + // mount object storage as root |
|
121 | + \OC\Files\Filesystem::initMountManager(); |
|
122 | + if (!self::$rootMounted) { |
|
123 | + \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); |
|
124 | + self::$rootMounted = true; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * mounting an object storage as the root fs will in essence remove the |
|
130 | + * necessity of a data folder being present. |
|
131 | + * |
|
132 | + * @param array $config containing 'class' and optional 'arguments' |
|
133 | + * @suppress PhanDeprecatedFunction |
|
134 | + */ |
|
135 | + private static function initObjectStoreMultibucketRootFS($config) { |
|
136 | + // check misconfiguration |
|
137 | + if (empty($config['class'])) { |
|
138 | + \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
139 | + } |
|
140 | + if (!isset($config['arguments'])) { |
|
141 | + $config['arguments'] = array(); |
|
142 | + } |
|
143 | + |
|
144 | + // instantiate object store implementation |
|
145 | + $name = $config['class']; |
|
146 | + if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { |
|
147 | + $segments = explode('\\', $name); |
|
148 | + OC_App::loadApp(strtolower($segments[1])); |
|
149 | + } |
|
150 | + |
|
151 | + if (!isset($config['arguments']['bucket'])) { |
|
152 | + $config['arguments']['bucket'] = ''; |
|
153 | + } |
|
154 | + // put the root FS always in first bucket for multibucket configuration |
|
155 | + $config['arguments']['bucket'] .= '0'; |
|
156 | + |
|
157 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
158 | + // mount with plain / root object store implementation |
|
159 | + $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; |
|
160 | + |
|
161 | + // mount object storage as root |
|
162 | + \OC\Files\Filesystem::initMountManager(); |
|
163 | + if (!self::$rootMounted) { |
|
164 | + \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); |
|
165 | + self::$rootMounted = true; |
|
166 | + } |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Can be set up |
|
171 | + * |
|
172 | + * @param string $user |
|
173 | + * @return boolean |
|
174 | + * @description configure the initial filesystem based on the configuration |
|
175 | + * @suppress PhanDeprecatedFunction |
|
176 | + * @suppress PhanAccessMethodInternal |
|
177 | + */ |
|
178 | + public static function setupFS($user = '') { |
|
179 | + //setting up the filesystem twice can only lead to trouble |
|
180 | + if (self::$fsSetup) { |
|
181 | + return false; |
|
182 | + } |
|
183 | + |
|
184 | + \OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem'); |
|
185 | + |
|
186 | + // If we are not forced to load a specific user we load the one that is logged in |
|
187 | + if ($user === null) { |
|
188 | + $user = ''; |
|
189 | + } else if ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) { |
|
190 | + $user = OC_User::getUser(); |
|
191 | + } |
|
192 | + |
|
193 | + // load all filesystem apps before, so no setup-hook gets lost |
|
194 | + OC_App::loadApps(array('filesystem')); |
|
195 | + |
|
196 | + // the filesystem will finish when $user is not empty, |
|
197 | + // mark fs setup here to avoid doing the setup from loading |
|
198 | + // OC_Filesystem |
|
199 | + if ($user != '') { |
|
200 | + self::$fsSetup = true; |
|
201 | + } |
|
202 | + |
|
203 | + \OC\Files\Filesystem::initMountManager(); |
|
204 | + |
|
205 | + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
206 | + \OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
|
207 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) { |
|
208 | + /** @var \OC\Files\Storage\Common $storage */ |
|
209 | + $storage->setMountOptions($mount->getOptions()); |
|
210 | + } |
|
211 | + return $storage; |
|
212 | + }); |
|
213 | + |
|
214 | + \OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
|
215 | + if (!$mount->getOption('enable_sharing', true)) { |
|
216 | + return new \OC\Files\Storage\Wrapper\PermissionsMask([ |
|
217 | + 'storage' => $storage, |
|
218 | + 'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE |
|
219 | + ]); |
|
220 | + } |
|
221 | + return $storage; |
|
222 | + }); |
|
223 | + |
|
224 | + // install storage availability wrapper, before most other wrappers |
|
225 | + \OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, \OCP\Files\Storage\IStorage $storage) { |
|
226 | + if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { |
|
227 | + return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]); |
|
228 | + } |
|
229 | + return $storage; |
|
230 | + }); |
|
231 | + |
|
232 | + \OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
|
233 | + if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { |
|
234 | + return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]); |
|
235 | + } |
|
236 | + return $storage; |
|
237 | + }); |
|
238 | + |
|
239 | + \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { |
|
240 | + // set up quota for home storages, even for other users |
|
241 | + // which can happen when using sharing |
|
242 | + |
|
243 | + /** |
|
244 | + * @var \OC\Files\Storage\Storage $storage |
|
245 | + */ |
|
246 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
247 | + || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
248 | + ) { |
|
249 | + /** @var \OC\Files\Storage\Home $storage */ |
|
250 | + if (is_object($storage->getUser())) { |
|
251 | + $user = $storage->getUser()->getUID(); |
|
252 | + $quota = OC_Util::getUserQuota($user); |
|
253 | + if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
254 | + return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); |
|
255 | + } |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + return $storage; |
|
260 | + }); |
|
261 | + |
|
262 | + OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); |
|
263 | + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true); |
|
264 | + |
|
265 | + //check if we are using an object storage |
|
266 | + $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null); |
|
267 | + $objectStoreMultibucket = \OC::$server->getSystemConfig()->getValue('objectstore_multibucket', null); |
|
268 | + |
|
269 | + // use the same order as in ObjectHomeMountProvider |
|
270 | + if (isset($objectStoreMultibucket)) { |
|
271 | + self::initObjectStoreMultibucketRootFS($objectStoreMultibucket); |
|
272 | + } elseif (isset($objectStore)) { |
|
273 | + self::initObjectStoreRootFS($objectStore); |
|
274 | + } else { |
|
275 | + self::initLocalStorageRootFS(); |
|
276 | + } |
|
277 | + |
|
278 | + if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) { |
|
279 | + \OC::$server->getEventLogger()->end('setup_fs'); |
|
280 | + return false; |
|
281 | + } |
|
282 | + |
|
283 | + //if we aren't logged in, there is no use to set up the filesystem |
|
284 | + if ($user != "") { |
|
285 | + |
|
286 | + $userDir = '/' . $user . '/files'; |
|
287 | + |
|
288 | + //jail the user into his "home" directory |
|
289 | + \OC\Files\Filesystem::init($user, $userDir); |
|
290 | + |
|
291 | + OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir)); |
|
292 | + } |
|
293 | + \OC::$server->getEventLogger()->end('setup_fs'); |
|
294 | + return true; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * check if a password is required for each public link |
|
299 | + * |
|
300 | + * @return boolean |
|
301 | + * @suppress PhanDeprecatedFunction |
|
302 | + */ |
|
303 | + public static function isPublicLinkPasswordRequired() { |
|
304 | + $enforcePassword = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_links_password', 'no'); |
|
305 | + return $enforcePassword === 'yes'; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * check if sharing is disabled for the current user |
|
310 | + * @param IConfig $config |
|
311 | + * @param IGroupManager $groupManager |
|
312 | + * @param IUser|null $user |
|
313 | + * @return bool |
|
314 | + */ |
|
315 | + public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { |
|
316 | + if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
317 | + $groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
318 | + $excludedGroups = json_decode($groupsList); |
|
319 | + if (is_null($excludedGroups)) { |
|
320 | + $excludedGroups = explode(',', $groupsList); |
|
321 | + $newValue = json_encode($excludedGroups); |
|
322 | + $config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
323 | + } |
|
324 | + $usersGroups = $groupManager->getUserGroupIds($user); |
|
325 | + if (!empty($usersGroups)) { |
|
326 | + $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
327 | + // if the user is only in groups which are disabled for sharing then |
|
328 | + // sharing is also disabled for the user |
|
329 | + if (empty($remainingGroups)) { |
|
330 | + return true; |
|
331 | + } |
|
332 | + } |
|
333 | + } |
|
334 | + return false; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * check if share API enforces a default expire date |
|
339 | + * |
|
340 | + * @return boolean |
|
341 | + * @suppress PhanDeprecatedFunction |
|
342 | + */ |
|
343 | + public static function isDefaultExpireDateEnforced() { |
|
344 | + $isDefaultExpireDateEnabled = \OC::$server->getConfig()->getAppValue('core', 'shareapi_default_expire_date', 'no'); |
|
345 | + $enforceDefaultExpireDate = false; |
|
346 | + if ($isDefaultExpireDateEnabled === 'yes') { |
|
347 | + $value = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_expire_date', 'no'); |
|
348 | + $enforceDefaultExpireDate = $value === 'yes'; |
|
349 | + } |
|
350 | + |
|
351 | + return $enforceDefaultExpireDate; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get the quota of a user |
|
356 | + * |
|
357 | + * @param string $userId |
|
358 | + * @return float Quota bytes |
|
359 | + */ |
|
360 | + public static function getUserQuota($userId) { |
|
361 | + $user = \OC::$server->getUserManager()->get($userId); |
|
362 | + if (is_null($user)) { |
|
363 | + return \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
364 | + } |
|
365 | + $userQuota = $user->getQuota(); |
|
366 | + if($userQuota === 'none') { |
|
367 | + return \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
368 | + } |
|
369 | + return OC_Helper::computerFileSize($userQuota); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * copies the skeleton to the users /files |
|
374 | + * |
|
375 | + * @param String $userId |
|
376 | + * @param \OCP\Files\Folder $userDirectory |
|
377 | + * @throws \RuntimeException |
|
378 | + * @suppress PhanDeprecatedFunction |
|
379 | + */ |
|
380 | + public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { |
|
381 | + |
|
382 | + $plainSkeletonDirectory = \OC::$server->getConfig()->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton'); |
|
383 | + $userLang = \OC::$server->getL10NFactory()->findLanguage(); |
|
384 | + $skeletonDirectory = str_replace('{lang}', $userLang, $plainSkeletonDirectory); |
|
385 | + |
|
386 | + if (!file_exists($skeletonDirectory)) { |
|
387 | + $dialectStart = strpos($userLang, '_'); |
|
388 | + if ($dialectStart !== false) { |
|
389 | + $skeletonDirectory = str_replace('{lang}', substr($userLang, 0, $dialectStart), $plainSkeletonDirectory); |
|
390 | + } |
|
391 | + if ($dialectStart === false || !file_exists($skeletonDirectory)) { |
|
392 | + $skeletonDirectory = str_replace('{lang}', 'default', $plainSkeletonDirectory); |
|
393 | + } |
|
394 | + if (!file_exists($skeletonDirectory)) { |
|
395 | + $skeletonDirectory = ''; |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + $instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', ''); |
|
400 | + |
|
401 | + if ($instanceId === null) { |
|
402 | + throw new \RuntimeException('no instance id!'); |
|
403 | + } |
|
404 | + $appdata = 'appdata_' . $instanceId; |
|
405 | + if ($userId === $appdata) { |
|
406 | + throw new \RuntimeException('username is reserved name: ' . $appdata); |
|
407 | + } |
|
408 | + |
|
409 | + if (!empty($skeletonDirectory)) { |
|
410 | + \OCP\Util::writeLog( |
|
411 | + 'files_skeleton', |
|
412 | + 'copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'), |
|
413 | + ILogger::DEBUG |
|
414 | + ); |
|
415 | + self::copyr($skeletonDirectory, $userDirectory); |
|
416 | + // update the file cache |
|
417 | + $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); |
|
418 | + } |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * copies a directory recursively by using streams |
|
423 | + * |
|
424 | + * @param string $source |
|
425 | + * @param \OCP\Files\Folder $target |
|
426 | + * @return void |
|
427 | + */ |
|
428 | + public static function copyr($source, \OCP\Files\Folder $target) { |
|
429 | + $logger = \OC::$server->getLogger(); |
|
430 | + |
|
431 | + // Verify if folder exists |
|
432 | + $dir = opendir($source); |
|
433 | + if($dir === false) { |
|
434 | + $logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']); |
|
435 | + return; |
|
436 | + } |
|
437 | + |
|
438 | + // Copy the files |
|
439 | + while (false !== ($file = readdir($dir))) { |
|
440 | + if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
441 | + if (is_dir($source . '/' . $file)) { |
|
442 | + $child = $target->newFolder($file); |
|
443 | + self::copyr($source . '/' . $file, $child); |
|
444 | + } else { |
|
445 | + $child = $target->newFile($file); |
|
446 | + $sourceStream = fopen($source . '/' . $file, 'r'); |
|
447 | + if($sourceStream === false) { |
|
448 | + $logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']); |
|
449 | + closedir($dir); |
|
450 | + return; |
|
451 | + } |
|
452 | + stream_copy_to_stream($sourceStream, $child->fopen('w')); |
|
453 | + } |
|
454 | + } |
|
455 | + } |
|
456 | + closedir($dir); |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * @return void |
|
461 | + * @suppress PhanUndeclaredMethod |
|
462 | + */ |
|
463 | + public static function tearDownFS() { |
|
464 | + \OC\Files\Filesystem::tearDown(); |
|
465 | + \OC::$server->getRootFolder()->clearCache(); |
|
466 | + self::$fsSetup = false; |
|
467 | + self::$rootMounted = false; |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * get the current installed version of ownCloud |
|
472 | + * |
|
473 | + * @return array |
|
474 | + */ |
|
475 | + public static function getVersion() { |
|
476 | + OC_Util::loadVersion(); |
|
477 | + return self::$versionCache['OC_Version']; |
|
478 | + } |
|
479 | + |
|
480 | + /** |
|
481 | + * get the current installed version string of ownCloud |
|
482 | + * |
|
483 | + * @return string |
|
484 | + */ |
|
485 | + public static function getVersionString() { |
|
486 | + OC_Util::loadVersion(); |
|
487 | + return self::$versionCache['OC_VersionString']; |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * @deprecated the value is of no use anymore |
|
492 | + * @return string |
|
493 | + */ |
|
494 | + public static function getEditionString() { |
|
495 | + return ''; |
|
496 | + } |
|
497 | + |
|
498 | + /** |
|
499 | + * @description get the update channel of the current installed of ownCloud. |
|
500 | + * @return string |
|
501 | + */ |
|
502 | + public static function getChannel() { |
|
503 | + OC_Util::loadVersion(); |
|
504 | + return \OC::$server->getConfig()->getSystemValue('updater.release.channel', self::$versionCache['OC_Channel']); |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * @description get the build number of the current installed of ownCloud. |
|
509 | + * @return string |
|
510 | + */ |
|
511 | + public static function getBuild() { |
|
512 | + OC_Util::loadVersion(); |
|
513 | + return self::$versionCache['OC_Build']; |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * @description load the version.php into the session as cache |
|
518 | + * @suppress PhanUndeclaredVariable |
|
519 | + */ |
|
520 | + private static function loadVersion() { |
|
521 | + if (self::$versionCache !== null) { |
|
522 | + return; |
|
523 | + } |
|
524 | + |
|
525 | + $timestamp = filemtime(OC::$SERVERROOT . '/version.php'); |
|
526 | + require OC::$SERVERROOT . '/version.php'; |
|
527 | + /** @var $timestamp int */ |
|
528 | + self::$versionCache['OC_Version_Timestamp'] = $timestamp; |
|
529 | + /** @var $OC_Version string */ |
|
530 | + self::$versionCache['OC_Version'] = $OC_Version; |
|
531 | + /** @var $OC_VersionString string */ |
|
532 | + self::$versionCache['OC_VersionString'] = $OC_VersionString; |
|
533 | + /** @var $OC_Build string */ |
|
534 | + self::$versionCache['OC_Build'] = $OC_Build; |
|
535 | + |
|
536 | + /** @var $OC_Channel string */ |
|
537 | + self::$versionCache['OC_Channel'] = $OC_Channel; |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * generates a path for JS/CSS files. If no application is provided it will create the path for core. |
|
542 | + * |
|
543 | + * @param string $application application to get the files from |
|
544 | + * @param string $directory directory within this application (css, js, vendor, etc) |
|
545 | + * @param string $file the file inside of the above folder |
|
546 | + * @return string the path |
|
547 | + */ |
|
548 | + private static function generatePath($application, $directory, $file) { |
|
549 | + if (is_null($file)) { |
|
550 | + $file = $application; |
|
551 | + $application = ""; |
|
552 | + } |
|
553 | + if (!empty($application)) { |
|
554 | + return "$application/$directory/$file"; |
|
555 | + } else { |
|
556 | + return "$directory/$file"; |
|
557 | + } |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * add a javascript file |
|
562 | + * |
|
563 | + * @param string $application application id |
|
564 | + * @param string|null $file filename |
|
565 | + * @param bool $prepend prepend the Script to the beginning of the list |
|
566 | + * @return void |
|
567 | + */ |
|
568 | + public static function addScript($application, $file = null, $prepend = false) { |
|
569 | + $path = OC_Util::generatePath($application, 'js', $file); |
|
570 | + |
|
571 | + // core js files need separate handling |
|
572 | + if ($application !== 'core' && $file !== null) { |
|
573 | + self::addTranslations ( $application ); |
|
574 | + } |
|
575 | + self::addExternalResource($application, $prepend, $path, "script"); |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * add a javascript file from the vendor sub folder |
|
580 | + * |
|
581 | + * @param string $application application id |
|
582 | + * @param string|null $file filename |
|
583 | + * @param bool $prepend prepend the Script to the beginning of the list |
|
584 | + * @return void |
|
585 | + */ |
|
586 | + public static function addVendorScript($application, $file = null, $prepend = false) { |
|
587 | + $path = OC_Util::generatePath($application, 'vendor', $file); |
|
588 | + self::addExternalResource($application, $prepend, $path, "script"); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * add a translation JS file |
|
593 | + * |
|
594 | + * @param string $application application id |
|
595 | + * @param string|null $languageCode language code, defaults to the current language |
|
596 | + * @param bool|null $prepend prepend the Script to the beginning of the list |
|
597 | + */ |
|
598 | + public static function addTranslations($application, $languageCode = null, $prepend = false) { |
|
599 | + if (is_null($languageCode)) { |
|
600 | + $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); |
|
601 | + } |
|
602 | + if (!empty($application)) { |
|
603 | + $path = "$application/l10n/$languageCode"; |
|
604 | + } else { |
|
605 | + $path = "l10n/$languageCode"; |
|
606 | + } |
|
607 | + self::addExternalResource($application, $prepend, $path, "script"); |
|
608 | + } |
|
609 | + |
|
610 | + /** |
|
611 | + * add a css file |
|
612 | + * |
|
613 | + * @param string $application application id |
|
614 | + * @param string|null $file filename |
|
615 | + * @param bool $prepend prepend the Style to the beginning of the list |
|
616 | + * @return void |
|
617 | + */ |
|
618 | + public static function addStyle($application, $file = null, $prepend = false) { |
|
619 | + $path = OC_Util::generatePath($application, 'css', $file); |
|
620 | + self::addExternalResource($application, $prepend, $path, "style"); |
|
621 | + } |
|
622 | + |
|
623 | + /** |
|
624 | + * add a css file from the vendor sub folder |
|
625 | + * |
|
626 | + * @param string $application application id |
|
627 | + * @param string|null $file filename |
|
628 | + * @param bool $prepend prepend the Style to the beginning of the list |
|
629 | + * @return void |
|
630 | + */ |
|
631 | + public static function addVendorStyle($application, $file = null, $prepend = false) { |
|
632 | + $path = OC_Util::generatePath($application, 'vendor', $file); |
|
633 | + self::addExternalResource($application, $prepend, $path, "style"); |
|
634 | + } |
|
635 | + |
|
636 | + /** |
|
637 | + * add an external resource css/js file |
|
638 | + * |
|
639 | + * @param string $application application id |
|
640 | + * @param bool $prepend prepend the file to the beginning of the list |
|
641 | + * @param string $path |
|
642 | + * @param string $type (script or style) |
|
643 | + * @return void |
|
644 | + */ |
|
645 | + private static function addExternalResource($application, $prepend, $path, $type = "script") { |
|
646 | + |
|
647 | + if ($type === "style") { |
|
648 | + if (!in_array($path, self::$styles)) { |
|
649 | + if ($prepend === true) { |
|
650 | + array_unshift ( self::$styles, $path ); |
|
651 | + } else { |
|
652 | + self::$styles[] = $path; |
|
653 | + } |
|
654 | + } |
|
655 | + } elseif ($type === "script") { |
|
656 | + if (!in_array($path, self::$scripts)) { |
|
657 | + if ($prepend === true) { |
|
658 | + array_unshift ( self::$scripts, $path ); |
|
659 | + } else { |
|
660 | + self::$scripts [] = $path; |
|
661 | + } |
|
662 | + } |
|
663 | + } |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * Add a custom element to the header |
|
668 | + * If $text is null then the element will be written as empty element. |
|
669 | + * So use "" to get a closing tag. |
|
670 | + * @param string $tag tag name of the element |
|
671 | + * @param array $attributes array of attributes for the element |
|
672 | + * @param string $text the text content for the element |
|
673 | + */ |
|
674 | + public static function addHeader($tag, $attributes, $text=null) { |
|
675 | + self::$headers[] = array( |
|
676 | + 'tag' => $tag, |
|
677 | + 'attributes' => $attributes, |
|
678 | + 'text' => $text |
|
679 | + ); |
|
680 | + } |
|
681 | + |
|
682 | + /** |
|
683 | + * check if the current server configuration is suitable for ownCloud |
|
684 | + * |
|
685 | + * @param \OC\SystemConfig $config |
|
686 | + * @return array arrays with error messages and hints |
|
687 | + */ |
|
688 | + public static function checkServer(\OC\SystemConfig $config) { |
|
689 | + $l = \OC::$server->getL10N('lib'); |
|
690 | + $errors = array(); |
|
691 | + $CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data'); |
|
692 | + |
|
693 | + if (!self::needUpgrade($config) && $config->getValue('installed', false)) { |
|
694 | + // this check needs to be done every time |
|
695 | + $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY); |
|
696 | + } |
|
697 | + |
|
698 | + // Assume that if checkServer() succeeded before in this session, then all is fine. |
|
699 | + if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) { |
|
700 | + return $errors; |
|
701 | + } |
|
702 | + |
|
703 | + $webServerRestart = false; |
|
704 | + $setup = new \OC\Setup( |
|
705 | + $config, |
|
706 | + \OC::$server->getIniWrapper(), |
|
707 | + \OC::$server->getL10N('lib'), |
|
708 | + \OC::$server->query(\OCP\Defaults::class), |
|
709 | + \OC::$server->getLogger(), |
|
710 | + \OC::$server->getSecureRandom(), |
|
711 | + \OC::$server->query(\OC\Installer::class) |
|
712 | + ); |
|
713 | + |
|
714 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
715 | + |
|
716 | + $availableDatabases = $setup->getSupportedDatabases(); |
|
717 | + if (empty($availableDatabases)) { |
|
718 | + $errors[] = array( |
|
719 | + 'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'), |
|
720 | + 'hint' => '' //TODO: sane hint |
|
721 | + ); |
|
722 | + $webServerRestart = true; |
|
723 | + } |
|
724 | + |
|
725 | + // Check if config folder is writable. |
|
726 | + if(!OC_Helper::isReadOnlyConfigEnabled()) { |
|
727 | + if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) { |
|
728 | + $errors[] = array( |
|
729 | + 'error' => $l->t('Cannot write into "config" directory'), |
|
730 | + 'hint' => $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', |
|
731 | + [$urlGenerator->linkToDocs('admin-dir_permissions')]) |
|
732 | + ); |
|
733 | + } |
|
734 | + } |
|
735 | + |
|
736 | + // Check if there is a writable install folder. |
|
737 | + if ($config->getValue('appstoreenabled', true)) { |
|
738 | + if (OC_App::getInstallPath() === null |
|
739 | + || !is_writable(OC_App::getInstallPath()) |
|
740 | + || !is_readable(OC_App::getInstallPath()) |
|
741 | + ) { |
|
742 | + $errors[] = array( |
|
743 | + 'error' => $l->t('Cannot write into "apps" directory'), |
|
744 | + 'hint' => $l->t('This can usually be fixed by giving the webserver write access to the apps directory' |
|
745 | + . ' or disabling the appstore in the config file. See %s', |
|
746 | + [$urlGenerator->linkToDocs('admin-dir_permissions')]) |
|
747 | + ); |
|
748 | + } |
|
749 | + } |
|
750 | + // Create root dir. |
|
751 | + if ($config->getValue('installed', false)) { |
|
752 | + if (!is_dir($CONFIG_DATADIRECTORY)) { |
|
753 | + $success = @mkdir($CONFIG_DATADIRECTORY); |
|
754 | + if ($success) { |
|
755 | + $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); |
|
756 | + } else { |
|
757 | + $errors[] = [ |
|
758 | + 'error' => $l->t('Cannot create "data" directory'), |
|
759 | + 'hint' => $l->t('This can usually be fixed by giving the webserver write access to the root directory. See %s', |
|
760 | + [$urlGenerator->linkToDocs('admin-dir_permissions')]) |
|
761 | + ]; |
|
762 | + } |
|
763 | + } else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) { |
|
764 | + //common hint for all file permissions error messages |
|
765 | + $permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.', |
|
766 | + [$urlGenerator->linkToDocs('admin-dir_permissions')]); |
|
767 | + $errors[] = [ |
|
768 | + 'error' => 'Your data directory is not writable', |
|
769 | + 'hint' => $permissionsHint |
|
770 | + ]; |
|
771 | + } else { |
|
772 | + $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); |
|
773 | + } |
|
774 | + } |
|
775 | + |
|
776 | + if (!OC_Util::isSetLocaleWorking()) { |
|
777 | + $errors[] = array( |
|
778 | + 'error' => $l->t('Setting locale to %s failed', |
|
779 | + array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' |
|
780 | + . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')), |
|
781 | + 'hint' => $l->t('Please install one of these locales on your system and restart your webserver.') |
|
782 | + ); |
|
783 | + } |
|
784 | + |
|
785 | + // Contains the dependencies that should be checked against |
|
786 | + // classes = class_exists |
|
787 | + // functions = function_exists |
|
788 | + // defined = defined |
|
789 | + // ini = ini_get |
|
790 | + // If the dependency is not found the missing module name is shown to the EndUser |
|
791 | + // When adding new checks always verify that they pass on Travis as well |
|
792 | + // for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini |
|
793 | + $dependencies = array( |
|
794 | + 'classes' => array( |
|
795 | + 'ZipArchive' => 'zip', |
|
796 | + 'DOMDocument' => 'dom', |
|
797 | + 'XMLWriter' => 'XMLWriter', |
|
798 | + 'XMLReader' => 'XMLReader', |
|
799 | + ), |
|
800 | + 'functions' => [ |
|
801 | + 'xml_parser_create' => 'libxml', |
|
802 | + 'mb_strcut' => 'mb multibyte', |
|
803 | + 'ctype_digit' => 'ctype', |
|
804 | + 'json_encode' => 'JSON', |
|
805 | + 'gd_info' => 'GD', |
|
806 | + 'gzencode' => 'zlib', |
|
807 | + 'iconv' => 'iconv', |
|
808 | + 'simplexml_load_string' => 'SimpleXML', |
|
809 | + 'hash' => 'HASH Message Digest Framework', |
|
810 | + 'curl_init' => 'cURL', |
|
811 | + 'openssl_verify' => 'OpenSSL', |
|
812 | + ], |
|
813 | + 'defined' => array( |
|
814 | + 'PDO::ATTR_DRIVER_NAME' => 'PDO' |
|
815 | + ), |
|
816 | + 'ini' => [ |
|
817 | + 'default_charset' => 'UTF-8', |
|
818 | + ], |
|
819 | + ); |
|
820 | + $missingDependencies = array(); |
|
821 | + $invalidIniSettings = []; |
|
822 | + $moduleHint = $l->t('Please ask your server administrator to install the module.'); |
|
823 | + |
|
824 | + /** |
|
825 | + * FIXME: The dependency check does not work properly on HHVM on the moment |
|
826 | + * and prevents installation. Once HHVM is more compatible with our |
|
827 | + * approach to check for these values we should re-enable those |
|
828 | + * checks. |
|
829 | + */ |
|
830 | + $iniWrapper = \OC::$server->getIniWrapper(); |
|
831 | + if (!self::runningOnHhvm()) { |
|
832 | + foreach ($dependencies['classes'] as $class => $module) { |
|
833 | + if (!class_exists($class)) { |
|
834 | + $missingDependencies[] = $module; |
|
835 | + } |
|
836 | + } |
|
837 | + foreach ($dependencies['functions'] as $function => $module) { |
|
838 | + if (!function_exists($function)) { |
|
839 | + $missingDependencies[] = $module; |
|
840 | + } |
|
841 | + } |
|
842 | + foreach ($dependencies['defined'] as $defined => $module) { |
|
843 | + if (!defined($defined)) { |
|
844 | + $missingDependencies[] = $module; |
|
845 | + } |
|
846 | + } |
|
847 | + foreach ($dependencies['ini'] as $setting => $expected) { |
|
848 | + if (is_bool($expected)) { |
|
849 | + if ($iniWrapper->getBool($setting) !== $expected) { |
|
850 | + $invalidIniSettings[] = [$setting, $expected]; |
|
851 | + } |
|
852 | + } |
|
853 | + if (is_int($expected)) { |
|
854 | + if ($iniWrapper->getNumeric($setting) !== $expected) { |
|
855 | + $invalidIniSettings[] = [$setting, $expected]; |
|
856 | + } |
|
857 | + } |
|
858 | + if (is_string($expected)) { |
|
859 | + if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) { |
|
860 | + $invalidIniSettings[] = [$setting, $expected]; |
|
861 | + } |
|
862 | + } |
|
863 | + } |
|
864 | + } |
|
865 | + |
|
866 | + foreach($missingDependencies as $missingDependency) { |
|
867 | + $errors[] = array( |
|
868 | + 'error' => $l->t('PHP module %s not installed.', array($missingDependency)), |
|
869 | + 'hint' => $moduleHint |
|
870 | + ); |
|
871 | + $webServerRestart = true; |
|
872 | + } |
|
873 | + foreach($invalidIniSettings as $setting) { |
|
874 | + if(is_bool($setting[1])) { |
|
875 | + $setting[1] = $setting[1] ? 'on' : 'off'; |
|
876 | + } |
|
877 | + $errors[] = [ |
|
878 | + 'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]), |
|
879 | + 'hint' => $l->t('Adjusting this setting in php.ini will make Nextcloud run again') |
|
880 | + ]; |
|
881 | + $webServerRestart = true; |
|
882 | + } |
|
883 | + |
|
884 | + /** |
|
885 | + * The mbstring.func_overload check can only be performed if the mbstring |
|
886 | + * module is installed as it will return null if the checking setting is |
|
887 | + * not available and thus a check on the boolean value fails. |
|
888 | + * |
|
889 | + * TODO: Should probably be implemented in the above generic dependency |
|
890 | + * check somehow in the long-term. |
|
891 | + */ |
|
892 | + if($iniWrapper->getBool('mbstring.func_overload') !== null && |
|
893 | + $iniWrapper->getBool('mbstring.func_overload') === true) { |
|
894 | + $errors[] = array( |
|
895 | + 'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]), |
|
896 | + 'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini') |
|
897 | + ); |
|
898 | + } |
|
899 | + |
|
900 | + if(function_exists('xml_parser_create') && |
|
901 | + LIBXML_LOADED_VERSION < 20700 ) { |
|
902 | + $version = LIBXML_LOADED_VERSION; |
|
903 | + $major = floor($version/10000); |
|
904 | + $version -= ($major * 10000); |
|
905 | + $minor = floor($version/100); |
|
906 | + $version -= ($minor * 100); |
|
907 | + $patch = $version; |
|
908 | + $errors[] = array( |
|
909 | + 'error' => $l->t('libxml2 2.7.0 is at least required. Currently %s is installed.', [$major . '.' . $minor . '.' . $patch]), |
|
910 | + 'hint' => $l->t('To fix this issue update your libxml2 version and restart your web server.') |
|
911 | + ); |
|
912 | + } |
|
913 | + |
|
914 | + if (!self::isAnnotationsWorking()) { |
|
915 | + $errors[] = array( |
|
916 | + 'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'), |
|
917 | + 'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.') |
|
918 | + ); |
|
919 | + } |
|
920 | + |
|
921 | + if (!\OC::$CLI && $webServerRestart) { |
|
922 | + $errors[] = array( |
|
923 | + 'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'), |
|
924 | + 'hint' => $l->t('Please ask your server administrator to restart the web server.') |
|
925 | + ); |
|
926 | + } |
|
927 | + |
|
928 | + $errors = array_merge($errors, self::checkDatabaseVersion()); |
|
929 | + |
|
930 | + // Cache the result of this function |
|
931 | + \OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0); |
|
932 | + |
|
933 | + return $errors; |
|
934 | + } |
|
935 | + |
|
936 | + /** |
|
937 | + * Check the database version |
|
938 | + * |
|
939 | + * @return array errors array |
|
940 | + */ |
|
941 | + public static function checkDatabaseVersion() { |
|
942 | + $l = \OC::$server->getL10N('lib'); |
|
943 | + $errors = array(); |
|
944 | + $dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite'); |
|
945 | + if ($dbType === 'pgsql') { |
|
946 | + // check PostgreSQL version |
|
947 | + try { |
|
948 | + $result = \OC_DB::executeAudited('SHOW SERVER_VERSION'); |
|
949 | + $data = $result->fetchRow(); |
|
950 | + if (isset($data['server_version'])) { |
|
951 | + $version = $data['server_version']; |
|
952 | + if (version_compare($version, '9.0.0', '<')) { |
|
953 | + $errors[] = array( |
|
954 | + 'error' => $l->t('PostgreSQL >= 9 required'), |
|
955 | + 'hint' => $l->t('Please upgrade your database version') |
|
956 | + ); |
|
957 | + } |
|
958 | + } |
|
959 | + } catch (\Doctrine\DBAL\DBALException $e) { |
|
960 | + $logger = \OC::$server->getLogger(); |
|
961 | + $logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9'); |
|
962 | + $logger->logException($e); |
|
963 | + } |
|
964 | + } |
|
965 | + return $errors; |
|
966 | + } |
|
967 | + |
|
968 | + /** |
|
969 | + * Check for correct file permissions of data directory |
|
970 | + * |
|
971 | + * @param string $dataDirectory |
|
972 | + * @return array arrays with error messages and hints |
|
973 | + */ |
|
974 | + public static function checkDataDirectoryPermissions($dataDirectory) { |
|
975 | + if(\OC::$server->getConfig()->getSystemValue('check_data_directory_permissions', true) === false) { |
|
976 | + return []; |
|
977 | + } |
|
978 | + $l = \OC::$server->getL10N('lib'); |
|
979 | + $errors = []; |
|
980 | + $permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory' |
|
981 | + . ' cannot be listed by other users.'); |
|
982 | + $perms = substr(decoct(@fileperms($dataDirectory)), -3); |
|
983 | + if (substr($perms, -1) !== '0') { |
|
984 | + chmod($dataDirectory, 0770); |
|
985 | + clearstatcache(); |
|
986 | + $perms = substr(decoct(@fileperms($dataDirectory)), -3); |
|
987 | + if ($perms[2] !== '0') { |
|
988 | + $errors[] = [ |
|
989 | + 'error' => $l->t('Your data directory is readable by other users'), |
|
990 | + 'hint' => $permissionsModHint |
|
991 | + ]; |
|
992 | + } |
|
993 | + } |
|
994 | + return $errors; |
|
995 | + } |
|
996 | + |
|
997 | + /** |
|
998 | + * Check that the data directory exists and is valid by |
|
999 | + * checking the existence of the ".ocdata" file. |
|
1000 | + * |
|
1001 | + * @param string $dataDirectory data directory path |
|
1002 | + * @return array errors found |
|
1003 | + */ |
|
1004 | + public static function checkDataDirectoryValidity($dataDirectory) { |
|
1005 | + $l = \OC::$server->getL10N('lib'); |
|
1006 | + $errors = []; |
|
1007 | + if ($dataDirectory[0] !== '/') { |
|
1008 | + $errors[] = [ |
|
1009 | + 'error' => $l->t('Your data directory must be an absolute path'), |
|
1010 | + 'hint' => $l->t('Check the value of "datadirectory" in your configuration') |
|
1011 | + ]; |
|
1012 | + } |
|
1013 | + if (!file_exists($dataDirectory . '/.ocdata')) { |
|
1014 | + $errors[] = [ |
|
1015 | + 'error' => $l->t('Your data directory is invalid'), |
|
1016 | + 'hint' => $l->t('Ensure there is a file called ".ocdata"' . |
|
1017 | + ' in the root of the data directory.') |
|
1018 | + ]; |
|
1019 | + } |
|
1020 | + return $errors; |
|
1021 | + } |
|
1022 | + |
|
1023 | + /** |
|
1024 | + * Check if the user is logged in, redirects to home if not. With |
|
1025 | + * redirect URL parameter to the request URI. |
|
1026 | + * |
|
1027 | + * @return void |
|
1028 | + */ |
|
1029 | + public static function checkLoggedIn() { |
|
1030 | + // Check if we are a user |
|
1031 | + if (!\OC::$server->getUserSession()->isLoggedIn()) { |
|
1032 | + header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute( |
|
1033 | + 'core.login.showLoginForm', |
|
1034 | + [ |
|
1035 | + 'redirect_url' => \OC::$server->getRequest()->getRequestUri(), |
|
1036 | + ] |
|
1037 | + ) |
|
1038 | + ); |
|
1039 | + exit(); |
|
1040 | + } |
|
1041 | + // Redirect to 2FA challenge selection if 2FA challenge was not solved yet |
|
1042 | + if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
1043 | + header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
1044 | + exit(); |
|
1045 | + } |
|
1046 | + } |
|
1047 | + |
|
1048 | + /** |
|
1049 | + * Check if the user is a admin, redirects to home if not |
|
1050 | + * |
|
1051 | + * @return void |
|
1052 | + */ |
|
1053 | + public static function checkAdminUser() { |
|
1054 | + OC_Util::checkLoggedIn(); |
|
1055 | + if (!OC_User::isAdminUser(OC_User::getUser())) { |
|
1056 | + header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); |
|
1057 | + exit(); |
|
1058 | + } |
|
1059 | + } |
|
1060 | + |
|
1061 | + /** |
|
1062 | + * Check if the user is a subadmin, redirects to home if not |
|
1063 | + * |
|
1064 | + * @return null|boolean $groups where the current user is subadmin |
|
1065 | + */ |
|
1066 | + public static function checkSubAdminUser() { |
|
1067 | + OC_Util::checkLoggedIn(); |
|
1068 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
1069 | + $isSubAdmin = false; |
|
1070 | + if($userObject !== null) { |
|
1071 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
1072 | + } |
|
1073 | + |
|
1074 | + if (!$isSubAdmin) { |
|
1075 | + header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); |
|
1076 | + exit(); |
|
1077 | + } |
|
1078 | + return true; |
|
1079 | + } |
|
1080 | + |
|
1081 | + /** |
|
1082 | + * Returns the URL of the default page |
|
1083 | + * based on the system configuration and |
|
1084 | + * the apps visible for the current user |
|
1085 | + * |
|
1086 | + * @return string URL |
|
1087 | + * @suppress PhanDeprecatedFunction |
|
1088 | + */ |
|
1089 | + public static function getDefaultPageUrl() { |
|
1090 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
1091 | + // Deny the redirect if the URL contains a @ |
|
1092 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
1093 | + if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) { |
|
1094 | + $location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); |
|
1095 | + } else { |
|
1096 | + $defaultPage = \OC::$server->getConfig()->getAppValue('core', 'defaultpage'); |
|
1097 | + if ($defaultPage) { |
|
1098 | + $location = $urlGenerator->getAbsoluteURL($defaultPage); |
|
1099 | + } else { |
|
1100 | + $appId = 'files'; |
|
1101 | + $config = \OC::$server->getConfig(); |
|
1102 | + $defaultApps = explode(',', $config->getSystemValue('defaultapp', 'files')); |
|
1103 | + // find the first app that is enabled for the current user |
|
1104 | + foreach ($defaultApps as $defaultApp) { |
|
1105 | + $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); |
|
1106 | + if (static::getAppManager()->isEnabledForUser($defaultApp)) { |
|
1107 | + $appId = $defaultApp; |
|
1108 | + break; |
|
1109 | + } |
|
1110 | + } |
|
1111 | + |
|
1112 | + if($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') { |
|
1113 | + $location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/'); |
|
1114 | + } else { |
|
1115 | + $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/'); |
|
1116 | + } |
|
1117 | + } |
|
1118 | + } |
|
1119 | + return $location; |
|
1120 | + } |
|
1121 | + |
|
1122 | + /** |
|
1123 | + * Redirect to the user default page |
|
1124 | + * |
|
1125 | + * @return void |
|
1126 | + */ |
|
1127 | + public static function redirectToDefaultPage() { |
|
1128 | + $location = self::getDefaultPageUrl(); |
|
1129 | + header('Location: ' . $location); |
|
1130 | + exit(); |
|
1131 | + } |
|
1132 | + |
|
1133 | + /** |
|
1134 | + * get an id unique for this instance |
|
1135 | + * |
|
1136 | + * @return string |
|
1137 | + */ |
|
1138 | + public static function getInstanceId() { |
|
1139 | + $id = \OC::$server->getSystemConfig()->getValue('instanceid', null); |
|
1140 | + if (is_null($id)) { |
|
1141 | + // We need to guarantee at least one letter in instanceid so it can be used as the session_name |
|
1142 | + $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
1143 | + \OC::$server->getSystemConfig()->setValue('instanceid', $id); |
|
1144 | + } |
|
1145 | + return $id; |
|
1146 | + } |
|
1147 | + |
|
1148 | + /** |
|
1149 | + * Public function to sanitize HTML |
|
1150 | + * |
|
1151 | + * This function is used to sanitize HTML and should be applied on any |
|
1152 | + * string or array of strings before displaying it on a web page. |
|
1153 | + * |
|
1154 | + * @param string|array $value |
|
1155 | + * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
1156 | + */ |
|
1157 | + public static function sanitizeHTML($value) { |
|
1158 | + if (is_array($value)) { |
|
1159 | + $value = array_map(function($value) { |
|
1160 | + return self::sanitizeHTML($value); |
|
1161 | + }, $value); |
|
1162 | + } else { |
|
1163 | + // Specify encoding for PHP<5.4 |
|
1164 | + $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); |
|
1165 | + } |
|
1166 | + return $value; |
|
1167 | + } |
|
1168 | + |
|
1169 | + /** |
|
1170 | + * Public function to encode url parameters |
|
1171 | + * |
|
1172 | + * This function is used to encode path to file before output. |
|
1173 | + * Encoding is done according to RFC 3986 with one exception: |
|
1174 | + * Character '/' is preserved as is. |
|
1175 | + * |
|
1176 | + * @param string $component part of URI to encode |
|
1177 | + * @return string |
|
1178 | + */ |
|
1179 | + public static function encodePath($component) { |
|
1180 | + $encoded = rawurlencode($component); |
|
1181 | + $encoded = str_replace('%2F', '/', $encoded); |
|
1182 | + return $encoded; |
|
1183 | + } |
|
1184 | + |
|
1185 | + |
|
1186 | + public function createHtaccessTestFile(\OCP\IConfig $config) { |
|
1187 | + // php dev server does not support htaccess |
|
1188 | + if (php_sapi_name() === 'cli-server') { |
|
1189 | + return false; |
|
1190 | + } |
|
1191 | + |
|
1192 | + // testdata |
|
1193 | + $fileName = '/htaccesstest.txt'; |
|
1194 | + $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.'; |
|
1195 | + |
|
1196 | + // creating a test file |
|
1197 | + $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; |
|
1198 | + |
|
1199 | + if (file_exists($testFile)) {// already running this test, possible recursive call |
|
1200 | + return false; |
|
1201 | + } |
|
1202 | + |
|
1203 | + $fp = @fopen($testFile, 'w'); |
|
1204 | + if (!$fp) { |
|
1205 | + throw new OC\HintException('Can\'t create test file to check for working .htaccess file.', |
|
1206 | + 'Make sure it is possible for the webserver to write to ' . $testFile); |
|
1207 | + } |
|
1208 | + fwrite($fp, $testContent); |
|
1209 | + fclose($fp); |
|
1210 | + |
|
1211 | + return $testContent; |
|
1212 | + } |
|
1213 | + |
|
1214 | + /** |
|
1215 | + * Check if the .htaccess file is working |
|
1216 | + * @param \OCP\IConfig $config |
|
1217 | + * @return bool |
|
1218 | + * @throws Exception |
|
1219 | + * @throws \OC\HintException If the test file can't get written. |
|
1220 | + */ |
|
1221 | + public function isHtaccessWorking(\OCP\IConfig $config) { |
|
1222 | + |
|
1223 | + if (\OC::$CLI || !$config->getSystemValue('check_for_working_htaccess', true)) { |
|
1224 | + return true; |
|
1225 | + } |
|
1226 | + |
|
1227 | + $testContent = $this->createHtaccessTestFile($config); |
|
1228 | + if ($testContent === false) { |
|
1229 | + return false; |
|
1230 | + } |
|
1231 | + |
|
1232 | + $fileName = '/htaccesstest.txt'; |
|
1233 | + $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; |
|
1234 | + |
|
1235 | + // accessing the file via http |
|
1236 | + $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName); |
|
1237 | + try { |
|
1238 | + $content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody(); |
|
1239 | + } catch (\Exception $e) { |
|
1240 | + $content = false; |
|
1241 | + } |
|
1242 | + |
|
1243 | + // cleanup |
|
1244 | + @unlink($testFile); |
|
1245 | + |
|
1246 | + /* |
|
1247 | 1247 | * If the content is not equal to test content our .htaccess |
1248 | 1248 | * is working as required |
1249 | 1249 | */ |
1250 | - return $content !== $testContent; |
|
1251 | - } |
|
1252 | - |
|
1253 | - /** |
|
1254 | - * Check if the setlocal call does not work. This can happen if the right |
|
1255 | - * local packages are not available on the server. |
|
1256 | - * |
|
1257 | - * @return bool |
|
1258 | - */ |
|
1259 | - public static function isSetLocaleWorking() { |
|
1260 | - \Patchwork\Utf8\Bootup::initLocale(); |
|
1261 | - if ('' === basename('§')) { |
|
1262 | - return false; |
|
1263 | - } |
|
1264 | - return true; |
|
1265 | - } |
|
1266 | - |
|
1267 | - /** |
|
1268 | - * Check if it's possible to get the inline annotations |
|
1269 | - * |
|
1270 | - * @return bool |
|
1271 | - */ |
|
1272 | - public static function isAnnotationsWorking() { |
|
1273 | - $reflection = new \ReflectionMethod(__METHOD__); |
|
1274 | - $docs = $reflection->getDocComment(); |
|
1275 | - |
|
1276 | - return (is_string($docs) && strlen($docs) > 50); |
|
1277 | - } |
|
1278 | - |
|
1279 | - /** |
|
1280 | - * Check if the PHP module fileinfo is loaded. |
|
1281 | - * |
|
1282 | - * @return bool |
|
1283 | - */ |
|
1284 | - public static function fileInfoLoaded() { |
|
1285 | - return function_exists('finfo_open'); |
|
1286 | - } |
|
1287 | - |
|
1288 | - /** |
|
1289 | - * clear all levels of output buffering |
|
1290 | - * |
|
1291 | - * @return void |
|
1292 | - */ |
|
1293 | - public static function obEnd() { |
|
1294 | - while (ob_get_level()) { |
|
1295 | - ob_end_clean(); |
|
1296 | - } |
|
1297 | - } |
|
1298 | - |
|
1299 | - /** |
|
1300 | - * Checks whether the server is running on Mac OS X |
|
1301 | - * |
|
1302 | - * @return bool true if running on Mac OS X, false otherwise |
|
1303 | - */ |
|
1304 | - public static function runningOnMac() { |
|
1305 | - return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN'); |
|
1306 | - } |
|
1307 | - |
|
1308 | - /** |
|
1309 | - * Checks whether server is running on HHVM |
|
1310 | - * |
|
1311 | - * @return bool True if running on HHVM, false otherwise |
|
1312 | - */ |
|
1313 | - public static function runningOnHhvm() { |
|
1314 | - return defined('HHVM_VERSION'); |
|
1315 | - } |
|
1316 | - |
|
1317 | - /** |
|
1318 | - * Handles the case that there may not be a theme, then check if a "default" |
|
1319 | - * theme exists and take that one |
|
1320 | - * |
|
1321 | - * @return string the theme |
|
1322 | - */ |
|
1323 | - public static function getTheme() { |
|
1324 | - $theme = \OC::$server->getSystemConfig()->getValue("theme", ''); |
|
1325 | - |
|
1326 | - if ($theme === '') { |
|
1327 | - if (is_dir(OC::$SERVERROOT . '/themes/default')) { |
|
1328 | - $theme = 'default'; |
|
1329 | - } |
|
1330 | - } |
|
1331 | - |
|
1332 | - return $theme; |
|
1333 | - } |
|
1334 | - |
|
1335 | - /** |
|
1336 | - * Clear a single file from the opcode cache |
|
1337 | - * This is useful for writing to the config file |
|
1338 | - * in case the opcode cache does not re-validate files |
|
1339 | - * Returns true if successful, false if unsuccessful: |
|
1340 | - * caller should fall back on clearing the entire cache |
|
1341 | - * with clearOpcodeCache() if unsuccessful |
|
1342 | - * |
|
1343 | - * @param string $path the path of the file to clear from the cache |
|
1344 | - * @return bool true if underlying function returns true, otherwise false |
|
1345 | - */ |
|
1346 | - public static function deleteFromOpcodeCache($path) { |
|
1347 | - $ret = false; |
|
1348 | - if ($path) { |
|
1349 | - // APC >= 3.1.1 |
|
1350 | - if (function_exists('apc_delete_file')) { |
|
1351 | - $ret = @apc_delete_file($path); |
|
1352 | - } |
|
1353 | - // Zend OpCache >= 7.0.0, PHP >= 5.5.0 |
|
1354 | - if (function_exists('opcache_invalidate')) { |
|
1355 | - $ret = opcache_invalidate($path); |
|
1356 | - } |
|
1357 | - } |
|
1358 | - return $ret; |
|
1359 | - } |
|
1360 | - |
|
1361 | - /** |
|
1362 | - * Clear the opcode cache if one exists |
|
1363 | - * This is necessary for writing to the config file |
|
1364 | - * in case the opcode cache does not re-validate files |
|
1365 | - * |
|
1366 | - * @return void |
|
1367 | - * @suppress PhanDeprecatedFunction |
|
1368 | - * @suppress PhanUndeclaredConstant |
|
1369 | - */ |
|
1370 | - public static function clearOpcodeCache() { |
|
1371 | - // APC |
|
1372 | - if (function_exists('apc_clear_cache')) { |
|
1373 | - apc_clear_cache(); |
|
1374 | - } |
|
1375 | - // Zend Opcache |
|
1376 | - if (function_exists('accelerator_reset')) { |
|
1377 | - accelerator_reset(); |
|
1378 | - } |
|
1379 | - // XCache |
|
1380 | - if (function_exists('xcache_clear_cache')) { |
|
1381 | - if (\OC::$server->getIniWrapper()->getBool('xcache.admin.enable_auth')) { |
|
1382 | - \OCP\Util::writeLog('core', 'XCache opcode cache will not be cleared because "xcache.admin.enable_auth" is enabled.', ILogger::WARN); |
|
1383 | - } else { |
|
1384 | - @xcache_clear_cache(XC_TYPE_PHP, 0); |
|
1385 | - } |
|
1386 | - } |
|
1387 | - // Opcache (PHP >= 5.5) |
|
1388 | - if (function_exists('opcache_reset')) { |
|
1389 | - opcache_reset(); |
|
1390 | - } |
|
1391 | - } |
|
1392 | - |
|
1393 | - /** |
|
1394 | - * Normalize a unicode string |
|
1395 | - * |
|
1396 | - * @param string $value a not normalized string |
|
1397 | - * @return bool|string |
|
1398 | - */ |
|
1399 | - public static function normalizeUnicode($value) { |
|
1400 | - if(Normalizer::isNormalized($value)) { |
|
1401 | - return $value; |
|
1402 | - } |
|
1403 | - |
|
1404 | - $normalizedValue = Normalizer::normalize($value); |
|
1405 | - if ($normalizedValue === null || $normalizedValue === false) { |
|
1406 | - \OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']); |
|
1407 | - return $value; |
|
1408 | - } |
|
1409 | - |
|
1410 | - return $normalizedValue; |
|
1411 | - } |
|
1412 | - |
|
1413 | - /** |
|
1414 | - * A human readable string is generated based on version and build number |
|
1415 | - * |
|
1416 | - * @return string |
|
1417 | - */ |
|
1418 | - public static function getHumanVersion() { |
|
1419 | - $version = OC_Util::getVersionString(); |
|
1420 | - $build = OC_Util::getBuild(); |
|
1421 | - if (!empty($build) and OC_Util::getChannel() === 'daily') { |
|
1422 | - $version .= ' Build:' . $build; |
|
1423 | - } |
|
1424 | - return $version; |
|
1425 | - } |
|
1426 | - |
|
1427 | - /** |
|
1428 | - * Returns whether the given file name is valid |
|
1429 | - * |
|
1430 | - * @param string $file file name to check |
|
1431 | - * @return bool true if the file name is valid, false otherwise |
|
1432 | - * @deprecated use \OC\Files\View::verifyPath() |
|
1433 | - */ |
|
1434 | - public static function isValidFileName($file) { |
|
1435 | - $trimmed = trim($file); |
|
1436 | - if ($trimmed === '') { |
|
1437 | - return false; |
|
1438 | - } |
|
1439 | - if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { |
|
1440 | - return false; |
|
1441 | - } |
|
1442 | - |
|
1443 | - // detect part files |
|
1444 | - if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) { |
|
1445 | - return false; |
|
1446 | - } |
|
1447 | - |
|
1448 | - foreach (str_split($trimmed) as $char) { |
|
1449 | - if (strpos(\OCP\Constants::FILENAME_INVALID_CHARS, $char) !== false) { |
|
1450 | - return false; |
|
1451 | - } |
|
1452 | - } |
|
1453 | - return true; |
|
1454 | - } |
|
1455 | - |
|
1456 | - /** |
|
1457 | - * Check whether the instance needs to perform an upgrade, |
|
1458 | - * either when the core version is higher or any app requires |
|
1459 | - * an upgrade. |
|
1460 | - * |
|
1461 | - * @param \OC\SystemConfig $config |
|
1462 | - * @return bool whether the core or any app needs an upgrade |
|
1463 | - * @throws \OC\HintException When the upgrade from the given version is not allowed |
|
1464 | - */ |
|
1465 | - public static function needUpgrade(\OC\SystemConfig $config) { |
|
1466 | - if ($config->getValue('installed', false)) { |
|
1467 | - $installedVersion = $config->getValue('version', '0.0.0'); |
|
1468 | - $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
1469 | - $versionDiff = version_compare($currentVersion, $installedVersion); |
|
1470 | - if ($versionDiff > 0) { |
|
1471 | - return true; |
|
1472 | - } else if ($config->getValue('debug', false) && $versionDiff < 0) { |
|
1473 | - // downgrade with debug |
|
1474 | - $installedMajor = explode('.', $installedVersion); |
|
1475 | - $installedMajor = $installedMajor[0] . '.' . $installedMajor[1]; |
|
1476 | - $currentMajor = explode('.', $currentVersion); |
|
1477 | - $currentMajor = $currentMajor[0] . '.' . $currentMajor[1]; |
|
1478 | - if ($installedMajor === $currentMajor) { |
|
1479 | - // Same major, allow downgrade for developers |
|
1480 | - return true; |
|
1481 | - } else { |
|
1482 | - // downgrade attempt, throw exception |
|
1483 | - throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); |
|
1484 | - } |
|
1485 | - } else if ($versionDiff < 0) { |
|
1486 | - // downgrade attempt, throw exception |
|
1487 | - throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); |
|
1488 | - } |
|
1489 | - |
|
1490 | - // also check for upgrades for apps (independently from the user) |
|
1491 | - $apps = \OC_App::getEnabledApps(false, true); |
|
1492 | - $shouldUpgrade = false; |
|
1493 | - foreach ($apps as $app) { |
|
1494 | - if (\OC_App::shouldUpgrade($app)) { |
|
1495 | - $shouldUpgrade = true; |
|
1496 | - break; |
|
1497 | - } |
|
1498 | - } |
|
1499 | - return $shouldUpgrade; |
|
1500 | - } else { |
|
1501 | - return false; |
|
1502 | - } |
|
1503 | - } |
|
1250 | + return $content !== $testContent; |
|
1251 | + } |
|
1252 | + |
|
1253 | + /** |
|
1254 | + * Check if the setlocal call does not work. This can happen if the right |
|
1255 | + * local packages are not available on the server. |
|
1256 | + * |
|
1257 | + * @return bool |
|
1258 | + */ |
|
1259 | + public static function isSetLocaleWorking() { |
|
1260 | + \Patchwork\Utf8\Bootup::initLocale(); |
|
1261 | + if ('' === basename('§')) { |
|
1262 | + return false; |
|
1263 | + } |
|
1264 | + return true; |
|
1265 | + } |
|
1266 | + |
|
1267 | + /** |
|
1268 | + * Check if it's possible to get the inline annotations |
|
1269 | + * |
|
1270 | + * @return bool |
|
1271 | + */ |
|
1272 | + public static function isAnnotationsWorking() { |
|
1273 | + $reflection = new \ReflectionMethod(__METHOD__); |
|
1274 | + $docs = $reflection->getDocComment(); |
|
1275 | + |
|
1276 | + return (is_string($docs) && strlen($docs) > 50); |
|
1277 | + } |
|
1278 | + |
|
1279 | + /** |
|
1280 | + * Check if the PHP module fileinfo is loaded. |
|
1281 | + * |
|
1282 | + * @return bool |
|
1283 | + */ |
|
1284 | + public static function fileInfoLoaded() { |
|
1285 | + return function_exists('finfo_open'); |
|
1286 | + } |
|
1287 | + |
|
1288 | + /** |
|
1289 | + * clear all levels of output buffering |
|
1290 | + * |
|
1291 | + * @return void |
|
1292 | + */ |
|
1293 | + public static function obEnd() { |
|
1294 | + while (ob_get_level()) { |
|
1295 | + ob_end_clean(); |
|
1296 | + } |
|
1297 | + } |
|
1298 | + |
|
1299 | + /** |
|
1300 | + * Checks whether the server is running on Mac OS X |
|
1301 | + * |
|
1302 | + * @return bool true if running on Mac OS X, false otherwise |
|
1303 | + */ |
|
1304 | + public static function runningOnMac() { |
|
1305 | + return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN'); |
|
1306 | + } |
|
1307 | + |
|
1308 | + /** |
|
1309 | + * Checks whether server is running on HHVM |
|
1310 | + * |
|
1311 | + * @return bool True if running on HHVM, false otherwise |
|
1312 | + */ |
|
1313 | + public static function runningOnHhvm() { |
|
1314 | + return defined('HHVM_VERSION'); |
|
1315 | + } |
|
1316 | + |
|
1317 | + /** |
|
1318 | + * Handles the case that there may not be a theme, then check if a "default" |
|
1319 | + * theme exists and take that one |
|
1320 | + * |
|
1321 | + * @return string the theme |
|
1322 | + */ |
|
1323 | + public static function getTheme() { |
|
1324 | + $theme = \OC::$server->getSystemConfig()->getValue("theme", ''); |
|
1325 | + |
|
1326 | + if ($theme === '') { |
|
1327 | + if (is_dir(OC::$SERVERROOT . '/themes/default')) { |
|
1328 | + $theme = 'default'; |
|
1329 | + } |
|
1330 | + } |
|
1331 | + |
|
1332 | + return $theme; |
|
1333 | + } |
|
1334 | + |
|
1335 | + /** |
|
1336 | + * Clear a single file from the opcode cache |
|
1337 | + * This is useful for writing to the config file |
|
1338 | + * in case the opcode cache does not re-validate files |
|
1339 | + * Returns true if successful, false if unsuccessful: |
|
1340 | + * caller should fall back on clearing the entire cache |
|
1341 | + * with clearOpcodeCache() if unsuccessful |
|
1342 | + * |
|
1343 | + * @param string $path the path of the file to clear from the cache |
|
1344 | + * @return bool true if underlying function returns true, otherwise false |
|
1345 | + */ |
|
1346 | + public static function deleteFromOpcodeCache($path) { |
|
1347 | + $ret = false; |
|
1348 | + if ($path) { |
|
1349 | + // APC >= 3.1.1 |
|
1350 | + if (function_exists('apc_delete_file')) { |
|
1351 | + $ret = @apc_delete_file($path); |
|
1352 | + } |
|
1353 | + // Zend OpCache >= 7.0.0, PHP >= 5.5.0 |
|
1354 | + if (function_exists('opcache_invalidate')) { |
|
1355 | + $ret = opcache_invalidate($path); |
|
1356 | + } |
|
1357 | + } |
|
1358 | + return $ret; |
|
1359 | + } |
|
1360 | + |
|
1361 | + /** |
|
1362 | + * Clear the opcode cache if one exists |
|
1363 | + * This is necessary for writing to the config file |
|
1364 | + * in case the opcode cache does not re-validate files |
|
1365 | + * |
|
1366 | + * @return void |
|
1367 | + * @suppress PhanDeprecatedFunction |
|
1368 | + * @suppress PhanUndeclaredConstant |
|
1369 | + */ |
|
1370 | + public static function clearOpcodeCache() { |
|
1371 | + // APC |
|
1372 | + if (function_exists('apc_clear_cache')) { |
|
1373 | + apc_clear_cache(); |
|
1374 | + } |
|
1375 | + // Zend Opcache |
|
1376 | + if (function_exists('accelerator_reset')) { |
|
1377 | + accelerator_reset(); |
|
1378 | + } |
|
1379 | + // XCache |
|
1380 | + if (function_exists('xcache_clear_cache')) { |
|
1381 | + if (\OC::$server->getIniWrapper()->getBool('xcache.admin.enable_auth')) { |
|
1382 | + \OCP\Util::writeLog('core', 'XCache opcode cache will not be cleared because "xcache.admin.enable_auth" is enabled.', ILogger::WARN); |
|
1383 | + } else { |
|
1384 | + @xcache_clear_cache(XC_TYPE_PHP, 0); |
|
1385 | + } |
|
1386 | + } |
|
1387 | + // Opcache (PHP >= 5.5) |
|
1388 | + if (function_exists('opcache_reset')) { |
|
1389 | + opcache_reset(); |
|
1390 | + } |
|
1391 | + } |
|
1392 | + |
|
1393 | + /** |
|
1394 | + * Normalize a unicode string |
|
1395 | + * |
|
1396 | + * @param string $value a not normalized string |
|
1397 | + * @return bool|string |
|
1398 | + */ |
|
1399 | + public static function normalizeUnicode($value) { |
|
1400 | + if(Normalizer::isNormalized($value)) { |
|
1401 | + return $value; |
|
1402 | + } |
|
1403 | + |
|
1404 | + $normalizedValue = Normalizer::normalize($value); |
|
1405 | + if ($normalizedValue === null || $normalizedValue === false) { |
|
1406 | + \OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']); |
|
1407 | + return $value; |
|
1408 | + } |
|
1409 | + |
|
1410 | + return $normalizedValue; |
|
1411 | + } |
|
1412 | + |
|
1413 | + /** |
|
1414 | + * A human readable string is generated based on version and build number |
|
1415 | + * |
|
1416 | + * @return string |
|
1417 | + */ |
|
1418 | + public static function getHumanVersion() { |
|
1419 | + $version = OC_Util::getVersionString(); |
|
1420 | + $build = OC_Util::getBuild(); |
|
1421 | + if (!empty($build) and OC_Util::getChannel() === 'daily') { |
|
1422 | + $version .= ' Build:' . $build; |
|
1423 | + } |
|
1424 | + return $version; |
|
1425 | + } |
|
1426 | + |
|
1427 | + /** |
|
1428 | + * Returns whether the given file name is valid |
|
1429 | + * |
|
1430 | + * @param string $file file name to check |
|
1431 | + * @return bool true if the file name is valid, false otherwise |
|
1432 | + * @deprecated use \OC\Files\View::verifyPath() |
|
1433 | + */ |
|
1434 | + public static function isValidFileName($file) { |
|
1435 | + $trimmed = trim($file); |
|
1436 | + if ($trimmed === '') { |
|
1437 | + return false; |
|
1438 | + } |
|
1439 | + if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { |
|
1440 | + return false; |
|
1441 | + } |
|
1442 | + |
|
1443 | + // detect part files |
|
1444 | + if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) { |
|
1445 | + return false; |
|
1446 | + } |
|
1447 | + |
|
1448 | + foreach (str_split($trimmed) as $char) { |
|
1449 | + if (strpos(\OCP\Constants::FILENAME_INVALID_CHARS, $char) !== false) { |
|
1450 | + return false; |
|
1451 | + } |
|
1452 | + } |
|
1453 | + return true; |
|
1454 | + } |
|
1455 | + |
|
1456 | + /** |
|
1457 | + * Check whether the instance needs to perform an upgrade, |
|
1458 | + * either when the core version is higher or any app requires |
|
1459 | + * an upgrade. |
|
1460 | + * |
|
1461 | + * @param \OC\SystemConfig $config |
|
1462 | + * @return bool whether the core or any app needs an upgrade |
|
1463 | + * @throws \OC\HintException When the upgrade from the given version is not allowed |
|
1464 | + */ |
|
1465 | + public static function needUpgrade(\OC\SystemConfig $config) { |
|
1466 | + if ($config->getValue('installed', false)) { |
|
1467 | + $installedVersion = $config->getValue('version', '0.0.0'); |
|
1468 | + $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
1469 | + $versionDiff = version_compare($currentVersion, $installedVersion); |
|
1470 | + if ($versionDiff > 0) { |
|
1471 | + return true; |
|
1472 | + } else if ($config->getValue('debug', false) && $versionDiff < 0) { |
|
1473 | + // downgrade with debug |
|
1474 | + $installedMajor = explode('.', $installedVersion); |
|
1475 | + $installedMajor = $installedMajor[0] . '.' . $installedMajor[1]; |
|
1476 | + $currentMajor = explode('.', $currentVersion); |
|
1477 | + $currentMajor = $currentMajor[0] . '.' . $currentMajor[1]; |
|
1478 | + if ($installedMajor === $currentMajor) { |
|
1479 | + // Same major, allow downgrade for developers |
|
1480 | + return true; |
|
1481 | + } else { |
|
1482 | + // downgrade attempt, throw exception |
|
1483 | + throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); |
|
1484 | + } |
|
1485 | + } else if ($versionDiff < 0) { |
|
1486 | + // downgrade attempt, throw exception |
|
1487 | + throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); |
|
1488 | + } |
|
1489 | + |
|
1490 | + // also check for upgrades for apps (independently from the user) |
|
1491 | + $apps = \OC_App::getEnabledApps(false, true); |
|
1492 | + $shouldUpgrade = false; |
|
1493 | + foreach ($apps as $app) { |
|
1494 | + if (\OC_App::shouldUpgrade($app)) { |
|
1495 | + $shouldUpgrade = true; |
|
1496 | + break; |
|
1497 | + } |
|
1498 | + } |
|
1499 | + return $shouldUpgrade; |
|
1500 | + } else { |
|
1501 | + return false; |
|
1502 | + } |
|
1503 | + } |
|
1504 | 1504 | |
1505 | 1505 | } |
@@ -37,119 +37,119 @@ |
||
37 | 37 | |
38 | 38 | class AppFetcher extends Fetcher { |
39 | 39 | |
40 | - /** @var CompareVersion */ |
|
41 | - private $compareVersion; |
|
40 | + /** @var CompareVersion */ |
|
41 | + private $compareVersion; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param Factory $appDataFactory |
|
45 | - * @param IClientService $clientService |
|
46 | - * @param ITimeFactory $timeFactory |
|
47 | - * @param IConfig $config |
|
48 | - * @param CompareVersion $compareVersion |
|
49 | - * @param ILogger $logger |
|
50 | - */ |
|
51 | - public function __construct(Factory $appDataFactory, |
|
52 | - IClientService $clientService, |
|
53 | - ITimeFactory $timeFactory, |
|
54 | - IConfig $config, |
|
55 | - CompareVersion $compareVersion, |
|
56 | - ILogger $logger) { |
|
57 | - parent::__construct( |
|
58 | - $appDataFactory, |
|
59 | - $clientService, |
|
60 | - $timeFactory, |
|
61 | - $config, |
|
62 | - $logger |
|
63 | - ); |
|
43 | + /** |
|
44 | + * @param Factory $appDataFactory |
|
45 | + * @param IClientService $clientService |
|
46 | + * @param ITimeFactory $timeFactory |
|
47 | + * @param IConfig $config |
|
48 | + * @param CompareVersion $compareVersion |
|
49 | + * @param ILogger $logger |
|
50 | + */ |
|
51 | + public function __construct(Factory $appDataFactory, |
|
52 | + IClientService $clientService, |
|
53 | + ITimeFactory $timeFactory, |
|
54 | + IConfig $config, |
|
55 | + CompareVersion $compareVersion, |
|
56 | + ILogger $logger) { |
|
57 | + parent::__construct( |
|
58 | + $appDataFactory, |
|
59 | + $clientService, |
|
60 | + $timeFactory, |
|
61 | + $config, |
|
62 | + $logger |
|
63 | + ); |
|
64 | 64 | |
65 | - $this->fileName = 'apps.json'; |
|
66 | - $this->setEndpoint(); |
|
67 | - $this->compareVersion = $compareVersion; |
|
68 | - } |
|
65 | + $this->fileName = 'apps.json'; |
|
66 | + $this->setEndpoint(); |
|
67 | + $this->compareVersion = $compareVersion; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Only returns the latest compatible app release in the releases array |
|
72 | - * |
|
73 | - * @param string $ETag |
|
74 | - * @param string $content |
|
75 | - * |
|
76 | - * @return array |
|
77 | - */ |
|
78 | - protected function fetch($ETag, $content) { |
|
79 | - /** @var mixed[] $response */ |
|
80 | - $response = parent::fetch($ETag, $content); |
|
70 | + /** |
|
71 | + * Only returns the latest compatible app release in the releases array |
|
72 | + * |
|
73 | + * @param string $ETag |
|
74 | + * @param string $content |
|
75 | + * |
|
76 | + * @return array |
|
77 | + */ |
|
78 | + protected function fetch($ETag, $content) { |
|
79 | + /** @var mixed[] $response */ |
|
80 | + $response = parent::fetch($ETag, $content); |
|
81 | 81 | |
82 | - foreach($response['data'] as $dataKey => $app) { |
|
83 | - $releases = []; |
|
82 | + foreach($response['data'] as $dataKey => $app) { |
|
83 | + $releases = []; |
|
84 | 84 | |
85 | - // Filter all compatible releases |
|
86 | - foreach($app['releases'] as $release) { |
|
87 | - // Exclude all nightly and pre-releases |
|
88 | - if($release['isNightly'] === false |
|
89 | - && strpos($release['version'], '-') === false) { |
|
90 | - // Exclude all versions not compatible with the current version |
|
91 | - try { |
|
92 | - $versionParser = new VersionParser(); |
|
93 | - $version = $versionParser->getVersion($release['rawPlatformVersionSpec']); |
|
94 | - $ncVersion = $this->getVersion(); |
|
95 | - $min = $version->getMinimumVersion(); |
|
96 | - $max = $version->getMaximumVersion(); |
|
97 | - $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>='); |
|
98 | - $maxFulfilled = $max !== '' && |
|
99 | - $this->compareVersion->isCompatible($ncVersion, $max, '<='); |
|
100 | - if ($minFulfilled && $maxFulfilled) { |
|
101 | - $releases[] = $release; |
|
102 | - } |
|
103 | - } catch (\InvalidArgumentException $e) { |
|
104 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
85 | + // Filter all compatible releases |
|
86 | + foreach($app['releases'] as $release) { |
|
87 | + // Exclude all nightly and pre-releases |
|
88 | + if($release['isNightly'] === false |
|
89 | + && strpos($release['version'], '-') === false) { |
|
90 | + // Exclude all versions not compatible with the current version |
|
91 | + try { |
|
92 | + $versionParser = new VersionParser(); |
|
93 | + $version = $versionParser->getVersion($release['rawPlatformVersionSpec']); |
|
94 | + $ncVersion = $this->getVersion(); |
|
95 | + $min = $version->getMinimumVersion(); |
|
96 | + $max = $version->getMaximumVersion(); |
|
97 | + $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>='); |
|
98 | + $maxFulfilled = $max !== '' && |
|
99 | + $this->compareVersion->isCompatible($ncVersion, $max, '<='); |
|
100 | + if ($minFulfilled && $maxFulfilled) { |
|
101 | + $releases[] = $release; |
|
102 | + } |
|
103 | + } catch (\InvalidArgumentException $e) { |
|
104 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]); |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | 108 | |
109 | - // Get the highest version |
|
110 | - $versions = []; |
|
111 | - foreach($releases as $release) { |
|
112 | - $versions[] = $release['version']; |
|
113 | - } |
|
114 | - usort($versions, 'version_compare'); |
|
115 | - $versions = array_reverse($versions); |
|
116 | - $compatible = false; |
|
117 | - if(isset($versions[0])) { |
|
118 | - $highestVersion = $versions[0]; |
|
119 | - foreach ($releases as $release) { |
|
120 | - if ((string)$release['version'] === (string)$highestVersion) { |
|
121 | - $compatible = true; |
|
122 | - $response['data'][$dataKey]['releases'] = [$release]; |
|
123 | - break; |
|
124 | - } |
|
125 | - } |
|
126 | - } |
|
127 | - if(!$compatible) { |
|
128 | - unset($response['data'][$dataKey]); |
|
129 | - } |
|
130 | - } |
|
109 | + // Get the highest version |
|
110 | + $versions = []; |
|
111 | + foreach($releases as $release) { |
|
112 | + $versions[] = $release['version']; |
|
113 | + } |
|
114 | + usort($versions, 'version_compare'); |
|
115 | + $versions = array_reverse($versions); |
|
116 | + $compatible = false; |
|
117 | + if(isset($versions[0])) { |
|
118 | + $highestVersion = $versions[0]; |
|
119 | + foreach ($releases as $release) { |
|
120 | + if ((string)$release['version'] === (string)$highestVersion) { |
|
121 | + $compatible = true; |
|
122 | + $response['data'][$dataKey]['releases'] = [$release]; |
|
123 | + break; |
|
124 | + } |
|
125 | + } |
|
126 | + } |
|
127 | + if(!$compatible) { |
|
128 | + unset($response['data'][$dataKey]); |
|
129 | + } |
|
130 | + } |
|
131 | 131 | |
132 | - $response['data'] = array_values($response['data']); |
|
133 | - return $response; |
|
134 | - } |
|
132 | + $response['data'] = array_values($response['data']); |
|
133 | + return $response; |
|
134 | + } |
|
135 | 135 | |
136 | - private function setEndpoint() { |
|
137 | - $versionArray = explode('.', $this->getVersion()); |
|
138 | - $this->endpointUrl = sprintf( |
|
139 | - 'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json', |
|
140 | - $versionArray[0], |
|
141 | - $versionArray[1], |
|
142 | - $versionArray[2] |
|
143 | - ); |
|
144 | - } |
|
136 | + private function setEndpoint() { |
|
137 | + $versionArray = explode('.', $this->getVersion()); |
|
138 | + $this->endpointUrl = sprintf( |
|
139 | + 'https://apps.nextcloud.com/api/v1/platform/%d.%d.%d/apps.json', |
|
140 | + $versionArray[0], |
|
141 | + $versionArray[1], |
|
142 | + $versionArray[2] |
|
143 | + ); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * @param string $version |
|
148 | - * @param string $fileName |
|
149 | - */ |
|
150 | - public function setVersion(string $version, string $fileName = 'apps.json') { |
|
151 | - parent::setVersion($version); |
|
152 | - $this->fileName = $fileName; |
|
153 | - $this->setEndpoint(); |
|
154 | - } |
|
146 | + /** |
|
147 | + * @param string $version |
|
148 | + * @param string $fileName |
|
149 | + */ |
|
150 | + public function setVersion(string $version, string $fileName = 'apps.json') { |
|
151 | + parent::setVersion($version); |
|
152 | + $this->fileName = $fileName; |
|
153 | + $this->setEndpoint(); |
|
154 | + } |
|
155 | 155 | } |
@@ -32,58 +32,58 @@ |
||
32 | 32 | |
33 | 33 | class CapabilitiesManager { |
34 | 34 | |
35 | - /** @var \Closure[] */ |
|
36 | - private $capabilities = array(); |
|
35 | + /** @var \Closure[] */ |
|
36 | + private $capabilities = array(); |
|
37 | 37 | |
38 | - /** @var ILogger */ |
|
39 | - private $logger; |
|
38 | + /** @var ILogger */ |
|
39 | + private $logger; |
|
40 | 40 | |
41 | - public function __construct(ILogger $logger) { |
|
42 | - $this->logger = $logger; |
|
43 | - } |
|
41 | + public function __construct(ILogger $logger) { |
|
42 | + $this->logger = $logger; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get an array of al the capabilities that are registered at this manager |
|
45 | + /** |
|
46 | + * Get an array of al the capabilities that are registered at this manager |
|
47 | 47 | * |
48 | - * @param bool $public get public capabilities only |
|
49 | - * @throws \InvalidArgumentException |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public function getCapabilities(bool $public = false) : array { |
|
53 | - $capabilities = []; |
|
54 | - foreach($this->capabilities as $capability) { |
|
55 | - try { |
|
56 | - $c = $capability(); |
|
57 | - } catch (QueryException $e) { |
|
58 | - $this->logger->logException($e, [ |
|
59 | - 'message' => 'CapabilitiesManager', |
|
60 | - 'level' => ILogger::ERROR, |
|
61 | - 'app' => 'core', |
|
62 | - ]); |
|
63 | - continue; |
|
64 | - } |
|
48 | + * @param bool $public get public capabilities only |
|
49 | + * @throws \InvalidArgumentException |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public function getCapabilities(bool $public = false) : array { |
|
53 | + $capabilities = []; |
|
54 | + foreach($this->capabilities as $capability) { |
|
55 | + try { |
|
56 | + $c = $capability(); |
|
57 | + } catch (QueryException $e) { |
|
58 | + $this->logger->logException($e, [ |
|
59 | + 'message' => 'CapabilitiesManager', |
|
60 | + 'level' => ILogger::ERROR, |
|
61 | + 'app' => 'core', |
|
62 | + ]); |
|
63 | + continue; |
|
64 | + } |
|
65 | 65 | |
66 | - if ($c instanceof ICapability) { |
|
67 | - if(!$public || $c instanceof IPublicCapability) { |
|
68 | - $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); |
|
69 | - } |
|
70 | - } else { |
|
71 | - throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); |
|
72 | - } |
|
73 | - } |
|
66 | + if ($c instanceof ICapability) { |
|
67 | + if(!$public || $c instanceof IPublicCapability) { |
|
68 | + $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); |
|
69 | + } |
|
70 | + } else { |
|
71 | + throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); |
|
72 | + } |
|
73 | + } |
|
74 | 74 | |
75 | - return $capabilities; |
|
76 | - } |
|
75 | + return $capabilities; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
80 | - * capabilities are actually requested |
|
81 | - * |
|
82 | - * $callable has to return an instance of OCP\Capabilities\ICapability |
|
83 | - * |
|
84 | - * @param \Closure $callable |
|
85 | - */ |
|
86 | - public function registerCapability(\Closure $callable) { |
|
87 | - $this->capabilities[] = $callable; |
|
88 | - } |
|
78 | + /** |
|
79 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
80 | + * capabilities are actually requested |
|
81 | + * |
|
82 | + * $callable has to return an instance of OCP\Capabilities\ICapability |
|
83 | + * |
|
84 | + * @param \Closure $callable |
|
85 | + */ |
|
86 | + public function registerCapability(\Closure $callable) { |
|
87 | + $this->capabilities[] = $callable; |
|
88 | + } |
|
89 | 89 | } |
@@ -30,116 +30,116 @@ |
||
30 | 30 | use OCP\ILogger; |
31 | 31 | |
32 | 32 | trait S3ConnectionTrait { |
33 | - /** @var array */ |
|
34 | - protected $params; |
|
35 | - |
|
36 | - /** @var S3Client */ |
|
37 | - protected $connection; |
|
38 | - |
|
39 | - /** @var string */ |
|
40 | - protected $id; |
|
41 | - |
|
42 | - /** @var string */ |
|
43 | - protected $bucket; |
|
44 | - |
|
45 | - /** @var int */ |
|
46 | - protected $timeout; |
|
47 | - |
|
48 | - protected $test; |
|
49 | - |
|
50 | - protected function parseParams($params) { |
|
51 | - if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
52 | - throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
|
53 | - } |
|
54 | - |
|
55 | - $this->id = 'amazon::' . $params['bucket']; |
|
56 | - |
|
57 | - $this->test = isset($params['test']); |
|
58 | - $this->bucket = $params['bucket']; |
|
59 | - $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
|
60 | - $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
61 | - $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
62 | - if (!isset($params['port']) || $params['port'] === '') { |
|
63 | - $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
64 | - } |
|
65 | - $this->params = $params; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Returns the connection |
|
71 | - * |
|
72 | - * @return S3Client connected client |
|
73 | - * @throws \Exception if connection could not be made |
|
74 | - */ |
|
75 | - protected function getConnection() { |
|
76 | - if (!is_null($this->connection)) { |
|
77 | - return $this->connection; |
|
78 | - } |
|
79 | - |
|
80 | - $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
81 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
82 | - |
|
83 | - $options = [ |
|
84 | - 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
|
85 | - 'credentials' => [ |
|
86 | - 'key' => $this->params['key'], |
|
87 | - 'secret' => $this->params['secret'], |
|
88 | - ], |
|
89 | - 'endpoint' => $base_url, |
|
90 | - 'region' => $this->params['region'], |
|
91 | - 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
|
92 | - 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()) |
|
93 | - ]; |
|
94 | - if (isset($this->params['proxy'])) { |
|
95 | - $options['request.options'] = ['proxy' => $this->params['proxy']]; |
|
96 | - } |
|
97 | - if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
|
98 | - $options['signature_version'] = 'v2'; |
|
99 | - } |
|
100 | - $this->connection = new S3Client($options); |
|
101 | - |
|
102 | - if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
|
103 | - throw new \Exception("The configured bucket name is invalid: " . $this->bucket); |
|
104 | - } |
|
105 | - |
|
106 | - if (!$this->connection->doesBucketExist($this->bucket)) { |
|
107 | - $logger = \OC::$server->getLogger(); |
|
108 | - try { |
|
109 | - $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
110 | - $this->connection->createBucket(array( |
|
111 | - 'Bucket' => $this->bucket |
|
112 | - )); |
|
113 | - $this->testTimeout(); |
|
114 | - } catch (S3Exception $e) { |
|
115 | - $logger->logException($e, [ |
|
116 | - 'message' => 'Invalid remote storage.', |
|
117 | - 'level' => ILogger::DEBUG, |
|
118 | - 'app' => 'objectstore', |
|
119 | - ]); |
|
120 | - throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - return $this->connection; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * when running the tests wait to let the buckets catch up |
|
129 | - */ |
|
130 | - private function testTimeout() { |
|
131 | - if ($this->test) { |
|
132 | - sleep($this->timeout); |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - public static function legacySignatureProvider($version, $service, $region) { |
|
137 | - switch ($version) { |
|
138 | - case 'v2': |
|
139 | - case 's3': |
|
140 | - return new S3Signature(); |
|
141 | - default: |
|
142 | - return null; |
|
143 | - } |
|
144 | - } |
|
33 | + /** @var array */ |
|
34 | + protected $params; |
|
35 | + |
|
36 | + /** @var S3Client */ |
|
37 | + protected $connection; |
|
38 | + |
|
39 | + /** @var string */ |
|
40 | + protected $id; |
|
41 | + |
|
42 | + /** @var string */ |
|
43 | + protected $bucket; |
|
44 | + |
|
45 | + /** @var int */ |
|
46 | + protected $timeout; |
|
47 | + |
|
48 | + protected $test; |
|
49 | + |
|
50 | + protected function parseParams($params) { |
|
51 | + if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
52 | + throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
|
53 | + } |
|
54 | + |
|
55 | + $this->id = 'amazon::' . $params['bucket']; |
|
56 | + |
|
57 | + $this->test = isset($params['test']); |
|
58 | + $this->bucket = $params['bucket']; |
|
59 | + $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
|
60 | + $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
61 | + $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
62 | + if (!isset($params['port']) || $params['port'] === '') { |
|
63 | + $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
64 | + } |
|
65 | + $this->params = $params; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Returns the connection |
|
71 | + * |
|
72 | + * @return S3Client connected client |
|
73 | + * @throws \Exception if connection could not be made |
|
74 | + */ |
|
75 | + protected function getConnection() { |
|
76 | + if (!is_null($this->connection)) { |
|
77 | + return $this->connection; |
|
78 | + } |
|
79 | + |
|
80 | + $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
81 | + $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
82 | + |
|
83 | + $options = [ |
|
84 | + 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
|
85 | + 'credentials' => [ |
|
86 | + 'key' => $this->params['key'], |
|
87 | + 'secret' => $this->params['secret'], |
|
88 | + ], |
|
89 | + 'endpoint' => $base_url, |
|
90 | + 'region' => $this->params['region'], |
|
91 | + 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
|
92 | + 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()) |
|
93 | + ]; |
|
94 | + if (isset($this->params['proxy'])) { |
|
95 | + $options['request.options'] = ['proxy' => $this->params['proxy']]; |
|
96 | + } |
|
97 | + if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
|
98 | + $options['signature_version'] = 'v2'; |
|
99 | + } |
|
100 | + $this->connection = new S3Client($options); |
|
101 | + |
|
102 | + if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
|
103 | + throw new \Exception("The configured bucket name is invalid: " . $this->bucket); |
|
104 | + } |
|
105 | + |
|
106 | + if (!$this->connection->doesBucketExist($this->bucket)) { |
|
107 | + $logger = \OC::$server->getLogger(); |
|
108 | + try { |
|
109 | + $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
110 | + $this->connection->createBucket(array( |
|
111 | + 'Bucket' => $this->bucket |
|
112 | + )); |
|
113 | + $this->testTimeout(); |
|
114 | + } catch (S3Exception $e) { |
|
115 | + $logger->logException($e, [ |
|
116 | + 'message' => 'Invalid remote storage.', |
|
117 | + 'level' => ILogger::DEBUG, |
|
118 | + 'app' => 'objectstore', |
|
119 | + ]); |
|
120 | + throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + return $this->connection; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * when running the tests wait to let the buckets catch up |
|
129 | + */ |
|
130 | + private function testTimeout() { |
|
131 | + if ($this->test) { |
|
132 | + sleep($this->timeout); |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + public static function legacySignatureProvider($version, $service, $region) { |
|
137 | + switch ($version) { |
|
138 | + case 'v2': |
|
139 | + case 's3': |
|
140 | + return new S3Signature(); |
|
141 | + default: |
|
142 | + return null; |
|
143 | + } |
|
144 | + } |
|
145 | 145 | } |
@@ -52,13 +52,13 @@ discard block |
||
52 | 52 | throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
53 | 53 | } |
54 | 54 | |
55 | - $this->id = 'amazon::' . $params['bucket']; |
|
55 | + $this->id = 'amazon::'.$params['bucket']; |
|
56 | 56 | |
57 | 57 | $this->test = isset($params['test']); |
58 | 58 | $this->bucket = $params['bucket']; |
59 | 59 | $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
60 | 60 | $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
61 | - $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
61 | + $params['hostname'] = empty($params['hostname']) ? 's3.'.$params['region'].'.amazonaws.com' : $params['hostname']; |
|
62 | 62 | if (!isset($params['port']) || $params['port'] === '') { |
63 | 63 | $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
64 | 64 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | } |
79 | 79 | |
80 | 80 | $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
81 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
81 | + $base_url = $scheme.'://'.$this->params['hostname'].':'.$this->params['port'].'/'; |
|
82 | 82 | |
83 | 83 | $options = [ |
84 | 84 | 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
@@ -100,13 +100,13 @@ discard block |
||
100 | 100 | $this->connection = new S3Client($options); |
101 | 101 | |
102 | 102 | if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
103 | - throw new \Exception("The configured bucket name is invalid: " . $this->bucket); |
|
103 | + throw new \Exception("The configured bucket name is invalid: ".$this->bucket); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | if (!$this->connection->doesBucketExist($this->bucket)) { |
107 | 107 | $logger = \OC::$server->getLogger(); |
108 | 108 | try { |
109 | - $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
109 | + $logger->info('Bucket "'.$this->bucket.'" does not exist - creating it.', ['app' => 'objectstore']); |
|
110 | 110 | $this->connection->createBucket(array( |
111 | 111 | 'Bucket' => $this->bucket |
112 | 112 | )); |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | 'level' => ILogger::DEBUG, |
118 | 118 | 'app' => 'objectstore', |
119 | 119 | ]); |
120 | - throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
120 | + throw new \Exception('Creation of bucket "'.$this->bucket.'" failed. '.$e->getMessage()); |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 |
@@ -70,857 +70,857 @@ |
||
70 | 70 | |
71 | 71 | class Filesystem { |
72 | 72 | |
73 | - /** |
|
74 | - * @var Mount\Manager $mounts |
|
75 | - */ |
|
76 | - private static $mounts; |
|
77 | - |
|
78 | - public static $loaded = false; |
|
79 | - /** |
|
80 | - * @var \OC\Files\View $defaultInstance |
|
81 | - */ |
|
82 | - static private $defaultInstance; |
|
83 | - |
|
84 | - static private $usersSetup = array(); |
|
85 | - |
|
86 | - static private $normalizedPathCache = null; |
|
87 | - |
|
88 | - static private $listeningForProviders = false; |
|
89 | - |
|
90 | - /** |
|
91 | - * classname which used for hooks handling |
|
92 | - * used as signalclass in OC_Hooks::emit() |
|
93 | - */ |
|
94 | - const CLASSNAME = 'OC_Filesystem'; |
|
95 | - |
|
96 | - /** |
|
97 | - * signalname emitted before file renaming |
|
98 | - * |
|
99 | - * @param string $oldpath |
|
100 | - * @param string $newpath |
|
101 | - */ |
|
102 | - const signal_rename = 'rename'; |
|
103 | - |
|
104 | - /** |
|
105 | - * signal emitted after file renaming |
|
106 | - * |
|
107 | - * @param string $oldpath |
|
108 | - * @param string $newpath |
|
109 | - */ |
|
110 | - const signal_post_rename = 'post_rename'; |
|
111 | - |
|
112 | - /** |
|
113 | - * signal emitted before file/dir creation |
|
114 | - * |
|
115 | - * @param string $path |
|
116 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
117 | - */ |
|
118 | - const signal_create = 'create'; |
|
119 | - |
|
120 | - /** |
|
121 | - * signal emitted after file/dir creation |
|
122 | - * |
|
123 | - * @param string $path |
|
124 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
125 | - */ |
|
126 | - const signal_post_create = 'post_create'; |
|
127 | - |
|
128 | - /** |
|
129 | - * signal emits before file/dir copy |
|
130 | - * |
|
131 | - * @param string $oldpath |
|
132 | - * @param string $newpath |
|
133 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
134 | - */ |
|
135 | - const signal_copy = 'copy'; |
|
136 | - |
|
137 | - /** |
|
138 | - * signal emits after file/dir copy |
|
139 | - * |
|
140 | - * @param string $oldpath |
|
141 | - * @param string $newpath |
|
142 | - */ |
|
143 | - const signal_post_copy = 'post_copy'; |
|
144 | - |
|
145 | - /** |
|
146 | - * signal emits before file/dir save |
|
147 | - * |
|
148 | - * @param string $path |
|
149 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
150 | - */ |
|
151 | - const signal_write = 'write'; |
|
152 | - |
|
153 | - /** |
|
154 | - * signal emits after file/dir save |
|
155 | - * |
|
156 | - * @param string $path |
|
157 | - */ |
|
158 | - const signal_post_write = 'post_write'; |
|
159 | - |
|
160 | - /** |
|
161 | - * signal emitted before file/dir update |
|
162 | - * |
|
163 | - * @param string $path |
|
164 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
165 | - */ |
|
166 | - const signal_update = 'update'; |
|
167 | - |
|
168 | - /** |
|
169 | - * signal emitted after file/dir update |
|
170 | - * |
|
171 | - * @param string $path |
|
172 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
173 | - */ |
|
174 | - const signal_post_update = 'post_update'; |
|
175 | - |
|
176 | - /** |
|
177 | - * signal emits when reading file/dir |
|
178 | - * |
|
179 | - * @param string $path |
|
180 | - */ |
|
181 | - const signal_read = 'read'; |
|
182 | - |
|
183 | - /** |
|
184 | - * signal emits when removing file/dir |
|
185 | - * |
|
186 | - * @param string $path |
|
187 | - */ |
|
188 | - const signal_delete = 'delete'; |
|
189 | - |
|
190 | - /** |
|
191 | - * parameters definitions for signals |
|
192 | - */ |
|
193 | - const signal_param_path = 'path'; |
|
194 | - const signal_param_oldpath = 'oldpath'; |
|
195 | - const signal_param_newpath = 'newpath'; |
|
196 | - |
|
197 | - /** |
|
198 | - * run - changing this flag to false in hook handler will cancel event |
|
199 | - */ |
|
200 | - const signal_param_run = 'run'; |
|
201 | - |
|
202 | - const signal_create_mount = 'create_mount'; |
|
203 | - const signal_delete_mount = 'delete_mount'; |
|
204 | - const signal_param_mount_type = 'mounttype'; |
|
205 | - const signal_param_users = 'users'; |
|
206 | - |
|
207 | - /** |
|
208 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
209 | - */ |
|
210 | - private static $loader; |
|
211 | - |
|
212 | - /** @var bool */ |
|
213 | - private static $logWarningWhenAddingStorageWrapper = true; |
|
214 | - |
|
215 | - /** |
|
216 | - * @param bool $shouldLog |
|
217 | - * @return bool previous value |
|
218 | - * @internal |
|
219 | - */ |
|
220 | - public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
221 | - $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
222 | - self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
223 | - return $previousValue; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $wrapperName |
|
228 | - * @param callable $wrapper |
|
229 | - * @param int $priority |
|
230 | - */ |
|
231 | - public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
232 | - if (self::$logWarningWhenAddingStorageWrapper) { |
|
233 | - \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
234 | - 'wrapper' => $wrapperName, |
|
235 | - 'app' => 'filesystem', |
|
236 | - ]); |
|
237 | - } |
|
238 | - |
|
239 | - $mounts = self::getMountManager()->getAll(); |
|
240 | - if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
241 | - // do not re-wrap if storage with this name already existed |
|
242 | - return; |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Returns the storage factory |
|
248 | - * |
|
249 | - * @return \OCP\Files\Storage\IStorageFactory |
|
250 | - */ |
|
251 | - public static function getLoader() { |
|
252 | - if (!self::$loader) { |
|
253 | - self::$loader = new StorageFactory(); |
|
254 | - } |
|
255 | - return self::$loader; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Returns the mount manager |
|
260 | - * |
|
261 | - * @return \OC\Files\Mount\Manager |
|
262 | - */ |
|
263 | - public static function getMountManager($user = '') { |
|
264 | - if (!self::$mounts) { |
|
265 | - \OC_Util::setupFS($user); |
|
266 | - } |
|
267 | - return self::$mounts; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * get the mountpoint of the storage object for a path |
|
272 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
273 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
274 | - * and doesn't take the chroot into account ) |
|
275 | - * |
|
276 | - * @param string $path |
|
277 | - * @return string |
|
278 | - */ |
|
279 | - static public function getMountPoint($path) { |
|
280 | - if (!self::$mounts) { |
|
281 | - \OC_Util::setupFS(); |
|
282 | - } |
|
283 | - $mount = self::$mounts->find($path); |
|
284 | - if ($mount) { |
|
285 | - return $mount->getMountPoint(); |
|
286 | - } else { |
|
287 | - return ''; |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * get a list of all mount points in a directory |
|
293 | - * |
|
294 | - * @param string $path |
|
295 | - * @return string[] |
|
296 | - */ |
|
297 | - static public function getMountPoints($path) { |
|
298 | - if (!self::$mounts) { |
|
299 | - \OC_Util::setupFS(); |
|
300 | - } |
|
301 | - $result = array(); |
|
302 | - $mounts = self::$mounts->findIn($path); |
|
303 | - foreach ($mounts as $mount) { |
|
304 | - $result[] = $mount->getMountPoint(); |
|
305 | - } |
|
306 | - return $result; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * get the storage mounted at $mountPoint |
|
311 | - * |
|
312 | - * @param string $mountPoint |
|
313 | - * @return \OC\Files\Storage\Storage |
|
314 | - */ |
|
315 | - public static function getStorage($mountPoint) { |
|
316 | - if (!self::$mounts) { |
|
317 | - \OC_Util::setupFS(); |
|
318 | - } |
|
319 | - $mount = self::$mounts->find($mountPoint); |
|
320 | - return $mount->getStorage(); |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * @param string $id |
|
325 | - * @return Mount\MountPoint[] |
|
326 | - */ |
|
327 | - public static function getMountByStorageId($id) { |
|
328 | - if (!self::$mounts) { |
|
329 | - \OC_Util::setupFS(); |
|
330 | - } |
|
331 | - return self::$mounts->findByStorageId($id); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @param int $id |
|
336 | - * @return Mount\MountPoint[] |
|
337 | - */ |
|
338 | - public static function getMountByNumericId($id) { |
|
339 | - if (!self::$mounts) { |
|
340 | - \OC_Util::setupFS(); |
|
341 | - } |
|
342 | - return self::$mounts->findByNumericId($id); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * resolve a path to a storage and internal path |
|
347 | - * |
|
348 | - * @param string $path |
|
349 | - * @return array an array consisting of the storage and the internal path |
|
350 | - */ |
|
351 | - static public function resolvePath($path) { |
|
352 | - if (!self::$mounts) { |
|
353 | - \OC_Util::setupFS(); |
|
354 | - } |
|
355 | - $mount = self::$mounts->find($path); |
|
356 | - if ($mount) { |
|
357 | - return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
358 | - } else { |
|
359 | - return array(null, null); |
|
360 | - } |
|
361 | - } |
|
362 | - |
|
363 | - static public function init($user, $root) { |
|
364 | - if (self::$defaultInstance) { |
|
365 | - return false; |
|
366 | - } |
|
367 | - self::getLoader(); |
|
368 | - self::$defaultInstance = new View($root); |
|
369 | - |
|
370 | - if (!self::$mounts) { |
|
371 | - self::$mounts = \OC::$server->getMountManager(); |
|
372 | - } |
|
373 | - |
|
374 | - //load custom mount config |
|
375 | - self::initMountPoints($user); |
|
376 | - |
|
377 | - self::$loaded = true; |
|
378 | - |
|
379 | - return true; |
|
380 | - } |
|
381 | - |
|
382 | - static public function initMountManager() { |
|
383 | - if (!self::$mounts) { |
|
384 | - self::$mounts = \OC::$server->getMountManager(); |
|
385 | - } |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * Initialize system and personal mount points for a user |
|
390 | - * |
|
391 | - * @param string $user |
|
392 | - * @throws \OC\User\NoUserException if the user is not available |
|
393 | - */ |
|
394 | - public static function initMountPoints($user = '') { |
|
395 | - if ($user == '') { |
|
396 | - $user = \OC_User::getUser(); |
|
397 | - } |
|
398 | - if ($user === null || $user === false || $user === '') { |
|
399 | - throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
400 | - } |
|
401 | - |
|
402 | - if (isset(self::$usersSetup[$user])) { |
|
403 | - return; |
|
404 | - } |
|
405 | - |
|
406 | - self::$usersSetup[$user] = true; |
|
407 | - |
|
408 | - $userManager = \OC::$server->getUserManager(); |
|
409 | - $userObject = $userManager->get($user); |
|
410 | - |
|
411 | - if (is_null($userObject)) { |
|
412 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR); |
|
413 | - // reset flag, this will make it possible to rethrow the exception if called again |
|
414 | - unset(self::$usersSetup[$user]); |
|
415 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
416 | - } |
|
417 | - |
|
418 | - $realUid = $userObject->getUID(); |
|
419 | - // workaround in case of different casings |
|
420 | - if ($user !== $realUid) { |
|
421 | - $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
422 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, ILogger::WARN); |
|
423 | - $user = $realUid; |
|
424 | - |
|
425 | - // again with the correct casing |
|
426 | - if (isset(self::$usersSetup[$user])) { |
|
427 | - return; |
|
428 | - } |
|
429 | - |
|
430 | - self::$usersSetup[$user] = true; |
|
431 | - } |
|
432 | - |
|
433 | - if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
434 | - /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
435 | - $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
436 | - |
|
437 | - // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
438 | - $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
439 | - |
|
440 | - self::getMountManager()->addMount($homeMount); |
|
441 | - |
|
442 | - \OC\Files\Filesystem::getStorage($user); |
|
443 | - |
|
444 | - // Chance to mount for other storages |
|
445 | - if ($userObject) { |
|
446 | - $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
447 | - $mounts[] = $homeMount; |
|
448 | - $mountConfigManager->registerMounts($userObject, $mounts); |
|
449 | - } |
|
450 | - |
|
451 | - self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
452 | - } else { |
|
453 | - self::getMountManager()->addMount(new MountPoint( |
|
454 | - new NullStorage([]), |
|
455 | - '/' . $user |
|
456 | - )); |
|
457 | - self::getMountManager()->addMount(new MountPoint( |
|
458 | - new NullStorage([]), |
|
459 | - '/' . $user . '/files' |
|
460 | - )); |
|
461 | - } |
|
462 | - \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Get mounts from mount providers that are registered after setup |
|
467 | - * |
|
468 | - * @param MountProviderCollection $mountConfigManager |
|
469 | - * @param IUserManager $userManager |
|
470 | - */ |
|
471 | - private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
472 | - if (!self::$listeningForProviders) { |
|
473 | - self::$listeningForProviders = true; |
|
474 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
475 | - foreach (Filesystem::$usersSetup as $user => $setup) { |
|
476 | - $userObject = $userManager->get($user); |
|
477 | - if ($userObject) { |
|
478 | - $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
479 | - array_walk($mounts, array(self::$mounts, 'addMount')); |
|
480 | - } |
|
481 | - } |
|
482 | - }); |
|
483 | - } |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * get the default filesystem view |
|
488 | - * |
|
489 | - * @return View |
|
490 | - */ |
|
491 | - static public function getView() { |
|
492 | - return self::$defaultInstance; |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * tear down the filesystem, removing all storage providers |
|
497 | - */ |
|
498 | - static public function tearDown() { |
|
499 | - self::clearMounts(); |
|
500 | - self::$defaultInstance = null; |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * get the relative path of the root data directory for the current user |
|
505 | - * |
|
506 | - * @return string |
|
507 | - * |
|
508 | - * Returns path like /admin/files |
|
509 | - */ |
|
510 | - static public function getRoot() { |
|
511 | - if (!self::$defaultInstance) { |
|
512 | - return null; |
|
513 | - } |
|
514 | - return self::$defaultInstance->getRoot(); |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * clear all mounts and storage backends |
|
519 | - */ |
|
520 | - public static function clearMounts() { |
|
521 | - if (self::$mounts) { |
|
522 | - self::$usersSetup = array(); |
|
523 | - self::$mounts->clear(); |
|
524 | - } |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
529 | - * |
|
530 | - * @param \OC\Files\Storage\Storage|string $class |
|
531 | - * @param array $arguments |
|
532 | - * @param string $mountpoint |
|
533 | - */ |
|
534 | - static public function mount($class, $arguments, $mountpoint) { |
|
535 | - if (!self::$mounts) { |
|
536 | - \OC_Util::setupFS(); |
|
537 | - } |
|
538 | - $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
539 | - self::$mounts->addMount($mount); |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * return the path to a local version of the file |
|
544 | - * we need this because we can't know if a file is stored local or not from |
|
545 | - * outside the filestorage and for some purposes a local file is needed |
|
546 | - * |
|
547 | - * @param string $path |
|
548 | - * @return string |
|
549 | - */ |
|
550 | - static public function getLocalFile($path) { |
|
551 | - return self::$defaultInstance->getLocalFile($path); |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * @param string $path |
|
556 | - * @return string |
|
557 | - */ |
|
558 | - static public function getLocalFolder($path) { |
|
559 | - return self::$defaultInstance->getLocalFolder($path); |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * return path to file which reflects one visible in browser |
|
564 | - * |
|
565 | - * @param string $path |
|
566 | - * @return string |
|
567 | - */ |
|
568 | - static public function getLocalPath($path) { |
|
569 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
570 | - $newpath = $path; |
|
571 | - if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
572 | - $newpath = substr($path, strlen($datadir)); |
|
573 | - } |
|
574 | - return $newpath; |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * check if the requested path is valid |
|
579 | - * |
|
580 | - * @param string $path |
|
581 | - * @return bool |
|
582 | - */ |
|
583 | - static public function isValidPath($path) { |
|
584 | - $path = self::normalizePath($path); |
|
585 | - if (!$path || $path[0] !== '/') { |
|
586 | - $path = '/' . $path; |
|
587 | - } |
|
588 | - if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
589 | - return false; |
|
590 | - } |
|
591 | - return true; |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * checks if a file is blacklisted for storage in the filesystem |
|
596 | - * Listens to write and rename hooks |
|
597 | - * |
|
598 | - * @param array $data from hook |
|
599 | - */ |
|
600 | - static public function isBlacklisted($data) { |
|
601 | - if (isset($data['path'])) { |
|
602 | - $path = $data['path']; |
|
603 | - } else if (isset($data['newpath'])) { |
|
604 | - $path = $data['newpath']; |
|
605 | - } |
|
606 | - if (isset($path)) { |
|
607 | - if (self::isFileBlacklisted($path)) { |
|
608 | - $data['run'] = false; |
|
609 | - } |
|
610 | - } |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * @param string $filename |
|
615 | - * @return bool |
|
616 | - */ |
|
617 | - static public function isFileBlacklisted($filename) { |
|
618 | - $filename = self::normalizePath($filename); |
|
619 | - |
|
620 | - $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
621 | - $filename = strtolower(basename($filename)); |
|
622 | - return in_array($filename, $blacklist); |
|
623 | - } |
|
624 | - |
|
625 | - /** |
|
626 | - * check if the directory should be ignored when scanning |
|
627 | - * NOTE: the special directories . and .. would cause never ending recursion |
|
628 | - * |
|
629 | - * @param String $dir |
|
630 | - * @return boolean |
|
631 | - */ |
|
632 | - static public function isIgnoredDir($dir) { |
|
633 | - if ($dir === '.' || $dir === '..') { |
|
634 | - return true; |
|
635 | - } |
|
636 | - return false; |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
641 | - */ |
|
642 | - static public function mkdir($path) { |
|
643 | - return self::$defaultInstance->mkdir($path); |
|
644 | - } |
|
645 | - |
|
646 | - static public function rmdir($path) { |
|
647 | - return self::$defaultInstance->rmdir($path); |
|
648 | - } |
|
649 | - |
|
650 | - static public function is_dir($path) { |
|
651 | - return self::$defaultInstance->is_dir($path); |
|
652 | - } |
|
653 | - |
|
654 | - static public function is_file($path) { |
|
655 | - return self::$defaultInstance->is_file($path); |
|
656 | - } |
|
657 | - |
|
658 | - static public function stat($path) { |
|
659 | - return self::$defaultInstance->stat($path); |
|
660 | - } |
|
661 | - |
|
662 | - static public function filetype($path) { |
|
663 | - return self::$defaultInstance->filetype($path); |
|
664 | - } |
|
665 | - |
|
666 | - static public function filesize($path) { |
|
667 | - return self::$defaultInstance->filesize($path); |
|
668 | - } |
|
669 | - |
|
670 | - static public function readfile($path) { |
|
671 | - return self::$defaultInstance->readfile($path); |
|
672 | - } |
|
673 | - |
|
674 | - static public function isCreatable($path) { |
|
675 | - return self::$defaultInstance->isCreatable($path); |
|
676 | - } |
|
677 | - |
|
678 | - static public function isReadable($path) { |
|
679 | - return self::$defaultInstance->isReadable($path); |
|
680 | - } |
|
681 | - |
|
682 | - static public function isUpdatable($path) { |
|
683 | - return self::$defaultInstance->isUpdatable($path); |
|
684 | - } |
|
685 | - |
|
686 | - static public function isDeletable($path) { |
|
687 | - return self::$defaultInstance->isDeletable($path); |
|
688 | - } |
|
689 | - |
|
690 | - static public function isSharable($path) { |
|
691 | - return self::$defaultInstance->isSharable($path); |
|
692 | - } |
|
693 | - |
|
694 | - static public function file_exists($path) { |
|
695 | - return self::$defaultInstance->file_exists($path); |
|
696 | - } |
|
697 | - |
|
698 | - static public function filemtime($path) { |
|
699 | - return self::$defaultInstance->filemtime($path); |
|
700 | - } |
|
701 | - |
|
702 | - static public function touch($path, $mtime = null) { |
|
703 | - return self::$defaultInstance->touch($path, $mtime); |
|
704 | - } |
|
705 | - |
|
706 | - /** |
|
707 | - * @return string |
|
708 | - */ |
|
709 | - static public function file_get_contents($path) { |
|
710 | - return self::$defaultInstance->file_get_contents($path); |
|
711 | - } |
|
712 | - |
|
713 | - static public function file_put_contents($path, $data) { |
|
714 | - return self::$defaultInstance->file_put_contents($path, $data); |
|
715 | - } |
|
716 | - |
|
717 | - static public function unlink($path) { |
|
718 | - return self::$defaultInstance->unlink($path); |
|
719 | - } |
|
720 | - |
|
721 | - static public function rename($path1, $path2) { |
|
722 | - return self::$defaultInstance->rename($path1, $path2); |
|
723 | - } |
|
724 | - |
|
725 | - static public function copy($path1, $path2) { |
|
726 | - return self::$defaultInstance->copy($path1, $path2); |
|
727 | - } |
|
728 | - |
|
729 | - static public function fopen($path, $mode) { |
|
730 | - return self::$defaultInstance->fopen($path, $mode); |
|
731 | - } |
|
732 | - |
|
733 | - /** |
|
734 | - * @return string |
|
735 | - */ |
|
736 | - static public function toTmpFile($path) { |
|
737 | - return self::$defaultInstance->toTmpFile($path); |
|
738 | - } |
|
739 | - |
|
740 | - static public function fromTmpFile($tmpFile, $path) { |
|
741 | - return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
742 | - } |
|
743 | - |
|
744 | - static public function getMimeType($path) { |
|
745 | - return self::$defaultInstance->getMimeType($path); |
|
746 | - } |
|
747 | - |
|
748 | - static public function hash($type, $path, $raw = false) { |
|
749 | - return self::$defaultInstance->hash($type, $path, $raw); |
|
750 | - } |
|
751 | - |
|
752 | - static public function free_space($path = '/') { |
|
753 | - return self::$defaultInstance->free_space($path); |
|
754 | - } |
|
755 | - |
|
756 | - static public function search($query) { |
|
757 | - return self::$defaultInstance->search($query); |
|
758 | - } |
|
759 | - |
|
760 | - /** |
|
761 | - * @param string $query |
|
762 | - */ |
|
763 | - static public function searchByMime($query) { |
|
764 | - return self::$defaultInstance->searchByMime($query); |
|
765 | - } |
|
766 | - |
|
767 | - /** |
|
768 | - * @param string|int $tag name or tag id |
|
769 | - * @param string $userId owner of the tags |
|
770 | - * @return FileInfo[] array or file info |
|
771 | - */ |
|
772 | - static public function searchByTag($tag, $userId) { |
|
773 | - return self::$defaultInstance->searchByTag($tag, $userId); |
|
774 | - } |
|
775 | - |
|
776 | - /** |
|
777 | - * check if a file or folder has been updated since $time |
|
778 | - * |
|
779 | - * @param string $path |
|
780 | - * @param int $time |
|
781 | - * @return bool |
|
782 | - */ |
|
783 | - static public function hasUpdated($path, $time) { |
|
784 | - return self::$defaultInstance->hasUpdated($path, $time); |
|
785 | - } |
|
786 | - |
|
787 | - /** |
|
788 | - * Fix common problems with a file path |
|
789 | - * |
|
790 | - * @param string $path |
|
791 | - * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
792 | - * @param bool $isAbsolutePath whether the given path is absolute |
|
793 | - * @param bool $keepUnicode true to disable unicode normalization |
|
794 | - * @return string |
|
795 | - */ |
|
796 | - public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
797 | - if (is_null(self::$normalizedPathCache)) { |
|
798 | - self::$normalizedPathCache = new CappedMemoryCache(2048); |
|
799 | - } |
|
800 | - |
|
801 | - /** |
|
802 | - * FIXME: This is a workaround for existing classes and files which call |
|
803 | - * this function with another type than a valid string. This |
|
804 | - * conversion should get removed as soon as all existing |
|
805 | - * function calls have been fixed. |
|
806 | - */ |
|
807 | - $path = (string)$path; |
|
808 | - |
|
809 | - $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
810 | - |
|
811 | - if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
812 | - return self::$normalizedPathCache[$cacheKey]; |
|
813 | - } |
|
814 | - |
|
815 | - if ($path == '') { |
|
816 | - return '/'; |
|
817 | - } |
|
818 | - |
|
819 | - //normalize unicode if possible |
|
820 | - if (!$keepUnicode) { |
|
821 | - $path = \OC_Util::normalizeUnicode($path); |
|
822 | - } |
|
823 | - |
|
824 | - //no windows style slashes |
|
825 | - $path = str_replace('\\', '/', $path); |
|
826 | - |
|
827 | - //add leading slash |
|
828 | - if ($path[0] !== '/') { |
|
829 | - $path = '/' . $path; |
|
830 | - } |
|
831 | - |
|
832 | - // remove '/./' |
|
833 | - // ugly, but str_replace() can't replace them all in one go |
|
834 | - // as the replacement itself is part of the search string |
|
835 | - // which will only be found during the next iteration |
|
836 | - while (strpos($path, '/./') !== false) { |
|
837 | - $path = str_replace('/./', '/', $path); |
|
838 | - } |
|
839 | - // remove sequences of slashes |
|
840 | - $path = preg_replace('#/{2,}#', '/', $path); |
|
841 | - |
|
842 | - //remove trailing slash |
|
843 | - if ($stripTrailingSlash and strlen($path) > 1) { |
|
844 | - $path = rtrim($path, '/'); |
|
845 | - } |
|
846 | - |
|
847 | - // remove trailing '/.' |
|
848 | - if (substr($path, -2) == '/.') { |
|
849 | - $path = substr($path, 0, -2); |
|
850 | - } |
|
851 | - |
|
852 | - $normalizedPath = $path; |
|
853 | - self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
854 | - |
|
855 | - return $normalizedPath; |
|
856 | - } |
|
857 | - |
|
858 | - /** |
|
859 | - * get the filesystem info |
|
860 | - * |
|
861 | - * @param string $path |
|
862 | - * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
863 | - * defaults to true |
|
864 | - * @return \OC\Files\FileInfo|bool False if file does not exist |
|
865 | - */ |
|
866 | - public static function getFileInfo($path, $includeMountPoints = true) { |
|
867 | - return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
868 | - } |
|
869 | - |
|
870 | - /** |
|
871 | - * change file metadata |
|
872 | - * |
|
873 | - * @param string $path |
|
874 | - * @param array $data |
|
875 | - * @return int |
|
876 | - * |
|
877 | - * returns the fileid of the updated file |
|
878 | - */ |
|
879 | - public static function putFileInfo($path, $data) { |
|
880 | - return self::$defaultInstance->putFileInfo($path, $data); |
|
881 | - } |
|
882 | - |
|
883 | - /** |
|
884 | - * get the content of a directory |
|
885 | - * |
|
886 | - * @param string $directory path under datadirectory |
|
887 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
888 | - * @return \OC\Files\FileInfo[] |
|
889 | - */ |
|
890 | - public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
891 | - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
892 | - } |
|
893 | - |
|
894 | - /** |
|
895 | - * Get the path of a file by id |
|
896 | - * |
|
897 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
898 | - * |
|
899 | - * @param int $id |
|
900 | - * @throws NotFoundException |
|
901 | - * @return string |
|
902 | - */ |
|
903 | - public static function getPath($id) { |
|
904 | - return self::$defaultInstance->getPath($id); |
|
905 | - } |
|
906 | - |
|
907 | - /** |
|
908 | - * Get the owner for a file or folder |
|
909 | - * |
|
910 | - * @param string $path |
|
911 | - * @return string |
|
912 | - */ |
|
913 | - public static function getOwner($path) { |
|
914 | - return self::$defaultInstance->getOwner($path); |
|
915 | - } |
|
916 | - |
|
917 | - /** |
|
918 | - * get the ETag for a file or folder |
|
919 | - * |
|
920 | - * @param string $path |
|
921 | - * @return string |
|
922 | - */ |
|
923 | - static public function getETag($path) { |
|
924 | - return self::$defaultInstance->getETag($path); |
|
925 | - } |
|
73 | + /** |
|
74 | + * @var Mount\Manager $mounts |
|
75 | + */ |
|
76 | + private static $mounts; |
|
77 | + |
|
78 | + public static $loaded = false; |
|
79 | + /** |
|
80 | + * @var \OC\Files\View $defaultInstance |
|
81 | + */ |
|
82 | + static private $defaultInstance; |
|
83 | + |
|
84 | + static private $usersSetup = array(); |
|
85 | + |
|
86 | + static private $normalizedPathCache = null; |
|
87 | + |
|
88 | + static private $listeningForProviders = false; |
|
89 | + |
|
90 | + /** |
|
91 | + * classname which used for hooks handling |
|
92 | + * used as signalclass in OC_Hooks::emit() |
|
93 | + */ |
|
94 | + const CLASSNAME = 'OC_Filesystem'; |
|
95 | + |
|
96 | + /** |
|
97 | + * signalname emitted before file renaming |
|
98 | + * |
|
99 | + * @param string $oldpath |
|
100 | + * @param string $newpath |
|
101 | + */ |
|
102 | + const signal_rename = 'rename'; |
|
103 | + |
|
104 | + /** |
|
105 | + * signal emitted after file renaming |
|
106 | + * |
|
107 | + * @param string $oldpath |
|
108 | + * @param string $newpath |
|
109 | + */ |
|
110 | + const signal_post_rename = 'post_rename'; |
|
111 | + |
|
112 | + /** |
|
113 | + * signal emitted before file/dir creation |
|
114 | + * |
|
115 | + * @param string $path |
|
116 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
117 | + */ |
|
118 | + const signal_create = 'create'; |
|
119 | + |
|
120 | + /** |
|
121 | + * signal emitted after file/dir creation |
|
122 | + * |
|
123 | + * @param string $path |
|
124 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
125 | + */ |
|
126 | + const signal_post_create = 'post_create'; |
|
127 | + |
|
128 | + /** |
|
129 | + * signal emits before file/dir copy |
|
130 | + * |
|
131 | + * @param string $oldpath |
|
132 | + * @param string $newpath |
|
133 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
134 | + */ |
|
135 | + const signal_copy = 'copy'; |
|
136 | + |
|
137 | + /** |
|
138 | + * signal emits after file/dir copy |
|
139 | + * |
|
140 | + * @param string $oldpath |
|
141 | + * @param string $newpath |
|
142 | + */ |
|
143 | + const signal_post_copy = 'post_copy'; |
|
144 | + |
|
145 | + /** |
|
146 | + * signal emits before file/dir save |
|
147 | + * |
|
148 | + * @param string $path |
|
149 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
150 | + */ |
|
151 | + const signal_write = 'write'; |
|
152 | + |
|
153 | + /** |
|
154 | + * signal emits after file/dir save |
|
155 | + * |
|
156 | + * @param string $path |
|
157 | + */ |
|
158 | + const signal_post_write = 'post_write'; |
|
159 | + |
|
160 | + /** |
|
161 | + * signal emitted before file/dir update |
|
162 | + * |
|
163 | + * @param string $path |
|
164 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
165 | + */ |
|
166 | + const signal_update = 'update'; |
|
167 | + |
|
168 | + /** |
|
169 | + * signal emitted after file/dir update |
|
170 | + * |
|
171 | + * @param string $path |
|
172 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
173 | + */ |
|
174 | + const signal_post_update = 'post_update'; |
|
175 | + |
|
176 | + /** |
|
177 | + * signal emits when reading file/dir |
|
178 | + * |
|
179 | + * @param string $path |
|
180 | + */ |
|
181 | + const signal_read = 'read'; |
|
182 | + |
|
183 | + /** |
|
184 | + * signal emits when removing file/dir |
|
185 | + * |
|
186 | + * @param string $path |
|
187 | + */ |
|
188 | + const signal_delete = 'delete'; |
|
189 | + |
|
190 | + /** |
|
191 | + * parameters definitions for signals |
|
192 | + */ |
|
193 | + const signal_param_path = 'path'; |
|
194 | + const signal_param_oldpath = 'oldpath'; |
|
195 | + const signal_param_newpath = 'newpath'; |
|
196 | + |
|
197 | + /** |
|
198 | + * run - changing this flag to false in hook handler will cancel event |
|
199 | + */ |
|
200 | + const signal_param_run = 'run'; |
|
201 | + |
|
202 | + const signal_create_mount = 'create_mount'; |
|
203 | + const signal_delete_mount = 'delete_mount'; |
|
204 | + const signal_param_mount_type = 'mounttype'; |
|
205 | + const signal_param_users = 'users'; |
|
206 | + |
|
207 | + /** |
|
208 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
209 | + */ |
|
210 | + private static $loader; |
|
211 | + |
|
212 | + /** @var bool */ |
|
213 | + private static $logWarningWhenAddingStorageWrapper = true; |
|
214 | + |
|
215 | + /** |
|
216 | + * @param bool $shouldLog |
|
217 | + * @return bool previous value |
|
218 | + * @internal |
|
219 | + */ |
|
220 | + public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
221 | + $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
222 | + self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
223 | + return $previousValue; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $wrapperName |
|
228 | + * @param callable $wrapper |
|
229 | + * @param int $priority |
|
230 | + */ |
|
231 | + public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
232 | + if (self::$logWarningWhenAddingStorageWrapper) { |
|
233 | + \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
234 | + 'wrapper' => $wrapperName, |
|
235 | + 'app' => 'filesystem', |
|
236 | + ]); |
|
237 | + } |
|
238 | + |
|
239 | + $mounts = self::getMountManager()->getAll(); |
|
240 | + if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
241 | + // do not re-wrap if storage with this name already existed |
|
242 | + return; |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Returns the storage factory |
|
248 | + * |
|
249 | + * @return \OCP\Files\Storage\IStorageFactory |
|
250 | + */ |
|
251 | + public static function getLoader() { |
|
252 | + if (!self::$loader) { |
|
253 | + self::$loader = new StorageFactory(); |
|
254 | + } |
|
255 | + return self::$loader; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Returns the mount manager |
|
260 | + * |
|
261 | + * @return \OC\Files\Mount\Manager |
|
262 | + */ |
|
263 | + public static function getMountManager($user = '') { |
|
264 | + if (!self::$mounts) { |
|
265 | + \OC_Util::setupFS($user); |
|
266 | + } |
|
267 | + return self::$mounts; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * get the mountpoint of the storage object for a path |
|
272 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
273 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
274 | + * and doesn't take the chroot into account ) |
|
275 | + * |
|
276 | + * @param string $path |
|
277 | + * @return string |
|
278 | + */ |
|
279 | + static public function getMountPoint($path) { |
|
280 | + if (!self::$mounts) { |
|
281 | + \OC_Util::setupFS(); |
|
282 | + } |
|
283 | + $mount = self::$mounts->find($path); |
|
284 | + if ($mount) { |
|
285 | + return $mount->getMountPoint(); |
|
286 | + } else { |
|
287 | + return ''; |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * get a list of all mount points in a directory |
|
293 | + * |
|
294 | + * @param string $path |
|
295 | + * @return string[] |
|
296 | + */ |
|
297 | + static public function getMountPoints($path) { |
|
298 | + if (!self::$mounts) { |
|
299 | + \OC_Util::setupFS(); |
|
300 | + } |
|
301 | + $result = array(); |
|
302 | + $mounts = self::$mounts->findIn($path); |
|
303 | + foreach ($mounts as $mount) { |
|
304 | + $result[] = $mount->getMountPoint(); |
|
305 | + } |
|
306 | + return $result; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * get the storage mounted at $mountPoint |
|
311 | + * |
|
312 | + * @param string $mountPoint |
|
313 | + * @return \OC\Files\Storage\Storage |
|
314 | + */ |
|
315 | + public static function getStorage($mountPoint) { |
|
316 | + if (!self::$mounts) { |
|
317 | + \OC_Util::setupFS(); |
|
318 | + } |
|
319 | + $mount = self::$mounts->find($mountPoint); |
|
320 | + return $mount->getStorage(); |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * @param string $id |
|
325 | + * @return Mount\MountPoint[] |
|
326 | + */ |
|
327 | + public static function getMountByStorageId($id) { |
|
328 | + if (!self::$mounts) { |
|
329 | + \OC_Util::setupFS(); |
|
330 | + } |
|
331 | + return self::$mounts->findByStorageId($id); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @param int $id |
|
336 | + * @return Mount\MountPoint[] |
|
337 | + */ |
|
338 | + public static function getMountByNumericId($id) { |
|
339 | + if (!self::$mounts) { |
|
340 | + \OC_Util::setupFS(); |
|
341 | + } |
|
342 | + return self::$mounts->findByNumericId($id); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * resolve a path to a storage and internal path |
|
347 | + * |
|
348 | + * @param string $path |
|
349 | + * @return array an array consisting of the storage and the internal path |
|
350 | + */ |
|
351 | + static public function resolvePath($path) { |
|
352 | + if (!self::$mounts) { |
|
353 | + \OC_Util::setupFS(); |
|
354 | + } |
|
355 | + $mount = self::$mounts->find($path); |
|
356 | + if ($mount) { |
|
357 | + return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
358 | + } else { |
|
359 | + return array(null, null); |
|
360 | + } |
|
361 | + } |
|
362 | + |
|
363 | + static public function init($user, $root) { |
|
364 | + if (self::$defaultInstance) { |
|
365 | + return false; |
|
366 | + } |
|
367 | + self::getLoader(); |
|
368 | + self::$defaultInstance = new View($root); |
|
369 | + |
|
370 | + if (!self::$mounts) { |
|
371 | + self::$mounts = \OC::$server->getMountManager(); |
|
372 | + } |
|
373 | + |
|
374 | + //load custom mount config |
|
375 | + self::initMountPoints($user); |
|
376 | + |
|
377 | + self::$loaded = true; |
|
378 | + |
|
379 | + return true; |
|
380 | + } |
|
381 | + |
|
382 | + static public function initMountManager() { |
|
383 | + if (!self::$mounts) { |
|
384 | + self::$mounts = \OC::$server->getMountManager(); |
|
385 | + } |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * Initialize system and personal mount points for a user |
|
390 | + * |
|
391 | + * @param string $user |
|
392 | + * @throws \OC\User\NoUserException if the user is not available |
|
393 | + */ |
|
394 | + public static function initMountPoints($user = '') { |
|
395 | + if ($user == '') { |
|
396 | + $user = \OC_User::getUser(); |
|
397 | + } |
|
398 | + if ($user === null || $user === false || $user === '') { |
|
399 | + throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
400 | + } |
|
401 | + |
|
402 | + if (isset(self::$usersSetup[$user])) { |
|
403 | + return; |
|
404 | + } |
|
405 | + |
|
406 | + self::$usersSetup[$user] = true; |
|
407 | + |
|
408 | + $userManager = \OC::$server->getUserManager(); |
|
409 | + $userObject = $userManager->get($user); |
|
410 | + |
|
411 | + if (is_null($userObject)) { |
|
412 | + \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR); |
|
413 | + // reset flag, this will make it possible to rethrow the exception if called again |
|
414 | + unset(self::$usersSetup[$user]); |
|
415 | + throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
416 | + } |
|
417 | + |
|
418 | + $realUid = $userObject->getUID(); |
|
419 | + // workaround in case of different casings |
|
420 | + if ($user !== $realUid) { |
|
421 | + $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
422 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, ILogger::WARN); |
|
423 | + $user = $realUid; |
|
424 | + |
|
425 | + // again with the correct casing |
|
426 | + if (isset(self::$usersSetup[$user])) { |
|
427 | + return; |
|
428 | + } |
|
429 | + |
|
430 | + self::$usersSetup[$user] = true; |
|
431 | + } |
|
432 | + |
|
433 | + if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
434 | + /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
435 | + $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
436 | + |
|
437 | + // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
438 | + $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
439 | + |
|
440 | + self::getMountManager()->addMount($homeMount); |
|
441 | + |
|
442 | + \OC\Files\Filesystem::getStorage($user); |
|
443 | + |
|
444 | + // Chance to mount for other storages |
|
445 | + if ($userObject) { |
|
446 | + $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
447 | + $mounts[] = $homeMount; |
|
448 | + $mountConfigManager->registerMounts($userObject, $mounts); |
|
449 | + } |
|
450 | + |
|
451 | + self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
452 | + } else { |
|
453 | + self::getMountManager()->addMount(new MountPoint( |
|
454 | + new NullStorage([]), |
|
455 | + '/' . $user |
|
456 | + )); |
|
457 | + self::getMountManager()->addMount(new MountPoint( |
|
458 | + new NullStorage([]), |
|
459 | + '/' . $user . '/files' |
|
460 | + )); |
|
461 | + } |
|
462 | + \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Get mounts from mount providers that are registered after setup |
|
467 | + * |
|
468 | + * @param MountProviderCollection $mountConfigManager |
|
469 | + * @param IUserManager $userManager |
|
470 | + */ |
|
471 | + private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
472 | + if (!self::$listeningForProviders) { |
|
473 | + self::$listeningForProviders = true; |
|
474 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
475 | + foreach (Filesystem::$usersSetup as $user => $setup) { |
|
476 | + $userObject = $userManager->get($user); |
|
477 | + if ($userObject) { |
|
478 | + $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
479 | + array_walk($mounts, array(self::$mounts, 'addMount')); |
|
480 | + } |
|
481 | + } |
|
482 | + }); |
|
483 | + } |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * get the default filesystem view |
|
488 | + * |
|
489 | + * @return View |
|
490 | + */ |
|
491 | + static public function getView() { |
|
492 | + return self::$defaultInstance; |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * tear down the filesystem, removing all storage providers |
|
497 | + */ |
|
498 | + static public function tearDown() { |
|
499 | + self::clearMounts(); |
|
500 | + self::$defaultInstance = null; |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * get the relative path of the root data directory for the current user |
|
505 | + * |
|
506 | + * @return string |
|
507 | + * |
|
508 | + * Returns path like /admin/files |
|
509 | + */ |
|
510 | + static public function getRoot() { |
|
511 | + if (!self::$defaultInstance) { |
|
512 | + return null; |
|
513 | + } |
|
514 | + return self::$defaultInstance->getRoot(); |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * clear all mounts and storage backends |
|
519 | + */ |
|
520 | + public static function clearMounts() { |
|
521 | + if (self::$mounts) { |
|
522 | + self::$usersSetup = array(); |
|
523 | + self::$mounts->clear(); |
|
524 | + } |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
529 | + * |
|
530 | + * @param \OC\Files\Storage\Storage|string $class |
|
531 | + * @param array $arguments |
|
532 | + * @param string $mountpoint |
|
533 | + */ |
|
534 | + static public function mount($class, $arguments, $mountpoint) { |
|
535 | + if (!self::$mounts) { |
|
536 | + \OC_Util::setupFS(); |
|
537 | + } |
|
538 | + $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
539 | + self::$mounts->addMount($mount); |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * return the path to a local version of the file |
|
544 | + * we need this because we can't know if a file is stored local or not from |
|
545 | + * outside the filestorage and for some purposes a local file is needed |
|
546 | + * |
|
547 | + * @param string $path |
|
548 | + * @return string |
|
549 | + */ |
|
550 | + static public function getLocalFile($path) { |
|
551 | + return self::$defaultInstance->getLocalFile($path); |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * @param string $path |
|
556 | + * @return string |
|
557 | + */ |
|
558 | + static public function getLocalFolder($path) { |
|
559 | + return self::$defaultInstance->getLocalFolder($path); |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * return path to file which reflects one visible in browser |
|
564 | + * |
|
565 | + * @param string $path |
|
566 | + * @return string |
|
567 | + */ |
|
568 | + static public function getLocalPath($path) { |
|
569 | + $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
570 | + $newpath = $path; |
|
571 | + if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
572 | + $newpath = substr($path, strlen($datadir)); |
|
573 | + } |
|
574 | + return $newpath; |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * check if the requested path is valid |
|
579 | + * |
|
580 | + * @param string $path |
|
581 | + * @return bool |
|
582 | + */ |
|
583 | + static public function isValidPath($path) { |
|
584 | + $path = self::normalizePath($path); |
|
585 | + if (!$path || $path[0] !== '/') { |
|
586 | + $path = '/' . $path; |
|
587 | + } |
|
588 | + if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
589 | + return false; |
|
590 | + } |
|
591 | + return true; |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * checks if a file is blacklisted for storage in the filesystem |
|
596 | + * Listens to write and rename hooks |
|
597 | + * |
|
598 | + * @param array $data from hook |
|
599 | + */ |
|
600 | + static public function isBlacklisted($data) { |
|
601 | + if (isset($data['path'])) { |
|
602 | + $path = $data['path']; |
|
603 | + } else if (isset($data['newpath'])) { |
|
604 | + $path = $data['newpath']; |
|
605 | + } |
|
606 | + if (isset($path)) { |
|
607 | + if (self::isFileBlacklisted($path)) { |
|
608 | + $data['run'] = false; |
|
609 | + } |
|
610 | + } |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * @param string $filename |
|
615 | + * @return bool |
|
616 | + */ |
|
617 | + static public function isFileBlacklisted($filename) { |
|
618 | + $filename = self::normalizePath($filename); |
|
619 | + |
|
620 | + $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
621 | + $filename = strtolower(basename($filename)); |
|
622 | + return in_array($filename, $blacklist); |
|
623 | + } |
|
624 | + |
|
625 | + /** |
|
626 | + * check if the directory should be ignored when scanning |
|
627 | + * NOTE: the special directories . and .. would cause never ending recursion |
|
628 | + * |
|
629 | + * @param String $dir |
|
630 | + * @return boolean |
|
631 | + */ |
|
632 | + static public function isIgnoredDir($dir) { |
|
633 | + if ($dir === '.' || $dir === '..') { |
|
634 | + return true; |
|
635 | + } |
|
636 | + return false; |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
641 | + */ |
|
642 | + static public function mkdir($path) { |
|
643 | + return self::$defaultInstance->mkdir($path); |
|
644 | + } |
|
645 | + |
|
646 | + static public function rmdir($path) { |
|
647 | + return self::$defaultInstance->rmdir($path); |
|
648 | + } |
|
649 | + |
|
650 | + static public function is_dir($path) { |
|
651 | + return self::$defaultInstance->is_dir($path); |
|
652 | + } |
|
653 | + |
|
654 | + static public function is_file($path) { |
|
655 | + return self::$defaultInstance->is_file($path); |
|
656 | + } |
|
657 | + |
|
658 | + static public function stat($path) { |
|
659 | + return self::$defaultInstance->stat($path); |
|
660 | + } |
|
661 | + |
|
662 | + static public function filetype($path) { |
|
663 | + return self::$defaultInstance->filetype($path); |
|
664 | + } |
|
665 | + |
|
666 | + static public function filesize($path) { |
|
667 | + return self::$defaultInstance->filesize($path); |
|
668 | + } |
|
669 | + |
|
670 | + static public function readfile($path) { |
|
671 | + return self::$defaultInstance->readfile($path); |
|
672 | + } |
|
673 | + |
|
674 | + static public function isCreatable($path) { |
|
675 | + return self::$defaultInstance->isCreatable($path); |
|
676 | + } |
|
677 | + |
|
678 | + static public function isReadable($path) { |
|
679 | + return self::$defaultInstance->isReadable($path); |
|
680 | + } |
|
681 | + |
|
682 | + static public function isUpdatable($path) { |
|
683 | + return self::$defaultInstance->isUpdatable($path); |
|
684 | + } |
|
685 | + |
|
686 | + static public function isDeletable($path) { |
|
687 | + return self::$defaultInstance->isDeletable($path); |
|
688 | + } |
|
689 | + |
|
690 | + static public function isSharable($path) { |
|
691 | + return self::$defaultInstance->isSharable($path); |
|
692 | + } |
|
693 | + |
|
694 | + static public function file_exists($path) { |
|
695 | + return self::$defaultInstance->file_exists($path); |
|
696 | + } |
|
697 | + |
|
698 | + static public function filemtime($path) { |
|
699 | + return self::$defaultInstance->filemtime($path); |
|
700 | + } |
|
701 | + |
|
702 | + static public function touch($path, $mtime = null) { |
|
703 | + return self::$defaultInstance->touch($path, $mtime); |
|
704 | + } |
|
705 | + |
|
706 | + /** |
|
707 | + * @return string |
|
708 | + */ |
|
709 | + static public function file_get_contents($path) { |
|
710 | + return self::$defaultInstance->file_get_contents($path); |
|
711 | + } |
|
712 | + |
|
713 | + static public function file_put_contents($path, $data) { |
|
714 | + return self::$defaultInstance->file_put_contents($path, $data); |
|
715 | + } |
|
716 | + |
|
717 | + static public function unlink($path) { |
|
718 | + return self::$defaultInstance->unlink($path); |
|
719 | + } |
|
720 | + |
|
721 | + static public function rename($path1, $path2) { |
|
722 | + return self::$defaultInstance->rename($path1, $path2); |
|
723 | + } |
|
724 | + |
|
725 | + static public function copy($path1, $path2) { |
|
726 | + return self::$defaultInstance->copy($path1, $path2); |
|
727 | + } |
|
728 | + |
|
729 | + static public function fopen($path, $mode) { |
|
730 | + return self::$defaultInstance->fopen($path, $mode); |
|
731 | + } |
|
732 | + |
|
733 | + /** |
|
734 | + * @return string |
|
735 | + */ |
|
736 | + static public function toTmpFile($path) { |
|
737 | + return self::$defaultInstance->toTmpFile($path); |
|
738 | + } |
|
739 | + |
|
740 | + static public function fromTmpFile($tmpFile, $path) { |
|
741 | + return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
742 | + } |
|
743 | + |
|
744 | + static public function getMimeType($path) { |
|
745 | + return self::$defaultInstance->getMimeType($path); |
|
746 | + } |
|
747 | + |
|
748 | + static public function hash($type, $path, $raw = false) { |
|
749 | + return self::$defaultInstance->hash($type, $path, $raw); |
|
750 | + } |
|
751 | + |
|
752 | + static public function free_space($path = '/') { |
|
753 | + return self::$defaultInstance->free_space($path); |
|
754 | + } |
|
755 | + |
|
756 | + static public function search($query) { |
|
757 | + return self::$defaultInstance->search($query); |
|
758 | + } |
|
759 | + |
|
760 | + /** |
|
761 | + * @param string $query |
|
762 | + */ |
|
763 | + static public function searchByMime($query) { |
|
764 | + return self::$defaultInstance->searchByMime($query); |
|
765 | + } |
|
766 | + |
|
767 | + /** |
|
768 | + * @param string|int $tag name or tag id |
|
769 | + * @param string $userId owner of the tags |
|
770 | + * @return FileInfo[] array or file info |
|
771 | + */ |
|
772 | + static public function searchByTag($tag, $userId) { |
|
773 | + return self::$defaultInstance->searchByTag($tag, $userId); |
|
774 | + } |
|
775 | + |
|
776 | + /** |
|
777 | + * check if a file or folder has been updated since $time |
|
778 | + * |
|
779 | + * @param string $path |
|
780 | + * @param int $time |
|
781 | + * @return bool |
|
782 | + */ |
|
783 | + static public function hasUpdated($path, $time) { |
|
784 | + return self::$defaultInstance->hasUpdated($path, $time); |
|
785 | + } |
|
786 | + |
|
787 | + /** |
|
788 | + * Fix common problems with a file path |
|
789 | + * |
|
790 | + * @param string $path |
|
791 | + * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
792 | + * @param bool $isAbsolutePath whether the given path is absolute |
|
793 | + * @param bool $keepUnicode true to disable unicode normalization |
|
794 | + * @return string |
|
795 | + */ |
|
796 | + public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
797 | + if (is_null(self::$normalizedPathCache)) { |
|
798 | + self::$normalizedPathCache = new CappedMemoryCache(2048); |
|
799 | + } |
|
800 | + |
|
801 | + /** |
|
802 | + * FIXME: This is a workaround for existing classes and files which call |
|
803 | + * this function with another type than a valid string. This |
|
804 | + * conversion should get removed as soon as all existing |
|
805 | + * function calls have been fixed. |
|
806 | + */ |
|
807 | + $path = (string)$path; |
|
808 | + |
|
809 | + $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
810 | + |
|
811 | + if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
812 | + return self::$normalizedPathCache[$cacheKey]; |
|
813 | + } |
|
814 | + |
|
815 | + if ($path == '') { |
|
816 | + return '/'; |
|
817 | + } |
|
818 | + |
|
819 | + //normalize unicode if possible |
|
820 | + if (!$keepUnicode) { |
|
821 | + $path = \OC_Util::normalizeUnicode($path); |
|
822 | + } |
|
823 | + |
|
824 | + //no windows style slashes |
|
825 | + $path = str_replace('\\', '/', $path); |
|
826 | + |
|
827 | + //add leading slash |
|
828 | + if ($path[0] !== '/') { |
|
829 | + $path = '/' . $path; |
|
830 | + } |
|
831 | + |
|
832 | + // remove '/./' |
|
833 | + // ugly, but str_replace() can't replace them all in one go |
|
834 | + // as the replacement itself is part of the search string |
|
835 | + // which will only be found during the next iteration |
|
836 | + while (strpos($path, '/./') !== false) { |
|
837 | + $path = str_replace('/./', '/', $path); |
|
838 | + } |
|
839 | + // remove sequences of slashes |
|
840 | + $path = preg_replace('#/{2,}#', '/', $path); |
|
841 | + |
|
842 | + //remove trailing slash |
|
843 | + if ($stripTrailingSlash and strlen($path) > 1) { |
|
844 | + $path = rtrim($path, '/'); |
|
845 | + } |
|
846 | + |
|
847 | + // remove trailing '/.' |
|
848 | + if (substr($path, -2) == '/.') { |
|
849 | + $path = substr($path, 0, -2); |
|
850 | + } |
|
851 | + |
|
852 | + $normalizedPath = $path; |
|
853 | + self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
854 | + |
|
855 | + return $normalizedPath; |
|
856 | + } |
|
857 | + |
|
858 | + /** |
|
859 | + * get the filesystem info |
|
860 | + * |
|
861 | + * @param string $path |
|
862 | + * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
863 | + * defaults to true |
|
864 | + * @return \OC\Files\FileInfo|bool False if file does not exist |
|
865 | + */ |
|
866 | + public static function getFileInfo($path, $includeMountPoints = true) { |
|
867 | + return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
868 | + } |
|
869 | + |
|
870 | + /** |
|
871 | + * change file metadata |
|
872 | + * |
|
873 | + * @param string $path |
|
874 | + * @param array $data |
|
875 | + * @return int |
|
876 | + * |
|
877 | + * returns the fileid of the updated file |
|
878 | + */ |
|
879 | + public static function putFileInfo($path, $data) { |
|
880 | + return self::$defaultInstance->putFileInfo($path, $data); |
|
881 | + } |
|
882 | + |
|
883 | + /** |
|
884 | + * get the content of a directory |
|
885 | + * |
|
886 | + * @param string $directory path under datadirectory |
|
887 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
888 | + * @return \OC\Files\FileInfo[] |
|
889 | + */ |
|
890 | + public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
891 | + return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
892 | + } |
|
893 | + |
|
894 | + /** |
|
895 | + * Get the path of a file by id |
|
896 | + * |
|
897 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
898 | + * |
|
899 | + * @param int $id |
|
900 | + * @throws NotFoundException |
|
901 | + * @return string |
|
902 | + */ |
|
903 | + public static function getPath($id) { |
|
904 | + return self::$defaultInstance->getPath($id); |
|
905 | + } |
|
906 | + |
|
907 | + /** |
|
908 | + * Get the owner for a file or folder |
|
909 | + * |
|
910 | + * @param string $path |
|
911 | + * @return string |
|
912 | + */ |
|
913 | + public static function getOwner($path) { |
|
914 | + return self::$defaultInstance->getOwner($path); |
|
915 | + } |
|
916 | + |
|
917 | + /** |
|
918 | + * get the ETag for a file or folder |
|
919 | + * |
|
920 | + * @param string $path |
|
921 | + * @return string |
|
922 | + */ |
|
923 | + static public function getETag($path) { |
|
924 | + return self::$defaultInstance->getETag($path); |
|
925 | + } |
|
926 | 926 | } |
@@ -409,17 +409,17 @@ discard block |
||
409 | 409 | $userObject = $userManager->get($user); |
410 | 410 | |
411 | 411 | if (is_null($userObject)) { |
412 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR); |
|
412 | + \OCP\Util::writeLog('files', ' Backends provided no user object for '.$user, ILogger::ERROR); |
|
413 | 413 | // reset flag, this will make it possible to rethrow the exception if called again |
414 | 414 | unset(self::$usersSetup[$user]); |
415 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
415 | + throw new \OC\User\NoUserException('Backends provided no user object for '.$user); |
|
416 | 416 | } |
417 | 417 | |
418 | 418 | $realUid = $userObject->getUID(); |
419 | 419 | // workaround in case of different casings |
420 | 420 | if ($user !== $realUid) { |
421 | 421 | $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
422 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, ILogger::WARN); |
|
422 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "'.$realUid.'" got "'.$user.'". Stack: '.$stack, ILogger::WARN); |
|
423 | 423 | $user = $realUid; |
424 | 424 | |
425 | 425 | // again with the correct casing |
@@ -452,11 +452,11 @@ discard block |
||
452 | 452 | } else { |
453 | 453 | self::getMountManager()->addMount(new MountPoint( |
454 | 454 | new NullStorage([]), |
455 | - '/' . $user |
|
455 | + '/'.$user |
|
456 | 456 | )); |
457 | 457 | self::getMountManager()->addMount(new MountPoint( |
458 | 458 | new NullStorage([]), |
459 | - '/' . $user . '/files' |
|
459 | + '/'.$user.'/files' |
|
460 | 460 | )); |
461 | 461 | } |
462 | 462 | \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
@@ -471,7 +471,7 @@ discard block |
||
471 | 471 | private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
472 | 472 | if (!self::$listeningForProviders) { |
473 | 473 | self::$listeningForProviders = true; |
474 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
474 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function(IMountProvider $provider) use ($userManager) { |
|
475 | 475 | foreach (Filesystem::$usersSetup as $user => $setup) { |
476 | 476 | $userObject = $userManager->get($user); |
477 | 477 | if ($userObject) { |
@@ -566,7 +566,7 @@ discard block |
||
566 | 566 | * @return string |
567 | 567 | */ |
568 | 568 | static public function getLocalPath($path) { |
569 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
569 | + $datadir = \OC_User::getHome(\OC_User::getUser()).'/files'; |
|
570 | 570 | $newpath = $path; |
571 | 571 | if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
572 | 572 | $newpath = substr($path, strlen($datadir)); |
@@ -583,7 +583,7 @@ discard block |
||
583 | 583 | static public function isValidPath($path) { |
584 | 584 | $path = self::normalizePath($path); |
585 | 585 | if (!$path || $path[0] !== '/') { |
586 | - $path = '/' . $path; |
|
586 | + $path = '/'.$path; |
|
587 | 587 | } |
588 | 588 | if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
589 | 589 | return false; |
@@ -804,7 +804,7 @@ discard block |
||
804 | 804 | * conversion should get removed as soon as all existing |
805 | 805 | * function calls have been fixed. |
806 | 806 | */ |
807 | - $path = (string)$path; |
|
807 | + $path = (string) $path; |
|
808 | 808 | |
809 | 809 | $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
810 | 810 | |
@@ -826,7 +826,7 @@ discard block |
||
826 | 826 | |
827 | 827 | //add leading slash |
828 | 828 | if ($path[0] !== '/') { |
829 | - $path = '/' . $path; |
|
829 | + $path = '/'.$path; |
|
830 | 830 | } |
831 | 831 | |
832 | 832 | // remove '/./' |
@@ -81,2084 +81,2084 @@ |
||
81 | 81 | * \OC\Files\Storage\Storage object |
82 | 82 | */ |
83 | 83 | class View { |
84 | - /** @var string */ |
|
85 | - private $fakeRoot = ''; |
|
86 | - |
|
87 | - /** |
|
88 | - * @var \OCP\Lock\ILockingProvider |
|
89 | - */ |
|
90 | - protected $lockingProvider; |
|
91 | - |
|
92 | - private $lockingEnabled; |
|
93 | - |
|
94 | - private $updaterEnabled = true; |
|
95 | - |
|
96 | - /** @var \OC\User\Manager */ |
|
97 | - private $userManager; |
|
98 | - |
|
99 | - /** @var \OCP\ILogger */ |
|
100 | - private $logger; |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $root |
|
104 | - * @throws \Exception If $root contains an invalid path |
|
105 | - */ |
|
106 | - public function __construct($root = '') { |
|
107 | - if (is_null($root)) { |
|
108 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
109 | - } |
|
110 | - if (!Filesystem::isValidPath($root)) { |
|
111 | - throw new \Exception(); |
|
112 | - } |
|
113 | - |
|
114 | - $this->fakeRoot = $root; |
|
115 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
116 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
117 | - $this->userManager = \OC::$server->getUserManager(); |
|
118 | - $this->logger = \OC::$server->getLogger(); |
|
119 | - } |
|
120 | - |
|
121 | - public function getAbsolutePath($path = '/') { |
|
122 | - if ($path === null) { |
|
123 | - return null; |
|
124 | - } |
|
125 | - $this->assertPathLength($path); |
|
126 | - if ($path === '') { |
|
127 | - $path = '/'; |
|
128 | - } |
|
129 | - if ($path[0] !== '/') { |
|
130 | - $path = '/' . $path; |
|
131 | - } |
|
132 | - return $this->fakeRoot . $path; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * change the root to a fake root |
|
137 | - * |
|
138 | - * @param string $fakeRoot |
|
139 | - * @return boolean|null |
|
140 | - */ |
|
141 | - public function chroot($fakeRoot) { |
|
142 | - if (!$fakeRoot == '') { |
|
143 | - if ($fakeRoot[0] !== '/') { |
|
144 | - $fakeRoot = '/' . $fakeRoot; |
|
145 | - } |
|
146 | - } |
|
147 | - $this->fakeRoot = $fakeRoot; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * get the fake root |
|
152 | - * |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - public function getRoot() { |
|
156 | - return $this->fakeRoot; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * get path relative to the root of the view |
|
161 | - * |
|
162 | - * @param string $path |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function getRelativePath($path) { |
|
166 | - $this->assertPathLength($path); |
|
167 | - if ($this->fakeRoot == '') { |
|
168 | - return $path; |
|
169 | - } |
|
170 | - |
|
171 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
172 | - return '/'; |
|
173 | - } |
|
174 | - |
|
175 | - // missing slashes can cause wrong matches! |
|
176 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
177 | - |
|
178 | - if (strpos($path, $root) !== 0) { |
|
179 | - return null; |
|
180 | - } else { |
|
181 | - $path = substr($path, strlen($this->fakeRoot)); |
|
182 | - if (strlen($path) === 0) { |
|
183 | - return '/'; |
|
184 | - } else { |
|
185 | - return $path; |
|
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * get the mountpoint of the storage object for a path |
|
192 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
193 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
194 | - * and does not take the chroot into account ) |
|
195 | - * |
|
196 | - * @param string $path |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function getMountPoint($path) { |
|
200 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * get the mountpoint of the storage object for a path |
|
205 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
206 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
207 | - * and does not take the chroot into account ) |
|
208 | - * |
|
209 | - * @param string $path |
|
210 | - * @return \OCP\Files\Mount\IMountPoint |
|
211 | - */ |
|
212 | - public function getMount($path) { |
|
213 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * resolve a path to a storage and internal path |
|
218 | - * |
|
219 | - * @param string $path |
|
220 | - * @return array an array consisting of the storage and the internal path |
|
221 | - */ |
|
222 | - public function resolvePath($path) { |
|
223 | - $a = $this->getAbsolutePath($path); |
|
224 | - $p = Filesystem::normalizePath($a); |
|
225 | - return Filesystem::resolvePath($p); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * return the path to a local version of the file |
|
230 | - * we need this because we can't know if a file is stored local or not from |
|
231 | - * outside the filestorage and for some purposes a local file is needed |
|
232 | - * |
|
233 | - * @param string $path |
|
234 | - * @return string |
|
235 | - */ |
|
236 | - public function getLocalFile($path) { |
|
237 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
238 | - $path = $this->getAbsolutePath($path); |
|
239 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
240 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
241 | - return $storage->getLocalFile($internalPath); |
|
242 | - } else { |
|
243 | - return null; |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @param string $path |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - public function getLocalFolder($path) { |
|
252 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
253 | - $path = $this->getAbsolutePath($path); |
|
254 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
255 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
256 | - return $storage->getLocalFolder($internalPath); |
|
257 | - } else { |
|
258 | - return null; |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * the following functions operate with arguments and return values identical |
|
264 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
265 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
266 | - */ |
|
267 | - public function mkdir($path) { |
|
268 | - return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * remove mount point |
|
273 | - * |
|
274 | - * @param \OC\Files\Mount\MoveableMount $mount |
|
275 | - * @param string $path relative to data/ |
|
276 | - * @return boolean |
|
277 | - */ |
|
278 | - protected function removeMount($mount, $path) { |
|
279 | - if ($mount instanceof MoveableMount) { |
|
280 | - // cut of /user/files to get the relative path to data/user/files |
|
281 | - $pathParts = explode('/', $path, 4); |
|
282 | - $relPath = '/' . $pathParts[3]; |
|
283 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
284 | - \OC_Hook::emit( |
|
285 | - Filesystem::CLASSNAME, "umount", |
|
286 | - array(Filesystem::signal_param_path => $relPath) |
|
287 | - ); |
|
288 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
289 | - $result = $mount->removeMount(); |
|
290 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
291 | - if ($result) { |
|
292 | - \OC_Hook::emit( |
|
293 | - Filesystem::CLASSNAME, "post_umount", |
|
294 | - array(Filesystem::signal_param_path => $relPath) |
|
295 | - ); |
|
296 | - } |
|
297 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
298 | - return $result; |
|
299 | - } else { |
|
300 | - // do not allow deleting the storage's root / the mount point |
|
301 | - // because for some storages it might delete the whole contents |
|
302 | - // but isn't supposed to work that way |
|
303 | - return false; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - public function disableCacheUpdate() { |
|
308 | - $this->updaterEnabled = false; |
|
309 | - } |
|
310 | - |
|
311 | - public function enableCacheUpdate() { |
|
312 | - $this->updaterEnabled = true; |
|
313 | - } |
|
314 | - |
|
315 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
316 | - if ($this->updaterEnabled) { |
|
317 | - if (is_null($time)) { |
|
318 | - $time = time(); |
|
319 | - } |
|
320 | - $storage->getUpdater()->update($internalPath, $time); |
|
321 | - } |
|
322 | - } |
|
323 | - |
|
324 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
325 | - if ($this->updaterEnabled) { |
|
326 | - $storage->getUpdater()->remove($internalPath); |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
331 | - if ($this->updaterEnabled) { |
|
332 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @param string $path |
|
338 | - * @return bool|mixed |
|
339 | - */ |
|
340 | - public function rmdir($path) { |
|
341 | - $absolutePath = $this->getAbsolutePath($path); |
|
342 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
343 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
344 | - return $this->removeMount($mount, $absolutePath); |
|
345 | - } |
|
346 | - if ($this->is_dir($path)) { |
|
347 | - $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
348 | - } else { |
|
349 | - $result = false; |
|
350 | - } |
|
351 | - |
|
352 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
353 | - $storage = $mount->getStorage(); |
|
354 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
355 | - $storage->getUpdater()->remove($internalPath); |
|
356 | - } |
|
357 | - return $result; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * @param string $path |
|
362 | - * @return resource |
|
363 | - */ |
|
364 | - public function opendir($path) { |
|
365 | - return $this->basicOperation('opendir', $path, array('read')); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * @param string $path |
|
370 | - * @return bool|mixed |
|
371 | - */ |
|
372 | - public function is_dir($path) { |
|
373 | - if ($path == '/') { |
|
374 | - return true; |
|
375 | - } |
|
376 | - return $this->basicOperation('is_dir', $path); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * @param string $path |
|
381 | - * @return bool|mixed |
|
382 | - */ |
|
383 | - public function is_file($path) { |
|
384 | - if ($path == '/') { |
|
385 | - return false; |
|
386 | - } |
|
387 | - return $this->basicOperation('is_file', $path); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * @param string $path |
|
392 | - * @return mixed |
|
393 | - */ |
|
394 | - public function stat($path) { |
|
395 | - return $this->basicOperation('stat', $path); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param string $path |
|
400 | - * @return mixed |
|
401 | - */ |
|
402 | - public function filetype($path) { |
|
403 | - return $this->basicOperation('filetype', $path); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * @param string $path |
|
408 | - * @return mixed |
|
409 | - */ |
|
410 | - public function filesize($path) { |
|
411 | - return $this->basicOperation('filesize', $path); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * @param string $path |
|
416 | - * @return bool|mixed |
|
417 | - * @throws \OCP\Files\InvalidPathException |
|
418 | - */ |
|
419 | - public function readfile($path) { |
|
420 | - $this->assertPathLength($path); |
|
421 | - @ob_end_clean(); |
|
422 | - $handle = $this->fopen($path, 'rb'); |
|
423 | - if ($handle) { |
|
424 | - $chunkSize = 8192; // 8 kB chunks |
|
425 | - while (!feof($handle)) { |
|
426 | - echo fread($handle, $chunkSize); |
|
427 | - flush(); |
|
428 | - } |
|
429 | - fclose($handle); |
|
430 | - return $this->filesize($path); |
|
431 | - } |
|
432 | - return false; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * @param string $path |
|
437 | - * @param int $from |
|
438 | - * @param int $to |
|
439 | - * @return bool|mixed |
|
440 | - * @throws \OCP\Files\InvalidPathException |
|
441 | - * @throws \OCP\Files\UnseekableException |
|
442 | - */ |
|
443 | - public function readfilePart($path, $from, $to) { |
|
444 | - $this->assertPathLength($path); |
|
445 | - @ob_end_clean(); |
|
446 | - $handle = $this->fopen($path, 'rb'); |
|
447 | - if ($handle) { |
|
448 | - $chunkSize = 8192; // 8 kB chunks |
|
449 | - $startReading = true; |
|
450 | - |
|
451 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
452 | - // forward file handle via chunked fread because fseek seem to have failed |
|
453 | - |
|
454 | - $end = $from + 1; |
|
455 | - while (!feof($handle) && ftell($handle) < $end) { |
|
456 | - $len = $from - ftell($handle); |
|
457 | - if ($len > $chunkSize) { |
|
458 | - $len = $chunkSize; |
|
459 | - } |
|
460 | - $result = fread($handle, $len); |
|
461 | - |
|
462 | - if ($result === false) { |
|
463 | - $startReading = false; |
|
464 | - break; |
|
465 | - } |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - if ($startReading) { |
|
470 | - $end = $to + 1; |
|
471 | - while (!feof($handle) && ftell($handle) < $end) { |
|
472 | - $len = $end - ftell($handle); |
|
473 | - if ($len > $chunkSize) { |
|
474 | - $len = $chunkSize; |
|
475 | - } |
|
476 | - echo fread($handle, $len); |
|
477 | - flush(); |
|
478 | - } |
|
479 | - return ftell($handle) - $from; |
|
480 | - } |
|
481 | - |
|
482 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
483 | - } |
|
484 | - return false; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * @param string $path |
|
489 | - * @return mixed |
|
490 | - */ |
|
491 | - public function isCreatable($path) { |
|
492 | - return $this->basicOperation('isCreatable', $path); |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * @param string $path |
|
497 | - * @return mixed |
|
498 | - */ |
|
499 | - public function isReadable($path) { |
|
500 | - return $this->basicOperation('isReadable', $path); |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * @param string $path |
|
505 | - * @return mixed |
|
506 | - */ |
|
507 | - public function isUpdatable($path) { |
|
508 | - return $this->basicOperation('isUpdatable', $path); |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * @param string $path |
|
513 | - * @return bool|mixed |
|
514 | - */ |
|
515 | - public function isDeletable($path) { |
|
516 | - $absolutePath = $this->getAbsolutePath($path); |
|
517 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
518 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
519 | - return $mount instanceof MoveableMount; |
|
520 | - } |
|
521 | - return $this->basicOperation('isDeletable', $path); |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * @param string $path |
|
526 | - * @return mixed |
|
527 | - */ |
|
528 | - public function isSharable($path) { |
|
529 | - return $this->basicOperation('isSharable', $path); |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * @param string $path |
|
534 | - * @return bool|mixed |
|
535 | - */ |
|
536 | - public function file_exists($path) { |
|
537 | - if ($path == '/') { |
|
538 | - return true; |
|
539 | - } |
|
540 | - return $this->basicOperation('file_exists', $path); |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * @param string $path |
|
545 | - * @return mixed |
|
546 | - */ |
|
547 | - public function filemtime($path) { |
|
548 | - return $this->basicOperation('filemtime', $path); |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * @param string $path |
|
553 | - * @param int|string $mtime |
|
554 | - * @return bool |
|
555 | - */ |
|
556 | - public function touch($path, $mtime = null) { |
|
557 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
558 | - $mtime = strtotime($mtime); |
|
559 | - } |
|
560 | - |
|
561 | - $hooks = array('touch'); |
|
562 | - |
|
563 | - if (!$this->file_exists($path)) { |
|
564 | - $hooks[] = 'create'; |
|
565 | - $hooks[] = 'write'; |
|
566 | - } |
|
567 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
568 | - if (!$result) { |
|
569 | - // If create file fails because of permissions on external storage like SMB folders, |
|
570 | - // check file exists and return false if not. |
|
571 | - if (!$this->file_exists($path)) { |
|
572 | - return false; |
|
573 | - } |
|
574 | - if (is_null($mtime)) { |
|
575 | - $mtime = time(); |
|
576 | - } |
|
577 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
578 | - $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
579 | - } |
|
580 | - return true; |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * @param string $path |
|
585 | - * @return mixed |
|
586 | - */ |
|
587 | - public function file_get_contents($path) { |
|
588 | - return $this->basicOperation('file_get_contents', $path, array('read')); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * @param bool $exists |
|
593 | - * @param string $path |
|
594 | - * @param bool $run |
|
595 | - */ |
|
596 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
597 | - if (!$exists) { |
|
598 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
599 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
600 | - Filesystem::signal_param_run => &$run, |
|
601 | - )); |
|
602 | - } else { |
|
603 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
604 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
605 | - Filesystem::signal_param_run => &$run, |
|
606 | - )); |
|
607 | - } |
|
608 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
609 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
610 | - Filesystem::signal_param_run => &$run, |
|
611 | - )); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * @param bool $exists |
|
616 | - * @param string $path |
|
617 | - */ |
|
618 | - protected function emit_file_hooks_post($exists, $path) { |
|
619 | - if (!$exists) { |
|
620 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
621 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
622 | - )); |
|
623 | - } else { |
|
624 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
625 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
626 | - )); |
|
627 | - } |
|
628 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
629 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
630 | - )); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * @param string $path |
|
635 | - * @param mixed $data |
|
636 | - * @return bool|mixed |
|
637 | - * @throws \Exception |
|
638 | - */ |
|
639 | - public function file_put_contents($path, $data) { |
|
640 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
641 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
642 | - if (Filesystem::isValidPath($path) |
|
643 | - and !Filesystem::isFileBlacklisted($path) |
|
644 | - ) { |
|
645 | - $path = $this->getRelativePath($absolutePath); |
|
646 | - |
|
647 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
648 | - |
|
649 | - $exists = $this->file_exists($path); |
|
650 | - $run = true; |
|
651 | - if ($this->shouldEmitHooks($path)) { |
|
652 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
653 | - } |
|
654 | - if (!$run) { |
|
655 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
656 | - return false; |
|
657 | - } |
|
658 | - |
|
659 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
660 | - |
|
661 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
662 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
663 | - $target = $storage->fopen($internalPath, 'w'); |
|
664 | - if ($target) { |
|
665 | - list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
666 | - fclose($target); |
|
667 | - fclose($data); |
|
668 | - |
|
669 | - $this->writeUpdate($storage, $internalPath); |
|
670 | - |
|
671 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
672 | - |
|
673 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
674 | - $this->emit_file_hooks_post($exists, $path); |
|
675 | - } |
|
676 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
677 | - return $result; |
|
678 | - } else { |
|
679 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
680 | - return false; |
|
681 | - } |
|
682 | - } else { |
|
683 | - return false; |
|
684 | - } |
|
685 | - } else { |
|
686 | - $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
687 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
688 | - } |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * @param string $path |
|
693 | - * @return bool|mixed |
|
694 | - */ |
|
695 | - public function unlink($path) { |
|
696 | - if ($path === '' || $path === '/') { |
|
697 | - // do not allow deleting the root |
|
698 | - return false; |
|
699 | - } |
|
700 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
701 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
702 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
703 | - if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
704 | - return $this->removeMount($mount, $absolutePath); |
|
705 | - } |
|
706 | - if ($this->is_dir($path)) { |
|
707 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
708 | - } else { |
|
709 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
710 | - } |
|
711 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
712 | - $storage = $mount->getStorage(); |
|
713 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
714 | - $storage->getUpdater()->remove($internalPath); |
|
715 | - return true; |
|
716 | - } else { |
|
717 | - return $result; |
|
718 | - } |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * @param string $directory |
|
723 | - * @return bool|mixed |
|
724 | - */ |
|
725 | - public function deleteAll($directory) { |
|
726 | - return $this->rmdir($directory); |
|
727 | - } |
|
728 | - |
|
729 | - /** |
|
730 | - * Rename/move a file or folder from the source path to target path. |
|
731 | - * |
|
732 | - * @param string $path1 source path |
|
733 | - * @param string $path2 target path |
|
734 | - * |
|
735 | - * @return bool|mixed |
|
736 | - */ |
|
737 | - public function rename($path1, $path2) { |
|
738 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
739 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
740 | - $result = false; |
|
741 | - if ( |
|
742 | - Filesystem::isValidPath($path2) |
|
743 | - and Filesystem::isValidPath($path1) |
|
744 | - and !Filesystem::isFileBlacklisted($path2) |
|
745 | - ) { |
|
746 | - $path1 = $this->getRelativePath($absolutePath1); |
|
747 | - $path2 = $this->getRelativePath($absolutePath2); |
|
748 | - $exists = $this->file_exists($path2); |
|
749 | - |
|
750 | - if ($path1 == null or $path2 == null) { |
|
751 | - return false; |
|
752 | - } |
|
753 | - |
|
754 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
755 | - try { |
|
756 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
757 | - |
|
758 | - $run = true; |
|
759 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
760 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
761 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
762 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
763 | - \OC_Hook::emit( |
|
764 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
765 | - array( |
|
766 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
767 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
768 | - Filesystem::signal_param_run => &$run |
|
769 | - ) |
|
770 | - ); |
|
771 | - } |
|
772 | - if ($run) { |
|
773 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
774 | - |
|
775 | - $manager = Filesystem::getMountManager(); |
|
776 | - $mount1 = $this->getMount($path1); |
|
777 | - $mount2 = $this->getMount($path2); |
|
778 | - $storage1 = $mount1->getStorage(); |
|
779 | - $storage2 = $mount2->getStorage(); |
|
780 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
781 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
782 | - |
|
783 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
784 | - try { |
|
785 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
786 | - |
|
787 | - if ($internalPath1 === '') { |
|
788 | - if ($mount1 instanceof MoveableMount) { |
|
789 | - if ($this->isTargetAllowed($absolutePath2)) { |
|
790 | - /** |
|
791 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
792 | - */ |
|
793 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
794 | - $result = $mount1->moveMount($absolutePath2); |
|
795 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
796 | - } else { |
|
797 | - $result = false; |
|
798 | - } |
|
799 | - } else { |
|
800 | - $result = false; |
|
801 | - } |
|
802 | - // moving a file/folder within the same mount point |
|
803 | - } elseif ($storage1 === $storage2) { |
|
804 | - if ($storage1) { |
|
805 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
806 | - } else { |
|
807 | - $result = false; |
|
808 | - } |
|
809 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
810 | - } else { |
|
811 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
812 | - } |
|
813 | - |
|
814 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
815 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
816 | - $this->writeUpdate($storage2, $internalPath2); |
|
817 | - } else if ($result) { |
|
818 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
819 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
820 | - } |
|
821 | - } |
|
822 | - } catch(\Exception $e) { |
|
823 | - throw $e; |
|
824 | - } finally { |
|
825 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
826 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
827 | - } |
|
828 | - |
|
829 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
830 | - if ($this->shouldEmitHooks()) { |
|
831 | - $this->emit_file_hooks_post($exists, $path2); |
|
832 | - } |
|
833 | - } elseif ($result) { |
|
834 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
835 | - \OC_Hook::emit( |
|
836 | - Filesystem::CLASSNAME, |
|
837 | - Filesystem::signal_post_rename, |
|
838 | - array( |
|
839 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
840 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
841 | - ) |
|
842 | - ); |
|
843 | - } |
|
844 | - } |
|
845 | - } |
|
846 | - } catch(\Exception $e) { |
|
847 | - throw $e; |
|
848 | - } finally { |
|
849 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
850 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
851 | - } |
|
852 | - } |
|
853 | - return $result; |
|
854 | - } |
|
855 | - |
|
856 | - /** |
|
857 | - * Copy a file/folder from the source path to target path |
|
858 | - * |
|
859 | - * @param string $path1 source path |
|
860 | - * @param string $path2 target path |
|
861 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
862 | - * |
|
863 | - * @return bool|mixed |
|
864 | - */ |
|
865 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
866 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
867 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
868 | - $result = false; |
|
869 | - if ( |
|
870 | - Filesystem::isValidPath($path2) |
|
871 | - and Filesystem::isValidPath($path1) |
|
872 | - and !Filesystem::isFileBlacklisted($path2) |
|
873 | - ) { |
|
874 | - $path1 = $this->getRelativePath($absolutePath1); |
|
875 | - $path2 = $this->getRelativePath($absolutePath2); |
|
876 | - |
|
877 | - if ($path1 == null or $path2 == null) { |
|
878 | - return false; |
|
879 | - } |
|
880 | - $run = true; |
|
881 | - |
|
882 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
883 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
884 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
885 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
886 | - |
|
887 | - try { |
|
888 | - |
|
889 | - $exists = $this->file_exists($path2); |
|
890 | - if ($this->shouldEmitHooks()) { |
|
891 | - \OC_Hook::emit( |
|
892 | - Filesystem::CLASSNAME, |
|
893 | - Filesystem::signal_copy, |
|
894 | - array( |
|
895 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
896 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
897 | - Filesystem::signal_param_run => &$run |
|
898 | - ) |
|
899 | - ); |
|
900 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
901 | - } |
|
902 | - if ($run) { |
|
903 | - $mount1 = $this->getMount($path1); |
|
904 | - $mount2 = $this->getMount($path2); |
|
905 | - $storage1 = $mount1->getStorage(); |
|
906 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
907 | - $storage2 = $mount2->getStorage(); |
|
908 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
909 | - |
|
910 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
911 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
912 | - |
|
913 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
914 | - if ($storage1) { |
|
915 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
916 | - } else { |
|
917 | - $result = false; |
|
918 | - } |
|
919 | - } else { |
|
920 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
921 | - } |
|
922 | - |
|
923 | - $this->writeUpdate($storage2, $internalPath2); |
|
924 | - |
|
925 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
926 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
927 | - |
|
928 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
929 | - \OC_Hook::emit( |
|
930 | - Filesystem::CLASSNAME, |
|
931 | - Filesystem::signal_post_copy, |
|
932 | - array( |
|
933 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
934 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
935 | - ) |
|
936 | - ); |
|
937 | - $this->emit_file_hooks_post($exists, $path2); |
|
938 | - } |
|
939 | - |
|
940 | - } |
|
941 | - } catch (\Exception $e) { |
|
942 | - $this->unlockFile($path2, $lockTypePath2); |
|
943 | - $this->unlockFile($path1, $lockTypePath1); |
|
944 | - throw $e; |
|
945 | - } |
|
946 | - |
|
947 | - $this->unlockFile($path2, $lockTypePath2); |
|
948 | - $this->unlockFile($path1, $lockTypePath1); |
|
949 | - |
|
950 | - } |
|
951 | - return $result; |
|
952 | - } |
|
953 | - |
|
954 | - /** |
|
955 | - * @param string $path |
|
956 | - * @param string $mode 'r' or 'w' |
|
957 | - * @return resource |
|
958 | - */ |
|
959 | - public function fopen($path, $mode) { |
|
960 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
961 | - $hooks = array(); |
|
962 | - switch ($mode) { |
|
963 | - case 'r': |
|
964 | - $hooks[] = 'read'; |
|
965 | - break; |
|
966 | - case 'r+': |
|
967 | - case 'w+': |
|
968 | - case 'x+': |
|
969 | - case 'a+': |
|
970 | - $hooks[] = 'read'; |
|
971 | - $hooks[] = 'write'; |
|
972 | - break; |
|
973 | - case 'w': |
|
974 | - case 'x': |
|
975 | - case 'a': |
|
976 | - $hooks[] = 'write'; |
|
977 | - break; |
|
978 | - default: |
|
979 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
980 | - } |
|
981 | - |
|
982 | - if ($mode !== 'r' && $mode !== 'w') { |
|
983 | - \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
984 | - } |
|
985 | - |
|
986 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
987 | - } |
|
988 | - |
|
989 | - /** |
|
990 | - * @param string $path |
|
991 | - * @return bool|string |
|
992 | - * @throws \OCP\Files\InvalidPathException |
|
993 | - */ |
|
994 | - public function toTmpFile($path) { |
|
995 | - $this->assertPathLength($path); |
|
996 | - if (Filesystem::isValidPath($path)) { |
|
997 | - $source = $this->fopen($path, 'r'); |
|
998 | - if ($source) { |
|
999 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1000 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1001 | - file_put_contents($tmpFile, $source); |
|
1002 | - return $tmpFile; |
|
1003 | - } else { |
|
1004 | - return false; |
|
1005 | - } |
|
1006 | - } else { |
|
1007 | - return false; |
|
1008 | - } |
|
1009 | - } |
|
1010 | - |
|
1011 | - /** |
|
1012 | - * @param string $tmpFile |
|
1013 | - * @param string $path |
|
1014 | - * @return bool|mixed |
|
1015 | - * @throws \OCP\Files\InvalidPathException |
|
1016 | - */ |
|
1017 | - public function fromTmpFile($tmpFile, $path) { |
|
1018 | - $this->assertPathLength($path); |
|
1019 | - if (Filesystem::isValidPath($path)) { |
|
1020 | - |
|
1021 | - // Get directory that the file is going into |
|
1022 | - $filePath = dirname($path); |
|
1023 | - |
|
1024 | - // Create the directories if any |
|
1025 | - if (!$this->file_exists($filePath)) { |
|
1026 | - $result = $this->createParentDirectories($filePath); |
|
1027 | - if ($result === false) { |
|
1028 | - return false; |
|
1029 | - } |
|
1030 | - } |
|
1031 | - |
|
1032 | - $source = fopen($tmpFile, 'r'); |
|
1033 | - if ($source) { |
|
1034 | - $result = $this->file_put_contents($path, $source); |
|
1035 | - // $this->file_put_contents() might have already closed |
|
1036 | - // the resource, so we check it, before trying to close it |
|
1037 | - // to avoid messages in the error log. |
|
1038 | - if (is_resource($source)) { |
|
1039 | - fclose($source); |
|
1040 | - } |
|
1041 | - unlink($tmpFile); |
|
1042 | - return $result; |
|
1043 | - } else { |
|
1044 | - return false; |
|
1045 | - } |
|
1046 | - } else { |
|
1047 | - return false; |
|
1048 | - } |
|
1049 | - } |
|
1050 | - |
|
1051 | - |
|
1052 | - /** |
|
1053 | - * @param string $path |
|
1054 | - * @return mixed |
|
1055 | - * @throws \OCP\Files\InvalidPathException |
|
1056 | - */ |
|
1057 | - public function getMimeType($path) { |
|
1058 | - $this->assertPathLength($path); |
|
1059 | - return $this->basicOperation('getMimeType', $path); |
|
1060 | - } |
|
1061 | - |
|
1062 | - /** |
|
1063 | - * @param string $type |
|
1064 | - * @param string $path |
|
1065 | - * @param bool $raw |
|
1066 | - * @return bool|null|string |
|
1067 | - */ |
|
1068 | - public function hash($type, $path, $raw = false) { |
|
1069 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1070 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1071 | - if (Filesystem::isValidPath($path)) { |
|
1072 | - $path = $this->getRelativePath($absolutePath); |
|
1073 | - if ($path == null) { |
|
1074 | - return false; |
|
1075 | - } |
|
1076 | - if ($this->shouldEmitHooks($path)) { |
|
1077 | - \OC_Hook::emit( |
|
1078 | - Filesystem::CLASSNAME, |
|
1079 | - Filesystem::signal_read, |
|
1080 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
1081 | - ); |
|
1082 | - } |
|
1083 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1084 | - if ($storage) { |
|
1085 | - return $storage->hash($type, $internalPath, $raw); |
|
1086 | - } |
|
1087 | - } |
|
1088 | - return null; |
|
1089 | - } |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * @param string $path |
|
1093 | - * @return mixed |
|
1094 | - * @throws \OCP\Files\InvalidPathException |
|
1095 | - */ |
|
1096 | - public function free_space($path = '/') { |
|
1097 | - $this->assertPathLength($path); |
|
1098 | - $result = $this->basicOperation('free_space', $path); |
|
1099 | - if ($result === null) { |
|
1100 | - throw new InvalidPathException(); |
|
1101 | - } |
|
1102 | - return $result; |
|
1103 | - } |
|
1104 | - |
|
1105 | - /** |
|
1106 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1107 | - * |
|
1108 | - * @param string $operation |
|
1109 | - * @param string $path |
|
1110 | - * @param array $hooks (optional) |
|
1111 | - * @param mixed $extraParam (optional) |
|
1112 | - * @return mixed |
|
1113 | - * @throws \Exception |
|
1114 | - * |
|
1115 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1116 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1117 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1118 | - */ |
|
1119 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1120 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1121 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1122 | - if (Filesystem::isValidPath($path) |
|
1123 | - and !Filesystem::isFileBlacklisted($path) |
|
1124 | - ) { |
|
1125 | - $path = $this->getRelativePath($absolutePath); |
|
1126 | - if ($path == null) { |
|
1127 | - return false; |
|
1128 | - } |
|
1129 | - |
|
1130 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1131 | - // always a shared lock during pre-hooks so the hook can read the file |
|
1132 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1133 | - } |
|
1134 | - |
|
1135 | - $run = $this->runHooks($hooks, $path); |
|
1136 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
1137 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1138 | - if ($run and $storage) { |
|
1139 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1140 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1141 | - } |
|
1142 | - try { |
|
1143 | - if (!is_null($extraParam)) { |
|
1144 | - $result = $storage->$operation($internalPath, $extraParam); |
|
1145 | - } else { |
|
1146 | - $result = $storage->$operation($internalPath); |
|
1147 | - } |
|
1148 | - } catch (\Exception $e) { |
|
1149 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1150 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1151 | - } else if (in_array('read', $hooks)) { |
|
1152 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1153 | - } |
|
1154 | - throw $e; |
|
1155 | - } |
|
1156 | - |
|
1157 | - if ($result && in_array('delete', $hooks) and $result) { |
|
1158 | - $this->removeUpdate($storage, $internalPath); |
|
1159 | - } |
|
1160 | - if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
1161 | - $this->writeUpdate($storage, $internalPath); |
|
1162 | - } |
|
1163 | - if ($result && in_array('touch', $hooks)) { |
|
1164 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1165 | - } |
|
1166 | - |
|
1167 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1168 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1169 | - } |
|
1170 | - |
|
1171 | - $unlockLater = false; |
|
1172 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1173 | - $unlockLater = true; |
|
1174 | - // make sure our unlocking callback will still be called if connection is aborted |
|
1175 | - ignore_user_abort(true); |
|
1176 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1177 | - if (in_array('write', $hooks)) { |
|
1178 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1179 | - } else if (in_array('read', $hooks)) { |
|
1180 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1181 | - } |
|
1182 | - }); |
|
1183 | - } |
|
1184 | - |
|
1185 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1186 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1187 | - $this->runHooks($hooks, $path, true); |
|
1188 | - } |
|
1189 | - } |
|
1190 | - |
|
1191 | - if (!$unlockLater |
|
1192 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1193 | - ) { |
|
1194 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1195 | - } |
|
1196 | - return $result; |
|
1197 | - } else { |
|
1198 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1199 | - } |
|
1200 | - } |
|
1201 | - return null; |
|
1202 | - } |
|
1203 | - |
|
1204 | - /** |
|
1205 | - * get the path relative to the default root for hook usage |
|
1206 | - * |
|
1207 | - * @param string $path |
|
1208 | - * @return string |
|
1209 | - */ |
|
1210 | - private function getHookPath($path) { |
|
1211 | - if (!Filesystem::getView()) { |
|
1212 | - return $path; |
|
1213 | - } |
|
1214 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1215 | - } |
|
1216 | - |
|
1217 | - private function shouldEmitHooks($path = '') { |
|
1218 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1219 | - return false; |
|
1220 | - } |
|
1221 | - if (!Filesystem::$loaded) { |
|
1222 | - return false; |
|
1223 | - } |
|
1224 | - $defaultRoot = Filesystem::getRoot(); |
|
1225 | - if ($defaultRoot === null) { |
|
1226 | - return false; |
|
1227 | - } |
|
1228 | - if ($this->fakeRoot === $defaultRoot) { |
|
1229 | - return true; |
|
1230 | - } |
|
1231 | - $fullPath = $this->getAbsolutePath($path); |
|
1232 | - |
|
1233 | - if ($fullPath === $defaultRoot) { |
|
1234 | - return true; |
|
1235 | - } |
|
1236 | - |
|
1237 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1238 | - } |
|
1239 | - |
|
1240 | - /** |
|
1241 | - * @param string[] $hooks |
|
1242 | - * @param string $path |
|
1243 | - * @param bool $post |
|
1244 | - * @return bool |
|
1245 | - */ |
|
1246 | - private function runHooks($hooks, $path, $post = false) { |
|
1247 | - $relativePath = $path; |
|
1248 | - $path = $this->getHookPath($path); |
|
1249 | - $prefix = $post ? 'post_' : ''; |
|
1250 | - $run = true; |
|
1251 | - if ($this->shouldEmitHooks($relativePath)) { |
|
1252 | - foreach ($hooks as $hook) { |
|
1253 | - if ($hook != 'read') { |
|
1254 | - \OC_Hook::emit( |
|
1255 | - Filesystem::CLASSNAME, |
|
1256 | - $prefix . $hook, |
|
1257 | - array( |
|
1258 | - Filesystem::signal_param_run => &$run, |
|
1259 | - Filesystem::signal_param_path => $path |
|
1260 | - ) |
|
1261 | - ); |
|
1262 | - } elseif (!$post) { |
|
1263 | - \OC_Hook::emit( |
|
1264 | - Filesystem::CLASSNAME, |
|
1265 | - $prefix . $hook, |
|
1266 | - array( |
|
1267 | - Filesystem::signal_param_path => $path |
|
1268 | - ) |
|
1269 | - ); |
|
1270 | - } |
|
1271 | - } |
|
1272 | - } |
|
1273 | - return $run; |
|
1274 | - } |
|
1275 | - |
|
1276 | - /** |
|
1277 | - * check if a file or folder has been updated since $time |
|
1278 | - * |
|
1279 | - * @param string $path |
|
1280 | - * @param int $time |
|
1281 | - * @return bool |
|
1282 | - */ |
|
1283 | - public function hasUpdated($path, $time) { |
|
1284 | - return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
1285 | - } |
|
1286 | - |
|
1287 | - /** |
|
1288 | - * @param string $ownerId |
|
1289 | - * @return \OC\User\User |
|
1290 | - */ |
|
1291 | - private function getUserObjectForOwner($ownerId) { |
|
1292 | - $owner = $this->userManager->get($ownerId); |
|
1293 | - if ($owner instanceof IUser) { |
|
1294 | - return $owner; |
|
1295 | - } else { |
|
1296 | - return new User($ownerId, null); |
|
1297 | - } |
|
1298 | - } |
|
1299 | - |
|
1300 | - /** |
|
1301 | - * Get file info from cache |
|
1302 | - * |
|
1303 | - * If the file is not in cached it will be scanned |
|
1304 | - * If the file has changed on storage the cache will be updated |
|
1305 | - * |
|
1306 | - * @param \OC\Files\Storage\Storage $storage |
|
1307 | - * @param string $internalPath |
|
1308 | - * @param string $relativePath |
|
1309 | - * @return ICacheEntry|bool |
|
1310 | - */ |
|
1311 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1312 | - $cache = $storage->getCache($internalPath); |
|
1313 | - $data = $cache->get($internalPath); |
|
1314 | - $watcher = $storage->getWatcher($internalPath); |
|
1315 | - |
|
1316 | - try { |
|
1317 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1318 | - if (!$data || $data['size'] === -1) { |
|
1319 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1320 | - if (!$storage->file_exists($internalPath)) { |
|
1321 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1322 | - return false; |
|
1323 | - } |
|
1324 | - $scanner = $storage->getScanner($internalPath); |
|
1325 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1326 | - $data = $cache->get($internalPath); |
|
1327 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1328 | - } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1329 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1330 | - $watcher->update($internalPath, $data); |
|
1331 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1332 | - $data = $cache->get($internalPath); |
|
1333 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1334 | - } |
|
1335 | - } catch (LockedException $e) { |
|
1336 | - // if the file is locked we just use the old cache info |
|
1337 | - } |
|
1338 | - |
|
1339 | - return $data; |
|
1340 | - } |
|
1341 | - |
|
1342 | - /** |
|
1343 | - * get the filesystem info |
|
1344 | - * |
|
1345 | - * @param string $path |
|
1346 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
1347 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1348 | - * defaults to true |
|
1349 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
1350 | - */ |
|
1351 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
1352 | - $this->assertPathLength($path); |
|
1353 | - if (!Filesystem::isValidPath($path)) { |
|
1354 | - return false; |
|
1355 | - } |
|
1356 | - if (Cache\Scanner::isPartialFile($path)) { |
|
1357 | - return $this->getPartFileInfo($path); |
|
1358 | - } |
|
1359 | - $relativePath = $path; |
|
1360 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1361 | - |
|
1362 | - $mount = Filesystem::getMountManager()->find($path); |
|
1363 | - if (!$mount) { |
|
1364 | - return false; |
|
1365 | - } |
|
1366 | - $storage = $mount->getStorage(); |
|
1367 | - $internalPath = $mount->getInternalPath($path); |
|
1368 | - if ($storage) { |
|
1369 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1370 | - |
|
1371 | - if (!$data instanceof ICacheEntry) { |
|
1372 | - return false; |
|
1373 | - } |
|
1374 | - |
|
1375 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1376 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1377 | - } |
|
1378 | - |
|
1379 | - $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
1380 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1381 | - |
|
1382 | - if ($data and isset($data['fileid'])) { |
|
1383 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1384 | - //add the sizes of other mount points to the folder |
|
1385 | - $extOnly = ($includeMountPoints === 'ext'); |
|
1386 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1387 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1388 | - $subStorage = $mount->getStorage(); |
|
1389 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1390 | - })); |
|
1391 | - } |
|
1392 | - } |
|
1393 | - |
|
1394 | - return $info; |
|
1395 | - } |
|
1396 | - |
|
1397 | - return false; |
|
1398 | - } |
|
1399 | - |
|
1400 | - /** |
|
1401 | - * get the content of a directory |
|
1402 | - * |
|
1403 | - * @param string $directory path under datadirectory |
|
1404 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1405 | - * @return FileInfo[] |
|
1406 | - */ |
|
1407 | - public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
1408 | - $this->assertPathLength($directory); |
|
1409 | - if (!Filesystem::isValidPath($directory)) { |
|
1410 | - return []; |
|
1411 | - } |
|
1412 | - $path = $this->getAbsolutePath($directory); |
|
1413 | - $path = Filesystem::normalizePath($path); |
|
1414 | - $mount = $this->getMount($directory); |
|
1415 | - if (!$mount) { |
|
1416 | - return []; |
|
1417 | - } |
|
1418 | - $storage = $mount->getStorage(); |
|
1419 | - $internalPath = $mount->getInternalPath($path); |
|
1420 | - if ($storage) { |
|
1421 | - $cache = $storage->getCache($internalPath); |
|
1422 | - $user = \OC_User::getUser(); |
|
1423 | - |
|
1424 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1425 | - |
|
1426 | - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
1427 | - return []; |
|
1428 | - } |
|
1429 | - |
|
1430 | - $folderId = $data['fileid']; |
|
1431 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1432 | - |
|
1433 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1434 | - /** |
|
1435 | - * @var \OC\Files\FileInfo[] $files |
|
1436 | - */ |
|
1437 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1438 | - if ($sharingDisabled) { |
|
1439 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1440 | - } |
|
1441 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1442 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1443 | - }, $contents); |
|
1444 | - |
|
1445 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1446 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1447 | - $dirLength = strlen($path); |
|
1448 | - foreach ($mounts as $mount) { |
|
1449 | - $mountPoint = $mount->getMountPoint(); |
|
1450 | - $subStorage = $mount->getStorage(); |
|
1451 | - if ($subStorage) { |
|
1452 | - $subCache = $subStorage->getCache(''); |
|
1453 | - |
|
1454 | - $rootEntry = $subCache->get(''); |
|
1455 | - if (!$rootEntry) { |
|
1456 | - $subScanner = $subStorage->getScanner(''); |
|
1457 | - try { |
|
1458 | - $subScanner->scanFile(''); |
|
1459 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1460 | - continue; |
|
1461 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
1462 | - continue; |
|
1463 | - } catch (\Exception $e) { |
|
1464 | - // sometimes when the storage is not available it can be any exception |
|
1465 | - \OC::$server->getLogger()->logException($e, [ |
|
1466 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1467 | - 'level' => ILogger::ERROR, |
|
1468 | - 'app' => 'lib', |
|
1469 | - ]); |
|
1470 | - continue; |
|
1471 | - } |
|
1472 | - $rootEntry = $subCache->get(''); |
|
1473 | - } |
|
1474 | - |
|
1475 | - if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
1476 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1477 | - if ($pos = strpos($relativePath, '/')) { |
|
1478 | - //mountpoint inside subfolder add size to the correct folder |
|
1479 | - $entryName = substr($relativePath, 0, $pos); |
|
1480 | - foreach ($files as &$entry) { |
|
1481 | - if ($entry->getName() === $entryName) { |
|
1482 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
1483 | - } |
|
1484 | - } |
|
1485 | - } else { //mountpoint in this folder, add an entry for it |
|
1486 | - $rootEntry['name'] = $relativePath; |
|
1487 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1488 | - $permissions = $rootEntry['permissions']; |
|
1489 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1490 | - // for shared files/folders we use the permissions given by the owner |
|
1491 | - if ($mount instanceof MoveableMount) { |
|
1492 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1493 | - } else { |
|
1494 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1495 | - } |
|
1496 | - |
|
1497 | - //remove any existing entry with the same name |
|
1498 | - foreach ($files as $i => $file) { |
|
1499 | - if ($file['name'] === $rootEntry['name']) { |
|
1500 | - unset($files[$i]); |
|
1501 | - break; |
|
1502 | - } |
|
1503 | - } |
|
1504 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1505 | - |
|
1506 | - // if sharing was disabled for the user we remove the share permissions |
|
1507 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
1508 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1509 | - } |
|
1510 | - |
|
1511 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1512 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1513 | - } |
|
1514 | - } |
|
1515 | - } |
|
1516 | - } |
|
1517 | - |
|
1518 | - if ($mimetype_filter) { |
|
1519 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1520 | - if (strpos($mimetype_filter, '/')) { |
|
1521 | - return $file->getMimetype() === $mimetype_filter; |
|
1522 | - } else { |
|
1523 | - return $file->getMimePart() === $mimetype_filter; |
|
1524 | - } |
|
1525 | - }); |
|
1526 | - } |
|
1527 | - |
|
1528 | - return $files; |
|
1529 | - } else { |
|
1530 | - return []; |
|
1531 | - } |
|
1532 | - } |
|
1533 | - |
|
1534 | - /** |
|
1535 | - * change file metadata |
|
1536 | - * |
|
1537 | - * @param string $path |
|
1538 | - * @param array|\OCP\Files\FileInfo $data |
|
1539 | - * @return int |
|
1540 | - * |
|
1541 | - * returns the fileid of the updated file |
|
1542 | - */ |
|
1543 | - public function putFileInfo($path, $data) { |
|
1544 | - $this->assertPathLength($path); |
|
1545 | - if ($data instanceof FileInfo) { |
|
1546 | - $data = $data->getData(); |
|
1547 | - } |
|
1548 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1549 | - /** |
|
1550 | - * @var \OC\Files\Storage\Storage $storage |
|
1551 | - * @var string $internalPath |
|
1552 | - */ |
|
1553 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
1554 | - if ($storage) { |
|
1555 | - $cache = $storage->getCache($path); |
|
1556 | - |
|
1557 | - if (!$cache->inCache($internalPath)) { |
|
1558 | - $scanner = $storage->getScanner($internalPath); |
|
1559 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1560 | - } |
|
1561 | - |
|
1562 | - return $cache->put($internalPath, $data); |
|
1563 | - } else { |
|
1564 | - return -1; |
|
1565 | - } |
|
1566 | - } |
|
1567 | - |
|
1568 | - /** |
|
1569 | - * search for files with the name matching $query |
|
1570 | - * |
|
1571 | - * @param string $query |
|
1572 | - * @return FileInfo[] |
|
1573 | - */ |
|
1574 | - public function search($query) { |
|
1575 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
1576 | - } |
|
1577 | - |
|
1578 | - /** |
|
1579 | - * search for files with the name matching $query |
|
1580 | - * |
|
1581 | - * @param string $query |
|
1582 | - * @return FileInfo[] |
|
1583 | - */ |
|
1584 | - public function searchRaw($query) { |
|
1585 | - return $this->searchCommon('search', array($query)); |
|
1586 | - } |
|
1587 | - |
|
1588 | - /** |
|
1589 | - * search for files by mimetype |
|
1590 | - * |
|
1591 | - * @param string $mimetype |
|
1592 | - * @return FileInfo[] |
|
1593 | - */ |
|
1594 | - public function searchByMime($mimetype) { |
|
1595 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
1596 | - } |
|
1597 | - |
|
1598 | - /** |
|
1599 | - * search for files by tag |
|
1600 | - * |
|
1601 | - * @param string|int $tag name or tag id |
|
1602 | - * @param string $userId owner of the tags |
|
1603 | - * @return FileInfo[] |
|
1604 | - */ |
|
1605 | - public function searchByTag($tag, $userId) { |
|
1606 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
1607 | - } |
|
1608 | - |
|
1609 | - /** |
|
1610 | - * @param string $method cache method |
|
1611 | - * @param array $args |
|
1612 | - * @return FileInfo[] |
|
1613 | - */ |
|
1614 | - private function searchCommon($method, $args) { |
|
1615 | - $files = array(); |
|
1616 | - $rootLength = strlen($this->fakeRoot); |
|
1617 | - |
|
1618 | - $mount = $this->getMount(''); |
|
1619 | - $mountPoint = $mount->getMountPoint(); |
|
1620 | - $storage = $mount->getStorage(); |
|
1621 | - if ($storage) { |
|
1622 | - $cache = $storage->getCache(''); |
|
1623 | - |
|
1624 | - $results = call_user_func_array(array($cache, $method), $args); |
|
1625 | - foreach ($results as $result) { |
|
1626 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1627 | - $internalPath = $result['path']; |
|
1628 | - $path = $mountPoint . $result['path']; |
|
1629 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1630 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1631 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1632 | - } |
|
1633 | - } |
|
1634 | - |
|
1635 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1636 | - foreach ($mounts as $mount) { |
|
1637 | - $mountPoint = $mount->getMountPoint(); |
|
1638 | - $storage = $mount->getStorage(); |
|
1639 | - if ($storage) { |
|
1640 | - $cache = $storage->getCache(''); |
|
1641 | - |
|
1642 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1643 | - $results = call_user_func_array(array($cache, $method), $args); |
|
1644 | - if ($results) { |
|
1645 | - foreach ($results as $result) { |
|
1646 | - $internalPath = $result['path']; |
|
1647 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1648 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
1649 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1650 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1651 | - } |
|
1652 | - } |
|
1653 | - } |
|
1654 | - } |
|
1655 | - } |
|
1656 | - return $files; |
|
1657 | - } |
|
1658 | - |
|
1659 | - /** |
|
1660 | - * Get the owner for a file or folder |
|
1661 | - * |
|
1662 | - * @param string $path |
|
1663 | - * @return string the user id of the owner |
|
1664 | - * @throws NotFoundException |
|
1665 | - */ |
|
1666 | - public function getOwner($path) { |
|
1667 | - $info = $this->getFileInfo($path); |
|
1668 | - if (!$info) { |
|
1669 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1670 | - } |
|
1671 | - return $info->getOwner()->getUID(); |
|
1672 | - } |
|
1673 | - |
|
1674 | - /** |
|
1675 | - * get the ETag for a file or folder |
|
1676 | - * |
|
1677 | - * @param string $path |
|
1678 | - * @return string |
|
1679 | - */ |
|
1680 | - public function getETag($path) { |
|
1681 | - /** |
|
1682 | - * @var Storage\Storage $storage |
|
1683 | - * @var string $internalPath |
|
1684 | - */ |
|
1685 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
1686 | - if ($storage) { |
|
1687 | - return $storage->getETag($internalPath); |
|
1688 | - } else { |
|
1689 | - return null; |
|
1690 | - } |
|
1691 | - } |
|
1692 | - |
|
1693 | - /** |
|
1694 | - * Get the path of a file by id, relative to the view |
|
1695 | - * |
|
1696 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
1697 | - * |
|
1698 | - * @param int $id |
|
1699 | - * @throws NotFoundException |
|
1700 | - * @return string |
|
1701 | - */ |
|
1702 | - public function getPath($id) { |
|
1703 | - $id = (int)$id; |
|
1704 | - $manager = Filesystem::getMountManager(); |
|
1705 | - $mounts = $manager->findIn($this->fakeRoot); |
|
1706 | - $mounts[] = $manager->find($this->fakeRoot); |
|
1707 | - // reverse the array so we start with the storage this view is in |
|
1708 | - // which is the most likely to contain the file we're looking for |
|
1709 | - $mounts = array_reverse($mounts); |
|
1710 | - foreach ($mounts as $mount) { |
|
1711 | - /** |
|
1712 | - * @var \OC\Files\Mount\MountPoint $mount |
|
1713 | - */ |
|
1714 | - if ($mount->getStorage()) { |
|
1715 | - $cache = $mount->getStorage()->getCache(); |
|
1716 | - $internalPath = $cache->getPathById($id); |
|
1717 | - if (is_string($internalPath)) { |
|
1718 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
1719 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1720 | - return $path; |
|
1721 | - } |
|
1722 | - } |
|
1723 | - } |
|
1724 | - } |
|
1725 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1726 | - } |
|
1727 | - |
|
1728 | - /** |
|
1729 | - * @param string $path |
|
1730 | - * @throws InvalidPathException |
|
1731 | - */ |
|
1732 | - private function assertPathLength($path) { |
|
1733 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1734 | - // Check for the string length - performed using isset() instead of strlen() |
|
1735 | - // because isset() is about 5x-40x faster. |
|
1736 | - if (isset($path[$maxLen])) { |
|
1737 | - $pathLen = strlen($path); |
|
1738 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1739 | - } |
|
1740 | - } |
|
1741 | - |
|
1742 | - /** |
|
1743 | - * check if it is allowed to move a mount point to a given target. |
|
1744 | - * It is not allowed to move a mount point into a different mount point or |
|
1745 | - * into an already shared folder |
|
1746 | - * |
|
1747 | - * @param string $target path |
|
1748 | - * @return boolean |
|
1749 | - */ |
|
1750 | - private function isTargetAllowed($target) { |
|
1751 | - |
|
1752 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
1753 | - if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
1754 | - \OCP\Util::writeLog('files', |
|
1755 | - 'It is not allowed to move one mount point into another one', |
|
1756 | - ILogger::DEBUG); |
|
1757 | - return false; |
|
1758 | - } |
|
1759 | - |
|
1760 | - // note: cannot use the view because the target is already locked |
|
1761 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1762 | - if ($fileId === -1) { |
|
1763 | - // target might not exist, need to check parent instead |
|
1764 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1765 | - } |
|
1766 | - |
|
1767 | - // check if any of the parents were shared by the current owner (include collections) |
|
1768 | - $shares = \OCP\Share::getItemShared( |
|
1769 | - 'folder', |
|
1770 | - $fileId, |
|
1771 | - \OCP\Share::FORMAT_NONE, |
|
1772 | - null, |
|
1773 | - true |
|
1774 | - ); |
|
1775 | - |
|
1776 | - if (count($shares) > 0) { |
|
1777 | - \OCP\Util::writeLog('files', |
|
1778 | - 'It is not allowed to move one mount point into a shared folder', |
|
1779 | - ILogger::DEBUG); |
|
1780 | - return false; |
|
1781 | - } |
|
1782 | - |
|
1783 | - return true; |
|
1784 | - } |
|
1785 | - |
|
1786 | - /** |
|
1787 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1788 | - * |
|
1789 | - * @param string $path |
|
1790 | - * @return \OCP\Files\FileInfo |
|
1791 | - */ |
|
1792 | - private function getPartFileInfo($path) { |
|
1793 | - $mount = $this->getMount($path); |
|
1794 | - $storage = $mount->getStorage(); |
|
1795 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1796 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1797 | - return new FileInfo( |
|
1798 | - $this->getAbsolutePath($path), |
|
1799 | - $storage, |
|
1800 | - $internalPath, |
|
1801 | - [ |
|
1802 | - 'fileid' => null, |
|
1803 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
1804 | - 'name' => basename($path), |
|
1805 | - 'etag' => null, |
|
1806 | - 'size' => $storage->filesize($internalPath), |
|
1807 | - 'mtime' => $storage->filemtime($internalPath), |
|
1808 | - 'encrypted' => false, |
|
1809 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1810 | - ], |
|
1811 | - $mount, |
|
1812 | - $owner |
|
1813 | - ); |
|
1814 | - } |
|
1815 | - |
|
1816 | - /** |
|
1817 | - * @param string $path |
|
1818 | - * @param string $fileName |
|
1819 | - * @throws InvalidPathException |
|
1820 | - */ |
|
1821 | - public function verifyPath($path, $fileName) { |
|
1822 | - try { |
|
1823 | - /** @type \OCP\Files\Storage $storage */ |
|
1824 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
1825 | - $storage->verifyPath($internalPath, $fileName); |
|
1826 | - } catch (ReservedWordException $ex) { |
|
1827 | - $l = \OC::$server->getL10N('lib'); |
|
1828 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1829 | - } catch (InvalidCharacterInPathException $ex) { |
|
1830 | - $l = \OC::$server->getL10N('lib'); |
|
1831 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1832 | - } catch (FileNameTooLongException $ex) { |
|
1833 | - $l = \OC::$server->getL10N('lib'); |
|
1834 | - throw new InvalidPathException($l->t('File name is too long')); |
|
1835 | - } catch (InvalidDirectoryException $ex) { |
|
1836 | - $l = \OC::$server->getL10N('lib'); |
|
1837 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1838 | - } catch (EmptyFileNameException $ex) { |
|
1839 | - $l = \OC::$server->getL10N('lib'); |
|
1840 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1841 | - } |
|
1842 | - } |
|
1843 | - |
|
1844 | - /** |
|
1845 | - * get all parent folders of $path |
|
1846 | - * |
|
1847 | - * @param string $path |
|
1848 | - * @return string[] |
|
1849 | - */ |
|
1850 | - private function getParents($path) { |
|
1851 | - $path = trim($path, '/'); |
|
1852 | - if (!$path) { |
|
1853 | - return []; |
|
1854 | - } |
|
1855 | - |
|
1856 | - $parts = explode('/', $path); |
|
1857 | - |
|
1858 | - // remove the single file |
|
1859 | - array_pop($parts); |
|
1860 | - $result = array('/'); |
|
1861 | - $resultPath = ''; |
|
1862 | - foreach ($parts as $part) { |
|
1863 | - if ($part) { |
|
1864 | - $resultPath .= '/' . $part; |
|
1865 | - $result[] = $resultPath; |
|
1866 | - } |
|
1867 | - } |
|
1868 | - return $result; |
|
1869 | - } |
|
1870 | - |
|
1871 | - /** |
|
1872 | - * Returns the mount point for which to lock |
|
1873 | - * |
|
1874 | - * @param string $absolutePath absolute path |
|
1875 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
1876 | - * is mounted directly on the given path, false otherwise |
|
1877 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
1878 | - */ |
|
1879 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1880 | - $results = []; |
|
1881 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1882 | - if (!$mount) { |
|
1883 | - return $results; |
|
1884 | - } |
|
1885 | - |
|
1886 | - if ($useParentMount) { |
|
1887 | - // find out if something is mounted directly on the path |
|
1888 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
1889 | - if ($internalPath === '') { |
|
1890 | - // resolve the parent mount instead |
|
1891 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1892 | - } |
|
1893 | - } |
|
1894 | - |
|
1895 | - return $mount; |
|
1896 | - } |
|
1897 | - |
|
1898 | - /** |
|
1899 | - * Lock the given path |
|
1900 | - * |
|
1901 | - * @param string $path the path of the file to lock, relative to the view |
|
1902 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1903 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1904 | - * |
|
1905 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1906 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
1907 | - */ |
|
1908 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
1909 | - $absolutePath = $this->getAbsolutePath($path); |
|
1910 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1911 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1912 | - return false; |
|
1913 | - } |
|
1914 | - |
|
1915 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1916 | - if ($mount) { |
|
1917 | - try { |
|
1918 | - $storage = $mount->getStorage(); |
|
1919 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1920 | - $storage->acquireLock( |
|
1921 | - $mount->getInternalPath($absolutePath), |
|
1922 | - $type, |
|
1923 | - $this->lockingProvider |
|
1924 | - ); |
|
1925 | - } |
|
1926 | - } catch (\OCP\Lock\LockedException $e) { |
|
1927 | - // rethrow with the a human-readable path |
|
1928 | - throw new \OCP\Lock\LockedException( |
|
1929 | - $this->getPathRelativeToFiles($absolutePath), |
|
1930 | - $e |
|
1931 | - ); |
|
1932 | - } |
|
1933 | - } |
|
1934 | - |
|
1935 | - return true; |
|
1936 | - } |
|
1937 | - |
|
1938 | - /** |
|
1939 | - * Change the lock type |
|
1940 | - * |
|
1941 | - * @param string $path the path of the file to lock, relative to the view |
|
1942 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1943 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1944 | - * |
|
1945 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1946 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
1947 | - */ |
|
1948 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
1949 | - $path = Filesystem::normalizePath($path); |
|
1950 | - $absolutePath = $this->getAbsolutePath($path); |
|
1951 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1952 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1953 | - return false; |
|
1954 | - } |
|
1955 | - |
|
1956 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1957 | - if ($mount) { |
|
1958 | - try { |
|
1959 | - $storage = $mount->getStorage(); |
|
1960 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1961 | - $storage->changeLock( |
|
1962 | - $mount->getInternalPath($absolutePath), |
|
1963 | - $type, |
|
1964 | - $this->lockingProvider |
|
1965 | - ); |
|
1966 | - } |
|
1967 | - } catch (\OCP\Lock\LockedException $e) { |
|
1968 | - try { |
|
1969 | - // rethrow with the a human-readable path |
|
1970 | - throw new \OCP\Lock\LockedException( |
|
1971 | - $this->getPathRelativeToFiles($absolutePath), |
|
1972 | - $e |
|
1973 | - ); |
|
1974 | - } catch (\InvalidArgumentException $e) { |
|
1975 | - throw new \OCP\Lock\LockedException( |
|
1976 | - $absolutePath, |
|
1977 | - $e |
|
1978 | - ); |
|
1979 | - } |
|
1980 | - } |
|
1981 | - } |
|
1982 | - |
|
1983 | - return true; |
|
1984 | - } |
|
1985 | - |
|
1986 | - /** |
|
1987 | - * Unlock the given path |
|
1988 | - * |
|
1989 | - * @param string $path the path of the file to unlock, relative to the view |
|
1990 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1991 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1992 | - * |
|
1993 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1994 | - */ |
|
1995 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
1996 | - $absolutePath = $this->getAbsolutePath($path); |
|
1997 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1998 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1999 | - return false; |
|
2000 | - } |
|
2001 | - |
|
2002 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2003 | - if ($mount) { |
|
2004 | - $storage = $mount->getStorage(); |
|
2005 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2006 | - $storage->releaseLock( |
|
2007 | - $mount->getInternalPath($absolutePath), |
|
2008 | - $type, |
|
2009 | - $this->lockingProvider |
|
2010 | - ); |
|
2011 | - } |
|
2012 | - } |
|
2013 | - |
|
2014 | - return true; |
|
2015 | - } |
|
2016 | - |
|
2017 | - /** |
|
2018 | - * Lock a path and all its parents up to the root of the view |
|
2019 | - * |
|
2020 | - * @param string $path the path of the file to lock relative to the view |
|
2021 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2022 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2023 | - * |
|
2024 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2025 | - */ |
|
2026 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
2027 | - $absolutePath = $this->getAbsolutePath($path); |
|
2028 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2029 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2030 | - return false; |
|
2031 | - } |
|
2032 | - |
|
2033 | - $this->lockPath($path, $type, $lockMountPoint); |
|
2034 | - |
|
2035 | - $parents = $this->getParents($path); |
|
2036 | - foreach ($parents as $parent) { |
|
2037 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2038 | - } |
|
2039 | - |
|
2040 | - return true; |
|
2041 | - } |
|
2042 | - |
|
2043 | - /** |
|
2044 | - * Unlock a path and all its parents up to the root of the view |
|
2045 | - * |
|
2046 | - * @param string $path the path of the file to lock relative to the view |
|
2047 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2048 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2049 | - * |
|
2050 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2051 | - */ |
|
2052 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2053 | - $absolutePath = $this->getAbsolutePath($path); |
|
2054 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2055 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2056 | - return false; |
|
2057 | - } |
|
2058 | - |
|
2059 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
2060 | - |
|
2061 | - $parents = $this->getParents($path); |
|
2062 | - foreach ($parents as $parent) { |
|
2063 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2064 | - } |
|
2065 | - |
|
2066 | - return true; |
|
2067 | - } |
|
2068 | - |
|
2069 | - /** |
|
2070 | - * Only lock files in data/user/files/ |
|
2071 | - * |
|
2072 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2073 | - * @return bool |
|
2074 | - */ |
|
2075 | - protected function shouldLockFile($path) { |
|
2076 | - $path = Filesystem::normalizePath($path); |
|
2077 | - |
|
2078 | - $pathSegments = explode('/', $path); |
|
2079 | - if (isset($pathSegments[2])) { |
|
2080 | - // E.g.: /username/files/path-to-file |
|
2081 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2082 | - } |
|
2083 | - |
|
2084 | - return strpos($path, '/appdata_') !== 0; |
|
2085 | - } |
|
2086 | - |
|
2087 | - /** |
|
2088 | - * Shortens the given absolute path to be relative to |
|
2089 | - * "$user/files". |
|
2090 | - * |
|
2091 | - * @param string $absolutePath absolute path which is under "files" |
|
2092 | - * |
|
2093 | - * @return string path relative to "files" with trimmed slashes or null |
|
2094 | - * if the path was NOT relative to files |
|
2095 | - * |
|
2096 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
2097 | - * @since 8.1.0 |
|
2098 | - */ |
|
2099 | - public function getPathRelativeToFiles($absolutePath) { |
|
2100 | - $path = Filesystem::normalizePath($absolutePath); |
|
2101 | - $parts = explode('/', trim($path, '/'), 3); |
|
2102 | - // "$user", "files", "path/to/dir" |
|
2103 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2104 | - $this->logger->error( |
|
2105 | - '$absolutePath must be relative to "files", value is "%s"', |
|
2106 | - [ |
|
2107 | - $absolutePath |
|
2108 | - ] |
|
2109 | - ); |
|
2110 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2111 | - } |
|
2112 | - if (isset($parts[2])) { |
|
2113 | - return $parts[2]; |
|
2114 | - } |
|
2115 | - return ''; |
|
2116 | - } |
|
2117 | - |
|
2118 | - /** |
|
2119 | - * @param string $filename |
|
2120 | - * @return array |
|
2121 | - * @throws \OC\User\NoUserException |
|
2122 | - * @throws NotFoundException |
|
2123 | - */ |
|
2124 | - public function getUidAndFilename($filename) { |
|
2125 | - $info = $this->getFileInfo($filename); |
|
2126 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
2127 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2128 | - } |
|
2129 | - $uid = $info->getOwner()->getUID(); |
|
2130 | - if ($uid != \OCP\User::getUser()) { |
|
2131 | - Filesystem::initMountPoints($uid); |
|
2132 | - $ownerView = new View('/' . $uid . '/files'); |
|
2133 | - try { |
|
2134 | - $filename = $ownerView->getPath($info['fileid']); |
|
2135 | - } catch (NotFoundException $e) { |
|
2136 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2137 | - } |
|
2138 | - } |
|
2139 | - return [$uid, $filename]; |
|
2140 | - } |
|
2141 | - |
|
2142 | - /** |
|
2143 | - * Creates parent non-existing folders |
|
2144 | - * |
|
2145 | - * @param string $filePath |
|
2146 | - * @return bool |
|
2147 | - */ |
|
2148 | - private function createParentDirectories($filePath) { |
|
2149 | - $directoryParts = explode('/', $filePath); |
|
2150 | - $directoryParts = array_filter($directoryParts); |
|
2151 | - foreach ($directoryParts as $key => $part) { |
|
2152 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2153 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
2154 | - if ($this->is_file($currentPath)) { |
|
2155 | - return false; |
|
2156 | - } |
|
2157 | - if (!$this->file_exists($currentPath)) { |
|
2158 | - $this->mkdir($currentPath); |
|
2159 | - } |
|
2160 | - } |
|
2161 | - |
|
2162 | - return true; |
|
2163 | - } |
|
84 | + /** @var string */ |
|
85 | + private $fakeRoot = ''; |
|
86 | + |
|
87 | + /** |
|
88 | + * @var \OCP\Lock\ILockingProvider |
|
89 | + */ |
|
90 | + protected $lockingProvider; |
|
91 | + |
|
92 | + private $lockingEnabled; |
|
93 | + |
|
94 | + private $updaterEnabled = true; |
|
95 | + |
|
96 | + /** @var \OC\User\Manager */ |
|
97 | + private $userManager; |
|
98 | + |
|
99 | + /** @var \OCP\ILogger */ |
|
100 | + private $logger; |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $root |
|
104 | + * @throws \Exception If $root contains an invalid path |
|
105 | + */ |
|
106 | + public function __construct($root = '') { |
|
107 | + if (is_null($root)) { |
|
108 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
109 | + } |
|
110 | + if (!Filesystem::isValidPath($root)) { |
|
111 | + throw new \Exception(); |
|
112 | + } |
|
113 | + |
|
114 | + $this->fakeRoot = $root; |
|
115 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
116 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
117 | + $this->userManager = \OC::$server->getUserManager(); |
|
118 | + $this->logger = \OC::$server->getLogger(); |
|
119 | + } |
|
120 | + |
|
121 | + public function getAbsolutePath($path = '/') { |
|
122 | + if ($path === null) { |
|
123 | + return null; |
|
124 | + } |
|
125 | + $this->assertPathLength($path); |
|
126 | + if ($path === '') { |
|
127 | + $path = '/'; |
|
128 | + } |
|
129 | + if ($path[0] !== '/') { |
|
130 | + $path = '/' . $path; |
|
131 | + } |
|
132 | + return $this->fakeRoot . $path; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * change the root to a fake root |
|
137 | + * |
|
138 | + * @param string $fakeRoot |
|
139 | + * @return boolean|null |
|
140 | + */ |
|
141 | + public function chroot($fakeRoot) { |
|
142 | + if (!$fakeRoot == '') { |
|
143 | + if ($fakeRoot[0] !== '/') { |
|
144 | + $fakeRoot = '/' . $fakeRoot; |
|
145 | + } |
|
146 | + } |
|
147 | + $this->fakeRoot = $fakeRoot; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * get the fake root |
|
152 | + * |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + public function getRoot() { |
|
156 | + return $this->fakeRoot; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * get path relative to the root of the view |
|
161 | + * |
|
162 | + * @param string $path |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function getRelativePath($path) { |
|
166 | + $this->assertPathLength($path); |
|
167 | + if ($this->fakeRoot == '') { |
|
168 | + return $path; |
|
169 | + } |
|
170 | + |
|
171 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
172 | + return '/'; |
|
173 | + } |
|
174 | + |
|
175 | + // missing slashes can cause wrong matches! |
|
176 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
177 | + |
|
178 | + if (strpos($path, $root) !== 0) { |
|
179 | + return null; |
|
180 | + } else { |
|
181 | + $path = substr($path, strlen($this->fakeRoot)); |
|
182 | + if (strlen($path) === 0) { |
|
183 | + return '/'; |
|
184 | + } else { |
|
185 | + return $path; |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * get the mountpoint of the storage object for a path |
|
192 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
193 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
194 | + * and does not take the chroot into account ) |
|
195 | + * |
|
196 | + * @param string $path |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function getMountPoint($path) { |
|
200 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * get the mountpoint of the storage object for a path |
|
205 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
206 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
207 | + * and does not take the chroot into account ) |
|
208 | + * |
|
209 | + * @param string $path |
|
210 | + * @return \OCP\Files\Mount\IMountPoint |
|
211 | + */ |
|
212 | + public function getMount($path) { |
|
213 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * resolve a path to a storage and internal path |
|
218 | + * |
|
219 | + * @param string $path |
|
220 | + * @return array an array consisting of the storage and the internal path |
|
221 | + */ |
|
222 | + public function resolvePath($path) { |
|
223 | + $a = $this->getAbsolutePath($path); |
|
224 | + $p = Filesystem::normalizePath($a); |
|
225 | + return Filesystem::resolvePath($p); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * return the path to a local version of the file |
|
230 | + * we need this because we can't know if a file is stored local or not from |
|
231 | + * outside the filestorage and for some purposes a local file is needed |
|
232 | + * |
|
233 | + * @param string $path |
|
234 | + * @return string |
|
235 | + */ |
|
236 | + public function getLocalFile($path) { |
|
237 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
238 | + $path = $this->getAbsolutePath($path); |
|
239 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
240 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
241 | + return $storage->getLocalFile($internalPath); |
|
242 | + } else { |
|
243 | + return null; |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @param string $path |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + public function getLocalFolder($path) { |
|
252 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
253 | + $path = $this->getAbsolutePath($path); |
|
254 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
255 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
256 | + return $storage->getLocalFolder($internalPath); |
|
257 | + } else { |
|
258 | + return null; |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * the following functions operate with arguments and return values identical |
|
264 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
265 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
266 | + */ |
|
267 | + public function mkdir($path) { |
|
268 | + return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * remove mount point |
|
273 | + * |
|
274 | + * @param \OC\Files\Mount\MoveableMount $mount |
|
275 | + * @param string $path relative to data/ |
|
276 | + * @return boolean |
|
277 | + */ |
|
278 | + protected function removeMount($mount, $path) { |
|
279 | + if ($mount instanceof MoveableMount) { |
|
280 | + // cut of /user/files to get the relative path to data/user/files |
|
281 | + $pathParts = explode('/', $path, 4); |
|
282 | + $relPath = '/' . $pathParts[3]; |
|
283 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
284 | + \OC_Hook::emit( |
|
285 | + Filesystem::CLASSNAME, "umount", |
|
286 | + array(Filesystem::signal_param_path => $relPath) |
|
287 | + ); |
|
288 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
289 | + $result = $mount->removeMount(); |
|
290 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
291 | + if ($result) { |
|
292 | + \OC_Hook::emit( |
|
293 | + Filesystem::CLASSNAME, "post_umount", |
|
294 | + array(Filesystem::signal_param_path => $relPath) |
|
295 | + ); |
|
296 | + } |
|
297 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
298 | + return $result; |
|
299 | + } else { |
|
300 | + // do not allow deleting the storage's root / the mount point |
|
301 | + // because for some storages it might delete the whole contents |
|
302 | + // but isn't supposed to work that way |
|
303 | + return false; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + public function disableCacheUpdate() { |
|
308 | + $this->updaterEnabled = false; |
|
309 | + } |
|
310 | + |
|
311 | + public function enableCacheUpdate() { |
|
312 | + $this->updaterEnabled = true; |
|
313 | + } |
|
314 | + |
|
315 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
316 | + if ($this->updaterEnabled) { |
|
317 | + if (is_null($time)) { |
|
318 | + $time = time(); |
|
319 | + } |
|
320 | + $storage->getUpdater()->update($internalPath, $time); |
|
321 | + } |
|
322 | + } |
|
323 | + |
|
324 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
325 | + if ($this->updaterEnabled) { |
|
326 | + $storage->getUpdater()->remove($internalPath); |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
331 | + if ($this->updaterEnabled) { |
|
332 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @param string $path |
|
338 | + * @return bool|mixed |
|
339 | + */ |
|
340 | + public function rmdir($path) { |
|
341 | + $absolutePath = $this->getAbsolutePath($path); |
|
342 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
343 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
344 | + return $this->removeMount($mount, $absolutePath); |
|
345 | + } |
|
346 | + if ($this->is_dir($path)) { |
|
347 | + $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
348 | + } else { |
|
349 | + $result = false; |
|
350 | + } |
|
351 | + |
|
352 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
353 | + $storage = $mount->getStorage(); |
|
354 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
355 | + $storage->getUpdater()->remove($internalPath); |
|
356 | + } |
|
357 | + return $result; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * @param string $path |
|
362 | + * @return resource |
|
363 | + */ |
|
364 | + public function opendir($path) { |
|
365 | + return $this->basicOperation('opendir', $path, array('read')); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * @param string $path |
|
370 | + * @return bool|mixed |
|
371 | + */ |
|
372 | + public function is_dir($path) { |
|
373 | + if ($path == '/') { |
|
374 | + return true; |
|
375 | + } |
|
376 | + return $this->basicOperation('is_dir', $path); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * @param string $path |
|
381 | + * @return bool|mixed |
|
382 | + */ |
|
383 | + public function is_file($path) { |
|
384 | + if ($path == '/') { |
|
385 | + return false; |
|
386 | + } |
|
387 | + return $this->basicOperation('is_file', $path); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * @param string $path |
|
392 | + * @return mixed |
|
393 | + */ |
|
394 | + public function stat($path) { |
|
395 | + return $this->basicOperation('stat', $path); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param string $path |
|
400 | + * @return mixed |
|
401 | + */ |
|
402 | + public function filetype($path) { |
|
403 | + return $this->basicOperation('filetype', $path); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * @param string $path |
|
408 | + * @return mixed |
|
409 | + */ |
|
410 | + public function filesize($path) { |
|
411 | + return $this->basicOperation('filesize', $path); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * @param string $path |
|
416 | + * @return bool|mixed |
|
417 | + * @throws \OCP\Files\InvalidPathException |
|
418 | + */ |
|
419 | + public function readfile($path) { |
|
420 | + $this->assertPathLength($path); |
|
421 | + @ob_end_clean(); |
|
422 | + $handle = $this->fopen($path, 'rb'); |
|
423 | + if ($handle) { |
|
424 | + $chunkSize = 8192; // 8 kB chunks |
|
425 | + while (!feof($handle)) { |
|
426 | + echo fread($handle, $chunkSize); |
|
427 | + flush(); |
|
428 | + } |
|
429 | + fclose($handle); |
|
430 | + return $this->filesize($path); |
|
431 | + } |
|
432 | + return false; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * @param string $path |
|
437 | + * @param int $from |
|
438 | + * @param int $to |
|
439 | + * @return bool|mixed |
|
440 | + * @throws \OCP\Files\InvalidPathException |
|
441 | + * @throws \OCP\Files\UnseekableException |
|
442 | + */ |
|
443 | + public function readfilePart($path, $from, $to) { |
|
444 | + $this->assertPathLength($path); |
|
445 | + @ob_end_clean(); |
|
446 | + $handle = $this->fopen($path, 'rb'); |
|
447 | + if ($handle) { |
|
448 | + $chunkSize = 8192; // 8 kB chunks |
|
449 | + $startReading = true; |
|
450 | + |
|
451 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
452 | + // forward file handle via chunked fread because fseek seem to have failed |
|
453 | + |
|
454 | + $end = $from + 1; |
|
455 | + while (!feof($handle) && ftell($handle) < $end) { |
|
456 | + $len = $from - ftell($handle); |
|
457 | + if ($len > $chunkSize) { |
|
458 | + $len = $chunkSize; |
|
459 | + } |
|
460 | + $result = fread($handle, $len); |
|
461 | + |
|
462 | + if ($result === false) { |
|
463 | + $startReading = false; |
|
464 | + break; |
|
465 | + } |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + if ($startReading) { |
|
470 | + $end = $to + 1; |
|
471 | + while (!feof($handle) && ftell($handle) < $end) { |
|
472 | + $len = $end - ftell($handle); |
|
473 | + if ($len > $chunkSize) { |
|
474 | + $len = $chunkSize; |
|
475 | + } |
|
476 | + echo fread($handle, $len); |
|
477 | + flush(); |
|
478 | + } |
|
479 | + return ftell($handle) - $from; |
|
480 | + } |
|
481 | + |
|
482 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
483 | + } |
|
484 | + return false; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * @param string $path |
|
489 | + * @return mixed |
|
490 | + */ |
|
491 | + public function isCreatable($path) { |
|
492 | + return $this->basicOperation('isCreatable', $path); |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * @param string $path |
|
497 | + * @return mixed |
|
498 | + */ |
|
499 | + public function isReadable($path) { |
|
500 | + return $this->basicOperation('isReadable', $path); |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * @param string $path |
|
505 | + * @return mixed |
|
506 | + */ |
|
507 | + public function isUpdatable($path) { |
|
508 | + return $this->basicOperation('isUpdatable', $path); |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * @param string $path |
|
513 | + * @return bool|mixed |
|
514 | + */ |
|
515 | + public function isDeletable($path) { |
|
516 | + $absolutePath = $this->getAbsolutePath($path); |
|
517 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
518 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
519 | + return $mount instanceof MoveableMount; |
|
520 | + } |
|
521 | + return $this->basicOperation('isDeletable', $path); |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * @param string $path |
|
526 | + * @return mixed |
|
527 | + */ |
|
528 | + public function isSharable($path) { |
|
529 | + return $this->basicOperation('isSharable', $path); |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * @param string $path |
|
534 | + * @return bool|mixed |
|
535 | + */ |
|
536 | + public function file_exists($path) { |
|
537 | + if ($path == '/') { |
|
538 | + return true; |
|
539 | + } |
|
540 | + return $this->basicOperation('file_exists', $path); |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * @param string $path |
|
545 | + * @return mixed |
|
546 | + */ |
|
547 | + public function filemtime($path) { |
|
548 | + return $this->basicOperation('filemtime', $path); |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * @param string $path |
|
553 | + * @param int|string $mtime |
|
554 | + * @return bool |
|
555 | + */ |
|
556 | + public function touch($path, $mtime = null) { |
|
557 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
558 | + $mtime = strtotime($mtime); |
|
559 | + } |
|
560 | + |
|
561 | + $hooks = array('touch'); |
|
562 | + |
|
563 | + if (!$this->file_exists($path)) { |
|
564 | + $hooks[] = 'create'; |
|
565 | + $hooks[] = 'write'; |
|
566 | + } |
|
567 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
568 | + if (!$result) { |
|
569 | + // If create file fails because of permissions on external storage like SMB folders, |
|
570 | + // check file exists and return false if not. |
|
571 | + if (!$this->file_exists($path)) { |
|
572 | + return false; |
|
573 | + } |
|
574 | + if (is_null($mtime)) { |
|
575 | + $mtime = time(); |
|
576 | + } |
|
577 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
578 | + $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
579 | + } |
|
580 | + return true; |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * @param string $path |
|
585 | + * @return mixed |
|
586 | + */ |
|
587 | + public function file_get_contents($path) { |
|
588 | + return $this->basicOperation('file_get_contents', $path, array('read')); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * @param bool $exists |
|
593 | + * @param string $path |
|
594 | + * @param bool $run |
|
595 | + */ |
|
596 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
597 | + if (!$exists) { |
|
598 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
599 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
600 | + Filesystem::signal_param_run => &$run, |
|
601 | + )); |
|
602 | + } else { |
|
603 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
604 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
605 | + Filesystem::signal_param_run => &$run, |
|
606 | + )); |
|
607 | + } |
|
608 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
609 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
610 | + Filesystem::signal_param_run => &$run, |
|
611 | + )); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * @param bool $exists |
|
616 | + * @param string $path |
|
617 | + */ |
|
618 | + protected function emit_file_hooks_post($exists, $path) { |
|
619 | + if (!$exists) { |
|
620 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
621 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
622 | + )); |
|
623 | + } else { |
|
624 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
625 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
626 | + )); |
|
627 | + } |
|
628 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
629 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
630 | + )); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * @param string $path |
|
635 | + * @param mixed $data |
|
636 | + * @return bool|mixed |
|
637 | + * @throws \Exception |
|
638 | + */ |
|
639 | + public function file_put_contents($path, $data) { |
|
640 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
641 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
642 | + if (Filesystem::isValidPath($path) |
|
643 | + and !Filesystem::isFileBlacklisted($path) |
|
644 | + ) { |
|
645 | + $path = $this->getRelativePath($absolutePath); |
|
646 | + |
|
647 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
648 | + |
|
649 | + $exists = $this->file_exists($path); |
|
650 | + $run = true; |
|
651 | + if ($this->shouldEmitHooks($path)) { |
|
652 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
653 | + } |
|
654 | + if (!$run) { |
|
655 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
656 | + return false; |
|
657 | + } |
|
658 | + |
|
659 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
660 | + |
|
661 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
662 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
663 | + $target = $storage->fopen($internalPath, 'w'); |
|
664 | + if ($target) { |
|
665 | + list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
666 | + fclose($target); |
|
667 | + fclose($data); |
|
668 | + |
|
669 | + $this->writeUpdate($storage, $internalPath); |
|
670 | + |
|
671 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
672 | + |
|
673 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
674 | + $this->emit_file_hooks_post($exists, $path); |
|
675 | + } |
|
676 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
677 | + return $result; |
|
678 | + } else { |
|
679 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
680 | + return false; |
|
681 | + } |
|
682 | + } else { |
|
683 | + return false; |
|
684 | + } |
|
685 | + } else { |
|
686 | + $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
687 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
688 | + } |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * @param string $path |
|
693 | + * @return bool|mixed |
|
694 | + */ |
|
695 | + public function unlink($path) { |
|
696 | + if ($path === '' || $path === '/') { |
|
697 | + // do not allow deleting the root |
|
698 | + return false; |
|
699 | + } |
|
700 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
701 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
702 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
703 | + if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
704 | + return $this->removeMount($mount, $absolutePath); |
|
705 | + } |
|
706 | + if ($this->is_dir($path)) { |
|
707 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
708 | + } else { |
|
709 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
710 | + } |
|
711 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
712 | + $storage = $mount->getStorage(); |
|
713 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
714 | + $storage->getUpdater()->remove($internalPath); |
|
715 | + return true; |
|
716 | + } else { |
|
717 | + return $result; |
|
718 | + } |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * @param string $directory |
|
723 | + * @return bool|mixed |
|
724 | + */ |
|
725 | + public function deleteAll($directory) { |
|
726 | + return $this->rmdir($directory); |
|
727 | + } |
|
728 | + |
|
729 | + /** |
|
730 | + * Rename/move a file or folder from the source path to target path. |
|
731 | + * |
|
732 | + * @param string $path1 source path |
|
733 | + * @param string $path2 target path |
|
734 | + * |
|
735 | + * @return bool|mixed |
|
736 | + */ |
|
737 | + public function rename($path1, $path2) { |
|
738 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
739 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
740 | + $result = false; |
|
741 | + if ( |
|
742 | + Filesystem::isValidPath($path2) |
|
743 | + and Filesystem::isValidPath($path1) |
|
744 | + and !Filesystem::isFileBlacklisted($path2) |
|
745 | + ) { |
|
746 | + $path1 = $this->getRelativePath($absolutePath1); |
|
747 | + $path2 = $this->getRelativePath($absolutePath2); |
|
748 | + $exists = $this->file_exists($path2); |
|
749 | + |
|
750 | + if ($path1 == null or $path2 == null) { |
|
751 | + return false; |
|
752 | + } |
|
753 | + |
|
754 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
755 | + try { |
|
756 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
757 | + |
|
758 | + $run = true; |
|
759 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
760 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
761 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
762 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
763 | + \OC_Hook::emit( |
|
764 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
765 | + array( |
|
766 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
767 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
768 | + Filesystem::signal_param_run => &$run |
|
769 | + ) |
|
770 | + ); |
|
771 | + } |
|
772 | + if ($run) { |
|
773 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
774 | + |
|
775 | + $manager = Filesystem::getMountManager(); |
|
776 | + $mount1 = $this->getMount($path1); |
|
777 | + $mount2 = $this->getMount($path2); |
|
778 | + $storage1 = $mount1->getStorage(); |
|
779 | + $storage2 = $mount2->getStorage(); |
|
780 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
781 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
782 | + |
|
783 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
784 | + try { |
|
785 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
786 | + |
|
787 | + if ($internalPath1 === '') { |
|
788 | + if ($mount1 instanceof MoveableMount) { |
|
789 | + if ($this->isTargetAllowed($absolutePath2)) { |
|
790 | + /** |
|
791 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
792 | + */ |
|
793 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
794 | + $result = $mount1->moveMount($absolutePath2); |
|
795 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
796 | + } else { |
|
797 | + $result = false; |
|
798 | + } |
|
799 | + } else { |
|
800 | + $result = false; |
|
801 | + } |
|
802 | + // moving a file/folder within the same mount point |
|
803 | + } elseif ($storage1 === $storage2) { |
|
804 | + if ($storage1) { |
|
805 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
806 | + } else { |
|
807 | + $result = false; |
|
808 | + } |
|
809 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
810 | + } else { |
|
811 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
812 | + } |
|
813 | + |
|
814 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
815 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
816 | + $this->writeUpdate($storage2, $internalPath2); |
|
817 | + } else if ($result) { |
|
818 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
819 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
820 | + } |
|
821 | + } |
|
822 | + } catch(\Exception $e) { |
|
823 | + throw $e; |
|
824 | + } finally { |
|
825 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
826 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
827 | + } |
|
828 | + |
|
829 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
830 | + if ($this->shouldEmitHooks()) { |
|
831 | + $this->emit_file_hooks_post($exists, $path2); |
|
832 | + } |
|
833 | + } elseif ($result) { |
|
834 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
835 | + \OC_Hook::emit( |
|
836 | + Filesystem::CLASSNAME, |
|
837 | + Filesystem::signal_post_rename, |
|
838 | + array( |
|
839 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
840 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
841 | + ) |
|
842 | + ); |
|
843 | + } |
|
844 | + } |
|
845 | + } |
|
846 | + } catch(\Exception $e) { |
|
847 | + throw $e; |
|
848 | + } finally { |
|
849 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
850 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
851 | + } |
|
852 | + } |
|
853 | + return $result; |
|
854 | + } |
|
855 | + |
|
856 | + /** |
|
857 | + * Copy a file/folder from the source path to target path |
|
858 | + * |
|
859 | + * @param string $path1 source path |
|
860 | + * @param string $path2 target path |
|
861 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
862 | + * |
|
863 | + * @return bool|mixed |
|
864 | + */ |
|
865 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
866 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
867 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
868 | + $result = false; |
|
869 | + if ( |
|
870 | + Filesystem::isValidPath($path2) |
|
871 | + and Filesystem::isValidPath($path1) |
|
872 | + and !Filesystem::isFileBlacklisted($path2) |
|
873 | + ) { |
|
874 | + $path1 = $this->getRelativePath($absolutePath1); |
|
875 | + $path2 = $this->getRelativePath($absolutePath2); |
|
876 | + |
|
877 | + if ($path1 == null or $path2 == null) { |
|
878 | + return false; |
|
879 | + } |
|
880 | + $run = true; |
|
881 | + |
|
882 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
883 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
884 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
885 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
886 | + |
|
887 | + try { |
|
888 | + |
|
889 | + $exists = $this->file_exists($path2); |
|
890 | + if ($this->shouldEmitHooks()) { |
|
891 | + \OC_Hook::emit( |
|
892 | + Filesystem::CLASSNAME, |
|
893 | + Filesystem::signal_copy, |
|
894 | + array( |
|
895 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
896 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
897 | + Filesystem::signal_param_run => &$run |
|
898 | + ) |
|
899 | + ); |
|
900 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
901 | + } |
|
902 | + if ($run) { |
|
903 | + $mount1 = $this->getMount($path1); |
|
904 | + $mount2 = $this->getMount($path2); |
|
905 | + $storage1 = $mount1->getStorage(); |
|
906 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
907 | + $storage2 = $mount2->getStorage(); |
|
908 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
909 | + |
|
910 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
911 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
912 | + |
|
913 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
914 | + if ($storage1) { |
|
915 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
916 | + } else { |
|
917 | + $result = false; |
|
918 | + } |
|
919 | + } else { |
|
920 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
921 | + } |
|
922 | + |
|
923 | + $this->writeUpdate($storage2, $internalPath2); |
|
924 | + |
|
925 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
926 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
927 | + |
|
928 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
929 | + \OC_Hook::emit( |
|
930 | + Filesystem::CLASSNAME, |
|
931 | + Filesystem::signal_post_copy, |
|
932 | + array( |
|
933 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
934 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
935 | + ) |
|
936 | + ); |
|
937 | + $this->emit_file_hooks_post($exists, $path2); |
|
938 | + } |
|
939 | + |
|
940 | + } |
|
941 | + } catch (\Exception $e) { |
|
942 | + $this->unlockFile($path2, $lockTypePath2); |
|
943 | + $this->unlockFile($path1, $lockTypePath1); |
|
944 | + throw $e; |
|
945 | + } |
|
946 | + |
|
947 | + $this->unlockFile($path2, $lockTypePath2); |
|
948 | + $this->unlockFile($path1, $lockTypePath1); |
|
949 | + |
|
950 | + } |
|
951 | + return $result; |
|
952 | + } |
|
953 | + |
|
954 | + /** |
|
955 | + * @param string $path |
|
956 | + * @param string $mode 'r' or 'w' |
|
957 | + * @return resource |
|
958 | + */ |
|
959 | + public function fopen($path, $mode) { |
|
960 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
961 | + $hooks = array(); |
|
962 | + switch ($mode) { |
|
963 | + case 'r': |
|
964 | + $hooks[] = 'read'; |
|
965 | + break; |
|
966 | + case 'r+': |
|
967 | + case 'w+': |
|
968 | + case 'x+': |
|
969 | + case 'a+': |
|
970 | + $hooks[] = 'read'; |
|
971 | + $hooks[] = 'write'; |
|
972 | + break; |
|
973 | + case 'w': |
|
974 | + case 'x': |
|
975 | + case 'a': |
|
976 | + $hooks[] = 'write'; |
|
977 | + break; |
|
978 | + default: |
|
979 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
980 | + } |
|
981 | + |
|
982 | + if ($mode !== 'r' && $mode !== 'w') { |
|
983 | + \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
984 | + } |
|
985 | + |
|
986 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
987 | + } |
|
988 | + |
|
989 | + /** |
|
990 | + * @param string $path |
|
991 | + * @return bool|string |
|
992 | + * @throws \OCP\Files\InvalidPathException |
|
993 | + */ |
|
994 | + public function toTmpFile($path) { |
|
995 | + $this->assertPathLength($path); |
|
996 | + if (Filesystem::isValidPath($path)) { |
|
997 | + $source = $this->fopen($path, 'r'); |
|
998 | + if ($source) { |
|
999 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1000 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1001 | + file_put_contents($tmpFile, $source); |
|
1002 | + return $tmpFile; |
|
1003 | + } else { |
|
1004 | + return false; |
|
1005 | + } |
|
1006 | + } else { |
|
1007 | + return false; |
|
1008 | + } |
|
1009 | + } |
|
1010 | + |
|
1011 | + /** |
|
1012 | + * @param string $tmpFile |
|
1013 | + * @param string $path |
|
1014 | + * @return bool|mixed |
|
1015 | + * @throws \OCP\Files\InvalidPathException |
|
1016 | + */ |
|
1017 | + public function fromTmpFile($tmpFile, $path) { |
|
1018 | + $this->assertPathLength($path); |
|
1019 | + if (Filesystem::isValidPath($path)) { |
|
1020 | + |
|
1021 | + // Get directory that the file is going into |
|
1022 | + $filePath = dirname($path); |
|
1023 | + |
|
1024 | + // Create the directories if any |
|
1025 | + if (!$this->file_exists($filePath)) { |
|
1026 | + $result = $this->createParentDirectories($filePath); |
|
1027 | + if ($result === false) { |
|
1028 | + return false; |
|
1029 | + } |
|
1030 | + } |
|
1031 | + |
|
1032 | + $source = fopen($tmpFile, 'r'); |
|
1033 | + if ($source) { |
|
1034 | + $result = $this->file_put_contents($path, $source); |
|
1035 | + // $this->file_put_contents() might have already closed |
|
1036 | + // the resource, so we check it, before trying to close it |
|
1037 | + // to avoid messages in the error log. |
|
1038 | + if (is_resource($source)) { |
|
1039 | + fclose($source); |
|
1040 | + } |
|
1041 | + unlink($tmpFile); |
|
1042 | + return $result; |
|
1043 | + } else { |
|
1044 | + return false; |
|
1045 | + } |
|
1046 | + } else { |
|
1047 | + return false; |
|
1048 | + } |
|
1049 | + } |
|
1050 | + |
|
1051 | + |
|
1052 | + /** |
|
1053 | + * @param string $path |
|
1054 | + * @return mixed |
|
1055 | + * @throws \OCP\Files\InvalidPathException |
|
1056 | + */ |
|
1057 | + public function getMimeType($path) { |
|
1058 | + $this->assertPathLength($path); |
|
1059 | + return $this->basicOperation('getMimeType', $path); |
|
1060 | + } |
|
1061 | + |
|
1062 | + /** |
|
1063 | + * @param string $type |
|
1064 | + * @param string $path |
|
1065 | + * @param bool $raw |
|
1066 | + * @return bool|null|string |
|
1067 | + */ |
|
1068 | + public function hash($type, $path, $raw = false) { |
|
1069 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1070 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1071 | + if (Filesystem::isValidPath($path)) { |
|
1072 | + $path = $this->getRelativePath($absolutePath); |
|
1073 | + if ($path == null) { |
|
1074 | + return false; |
|
1075 | + } |
|
1076 | + if ($this->shouldEmitHooks($path)) { |
|
1077 | + \OC_Hook::emit( |
|
1078 | + Filesystem::CLASSNAME, |
|
1079 | + Filesystem::signal_read, |
|
1080 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
1081 | + ); |
|
1082 | + } |
|
1083 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1084 | + if ($storage) { |
|
1085 | + return $storage->hash($type, $internalPath, $raw); |
|
1086 | + } |
|
1087 | + } |
|
1088 | + return null; |
|
1089 | + } |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * @param string $path |
|
1093 | + * @return mixed |
|
1094 | + * @throws \OCP\Files\InvalidPathException |
|
1095 | + */ |
|
1096 | + public function free_space($path = '/') { |
|
1097 | + $this->assertPathLength($path); |
|
1098 | + $result = $this->basicOperation('free_space', $path); |
|
1099 | + if ($result === null) { |
|
1100 | + throw new InvalidPathException(); |
|
1101 | + } |
|
1102 | + return $result; |
|
1103 | + } |
|
1104 | + |
|
1105 | + /** |
|
1106 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1107 | + * |
|
1108 | + * @param string $operation |
|
1109 | + * @param string $path |
|
1110 | + * @param array $hooks (optional) |
|
1111 | + * @param mixed $extraParam (optional) |
|
1112 | + * @return mixed |
|
1113 | + * @throws \Exception |
|
1114 | + * |
|
1115 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1116 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1117 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1118 | + */ |
|
1119 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1120 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1121 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1122 | + if (Filesystem::isValidPath($path) |
|
1123 | + and !Filesystem::isFileBlacklisted($path) |
|
1124 | + ) { |
|
1125 | + $path = $this->getRelativePath($absolutePath); |
|
1126 | + if ($path == null) { |
|
1127 | + return false; |
|
1128 | + } |
|
1129 | + |
|
1130 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1131 | + // always a shared lock during pre-hooks so the hook can read the file |
|
1132 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1133 | + } |
|
1134 | + |
|
1135 | + $run = $this->runHooks($hooks, $path); |
|
1136 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
1137 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1138 | + if ($run and $storage) { |
|
1139 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1140 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1141 | + } |
|
1142 | + try { |
|
1143 | + if (!is_null($extraParam)) { |
|
1144 | + $result = $storage->$operation($internalPath, $extraParam); |
|
1145 | + } else { |
|
1146 | + $result = $storage->$operation($internalPath); |
|
1147 | + } |
|
1148 | + } catch (\Exception $e) { |
|
1149 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1150 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1151 | + } else if (in_array('read', $hooks)) { |
|
1152 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1153 | + } |
|
1154 | + throw $e; |
|
1155 | + } |
|
1156 | + |
|
1157 | + if ($result && in_array('delete', $hooks) and $result) { |
|
1158 | + $this->removeUpdate($storage, $internalPath); |
|
1159 | + } |
|
1160 | + if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
1161 | + $this->writeUpdate($storage, $internalPath); |
|
1162 | + } |
|
1163 | + if ($result && in_array('touch', $hooks)) { |
|
1164 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1165 | + } |
|
1166 | + |
|
1167 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1168 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1169 | + } |
|
1170 | + |
|
1171 | + $unlockLater = false; |
|
1172 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1173 | + $unlockLater = true; |
|
1174 | + // make sure our unlocking callback will still be called if connection is aborted |
|
1175 | + ignore_user_abort(true); |
|
1176 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1177 | + if (in_array('write', $hooks)) { |
|
1178 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1179 | + } else if (in_array('read', $hooks)) { |
|
1180 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1181 | + } |
|
1182 | + }); |
|
1183 | + } |
|
1184 | + |
|
1185 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1186 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1187 | + $this->runHooks($hooks, $path, true); |
|
1188 | + } |
|
1189 | + } |
|
1190 | + |
|
1191 | + if (!$unlockLater |
|
1192 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1193 | + ) { |
|
1194 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1195 | + } |
|
1196 | + return $result; |
|
1197 | + } else { |
|
1198 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1199 | + } |
|
1200 | + } |
|
1201 | + return null; |
|
1202 | + } |
|
1203 | + |
|
1204 | + /** |
|
1205 | + * get the path relative to the default root for hook usage |
|
1206 | + * |
|
1207 | + * @param string $path |
|
1208 | + * @return string |
|
1209 | + */ |
|
1210 | + private function getHookPath($path) { |
|
1211 | + if (!Filesystem::getView()) { |
|
1212 | + return $path; |
|
1213 | + } |
|
1214 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1215 | + } |
|
1216 | + |
|
1217 | + private function shouldEmitHooks($path = '') { |
|
1218 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1219 | + return false; |
|
1220 | + } |
|
1221 | + if (!Filesystem::$loaded) { |
|
1222 | + return false; |
|
1223 | + } |
|
1224 | + $defaultRoot = Filesystem::getRoot(); |
|
1225 | + if ($defaultRoot === null) { |
|
1226 | + return false; |
|
1227 | + } |
|
1228 | + if ($this->fakeRoot === $defaultRoot) { |
|
1229 | + return true; |
|
1230 | + } |
|
1231 | + $fullPath = $this->getAbsolutePath($path); |
|
1232 | + |
|
1233 | + if ($fullPath === $defaultRoot) { |
|
1234 | + return true; |
|
1235 | + } |
|
1236 | + |
|
1237 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1238 | + } |
|
1239 | + |
|
1240 | + /** |
|
1241 | + * @param string[] $hooks |
|
1242 | + * @param string $path |
|
1243 | + * @param bool $post |
|
1244 | + * @return bool |
|
1245 | + */ |
|
1246 | + private function runHooks($hooks, $path, $post = false) { |
|
1247 | + $relativePath = $path; |
|
1248 | + $path = $this->getHookPath($path); |
|
1249 | + $prefix = $post ? 'post_' : ''; |
|
1250 | + $run = true; |
|
1251 | + if ($this->shouldEmitHooks($relativePath)) { |
|
1252 | + foreach ($hooks as $hook) { |
|
1253 | + if ($hook != 'read') { |
|
1254 | + \OC_Hook::emit( |
|
1255 | + Filesystem::CLASSNAME, |
|
1256 | + $prefix . $hook, |
|
1257 | + array( |
|
1258 | + Filesystem::signal_param_run => &$run, |
|
1259 | + Filesystem::signal_param_path => $path |
|
1260 | + ) |
|
1261 | + ); |
|
1262 | + } elseif (!$post) { |
|
1263 | + \OC_Hook::emit( |
|
1264 | + Filesystem::CLASSNAME, |
|
1265 | + $prefix . $hook, |
|
1266 | + array( |
|
1267 | + Filesystem::signal_param_path => $path |
|
1268 | + ) |
|
1269 | + ); |
|
1270 | + } |
|
1271 | + } |
|
1272 | + } |
|
1273 | + return $run; |
|
1274 | + } |
|
1275 | + |
|
1276 | + /** |
|
1277 | + * check if a file or folder has been updated since $time |
|
1278 | + * |
|
1279 | + * @param string $path |
|
1280 | + * @param int $time |
|
1281 | + * @return bool |
|
1282 | + */ |
|
1283 | + public function hasUpdated($path, $time) { |
|
1284 | + return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
1285 | + } |
|
1286 | + |
|
1287 | + /** |
|
1288 | + * @param string $ownerId |
|
1289 | + * @return \OC\User\User |
|
1290 | + */ |
|
1291 | + private function getUserObjectForOwner($ownerId) { |
|
1292 | + $owner = $this->userManager->get($ownerId); |
|
1293 | + if ($owner instanceof IUser) { |
|
1294 | + return $owner; |
|
1295 | + } else { |
|
1296 | + return new User($ownerId, null); |
|
1297 | + } |
|
1298 | + } |
|
1299 | + |
|
1300 | + /** |
|
1301 | + * Get file info from cache |
|
1302 | + * |
|
1303 | + * If the file is not in cached it will be scanned |
|
1304 | + * If the file has changed on storage the cache will be updated |
|
1305 | + * |
|
1306 | + * @param \OC\Files\Storage\Storage $storage |
|
1307 | + * @param string $internalPath |
|
1308 | + * @param string $relativePath |
|
1309 | + * @return ICacheEntry|bool |
|
1310 | + */ |
|
1311 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1312 | + $cache = $storage->getCache($internalPath); |
|
1313 | + $data = $cache->get($internalPath); |
|
1314 | + $watcher = $storage->getWatcher($internalPath); |
|
1315 | + |
|
1316 | + try { |
|
1317 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1318 | + if (!$data || $data['size'] === -1) { |
|
1319 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1320 | + if (!$storage->file_exists($internalPath)) { |
|
1321 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1322 | + return false; |
|
1323 | + } |
|
1324 | + $scanner = $storage->getScanner($internalPath); |
|
1325 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1326 | + $data = $cache->get($internalPath); |
|
1327 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1328 | + } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1329 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1330 | + $watcher->update($internalPath, $data); |
|
1331 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1332 | + $data = $cache->get($internalPath); |
|
1333 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1334 | + } |
|
1335 | + } catch (LockedException $e) { |
|
1336 | + // if the file is locked we just use the old cache info |
|
1337 | + } |
|
1338 | + |
|
1339 | + return $data; |
|
1340 | + } |
|
1341 | + |
|
1342 | + /** |
|
1343 | + * get the filesystem info |
|
1344 | + * |
|
1345 | + * @param string $path |
|
1346 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
1347 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1348 | + * defaults to true |
|
1349 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
1350 | + */ |
|
1351 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
1352 | + $this->assertPathLength($path); |
|
1353 | + if (!Filesystem::isValidPath($path)) { |
|
1354 | + return false; |
|
1355 | + } |
|
1356 | + if (Cache\Scanner::isPartialFile($path)) { |
|
1357 | + return $this->getPartFileInfo($path); |
|
1358 | + } |
|
1359 | + $relativePath = $path; |
|
1360 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1361 | + |
|
1362 | + $mount = Filesystem::getMountManager()->find($path); |
|
1363 | + if (!$mount) { |
|
1364 | + return false; |
|
1365 | + } |
|
1366 | + $storage = $mount->getStorage(); |
|
1367 | + $internalPath = $mount->getInternalPath($path); |
|
1368 | + if ($storage) { |
|
1369 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1370 | + |
|
1371 | + if (!$data instanceof ICacheEntry) { |
|
1372 | + return false; |
|
1373 | + } |
|
1374 | + |
|
1375 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1376 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1377 | + } |
|
1378 | + |
|
1379 | + $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
1380 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1381 | + |
|
1382 | + if ($data and isset($data['fileid'])) { |
|
1383 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1384 | + //add the sizes of other mount points to the folder |
|
1385 | + $extOnly = ($includeMountPoints === 'ext'); |
|
1386 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1387 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1388 | + $subStorage = $mount->getStorage(); |
|
1389 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1390 | + })); |
|
1391 | + } |
|
1392 | + } |
|
1393 | + |
|
1394 | + return $info; |
|
1395 | + } |
|
1396 | + |
|
1397 | + return false; |
|
1398 | + } |
|
1399 | + |
|
1400 | + /** |
|
1401 | + * get the content of a directory |
|
1402 | + * |
|
1403 | + * @param string $directory path under datadirectory |
|
1404 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1405 | + * @return FileInfo[] |
|
1406 | + */ |
|
1407 | + public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
1408 | + $this->assertPathLength($directory); |
|
1409 | + if (!Filesystem::isValidPath($directory)) { |
|
1410 | + return []; |
|
1411 | + } |
|
1412 | + $path = $this->getAbsolutePath($directory); |
|
1413 | + $path = Filesystem::normalizePath($path); |
|
1414 | + $mount = $this->getMount($directory); |
|
1415 | + if (!$mount) { |
|
1416 | + return []; |
|
1417 | + } |
|
1418 | + $storage = $mount->getStorage(); |
|
1419 | + $internalPath = $mount->getInternalPath($path); |
|
1420 | + if ($storage) { |
|
1421 | + $cache = $storage->getCache($internalPath); |
|
1422 | + $user = \OC_User::getUser(); |
|
1423 | + |
|
1424 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1425 | + |
|
1426 | + if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
1427 | + return []; |
|
1428 | + } |
|
1429 | + |
|
1430 | + $folderId = $data['fileid']; |
|
1431 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1432 | + |
|
1433 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1434 | + /** |
|
1435 | + * @var \OC\Files\FileInfo[] $files |
|
1436 | + */ |
|
1437 | + $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1438 | + if ($sharingDisabled) { |
|
1439 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1440 | + } |
|
1441 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1442 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1443 | + }, $contents); |
|
1444 | + |
|
1445 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1446 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1447 | + $dirLength = strlen($path); |
|
1448 | + foreach ($mounts as $mount) { |
|
1449 | + $mountPoint = $mount->getMountPoint(); |
|
1450 | + $subStorage = $mount->getStorage(); |
|
1451 | + if ($subStorage) { |
|
1452 | + $subCache = $subStorage->getCache(''); |
|
1453 | + |
|
1454 | + $rootEntry = $subCache->get(''); |
|
1455 | + if (!$rootEntry) { |
|
1456 | + $subScanner = $subStorage->getScanner(''); |
|
1457 | + try { |
|
1458 | + $subScanner->scanFile(''); |
|
1459 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1460 | + continue; |
|
1461 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
1462 | + continue; |
|
1463 | + } catch (\Exception $e) { |
|
1464 | + // sometimes when the storage is not available it can be any exception |
|
1465 | + \OC::$server->getLogger()->logException($e, [ |
|
1466 | + 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1467 | + 'level' => ILogger::ERROR, |
|
1468 | + 'app' => 'lib', |
|
1469 | + ]); |
|
1470 | + continue; |
|
1471 | + } |
|
1472 | + $rootEntry = $subCache->get(''); |
|
1473 | + } |
|
1474 | + |
|
1475 | + if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
1476 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1477 | + if ($pos = strpos($relativePath, '/')) { |
|
1478 | + //mountpoint inside subfolder add size to the correct folder |
|
1479 | + $entryName = substr($relativePath, 0, $pos); |
|
1480 | + foreach ($files as &$entry) { |
|
1481 | + if ($entry->getName() === $entryName) { |
|
1482 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
1483 | + } |
|
1484 | + } |
|
1485 | + } else { //mountpoint in this folder, add an entry for it |
|
1486 | + $rootEntry['name'] = $relativePath; |
|
1487 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1488 | + $permissions = $rootEntry['permissions']; |
|
1489 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1490 | + // for shared files/folders we use the permissions given by the owner |
|
1491 | + if ($mount instanceof MoveableMount) { |
|
1492 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1493 | + } else { |
|
1494 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1495 | + } |
|
1496 | + |
|
1497 | + //remove any existing entry with the same name |
|
1498 | + foreach ($files as $i => $file) { |
|
1499 | + if ($file['name'] === $rootEntry['name']) { |
|
1500 | + unset($files[$i]); |
|
1501 | + break; |
|
1502 | + } |
|
1503 | + } |
|
1504 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1505 | + |
|
1506 | + // if sharing was disabled for the user we remove the share permissions |
|
1507 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
1508 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1509 | + } |
|
1510 | + |
|
1511 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1512 | + $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1513 | + } |
|
1514 | + } |
|
1515 | + } |
|
1516 | + } |
|
1517 | + |
|
1518 | + if ($mimetype_filter) { |
|
1519 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1520 | + if (strpos($mimetype_filter, '/')) { |
|
1521 | + return $file->getMimetype() === $mimetype_filter; |
|
1522 | + } else { |
|
1523 | + return $file->getMimePart() === $mimetype_filter; |
|
1524 | + } |
|
1525 | + }); |
|
1526 | + } |
|
1527 | + |
|
1528 | + return $files; |
|
1529 | + } else { |
|
1530 | + return []; |
|
1531 | + } |
|
1532 | + } |
|
1533 | + |
|
1534 | + /** |
|
1535 | + * change file metadata |
|
1536 | + * |
|
1537 | + * @param string $path |
|
1538 | + * @param array|\OCP\Files\FileInfo $data |
|
1539 | + * @return int |
|
1540 | + * |
|
1541 | + * returns the fileid of the updated file |
|
1542 | + */ |
|
1543 | + public function putFileInfo($path, $data) { |
|
1544 | + $this->assertPathLength($path); |
|
1545 | + if ($data instanceof FileInfo) { |
|
1546 | + $data = $data->getData(); |
|
1547 | + } |
|
1548 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1549 | + /** |
|
1550 | + * @var \OC\Files\Storage\Storage $storage |
|
1551 | + * @var string $internalPath |
|
1552 | + */ |
|
1553 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
1554 | + if ($storage) { |
|
1555 | + $cache = $storage->getCache($path); |
|
1556 | + |
|
1557 | + if (!$cache->inCache($internalPath)) { |
|
1558 | + $scanner = $storage->getScanner($internalPath); |
|
1559 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1560 | + } |
|
1561 | + |
|
1562 | + return $cache->put($internalPath, $data); |
|
1563 | + } else { |
|
1564 | + return -1; |
|
1565 | + } |
|
1566 | + } |
|
1567 | + |
|
1568 | + /** |
|
1569 | + * search for files with the name matching $query |
|
1570 | + * |
|
1571 | + * @param string $query |
|
1572 | + * @return FileInfo[] |
|
1573 | + */ |
|
1574 | + public function search($query) { |
|
1575 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
1576 | + } |
|
1577 | + |
|
1578 | + /** |
|
1579 | + * search for files with the name matching $query |
|
1580 | + * |
|
1581 | + * @param string $query |
|
1582 | + * @return FileInfo[] |
|
1583 | + */ |
|
1584 | + public function searchRaw($query) { |
|
1585 | + return $this->searchCommon('search', array($query)); |
|
1586 | + } |
|
1587 | + |
|
1588 | + /** |
|
1589 | + * search for files by mimetype |
|
1590 | + * |
|
1591 | + * @param string $mimetype |
|
1592 | + * @return FileInfo[] |
|
1593 | + */ |
|
1594 | + public function searchByMime($mimetype) { |
|
1595 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
1596 | + } |
|
1597 | + |
|
1598 | + /** |
|
1599 | + * search for files by tag |
|
1600 | + * |
|
1601 | + * @param string|int $tag name or tag id |
|
1602 | + * @param string $userId owner of the tags |
|
1603 | + * @return FileInfo[] |
|
1604 | + */ |
|
1605 | + public function searchByTag($tag, $userId) { |
|
1606 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
1607 | + } |
|
1608 | + |
|
1609 | + /** |
|
1610 | + * @param string $method cache method |
|
1611 | + * @param array $args |
|
1612 | + * @return FileInfo[] |
|
1613 | + */ |
|
1614 | + private function searchCommon($method, $args) { |
|
1615 | + $files = array(); |
|
1616 | + $rootLength = strlen($this->fakeRoot); |
|
1617 | + |
|
1618 | + $mount = $this->getMount(''); |
|
1619 | + $mountPoint = $mount->getMountPoint(); |
|
1620 | + $storage = $mount->getStorage(); |
|
1621 | + if ($storage) { |
|
1622 | + $cache = $storage->getCache(''); |
|
1623 | + |
|
1624 | + $results = call_user_func_array(array($cache, $method), $args); |
|
1625 | + foreach ($results as $result) { |
|
1626 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1627 | + $internalPath = $result['path']; |
|
1628 | + $path = $mountPoint . $result['path']; |
|
1629 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1630 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1631 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1632 | + } |
|
1633 | + } |
|
1634 | + |
|
1635 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1636 | + foreach ($mounts as $mount) { |
|
1637 | + $mountPoint = $mount->getMountPoint(); |
|
1638 | + $storage = $mount->getStorage(); |
|
1639 | + if ($storage) { |
|
1640 | + $cache = $storage->getCache(''); |
|
1641 | + |
|
1642 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1643 | + $results = call_user_func_array(array($cache, $method), $args); |
|
1644 | + if ($results) { |
|
1645 | + foreach ($results as $result) { |
|
1646 | + $internalPath = $result['path']; |
|
1647 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1648 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
1649 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1650 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1651 | + } |
|
1652 | + } |
|
1653 | + } |
|
1654 | + } |
|
1655 | + } |
|
1656 | + return $files; |
|
1657 | + } |
|
1658 | + |
|
1659 | + /** |
|
1660 | + * Get the owner for a file or folder |
|
1661 | + * |
|
1662 | + * @param string $path |
|
1663 | + * @return string the user id of the owner |
|
1664 | + * @throws NotFoundException |
|
1665 | + */ |
|
1666 | + public function getOwner($path) { |
|
1667 | + $info = $this->getFileInfo($path); |
|
1668 | + if (!$info) { |
|
1669 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1670 | + } |
|
1671 | + return $info->getOwner()->getUID(); |
|
1672 | + } |
|
1673 | + |
|
1674 | + /** |
|
1675 | + * get the ETag for a file or folder |
|
1676 | + * |
|
1677 | + * @param string $path |
|
1678 | + * @return string |
|
1679 | + */ |
|
1680 | + public function getETag($path) { |
|
1681 | + /** |
|
1682 | + * @var Storage\Storage $storage |
|
1683 | + * @var string $internalPath |
|
1684 | + */ |
|
1685 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
1686 | + if ($storage) { |
|
1687 | + return $storage->getETag($internalPath); |
|
1688 | + } else { |
|
1689 | + return null; |
|
1690 | + } |
|
1691 | + } |
|
1692 | + |
|
1693 | + /** |
|
1694 | + * Get the path of a file by id, relative to the view |
|
1695 | + * |
|
1696 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
1697 | + * |
|
1698 | + * @param int $id |
|
1699 | + * @throws NotFoundException |
|
1700 | + * @return string |
|
1701 | + */ |
|
1702 | + public function getPath($id) { |
|
1703 | + $id = (int)$id; |
|
1704 | + $manager = Filesystem::getMountManager(); |
|
1705 | + $mounts = $manager->findIn($this->fakeRoot); |
|
1706 | + $mounts[] = $manager->find($this->fakeRoot); |
|
1707 | + // reverse the array so we start with the storage this view is in |
|
1708 | + // which is the most likely to contain the file we're looking for |
|
1709 | + $mounts = array_reverse($mounts); |
|
1710 | + foreach ($mounts as $mount) { |
|
1711 | + /** |
|
1712 | + * @var \OC\Files\Mount\MountPoint $mount |
|
1713 | + */ |
|
1714 | + if ($mount->getStorage()) { |
|
1715 | + $cache = $mount->getStorage()->getCache(); |
|
1716 | + $internalPath = $cache->getPathById($id); |
|
1717 | + if (is_string($internalPath)) { |
|
1718 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
1719 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1720 | + return $path; |
|
1721 | + } |
|
1722 | + } |
|
1723 | + } |
|
1724 | + } |
|
1725 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1726 | + } |
|
1727 | + |
|
1728 | + /** |
|
1729 | + * @param string $path |
|
1730 | + * @throws InvalidPathException |
|
1731 | + */ |
|
1732 | + private function assertPathLength($path) { |
|
1733 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1734 | + // Check for the string length - performed using isset() instead of strlen() |
|
1735 | + // because isset() is about 5x-40x faster. |
|
1736 | + if (isset($path[$maxLen])) { |
|
1737 | + $pathLen = strlen($path); |
|
1738 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1739 | + } |
|
1740 | + } |
|
1741 | + |
|
1742 | + /** |
|
1743 | + * check if it is allowed to move a mount point to a given target. |
|
1744 | + * It is not allowed to move a mount point into a different mount point or |
|
1745 | + * into an already shared folder |
|
1746 | + * |
|
1747 | + * @param string $target path |
|
1748 | + * @return boolean |
|
1749 | + */ |
|
1750 | + private function isTargetAllowed($target) { |
|
1751 | + |
|
1752 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
1753 | + if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
1754 | + \OCP\Util::writeLog('files', |
|
1755 | + 'It is not allowed to move one mount point into another one', |
|
1756 | + ILogger::DEBUG); |
|
1757 | + return false; |
|
1758 | + } |
|
1759 | + |
|
1760 | + // note: cannot use the view because the target is already locked |
|
1761 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1762 | + if ($fileId === -1) { |
|
1763 | + // target might not exist, need to check parent instead |
|
1764 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1765 | + } |
|
1766 | + |
|
1767 | + // check if any of the parents were shared by the current owner (include collections) |
|
1768 | + $shares = \OCP\Share::getItemShared( |
|
1769 | + 'folder', |
|
1770 | + $fileId, |
|
1771 | + \OCP\Share::FORMAT_NONE, |
|
1772 | + null, |
|
1773 | + true |
|
1774 | + ); |
|
1775 | + |
|
1776 | + if (count($shares) > 0) { |
|
1777 | + \OCP\Util::writeLog('files', |
|
1778 | + 'It is not allowed to move one mount point into a shared folder', |
|
1779 | + ILogger::DEBUG); |
|
1780 | + return false; |
|
1781 | + } |
|
1782 | + |
|
1783 | + return true; |
|
1784 | + } |
|
1785 | + |
|
1786 | + /** |
|
1787 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1788 | + * |
|
1789 | + * @param string $path |
|
1790 | + * @return \OCP\Files\FileInfo |
|
1791 | + */ |
|
1792 | + private function getPartFileInfo($path) { |
|
1793 | + $mount = $this->getMount($path); |
|
1794 | + $storage = $mount->getStorage(); |
|
1795 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1796 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1797 | + return new FileInfo( |
|
1798 | + $this->getAbsolutePath($path), |
|
1799 | + $storage, |
|
1800 | + $internalPath, |
|
1801 | + [ |
|
1802 | + 'fileid' => null, |
|
1803 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
1804 | + 'name' => basename($path), |
|
1805 | + 'etag' => null, |
|
1806 | + 'size' => $storage->filesize($internalPath), |
|
1807 | + 'mtime' => $storage->filemtime($internalPath), |
|
1808 | + 'encrypted' => false, |
|
1809 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1810 | + ], |
|
1811 | + $mount, |
|
1812 | + $owner |
|
1813 | + ); |
|
1814 | + } |
|
1815 | + |
|
1816 | + /** |
|
1817 | + * @param string $path |
|
1818 | + * @param string $fileName |
|
1819 | + * @throws InvalidPathException |
|
1820 | + */ |
|
1821 | + public function verifyPath($path, $fileName) { |
|
1822 | + try { |
|
1823 | + /** @type \OCP\Files\Storage $storage */ |
|
1824 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
1825 | + $storage->verifyPath($internalPath, $fileName); |
|
1826 | + } catch (ReservedWordException $ex) { |
|
1827 | + $l = \OC::$server->getL10N('lib'); |
|
1828 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1829 | + } catch (InvalidCharacterInPathException $ex) { |
|
1830 | + $l = \OC::$server->getL10N('lib'); |
|
1831 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1832 | + } catch (FileNameTooLongException $ex) { |
|
1833 | + $l = \OC::$server->getL10N('lib'); |
|
1834 | + throw new InvalidPathException($l->t('File name is too long')); |
|
1835 | + } catch (InvalidDirectoryException $ex) { |
|
1836 | + $l = \OC::$server->getL10N('lib'); |
|
1837 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1838 | + } catch (EmptyFileNameException $ex) { |
|
1839 | + $l = \OC::$server->getL10N('lib'); |
|
1840 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1841 | + } |
|
1842 | + } |
|
1843 | + |
|
1844 | + /** |
|
1845 | + * get all parent folders of $path |
|
1846 | + * |
|
1847 | + * @param string $path |
|
1848 | + * @return string[] |
|
1849 | + */ |
|
1850 | + private function getParents($path) { |
|
1851 | + $path = trim($path, '/'); |
|
1852 | + if (!$path) { |
|
1853 | + return []; |
|
1854 | + } |
|
1855 | + |
|
1856 | + $parts = explode('/', $path); |
|
1857 | + |
|
1858 | + // remove the single file |
|
1859 | + array_pop($parts); |
|
1860 | + $result = array('/'); |
|
1861 | + $resultPath = ''; |
|
1862 | + foreach ($parts as $part) { |
|
1863 | + if ($part) { |
|
1864 | + $resultPath .= '/' . $part; |
|
1865 | + $result[] = $resultPath; |
|
1866 | + } |
|
1867 | + } |
|
1868 | + return $result; |
|
1869 | + } |
|
1870 | + |
|
1871 | + /** |
|
1872 | + * Returns the mount point for which to lock |
|
1873 | + * |
|
1874 | + * @param string $absolutePath absolute path |
|
1875 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
1876 | + * is mounted directly on the given path, false otherwise |
|
1877 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
1878 | + */ |
|
1879 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1880 | + $results = []; |
|
1881 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1882 | + if (!$mount) { |
|
1883 | + return $results; |
|
1884 | + } |
|
1885 | + |
|
1886 | + if ($useParentMount) { |
|
1887 | + // find out if something is mounted directly on the path |
|
1888 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
1889 | + if ($internalPath === '') { |
|
1890 | + // resolve the parent mount instead |
|
1891 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1892 | + } |
|
1893 | + } |
|
1894 | + |
|
1895 | + return $mount; |
|
1896 | + } |
|
1897 | + |
|
1898 | + /** |
|
1899 | + * Lock the given path |
|
1900 | + * |
|
1901 | + * @param string $path the path of the file to lock, relative to the view |
|
1902 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1903 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1904 | + * |
|
1905 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1906 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
1907 | + */ |
|
1908 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
1909 | + $absolutePath = $this->getAbsolutePath($path); |
|
1910 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1911 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1912 | + return false; |
|
1913 | + } |
|
1914 | + |
|
1915 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1916 | + if ($mount) { |
|
1917 | + try { |
|
1918 | + $storage = $mount->getStorage(); |
|
1919 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1920 | + $storage->acquireLock( |
|
1921 | + $mount->getInternalPath($absolutePath), |
|
1922 | + $type, |
|
1923 | + $this->lockingProvider |
|
1924 | + ); |
|
1925 | + } |
|
1926 | + } catch (\OCP\Lock\LockedException $e) { |
|
1927 | + // rethrow with the a human-readable path |
|
1928 | + throw new \OCP\Lock\LockedException( |
|
1929 | + $this->getPathRelativeToFiles($absolutePath), |
|
1930 | + $e |
|
1931 | + ); |
|
1932 | + } |
|
1933 | + } |
|
1934 | + |
|
1935 | + return true; |
|
1936 | + } |
|
1937 | + |
|
1938 | + /** |
|
1939 | + * Change the lock type |
|
1940 | + * |
|
1941 | + * @param string $path the path of the file to lock, relative to the view |
|
1942 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1943 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1944 | + * |
|
1945 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1946 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
1947 | + */ |
|
1948 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
1949 | + $path = Filesystem::normalizePath($path); |
|
1950 | + $absolutePath = $this->getAbsolutePath($path); |
|
1951 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1952 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1953 | + return false; |
|
1954 | + } |
|
1955 | + |
|
1956 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1957 | + if ($mount) { |
|
1958 | + try { |
|
1959 | + $storage = $mount->getStorage(); |
|
1960 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1961 | + $storage->changeLock( |
|
1962 | + $mount->getInternalPath($absolutePath), |
|
1963 | + $type, |
|
1964 | + $this->lockingProvider |
|
1965 | + ); |
|
1966 | + } |
|
1967 | + } catch (\OCP\Lock\LockedException $e) { |
|
1968 | + try { |
|
1969 | + // rethrow with the a human-readable path |
|
1970 | + throw new \OCP\Lock\LockedException( |
|
1971 | + $this->getPathRelativeToFiles($absolutePath), |
|
1972 | + $e |
|
1973 | + ); |
|
1974 | + } catch (\InvalidArgumentException $e) { |
|
1975 | + throw new \OCP\Lock\LockedException( |
|
1976 | + $absolutePath, |
|
1977 | + $e |
|
1978 | + ); |
|
1979 | + } |
|
1980 | + } |
|
1981 | + } |
|
1982 | + |
|
1983 | + return true; |
|
1984 | + } |
|
1985 | + |
|
1986 | + /** |
|
1987 | + * Unlock the given path |
|
1988 | + * |
|
1989 | + * @param string $path the path of the file to unlock, relative to the view |
|
1990 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1991 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1992 | + * |
|
1993 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1994 | + */ |
|
1995 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
1996 | + $absolutePath = $this->getAbsolutePath($path); |
|
1997 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1998 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1999 | + return false; |
|
2000 | + } |
|
2001 | + |
|
2002 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2003 | + if ($mount) { |
|
2004 | + $storage = $mount->getStorage(); |
|
2005 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2006 | + $storage->releaseLock( |
|
2007 | + $mount->getInternalPath($absolutePath), |
|
2008 | + $type, |
|
2009 | + $this->lockingProvider |
|
2010 | + ); |
|
2011 | + } |
|
2012 | + } |
|
2013 | + |
|
2014 | + return true; |
|
2015 | + } |
|
2016 | + |
|
2017 | + /** |
|
2018 | + * Lock a path and all its parents up to the root of the view |
|
2019 | + * |
|
2020 | + * @param string $path the path of the file to lock relative to the view |
|
2021 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2022 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2023 | + * |
|
2024 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2025 | + */ |
|
2026 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
2027 | + $absolutePath = $this->getAbsolutePath($path); |
|
2028 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2029 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2030 | + return false; |
|
2031 | + } |
|
2032 | + |
|
2033 | + $this->lockPath($path, $type, $lockMountPoint); |
|
2034 | + |
|
2035 | + $parents = $this->getParents($path); |
|
2036 | + foreach ($parents as $parent) { |
|
2037 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2038 | + } |
|
2039 | + |
|
2040 | + return true; |
|
2041 | + } |
|
2042 | + |
|
2043 | + /** |
|
2044 | + * Unlock a path and all its parents up to the root of the view |
|
2045 | + * |
|
2046 | + * @param string $path the path of the file to lock relative to the view |
|
2047 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2048 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2049 | + * |
|
2050 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2051 | + */ |
|
2052 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2053 | + $absolutePath = $this->getAbsolutePath($path); |
|
2054 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2055 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2056 | + return false; |
|
2057 | + } |
|
2058 | + |
|
2059 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
2060 | + |
|
2061 | + $parents = $this->getParents($path); |
|
2062 | + foreach ($parents as $parent) { |
|
2063 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2064 | + } |
|
2065 | + |
|
2066 | + return true; |
|
2067 | + } |
|
2068 | + |
|
2069 | + /** |
|
2070 | + * Only lock files in data/user/files/ |
|
2071 | + * |
|
2072 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2073 | + * @return bool |
|
2074 | + */ |
|
2075 | + protected function shouldLockFile($path) { |
|
2076 | + $path = Filesystem::normalizePath($path); |
|
2077 | + |
|
2078 | + $pathSegments = explode('/', $path); |
|
2079 | + if (isset($pathSegments[2])) { |
|
2080 | + // E.g.: /username/files/path-to-file |
|
2081 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2082 | + } |
|
2083 | + |
|
2084 | + return strpos($path, '/appdata_') !== 0; |
|
2085 | + } |
|
2086 | + |
|
2087 | + /** |
|
2088 | + * Shortens the given absolute path to be relative to |
|
2089 | + * "$user/files". |
|
2090 | + * |
|
2091 | + * @param string $absolutePath absolute path which is under "files" |
|
2092 | + * |
|
2093 | + * @return string path relative to "files" with trimmed slashes or null |
|
2094 | + * if the path was NOT relative to files |
|
2095 | + * |
|
2096 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
2097 | + * @since 8.1.0 |
|
2098 | + */ |
|
2099 | + public function getPathRelativeToFiles($absolutePath) { |
|
2100 | + $path = Filesystem::normalizePath($absolutePath); |
|
2101 | + $parts = explode('/', trim($path, '/'), 3); |
|
2102 | + // "$user", "files", "path/to/dir" |
|
2103 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2104 | + $this->logger->error( |
|
2105 | + '$absolutePath must be relative to "files", value is "%s"', |
|
2106 | + [ |
|
2107 | + $absolutePath |
|
2108 | + ] |
|
2109 | + ); |
|
2110 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2111 | + } |
|
2112 | + if (isset($parts[2])) { |
|
2113 | + return $parts[2]; |
|
2114 | + } |
|
2115 | + return ''; |
|
2116 | + } |
|
2117 | + |
|
2118 | + /** |
|
2119 | + * @param string $filename |
|
2120 | + * @return array |
|
2121 | + * @throws \OC\User\NoUserException |
|
2122 | + * @throws NotFoundException |
|
2123 | + */ |
|
2124 | + public function getUidAndFilename($filename) { |
|
2125 | + $info = $this->getFileInfo($filename); |
|
2126 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
2127 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2128 | + } |
|
2129 | + $uid = $info->getOwner()->getUID(); |
|
2130 | + if ($uid != \OCP\User::getUser()) { |
|
2131 | + Filesystem::initMountPoints($uid); |
|
2132 | + $ownerView = new View('/' . $uid . '/files'); |
|
2133 | + try { |
|
2134 | + $filename = $ownerView->getPath($info['fileid']); |
|
2135 | + } catch (NotFoundException $e) { |
|
2136 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2137 | + } |
|
2138 | + } |
|
2139 | + return [$uid, $filename]; |
|
2140 | + } |
|
2141 | + |
|
2142 | + /** |
|
2143 | + * Creates parent non-existing folders |
|
2144 | + * |
|
2145 | + * @param string $filePath |
|
2146 | + * @return bool |
|
2147 | + */ |
|
2148 | + private function createParentDirectories($filePath) { |
|
2149 | + $directoryParts = explode('/', $filePath); |
|
2150 | + $directoryParts = array_filter($directoryParts); |
|
2151 | + foreach ($directoryParts as $key => $part) { |
|
2152 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2153 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
2154 | + if ($this->is_file($currentPath)) { |
|
2155 | + return false; |
|
2156 | + } |
|
2157 | + if (!$this->file_exists($currentPath)) { |
|
2158 | + $this->mkdir($currentPath); |
|
2159 | + } |
|
2160 | + } |
|
2161 | + |
|
2162 | + return true; |
|
2163 | + } |
|
2164 | 2164 | } |
@@ -127,9 +127,9 @@ discard block |
||
127 | 127 | $path = '/'; |
128 | 128 | } |
129 | 129 | if ($path[0] !== '/') { |
130 | - $path = '/' . $path; |
|
130 | + $path = '/'.$path; |
|
131 | 131 | } |
132 | - return $this->fakeRoot . $path; |
|
132 | + return $this->fakeRoot.$path; |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | public function chroot($fakeRoot) { |
142 | 142 | if (!$fakeRoot == '') { |
143 | 143 | if ($fakeRoot[0] !== '/') { |
144 | - $fakeRoot = '/' . $fakeRoot; |
|
144 | + $fakeRoot = '/'.$fakeRoot; |
|
145 | 145 | } |
146 | 146 | } |
147 | 147 | $this->fakeRoot = $fakeRoot; |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | } |
174 | 174 | |
175 | 175 | // missing slashes can cause wrong matches! |
176 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
176 | + $root = rtrim($this->fakeRoot, '/').'/'; |
|
177 | 177 | |
178 | 178 | if (strpos($path, $root) !== 0) { |
179 | 179 | return null; |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | if ($mount instanceof MoveableMount) { |
280 | 280 | // cut of /user/files to get the relative path to data/user/files |
281 | 281 | $pathParts = explode('/', $path, 4); |
282 | - $relPath = '/' . $pathParts[3]; |
|
282 | + $relPath = '/'.$pathParts[3]; |
|
283 | 283 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
284 | 284 | \OC_Hook::emit( |
285 | 285 | Filesystem::CLASSNAME, "umount", |
@@ -699,7 +699,7 @@ discard block |
||
699 | 699 | } |
700 | 700 | $postFix = (substr($path, -1) === '/') ? '/' : ''; |
701 | 701 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
702 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
702 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); |
|
703 | 703 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
704 | 704 | return $this->removeMount($mount, $absolutePath); |
705 | 705 | } |
@@ -819,7 +819,7 @@ discard block |
||
819 | 819 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
820 | 820 | } |
821 | 821 | } |
822 | - } catch(\Exception $e) { |
|
822 | + } catch (\Exception $e) { |
|
823 | 823 | throw $e; |
824 | 824 | } finally { |
825 | 825 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -843,7 +843,7 @@ discard block |
||
843 | 843 | } |
844 | 844 | } |
845 | 845 | } |
846 | - } catch(\Exception $e) { |
|
846 | + } catch (\Exception $e) { |
|
847 | 847 | throw $e; |
848 | 848 | } finally { |
849 | 849 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -976,7 +976,7 @@ discard block |
||
976 | 976 | $hooks[] = 'write'; |
977 | 977 | break; |
978 | 978 | default: |
979 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
979 | + \OCP\Util::writeLog('core', 'invalid mode ('.$mode.') for '.$path, ILogger::ERROR); |
|
980 | 980 | } |
981 | 981 | |
982 | 982 | if ($mode !== 'r' && $mode !== 'w') { |
@@ -1080,7 +1080,7 @@ discard block |
||
1080 | 1080 | array(Filesystem::signal_param_path => $this->getHookPath($path)) |
1081 | 1081 | ); |
1082 | 1082 | } |
1083 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1083 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
1084 | 1084 | if ($storage) { |
1085 | 1085 | return $storage->hash($type, $internalPath, $raw); |
1086 | 1086 | } |
@@ -1134,7 +1134,7 @@ discard block |
||
1134 | 1134 | |
1135 | 1135 | $run = $this->runHooks($hooks, $path); |
1136 | 1136 | /** @var \OC\Files\Storage\Storage $storage */ |
1137 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1137 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
1138 | 1138 | if ($run and $storage) { |
1139 | 1139 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
1140 | 1140 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
@@ -1173,7 +1173,7 @@ discard block |
||
1173 | 1173 | $unlockLater = true; |
1174 | 1174 | // make sure our unlocking callback will still be called if connection is aborted |
1175 | 1175 | ignore_user_abort(true); |
1176 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1176 | + $result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { |
|
1177 | 1177 | if (in_array('write', $hooks)) { |
1178 | 1178 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
1179 | 1179 | } else if (in_array('read', $hooks)) { |
@@ -1234,7 +1234,7 @@ discard block |
||
1234 | 1234 | return true; |
1235 | 1235 | } |
1236 | 1236 | |
1237 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1237 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); |
|
1238 | 1238 | } |
1239 | 1239 | |
1240 | 1240 | /** |
@@ -1253,7 +1253,7 @@ discard block |
||
1253 | 1253 | if ($hook != 'read') { |
1254 | 1254 | \OC_Hook::emit( |
1255 | 1255 | Filesystem::CLASSNAME, |
1256 | - $prefix . $hook, |
|
1256 | + $prefix.$hook, |
|
1257 | 1257 | array( |
1258 | 1258 | Filesystem::signal_param_run => &$run, |
1259 | 1259 | Filesystem::signal_param_path => $path |
@@ -1262,7 +1262,7 @@ discard block |
||
1262 | 1262 | } elseif (!$post) { |
1263 | 1263 | \OC_Hook::emit( |
1264 | 1264 | Filesystem::CLASSNAME, |
1265 | - $prefix . $hook, |
|
1265 | + $prefix.$hook, |
|
1266 | 1266 | array( |
1267 | 1267 | Filesystem::signal_param_path => $path |
1268 | 1268 | ) |
@@ -1357,7 +1357,7 @@ discard block |
||
1357 | 1357 | return $this->getPartFileInfo($path); |
1358 | 1358 | } |
1359 | 1359 | $relativePath = $path; |
1360 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1360 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
1361 | 1361 | |
1362 | 1362 | $mount = Filesystem::getMountManager()->find($path); |
1363 | 1363 | if (!$mount) { |
@@ -1384,7 +1384,7 @@ discard block |
||
1384 | 1384 | //add the sizes of other mount points to the folder |
1385 | 1385 | $extOnly = ($includeMountPoints === 'ext'); |
1386 | 1386 | $mounts = Filesystem::getMountManager()->findIn($path); |
1387 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1387 | + $info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { |
|
1388 | 1388 | $subStorage = $mount->getStorage(); |
1389 | 1389 | return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
1390 | 1390 | })); |
@@ -1434,12 +1434,12 @@ discard block |
||
1434 | 1434 | /** |
1435 | 1435 | * @var \OC\Files\FileInfo[] $files |
1436 | 1436 | */ |
1437 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1437 | + $files = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1438 | 1438 | if ($sharingDisabled) { |
1439 | 1439 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
1440 | 1440 | } |
1441 | 1441 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
1442 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1442 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1443 | 1443 | }, $contents); |
1444 | 1444 | |
1445 | 1445 | //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
@@ -1463,7 +1463,7 @@ discard block |
||
1463 | 1463 | } catch (\Exception $e) { |
1464 | 1464 | // sometimes when the storage is not available it can be any exception |
1465 | 1465 | \OC::$server->getLogger()->logException($e, [ |
1466 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1466 | + 'message' => 'Exception while scanning storage "'.$subStorage->getId().'"', |
|
1467 | 1467 | 'level' => ILogger::ERROR, |
1468 | 1468 | 'app' => 'lib', |
1469 | 1469 | ]); |
@@ -1501,7 +1501,7 @@ discard block |
||
1501 | 1501 | break; |
1502 | 1502 | } |
1503 | 1503 | } |
1504 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1504 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1505 | 1505 | |
1506 | 1506 | // if sharing was disabled for the user we remove the share permissions |
1507 | 1507 | if (\OCP\Util::isSharingDisabledForUser()) { |
@@ -1509,14 +1509,14 @@ discard block |
||
1509 | 1509 | } |
1510 | 1510 | |
1511 | 1511 | $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
1512 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1512 | + $files[] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1513 | 1513 | } |
1514 | 1514 | } |
1515 | 1515 | } |
1516 | 1516 | } |
1517 | 1517 | |
1518 | 1518 | if ($mimetype_filter) { |
1519 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1519 | + $files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { |
|
1520 | 1520 | if (strpos($mimetype_filter, '/')) { |
1521 | 1521 | return $file->getMimetype() === $mimetype_filter; |
1522 | 1522 | } else { |
@@ -1545,7 +1545,7 @@ discard block |
||
1545 | 1545 | if ($data instanceof FileInfo) { |
1546 | 1546 | $data = $data->getData(); |
1547 | 1547 | } |
1548 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1548 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
1549 | 1549 | /** |
1550 | 1550 | * @var \OC\Files\Storage\Storage $storage |
1551 | 1551 | * @var string $internalPath |
@@ -1572,7 +1572,7 @@ discard block |
||
1572 | 1572 | * @return FileInfo[] |
1573 | 1573 | */ |
1574 | 1574 | public function search($query) { |
1575 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
1575 | + return $this->searchCommon('search', array('%'.$query.'%')); |
|
1576 | 1576 | } |
1577 | 1577 | |
1578 | 1578 | /** |
@@ -1623,10 +1623,10 @@ discard block |
||
1623 | 1623 | |
1624 | 1624 | $results = call_user_func_array(array($cache, $method), $args); |
1625 | 1625 | foreach ($results as $result) { |
1626 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1626 | + if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { |
|
1627 | 1627 | $internalPath = $result['path']; |
1628 | - $path = $mountPoint . $result['path']; |
|
1629 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1628 | + $path = $mountPoint.$result['path']; |
|
1629 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); |
|
1630 | 1630 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
1631 | 1631 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
1632 | 1632 | } |
@@ -1644,8 +1644,8 @@ discard block |
||
1644 | 1644 | if ($results) { |
1645 | 1645 | foreach ($results as $result) { |
1646 | 1646 | $internalPath = $result['path']; |
1647 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1648 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
1647 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); |
|
1648 | + $path = rtrim($mountPoint.$internalPath, '/'); |
|
1649 | 1649 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
1650 | 1650 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
1651 | 1651 | } |
@@ -1666,7 +1666,7 @@ discard block |
||
1666 | 1666 | public function getOwner($path) { |
1667 | 1667 | $info = $this->getFileInfo($path); |
1668 | 1668 | if (!$info) { |
1669 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1669 | + throw new NotFoundException($path.' not found while trying to get owner'); |
|
1670 | 1670 | } |
1671 | 1671 | return $info->getOwner()->getUID(); |
1672 | 1672 | } |
@@ -1700,7 +1700,7 @@ discard block |
||
1700 | 1700 | * @return string |
1701 | 1701 | */ |
1702 | 1702 | public function getPath($id) { |
1703 | - $id = (int)$id; |
|
1703 | + $id = (int) $id; |
|
1704 | 1704 | $manager = Filesystem::getMountManager(); |
1705 | 1705 | $mounts = $manager->findIn($this->fakeRoot); |
1706 | 1706 | $mounts[] = $manager->find($this->fakeRoot); |
@@ -1715,7 +1715,7 @@ discard block |
||
1715 | 1715 | $cache = $mount->getStorage()->getCache(); |
1716 | 1716 | $internalPath = $cache->getPathById($id); |
1717 | 1717 | if (is_string($internalPath)) { |
1718 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
1718 | + $fullPath = $mount->getMountPoint().$internalPath; |
|
1719 | 1719 | if (!is_null($path = $this->getRelativePath($fullPath))) { |
1720 | 1720 | return $path; |
1721 | 1721 | } |
@@ -1758,10 +1758,10 @@ discard block |
||
1758 | 1758 | } |
1759 | 1759 | |
1760 | 1760 | // note: cannot use the view because the target is already locked |
1761 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1761 | + $fileId = (int) $targetStorage->getCache()->getId($targetInternalPath); |
|
1762 | 1762 | if ($fileId === -1) { |
1763 | 1763 | // target might not exist, need to check parent instead |
1764 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1764 | + $fileId = (int) $targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1765 | 1765 | } |
1766 | 1766 | |
1767 | 1767 | // check if any of the parents were shared by the current owner (include collections) |
@@ -1861,7 +1861,7 @@ discard block |
||
1861 | 1861 | $resultPath = ''; |
1862 | 1862 | foreach ($parts as $part) { |
1863 | 1863 | if ($part) { |
1864 | - $resultPath .= '/' . $part; |
|
1864 | + $resultPath .= '/'.$part; |
|
1865 | 1865 | $result[] = $resultPath; |
1866 | 1866 | } |
1867 | 1867 | } |
@@ -2124,16 +2124,16 @@ discard block |
||
2124 | 2124 | public function getUidAndFilename($filename) { |
2125 | 2125 | $info = $this->getFileInfo($filename); |
2126 | 2126 | if (!$info instanceof \OCP\Files\FileInfo) { |
2127 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2127 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); |
|
2128 | 2128 | } |
2129 | 2129 | $uid = $info->getOwner()->getUID(); |
2130 | 2130 | if ($uid != \OCP\User::getUser()) { |
2131 | 2131 | Filesystem::initMountPoints($uid); |
2132 | - $ownerView = new View('/' . $uid . '/files'); |
|
2132 | + $ownerView = new View('/'.$uid.'/files'); |
|
2133 | 2133 | try { |
2134 | 2134 | $filename = $ownerView->getPath($info['fileid']); |
2135 | 2135 | } catch (NotFoundException $e) { |
2136 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2136 | + throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); |
|
2137 | 2137 | } |
2138 | 2138 | } |
2139 | 2139 | return [$uid, $filename]; |
@@ -2150,7 +2150,7 @@ discard block |
||
2150 | 2150 | $directoryParts = array_filter($directoryParts); |
2151 | 2151 | foreach ($directoryParts as $key => $part) { |
2152 | 2152 | $currentPathElements = array_slice($directoryParts, 0, $key); |
2153 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
2153 | + $currentPath = '/'.implode('/', $currentPathElements); |
|
2154 | 2154 | if ($this->is_file($currentPath)) { |
2155 | 2155 | return false; |
2156 | 2156 | } |
@@ -36,247 +36,247 @@ |
||
36 | 36 | use OCP\ILogger; |
37 | 37 | |
38 | 38 | class MountPoint implements IMountPoint { |
39 | - /** |
|
40 | - * @var \OC\Files\Storage\Storage $storage |
|
41 | - */ |
|
42 | - protected $storage = null; |
|
43 | - protected $class; |
|
44 | - protected $storageId; |
|
45 | - protected $rootId = null; |
|
39 | + /** |
|
40 | + * @var \OC\Files\Storage\Storage $storage |
|
41 | + */ |
|
42 | + protected $storage = null; |
|
43 | + protected $class; |
|
44 | + protected $storageId; |
|
45 | + protected $rootId = null; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Configuration options for the storage backend |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $arguments = array(); |
|
53 | - protected $mountPoint; |
|
47 | + /** |
|
48 | + * Configuration options for the storage backend |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $arguments = array(); |
|
53 | + protected $mountPoint; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Mount specific options |
|
57 | - * |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $mountOptions = array(); |
|
55 | + /** |
|
56 | + * Mount specific options |
|
57 | + * |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $mountOptions = array(); |
|
61 | 61 | |
62 | - /** |
|
63 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
64 | - */ |
|
65 | - private $loader; |
|
62 | + /** |
|
63 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
64 | + */ |
|
65 | + private $loader; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Specified whether the storage is invalid after failing to |
|
69 | - * instantiate it. |
|
70 | - * |
|
71 | - * @var bool |
|
72 | - */ |
|
73 | - private $invalidStorage = false; |
|
67 | + /** |
|
68 | + * Specified whether the storage is invalid after failing to |
|
69 | + * instantiate it. |
|
70 | + * |
|
71 | + * @var bool |
|
72 | + */ |
|
73 | + private $invalidStorage = false; |
|
74 | 74 | |
75 | - /** @var int|null */ |
|
76 | - protected $mountId; |
|
75 | + /** @var int|null */ |
|
76 | + protected $mountId; |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string|\OC\Files\Storage\Storage $storage |
|
80 | - * @param string $mountpoint |
|
81 | - * @param array $arguments (optional) configuration for the storage backend |
|
82 | - * @param \OCP\Files\Storage\IStorageFactory $loader |
|
83 | - * @param array $mountOptions mount specific options |
|
84 | - * @param int|null $mountId |
|
85 | - * @throws \Exception |
|
86 | - */ |
|
87 | - public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
|
88 | - if (is_null($arguments)) { |
|
89 | - $arguments = array(); |
|
90 | - } |
|
91 | - if (is_null($loader)) { |
|
92 | - $this->loader = new StorageFactory(); |
|
93 | - } else { |
|
94 | - $this->loader = $loader; |
|
95 | - } |
|
78 | + /** |
|
79 | + * @param string|\OC\Files\Storage\Storage $storage |
|
80 | + * @param string $mountpoint |
|
81 | + * @param array $arguments (optional) configuration for the storage backend |
|
82 | + * @param \OCP\Files\Storage\IStorageFactory $loader |
|
83 | + * @param array $mountOptions mount specific options |
|
84 | + * @param int|null $mountId |
|
85 | + * @throws \Exception |
|
86 | + */ |
|
87 | + public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
|
88 | + if (is_null($arguments)) { |
|
89 | + $arguments = array(); |
|
90 | + } |
|
91 | + if (is_null($loader)) { |
|
92 | + $this->loader = new StorageFactory(); |
|
93 | + } else { |
|
94 | + $this->loader = $loader; |
|
95 | + } |
|
96 | 96 | |
97 | - if (!is_null($mountOptions)) { |
|
98 | - $this->mountOptions = $mountOptions; |
|
99 | - } |
|
97 | + if (!is_null($mountOptions)) { |
|
98 | + $this->mountOptions = $mountOptions; |
|
99 | + } |
|
100 | 100 | |
101 | - $mountpoint = $this->formatPath($mountpoint); |
|
102 | - $this->mountPoint = $mountpoint; |
|
103 | - if ($storage instanceof Storage) { |
|
104 | - $this->class = get_class($storage); |
|
105 | - $this->storage = $this->loader->wrap($this, $storage); |
|
106 | - } else { |
|
107 | - // Update old classes to new namespace |
|
108 | - if (strpos($storage, 'OC_Filestorage_') !== false) { |
|
109 | - $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
110 | - } |
|
111 | - $this->class = $storage; |
|
112 | - $this->arguments = $arguments; |
|
113 | - } |
|
114 | - $this->mountId = $mountId; |
|
115 | - } |
|
101 | + $mountpoint = $this->formatPath($mountpoint); |
|
102 | + $this->mountPoint = $mountpoint; |
|
103 | + if ($storage instanceof Storage) { |
|
104 | + $this->class = get_class($storage); |
|
105 | + $this->storage = $this->loader->wrap($this, $storage); |
|
106 | + } else { |
|
107 | + // Update old classes to new namespace |
|
108 | + if (strpos($storage, 'OC_Filestorage_') !== false) { |
|
109 | + $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
110 | + } |
|
111 | + $this->class = $storage; |
|
112 | + $this->arguments = $arguments; |
|
113 | + } |
|
114 | + $this->mountId = $mountId; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * get complete path to the mount point, relative to data/ |
|
119 | - * |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public function getMountPoint() { |
|
123 | - return $this->mountPoint; |
|
124 | - } |
|
117 | + /** |
|
118 | + * get complete path to the mount point, relative to data/ |
|
119 | + * |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public function getMountPoint() { |
|
123 | + return $this->mountPoint; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Sets the mount point path, relative to data/ |
|
128 | - * |
|
129 | - * @param string $mountPoint new mount point |
|
130 | - */ |
|
131 | - public function setMountPoint($mountPoint) { |
|
132 | - $this->mountPoint = $this->formatPath($mountPoint); |
|
133 | - } |
|
126 | + /** |
|
127 | + * Sets the mount point path, relative to data/ |
|
128 | + * |
|
129 | + * @param string $mountPoint new mount point |
|
130 | + */ |
|
131 | + public function setMountPoint($mountPoint) { |
|
132 | + $this->mountPoint = $this->formatPath($mountPoint); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * create the storage that is mounted |
|
137 | - */ |
|
138 | - private function createStorage() { |
|
139 | - if ($this->invalidStorage) { |
|
140 | - return; |
|
141 | - } |
|
135 | + /** |
|
136 | + * create the storage that is mounted |
|
137 | + */ |
|
138 | + private function createStorage() { |
|
139 | + if ($this->invalidStorage) { |
|
140 | + return; |
|
141 | + } |
|
142 | 142 | |
143 | - if (class_exists($this->class)) { |
|
144 | - try { |
|
145 | - $class = $this->class; |
|
146 | - // prevent recursion by setting the storage before applying wrappers |
|
147 | - $this->storage = new $class($this->arguments); |
|
148 | - $this->storage = $this->loader->wrap($this, $this->storage); |
|
149 | - } catch (\Exception $exception) { |
|
150 | - $this->storage = null; |
|
151 | - $this->invalidStorage = true; |
|
152 | - if ($this->mountPoint === '/') { |
|
153 | - // the root storage could not be initialized, show the user! |
|
154 | - throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception); |
|
155 | - } else { |
|
156 | - \OC::$server->getLogger()->logException($exception, ['level' => ILogger::ERROR]); |
|
157 | - } |
|
158 | - return; |
|
159 | - } |
|
160 | - } else { |
|
161 | - \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
162 | - $this->invalidStorage = true; |
|
163 | - return; |
|
164 | - } |
|
165 | - } |
|
143 | + if (class_exists($this->class)) { |
|
144 | + try { |
|
145 | + $class = $this->class; |
|
146 | + // prevent recursion by setting the storage before applying wrappers |
|
147 | + $this->storage = new $class($this->arguments); |
|
148 | + $this->storage = $this->loader->wrap($this, $this->storage); |
|
149 | + } catch (\Exception $exception) { |
|
150 | + $this->storage = null; |
|
151 | + $this->invalidStorage = true; |
|
152 | + if ($this->mountPoint === '/') { |
|
153 | + // the root storage could not be initialized, show the user! |
|
154 | + throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception); |
|
155 | + } else { |
|
156 | + \OC::$server->getLogger()->logException($exception, ['level' => ILogger::ERROR]); |
|
157 | + } |
|
158 | + return; |
|
159 | + } |
|
160 | + } else { |
|
161 | + \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
162 | + $this->invalidStorage = true; |
|
163 | + return; |
|
164 | + } |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @return \OC\Files\Storage\Storage |
|
169 | - */ |
|
170 | - public function getStorage() { |
|
171 | - if (is_null($this->storage)) { |
|
172 | - $this->createStorage(); |
|
173 | - } |
|
174 | - return $this->storage; |
|
175 | - } |
|
167 | + /** |
|
168 | + * @return \OC\Files\Storage\Storage |
|
169 | + */ |
|
170 | + public function getStorage() { |
|
171 | + if (is_null($this->storage)) { |
|
172 | + $this->createStorage(); |
|
173 | + } |
|
174 | + return $this->storage; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - public function getStorageId() { |
|
181 | - if (!$this->storageId) { |
|
182 | - if (is_null($this->storage)) { |
|
183 | - $storage = $this->createStorage(); //FIXME: start using exceptions |
|
184 | - if (is_null($storage)) { |
|
185 | - return null; |
|
186 | - } |
|
177 | + /** |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + public function getStorageId() { |
|
181 | + if (!$this->storageId) { |
|
182 | + if (is_null($this->storage)) { |
|
183 | + $storage = $this->createStorage(); //FIXME: start using exceptions |
|
184 | + if (is_null($storage)) { |
|
185 | + return null; |
|
186 | + } |
|
187 | 187 | |
188 | - $this->storage = $storage; |
|
189 | - } |
|
190 | - $this->storageId = $this->storage->getId(); |
|
191 | - if (strlen($this->storageId) > 64) { |
|
192 | - $this->storageId = md5($this->storageId); |
|
193 | - } |
|
194 | - } |
|
195 | - return $this->storageId; |
|
196 | - } |
|
188 | + $this->storage = $storage; |
|
189 | + } |
|
190 | + $this->storageId = $this->storage->getId(); |
|
191 | + if (strlen($this->storageId) > 64) { |
|
192 | + $this->storageId = md5($this->storageId); |
|
193 | + } |
|
194 | + } |
|
195 | + return $this->storageId; |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * @return int |
|
200 | - */ |
|
201 | - public function getNumericStorageId() { |
|
202 | - return $this->getStorage()->getStorageCache()->getNumericId(); |
|
203 | - } |
|
198 | + /** |
|
199 | + * @return int |
|
200 | + */ |
|
201 | + public function getNumericStorageId() { |
|
202 | + return $this->getStorage()->getStorageCache()->getNumericId(); |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * @param string $path |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - public function getInternalPath($path) { |
|
210 | - $path = Filesystem::normalizePath($path, true, false, true); |
|
211 | - if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
212 | - $internalPath = ''; |
|
213 | - } else { |
|
214 | - $internalPath = substr($path, strlen($this->mountPoint)); |
|
215 | - } |
|
216 | - // substr returns false instead of an empty string, we always want a string |
|
217 | - return (string)$internalPath; |
|
218 | - } |
|
205 | + /** |
|
206 | + * @param string $path |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + public function getInternalPath($path) { |
|
210 | + $path = Filesystem::normalizePath($path, true, false, true); |
|
211 | + if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
212 | + $internalPath = ''; |
|
213 | + } else { |
|
214 | + $internalPath = substr($path, strlen($this->mountPoint)); |
|
215 | + } |
|
216 | + // substr returns false instead of an empty string, we always want a string |
|
217 | + return (string)$internalPath; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * @param string $path |
|
222 | - * @return string |
|
223 | - */ |
|
224 | - private function formatPath($path) { |
|
225 | - $path = Filesystem::normalizePath($path); |
|
226 | - if (strlen($path) > 1) { |
|
227 | - $path .= '/'; |
|
228 | - } |
|
229 | - return $path; |
|
230 | - } |
|
220 | + /** |
|
221 | + * @param string $path |
|
222 | + * @return string |
|
223 | + */ |
|
224 | + private function formatPath($path) { |
|
225 | + $path = Filesystem::normalizePath($path); |
|
226 | + if (strlen($path) > 1) { |
|
227 | + $path .= '/'; |
|
228 | + } |
|
229 | + return $path; |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * @param callable $wrapper |
|
234 | - */ |
|
235 | - public function wrapStorage($wrapper) { |
|
236 | - $storage = $this->getStorage(); |
|
237 | - // storage can be null if it couldn't be initialized |
|
238 | - if ($storage != null) { |
|
239 | - $this->storage = $wrapper($this->mountPoint, $storage, $this); |
|
240 | - } |
|
241 | - } |
|
232 | + /** |
|
233 | + * @param callable $wrapper |
|
234 | + */ |
|
235 | + public function wrapStorage($wrapper) { |
|
236 | + $storage = $this->getStorage(); |
|
237 | + // storage can be null if it couldn't be initialized |
|
238 | + if ($storage != null) { |
|
239 | + $this->storage = $wrapper($this->mountPoint, $storage, $this); |
|
240 | + } |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * Get a mount option |
|
245 | - * |
|
246 | - * @param string $name Name of the mount option to get |
|
247 | - * @param mixed $default Default value for the mount option |
|
248 | - * @return mixed |
|
249 | - */ |
|
250 | - public function getOption($name, $default) { |
|
251 | - return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
252 | - } |
|
243 | + /** |
|
244 | + * Get a mount option |
|
245 | + * |
|
246 | + * @param string $name Name of the mount option to get |
|
247 | + * @param mixed $default Default value for the mount option |
|
248 | + * @return mixed |
|
249 | + */ |
|
250 | + public function getOption($name, $default) { |
|
251 | + return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Get all options for the mount |
|
256 | - * |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - public function getOptions() { |
|
260 | - return $this->mountOptions; |
|
261 | - } |
|
254 | + /** |
|
255 | + * Get all options for the mount |
|
256 | + * |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + public function getOptions() { |
|
260 | + return $this->mountOptions; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * Get the file id of the root of the storage |
|
265 | - * |
|
266 | - * @return int |
|
267 | - */ |
|
268 | - public function getStorageRootId() { |
|
269 | - if (is_null($this->rootId)) { |
|
270 | - $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
271 | - } |
|
272 | - return $this->rootId; |
|
273 | - } |
|
263 | + /** |
|
264 | + * Get the file id of the root of the storage |
|
265 | + * |
|
266 | + * @return int |
|
267 | + */ |
|
268 | + public function getStorageRootId() { |
|
269 | + if (is_null($this->rootId)) { |
|
270 | + $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
271 | + } |
|
272 | + return $this->rootId; |
|
273 | + } |
|
274 | 274 | |
275 | - public function getMountId() { |
|
276 | - return $this->mountId; |
|
277 | - } |
|
275 | + public function getMountId() { |
|
276 | + return $this->mountId; |
|
277 | + } |
|
278 | 278 | |
279 | - public function getMountType() { |
|
280 | - return ''; |
|
281 | - } |
|
279 | + public function getMountType() { |
|
280 | + return ''; |
|
281 | + } |
|
282 | 282 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | } else { |
107 | 107 | // Update old classes to new namespace |
108 | 108 | if (strpos($storage, 'OC_Filestorage_') !== false) { |
109 | - $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
109 | + $storage = '\OC\Files\Storage\\'.substr($storage, 15); |
|
110 | 110 | } |
111 | 111 | $this->class = $storage; |
112 | 112 | $this->arguments = $arguments; |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | return; |
159 | 159 | } |
160 | 160 | } else { |
161 | - \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
161 | + \OCP\Util::writeLog('core', 'storage backend '.$this->class.' not found', ILogger::ERROR); |
|
162 | 162 | $this->invalidStorage = true; |
163 | 163 | return; |
164 | 164 | } |
@@ -208,13 +208,13 @@ discard block |
||
208 | 208 | */ |
209 | 209 | public function getInternalPath($path) { |
210 | 210 | $path = Filesystem::normalizePath($path, true, false, true); |
211 | - if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
211 | + if ($this->mountPoint === $path or $this->mountPoint.'/' === $path) { |
|
212 | 212 | $internalPath = ''; |
213 | 213 | } else { |
214 | 214 | $internalPath = substr($path, strlen($this->mountPoint)); |
215 | 215 | } |
216 | 216 | // substr returns false instead of an empty string, we always want a string |
217 | - return (string)$internalPath; |
|
217 | + return (string) $internalPath; |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | */ |
268 | 268 | public function getStorageRootId() { |
269 | 269 | if (is_null($this->rootId)) { |
270 | - $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
270 | + $this->rootId = (int) $this->getStorage()->getCache()->getId(''); |
|
271 | 271 | } |
272 | 272 | return $this->rootId; |
273 | 273 | } |
@@ -33,107 +33,107 @@ |
||
33 | 33 | * Mount provider for object store home storages |
34 | 34 | */ |
35 | 35 | class ObjectHomeMountProvider implements IHomeMountProvider { |
36 | - /** |
|
37 | - * @var IConfig |
|
38 | - */ |
|
39 | - private $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * ObjectStoreHomeMountProvider constructor. |
|
43 | - * |
|
44 | - * @param IConfig $config |
|
45 | - */ |
|
46 | - public function __construct(IConfig $config) { |
|
47 | - $this->config = $config; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Get the cache mount for a user |
|
52 | - * |
|
53 | - * @param IUser $user |
|
54 | - * @param IStorageFactory $loader |
|
55 | - * @return \OCP\Files\Mount\IMountPoint |
|
56 | - */ |
|
57 | - public function getHomeMountForUser(IUser $user, IStorageFactory $loader) { |
|
58 | - |
|
59 | - $config = $this->getMultiBucketObjectStoreConfig($user); |
|
60 | - if ($config === null) { |
|
61 | - $config = $this->getSingleBucketObjectStoreConfig($user); |
|
62 | - } |
|
63 | - |
|
64 | - if ($config === null) { |
|
65 | - return null; |
|
66 | - } |
|
67 | - |
|
68 | - return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param IUser $user |
|
73 | - * @return array|null |
|
74 | - */ |
|
75 | - private function getSingleBucketObjectStoreConfig(IUser $user) { |
|
76 | - $config = $this->config->getSystemValue('objectstore'); |
|
77 | - if (!is_array($config)) { |
|
78 | - return null; |
|
79 | - } |
|
80 | - |
|
81 | - // sanity checks |
|
82 | - if (empty($config['class'])) { |
|
83 | - \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
84 | - } |
|
85 | - if (!isset($config['arguments'])) { |
|
86 | - $config['arguments'] = []; |
|
87 | - } |
|
88 | - // instantiate object store implementation |
|
89 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
90 | - |
|
91 | - $config['arguments']['user'] = $user; |
|
92 | - |
|
93 | - return $config; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param IUser $user |
|
98 | - * @return array|null |
|
99 | - */ |
|
100 | - private function getMultiBucketObjectStoreConfig(IUser $user) { |
|
101 | - $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
102 | - if (!is_array($config)) { |
|
103 | - return null; |
|
104 | - } |
|
105 | - |
|
106 | - // sanity checks |
|
107 | - if (empty($config['class'])) { |
|
108 | - \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
109 | - } |
|
110 | - if (!isset($config['arguments'])) { |
|
111 | - $config['arguments'] = []; |
|
112 | - } |
|
113 | - $config['arguments']['user'] = $user; |
|
114 | - |
|
115 | - $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null); |
|
116 | - |
|
117 | - if ($bucket === null) { |
|
118 | - /* |
|
36 | + /** |
|
37 | + * @var IConfig |
|
38 | + */ |
|
39 | + private $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * ObjectStoreHomeMountProvider constructor. |
|
43 | + * |
|
44 | + * @param IConfig $config |
|
45 | + */ |
|
46 | + public function __construct(IConfig $config) { |
|
47 | + $this->config = $config; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Get the cache mount for a user |
|
52 | + * |
|
53 | + * @param IUser $user |
|
54 | + * @param IStorageFactory $loader |
|
55 | + * @return \OCP\Files\Mount\IMountPoint |
|
56 | + */ |
|
57 | + public function getHomeMountForUser(IUser $user, IStorageFactory $loader) { |
|
58 | + |
|
59 | + $config = $this->getMultiBucketObjectStoreConfig($user); |
|
60 | + if ($config === null) { |
|
61 | + $config = $this->getSingleBucketObjectStoreConfig($user); |
|
62 | + } |
|
63 | + |
|
64 | + if ($config === null) { |
|
65 | + return null; |
|
66 | + } |
|
67 | + |
|
68 | + return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param IUser $user |
|
73 | + * @return array|null |
|
74 | + */ |
|
75 | + private function getSingleBucketObjectStoreConfig(IUser $user) { |
|
76 | + $config = $this->config->getSystemValue('objectstore'); |
|
77 | + if (!is_array($config)) { |
|
78 | + return null; |
|
79 | + } |
|
80 | + |
|
81 | + // sanity checks |
|
82 | + if (empty($config['class'])) { |
|
83 | + \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
84 | + } |
|
85 | + if (!isset($config['arguments'])) { |
|
86 | + $config['arguments'] = []; |
|
87 | + } |
|
88 | + // instantiate object store implementation |
|
89 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
90 | + |
|
91 | + $config['arguments']['user'] = $user; |
|
92 | + |
|
93 | + return $config; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param IUser $user |
|
98 | + * @return array|null |
|
99 | + */ |
|
100 | + private function getMultiBucketObjectStoreConfig(IUser $user) { |
|
101 | + $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
102 | + if (!is_array($config)) { |
|
103 | + return null; |
|
104 | + } |
|
105 | + |
|
106 | + // sanity checks |
|
107 | + if (empty($config['class'])) { |
|
108 | + \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); |
|
109 | + } |
|
110 | + if (!isset($config['arguments'])) { |
|
111 | + $config['arguments'] = []; |
|
112 | + } |
|
113 | + $config['arguments']['user'] = $user; |
|
114 | + |
|
115 | + $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null); |
|
116 | + |
|
117 | + if ($bucket === null) { |
|
118 | + /* |
|
119 | 119 | * Use any provided bucket argument as prefix |
120 | 120 | * and add the mapping from username => bucket |
121 | 121 | */ |
122 | - if (!isset($config['arguments']['bucket'])) { |
|
123 | - $config['arguments']['bucket'] = ''; |
|
124 | - } |
|
125 | - $mapper = new \OC\Files\ObjectStore\Mapper($user); |
|
126 | - $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64; |
|
127 | - $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets); |
|
128 | - |
|
129 | - $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']); |
|
130 | - } else { |
|
131 | - $config['arguments']['bucket'] = $bucket; |
|
132 | - } |
|
133 | - |
|
134 | - // instantiate object store implementation |
|
135 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
136 | - |
|
137 | - return $config; |
|
138 | - } |
|
122 | + if (!isset($config['arguments']['bucket'])) { |
|
123 | + $config['arguments']['bucket'] = ''; |
|
124 | + } |
|
125 | + $mapper = new \OC\Files\ObjectStore\Mapper($user); |
|
126 | + $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64; |
|
127 | + $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets); |
|
128 | + |
|
129 | + $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']); |
|
130 | + } else { |
|
131 | + $config['arguments']['bucket'] = $bucket; |
|
132 | + } |
|
133 | + |
|
134 | + // instantiate object store implementation |
|
135 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); |
|
136 | + |
|
137 | + return $config; |
|
138 | + } |
|
139 | 139 | } |