You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OCS.php 3.3KB

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