@@ -37,466 +37,466 @@ |
||
| 37 | 37 | |
| 38 | 38 | class OC_API { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * API authentication levels |
|
| 42 | - */ |
|
| 43 | - |
|
| 44 | - /** @deprecated Use \OCP\API::GUEST_AUTH instead */ |
|
| 45 | - const GUEST_AUTH = 0; |
|
| 46 | - |
|
| 47 | - /** @deprecated Use \OCP\API::USER_AUTH instead */ |
|
| 48 | - const USER_AUTH = 1; |
|
| 49 | - |
|
| 50 | - /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */ |
|
| 51 | - const SUBADMIN_AUTH = 2; |
|
| 52 | - |
|
| 53 | - /** @deprecated Use \OCP\API::ADMIN_AUTH instead */ |
|
| 54 | - const ADMIN_AUTH = 3; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * API Response Codes |
|
| 58 | - */ |
|
| 59 | - |
|
| 60 | - /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */ |
|
| 61 | - const RESPOND_UNAUTHORISED = 997; |
|
| 62 | - |
|
| 63 | - /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */ |
|
| 64 | - const RESPOND_SERVER_ERROR = 996; |
|
| 65 | - |
|
| 66 | - /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */ |
|
| 67 | - const RESPOND_NOT_FOUND = 998; |
|
| 68 | - |
|
| 69 | - /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */ |
|
| 70 | - const RESPOND_UNKNOWN_ERROR = 999; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * api actions |
|
| 74 | - */ |
|
| 75 | - protected static $actions = array(); |
|
| 76 | - private static $logoutRequired = false; |
|
| 77 | - private static $isLoggedIn = false; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * registers an api call |
|
| 81 | - * @param string $method the http method |
|
| 82 | - * @param string $url the url to match |
|
| 83 | - * @param callable $action the function to run |
|
| 84 | - * @param string $app the id of the app registering the call |
|
| 85 | - * @param int $authLevel the level of authentication required for the call |
|
| 86 | - * @param array $defaults |
|
| 87 | - * @param array $requirements |
|
| 88 | - */ |
|
| 89 | - public static function register($method, $url, $action, $app, |
|
| 90 | - $authLevel = API::USER_AUTH, |
|
| 91 | - $defaults = array(), |
|
| 92 | - $requirements = array()) { |
|
| 93 | - $name = strtolower($method).$url; |
|
| 94 | - $name = str_replace(array('/', '{', '}'), '_', $name); |
|
| 95 | - if(!isset(self::$actions[$name])) { |
|
| 96 | - $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
|
| 97 | - OC::$server->getRouter()->useCollection('ocs'); |
|
| 98 | - OC::$server->getRouter()->create($name, $url) |
|
| 99 | - ->method($method) |
|
| 100 | - ->defaults($defaults) |
|
| 101 | - ->requirements($requirements) |
|
| 102 | - ->action('OC_API', 'call'); |
|
| 103 | - self::$actions[$name] = array(); |
|
| 104 | - OC::$server->getRouter()->useCollection($oldCollection); |
|
| 105 | - } |
|
| 106 | - self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * handles an api call |
|
| 111 | - * @param array $parameters |
|
| 112 | - */ |
|
| 113 | - public static function call($parameters) { |
|
| 114 | - $request = \OC::$server->getRequest(); |
|
| 115 | - $method = $request->getMethod(); |
|
| 116 | - |
|
| 117 | - // Prepare the request variables |
|
| 118 | - if($method === 'PUT') { |
|
| 119 | - $parameters['_put'] = $request->getParams(); |
|
| 120 | - } else if($method === 'DELETE') { |
|
| 121 | - $parameters['_delete'] = $request->getParams(); |
|
| 122 | - } |
|
| 123 | - $name = $parameters['_route']; |
|
| 124 | - // Foreach registered action |
|
| 125 | - $responses = array(); |
|
| 126 | - $appManager = \OC::$server->getAppManager(); |
|
| 127 | - foreach(self::$actions[$name] as $action) { |
|
| 128 | - // Check authentication and availability |
|
| 129 | - if(!self::isAuthorised($action)) { |
|
| 130 | - $responses[] = array( |
|
| 131 | - 'app' => $action['app'], |
|
| 132 | - 'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
|
| 133 | - 'shipped' => $appManager->isShipped($action['app']), |
|
| 134 | - ); |
|
| 135 | - continue; |
|
| 136 | - } |
|
| 137 | - if(!is_callable($action['action'])) { |
|
| 138 | - $responses[] = array( |
|
| 139 | - 'app' => $action['app'], |
|
| 140 | - 'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
|
| 141 | - 'shipped' => $appManager->isShipped($action['app']), |
|
| 142 | - ); |
|
| 143 | - continue; |
|
| 144 | - } |
|
| 145 | - // Run the action |
|
| 146 | - $responses[] = array( |
|
| 147 | - 'app' => $action['app'], |
|
| 148 | - 'response' => call_user_func($action['action'], $parameters), |
|
| 149 | - 'shipped' => $appManager->isShipped($action['app']), |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - $response = self::mergeResponses($responses); |
|
| 153 | - $format = self::requestedFormat(); |
|
| 154 | - if (self::$logoutRequired) { |
|
| 155 | - \OC::$server->getUserSession()->logout(); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - self::respond($response, $format); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * merge the returned result objects into one response |
|
| 163 | - * @param array $responses |
|
| 164 | - * @return \OC\OCS\Result |
|
| 165 | - */ |
|
| 166 | - public static function mergeResponses($responses) { |
|
| 167 | - // Sort into shipped and third-party |
|
| 168 | - $shipped = array( |
|
| 169 | - 'succeeded' => array(), |
|
| 170 | - 'failed' => array(), |
|
| 171 | - ); |
|
| 172 | - $thirdparty = array( |
|
| 173 | - 'succeeded' => array(), |
|
| 174 | - 'failed' => array(), |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - foreach($responses as $response) { |
|
| 178 | - if($response['shipped'] || ($response['app'] === 'core')) { |
|
| 179 | - if($response['response']->succeeded()) { |
|
| 180 | - $shipped['succeeded'][$response['app']] = $response; |
|
| 181 | - } else { |
|
| 182 | - $shipped['failed'][$response['app']] = $response; |
|
| 183 | - } |
|
| 184 | - } else { |
|
| 185 | - if($response['response']->succeeded()) { |
|
| 186 | - $thirdparty['succeeded'][$response['app']] = $response; |
|
| 187 | - } else { |
|
| 188 | - $thirdparty['failed'][$response['app']] = $response; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - // Remove any error responses if there is one shipped response that succeeded |
|
| 194 | - if(!empty($shipped['failed'])) { |
|
| 195 | - // Which shipped response do we use if they all failed? |
|
| 196 | - // They may have failed for different reasons (different status codes) |
|
| 197 | - // Which response code should we return? |
|
| 198 | - // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
|
| 199 | - // Merge failed responses if more than one |
|
| 200 | - $data = array(); |
|
| 201 | - foreach($shipped['failed'] as $failure) { |
|
| 202 | - $data = array_merge_recursive($data, $failure['response']->getData()); |
|
| 203 | - } |
|
| 204 | - $picked = reset($shipped['failed']); |
|
| 205 | - $code = $picked['response']->getStatusCode(); |
|
| 206 | - $meta = $picked['response']->getMeta(); |
|
| 207 | - $headers = $picked['response']->getHeaders(); |
|
| 208 | - $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers); |
|
| 209 | - return $response; |
|
| 210 | - } elseif(!empty($shipped['succeeded'])) { |
|
| 211 | - $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
|
| 212 | - } elseif(!empty($thirdparty['failed'])) { |
|
| 213 | - // Merge failed responses if more than one |
|
| 214 | - $data = array(); |
|
| 215 | - foreach($thirdparty['failed'] as $failure) { |
|
| 216 | - $data = array_merge_recursive($data, $failure['response']->getData()); |
|
| 217 | - } |
|
| 218 | - $picked = reset($thirdparty['failed']); |
|
| 219 | - $code = $picked['response']->getStatusCode(); |
|
| 220 | - $meta = $picked['response']->getMeta(); |
|
| 221 | - $headers = $picked['response']->getHeaders(); |
|
| 222 | - $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers); |
|
| 223 | - return $response; |
|
| 224 | - } else { |
|
| 225 | - $responses = $thirdparty['succeeded']; |
|
| 226 | - } |
|
| 227 | - // Merge the successful responses |
|
| 228 | - $data = []; |
|
| 229 | - $codes = []; |
|
| 230 | - $header = []; |
|
| 231 | - |
|
| 232 | - foreach($responses as $response) { |
|
| 233 | - if($response['shipped']) { |
|
| 234 | - $data = array_merge_recursive($response['response']->getData(), $data); |
|
| 235 | - } else { |
|
| 236 | - $data = array_merge_recursive($data, $response['response']->getData()); |
|
| 237 | - } |
|
| 238 | - $header = array_merge_recursive($header, $response['response']->getHeaders()); |
|
| 239 | - $codes[] = ['code' => $response['response']->getStatusCode(), |
|
| 240 | - 'meta' => $response['response']->getMeta()]; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // Use any non 100 status codes |
|
| 244 | - $statusCode = 100; |
|
| 245 | - $statusMessage = null; |
|
| 246 | - foreach($codes as $code) { |
|
| 247 | - if($code['code'] != 100) { |
|
| 248 | - $statusCode = $code['code']; |
|
| 249 | - $statusMessage = $code['meta']['message']; |
|
| 250 | - break; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return new \OC\OCS\Result($data, $statusCode, $statusMessage, $header); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * authenticate the api call |
|
| 259 | - * @param array $action the action details as supplied to OC_API::register() |
|
| 260 | - * @return bool |
|
| 261 | - */ |
|
| 262 | - private static function isAuthorised($action) { |
|
| 263 | - $level = $action['authlevel']; |
|
| 264 | - switch($level) { |
|
| 265 | - case API::GUEST_AUTH: |
|
| 266 | - // Anyone can access |
|
| 267 | - return true; |
|
| 268 | - case API::USER_AUTH: |
|
| 269 | - // User required |
|
| 270 | - return self::loginUser(); |
|
| 271 | - case API::SUBADMIN_AUTH: |
|
| 272 | - // Check for subadmin |
|
| 273 | - $user = self::loginUser(); |
|
| 274 | - if(!$user) { |
|
| 275 | - return false; |
|
| 276 | - } else { |
|
| 277 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
| 278 | - if($userObject === null) { |
|
| 279 | - return false; |
|
| 280 | - } |
|
| 281 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
| 282 | - $admin = OC_User::isAdminUser($user); |
|
| 283 | - if($isSubAdmin || $admin) { |
|
| 284 | - return true; |
|
| 285 | - } else { |
|
| 286 | - return false; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - case API::ADMIN_AUTH: |
|
| 290 | - // Check for admin |
|
| 291 | - $user = self::loginUser(); |
|
| 292 | - if(!$user) { |
|
| 293 | - return false; |
|
| 294 | - } else { |
|
| 295 | - return OC_User::isAdminUser($user); |
|
| 296 | - } |
|
| 297 | - default: |
|
| 298 | - // oops looks like invalid level supplied |
|
| 299 | - return false; |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * http basic auth |
|
| 305 | - * @return string|false (username, or false on failure) |
|
| 306 | - */ |
|
| 307 | - private static function loginUser() { |
|
| 308 | - if(self::$isLoggedIn === true) { |
|
| 309 | - return \OC_User::getUser(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - // reuse existing login |
|
| 313 | - $loggedIn = \OC::$server->getUserSession()->isLoggedIn(); |
|
| 314 | - if ($loggedIn === true) { |
|
| 315 | - if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
| 316 | - // Do not allow access to OCS until the 2FA challenge was solved successfully |
|
| 317 | - return false; |
|
| 318 | - } |
|
| 319 | - $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false; |
|
| 320 | - if ($ocsApiRequest) { |
|
| 321 | - |
|
| 322 | - // initialize the user's filesystem |
|
| 323 | - \OC_Util::setupFS(\OC_User::getUser()); |
|
| 324 | - self::$isLoggedIn = true; |
|
| 325 | - |
|
| 326 | - return OC_User::getUser(); |
|
| 327 | - } |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - // basic auth - because OC_User::login will create a new session we shall only try to login |
|
| 332 | - // if user and pass are set |
|
| 333 | - $userSession = \OC::$server->getUserSession(); |
|
| 334 | - $request = \OC::$server->getRequest(); |
|
| 335 | - try { |
|
| 336 | - if ($userSession->tryTokenLogin($request) |
|
| 337 | - || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) { |
|
| 338 | - self::$logoutRequired = true; |
|
| 339 | - } else { |
|
| 340 | - return false; |
|
| 341 | - } |
|
| 342 | - // initialize the user's filesystem |
|
| 343 | - \OC_Util::setupFS(\OC_User::getUser()); |
|
| 344 | - self::$isLoggedIn = true; |
|
| 345 | - |
|
| 346 | - return \OC_User::getUser(); |
|
| 347 | - } catch (\OC\User\LoginException $e) { |
|
| 348 | - return false; |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * respond to a call |
|
| 354 | - * @param \OC\OCS\Result $result |
|
| 355 | - * @param string $format the format xml|json |
|
| 356 | - */ |
|
| 357 | - public static function respond($result, $format='xml') { |
|
| 358 | - $request = \OC::$server->getRequest(); |
|
| 359 | - |
|
| 360 | - // Send 401 headers if unauthorised |
|
| 361 | - if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
| 362 | - // If request comes from JS return dummy auth request |
|
| 363 | - if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
| 364 | - header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
| 365 | - } else { |
|
| 366 | - header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
| 367 | - } |
|
| 368 | - header('HTTP/1.0 401 Unauthorized'); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - foreach($result->getHeaders() as $name => $value) { |
|
| 372 | - header($name . ': ' . $value); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - $meta = $result->getMeta(); |
|
| 376 | - $data = $result->getData(); |
|
| 377 | - if (self::isV2($request)) { |
|
| 378 | - $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
| 379 | - if (!is_null($statusCode)) { |
|
| 380 | - $meta['statuscode'] = $statusCode; |
|
| 381 | - OC_Response::setStatus($statusCode); |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - self::setContentType($format); |
|
| 386 | - $body = self::renderResult($format, $meta, $data); |
|
| 387 | - echo $body; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * @param XMLWriter $writer |
|
| 392 | - */ |
|
| 393 | - private static function toXML($array, $writer) { |
|
| 394 | - foreach($array as $k => $v) { |
|
| 395 | - if ($k[0] === '@') { |
|
| 396 | - $writer->writeAttribute(substr($k, 1), $v); |
|
| 397 | - continue; |
|
| 398 | - } else if (is_numeric($k)) { |
|
| 399 | - $k = 'element'; |
|
| 400 | - } |
|
| 401 | - if(is_array($v)) { |
|
| 402 | - $writer->startElement($k); |
|
| 403 | - self::toXML($v, $writer); |
|
| 404 | - $writer->endElement(); |
|
| 405 | - } else { |
|
| 406 | - $writer->writeElement($k, $v); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * @return string |
|
| 413 | - */ |
|
| 414 | - public static function requestedFormat() { |
|
| 415 | - $formats = array('json', 'xml'); |
|
| 416 | - |
|
| 417 | - $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
| 418 | - return $format; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Based on the requested format the response content type is set |
|
| 423 | - * @param string $format |
|
| 424 | - */ |
|
| 425 | - public static function setContentType($format = null) { |
|
| 426 | - $format = is_null($format) ? self::requestedFormat() : $format; |
|
| 427 | - if ($format === 'xml') { |
|
| 428 | - header('Content-type: text/xml; charset=UTF-8'); |
|
| 429 | - return; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - if ($format === 'json') { |
|
| 433 | - header('Content-Type: application/json; charset=utf-8'); |
|
| 434 | - return; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - header('Content-Type: application/octet-stream; charset=utf-8'); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * @param \OCP\IRequest $request |
|
| 442 | - * @return bool |
|
| 443 | - */ |
|
| 444 | - protected static function isV2(\OCP\IRequest $request) { |
|
| 445 | - $script = $request->getScriptName(); |
|
| 446 | - |
|
| 447 | - return substr($script, -11) === '/ocs/v2.php'; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * @param integer $sc |
|
| 452 | - * @return int |
|
| 453 | - */ |
|
| 454 | - public static function mapStatusCodes($sc) { |
|
| 455 | - switch ($sc) { |
|
| 456 | - case API::RESPOND_NOT_FOUND: |
|
| 457 | - return Http::STATUS_NOT_FOUND; |
|
| 458 | - case API::RESPOND_SERVER_ERROR: |
|
| 459 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
| 460 | - case API::RESPOND_UNKNOWN_ERROR: |
|
| 461 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
| 462 | - case API::RESPOND_UNAUTHORISED: |
|
| 463 | - // already handled for v1 |
|
| 464 | - return null; |
|
| 465 | - case 100: |
|
| 466 | - return Http::STATUS_OK; |
|
| 467 | - } |
|
| 468 | - // any 2xx, 4xx and 5xx will be used as is |
|
| 469 | - if ($sc >= 200 && $sc < 600) { |
|
| 470 | - return $sc; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - return Http::STATUS_BAD_REQUEST; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * @param string $format |
|
| 478 | - * @return string |
|
| 479 | - */ |
|
| 480 | - public static function renderResult($format, $meta, $data) { |
|
| 481 | - $response = array( |
|
| 482 | - 'ocs' => array( |
|
| 483 | - 'meta' => $meta, |
|
| 484 | - 'data' => $data, |
|
| 485 | - ), |
|
| 486 | - ); |
|
| 487 | - if ($format == 'json') { |
|
| 488 | - if (is_array($response)) { |
|
| 489 | - array_walk_recursive($response, array('OC_JSON', 'to_string')); |
|
| 490 | - } |
|
| 491 | - return json_encode($response, JSON_HEX_TAG); |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - $writer = new XMLWriter(); |
|
| 495 | - $writer->openMemory(); |
|
| 496 | - $writer->setIndent(true); |
|
| 497 | - $writer->startDocument(); |
|
| 498 | - self::toXML($response, $writer); |
|
| 499 | - $writer->endDocument(); |
|
| 500 | - return $writer->outputMemory(true); |
|
| 501 | - } |
|
| 40 | + /** |
|
| 41 | + * API authentication levels |
|
| 42 | + */ |
|
| 43 | + |
|
| 44 | + /** @deprecated Use \OCP\API::GUEST_AUTH instead */ |
|
| 45 | + const GUEST_AUTH = 0; |
|
| 46 | + |
|
| 47 | + /** @deprecated Use \OCP\API::USER_AUTH instead */ |
|
| 48 | + const USER_AUTH = 1; |
|
| 49 | + |
|
| 50 | + /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */ |
|
| 51 | + const SUBADMIN_AUTH = 2; |
|
| 52 | + |
|
| 53 | + /** @deprecated Use \OCP\API::ADMIN_AUTH instead */ |
|
| 54 | + const ADMIN_AUTH = 3; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * API Response Codes |
|
| 58 | + */ |
|
| 59 | + |
|
| 60 | + /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */ |
|
| 61 | + const RESPOND_UNAUTHORISED = 997; |
|
| 62 | + |
|
| 63 | + /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */ |
|
| 64 | + const RESPOND_SERVER_ERROR = 996; |
|
| 65 | + |
|
| 66 | + /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */ |
|
| 67 | + const RESPOND_NOT_FOUND = 998; |
|
| 68 | + |
|
| 69 | + /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */ |
|
| 70 | + const RESPOND_UNKNOWN_ERROR = 999; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * api actions |
|
| 74 | + */ |
|
| 75 | + protected static $actions = array(); |
|
| 76 | + private static $logoutRequired = false; |
|
| 77 | + private static $isLoggedIn = false; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * registers an api call |
|
| 81 | + * @param string $method the http method |
|
| 82 | + * @param string $url the url to match |
|
| 83 | + * @param callable $action the function to run |
|
| 84 | + * @param string $app the id of the app registering the call |
|
| 85 | + * @param int $authLevel the level of authentication required for the call |
|
| 86 | + * @param array $defaults |
|
| 87 | + * @param array $requirements |
|
| 88 | + */ |
|
| 89 | + public static function register($method, $url, $action, $app, |
|
| 90 | + $authLevel = API::USER_AUTH, |
|
| 91 | + $defaults = array(), |
|
| 92 | + $requirements = array()) { |
|
| 93 | + $name = strtolower($method).$url; |
|
| 94 | + $name = str_replace(array('/', '{', '}'), '_', $name); |
|
| 95 | + if(!isset(self::$actions[$name])) { |
|
| 96 | + $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
|
| 97 | + OC::$server->getRouter()->useCollection('ocs'); |
|
| 98 | + OC::$server->getRouter()->create($name, $url) |
|
| 99 | + ->method($method) |
|
| 100 | + ->defaults($defaults) |
|
| 101 | + ->requirements($requirements) |
|
| 102 | + ->action('OC_API', 'call'); |
|
| 103 | + self::$actions[$name] = array(); |
|
| 104 | + OC::$server->getRouter()->useCollection($oldCollection); |
|
| 105 | + } |
|
| 106 | + self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * handles an api call |
|
| 111 | + * @param array $parameters |
|
| 112 | + */ |
|
| 113 | + public static function call($parameters) { |
|
| 114 | + $request = \OC::$server->getRequest(); |
|
| 115 | + $method = $request->getMethod(); |
|
| 116 | + |
|
| 117 | + // Prepare the request variables |
|
| 118 | + if($method === 'PUT') { |
|
| 119 | + $parameters['_put'] = $request->getParams(); |
|
| 120 | + } else if($method === 'DELETE') { |
|
| 121 | + $parameters['_delete'] = $request->getParams(); |
|
| 122 | + } |
|
| 123 | + $name = $parameters['_route']; |
|
| 124 | + // Foreach registered action |
|
| 125 | + $responses = array(); |
|
| 126 | + $appManager = \OC::$server->getAppManager(); |
|
| 127 | + foreach(self::$actions[$name] as $action) { |
|
| 128 | + // Check authentication and availability |
|
| 129 | + if(!self::isAuthorised($action)) { |
|
| 130 | + $responses[] = array( |
|
| 131 | + 'app' => $action['app'], |
|
| 132 | + 'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
|
| 133 | + 'shipped' => $appManager->isShipped($action['app']), |
|
| 134 | + ); |
|
| 135 | + continue; |
|
| 136 | + } |
|
| 137 | + if(!is_callable($action['action'])) { |
|
| 138 | + $responses[] = array( |
|
| 139 | + 'app' => $action['app'], |
|
| 140 | + 'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
|
| 141 | + 'shipped' => $appManager->isShipped($action['app']), |
|
| 142 | + ); |
|
| 143 | + continue; |
|
| 144 | + } |
|
| 145 | + // Run the action |
|
| 146 | + $responses[] = array( |
|
| 147 | + 'app' => $action['app'], |
|
| 148 | + 'response' => call_user_func($action['action'], $parameters), |
|
| 149 | + 'shipped' => $appManager->isShipped($action['app']), |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + $response = self::mergeResponses($responses); |
|
| 153 | + $format = self::requestedFormat(); |
|
| 154 | + if (self::$logoutRequired) { |
|
| 155 | + \OC::$server->getUserSession()->logout(); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + self::respond($response, $format); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * merge the returned result objects into one response |
|
| 163 | + * @param array $responses |
|
| 164 | + * @return \OC\OCS\Result |
|
| 165 | + */ |
|
| 166 | + public static function mergeResponses($responses) { |
|
| 167 | + // Sort into shipped and third-party |
|
| 168 | + $shipped = array( |
|
| 169 | + 'succeeded' => array(), |
|
| 170 | + 'failed' => array(), |
|
| 171 | + ); |
|
| 172 | + $thirdparty = array( |
|
| 173 | + 'succeeded' => array(), |
|
| 174 | + 'failed' => array(), |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + foreach($responses as $response) { |
|
| 178 | + if($response['shipped'] || ($response['app'] === 'core')) { |
|
| 179 | + if($response['response']->succeeded()) { |
|
| 180 | + $shipped['succeeded'][$response['app']] = $response; |
|
| 181 | + } else { |
|
| 182 | + $shipped['failed'][$response['app']] = $response; |
|
| 183 | + } |
|
| 184 | + } else { |
|
| 185 | + if($response['response']->succeeded()) { |
|
| 186 | + $thirdparty['succeeded'][$response['app']] = $response; |
|
| 187 | + } else { |
|
| 188 | + $thirdparty['failed'][$response['app']] = $response; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + // Remove any error responses if there is one shipped response that succeeded |
|
| 194 | + if(!empty($shipped['failed'])) { |
|
| 195 | + // Which shipped response do we use if they all failed? |
|
| 196 | + // They may have failed for different reasons (different status codes) |
|
| 197 | + // Which response code should we return? |
|
| 198 | + // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
|
| 199 | + // Merge failed responses if more than one |
|
| 200 | + $data = array(); |
|
| 201 | + foreach($shipped['failed'] as $failure) { |
|
| 202 | + $data = array_merge_recursive($data, $failure['response']->getData()); |
|
| 203 | + } |
|
| 204 | + $picked = reset($shipped['failed']); |
|
| 205 | + $code = $picked['response']->getStatusCode(); |
|
| 206 | + $meta = $picked['response']->getMeta(); |
|
| 207 | + $headers = $picked['response']->getHeaders(); |
|
| 208 | + $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers); |
|
| 209 | + return $response; |
|
| 210 | + } elseif(!empty($shipped['succeeded'])) { |
|
| 211 | + $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
|
| 212 | + } elseif(!empty($thirdparty['failed'])) { |
|
| 213 | + // Merge failed responses if more than one |
|
| 214 | + $data = array(); |
|
| 215 | + foreach($thirdparty['failed'] as $failure) { |
|
| 216 | + $data = array_merge_recursive($data, $failure['response']->getData()); |
|
| 217 | + } |
|
| 218 | + $picked = reset($thirdparty['failed']); |
|
| 219 | + $code = $picked['response']->getStatusCode(); |
|
| 220 | + $meta = $picked['response']->getMeta(); |
|
| 221 | + $headers = $picked['response']->getHeaders(); |
|
| 222 | + $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers); |
|
| 223 | + return $response; |
|
| 224 | + } else { |
|
| 225 | + $responses = $thirdparty['succeeded']; |
|
| 226 | + } |
|
| 227 | + // Merge the successful responses |
|
| 228 | + $data = []; |
|
| 229 | + $codes = []; |
|
| 230 | + $header = []; |
|
| 231 | + |
|
| 232 | + foreach($responses as $response) { |
|
| 233 | + if($response['shipped']) { |
|
| 234 | + $data = array_merge_recursive($response['response']->getData(), $data); |
|
| 235 | + } else { |
|
| 236 | + $data = array_merge_recursive($data, $response['response']->getData()); |
|
| 237 | + } |
|
| 238 | + $header = array_merge_recursive($header, $response['response']->getHeaders()); |
|
| 239 | + $codes[] = ['code' => $response['response']->getStatusCode(), |
|
| 240 | + 'meta' => $response['response']->getMeta()]; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // Use any non 100 status codes |
|
| 244 | + $statusCode = 100; |
|
| 245 | + $statusMessage = null; |
|
| 246 | + foreach($codes as $code) { |
|
| 247 | + if($code['code'] != 100) { |
|
| 248 | + $statusCode = $code['code']; |
|
| 249 | + $statusMessage = $code['meta']['message']; |
|
| 250 | + break; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return new \OC\OCS\Result($data, $statusCode, $statusMessage, $header); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * authenticate the api call |
|
| 259 | + * @param array $action the action details as supplied to OC_API::register() |
|
| 260 | + * @return bool |
|
| 261 | + */ |
|
| 262 | + private static function isAuthorised($action) { |
|
| 263 | + $level = $action['authlevel']; |
|
| 264 | + switch($level) { |
|
| 265 | + case API::GUEST_AUTH: |
|
| 266 | + // Anyone can access |
|
| 267 | + return true; |
|
| 268 | + case API::USER_AUTH: |
|
| 269 | + // User required |
|
| 270 | + return self::loginUser(); |
|
| 271 | + case API::SUBADMIN_AUTH: |
|
| 272 | + // Check for subadmin |
|
| 273 | + $user = self::loginUser(); |
|
| 274 | + if(!$user) { |
|
| 275 | + return false; |
|
| 276 | + } else { |
|
| 277 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
| 278 | + if($userObject === null) { |
|
| 279 | + return false; |
|
| 280 | + } |
|
| 281 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
| 282 | + $admin = OC_User::isAdminUser($user); |
|
| 283 | + if($isSubAdmin || $admin) { |
|
| 284 | + return true; |
|
| 285 | + } else { |
|
| 286 | + return false; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + case API::ADMIN_AUTH: |
|
| 290 | + // Check for admin |
|
| 291 | + $user = self::loginUser(); |
|
| 292 | + if(!$user) { |
|
| 293 | + return false; |
|
| 294 | + } else { |
|
| 295 | + return OC_User::isAdminUser($user); |
|
| 296 | + } |
|
| 297 | + default: |
|
| 298 | + // oops looks like invalid level supplied |
|
| 299 | + return false; |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * http basic auth |
|
| 305 | + * @return string|false (username, or false on failure) |
|
| 306 | + */ |
|
| 307 | + private static function loginUser() { |
|
| 308 | + if(self::$isLoggedIn === true) { |
|
| 309 | + return \OC_User::getUser(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + // reuse existing login |
|
| 313 | + $loggedIn = \OC::$server->getUserSession()->isLoggedIn(); |
|
| 314 | + if ($loggedIn === true) { |
|
| 315 | + if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
| 316 | + // Do not allow access to OCS until the 2FA challenge was solved successfully |
|
| 317 | + return false; |
|
| 318 | + } |
|
| 319 | + $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false; |
|
| 320 | + if ($ocsApiRequest) { |
|
| 321 | + |
|
| 322 | + // initialize the user's filesystem |
|
| 323 | + \OC_Util::setupFS(\OC_User::getUser()); |
|
| 324 | + self::$isLoggedIn = true; |
|
| 325 | + |
|
| 326 | + return OC_User::getUser(); |
|
| 327 | + } |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + // basic auth - because OC_User::login will create a new session we shall only try to login |
|
| 332 | + // if user and pass are set |
|
| 333 | + $userSession = \OC::$server->getUserSession(); |
|
| 334 | + $request = \OC::$server->getRequest(); |
|
| 335 | + try { |
|
| 336 | + if ($userSession->tryTokenLogin($request) |
|
| 337 | + || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) { |
|
| 338 | + self::$logoutRequired = true; |
|
| 339 | + } else { |
|
| 340 | + return false; |
|
| 341 | + } |
|
| 342 | + // initialize the user's filesystem |
|
| 343 | + \OC_Util::setupFS(\OC_User::getUser()); |
|
| 344 | + self::$isLoggedIn = true; |
|
| 345 | + |
|
| 346 | + return \OC_User::getUser(); |
|
| 347 | + } catch (\OC\User\LoginException $e) { |
|
| 348 | + return false; |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * respond to a call |
|
| 354 | + * @param \OC\OCS\Result $result |
|
| 355 | + * @param string $format the format xml|json |
|
| 356 | + */ |
|
| 357 | + public static function respond($result, $format='xml') { |
|
| 358 | + $request = \OC::$server->getRequest(); |
|
| 359 | + |
|
| 360 | + // Send 401 headers if unauthorised |
|
| 361 | + if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
| 362 | + // If request comes from JS return dummy auth request |
|
| 363 | + if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
| 364 | + header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
| 365 | + } else { |
|
| 366 | + header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
| 367 | + } |
|
| 368 | + header('HTTP/1.0 401 Unauthorized'); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + foreach($result->getHeaders() as $name => $value) { |
|
| 372 | + header($name . ': ' . $value); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + $meta = $result->getMeta(); |
|
| 376 | + $data = $result->getData(); |
|
| 377 | + if (self::isV2($request)) { |
|
| 378 | + $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
| 379 | + if (!is_null($statusCode)) { |
|
| 380 | + $meta['statuscode'] = $statusCode; |
|
| 381 | + OC_Response::setStatus($statusCode); |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + self::setContentType($format); |
|
| 386 | + $body = self::renderResult($format, $meta, $data); |
|
| 387 | + echo $body; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * @param XMLWriter $writer |
|
| 392 | + */ |
|
| 393 | + private static function toXML($array, $writer) { |
|
| 394 | + foreach($array as $k => $v) { |
|
| 395 | + if ($k[0] === '@') { |
|
| 396 | + $writer->writeAttribute(substr($k, 1), $v); |
|
| 397 | + continue; |
|
| 398 | + } else if (is_numeric($k)) { |
|
| 399 | + $k = 'element'; |
|
| 400 | + } |
|
| 401 | + if(is_array($v)) { |
|
| 402 | + $writer->startElement($k); |
|
| 403 | + self::toXML($v, $writer); |
|
| 404 | + $writer->endElement(); |
|
| 405 | + } else { |
|
| 406 | + $writer->writeElement($k, $v); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * @return string |
|
| 413 | + */ |
|
| 414 | + public static function requestedFormat() { |
|
| 415 | + $formats = array('json', 'xml'); |
|
| 416 | + |
|
| 417 | + $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
| 418 | + return $format; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Based on the requested format the response content type is set |
|
| 423 | + * @param string $format |
|
| 424 | + */ |
|
| 425 | + public static function setContentType($format = null) { |
|
| 426 | + $format = is_null($format) ? self::requestedFormat() : $format; |
|
| 427 | + if ($format === 'xml') { |
|
| 428 | + header('Content-type: text/xml; charset=UTF-8'); |
|
| 429 | + return; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + if ($format === 'json') { |
|
| 433 | + header('Content-Type: application/json; charset=utf-8'); |
|
| 434 | + return; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + header('Content-Type: application/octet-stream; charset=utf-8'); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * @param \OCP\IRequest $request |
|
| 442 | + * @return bool |
|
| 443 | + */ |
|
| 444 | + protected static function isV2(\OCP\IRequest $request) { |
|
| 445 | + $script = $request->getScriptName(); |
|
| 446 | + |
|
| 447 | + return substr($script, -11) === '/ocs/v2.php'; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * @param integer $sc |
|
| 452 | + * @return int |
|
| 453 | + */ |
|
| 454 | + public static function mapStatusCodes($sc) { |
|
| 455 | + switch ($sc) { |
|
| 456 | + case API::RESPOND_NOT_FOUND: |
|
| 457 | + return Http::STATUS_NOT_FOUND; |
|
| 458 | + case API::RESPOND_SERVER_ERROR: |
|
| 459 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
| 460 | + case API::RESPOND_UNKNOWN_ERROR: |
|
| 461 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
| 462 | + case API::RESPOND_UNAUTHORISED: |
|
| 463 | + // already handled for v1 |
|
| 464 | + return null; |
|
| 465 | + case 100: |
|
| 466 | + return Http::STATUS_OK; |
|
| 467 | + } |
|
| 468 | + // any 2xx, 4xx and 5xx will be used as is |
|
| 469 | + if ($sc >= 200 && $sc < 600) { |
|
| 470 | + return $sc; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + return Http::STATUS_BAD_REQUEST; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * @param string $format |
|
| 478 | + * @return string |
|
| 479 | + */ |
|
| 480 | + public static function renderResult($format, $meta, $data) { |
|
| 481 | + $response = array( |
|
| 482 | + 'ocs' => array( |
|
| 483 | + 'meta' => $meta, |
|
| 484 | + 'data' => $data, |
|
| 485 | + ), |
|
| 486 | + ); |
|
| 487 | + if ($format == 'json') { |
|
| 488 | + if (is_array($response)) { |
|
| 489 | + array_walk_recursive($response, array('OC_JSON', 'to_string')); |
|
| 490 | + } |
|
| 491 | + return json_encode($response, JSON_HEX_TAG); |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + $writer = new XMLWriter(); |
|
| 495 | + $writer->openMemory(); |
|
| 496 | + $writer->setIndent(true); |
|
| 497 | + $writer->startDocument(); |
|
| 498 | + self::toXML($response, $writer); |
|
| 499 | + $writer->endDocument(); |
|
| 500 | + return $writer->outputMemory(true); |
|
| 501 | + } |
|
| 502 | 502 | } |
@@ -37,146 +37,146 @@ |
||
| 37 | 37 | * @deprecated Use a AppFramework JSONResponse instead |
| 38 | 38 | */ |
| 39 | 39 | class OC_JSON{ |
| 40 | - static protected $send_content_type_header = false; |
|
| 41 | - /** |
|
| 42 | - * set Content-Type header to jsonrequest |
|
| 43 | - * @deprecated Use a AppFramework JSONResponse instead |
|
| 44 | - */ |
|
| 45 | - public static function setContentTypeHeader($type='application/json') { |
|
| 46 | - if (!self::$send_content_type_header) { |
|
| 47 | - // We send json data |
|
| 48 | - header( 'Content-Type: '.$type . '; charset=utf-8'); |
|
| 49 | - self::$send_content_type_header = true; |
|
| 50 | - } |
|
| 51 | - } |
|
| 40 | + static protected $send_content_type_header = false; |
|
| 41 | + /** |
|
| 42 | + * set Content-Type header to jsonrequest |
|
| 43 | + * @deprecated Use a AppFramework JSONResponse instead |
|
| 44 | + */ |
|
| 45 | + public static function setContentTypeHeader($type='application/json') { |
|
| 46 | + if (!self::$send_content_type_header) { |
|
| 47 | + // We send json data |
|
| 48 | + header( 'Content-Type: '.$type . '; charset=utf-8'); |
|
| 49 | + self::$send_content_type_header = true; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Check if the app is enabled, send json error msg if not |
|
| 55 | - * @param string $app |
|
| 56 | - * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled. |
|
| 57 | - * @suppress PhanDeprecatedFunction |
|
| 58 | - */ |
|
| 59 | - public static function checkAppEnabled($app) { |
|
| 60 | - if( !\OC::$server->getAppManager()->isEnabledForUser($app)) { |
|
| 61 | - $l = \OC::$server->getL10N('lib'); |
|
| 62 | - self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ))); |
|
| 63 | - exit(); |
|
| 64 | - } |
|
| 65 | - } |
|
| 53 | + /** |
|
| 54 | + * Check if the app is enabled, send json error msg if not |
|
| 55 | + * @param string $app |
|
| 56 | + * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled. |
|
| 57 | + * @suppress PhanDeprecatedFunction |
|
| 58 | + */ |
|
| 59 | + public static function checkAppEnabled($app) { |
|
| 60 | + if( !\OC::$server->getAppManager()->isEnabledForUser($app)) { |
|
| 61 | + $l = \OC::$server->getL10N('lib'); |
|
| 62 | + self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ))); |
|
| 63 | + exit(); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Check if the user is logged in, send json error msg if not |
|
| 69 | - * @deprecated Use annotation based ACLs from the AppFramework instead |
|
| 70 | - * @suppress PhanDeprecatedFunction |
|
| 71 | - */ |
|
| 72 | - public static function checkLoggedIn() { |
|
| 73 | - $twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager(); |
|
| 74 | - if( !\OC::$server->getUserSession()->isLoggedIn() |
|
| 75 | - || $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
| 76 | - $l = \OC::$server->getL10N('lib'); |
|
| 77 | - http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED); |
|
| 78 | - self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 79 | - exit(); |
|
| 80 | - } |
|
| 81 | - } |
|
| 67 | + /** |
|
| 68 | + * Check if the user is logged in, send json error msg if not |
|
| 69 | + * @deprecated Use annotation based ACLs from the AppFramework instead |
|
| 70 | + * @suppress PhanDeprecatedFunction |
|
| 71 | + */ |
|
| 72 | + public static function checkLoggedIn() { |
|
| 73 | + $twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager(); |
|
| 74 | + if( !\OC::$server->getUserSession()->isLoggedIn() |
|
| 75 | + || $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
| 76 | + $l = \OC::$server->getL10N('lib'); |
|
| 77 | + http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED); |
|
| 78 | + self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 79 | + exit(); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Check an ajax get/post call if the request token is valid, send json error msg if not. |
|
| 85 | - * @deprecated Use annotation based CSRF checks from the AppFramework instead |
|
| 86 | - * @suppress PhanDeprecatedFunction |
|
| 87 | - */ |
|
| 88 | - public static function callCheck() { |
|
| 89 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 90 | - header('Location: '.\OC::$WEBROOT); |
|
| 91 | - exit(); |
|
| 92 | - } |
|
| 83 | + /** |
|
| 84 | + * Check an ajax get/post call if the request token is valid, send json error msg if not. |
|
| 85 | + * @deprecated Use annotation based CSRF checks from the AppFramework instead |
|
| 86 | + * @suppress PhanDeprecatedFunction |
|
| 87 | + */ |
|
| 88 | + public static function callCheck() { |
|
| 89 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 90 | + header('Location: '.\OC::$WEBROOT); |
|
| 91 | + exit(); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - if( !\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 95 | - $l = \OC::$server->getL10N('lib'); |
|
| 96 | - self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ))); |
|
| 97 | - exit(); |
|
| 98 | - } |
|
| 99 | - } |
|
| 94 | + if( !\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 95 | + $l = \OC::$server->getL10N('lib'); |
|
| 96 | + self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ))); |
|
| 97 | + exit(); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Check if the user is a admin, send json error msg if not. |
|
| 103 | - * @deprecated Use annotation based ACLs from the AppFramework instead |
|
| 104 | - * @suppress PhanDeprecatedFunction |
|
| 105 | - */ |
|
| 106 | - public static function checkAdminUser() { |
|
| 107 | - if( !OC_User::isAdminUser(OC_User::getUser())) { |
|
| 108 | - $l = \OC::$server->getL10N('lib'); |
|
| 109 | - self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 110 | - exit(); |
|
| 111 | - } |
|
| 112 | - } |
|
| 101 | + /** |
|
| 102 | + * Check if the user is a admin, send json error msg if not. |
|
| 103 | + * @deprecated Use annotation based ACLs from the AppFramework instead |
|
| 104 | + * @suppress PhanDeprecatedFunction |
|
| 105 | + */ |
|
| 106 | + public static function checkAdminUser() { |
|
| 107 | + if( !OC_User::isAdminUser(OC_User::getUser())) { |
|
| 108 | + $l = \OC::$server->getL10N('lib'); |
|
| 109 | + self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 110 | + exit(); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Check if the user is a subadmin, send json error msg if not |
|
| 116 | - * @deprecated Use annotation based ACLs from the AppFramework instead |
|
| 117 | - * @suppress PhanDeprecatedFunction |
|
| 118 | - */ |
|
| 119 | - public static function checkSubAdminUser() { |
|
| 120 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
| 121 | - $isSubAdmin = false; |
|
| 122 | - if($userObject !== null) { |
|
| 123 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
| 124 | - } |
|
| 114 | + /** |
|
| 115 | + * Check if the user is a subadmin, send json error msg if not |
|
| 116 | + * @deprecated Use annotation based ACLs from the AppFramework instead |
|
| 117 | + * @suppress PhanDeprecatedFunction |
|
| 118 | + */ |
|
| 119 | + public static function checkSubAdminUser() { |
|
| 120 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
| 121 | + $isSubAdmin = false; |
|
| 122 | + if($userObject !== null) { |
|
| 123 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - if(!$isSubAdmin) { |
|
| 127 | - $l = \OC::$server->getL10N('lib'); |
|
| 128 | - self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 129 | - exit(); |
|
| 130 | - } |
|
| 131 | - } |
|
| 126 | + if(!$isSubAdmin) { |
|
| 127 | + $l = \OC::$server->getL10N('lib'); |
|
| 128 | + self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 129 | + exit(); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Send json error msg |
|
| 135 | - * @deprecated Use a AppFramework JSONResponse instead |
|
| 136 | - * @suppress PhanDeprecatedFunction |
|
| 137 | - */ |
|
| 138 | - public static function error($data = array()) { |
|
| 139 | - $data['status'] = 'error'; |
|
| 140 | - self::encodedPrint($data); |
|
| 141 | - } |
|
| 133 | + /** |
|
| 134 | + * Send json error msg |
|
| 135 | + * @deprecated Use a AppFramework JSONResponse instead |
|
| 136 | + * @suppress PhanDeprecatedFunction |
|
| 137 | + */ |
|
| 138 | + public static function error($data = array()) { |
|
| 139 | + $data['status'] = 'error'; |
|
| 140 | + self::encodedPrint($data); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Send json success msg |
|
| 145 | - * @deprecated Use a AppFramework JSONResponse instead |
|
| 146 | - * @suppress PhanDeprecatedFunction |
|
| 147 | - */ |
|
| 148 | - public static function success($data = array()) { |
|
| 149 | - $data['status'] = 'success'; |
|
| 150 | - self::encodedPrint($data); |
|
| 151 | - } |
|
| 143 | + /** |
|
| 144 | + * Send json success msg |
|
| 145 | + * @deprecated Use a AppFramework JSONResponse instead |
|
| 146 | + * @suppress PhanDeprecatedFunction |
|
| 147 | + */ |
|
| 148 | + public static function success($data = array()) { |
|
| 149 | + $data['status'] = 'success'; |
|
| 150 | + self::encodedPrint($data); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Convert OC_L10N_String to string, for use in json encodings |
|
| 155 | - */ |
|
| 156 | - protected static function to_string(&$value) { |
|
| 157 | - if ($value instanceof \OC\L10N\L10NString) { |
|
| 158 | - $value = (string)$value; |
|
| 159 | - } |
|
| 160 | - } |
|
| 153 | + /** |
|
| 154 | + * Convert OC_L10N_String to string, for use in json encodings |
|
| 155 | + */ |
|
| 156 | + protected static function to_string(&$value) { |
|
| 157 | + if ($value instanceof \OC\L10N\L10NString) { |
|
| 158 | + $value = (string)$value; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Encode and print $data in json format |
|
| 164 | - * @deprecated Use a AppFramework JSONResponse instead |
|
| 165 | - * @suppress PhanDeprecatedFunction |
|
| 166 | - */ |
|
| 167 | - public static function encodedPrint($data) { |
|
| 168 | - self::setContentTypeHeader(); |
|
| 169 | - echo self::encode($data); |
|
| 170 | - } |
|
| 162 | + /** |
|
| 163 | + * Encode and print $data in json format |
|
| 164 | + * @deprecated Use a AppFramework JSONResponse instead |
|
| 165 | + * @suppress PhanDeprecatedFunction |
|
| 166 | + */ |
|
| 167 | + public static function encodedPrint($data) { |
|
| 168 | + self::setContentTypeHeader(); |
|
| 169 | + echo self::encode($data); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Encode JSON |
|
| 174 | - * @deprecated Use a AppFramework JSONResponse instead |
|
| 175 | - */ |
|
| 176 | - private static function encode($data) { |
|
| 177 | - if (is_array($data)) { |
|
| 178 | - array_walk_recursive($data, array('OC_JSON', 'to_string')); |
|
| 179 | - } |
|
| 180 | - return json_encode($data, JSON_HEX_TAG); |
|
| 181 | - } |
|
| 172 | + /** |
|
| 173 | + * Encode JSON |
|
| 174 | + * @deprecated Use a AppFramework JSONResponse instead |
|
| 175 | + */ |
|
| 176 | + private static function encode($data) { |
|
| 177 | + if (is_array($data)) { |
|
| 178 | + array_walk_recursive($data, array('OC_JSON', 'to_string')); |
|
| 179 | + } |
|
| 180 | + return json_encode($data, JSON_HEX_TAG); |
|
| 181 | + } |
|
| 182 | 182 | } |
@@ -36,16 +36,16 @@ discard block |
||
| 36 | 36 | * Class OC_JSON |
| 37 | 37 | * @deprecated Use a AppFramework JSONResponse instead |
| 38 | 38 | */ |
| 39 | -class OC_JSON{ |
|
| 39 | +class OC_JSON { |
|
| 40 | 40 | static protected $send_content_type_header = false; |
| 41 | 41 | /** |
| 42 | 42 | * set Content-Type header to jsonrequest |
| 43 | 43 | * @deprecated Use a AppFramework JSONResponse instead |
| 44 | 44 | */ |
| 45 | - public static function setContentTypeHeader($type='application/json') { |
|
| 45 | + public static function setContentTypeHeader($type = 'application/json') { |
|
| 46 | 46 | if (!self::$send_content_type_header) { |
| 47 | 47 | // We send json data |
| 48 | - header( 'Content-Type: '.$type . '; charset=utf-8'); |
|
| 48 | + header('Content-Type: '.$type.'; charset=utf-8'); |
|
| 49 | 49 | self::$send_content_type_header = true; |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | * @suppress PhanDeprecatedFunction |
| 58 | 58 | */ |
| 59 | 59 | public static function checkAppEnabled($app) { |
| 60 | - if( !\OC::$server->getAppManager()->isEnabledForUser($app)) { |
|
| 60 | + if (!\OC::$server->getAppManager()->isEnabledForUser($app)) { |
|
| 61 | 61 | $l = \OC::$server->getL10N('lib'); |
| 62 | - self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ))); |
|
| 62 | + self::error(array('data' => array('message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled'))); |
|
| 63 | 63 | exit(); |
| 64 | 64 | } |
| 65 | 65 | } |
@@ -71,11 +71,11 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | public static function checkLoggedIn() { |
| 73 | 73 | $twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager(); |
| 74 | - if( !\OC::$server->getUserSession()->isLoggedIn() |
|
| 74 | + if (!\OC::$server->getUserSession()->isLoggedIn() |
|
| 75 | 75 | || $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
| 76 | 76 | $l = \OC::$server->getL10N('lib'); |
| 77 | 77 | http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED); |
| 78 | - self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 78 | + self::error(array('data' => array('message' => $l->t('Authentication error'), 'error' => 'authentication_error'))); |
|
| 79 | 79 | exit(); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -86,14 +86,14 @@ discard block |
||
| 86 | 86 | * @suppress PhanDeprecatedFunction |
| 87 | 87 | */ |
| 88 | 88 | public static function callCheck() { |
| 89 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 89 | + if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 90 | 90 | header('Location: '.\OC::$WEBROOT); |
| 91 | 91 | exit(); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - if( !\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 94 | + if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 95 | 95 | $l = \OC::$server->getL10N('lib'); |
| 96 | - self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ))); |
|
| 96 | + self::error(array('data' => array('message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired'))); |
|
| 97 | 97 | exit(); |
| 98 | 98 | } |
| 99 | 99 | } |
@@ -104,9 +104,9 @@ discard block |
||
| 104 | 104 | * @suppress PhanDeprecatedFunction |
| 105 | 105 | */ |
| 106 | 106 | public static function checkAdminUser() { |
| 107 | - if( !OC_User::isAdminUser(OC_User::getUser())) { |
|
| 107 | + if (!OC_User::isAdminUser(OC_User::getUser())) { |
|
| 108 | 108 | $l = \OC::$server->getL10N('lib'); |
| 109 | - self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 109 | + self::error(array('data' => array('message' => $l->t('Authentication error'), 'error' => 'authentication_error'))); |
|
| 110 | 110 | exit(); |
| 111 | 111 | } |
| 112 | 112 | } |
@@ -119,13 +119,13 @@ discard block |
||
| 119 | 119 | public static function checkSubAdminUser() { |
| 120 | 120 | $userObject = \OC::$server->getUserSession()->getUser(); |
| 121 | 121 | $isSubAdmin = false; |
| 122 | - if($userObject !== null) { |
|
| 122 | + if ($userObject !== null) { |
|
| 123 | 123 | $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if(!$isSubAdmin) { |
|
| 126 | + if (!$isSubAdmin) { |
|
| 127 | 127 | $l = \OC::$server->getL10N('lib'); |
| 128 | - self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ))); |
|
| 128 | + self::error(array('data' => array('message' => $l->t('Authentication error'), 'error' => 'authentication_error'))); |
|
| 129 | 129 | exit(); |
| 130 | 130 | } |
| 131 | 131 | } |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | protected static function to_string(&$value) { |
| 157 | 157 | if ($value instanceof \OC\L10N\L10NString) { |
| 158 | - $value = (string)$value; |
|
| 158 | + $value = (string) $value; |
|
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | |
@@ -33,106 +33,106 @@ |
||
| 33 | 33 | * use server side events with caution, to many open requests can hang the server |
| 34 | 34 | */ |
| 35 | 35 | class OC_EventSource implements \OCP\IEventSource { |
| 36 | - /** |
|
| 37 | - * @var bool |
|
| 38 | - */ |
|
| 39 | - private $fallback; |
|
| 36 | + /** |
|
| 37 | + * @var bool |
|
| 38 | + */ |
|
| 39 | + private $fallback; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 44 | - private $fallBackId = 0; |
|
| 41 | + /** |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | + private $fallBackId = 0; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @var bool |
|
| 48 | - */ |
|
| 49 | - private $started = false; |
|
| 46 | + /** |
|
| 47 | + * @var bool |
|
| 48 | + */ |
|
| 49 | + private $started = false; |
|
| 50 | 50 | |
| 51 | - protected function init() { |
|
| 52 | - if ($this->started) { |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - $this->started = true; |
|
| 51 | + protected function init() { |
|
| 52 | + if ($this->started) { |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | + $this->started = true; |
|
| 56 | 56 | |
| 57 | - // prevent php output buffering, caching and nginx buffering |
|
| 58 | - OC_Util::obEnd(); |
|
| 59 | - header('Cache-Control: no-cache'); |
|
| 60 | - header('X-Accel-Buffering: no'); |
|
| 61 | - $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
| 62 | - if ($this->fallback) { |
|
| 63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
| 64 | - /** |
|
| 65 | - * FIXME: The default content-security-policy of ownCloud forbids inline |
|
| 66 | - * JavaScript for security reasons. IE starting on Windows 10 will |
|
| 67 | - * however also obey the CSP which will break the event source fallback. |
|
| 68 | - * |
|
| 69 | - * As a workaround thus we set a custom policy which allows the execution |
|
| 70 | - * of inline JavaScript. |
|
| 71 | - * |
|
| 72 | - * @link https://github.com/owncloud/core/issues/14286 |
|
| 73 | - */ |
|
| 74 | - header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
| 75 | - header("Content-Type: text/html"); |
|
| 76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
| 77 | - } else { |
|
| 78 | - header("Content-Type: text/event-stream"); |
|
| 79 | - } |
|
| 80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 81 | - header('Location: '.\OC::$WEBROOT); |
|
| 82 | - exit(); |
|
| 83 | - } |
|
| 84 | - if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 85 | - $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
| 86 | - $this->close(); |
|
| 87 | - exit(); |
|
| 88 | - } |
|
| 89 | - flush(); |
|
| 90 | - } |
|
| 57 | + // prevent php output buffering, caching and nginx buffering |
|
| 58 | + OC_Util::obEnd(); |
|
| 59 | + header('Cache-Control: no-cache'); |
|
| 60 | + header('X-Accel-Buffering: no'); |
|
| 61 | + $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
| 62 | + if ($this->fallback) { |
|
| 63 | + $this->fallBackId = (int)$_GET['fallback_id']; |
|
| 64 | + /** |
|
| 65 | + * FIXME: The default content-security-policy of ownCloud forbids inline |
|
| 66 | + * JavaScript for security reasons. IE starting on Windows 10 will |
|
| 67 | + * however also obey the CSP which will break the event source fallback. |
|
| 68 | + * |
|
| 69 | + * As a workaround thus we set a custom policy which allows the execution |
|
| 70 | + * of inline JavaScript. |
|
| 71 | + * |
|
| 72 | + * @link https://github.com/owncloud/core/issues/14286 |
|
| 73 | + */ |
|
| 74 | + header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
| 75 | + header("Content-Type: text/html"); |
|
| 76 | + echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
| 77 | + } else { |
|
| 78 | + header("Content-Type: text/event-stream"); |
|
| 79 | + } |
|
| 80 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 81 | + header('Location: '.\OC::$WEBROOT); |
|
| 82 | + exit(); |
|
| 83 | + } |
|
| 84 | + if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 85 | + $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
| 86 | + $this->close(); |
|
| 87 | + exit(); |
|
| 88 | + } |
|
| 89 | + flush(); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - private function encode($data) { |
|
| 93 | - if (is_array($data)) { |
|
| 94 | - array_walk_recursive($data, array('OC_JSON', 'to_string')); |
|
| 95 | - } |
|
| 96 | - return json_encode($data, JSON_HEX_TAG); |
|
| 97 | - } |
|
| 92 | + private function encode($data) { |
|
| 93 | + if (is_array($data)) { |
|
| 94 | + array_walk_recursive($data, array('OC_JSON', 'to_string')); |
|
| 95 | + } |
|
| 96 | + return json_encode($data, JSON_HEX_TAG); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * send a message to the client |
|
| 101 | - * |
|
| 102 | - * @param string $type |
|
| 103 | - * @param mixed $data |
|
| 104 | - * |
|
| 105 | - * @throws \BadMethodCallException |
|
| 106 | - * if only one parameter is given, a typeless message will be send with that parameter as data |
|
| 107 | - * @suppress PhanDeprecatedFunction |
|
| 108 | - */ |
|
| 109 | - public function send($type, $data = null) { |
|
| 110 | - if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
| 111 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
| 112 | - } |
|
| 113 | - $this->init(); |
|
| 114 | - if (is_null($data)) { |
|
| 115 | - $data = $type; |
|
| 116 | - $type = null; |
|
| 117 | - } |
|
| 118 | - if ($this->fallback) { |
|
| 119 | - $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
| 120 | - . $this->fallBackId . ',"' . $type . '",' . $this->encode($data) . ')</script>' . PHP_EOL; |
|
| 121 | - echo $response; |
|
| 122 | - } else { |
|
| 123 | - if ($type) { |
|
| 124 | - echo 'event: ' . $type . PHP_EOL; |
|
| 125 | - } |
|
| 126 | - echo 'data: ' . $this->encode($data) . PHP_EOL; |
|
| 127 | - } |
|
| 128 | - echo PHP_EOL; |
|
| 129 | - flush(); |
|
| 130 | - } |
|
| 99 | + /** |
|
| 100 | + * send a message to the client |
|
| 101 | + * |
|
| 102 | + * @param string $type |
|
| 103 | + * @param mixed $data |
|
| 104 | + * |
|
| 105 | + * @throws \BadMethodCallException |
|
| 106 | + * if only one parameter is given, a typeless message will be send with that parameter as data |
|
| 107 | + * @suppress PhanDeprecatedFunction |
|
| 108 | + */ |
|
| 109 | + public function send($type, $data = null) { |
|
| 110 | + if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
| 111 | + throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
| 112 | + } |
|
| 113 | + $this->init(); |
|
| 114 | + if (is_null($data)) { |
|
| 115 | + $data = $type; |
|
| 116 | + $type = null; |
|
| 117 | + } |
|
| 118 | + if ($this->fallback) { |
|
| 119 | + $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
| 120 | + . $this->fallBackId . ',"' . $type . '",' . $this->encode($data) . ')</script>' . PHP_EOL; |
|
| 121 | + echo $response; |
|
| 122 | + } else { |
|
| 123 | + if ($type) { |
|
| 124 | + echo 'event: ' . $type . PHP_EOL; |
|
| 125 | + } |
|
| 126 | + echo 'data: ' . $this->encode($data) . PHP_EOL; |
|
| 127 | + } |
|
| 128 | + echo PHP_EOL; |
|
| 129 | + flush(); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * close the connection of the event source |
|
| 134 | - */ |
|
| 135 | - public function close() { |
|
| 136 | - $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
| 137 | - } |
|
| 132 | + /** |
|
| 133 | + * close the connection of the event source |
|
| 134 | + */ |
|
| 135 | + public function close() { |
|
| 136 | + $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | header('X-Accel-Buffering: no'); |
| 61 | 61 | $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
| 62 | 62 | if ($this->fallback) { |
| 63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
| 63 | + $this->fallBackId = (int) $_GET['fallback_id']; |
|
| 64 | 64 | /** |
| 65 | 65 | * FIXME: The default content-security-policy of ownCloud forbids inline |
| 66 | 66 | * JavaScript for security reasons. IE starting on Windows 10 will |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
| 75 | 75 | header("Content-Type: text/html"); |
| 76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
| 76 | + echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy |
|
| 77 | 77 | } else { |
| 78 | 78 | header("Content-Type: text/event-stream"); |
| 79 | 79 | } |
| 80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 80 | + if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 81 | 81 | header('Location: '.\OC::$WEBROOT); |
| 82 | 82 | exit(); |
| 83 | 83 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | */ |
| 109 | 109 | public function send($type, $data = null) { |
| 110 | 110 | if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
| 111 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
| 111 | + throw new BadMethodCallException('Type needs to be alphanumeric ('.$type.')'); |
|
| 112 | 112 | } |
| 113 | 113 | $this->init(); |
| 114 | 114 | if (is_null($data)) { |
@@ -117,13 +117,13 @@ discard block |
||
| 117 | 117 | } |
| 118 | 118 | if ($this->fallback) { |
| 119 | 119 | $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
| 120 | - . $this->fallBackId . ',"' . $type . '",' . $this->encode($data) . ')</script>' . PHP_EOL; |
|
| 120 | + . $this->fallBackId.',"'.$type.'",'.$this->encode($data).')</script>'.PHP_EOL; |
|
| 121 | 121 | echo $response; |
| 122 | 122 | } else { |
| 123 | 123 | if ($type) { |
| 124 | - echo 'event: ' . $type . PHP_EOL; |
|
| 124 | + echo 'event: '.$type.PHP_EOL; |
|
| 125 | 125 | } |
| 126 | - echo 'data: ' . $this->encode($data) . PHP_EOL; |
|
| 126 | + echo 'data: '.$this->encode($data).PHP_EOL; |
|
| 127 | 127 | } |
| 128 | 128 | echo PHP_EOL; |
| 129 | 129 | flush(); |
@@ -41,136 +41,136 @@ |
||
| 41 | 41 | * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
| 42 | 42 | */ |
| 43 | 43 | class JSON { |
| 44 | - /** |
|
| 45 | - * Check if the user is logged in, send json error msg if not. |
|
| 46 | - * |
|
| 47 | - * This method checks if a user is logged in. If not, a json error |
|
| 48 | - * response will be return and the method will exit from execution |
|
| 49 | - * of the script. |
|
| 50 | - * The returned json will be in the format: |
|
| 51 | - * |
|
| 52 | - * {"status":"error","data":{"message":"Authentication error."}} |
|
| 53 | - * |
|
| 54 | - * Add this call to the start of all ajax method files that requires |
|
| 55 | - * an authenticated user. |
|
| 56 | - * @deprecated 8.1.0 Use annotation based ACLs from the AppFramework instead |
|
| 57 | - * |
|
| 58 | - * @suppress PhanDeprecatedFunction |
|
| 59 | - */ |
|
| 60 | - public static function checkLoggedIn() { |
|
| 61 | - \OC_JSON::checkLoggedIn(); |
|
| 62 | - } |
|
| 44 | + /** |
|
| 45 | + * Check if the user is logged in, send json error msg if not. |
|
| 46 | + * |
|
| 47 | + * This method checks if a user is logged in. If not, a json error |
|
| 48 | + * response will be return and the method will exit from execution |
|
| 49 | + * of the script. |
|
| 50 | + * The returned json will be in the format: |
|
| 51 | + * |
|
| 52 | + * {"status":"error","data":{"message":"Authentication error."}} |
|
| 53 | + * |
|
| 54 | + * Add this call to the start of all ajax method files that requires |
|
| 55 | + * an authenticated user. |
|
| 56 | + * @deprecated 8.1.0 Use annotation based ACLs from the AppFramework instead |
|
| 57 | + * |
|
| 58 | + * @suppress PhanDeprecatedFunction |
|
| 59 | + */ |
|
| 60 | + public static function checkLoggedIn() { |
|
| 61 | + \OC_JSON::checkLoggedIn(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Check an ajax get/post call if the request token is valid. |
|
| 66 | - * |
|
| 67 | - * This method checks for a valid variable 'requesttoken' in $_GET, |
|
| 68 | - * $_POST and $_SERVER. If a valid token is not found, a json error |
|
| 69 | - * response will be return and the method will exit from execution |
|
| 70 | - * of the script. |
|
| 71 | - * The returned json will be in the format: |
|
| 72 | - * |
|
| 73 | - * {"status":"error","data":{"message":"Token expired. Please reload page."}} |
|
| 74 | - * |
|
| 75 | - * Add this call to the start of all ajax method files that creates, |
|
| 76 | - * updates or deletes anything. |
|
| 77 | - * In cases where you e.g. use an ajax call to load a dialog containing |
|
| 78 | - * a submittable form, you will need to add the requesttoken first as a |
|
| 79 | - * parameter to the ajax call, then assign it to the template and finally |
|
| 80 | - * add a hidden input field also named 'requesttoken' containing the value. |
|
| 81 | - * @deprecated 8.1.0 Use annotation based CSRF checks from the AppFramework instead |
|
| 82 | - * |
|
| 83 | - * @suppress PhanDeprecatedFunction |
|
| 84 | - */ |
|
| 85 | - public static function callCheck() { |
|
| 86 | - \OC_JSON::callCheck(); |
|
| 87 | - } |
|
| 64 | + /** |
|
| 65 | + * Check an ajax get/post call if the request token is valid. |
|
| 66 | + * |
|
| 67 | + * This method checks for a valid variable 'requesttoken' in $_GET, |
|
| 68 | + * $_POST and $_SERVER. If a valid token is not found, a json error |
|
| 69 | + * response will be return and the method will exit from execution |
|
| 70 | + * of the script. |
|
| 71 | + * The returned json will be in the format: |
|
| 72 | + * |
|
| 73 | + * {"status":"error","data":{"message":"Token expired. Please reload page."}} |
|
| 74 | + * |
|
| 75 | + * Add this call to the start of all ajax method files that creates, |
|
| 76 | + * updates or deletes anything. |
|
| 77 | + * In cases where you e.g. use an ajax call to load a dialog containing |
|
| 78 | + * a submittable form, you will need to add the requesttoken first as a |
|
| 79 | + * parameter to the ajax call, then assign it to the template and finally |
|
| 80 | + * add a hidden input field also named 'requesttoken' containing the value. |
|
| 81 | + * @deprecated 8.1.0 Use annotation based CSRF checks from the AppFramework instead |
|
| 82 | + * |
|
| 83 | + * @suppress PhanDeprecatedFunction |
|
| 84 | + */ |
|
| 85 | + public static function callCheck() { |
|
| 86 | + \OC_JSON::callCheck(); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Send json success msg |
|
| 91 | - * |
|
| 92 | - * Return a json success message with optional extra data. |
|
| 93 | - * @see \OCP\JSON::error() for the format to use. |
|
| 94 | - * |
|
| 95 | - * @param array $data The data to use |
|
| 96 | - * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
|
| 97 | - * @suppress PhanDeprecatedFunction |
|
| 98 | - */ |
|
| 99 | - public static function success( $data = array() ) { |
|
| 100 | - \OC_JSON::success($data); |
|
| 101 | - } |
|
| 89 | + /** |
|
| 90 | + * Send json success msg |
|
| 91 | + * |
|
| 92 | + * Return a json success message with optional extra data. |
|
| 93 | + * @see \OCP\JSON::error() for the format to use. |
|
| 94 | + * |
|
| 95 | + * @param array $data The data to use |
|
| 96 | + * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
|
| 97 | + * @suppress PhanDeprecatedFunction |
|
| 98 | + */ |
|
| 99 | + public static function success( $data = array() ) { |
|
| 100 | + \OC_JSON::success($data); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Send json error msg |
|
| 105 | - * |
|
| 106 | - * Return a json error message with optional extra data for |
|
| 107 | - * error message or app specific data. |
|
| 108 | - * |
|
| 109 | - * Example use: |
|
| 110 | - * |
|
| 111 | - * $id = [some value] |
|
| 112 | - * OCP\JSON::error(array('data':array('message':'An error happened', 'id': $id))); |
|
| 113 | - * |
|
| 114 | - * Will return the json formatted string: |
|
| 115 | - * |
|
| 116 | - * {"status":"error","data":{"message":"An error happened", "id":[some value]}} |
|
| 117 | - * |
|
| 118 | - * @param array $data The data to use |
|
| 119 | - * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
|
| 120 | - * @suppress PhanDeprecatedFunction |
|
| 121 | - */ |
|
| 122 | - public static function error( $data = array() ) { |
|
| 123 | - \OC_JSON::error($data); |
|
| 124 | - } |
|
| 103 | + /** |
|
| 104 | + * Send json error msg |
|
| 105 | + * |
|
| 106 | + * Return a json error message with optional extra data for |
|
| 107 | + * error message or app specific data. |
|
| 108 | + * |
|
| 109 | + * Example use: |
|
| 110 | + * |
|
| 111 | + * $id = [some value] |
|
| 112 | + * OCP\JSON::error(array('data':array('message':'An error happened', 'id': $id))); |
|
| 113 | + * |
|
| 114 | + * Will return the json formatted string: |
|
| 115 | + * |
|
| 116 | + * {"status":"error","data":{"message":"An error happened", "id":[some value]}} |
|
| 117 | + * |
|
| 118 | + * @param array $data The data to use |
|
| 119 | + * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
|
| 120 | + * @suppress PhanDeprecatedFunction |
|
| 121 | + */ |
|
| 122 | + public static function error( $data = array() ) { |
|
| 123 | + \OC_JSON::error($data); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Set Content-Type header to jsonrequest |
|
| 128 | - * @param string $type The content type header |
|
| 129 | - * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
|
| 130 | - * @suppress PhanDeprecatedFunction |
|
| 131 | - */ |
|
| 132 | - public static function setContentTypeHeader( $type='application/json' ) { |
|
| 133 | - \OC_JSON::setContentTypeHeader($type); |
|
| 134 | - } |
|
| 126 | + /** |
|
| 127 | + * Set Content-Type header to jsonrequest |
|
| 128 | + * @param string $type The content type header |
|
| 129 | + * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
|
| 130 | + * @suppress PhanDeprecatedFunction |
|
| 131 | + */ |
|
| 132 | + public static function setContentTypeHeader( $type='application/json' ) { |
|
| 133 | + \OC_JSON::setContentTypeHeader($type); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Check if the App is enabled and send JSON error message instead |
|
| 138 | - * |
|
| 139 | - * This method checks if a specific app is enabled. If not, a json error |
|
| 140 | - * response will be return and the method will exit from execution |
|
| 141 | - * of the script. |
|
| 142 | - * The returned json will be in the format: |
|
| 143 | - * |
|
| 144 | - * {"status":"error","data":{"message":"Application is not enabled."}} |
|
| 145 | - * |
|
| 146 | - * Add this call to the start of all ajax method files that requires |
|
| 147 | - * a specific app to be enabled. |
|
| 148 | - * |
|
| 149 | - * @param string $app The app to check |
|
| 150 | - * @deprecated 8.1.0 Use the AppFramework instead. It will automatically check if the app is enabled. |
|
| 151 | - * @suppress PhanDeprecatedFunction |
|
| 152 | - */ |
|
| 153 | - public static function checkAppEnabled( $app ) { |
|
| 154 | - \OC_JSON::checkAppEnabled($app); |
|
| 155 | - } |
|
| 136 | + /** |
|
| 137 | + * Check if the App is enabled and send JSON error message instead |
|
| 138 | + * |
|
| 139 | + * This method checks if a specific app is enabled. If not, a json error |
|
| 140 | + * response will be return and the method will exit from execution |
|
| 141 | + * of the script. |
|
| 142 | + * The returned json will be in the format: |
|
| 143 | + * |
|
| 144 | + * {"status":"error","data":{"message":"Application is not enabled."}} |
|
| 145 | + * |
|
| 146 | + * Add this call to the start of all ajax method files that requires |
|
| 147 | + * a specific app to be enabled. |
|
| 148 | + * |
|
| 149 | + * @param string $app The app to check |
|
| 150 | + * @deprecated 8.1.0 Use the AppFramework instead. It will automatically check if the app is enabled. |
|
| 151 | + * @suppress PhanDeprecatedFunction |
|
| 152 | + */ |
|
| 153 | + public static function checkAppEnabled( $app ) { |
|
| 154 | + \OC_JSON::checkAppEnabled($app); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * Check if the user is a admin, send json error msg if not |
|
| 159 | - * |
|
| 160 | - * This method checks if the current user has admin rights. If not, a json error |
|
| 161 | - * response will be return and the method will exit from execution |
|
| 162 | - * of the script. |
|
| 163 | - * The returned json will be in the format: |
|
| 164 | - * |
|
| 165 | - * {"status":"error","data":{"message":"Authentication error."}} |
|
| 166 | - * |
|
| 167 | - * Add this call to the start of all ajax method files that requires |
|
| 168 | - * administrative rights. |
|
| 169 | - * |
|
| 170 | - * @deprecated 8.1.0 Use annotation based ACLs from the AppFramework instead |
|
| 171 | - * @suppress PhanDeprecatedFunction |
|
| 172 | - */ |
|
| 173 | - public static function checkAdminUser() { |
|
| 174 | - \OC_JSON::checkAdminUser(); |
|
| 175 | - } |
|
| 157 | + /** |
|
| 158 | + * Check if the user is a admin, send json error msg if not |
|
| 159 | + * |
|
| 160 | + * This method checks if the current user has admin rights. If not, a json error |
|
| 161 | + * response will be return and the method will exit from execution |
|
| 162 | + * of the script. |
|
| 163 | + * The returned json will be in the format: |
|
| 164 | + * |
|
| 165 | + * {"status":"error","data":{"message":"Authentication error."}} |
|
| 166 | + * |
|
| 167 | + * Add this call to the start of all ajax method files that requires |
|
| 168 | + * administrative rights. |
|
| 169 | + * |
|
| 170 | + * @deprecated 8.1.0 Use annotation based ACLs from the AppFramework instead |
|
| 171 | + * @suppress PhanDeprecatedFunction |
|
| 172 | + */ |
|
| 173 | + public static function checkAdminUser() { |
|
| 174 | + \OC_JSON::checkAdminUser(); |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
| 97 | 97 | * @suppress PhanDeprecatedFunction |
| 98 | 98 | */ |
| 99 | - public static function success( $data = array() ) { |
|
| 99 | + public static function success($data = array()) { |
|
| 100 | 100 | \OC_JSON::success($data); |
| 101 | 101 | } |
| 102 | 102 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
| 120 | 120 | * @suppress PhanDeprecatedFunction |
| 121 | 121 | */ |
| 122 | - public static function error( $data = array() ) { |
|
| 122 | + public static function error($data = array()) { |
|
| 123 | 123 | \OC_JSON::error($data); |
| 124 | 124 | } |
| 125 | 125 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | * @deprecated 8.1.0 Use a AppFramework JSONResponse instead |
| 130 | 130 | * @suppress PhanDeprecatedFunction |
| 131 | 131 | */ |
| 132 | - public static function setContentTypeHeader( $type='application/json' ) { |
|
| 132 | + public static function setContentTypeHeader($type = 'application/json') { |
|
| 133 | 133 | \OC_JSON::setContentTypeHeader($type); |
| 134 | 134 | } |
| 135 | 135 | |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * @deprecated 8.1.0 Use the AppFramework instead. It will automatically check if the app is enabled. |
| 151 | 151 | * @suppress PhanDeprecatedFunction |
| 152 | 152 | */ |
| 153 | - public static function checkAppEnabled( $app ) { |
|
| 153 | + public static function checkAppEnabled($app) { |
|
| 154 | 154 | \OC_JSON::checkAppEnabled($app); |
| 155 | 155 | } |
| 156 | 156 | |