Passed
Push — master ( 78ca18...b2ef00 )
by Roeland
14:30 queued 11s
created
lib/private/legacy/OC_API.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -34,157 +34,157 @@
 block discarded – undo
34 34
 
35 35
 class OC_API {
36 36
 
37
-	/**
38
-	 * api actions
39
-	 */
40
-	protected static $actions = [];
41
-
42
-	/**
43
-	 * respond to a call
44
-	 * @param \OC\OCS\Result $result
45
-	 * @param string $format the format xml|json
46
-	 * @psalm-taint-escape html
47
-	 */
48
-	public static function respond($result, $format = 'xml') {
49
-		$request = \OC::$server->getRequest();
50
-
51
-		// Send 401 headers if unauthorised
52
-		if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
53
-			// If request comes from JS return dummy auth request
54
-			if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
55
-				header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
56
-			} else {
57
-				header('WWW-Authenticate: Basic realm="Authorisation Required"');
58
-			}
59
-			http_response_code(401);
60
-		}
61
-
62
-		foreach ($result->getHeaders() as $name => $value) {
63
-			header($name . ': ' . $value);
64
-		}
65
-
66
-		$meta = $result->getMeta();
67
-		$data = $result->getData();
68
-		if (self::isV2($request)) {
69
-			$statusCode = self::mapStatusCodes($result->getStatusCode());
70
-			if (!is_null($statusCode)) {
71
-				$meta['statuscode'] = $statusCode;
72
-				http_response_code($statusCode);
73
-			}
74
-		}
75
-
76
-		self::setContentType($format);
77
-		$body = self::renderResult($format, $meta, $data);
78
-		echo $body;
79
-	}
80
-
81
-	/**
82
-	 * @param XMLWriter $writer
83
-	 */
84
-	private static function toXML($array, $writer) {
85
-		foreach ($array as $k => $v) {
86
-			if ($k[0] === '@') {
87
-				$writer->writeAttribute(substr($k, 1), $v);
88
-				continue;
89
-			} elseif (is_numeric($k)) {
90
-				$k = 'element';
91
-			}
92
-			if (is_array($v)) {
93
-				$writer->startElement($k);
94
-				self::toXML($v, $writer);
95
-				$writer->endElement();
96
-			} else {
97
-				$writer->writeElement($k, $v);
98
-			}
99
-		}
100
-	}
101
-
102
-	/**
103
-	 * @return string
104
-	 */
105
-	public static function requestedFormat() {
106
-		$formats = ['json', 'xml'];
107
-
108
-		$format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
109
-		return $format;
110
-	}
111
-
112
-	/**
113
-	 * Based on the requested format the response content type is set
114
-	 * @param string $format
115
-	 */
116
-	public static function setContentType($format = null) {
117
-		$format = is_null($format) ? self::requestedFormat() : $format;
118
-		if ($format === 'xml') {
119
-			header('Content-type: text/xml; charset=UTF-8');
120
-			return;
121
-		}
122
-
123
-		if ($format === 'json') {
124
-			header('Content-Type: application/json; charset=utf-8');
125
-			return;
126
-		}
127
-
128
-		header('Content-Type: application/octet-stream; charset=utf-8');
129
-	}
130
-
131
-	/**
132
-	 * @param \OCP\IRequest $request
133
-	 * @return bool
134
-	 */
135
-	protected static function isV2(\OCP\IRequest $request) {
136
-		$script = $request->getScriptName();
137
-
138
-		return substr($script, -11) === '/ocs/v2.php';
139
-	}
140
-
141
-	/**
142
-	 * @param integer $sc
143
-	 * @return int
144
-	 */
145
-	public static function mapStatusCodes($sc) {
146
-		switch ($sc) {
147
-			case API::RESPOND_NOT_FOUND:
148
-				return Http::STATUS_NOT_FOUND;
149
-			case API::RESPOND_SERVER_ERROR:
150
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
151
-			case API::RESPOND_UNKNOWN_ERROR:
152
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
153
-			case API::RESPOND_UNAUTHORISED:
154
-				// already handled for v1
155
-				return null;
156
-			case 100:
157
-				return Http::STATUS_OK;
158
-		}
159
-		// any 2xx, 4xx and 5xx will be used as is
160
-		if ($sc >= 200 && $sc < 600) {
161
-			return $sc;
162
-		}
163
-
164
-		return Http::STATUS_BAD_REQUEST;
165
-	}
166
-
167
-	/**
168
-	 * @param string $format
169
-	 * @return string
170
-	 */
171
-	public static function renderResult($format, $meta, $data) {
172
-		$response = [
173
-			'ocs' => [
174
-				'meta' => $meta,
175
-				'data' => $data,
176
-			],
177
-		];
178
-		if ($format == 'json') {
179
-			return OC_JSON::encode($response);
180
-		}
181
-
182
-		$writer = new XMLWriter();
183
-		$writer->openMemory();
184
-		$writer->setIndent(true);
185
-		$writer->startDocument();
186
-		self::toXML($response, $writer);
187
-		$writer->endDocument();
188
-		return $writer->outputMemory(true);
189
-	}
37
+    /**
38
+     * api actions
39
+     */
40
+    protected static $actions = [];
41
+
42
+    /**
43
+     * respond to a call
44
+     * @param \OC\OCS\Result $result
45
+     * @param string $format the format xml|json
46
+     * @psalm-taint-escape html
47
+     */
48
+    public static function respond($result, $format = 'xml') {
49
+        $request = \OC::$server->getRequest();
50
+
51
+        // Send 401 headers if unauthorised
52
+        if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
53
+            // If request comes from JS return dummy auth request
54
+            if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
55
+                header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
56
+            } else {
57
+                header('WWW-Authenticate: Basic realm="Authorisation Required"');
58
+            }
59
+            http_response_code(401);
60
+        }
61
+
62
+        foreach ($result->getHeaders() as $name => $value) {
63
+            header($name . ': ' . $value);
64
+        }
65
+
66
+        $meta = $result->getMeta();
67
+        $data = $result->getData();
68
+        if (self::isV2($request)) {
69
+            $statusCode = self::mapStatusCodes($result->getStatusCode());
70
+            if (!is_null($statusCode)) {
71
+                $meta['statuscode'] = $statusCode;
72
+                http_response_code($statusCode);
73
+            }
74
+        }
75
+
76
+        self::setContentType($format);
77
+        $body = self::renderResult($format, $meta, $data);
78
+        echo $body;
79
+    }
80
+
81
+    /**
82
+     * @param XMLWriter $writer
83
+     */
84
+    private static function toXML($array, $writer) {
85
+        foreach ($array as $k => $v) {
86
+            if ($k[0] === '@') {
87
+                $writer->writeAttribute(substr($k, 1), $v);
88
+                continue;
89
+            } elseif (is_numeric($k)) {
90
+                $k = 'element';
91
+            }
92
+            if (is_array($v)) {
93
+                $writer->startElement($k);
94
+                self::toXML($v, $writer);
95
+                $writer->endElement();
96
+            } else {
97
+                $writer->writeElement($k, $v);
98
+            }
99
+        }
100
+    }
101
+
102
+    /**
103
+     * @return string
104
+     */
105
+    public static function requestedFormat() {
106
+        $formats = ['json', 'xml'];
107
+
108
+        $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
109
+        return $format;
110
+    }
111
+
112
+    /**
113
+     * Based on the requested format the response content type is set
114
+     * @param string $format
115
+     */
116
+    public static function setContentType($format = null) {
117
+        $format = is_null($format) ? self::requestedFormat() : $format;
118
+        if ($format === 'xml') {
119
+            header('Content-type: text/xml; charset=UTF-8');
120
+            return;
121
+        }
122
+
123
+        if ($format === 'json') {
124
+            header('Content-Type: application/json; charset=utf-8');
125
+            return;
126
+        }
127
+
128
+        header('Content-Type: application/octet-stream; charset=utf-8');
129
+    }
130
+
131
+    /**
132
+     * @param \OCP\IRequest $request
133
+     * @return bool
134
+     */
135
+    protected static function isV2(\OCP\IRequest $request) {
136
+        $script = $request->getScriptName();
137
+
138
+        return substr($script, -11) === '/ocs/v2.php';
139
+    }
140
+
141
+    /**
142
+     * @param integer $sc
143
+     * @return int
144
+     */
145
+    public static function mapStatusCodes($sc) {
146
+        switch ($sc) {
147
+            case API::RESPOND_NOT_FOUND:
148
+                return Http::STATUS_NOT_FOUND;
149
+            case API::RESPOND_SERVER_ERROR:
150
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
151
+            case API::RESPOND_UNKNOWN_ERROR:
152
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
153
+            case API::RESPOND_UNAUTHORISED:
154
+                // already handled for v1
155
+                return null;
156
+            case 100:
157
+                return Http::STATUS_OK;
158
+        }
159
+        // any 2xx, 4xx and 5xx will be used as is
160
+        if ($sc >= 200 && $sc < 600) {
161
+            return $sc;
162
+        }
163
+
164
+        return Http::STATUS_BAD_REQUEST;
165
+    }
166
+
167
+    /**
168
+     * @param string $format
169
+     * @return string
170
+     */
171
+    public static function renderResult($format, $meta, $data) {
172
+        $response = [
173
+            'ocs' => [
174
+                'meta' => $meta,
175
+                'data' => $data,
176
+            ],
177
+        ];
178
+        if ($format == 'json') {
179
+            return OC_JSON::encode($response);
180
+        }
181
+
182
+        $writer = new XMLWriter();
183
+        $writer->openMemory();
184
+        $writer->setIndent(true);
185
+        $writer->startDocument();
186
+        self::toXML($response, $writer);
187
+        $writer->endDocument();
188
+        return $writer->outputMemory(true);
189
+    }
190 190
 }
