forked from piernov/zotprime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorageController.php
168 lines (137 loc) · 4.71 KB
/
StorageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the Zotero Data Server.
Copyright © 2013 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
require('ApiController.php');
class StorageController extends ApiController {
// Use 1TB as a numeric stand-in for unlimited, for now
const UNLIMITED = 1000000;
//
// Storage-related
//
public function laststoragesync() {
$this->allowMethods(array('GET', 'POST'));
// Uninitialized publications library
if (!$this->objectLibraryID) {
$this->e404();
}
// Deprecated after 3.0, which used auth=1
if ($this->apiVersion < 2 || !empty($_GET['auth'])) {
$lastSync = Zotero_Users::getLastStorageSync($this->objectUserID);
}
else {
$lastSync = Zotero_Libraries::getLastStorageSync($this->objectLibraryID);
}
if (!$lastSync) {
$this->e404();
}
echo $lastSync;
exit;
}
public function removestoragefiles() {
$this->allowMethods(array('POST'));
$sql = "DELETE SFI FROM storageFileItems SFI JOIN items USING (itemID) WHERE libraryID=?";
Zotero_DB::query($sql, $this->objectLibraryID, Zotero_Shards::getByLibraryID($this->objectLibraryID));
Zotero_Storage::clearUserUsage(Zotero_Libraries::getOwner($this->objectLibraryID));
$sql = "DELETE FROM storageFileLibraries WHERE libraryID = ?";
Zotero_DB::query($sql, $this->objectLibraryID);
header("HTTP/1.1 204 No Content");
exit;
}
public function storageadmin() {
if (!$this->permissions->isSuper()) {
$this->e404();
}
$this->allowMethods(array('GET', 'POST'));
Zotero_DB::beginTransaction();
if ($this->method == 'POST') {
if (!isset($_POST['quota'])) {
$this->e400("Quota not provided");
}
// Accept 'unlimited' via API
if ($_POST['quota'] == 'unlimited') {
$_POST['quota'] = self::UNLIMITED;
}
if (!isset($_POST['expiration'])) {
$this->e400("Expiration not provided");
}
if (!is_numeric($_POST['quota']) || $_POST['quota'] < 0) {
$this->e400("Invalid quota");
}
if (!is_numeric($_POST['expiration'])) {
$this->e400("Invalid expiration");
}
$halfHourAgo = strtotime("-30 minutes");
if ($_POST['expiration'] != 0 && $_POST['expiration'] < $halfHourAgo) {
$this->e400("Expiration is in the past");
}
try {
Zotero_Storage::setUserValues($this->objectUserID, $_POST['quota'], $_POST['expiration']);
}
catch (Exception $e) {
if ($e->getCode() == Z_ERROR_GROUP_QUOTA_SET_BELOW_USAGE) {
$this->e409("Cannot set quota below current usage");
}
$this->handleException($e);
}
}
// GET request
$xml = new SimpleXMLElement('<storage/>');
$quota = Zotero_Storage::getEffectiveUserQuota($this->objectUserID);
$xml->quota = $quota;
$instQuota = Zotero_Storage::getInstitutionalUserQuota($this->objectUserID);
// If personal quota is in effect
if (!$instQuota || $quota > $instQuota) {
$values = Zotero_Storage::getUserValues($this->objectUserID);
if ($values) {
$xml->expiration = (int) $values['expiration'];
}
}
// Return 'unlimited' via API
if ($quota == self::UNLIMITED) {
$xml->quota = 'unlimited';
}
$usage = Zotero_Storage::getUserUsage($this->objectUserID, 'mb');
$xml->usage->total = $usage['total'];
$xml->usage->library = $usage['library'];
foreach ($usage['groups'] as $group) {
if (!isset($group['id'])) {
throw new Exception("Group id isn't set");
}
if (!isset($group['usage'])) {
throw new Exception("Group usage isn't set");
}
$xmlGroup = $xml->usage->addChild('group', $group['usage']);
$xmlGroup['id'] = $group['id'];
}
Zotero_DB::commit();
header('application/xml');
echo $xml->asXML();
exit;
}
public function storagetransferbucket() {
// DISABLED
$this->e404();
if (!$this->permissions->isSuper()) {
$this->e404();
}
$this->allowMethods(array('POST'));
Zotero_Storage::transferBucket('zoterofilestorage', 'zoterofilestoragetest');
exit;
}
}