Completed
Push — master ( 3c4251...063036 )
by Roeland
11:46
created

OC_JSON::checkSubAdminUser()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 5
Ratio 38.46 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 5
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Bernhard Posselt <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Felix Moeller <[email protected]>
9
 * @author Georg Ehrke <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 * @author Sebastian Wessalowski <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 * @author Thomas Tanghus <[email protected]>
17
 * @author Vincent Petry <[email protected]>
18
 *
19
 * @license AGPL-3.0
20
 *
21
 * This code is free software: you can redistribute it and/or modify
22
 * it under the terms of the GNU Affero General Public License, version 3,
23
 * as published by the Free Software Foundation.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License, version 3,
31
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
32
 *
33
 */
34
35
/**
36
 * Class OC_JSON
37
 * @deprecated Use a AppFramework JSONResponse instead
38
 */
39
class OC_JSON{
40
41
	/**
42
	 * Check if the app is enabled, send json error msg if not
43
	 * @param string $app
44
	 * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
45
	 * @suppress PhanDeprecatedFunction
46
	 */
47
	public static function checkAppEnabled($app) {
48
		if( !\OC::$server->getAppManager()->isEnabledForUser($app)) {
49
			$l = \OC::$server->getL10N('lib');
50
			self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' )));
51
			exit();
52
		}
53
	}
54
55
	/**
56
	 * Check if the user is logged in, send json error msg if not
57
	 * @deprecated Use annotation based ACLs from the AppFramework instead
58
	 * @suppress PhanDeprecatedFunction
59
	 */
60
	public static function checkLoggedIn() {
61
		$twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager();
62
		if( !\OC::$server->getUserSession()->isLoggedIn()
63
			|| $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
64
			$l = \OC::$server->getL10N('lib');
65
			http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
66
			self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
67
			exit();
68
		}
69
	}
70
71
	/**
72
	 * Check an ajax get/post call if the request token is valid, send json error msg if not.
73
	 * @deprecated Use annotation based CSRF checks from the AppFramework instead
74
	 * @suppress PhanDeprecatedFunction
75
	 */
76
	public static function callCheck() {
77
		if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
78
			header('Location: '.\OC::$WEBROOT);
79
			exit();
80
		}
81
82
		if( !\OC::$server->getRequest()->passesCSRFCheck()) {
83
			$l = \OC::$server->getL10N('lib');
84
			self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' )));
85
			exit();
86
		}
87
	}
88
89
	/**
90
	 * Check if the user is a admin, send json error msg if not.
91
	 * @deprecated Use annotation based ACLs from the AppFramework instead
92
	 * @suppress PhanDeprecatedFunction
93
	 */
94
	public static function checkAdminUser() {
95
		if( !OC_User::isAdminUser(OC_User::getUser())) {
96
			$l = \OC::$server->getL10N('lib');
97
			self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
98
			exit();
99
		}
100
	}
101
102
	/**
103
	 * Send json error msg
104
	 * @deprecated Use a AppFramework JSONResponse instead
105
	 * @suppress PhanDeprecatedFunction
106
	 */
107
	public static function error($data = array()) {
108
		$data['status'] = 'error';
109
		header( 'Content-Type: application/json; charset=utf-8');
110
		echo self::encode($data);
111
	}
112
113
	/**
114
	 * Send json success msg
115
	 * @deprecated Use a AppFramework JSONResponse instead
116
	 * @suppress PhanDeprecatedFunction
117
	 */
118
	public static function success($data = array()) {
119
		$data['status'] = 'success';
120
		header( 'Content-Type: application/json; charset=utf-8');
121
		echo self::encode($data);
122
	}
123
124
	/**
125
	 * Convert OC_L10N_String to string, for use in json encodings
126
	 */
127
	protected static function to_string(&$value) {
128
		if ($value instanceof \OC\L10N\L10NString) {
129
			$value = (string)$value;
130
		}
131
	}
132
133
	/**
134
	 * Encode JSON
135
	 * @deprecated Use a AppFramework JSONResponse instead
136
	 */
137
	public static function encode($data) {
138
		if (is_array($data)) {
139
			array_walk_recursive($data, array('OC_JSON', 'to_string'));
140
		}
141
		return json_encode($data, JSON_HEX_TAG);
142
	}
143
}
144