Please login to merge, or discard this patch.
lib/private/legacy/OC_JSON.php 1 patch
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -34,108 +34,108 @@
 block discarded – undo
34 34
  */
35 35
 class OC_JSON {
36 36
 
37
-	/**
38
-	 * Check if the app is enabled, send json error msg if not
39
-	 * @param string $app
40
-	 * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
41
-	 * @suppress PhanDeprecatedFunction
42
-	 */
43
-	public static function checkAppEnabled($app) {
44
-		if (!\OC::$server->getAppManager()->isEnabledForUser($app)) {
45
-			$l = \OC::$server->getL10N('lib');
46
-			self::error([ 'data' => [ 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ]]);
47
-			exit();
48
-		}
49
-	}
37
+    /**
38
+     * Check if the app is enabled, send json error msg if not
39
+     * @param string $app
40
+     * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
41
+     * @suppress PhanDeprecatedFunction
42
+     */
43
+    public static function checkAppEnabled($app) {
44
+        if (!\OC::$server->getAppManager()->isEnabledForUser($app)) {
45
+            $l = \OC::$server->getL10N('lib');
46
+            self::error([ 'data' => [ 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ]]);
47
+            exit();
48
+        }
49
+    }
50 50
 
51
-	/**
52
-	 * Check if the user is logged in, send json error msg if not
53
-	 * @deprecated Use annotation based ACLs from the AppFramework instead
54
-	 * @suppress PhanDeprecatedFunction
55
-	 */
56
-	public static function checkLoggedIn() {
57
-		$twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager();
58
-		if (!\OC::$server->getUserSession()->isLoggedIn()
59
-			|| $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
60
-			$l = \OC::$server->getL10N('lib');
61
-			http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
62
-			self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
63
-			exit();
64
-		}
65
-	}
51
+    /**
52
+     * Check if the user is logged in, send json error msg if not
53
+     * @deprecated Use annotation based ACLs from the AppFramework instead
54
+     * @suppress PhanDeprecatedFunction
55
+     */
56
+    public static function checkLoggedIn() {
57
+        $twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager();
58
+        if (!\OC::$server->getUserSession()->isLoggedIn()
59
+            || $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
60
+            $l = \OC::$server->getL10N('lib');
61
+            http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
62
+            self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
63
+            exit();
64
+        }
65
+    }
66 66
 
67
-	/**
68
-	 * Check an ajax get/post call if the request token is valid, send json error msg if not.
69
-	 * @deprecated Use annotation based CSRF checks from the AppFramework instead
70
-	 * @suppress PhanDeprecatedFunction
71
-	 */
72
-	public static function callCheck() {
73
-		if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
74
-			header('Location: '.\OC::$WEBROOT);
75
-			exit();
76
-		}
67
+    /**
68
+     * Check an ajax get/post call if the request token is valid, send json error msg if not.
69
+     * @deprecated Use annotation based CSRF checks from the AppFramework instead
70
+     * @suppress PhanDeprecatedFunction
71
+     */
72
+    public static function callCheck() {
73
+        if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
74
+            header('Location: '.\OC::$WEBROOT);
75
+            exit();
76
+        }
77 77
 
78
-		if (!\OC::$server->getRequest()->passesCSRFCheck()) {
79
-			$l = \OC::$server->getL10N('lib');
80
-			self::error([ 'data' => [ 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ]]);
81
-			exit();
82
-		}
83
-	}
78
+        if (!\OC::$server->getRequest()->passesCSRFCheck()) {
79
+            $l = \OC::$server->getL10N('lib');
80
+            self::error([ 'data' => [ 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ]]);
81
+            exit();
82
+        }
83
+    }
84 84
 
