Completed
Push — master ( dc4ba3...be35b5 )
by Morris
44:34 queued 21:40
created

OC_JSON::encodedPrint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 9.4285
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{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkAppEnabled() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkLoggedIn() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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();
0 ignored issues
show
Coding Style Compatibility introduced by
The method callCheck() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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();
0 ignored issues
show
Coding Style Compatibility introduced by
The method callCheck() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkAdminUser() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
99
		}
100
	}
101
102
	/**
103
	 * Check if the user is a subadmin, send json error msg if not
104
	 * @deprecated Use annotation based ACLs from the AppFramework instead
105
	 * @suppress PhanDeprecatedFunction
106
	 */
107
	public static function checkSubAdminUser() {
108
		$userObject = \OC::$server->getUserSession()->getUser();
109
		$isSubAdmin = false;
110
		if($userObject !== null) {
111
			$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
112
		}
113
114
		if(!$isSubAdmin) {
115
			$l = \OC::$server->getL10N('lib');
116
			self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
117
			exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkSubAdminUser() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
118
		}
119
	}
120
121
	/**
122
	 * Send json error msg
123
	 * @deprecated Use a AppFramework JSONResponse instead
124
	 * @suppress PhanDeprecatedFunction
125
	 */
126
	public static function error($data = array()) {
127
		$data['status'] = 'error';
128
		header( 'Content-Type: application/json; charset=utf-8');
129
		echo self::encode($data);
130
	}
131
132
	/**
133
	 * Send json success msg
134
	 * @deprecated Use a AppFramework JSONResponse instead
135
	 * @suppress PhanDeprecatedFunction
136
	 */
137
	public static function success($data = array()) {
138
		$data['status'] = 'success';
139
		header( 'Content-Type: application/json; charset=utf-8');
140
		echo self::encode($data);
141
	}
142
143
	/**
144
	 * Convert OC_L10N_String to string, for use in json encodings
145
	 */
146
	protected static function to_string(&$value) {
147
		if ($value instanceof \OC\L10N\L10NString) {
148
			$value = (string)$value;
149
		}
150
	}
151
152
	/**
153
	 * Encode JSON
154
	 * @deprecated Use a AppFramework JSONResponse instead
155
	 */
156
	public static function encode($data) {
157
		if (is_array($data)) {
158
			array_walk_recursive($data, array('OC_JSON', 'to_string'));
159
		}
160
		return json_encode($data, JSON_HEX_TAG);
161
	}
162
}
163