1 /*
2  * Copyright (C) by Roeland Jago Douma <roeland@famdouma.nl>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12  * for more details.
13  */
14 
15 #include "capabilities.h"
16 
17 #include <QVariantMap>
18 #include <QDebug>
19 
20 namespace OCC {
21 
22 
Capabilities(const QVariantMap & capabilities)23 Capabilities::Capabilities(const QVariantMap &capabilities)
24     : _capabilities(capabilities)
25     , _fileSharingCapabilities(_capabilities.value(QStringLiteral("files_sharing")).toMap())
26     , _fileSharingPublicCapabilities(_fileSharingCapabilities.value(QStringLiteral("public"), {}).toMap())
27     , _tusSupport(_capabilities.value(QStringLiteral("files")).toMap().value(QStringLiteral("tus_support")).toMap())
28 {
29 }
30 
shareAPI() const31 bool Capabilities::shareAPI() const
32 {
33     // This was later added so if it is not present just assume the API is enabled.
34     return _fileSharingCapabilities.value(QStringLiteral("api_enabled"), true).toBool();
35 }
36 
sharePublicLink() const37 bool Capabilities::sharePublicLink() const
38 {
39     // This was later added so if it is not present just assume that link sharing is enabled.
40     return shareAPI() && _fileSharingPublicCapabilities.value(QStringLiteral("enabled"), true).toBool();
41 }
42 
sharePublicLinkAllowUpload() const43 bool Capabilities::sharePublicLinkAllowUpload() const
44 {
45     return _fileSharingPublicCapabilities.value(QStringLiteral("upload")).toBool();
46 }
47 
sharePublicLinkSupportsUploadOnly() const48 bool Capabilities::sharePublicLinkSupportsUploadOnly() const
49 {
50     return _fileSharingPublicCapabilities.value(QStringLiteral("supports_upload_only")).toBool();
51 }
52 
getEnforcePasswordCapability(const QVariantMap & fileSharingPublicCapabilities,const QString & name)53 static bool getEnforcePasswordCapability(const QVariantMap &fileSharingPublicCapabilities, const QString &name)
54 {
55     auto value = fileSharingPublicCapabilities[QStringLiteral("password")].toMap()[QStringLiteral("enforced_for")].toMap()[name];
56     if (!value.isValid())
57         return fileSharingPublicCapabilities[QStringLiteral("password")].toMap()[QStringLiteral("enforced")].toBool();
58     return value.toBool();
59 }
60 
sharePublicLinkEnforcePasswordForReadOnly() const61 bool Capabilities::sharePublicLinkEnforcePasswordForReadOnly() const
62 {
63     return getEnforcePasswordCapability(_fileSharingPublicCapabilities, QStringLiteral("read_only"));
64 }
65 
sharePublicLinkEnforcePasswordForReadWrite() const66 bool Capabilities::sharePublicLinkEnforcePasswordForReadWrite() const
67 {
68     return getEnforcePasswordCapability(_fileSharingPublicCapabilities, QStringLiteral("read_write"));
69 }
70 
sharePublicLinkEnforcePasswordForUploadOnly() const71 bool Capabilities::sharePublicLinkEnforcePasswordForUploadOnly() const
72 {
73     return getEnforcePasswordCapability(_fileSharingPublicCapabilities, QStringLiteral("upload_only"));
74 }
75 
sharePublicLinkDefaultExpire() const76 bool Capabilities::sharePublicLinkDefaultExpire() const
77 {
78     return _fileSharingPublicCapabilities.value(QStringLiteral("expire_date")).toMap().value(QStringLiteral("enabled")).toBool();
79 }
80 
sharePublicLinkDefaultExpireDateDays() const81 int Capabilities::sharePublicLinkDefaultExpireDateDays() const
82 {
83     return _fileSharingPublicCapabilities.value(QStringLiteral("expire_date")).toMap().value(QStringLiteral("days")).toInt();
84 }
85 
sharePublicLinkEnforceExpireDate() const86 bool Capabilities::sharePublicLinkEnforceExpireDate() const
87 {
88     return _fileSharingPublicCapabilities.value(QStringLiteral("expire_date")).toMap().value(QStringLiteral("enforced")).toBool();
89 }
90 
sharePublicLinkMultiple() const91 bool Capabilities::sharePublicLinkMultiple() const
92 {
93     return _fileSharingPublicCapabilities.value(QStringLiteral("multiple")).toBool();
94 }
95 
shareResharing() const96 bool Capabilities::shareResharing() const
97 {
98     return _fileSharingCapabilities.value(QStringLiteral("resharing")).toBool();
99 }
100 
defaultPermissions() const101 int Capabilities::defaultPermissions() const
102 {
103     return _fileSharingCapabilities.value(QStringLiteral("default_permissions"), 1).toInt();
104 }
105 
notificationsAvailable() const106 bool Capabilities::notificationsAvailable() const
107 {
108     // We require the OCS style API in 9.x, can't deal with the REST one only found in 8.2
109     return _capabilities.contains(QStringLiteral("notifications")) && _capabilities.value(QStringLiteral("notifications")).toMap().contains(QStringLiteral("ocs-endpoints"));
110 }
111 
isValid() const112 bool Capabilities::isValid() const
113 {
114     return !_capabilities.isEmpty();
115 }
116 
supportedChecksumTypes() const117 QList<QByteArray> Capabilities::supportedChecksumTypes() const
118 {
119     QList<QByteArray> list;
120     foreach (const auto &t, _capabilities.value(QStringLiteral("checksums")).toMap().value(QStringLiteral("supportedTypes")).toList()) {
121         list.push_back(t.toByteArray());
122     }
123     return list;
124 }
125 
preferredUploadChecksumType() const126 QByteArray Capabilities::preferredUploadChecksumType() const
127 {
128     return qEnvironmentVariable("OWNCLOUD_CONTENT_CHECKSUM_TYPE",
129                                 _capabilities.value(QStringLiteral("checksums")).toMap()
130                                 .value(QStringLiteral("preferredUploadType"), QStringLiteral("SHA1")).toString()).toUtf8();
131 }
132 
uploadChecksumType() const133 QByteArray Capabilities::uploadChecksumType() const
134 {
135     QByteArray preferred = preferredUploadChecksumType();
136     if (!preferred.isEmpty())
137         return preferred;
138     QList<QByteArray> supported = supportedChecksumTypes();
139     if (!supported.isEmpty())
140         return supported.first();
141     return QByteArray();
142 }
143 
chunkingNg() const144 bool Capabilities::chunkingNg() const
145 {
146     if (!bigfilechunkingEnabled())
147     {
148         return false;
149     }
150     static const auto chunkng = qgetenv("OWNCLOUD_CHUNKING_NG");
151     if (chunkng == "0")
152         return false;
153     if (chunkng == "1")
154         return true;
155     return _capabilities.value(QStringLiteral("dav")).toMap().value(QStringLiteral("chunking")).toByteArray() >= "1.0";
156 }
157 
bigfilechunkingEnabled() const158 bool Capabilities::bigfilechunkingEnabled() const
159 {
160     bool ok;
161     const int chunkSize = qEnvironmentVariableIntValue("OWNCLOUD_CHUNK_SIZE", &ok);
162     if (ok && chunkSize == 0)
163     {
164         return false;
165     }
166     return _capabilities.value(QStringLiteral("files")).toMap().value(QStringLiteral("bigfilechunking"), true).toBool();
167 }
168 
tusSupport() const169 const TusSupport &Capabilities::tusSupport() const
170 {
171     return _tusSupport;
172 }
173 
chunkingParallelUploadDisabled() const174 bool Capabilities::chunkingParallelUploadDisabled() const
175 {
176     return _capabilities.value(QStringLiteral("dav")).toMap().value(QStringLiteral("chunkingParallelUploadDisabled")).toBool();
177 }
178 
privateLinkPropertyAvailable() const179 bool Capabilities::privateLinkPropertyAvailable() const
180 {
181     return _capabilities.value(QStringLiteral("files")).toMap().value(QStringLiteral("privateLinks")).toBool();
182 }
183 
privateLinkDetailsParamAvailable() const184 bool Capabilities::privateLinkDetailsParamAvailable() const
185 {
186     return _capabilities.value(QStringLiteral("files")).toMap().value(QStringLiteral("privateLinksDetailsParam")).toBool();
187 }
188 
httpErrorCodesThatResetFailingChunkedUploads() const189 QList<int> Capabilities::httpErrorCodesThatResetFailingChunkedUploads() const
190 {
191     QList<int> list;
192     foreach (const auto &t, _capabilities.value(QStringLiteral("dav")).toMap().value(QStringLiteral("httpErrorCodesThatResetFailingChunkedUploads")).toList()) {
193         list.push_back(t.toInt());
194     }
195     return list;
196 }
197 
invalidFilenameRegex() const198 QString Capabilities::invalidFilenameRegex() const
199 {
200     return _capabilities[QStringLiteral("dav")].toMap()[QStringLiteral("invalidFilenameRegex")].toString();
201 }
202 
uploadConflictFiles() const203 bool Capabilities::uploadConflictFiles() const
204 {
205     static auto envIsSet = !qEnvironmentVariableIsEmpty("OWNCLOUD_UPLOAD_CONFLICT_FILES");
206     static int envValue = qEnvironmentVariableIntValue("OWNCLOUD_UPLOAD_CONFLICT_FILES");
207     if (envIsSet)
208         return envValue != 0;
209 
210     return _capabilities[QStringLiteral("uploadConflictFiles")].toBool();
211 }
212 
versioningEnabled() const213 bool Capabilities::versioningEnabled() const
214 {
215     return _capabilities.value(QStringLiteral("files")).toMap().value(QStringLiteral("versioning")).toBool();
216 }
217 
blacklistedFiles() const218 QStringList Capabilities::blacklistedFiles() const
219 {
220     return _capabilities.value(QStringLiteral("files")).toMap().value(QStringLiteral("blacklisted_files")).toStringList();
221 }
222 
TusSupport(const QVariantMap & tus_support)223 TusSupport::TusSupport(const QVariantMap &tus_support)
224 {
225     if (tus_support.isEmpty() || qEnvironmentVariableIsSet("OWNCLOUD_NO_TUS")) {
226         return;
227     }
228     version = QVersionNumber::fromString(tus_support.value(QStringLiteral("version")).toString());
229     resumable = QVersionNumber::fromString(tus_support.value(QStringLiteral("resumable")).toString());
230     extensions = tus_support.value(QStringLiteral("extension")).toString().split(QLatin1Char(','), QString::SkipEmptyParts);
231     max_chunk_size = tus_support.value(QStringLiteral("max_chunk_size")).value<quint64>();
232     http_method_override = tus_support.value(QStringLiteral("http_method_override")).toString();
233 }
234 
isValid() const235 bool TusSupport::isValid() const
236 {
237     return !version.isNull();
238 }
239 }
240