85
-	/**
86
-	 * Check if the user is a admin, send json error msg if not.
87
-	 * @deprecated Use annotation based ACLs from the AppFramework instead
88
-	 * @suppress PhanDeprecatedFunction
89
-	 */
90
-	public static function checkAdminUser() {
91
-		if (!OC_User::isAdminUser(OC_User::getUser())) {
92
-			$l = \OC::$server->getL10N('lib');
93
-			self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
94
-			exit();
95
-		}
96
-	}
85
+    /**
86
+     * Check if the user is a admin, send json error msg if not.
87
+     * @deprecated Use annotation based ACLs from the AppFramework instead
88
+     * @suppress PhanDeprecatedFunction
89
+     */
90
+    public static function checkAdminUser() {
91
+        if (!OC_User::isAdminUser(OC_User::getUser())) {
92
+            $l = \OC::$server->getL10N('lib');
93
+            self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
94
+            exit();
95
+        }
96
+    }
97 97
 
98
-	/**
99
-	 * Send json error msg
100
-	 * @deprecated Use a AppFramework JSONResponse instead
101
-	 * @suppress PhanDeprecatedFunction
102
-	 * @psalm-taint-escape html
103
-	 */
104
-	public static function error($data = []) {
105
-		$data['status'] = 'error';
106
-		header('Content-Type: application/json; charset=utf-8');
107
-		echo self::encode($data);
108
-	}
98
+    /**
99
+     * Send json error msg
100
+     * @deprecated Use a AppFramework JSONResponse instead
101
+     * @suppress PhanDeprecatedFunction
102
+     * @psalm-taint-escape html
103
+     */
104
+    public static function error($data = []) {
105
+        $data['status'] = 'error';
106
+        header('Content-Type: application/json; charset=utf-8');
107
+        echo self::encode($data);
108
+    }
109 109
 
