1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Felix Moeller <[email protected]> |
6
|
|
|
* @author Morris Jobke <[email protected]> |
7
|
|
|
* @author Robin Appelman <[email protected]> |
8
|
|
|
* @author scambra <[email protected]> |
9
|
|
|
* @author Thomas Müller <[email protected]> |
10
|
|
|
* @author Vincent Petry <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @license AGPL-3.0 |
13
|
|
|
* |
14
|
|
|
* This code is free software: you can redistribute it and/or modify |
15
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
16
|
|
|
* as published by the Free Software Foundation. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21
|
|
|
* GNU Affero General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
namespace OCA\DAV\Connector\Sabre; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This plugin check user quota and deny creating files when they exceeds the quota. |
31
|
|
|
* |
32
|
|
|
* @author Sergio Cambra |
33
|
|
|
* @copyright Copyright (C) 2012 entreCables S.L. All rights reserved. |
34
|
|
|
* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License |
35
|
|
|
*/ |
36
|
|
|
class QuotaPlugin extends \Sabre\DAV\ServerPlugin { |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \OC\Files\View |
40
|
|
|
*/ |
41
|
|
|
private $view; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Reference to main server object |
45
|
|
|
* |
46
|
|
|
* @var \Sabre\DAV\Server |
47
|
|
|
*/ |
48
|
|
|
private $server; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \OC\Files\View $view |
52
|
|
|
*/ |
53
|
|
|
public function __construct($view) { |
54
|
|
|
$this->view = $view; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* This initializes the plugin. |
59
|
|
|
* |
60
|
|
|
* This function is called by \Sabre\DAV\Server, after |
61
|
|
|
* addPlugin is called. |
62
|
|
|
* |
63
|
|
|
* This method should set up the requires event subscriptions. |
64
|
|
|
* |
65
|
|
|
* @param \Sabre\DAV\Server $server |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function initialize(\Sabre\DAV\Server $server) { |
69
|
|
|
|
70
|
|
|
$this->server = $server; |
71
|
|
|
|
72
|
|
|
$server->on('beforeWriteContent', array($this, 'checkQuota'), 10); |
73
|
|
|
$server->on('beforeCreateFile', array($this, 'checkQuota'), 10); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This method is called before any HTTP method and validates there is enough free space to store the file |
78
|
|
|
* |
79
|
|
|
* @param string $uri |
80
|
|
|
* @param null $data |
81
|
|
|
* @throws \Sabre\DAV\Exception\InsufficientStorage |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function checkQuota($uri, $data = null) { |
85
|
|
|
$length = $this->getLength(); |
86
|
|
|
if ($length) { |
87
|
|
|
if (substr($uri, 0, 1) !== '/') { |
88
|
|
|
$uri = '/' . $uri; |
89
|
|
|
} |
90
|
|
|
list($parentUri, $newName) = \Sabre\HTTP\URLUtil::splitPath($uri); |
91
|
|
|
if(is_null($parentUri)) { |
92
|
|
|
$parentUri = ''; |
93
|
|
|
} |
94
|
|
|
$req = $this->server->httpRequest; |
95
|
|
|
if ($req->getHeader('OC-Chunked')) { |
96
|
|
|
$info = \OC_FileChunking::decodeName($newName); |
97
|
|
|
$chunkHandler = $this->getFileChunking($info); |
98
|
|
|
// subtract the already uploaded size to see whether |
99
|
|
|
// there is still enough space for the remaining chunks |
100
|
|
|
$length -= $chunkHandler->getCurrentSize(); |
101
|
|
|
// use target file name for free space check in case of shared files |
102
|
|
|
$uri = rtrim($parentUri, '/') . '/' . $info['name']; |
103
|
|
|
} |
104
|
|
|
$freeSpace = $this->getFreeSpace($uri); |
105
|
|
|
if ($freeSpace !== \OCP\Files\FileInfo::SPACE_UNKNOWN && $length > $freeSpace) { |
106
|
|
|
if (isset($chunkHandler)) { |
107
|
|
|
$chunkHandler->cleanup(); |
108
|
|
|
} |
109
|
|
|
throw new \Sabre\DAV\Exception\InsufficientStorage(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getFileChunking($info) { |
116
|
|
|
// FIXME: need a factory for better mocking support |
117
|
|
|
return new \OC_FileChunking($info); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getLength() { |
121
|
|
|
$req = $this->server->httpRequest; |
122
|
|
|
$length = $req->getHeader('X-Expected-Entity-Length'); |
123
|
|
|
if (!is_numeric($length)) { |
124
|
|
|
$length = $req->getHeader('Content-Length'); |
125
|
|
|
$length = is_numeric($length) ? $length : null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$ocLength = $req->getHeader('OC-Total-Length'); |
129
|
|
|
if (is_numeric($length) && is_numeric($ocLength)) { |
130
|
|
|
return max($length, $ocLength); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $length; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $uri |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function getFreeSpace($uri) { |
141
|
|
|
try { |
142
|
|
|
$freeSpace = $this->view->free_space(ltrim($uri, '/')); |
143
|
|
|
return $freeSpace; |
144
|
|
|
} catch (\OCP\Files\StorageNotAvailableException $e) { |
145
|
|
|
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|