json.php 5.43 KB
Newer Older
Bart Visscher's avatar
Bart Visscher committed
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
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
 * @author Bart Visscher <bartv@thisnet.nl>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @author Felix Moeller <mail@felixmoeller.de>
 * @author Georg Ehrke <georg@owncloud.com>
 * @author Lukas Reschke <lukas@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <icewind@owncloud.com>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 * @author Thomas Tanghus <thomas@tanghus.net>
 * @author Vincent Petry <pvince81@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
29
30
31
32
33
 */

/**
 * Class OC_JSON
 * @deprecated Use a AppFramework JSONResponse instead
34
 */
Bart Visscher's avatar
Bart Visscher committed
35
36
37
38
class OC_JSON{
	static protected $send_content_type_header = false;
	/**
	 * set Content-Type header to jsonrequest
39
	 * @deprecated Use a AppFramework JSONResponse instead
Bart Visscher's avatar
Bart Visscher committed
40
	 */
Thomas Mueller's avatar
Thomas Mueller committed
41
42
	public static function setContentTypeHeader($type='application/json') {
		if (!self::$send_content_type_header) {
Bart Visscher's avatar
Bart Visscher committed
43
			// We send json data
Bernhard Posselt's avatar
Bernhard Posselt committed
44
			header( 'Content-Type: '.$type . '; charset=utf-8');
Bart Visscher's avatar
Bart Visscher committed
45
46
47
48
			self::$send_content_type_header = true;
		}
	}

49
	/**
50
51
52
53
	 * Check if the app is enabled, send json error msg if not
	 * @param string $app
	 * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
	 */
Thomas Mueller's avatar
Thomas Mueller committed
54
55
	public static function checkAppEnabled($app) {
		if( !OC_App::isEnabled($app)) {
56
			$l = \OC::$server->getL10N('lib');
57
			self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' )));
58
59
60
61
			exit();
		}
	}

Bart Visscher's avatar
Bart Visscher committed
62
	/**
63
64
65
	 * Check if the user is logged in, send json error msg if not
	 * @deprecated Use annotation based ACLs from the AppFramework instead
	 */
Thomas Mueller's avatar
Thomas Mueller committed
66
67
	public static function checkLoggedIn() {
		if( !OC_User::isLoggedIn()) {
68
			$l = \OC::$server->getL10N('lib');
69
			self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
Bart Visscher's avatar
Bart Visscher committed
70
71
72
73
			exit();
		}
	}

74
	/**
Lukas Reschke's avatar
Lukas Reschke committed
75
	 * Check an ajax get/post call if the request token is valid, send json error msg if not.
76
	 * @deprecated Use annotation based CSRF checks from the AppFramework instead
77
	 */
Thomas Mueller's avatar
Thomas Mueller committed
78
	public static function callCheck() {
Morris Jobke's avatar
Morris Jobke committed
79
		if( !(\OC::$server->getRequest()->passesCSRFCheck())) {
80
			$l = \OC::$server->getL10N('lib');
81
			self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' )));
82
83
84
			exit();
		}
	}
Bart Visscher's avatar
Bart Visscher committed
85

Bart Visscher's avatar
Bart Visscher committed
86
	/**
87
88
89
	 * Check if the user is a admin, send json error msg if not.
	 * @deprecated Use annotation based ACLs from the AppFramework instead
	 */
Thomas Mueller's avatar
Thomas Mueller committed
90
	public static function checkAdminUser() {
Lukas Reschke's avatar
Lukas Reschke committed
91
		if( !OC_User::isAdminUser(OC_User::getUser())) {
92
			$l = \OC::$server->getL10N('lib');
93
			self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
Bart Visscher's avatar
Bart Visscher committed
94
95
96
			exit();
		}
	}
Bart Visscher's avatar
Bart Visscher committed
97

98
99
100
	/**
	 * Check is a given user exists - send json error msg if not
	 * @param string $user
101
	 * @deprecated Use a AppFramework JSONResponse instead
102
103
104
	 */
	public static function checkUserExists($user) {
		if (!OCP\User::userExists($user)) {
105
			$l = \OC::$server->getL10N('lib');
106
			OCP\JSON::error(array('data' => array('message' => $l->t('Unknown user'), 'error' => 'unknown_user' )));
107
108
109
110
111
			exit;
		}
	}


112
	/**
113
114
115
	 * Check if the user is a subadmin, send json error msg if not
	 * @deprecated Use annotation based ACLs from the AppFramework instead
	 */
Thomas Mueller's avatar
Thomas Mueller committed
116
	public static function checkSubAdminUser() {
117
118
119
120
121
122
123
		$userObject = \OC::$server->getUserSession()->getUser();
		$isSubAdmin = false;
		if($userObject !== null) {
			$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
		}

		if(!$isSubAdmin) {
124
			$l = \OC::$server->getL10N('lib');
125
			self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
126
127
128
			exit();
		}
	}
Bart Visscher's avatar
Bart Visscher committed
129

Bart Visscher's avatar
Bart Visscher committed
130
	/**
131
132
133
	 * Send json error msg
	 * @deprecated Use a AppFramework JSONResponse instead
	 */
Thomas Mueller's avatar
Thomas Mueller committed
134
	public static function error($data = array()) {
Bart Visscher's avatar
Bart Visscher committed
135
136
137
138
139
		$data['status'] = 'error';
		self::encodedPrint($data);
	}

	/**
140
141
142
	 * Send json success msg
	 * @deprecated Use a AppFramework JSONResponse instead
	 */
Thomas Mueller's avatar
Thomas Mueller committed
143
	public static function success($data = array()) {
Bart Visscher's avatar
Bart Visscher committed
144
145
146
147
		$data['status'] = 'success';
		self::encodedPrint($data);
	}

148
149
150
	/**
	 * Convert OC_L10N_String to string, for use in json encodings
	 */
Thomas Mueller's avatar
Thomas Mueller committed
151
	protected static function to_string(&$value) {
152
153
154
155
156
		if ($value instanceof OC_L10N_String) {
			$value = (string)$value;
		}
	}

Bart Visscher's avatar
Bart Visscher committed
157
	/**
158
159
160
	 * Encode and print $data in json format
	 * @deprecated Use a AppFramework JSONResponse instead
	 */
Felix Moeller's avatar
Felix Moeller committed
161
	public static function encodedPrint($data, $setContentType=true) {
Thomas Mueller's avatar
Thomas Mueller committed
162
		if($setContentType) {
Robin Appelman's avatar
Robin Appelman committed
163
164
			self::setContentTypeHeader();
		}
165
166
167
168
169
		echo self::encode($data);
	}

	/**
	 * Encode JSON
170
	 * @deprecated Use a AppFramework JSONResponse instead
171
172
	 */
	public static function encode($data) {
Thomas Müller's avatar
Thomas Müller committed
173
174
175
		if (is_array($data)) {
			array_walk_recursive($data, array('OC_JSON', 'to_string'));
		}
Lukas Reschke's avatar
Lukas Reschke committed
176
		return json_encode($data, JSON_HEX_TAG);
Bart Visscher's avatar
Bart Visscher committed
177
178
	}
}