@@ -28,61 +28,61 @@ |
||
| 28 | 28 | |
| 29 | 29 | class Api { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Formats the given mount config to a mount entry. |
|
| 33 | - * |
|
| 34 | - * @param string $mountPoint mount point name, relative to the data dir |
|
| 35 | - * @param array $mountConfig mount config to format |
|
| 36 | - * |
|
| 37 | - * @return array entry |
|
| 38 | - */ |
|
| 39 | - private static function formatMount($mountPoint, $mountConfig) { |
|
| 40 | - // strip "/$user/files" from mount point |
|
| 41 | - $mountPoint = explode('/', trim($mountPoint, '/'), 3); |
|
| 42 | - $mountPoint = isset($mountPoint[2]) ? $mountPoint[2] : ''; |
|
| 31 | + /** |
|
| 32 | + * Formats the given mount config to a mount entry. |
|
| 33 | + * |
|
| 34 | + * @param string $mountPoint mount point name, relative to the data dir |
|
| 35 | + * @param array $mountConfig mount config to format |
|
| 36 | + * |
|
| 37 | + * @return array entry |
|
| 38 | + */ |
|
| 39 | + private static function formatMount($mountPoint, $mountConfig) { |
|
| 40 | + // strip "/$user/files" from mount point |
|
| 41 | + $mountPoint = explode('/', trim($mountPoint, '/'), 3); |
|
| 42 | + $mountPoint = isset($mountPoint[2]) ? $mountPoint[2] : ''; |
|
| 43 | 43 | |
| 44 | - // split path from mount point |
|
| 45 | - $path = dirname($mountPoint); |
|
| 46 | - if ($path === '.') { |
|
| 47 | - $path = ''; |
|
| 48 | - } |
|
| 44 | + // split path from mount point |
|
| 45 | + $path = dirname($mountPoint); |
|
| 46 | + if ($path === '.') { |
|
| 47 | + $path = ''; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - $isSystemMount = !$mountConfig['personal']; |
|
| 50 | + $isSystemMount = !$mountConfig['personal']; |
|
| 51 | 51 | |
| 52 | - $permissions = \OCP\Constants::PERMISSION_READ; |
|
| 53 | - // personal mounts can be deleted |
|
| 54 | - if (!$isSystemMount) { |
|
| 55 | - $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
| 56 | - } |
|
| 52 | + $permissions = \OCP\Constants::PERMISSION_READ; |
|
| 53 | + // personal mounts can be deleted |
|
| 54 | + if (!$isSystemMount) { |
|
| 55 | + $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $entry = array( |
|
| 59 | - 'name' => basename($mountPoint), |
|
| 60 | - 'path' => $path, |
|
| 61 | - 'type' => 'dir', |
|
| 62 | - 'backend' => $mountConfig['backend'], |
|
| 63 | - 'scope' => ( $isSystemMount ? 'system' : 'personal' ), |
|
| 64 | - 'permissions' => $permissions, |
|
| 65 | - 'id' => $mountConfig['id'], |
|
| 66 | - 'class' => $mountConfig['class'] |
|
| 67 | - ); |
|
| 68 | - return $entry; |
|
| 69 | - } |
|
| 58 | + $entry = array( |
|
| 59 | + 'name' => basename($mountPoint), |
|
| 60 | + 'path' => $path, |
|
| 61 | + 'type' => 'dir', |
|
| 62 | + 'backend' => $mountConfig['backend'], |
|
| 63 | + 'scope' => ( $isSystemMount ? 'system' : 'personal' ), |
|
| 64 | + 'permissions' => $permissions, |
|
| 65 | + 'id' => $mountConfig['id'], |
|
| 66 | + 'class' => $mountConfig['class'] |
|
| 67 | + ); |
|
| 68 | + return $entry; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Returns the mount points visible for this user. |
|
| 73 | - * |
|
| 74 | - * @param array $params |
|
| 75 | - * @return \OC\OCS\Result share information |
|
| 76 | - */ |
|
| 77 | - public static function getUserMounts($params) { |
|
| 78 | - $entries = array(); |
|
| 79 | - $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 71 | + /** |
|
| 72 | + * Returns the mount points visible for this user. |
|
| 73 | + * |
|
| 74 | + * @param array $params |
|
| 75 | + * @return \OC\OCS\Result share information |
|
| 76 | + */ |
|
| 77 | + public static function getUserMounts($params) { |
|
| 78 | + $entries = array(); |
|
| 79 | + $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 80 | 80 | |
| 81 | - $mounts = \OC_Mount_Config::getAbsoluteMountPoints($user); |
|
| 82 | - foreach($mounts as $mountPoint => $mount) { |
|
| 83 | - $entries[] = self::formatMount($mountPoint, $mount); |
|
| 84 | - } |
|
| 81 | + $mounts = \OC_Mount_Config::getAbsoluteMountPoints($user); |
|
| 82 | + foreach($mounts as $mountPoint => $mount) { |
|
| 83 | + $entries[] = self::formatMount($mountPoint, $mount); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - return new \OC\OCS\Result($entries); |
|
| 87 | - } |
|
| 86 | + return new \OC\OCS\Result($entries); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -32,13 +32,13 @@ discard block |
||
| 32 | 32 | require_once __DIR__ . '/../lib/base.php'; |
| 33 | 33 | |
| 34 | 34 | if (\OCP\Util::needUpgrade() |
| 35 | - || \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 36 | - // since the behavior of apps or remotes are unpredictable during |
|
| 37 | - // an upgrade, return a 503 directly |
|
| 38 | - OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); |
|
| 39 | - $response = new \OC\OCS\Result(null, OC_Response::STATUS_SERVICE_UNAVAILABLE, 'Service unavailable'); |
|
| 40 | - OC_API::respond($response, OC_API::requestedFormat()); |
|
| 41 | - exit; |
|
| 35 | + || \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 36 | + // since the behavior of apps or remotes are unpredictable during |
|
| 37 | + // an upgrade, return a 503 directly |
|
| 38 | + OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); |
|
| 39 | + $response = new \OC\OCS\Result(null, OC_Response::STATUS_SERVICE_UNAVAILABLE, 'Service unavailable'); |
|
| 40 | + OC_API::respond($response, OC_API::requestedFormat()); |
|
| 41 | + exit; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
@@ -49,53 +49,53 @@ discard block |
||
| 49 | 49 | * We first try the old routes since the appframework triggers more login stuff. |
| 50 | 50 | */ |
| 51 | 51 | try { |
| 52 | - OC_App::loadApps(['session']); |
|
| 53 | - OC_App::loadApps(['authentication']); |
|
| 54 | - // load all apps to get all api routes properly setup |
|
| 55 | - OC_App::loadApps(); |
|
| 52 | + OC_App::loadApps(['session']); |
|
| 53 | + OC_App::loadApps(['authentication']); |
|
| 54 | + // load all apps to get all api routes properly setup |
|
| 55 | + OC_App::loadApps(); |
|
| 56 | 56 | |
| 57 | - OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 58 | - return; |
|
| 57 | + OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 58 | + return; |
|
| 59 | 59 | } catch (ResourceNotFoundException $e) { |
| 60 | - // Fall through the not found |
|
| 60 | + // Fall through the not found |
|
| 61 | 61 | } catch (MethodNotAllowedException $e) { |
| 62 | - OC_API::setContentType(); |
|
| 63 | - OC_Response::setStatus(405); |
|
| 64 | - exit(); |
|
| 62 | + OC_API::setContentType(); |
|
| 63 | + OC_Response::setStatus(405); |
|
| 64 | + exit(); |
|
| 65 | 65 | } catch (Exception $ex) { |
| 66 | - OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 67 | - exit(); |
|
| 66 | + OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 67 | + exit(); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /* |
| 71 | 71 | * Try the appframework routes |
| 72 | 72 | */ |
| 73 | 73 | try { |
| 74 | - if(!\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 75 | - OC::handleLogin(\OC::$server->getRequest()); |
|
| 76 | - } |
|
| 77 | - OC::$server->getRouter()->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 74 | + if(!\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 75 | + OC::handleLogin(\OC::$server->getRequest()); |
|
| 76 | + } |
|
| 77 | + OC::$server->getRouter()->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 78 | 78 | } catch (ResourceNotFoundException $e) { |
| 79 | - OC_API::setContentType(); |
|
| 79 | + OC_API::setContentType(); |
|
| 80 | 80 | |
| 81 | - $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 82 | - $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 83 | - .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 84 | - OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 81 | + $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 82 | + $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 83 | + .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 84 | + OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 85 | 85 | } catch (MethodNotAllowedException $e) { |
| 86 | - OC_API::setContentType(); |
|
| 87 | - OC_Response::setStatus(405); |
|
| 86 | + OC_API::setContentType(); |
|
| 87 | + OC_Response::setStatus(405); |
|
| 88 | 88 | } catch (\OC\OCS\Exception $ex) { |
| 89 | - OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 89 | + OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 90 | 90 | } catch (\OC\User\LoginException $e) { |
| 91 | - OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'Unauthorised')); |
|
| 91 | + OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'Unauthorised')); |
|
| 92 | 92 | } catch (\Exception $e) { |
| 93 | - \OC::$server->getLogger()->logException($e); |
|
| 94 | - OC_API::setContentType(); |
|
| 93 | + \OC::$server->getLogger()->logException($e); |
|
| 94 | + OC_API::setContentType(); |
|
| 95 | 95 | |
| 96 | - $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 97 | - $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 98 | - .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 99 | - OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 96 | + $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 97 | + $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 98 | + .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 99 | + OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 100 | 100 | } |
| 101 | 101 | |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | */ |
| 31 | 31 | |
| 32 | -require_once __DIR__ . '/../lib/base.php'; |
|
| 32 | +require_once __DIR__.'/../lib/base.php'; |
|
| 33 | 33 | |
| 34 | 34 | if (\OCP\Util::needUpgrade() |
| 35 | 35 | || \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * Try the appframework routes |
| 72 | 72 | */ |
| 73 | 73 | try { |
| 74 | - if(!\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 74 | + if (!\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 75 | 75 | OC::handleLogin(\OC::$server->getRequest()); |
| 76 | 76 | } |
| 77 | 77 | OC::$server->getRouter()->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo()); |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | OC_API::setContentType(); |
| 80 | 80 | |
| 81 | 81 | $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
| 82 | - $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 82 | + $txt = 'Invalid query, please check the syntax. API specifications are here:' |
|
| 83 | 83 | .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
| 84 | 84 | OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
| 85 | 85 | } catch (MethodNotAllowedException $e) { |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | OC_API::setContentType(); |
| 95 | 95 | |
| 96 | 96 | $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
| 97 | - $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 97 | + $txt = 'Invalid query, please check the syntax. API specifications are here:' |
|
| 98 | 98 | .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
| 99 | 99 | OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
| 100 | 100 | } |
@@ -37,463 +37,463 @@ |
||
| 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 | - return OC_JSON::encode($response); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - $writer = new XMLWriter(); |
|
| 492 | - $writer->openMemory(); |
|
| 493 | - $writer->setIndent(true); |
|
| 494 | - $writer->startDocument(); |
|
| 495 | - self::toXML($response, $writer); |
|
| 496 | - $writer->endDocument(); |
|
| 497 | - return $writer->outputMemory(true); |
|
| 498 | - } |
|
| 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 | + return OC_JSON::encode($response); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + $writer = new XMLWriter(); |
|
| 492 | + $writer->openMemory(); |
|
| 493 | + $writer->setIndent(true); |
|
| 494 | + $writer->startDocument(); |
|
| 495 | + self::toXML($response, $writer); |
|
| 496 | + $writer->endDocument(); |
|
| 497 | + return $writer->outputMemory(true); |
|
| 498 | + } |
|
| 499 | 499 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | $requirements = array()) { |
| 93 | 93 | $name = strtolower($method).$url; |
| 94 | 94 | $name = str_replace(array('/', '{', '}'), '_', $name); |
| 95 | - if(!isset(self::$actions[$name])) { |
|
| 95 | + if (!isset(self::$actions[$name])) { |
|
| 96 | 96 | $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
| 97 | 97 | OC::$server->getRouter()->useCollection('ocs'); |
| 98 | 98 | OC::$server->getRouter()->create($name, $url) |
@@ -115,18 +115,18 @@ discard block |
||
| 115 | 115 | $method = $request->getMethod(); |
| 116 | 116 | |
| 117 | 117 | // Prepare the request variables |
| 118 | - if($method === 'PUT') { |
|
| 118 | + if ($method === 'PUT') { |
|
| 119 | 119 | $parameters['_put'] = $request->getParams(); |
| 120 | - } else if($method === 'DELETE') { |
|
| 120 | + } else if ($method === 'DELETE') { |
|
| 121 | 121 | $parameters['_delete'] = $request->getParams(); |
| 122 | 122 | } |
| 123 | 123 | $name = $parameters['_route']; |
| 124 | 124 | // Foreach registered action |
| 125 | 125 | $responses = array(); |
| 126 | 126 | $appManager = \OC::$server->getAppManager(); |
| 127 | - foreach(self::$actions[$name] as $action) { |
|
| 127 | + foreach (self::$actions[$name] as $action) { |
|
| 128 | 128 | // Check authentication and availability |
| 129 | - if(!self::isAuthorised($action)) { |
|
| 129 | + if (!self::isAuthorised($action)) { |
|
| 130 | 130 | $responses[] = array( |
| 131 | 131 | 'app' => $action['app'], |
| 132 | 132 | 'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | ); |
| 135 | 135 | continue; |
| 136 | 136 | } |
| 137 | - if(!is_callable($action['action'])) { |
|
| 137 | + if (!is_callable($action['action'])) { |
|
| 138 | 138 | $responses[] = array( |
| 139 | 139 | 'app' => $action['app'], |
| 140 | 140 | 'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
@@ -174,15 +174,15 @@ discard block |
||
| 174 | 174 | 'failed' => array(), |
| 175 | 175 | ); |
| 176 | 176 | |
| 177 | - foreach($responses as $response) { |
|
| 178 | - if($response['shipped'] || ($response['app'] === 'core')) { |
|
| 179 | - if($response['response']->succeeded()) { |
|
| 177 | + foreach ($responses as $response) { |
|
| 178 | + if ($response['shipped'] || ($response['app'] === 'core')) { |
|
| 179 | + if ($response['response']->succeeded()) { |
|
| 180 | 180 | $shipped['succeeded'][$response['app']] = $response; |
| 181 | 181 | } else { |
| 182 | 182 | $shipped['failed'][$response['app']] = $response; |
| 183 | 183 | } |
| 184 | 184 | } else { |
| 185 | - if($response['response']->succeeded()) { |
|
| 185 | + if ($response['response']->succeeded()) { |
|
| 186 | 186 | $thirdparty['succeeded'][$response['app']] = $response; |
| 187 | 187 | } else { |
| 188 | 188 | $thirdparty['failed'][$response['app']] = $response; |
@@ -191,14 +191,14 @@ discard block |
||
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | // Remove any error responses if there is one shipped response that succeeded |
| 194 | - if(!empty($shipped['failed'])) { |
|
| 194 | + if (!empty($shipped['failed'])) { |
|
| 195 | 195 | // Which shipped response do we use if they all failed? |
| 196 | 196 | // They may have failed for different reasons (different status codes) |
| 197 | 197 | // Which response code should we return? |
| 198 | 198 | // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
| 199 | 199 | // Merge failed responses if more than one |
| 200 | 200 | $data = array(); |
| 201 | - foreach($shipped['failed'] as $failure) { |
|
| 201 | + foreach ($shipped['failed'] as $failure) { |
|
| 202 | 202 | $data = array_merge_recursive($data, $failure['response']->getData()); |
| 203 | 203 | } |
| 204 | 204 | $picked = reset($shipped['failed']); |
@@ -207,12 +207,12 @@ discard block |
||
| 207 | 207 | $headers = $picked['response']->getHeaders(); |
| 208 | 208 | $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers); |
| 209 | 209 | return $response; |
| 210 | - } elseif(!empty($shipped['succeeded'])) { |
|
| 210 | + } elseif (!empty($shipped['succeeded'])) { |
|
| 211 | 211 | $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
| 212 | - } elseif(!empty($thirdparty['failed'])) { |
|
| 212 | + } elseif (!empty($thirdparty['failed'])) { |
|
| 213 | 213 | // Merge failed responses if more than one |
| 214 | 214 | $data = array(); |
| 215 | - foreach($thirdparty['failed'] as $failure) { |
|
| 215 | + foreach ($thirdparty['failed'] as $failure) { |
|
| 216 | 216 | $data = array_merge_recursive($data, $failure['response']->getData()); |
| 217 | 217 | } |
| 218 | 218 | $picked = reset($thirdparty['failed']); |
@@ -229,8 +229,8 @@ discard block |
||
| 229 | 229 | $codes = []; |
| 230 | 230 | $header = []; |
| 231 | 231 | |
| 232 | - foreach($responses as $response) { |
|
| 233 | - if($response['shipped']) { |
|
| 232 | + foreach ($responses as $response) { |
|
| 233 | + if ($response['shipped']) { |
|
| 234 | 234 | $data = array_merge_recursive($response['response']->getData(), $data); |
| 235 | 235 | } else { |
| 236 | 236 | $data = array_merge_recursive($data, $response['response']->getData()); |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | // Use any non 100 status codes |
| 244 | 244 | $statusCode = 100; |
| 245 | 245 | $statusMessage = null; |
| 246 | - foreach($codes as $code) { |
|
| 247 | - if($code['code'] != 100) { |
|
| 246 | + foreach ($codes as $code) { |
|
| 247 | + if ($code['code'] != 100) { |
|
| 248 | 248 | $statusCode = $code['code']; |
| 249 | 249 | $statusMessage = $code['meta']['message']; |
| 250 | 250 | break; |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | private static function isAuthorised($action) { |
| 263 | 263 | $level = $action['authlevel']; |
| 264 | - switch($level) { |
|
| 264 | + switch ($level) { |
|
| 265 | 265 | case API::GUEST_AUTH: |
| 266 | 266 | // Anyone can access |
| 267 | 267 | return true; |
@@ -271,16 +271,16 @@ discard block |
||
| 271 | 271 | case API::SUBADMIN_AUTH: |
| 272 | 272 | // Check for subadmin |
| 273 | 273 | $user = self::loginUser(); |
| 274 | - if(!$user) { |
|
| 274 | + if (!$user) { |
|
| 275 | 275 | return false; |
| 276 | 276 | } else { |
| 277 | 277 | $userObject = \OC::$server->getUserSession()->getUser(); |
| 278 | - if($userObject === null) { |
|
| 278 | + if ($userObject === null) { |
|
| 279 | 279 | return false; |
| 280 | 280 | } |
| 281 | 281 | $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
| 282 | 282 | $admin = OC_User::isAdminUser($user); |
| 283 | - if($isSubAdmin || $admin) { |
|
| 283 | + if ($isSubAdmin || $admin) { |
|
| 284 | 284 | return true; |
| 285 | 285 | } else { |
| 286 | 286 | return false; |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | case API::ADMIN_AUTH: |
| 290 | 290 | // Check for admin |
| 291 | 291 | $user = self::loginUser(); |
| 292 | - if(!$user) { |
|
| 292 | + if (!$user) { |
|
| 293 | 293 | return false; |
| 294 | 294 | } else { |
| 295 | 295 | return OC_User::isAdminUser($user); |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | * @return string|false (username, or false on failure) |
| 306 | 306 | */ |
| 307 | 307 | private static function loginUser() { |
| 308 | - if(self::$isLoggedIn === true) { |
|
| 308 | + if (self::$isLoggedIn === true) { |
|
| 309 | 309 | return \OC_User::getUser(); |
| 310 | 310 | } |
| 311 | 311 | |
@@ -354,13 +354,13 @@ discard block |
||
| 354 | 354 | * @param \OC\OCS\Result $result |
| 355 | 355 | * @param string $format the format xml|json |
| 356 | 356 | */ |
| 357 | - public static function respond($result, $format='xml') { |
|
| 357 | + public static function respond($result, $format = 'xml') { |
|
| 358 | 358 | $request = \OC::$server->getRequest(); |
| 359 | 359 | |
| 360 | 360 | // Send 401 headers if unauthorised |
| 361 | - if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
| 361 | + if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
| 362 | 362 | // If request comes from JS return dummy auth request |
| 363 | - if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
| 363 | + if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
| 364 | 364 | header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
| 365 | 365 | } else { |
| 366 | 366 | header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
@@ -368,8 +368,8 @@ discard block |
||
| 368 | 368 | header('HTTP/1.0 401 Unauthorized'); |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - foreach($result->getHeaders() as $name => $value) { |
|
| 372 | - header($name . ': ' . $value); |
|
| 371 | + foreach ($result->getHeaders() as $name => $value) { |
|
| 372 | + header($name.': '.$value); |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | $meta = $result->getMeta(); |
@@ -391,14 +391,14 @@ discard block |
||
| 391 | 391 | * @param XMLWriter $writer |
| 392 | 392 | */ |
| 393 | 393 | private static function toXML($array, $writer) { |
| 394 | - foreach($array as $k => $v) { |
|
| 394 | + foreach ($array as $k => $v) { |
|
| 395 | 395 | if ($k[0] === '@') { |
| 396 | 396 | $writer->writeAttribute(substr($k, 1), $v); |
| 397 | 397 | continue; |
| 398 | 398 | } else if (is_numeric($k)) { |
| 399 | 399 | $k = 'element'; |
| 400 | 400 | } |
| 401 | - if(is_array($v)) { |
|
| 401 | + if (is_array($v)) { |
|
| 402 | 402 | $writer->startElement($k); |
| 403 | 403 | self::toXML($v, $writer); |
| 404 | 404 | $writer->endElement(); |
@@ -36,59 +36,59 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class OCSResponse extends Response { |
| 38 | 38 | |
| 39 | - private $data; |
|
| 40 | - private $format; |
|
| 41 | - private $statuscode; |
|
| 42 | - private $message; |
|
| 43 | - private $itemscount; |
|
| 44 | - private $itemsperpage; |
|
| 39 | + private $data; |
|
| 40 | + private $format; |
|
| 41 | + private $statuscode; |
|
| 42 | + private $message; |
|
| 43 | + private $itemscount; |
|
| 44 | + private $itemsperpage; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * generates the xml or json response for the API call from an multidimenional data array. |
|
| 48 | - * @param string $format |
|
| 49 | - * @param int $statuscode |
|
| 50 | - * @param string $message |
|
| 51 | - * @param array $data |
|
| 52 | - * @param int|string $itemscount |
|
| 53 | - * @param int|string $itemsperpage |
|
| 54 | - * @since 8.1.0 |
|
| 55 | - * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController |
|
| 56 | - */ |
|
| 57 | - public function __construct($format, $statuscode, $message, |
|
| 58 | - $data=[], $itemscount='', |
|
| 59 | - $itemsperpage='') { |
|
| 60 | - $this->format = $format; |
|
| 61 | - $this->statuscode = $statuscode; |
|
| 62 | - $this->message = $message; |
|
| 63 | - $this->data = $data; |
|
| 64 | - $this->itemscount = $itemscount; |
|
| 65 | - $this->itemsperpage = $itemsperpage; |
|
| 46 | + /** |
|
| 47 | + * generates the xml or json response for the API call from an multidimenional data array. |
|
| 48 | + * @param string $format |
|
| 49 | + * @param int $statuscode |
|
| 50 | + * @param string $message |
|
| 51 | + * @param array $data |
|
| 52 | + * @param int|string $itemscount |
|
| 53 | + * @param int|string $itemsperpage |
|
| 54 | + * @since 8.1.0 |
|
| 55 | + * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController |
|
| 56 | + */ |
|
| 57 | + public function __construct($format, $statuscode, $message, |
|
| 58 | + $data=[], $itemscount='', |
|
| 59 | + $itemsperpage='') { |
|
| 60 | + $this->format = $format; |
|
| 61 | + $this->statuscode = $statuscode; |
|
| 62 | + $this->message = $message; |
|
| 63 | + $this->data = $data; |
|
| 64 | + $this->itemscount = $itemscount; |
|
| 65 | + $this->itemsperpage = $itemsperpage; |
|
| 66 | 66 | |
| 67 | - // set the correct header based on the format parameter |
|
| 68 | - if ($format === 'json') { |
|
| 69 | - $this->addHeader( |
|
| 70 | - 'Content-Type', 'application/json; charset=utf-8' |
|
| 71 | - ); |
|
| 72 | - } else { |
|
| 73 | - $this->addHeader( |
|
| 74 | - 'Content-Type', 'application/xml; charset=utf-8' |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - } |
|
| 67 | + // set the correct header based on the format parameter |
|
| 68 | + if ($format === 'json') { |
|
| 69 | + $this->addHeader( |
|
| 70 | + 'Content-Type', 'application/json; charset=utf-8' |
|
| 71 | + ); |
|
| 72 | + } else { |
|
| 73 | + $this->addHeader( |
|
| 74 | + 'Content-Type', 'application/xml; charset=utf-8' |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * @return string |
|
| 81 | - * @since 8.1.0 |
|
| 82 | - * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController |
|
| 83 | - * @suppress PhanDeprecatedClass |
|
| 84 | - */ |
|
| 85 | - public function render() { |
|
| 86 | - $r = new \OC\OCS\Result($this->data, $this->statuscode, $this->message); |
|
| 87 | - $r->setTotalItems($this->itemscount); |
|
| 88 | - $r->setItemsPerPage($this->itemsperpage); |
|
| 79 | + /** |
|
| 80 | + * @return string |
|
| 81 | + * @since 8.1.0 |
|
| 82 | + * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController |
|
| 83 | + * @suppress PhanDeprecatedClass |
|
| 84 | + */ |
|
| 85 | + public function render() { |
|
| 86 | + $r = new \OC\OCS\Result($this->data, $this->statuscode, $this->message); |
|
| 87 | + $r->setTotalItems($this->itemscount); |
|
| 88 | + $r->setItemsPerPage($this->itemsperpage); |
|
| 89 | 89 | |
| 90 | - return \OC_API::renderResult($this->format, $r->getMeta(), $r->getData()); |
|
| 91 | - } |
|
| 90 | + return \OC_API::renderResult($this->format, $r->getMeta(), $r->getData()); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | |
| 94 | 94 | } |