OCS.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Remote\Api;
  22. use GuzzleHttp\Exception\ClientException;
  23. use OC\ForbiddenException;
  24. use OC\Remote\User;
  25. use OCP\API;
  26. use OCP\Remote\Api\ICapabilitiesApi;
  27. use OCP\Remote\Api\IUserApi;
  28. class OCS extends ApiBase implements ICapabilitiesApi, IUserApi {
  29. /**
  30. * @param string $method
  31. * @param string $url
  32. * @param array $body
  33. * @param array $query
  34. * @param array $headers
  35. * @return array
  36. * @throws ForbiddenException
  37. * @throws NotFoundException
  38. * @throws \Exception
  39. */
  40. protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
  41. try {
  42. $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true);
  43. } catch (ClientException $e) {
  44. if ($e->getResponse()->getStatusCode() === 404) {
  45. throw new NotFoundException();
  46. } else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) {
  47. throw new ForbiddenException();
  48. } else {
  49. throw $e;
  50. }
  51. }
  52. if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) {
  53. throw new \Exception('Invalid ocs response');
  54. }
  55. if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) {
  56. throw new ForbiddenException();
  57. }
  58. if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) {
  59. throw new NotFoundException();
  60. }
  61. if ($response['ocs']['meta']['status'] !== 'ok') {
  62. throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']);
  63. }
  64. return $response['ocs']['data'];
  65. }
  66. /**
  67. * @param array $data
  68. * @param string $type
  69. * @param string[] $keys
  70. * @throws \Exception
  71. */
  72. private function checkResponseArray(array $data, $type, array $keys) {
  73. foreach ($keys as $key) {
  74. if (!array_key_exists($key, $data)) {
  75. throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found');
  76. }
  77. }
  78. }
  79. public function getUser($userId) {
  80. $result = $this->request('get', 'cloud/users/' . $userId);
  81. $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS);
  82. return new User($result);
  83. }
  84. /**
  85. * @return array The capabilities in the form of [$appId => [$capability => $value]]
  86. */
  87. public function getCapabilities() {
  88. $result = $this->request('get', 'cloud/capabilities');
  89. return $result['capabilities'];
  90. }
  91. }