notifications.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  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, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\FederatedFileSharing;
  22. use OCP\Http\Client\IClientService;
  23. class Notifications {
  24. const BASE_PATH_TO_SHARE_API = '/ocs/v1.php/cloud/shares';
  25. const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
  26. /** @var AddressHandler */
  27. private $addressHandler;
  28. /** @var IClientService */
  29. private $httpClientService;
  30. /**
  31. * Notifications constructor.
  32. *
  33. * @param AddressHandler $addressHandler
  34. * @param IClientService $httpClientService
  35. */
  36. public function __construct(
  37. AddressHandler $addressHandler,
  38. IClientService $httpClientService
  39. ) {
  40. $this->addressHandler = $addressHandler;
  41. $this->httpClientService = $httpClientService;
  42. }
  43. /**
  44. * send server-to-server share to remote server
  45. *
  46. * @param string $token
  47. * @param string $shareWith
  48. * @param string $name
  49. * @param int $remote_id
  50. * @param string $owner
  51. * @return bool
  52. */
  53. public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) {
  54. list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
  55. if ($user && $remote) {
  56. $url = $remote . self::BASE_PATH_TO_SHARE_API . '?format=' . self::RESPONSE_FORMAT;
  57. $local = $this->addressHandler->generateRemoteURL();
  58. $fields = array(
  59. 'shareWith' => $user,
  60. 'token' => $token,
  61. 'name' => $name,
  62. 'remoteId' => $remote_id,
  63. 'owner' => $owner,
  64. 'remote' => $local,
  65. );
  66. $url = $this->addressHandler->removeProtocolFromUrl($url);
  67. $result = $this->tryHttpPost($url, $fields);
  68. $status = json_decode($result['result'], true);
  69. if ($result['success'] && $status['ocs']['meta']['statuscode'] === 100) {
  70. \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]);
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * send server-to-server unshare to remote server
  78. *
  79. * @param string $remote url
  80. * @param int $id share id
  81. * @param string $token
  82. * @return bool
  83. */
  84. public function sendRemoteUnShare($remote, $id, $token) {
  85. $url = rtrim($remote, '/') . self::BASE_PATH_TO_SHARE_API . '/' . $id . '/unshare?format=' . self::RESPONSE_FORMAT;
  86. $fields = array('token' => $token, 'format' => 'json');
  87. $url = $this->addressHandler->removeProtocolFromUrl($url);
  88. $result = $this->tryHttpPost($url, $fields);
  89. $status = json_decode($result['result'], true);
  90. return ($result['success'] && $status['ocs']['meta']['statuscode'] === 100);
  91. }
  92. /**
  93. * try http post first with https and then with http as a fallback
  94. *
  95. * @param string $url
  96. * @param array $fields post parameters
  97. * @return array
  98. */
  99. private function tryHttpPost($url, array $fields) {
  100. $client = $this->httpClientService->newClient();
  101. $protocol = 'https://';
  102. $result = [
  103. 'success' => false,
  104. 'result' => '',
  105. ];
  106. $try = 0;
  107. while ($result['success'] === false && $try < 2) {
  108. try {
  109. $response = $client->post($protocol . $url, [
  110. 'body' => $fields
  111. ]);
  112. $result['result'] = $response->getBody();
  113. $result['success'] = true;
  114. break;
  115. } catch (\Exception $e) {
  116. $try++;
  117. $protocol = 'http://';
  118. }
  119. }
  120. return $result;
  121. }
  122. }