110
-	/**
111
-	 * Send json success msg
112
-	 * @deprecated Use a AppFramework JSONResponse instead
113
-	 * @suppress PhanDeprecatedFunction
114
-	 * @psalm-taint-escape html
115
-	 */
116
-	public static function success($data = []) {
117
-		$data['status'] = 'success';
118
-		header('Content-Type: application/json; charset=utf-8');
119
-		echo self::encode($data);
120
-	}
110
+    /**
111
+     * Send json success msg
112
+     * @deprecated Use a AppFramework JSONResponse instead
113
+     * @suppress PhanDeprecatedFunction
114
+     * @psalm-taint-escape html
115
+     */
116
+    public static function success($data = []) {
117
+        $data['status'] = 'success';
118
+        header('Content-Type: application/json; charset=utf-8');
119
+        echo self::encode($data);
120
+    }
121 121
 
122
-	/**
123
-	 * Convert OC_L10N_String to string, for use in json encodings
124
-	 */
125
-	protected static function to_string(&$value) {
126
-		if ($value instanceof \OC\L10N\L10NString) {
127
-			$value = (string)$value;
128
-		}
129
-	}
122
+    /**
123
+     * Convert OC_L10N_String to string, for use in json encodings
124
+     */
125
+    protected static function to_string(&$value) {
126
+        if ($value instanceof \OC\L10N\L10NString) {
127
+            $value = (string)$value;
128
+        }
129
+    }
130 130
 
131
-	/**
132
-	 * Encode JSON
133
-	 * @deprecated Use a AppFramework JSONResponse instead
134
-	 */
135
-	public static function encode($data) {
136
-		if (is_array($data)) {
137
-			array_walk_recursive($data, ['OC_JSON', 'to_string']);
138
-		}
139
-		return json_encode($data, JSON_HEX_TAG);
140
-	}
131
+    /**
132
+     * Encode JSON
133
+     * @deprecated Use a AppFramework JSONResponse instead
134
+     */
135
+    public static function encode($data) {
136
+        if (is_array($data)) {
137
+            array_walk_recursive($data, ['OC_JSON', 'to_string']);
138
+        }
139
+        return json_encode($data, JSON_HEX_TAG);
140
+    }
141 141
 }
Please login to merge, or discard this patch.