@@ -33,157 +33,157 @@ |
||
33 | 33 | |
34 | 34 | class OC_API { |
35 | 35 | |
36 | - /** |
|
37 | - * api actions |
|
38 | - */ |
|
39 | - protected static $actions = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * respond to a call |
|
43 | - * @param \OC\OCS\Result $result |
|
44 | - * @param string $format the format xml|json |
|
45 | - * @psalm-taint-escape html |
|
46 | - */ |
|
47 | - public static function respond($result, $format = 'xml') { |
|
48 | - $request = \OC::$server->getRequest(); |
|
49 | - |
|
50 | - // Send 401 headers if unauthorised |
|
51 | - if ($result->getStatusCode() === \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED) { |
|
52 | - // If request comes from JS return dummy auth request |
|
53 | - if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
54 | - header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
55 | - } else { |
|
56 | - header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
57 | - } |
|
58 | - http_response_code(401); |
|
59 | - } |
|
60 | - |
|
61 | - foreach ($result->getHeaders() as $name => $value) { |
|
62 | - header($name . ': ' . $value); |
|
63 | - } |
|
64 | - |
|
65 | - $meta = $result->getMeta(); |
|
66 | - $data = $result->getData(); |
|
67 | - if (self::isV2($request)) { |
|
68 | - $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
69 | - if (!is_null($statusCode)) { |
|
70 | - $meta['statuscode'] = $statusCode; |
|
71 | - http_response_code($statusCode); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - self::setContentType($format); |
|
76 | - $body = self::renderResult($format, $meta, $data); |
|
77 | - echo $body; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param XMLWriter $writer |
|
82 | - */ |
|
83 | - private static function toXML($array, $writer) { |
|
84 | - foreach ($array as $k => $v) { |
|
85 | - if ($k[0] === '@') { |
|
86 | - $writer->writeAttribute(substr($k, 1), $v); |
|
87 | - continue; |
|
88 | - } elseif (is_numeric($k)) { |
|
89 | - $k = 'element'; |
|
90 | - } |
|
91 | - if (is_array($v)) { |
|
92 | - $writer->startElement($k); |
|
93 | - self::toXML($v, $writer); |
|
94 | - $writer->endElement(); |
|
95 | - } else { |
|
96 | - $writer->writeElement($k, $v); |
|
97 | - } |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public static function requestedFormat() { |
|
105 | - $formats = ['json', 'xml']; |
|
106 | - |
|
107 | - $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
108 | - return $format; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Based on the requested format the response content type is set |
|
113 | - * @param string $format |
|
114 | - */ |
|
115 | - public static function setContentType($format = null) { |
|
116 | - $format = is_null($format) ? self::requestedFormat() : $format; |
|
117 | - if ($format === 'xml') { |
|
118 | - header('Content-type: text/xml; charset=UTF-8'); |
|
119 | - return; |
|
120 | - } |
|
121 | - |
|
122 | - if ($format === 'json') { |
|
123 | - header('Content-Type: application/json; charset=utf-8'); |
|
124 | - return; |
|
125 | - } |
|
126 | - |
|
127 | - header('Content-Type: application/octet-stream; charset=utf-8'); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @param \OCP\IRequest $request |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - protected static function isV2(\OCP\IRequest $request) { |
|
135 | - $script = $request->getScriptName(); |
|
136 | - |
|
137 | - return substr($script, -11) === '/ocs/v2.php'; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param integer $sc |
|
142 | - * @return int |
|
143 | - */ |
|
144 | - public static function mapStatusCodes($sc) { |
|
145 | - switch ($sc) { |
|
146 | - case \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND: |
|
147 | - return Http::STATUS_NOT_FOUND; |
|
148 | - case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR: |
|
149 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
150 | - case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR: |
|
151 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
152 | - case \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED: |
|
153 | - // already handled for v1 |
|
154 | - return null; |
|
155 | - case 100: |
|
156 | - return Http::STATUS_OK; |
|
157 | - } |
|
158 | - // any 2xx, 4xx and 5xx will be used as is |
|
159 | - if ($sc >= 200 && $sc < 600) { |
|
160 | - return $sc; |
|
161 | - } |
|
162 | - |
|
163 | - return Http::STATUS_BAD_REQUEST; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param string $format |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public static function renderResult($format, $meta, $data) { |
|
171 | - $response = [ |
|
172 | - 'ocs' => [ |
|
173 | - 'meta' => $meta, |
|
174 | - 'data' => $data, |
|
175 | - ], |
|
176 | - ]; |
|
177 | - if ($format == 'json') { |
|
178 | - return OC_JSON::encode($response); |
|
179 | - } |
|
180 | - |
|
181 | - $writer = new XMLWriter(); |
|
182 | - $writer->openMemory(); |
|
183 | - $writer->setIndent(true); |
|
184 | - $writer->startDocument(); |
|
185 | - self::toXML($response, $writer); |
|
186 | - $writer->endDocument(); |
|
187 | - return $writer->outputMemory(true); |
|
188 | - } |
|
36 | + /** |
|
37 | + * api actions |
|
38 | + */ |
|
39 | + protected static $actions = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * respond to a call |
|
43 | + * @param \OC\OCS\Result $result |
|
44 | + * @param string $format the format xml|json |
|
45 | + * @psalm-taint-escape html |
|
46 | + */ |
|
47 | + public static function respond($result, $format = 'xml') { |
|
48 | + $request = \OC::$server->getRequest(); |
|
49 | + |
|
50 | + // Send 401 headers if unauthorised |
|
51 | + if ($result->getStatusCode() === \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED) { |
|
52 | + // If request comes from JS return dummy auth request |
|
53 | + if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
54 | + header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
55 | + } else { |
|
56 | + header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
57 | + } |
|
58 | + http_response_code(401); |
|
59 | + } |
|
60 | + |
|
61 | + foreach ($result->getHeaders() as $name => $value) { |
|
62 | + header($name . ': ' . $value); |
|
63 | + } |
|
64 | + |
|
65 | + $meta = $result->getMeta(); |
|
66 | + $data = $result->getData(); |
|
67 | + if (self::isV2($request)) { |
|
68 | + $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
69 | + if (!is_null($statusCode)) { |
|
70 | + $meta['statuscode'] = $statusCode; |
|
71 | + http_response_code($statusCode); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + self::setContentType($format); |
|
76 | + $body = self::renderResult($format, $meta, $data); |
|
77 | + echo $body; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param XMLWriter $writer |
|
82 | + */ |
|
83 | + private static function toXML($array, $writer) { |
|
84 | + foreach ($array as $k => $v) { |
|
85 | + if ($k[0] === '@') { |
|
86 | + $writer->writeAttribute(substr($k, 1), $v); |
|
87 | + continue; |
|
88 | + } elseif (is_numeric($k)) { |
|
89 | + $k = 'element'; |
|
90 | + } |
|
91 | + if (is_array($v)) { |
|
92 | + $writer->startElement($k); |
|
93 | + self::toXML($v, $writer); |
|
94 | + $writer->endElement(); |
|
95 | + } else { |
|
96 | + $writer->writeElement($k, $v); |
|
97 | + } |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public static function requestedFormat() { |
|
105 | + $formats = ['json', 'xml']; |
|
106 | + |
|
107 | + $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
108 | + return $format; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Based on the requested format the response content type is set |
|
113 | + * @param string $format |
|
114 | + */ |
|
115 | + public static function setContentType($format = null) { |
|
116 | + $format = is_null($format) ? self::requestedFormat() : $format; |
|
117 | + if ($format === 'xml') { |
|
118 | + header('Content-type: text/xml; charset=UTF-8'); |
|
119 | + return; |
|
120 | + } |
|
121 | + |
|
122 | + if ($format === 'json') { |
|
123 | + header('Content-Type: application/json; charset=utf-8'); |
|
124 | + return; |
|
125 | + } |
|
126 | + |
|
127 | + header('Content-Type: application/octet-stream; charset=utf-8'); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @param \OCP\IRequest $request |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + protected static function isV2(\OCP\IRequest $request) { |
|
135 | + $script = $request->getScriptName(); |
|
136 | + |
|
137 | + return substr($script, -11) === '/ocs/v2.php'; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param integer $sc |
|
142 | + * @return int |
|
143 | + */ |
|
144 | + public static function mapStatusCodes($sc) { |
|
145 | + switch ($sc) { |
|
146 | + case \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND: |
|
147 | + return Http::STATUS_NOT_FOUND; |
|
148 | + case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR: |
|
149 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
150 | + case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR: |
|
151 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
152 | + case \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED: |
|
153 | + // already handled for v1 |
|
154 | + return null; |
|
155 | + case 100: |
|
156 | + return Http::STATUS_OK; |
|
157 | + } |
|
158 | + // any 2xx, 4xx and 5xx will be used as is |
|
159 | + if ($sc >= 200 && $sc < 600) { |
|
160 | + return $sc; |
|
161 | + } |
|
162 | + |
|
163 | + return Http::STATUS_BAD_REQUEST; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param string $format |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public static function renderResult($format, $meta, $data) { |
|
171 | + $response = [ |
|
172 | + 'ocs' => [ |
|
173 | + 'meta' => $meta, |
|
174 | + 'data' => $data, |
|
175 | + ], |
|
176 | + ]; |
|
177 | + if ($format == 'json') { |
|
178 | + return OC_JSON::encode($response); |
|
179 | + } |
|
180 | + |
|
181 | + $writer = new XMLWriter(); |
|
182 | + $writer->openMemory(); |
|
183 | + $writer->setIndent(true); |
|
184 | + $writer->startDocument(); |
|
185 | + self::toXML($response, $writer); |
|
186 | + $writer->endDocument(); |
|
187 | + return $writer->outputMemory(true); |
|
188 | + } |
|
189 | 189 | } |