|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Bart Visscher <[email protected]> |
|
4
|
|
|
* @author Bernhard Posselt <[email protected]> |
|
5
|
|
|
* @author Christoph Wurst <[email protected]> |
|
6
|
|
|
* @author Felix Moeller <[email protected]> |
|
7
|
|
|
* @author Georg Ehrke <[email protected]> |
|
8
|
|
|
* @author Lukas Reschke <[email protected]> |
|
9
|
|
|
* @author Morris Jobke <[email protected]> |
|
10
|
|
|
* @author Robin Appelman <[email protected]> |
|
11
|
|
|
* @author Thomas Müller <[email protected]> |
|
12
|
|
|
* @author Thomas Tanghus <[email protected]> |
|
13
|
|
|
* @author Vincent Petry <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
|
16
|
|
|
* @license AGPL-3.0 |
|
17
|
|
|
* |
|
18
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
19
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* as published by the Free Software Foundation. |
|
21
|
|
|
* |
|
22
|
|
|
* This program is distributed in the hope that it will be useful, |
|
23
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
24
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
25
|
|
|
* GNU Affero General Public License for more details. |
|
26
|
|
|
* |
|
27
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
28
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
use OC\L10N\L10NString; |
|
33
|
|
|
use OC\Authentication\Exceptions\AccountCheckException; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class OC_JSON |
|
37
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
38
|
|
|
*/ |
|
39
|
|
|
class OC_JSON { |
|
40
|
|
|
protected static $send_content_type_header = false; |
|
41
|
|
|
/** |
|
42
|
|
|
* set Content-Type header to jsonrequest |
|
43
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function setContentTypeHeader($type='application/json') { |
|
46
|
|
|
if (!self::$send_content_type_header) { |
|
47
|
|
|
// We send json data |
|
48
|
|
|
\header('Content-Type: '.$type . '; charset=utf-8'); |
|
49
|
|
|
self::$send_content_type_header = true; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check if the app is enabled, send json error msg if not |
|
55
|
|
|
* @param string $app |
|
56
|
|
|
* @deprecated Use the AppFramework instead. It will automatically check if the app is enabled. |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public static function checkAppEnabled($app) { |
|
59
|
|
|
if (!OC_App::isEnabled($app)) { |
|
60
|
|
|
$l = \OC::$server->getL10N('lib'); |
|
61
|
|
|
self::error(['data' => ['message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled']]); |
|
62
|
|
|
exit(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
private static function sendErrorAndExit() { |
|
67
|
|
|
$l = \OC::$server->getL10N('lib'); |
|
68
|
|
|
\http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED); |
|
69
|
|
|
self::error(['data' => ['message' => $l->t('Authentication error'), 'error' => 'authentication_error']]); |
|
70
|
|
|
exit(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if the user is logged in, send json error msg if not |
|
75
|
|
|
* @deprecated Use annotation based ACLs from the AppFramework instead |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function checkLoggedIn() { |
|
78
|
|
|
static $loginCalled = false; |
|
79
|
|
|
$userSession = \OC::$server->getUserSession(); |
|
80
|
|
|
if (!$loginCalled && !$userSession->isLoggedIn()) { |
|
81
|
|
|
\OC::handleLogin(\OC::$server->getRequest()); |
|
82
|
|
|
$loginCalled = true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (!$userSession->isLoggedIn()) { |
|
86
|
|
|
self::sendErrorAndExit(); |
|
87
|
|
|
} |
|
88
|
|
|
if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor()) { |
|
89
|
|
|
self::sendErrorAndExit(); |
|
90
|
|
|
} |
|
91
|
|
|
try { |
|
92
|
|
|
\OC::$server->getAccountModuleManager()->check($userSession->getUser()); |
|
|
|
|
|
|
93
|
|
|
} catch (AccountCheckException $ex) { |
|
94
|
|
|
self::sendErrorAndExit(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Check an ajax get/post call if the request token is valid, send json error msg if not. |
|
100
|
|
|
* @deprecated Use annotation based CSRF checks from the AppFramework instead |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
public static function callCheck() { |
|
103
|
|
|
if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
104
|
|
|
$l = \OC::$server->getL10N('lib'); |
|
105
|
|
|
self::error(['data' => ['message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired']]); |
|
106
|
|
|
exit(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Check if the user is a admin, send json error msg if not. |
|
112
|
|
|
* @deprecated Use annotation based ACLs from the AppFramework instead |
|
113
|
|
|
*/ |
|
114
|
|
View Code Duplication |
public static function checkAdminUser() { |
|
115
|
|
|
if (!OC_User::isAdminUser(OC_User::getUser())) { |
|
|
|
|
|
|
116
|
|
|
$l = \OC::$server->getL10N('lib'); |
|
117
|
|
|
self::error(['data' => ['message' => $l->t('Authentication error'), 'error' => 'authentication_error']]); |
|
118
|
|
|
exit(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Check is a given user exists - send json error msg if not |
|
124
|
|
|
* @param string $user |
|
125
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
public static function checkUserExists($user) { |
|
128
|
|
|
if (!OCP\User::userExists($user)) { |
|
129
|
|
|
$l = \OC::$server->getL10N('lib'); |
|
130
|
|
|
OCP\JSON::error(['data' => ['message' => $l->t('Unknown user'), 'error' => 'unknown_user']]); |
|
131
|
|
|
exit; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Check if the user has administration privileges, send json error msg if not |
|
137
|
|
|
* @deprecated Use annotation based ACLs from the AppFramework instead |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function checkSubAdminUser() { |
|
140
|
|
|
$hasUserManagementPrivileges = false; |
|
141
|
|
|
$userObject = \OC::$server->getUserSession()->getUser(); |
|
142
|
|
View Code Duplication |
if ($userObject !== null) { |
|
|
|
|
|
|
143
|
|
|
//Admin and SubAdmins are allowed to access user management |
|
144
|
|
|
$hasUserManagementPrivileges = \OC::$server->getGroupManager()->isAdmin($userObject->getUID()) |
|
145
|
|
|
|| \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!$hasUserManagementPrivileges) { |
|
149
|
|
|
$l = \OC::$server->getL10N('lib'); |
|
150
|
|
|
self::error(['data' => ['message' => $l->t('Authentication error'), 'error' => 'authentication_error']]); |
|
151
|
|
|
exit(); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Send json error msg |
|
157
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function error($data = []) { |
|
160
|
|
|
$data['status'] = 'error'; |
|
161
|
|
|
self::encodedPrint($data); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Send json success msg |
|
166
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function success($data = []) { |
|
169
|
|
|
$data['status'] = 'success'; |
|
170
|
|
|
self::encodedPrint($data); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Convert \OC\L10N\String to string, for use in json encodings |
|
175
|
|
|
*/ |
|
176
|
|
|
protected static function to_string(&$value) { |
|
177
|
|
|
if ($value instanceof L10NString) { |
|
178
|
|
|
$value = (string)$value; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Encode and print $data in json format |
|
184
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
185
|
|
|
*/ |
|
186
|
|
|
public static function encodedPrint($data, $setContentType=true) { |
|
187
|
|
|
if ($setContentType) { |
|
188
|
|
|
self::setContentTypeHeader(); |
|
189
|
|
|
} |
|
190
|
|
|
echo self::encode($data); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Encode JSON |
|
195
|
|
|
* @deprecated Use a AppFramework JSONResponse instead |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function encode($data) { |
|
198
|
|
|
if (\is_array($data)) { |
|
199
|
|
|
\array_walk_recursive($data, ['OC_JSON', 'to_string']); |
|
200
|
|
|
} |
|
201
|
|
|
return \json_encode($data, JSON_HEX_TAG); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: