storage.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\External;
  26. use OC\Files\Storage\DAV;
  27. use OC\ForbiddenException;
  28. use OCA\Files_Sharing\ISharedStorage;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\StorageInvalidException;
  31. use OCP\Files\StorageNotAvailableException;
  32. class Storage extends DAV implements ISharedStorage {
  33. /**
  34. * @var string
  35. */
  36. private $remoteUser;
  37. /**
  38. * @var string
  39. */
  40. private $remote;
  41. /**
  42. * @var string
  43. */
  44. private $mountPoint;
  45. /**
  46. * @var string
  47. */
  48. private $token;
  49. /**
  50. * @var \OCP\ICertificateManager
  51. */
  52. private $certificateManager;
  53. private $updateChecked = false;
  54. /**
  55. * @var \OCA\Files_Sharing\External\Manager
  56. */
  57. private $manager;
  58. public function __construct($options) {
  59. $this->manager = $options['manager'];
  60. $this->certificateManager = $options['certificateManager'];
  61. $this->remote = $options['remote'];
  62. $this->remoteUser = $options['owner'];
  63. list($protocol, $remote) = explode('://', $this->remote);
  64. if (strpos($remote, '/')) {
  65. list($host, $root) = explode('/', $remote, 2);
  66. } else {
  67. $host = $remote;
  68. $root = '';
  69. }
  70. $secure = $protocol === 'https';
  71. $root = rtrim($root, '/') . '/public.php/webdav';
  72. $this->mountPoint = $options['mountpoint'];
  73. $this->token = $options['token'];
  74. parent::__construct(array(
  75. 'secure' => $secure,
  76. 'host' => $host,
  77. 'root' => $root,
  78. 'user' => $options['token'],
  79. 'password' => (string)$options['password']
  80. ));
  81. $this->getWatcher()->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
  82. }
  83. public function getRemoteUser() {
  84. return $this->remoteUser;
  85. }
  86. public function getRemote() {
  87. return $this->remote;
  88. }
  89. public function getMountPoint() {
  90. return $this->mountPoint;
  91. }
  92. public function getToken() {
  93. return $this->token;
  94. }
  95. public function getPassword() {
  96. return $this->password;
  97. }
  98. /**
  99. * @brief get id of the mount point
  100. * @return string
  101. */
  102. public function getId() {
  103. return 'shared::' . md5($this->token . '@' . $this->remote);
  104. }
  105. public function getCache($path = '', $storage = null) {
  106. if (is_null($this->cache)) {
  107. $this->cache = new Cache($this, $this->remote, $this->remoteUser);
  108. }
  109. return $this->cache;
  110. }
  111. /**
  112. * @param string $path
  113. * @param \OC\Files\Storage\Storage $storage
  114. * @return \OCA\Files_Sharing\External\Scanner
  115. */
  116. public function getScanner($path = '', $storage = null) {
  117. if (!$storage) {
  118. $storage = $this;
  119. }
  120. if (!isset($this->scanner)) {
  121. $this->scanner = new Scanner($storage);
  122. }
  123. return $this->scanner;
  124. }
  125. /**
  126. * check if a file or folder has been updated since $time
  127. *
  128. * @param string $path
  129. * @param int $time
  130. * @throws \OCP\Files\StorageNotAvailableException
  131. * @throws \OCP\Files\StorageInvalidException
  132. * @return bool
  133. */
  134. public function hasUpdated($path, $time) {
  135. // since for owncloud webdav servers we can rely on etag propagation we only need to check the root of the storage
  136. // because of that we only do one check for the entire storage per request
  137. if ($this->updateChecked) {
  138. return false;
  139. }
  140. $this->updateChecked = true;
  141. try {
  142. return parent::hasUpdated('', $time);
  143. } catch (StorageInvalidException $e) {
  144. // check if it needs to be removed
  145. $this->checkStorageAvailability();
  146. throw $e;
  147. } catch (StorageNotAvailableException $e) {
  148. // check if it needs to be removed or just temp unavailable
  149. $this->checkStorageAvailability();
  150. throw $e;
  151. }
  152. }
  153. /**
  154. * Check whether this storage is permanently or temporarily
  155. * unavailable
  156. *
  157. * @throws \OCP\Files\StorageNotAvailableException
  158. * @throws \OCP\Files\StorageInvalidException
  159. */
  160. public function checkStorageAvailability() {
  161. // see if we can find out why the share is unavailable
  162. try {
  163. $this->getShareInfo();
  164. } catch (NotFoundException $e) {
  165. // a 404 can either mean that the share no longer exists or there is no ownCloud on the remote
  166. if ($this->testRemote()) {
  167. // valid ownCloud instance means that the public share no longer exists
  168. // since this is permanent (re-sharing the file will create a new token)
  169. // we remove the invalid storage
  170. $this->manager->removeShare($this->mountPoint);
  171. $this->manager->getMountManager()->removeMount($this->mountPoint);
  172. throw new StorageInvalidException();
  173. } else {
  174. // ownCloud instance is gone, likely to be a temporary server configuration error
  175. throw new StorageNotAvailableException();
  176. }
  177. } catch (ForbiddenException $e) {
  178. // auth error, remove share for now (provide a dialog in the future)
  179. $this->manager->removeShare($this->mountPoint);
  180. $this->manager->getMountManager()->removeMount($this->mountPoint);
  181. throw new StorageInvalidException();
  182. } catch (\GuzzleHttp\Exception\ConnectException $e) {
  183. throw new StorageNotAvailableException();
  184. } catch (\GuzzleHttp\Exception\RequestException $e) {
  185. throw new StorageNotAvailableException();
  186. } catch (\Exception $e) {
  187. throw $e;
  188. }
  189. }
  190. public function file_exists($path) {
  191. if ($path === '') {
  192. return true;
  193. } else {
  194. return parent::file_exists($path);
  195. }
  196. }
  197. /**
  198. * check if the configured remote is a valid ownCloud instance
  199. *
  200. * @return bool
  201. */
  202. protected function testRemote() {
  203. try {
  204. $result = file_get_contents($this->remote . '/status.php');
  205. $data = json_decode($result);
  206. return is_object($data) and !empty($data->version);
  207. } catch (\Exception $e) {
  208. return false;
  209. }
  210. }
  211. /**
  212. * @return mixed
  213. * @throws ForbiddenException
  214. * @throws NotFoundException
  215. * @throws \Exception
  216. */
  217. public function getShareInfo() {
  218. $remote = $this->getRemote();
  219. $token = $this->getToken();
  220. $password = $this->getPassword();
  221. $url = rtrim($remote, '/') . '/index.php/apps/files_sharing/shareinfo?t=' . $token;
  222. // TODO: DI
  223. $client = \OC::$server->getHTTPClientService()->newClient();
  224. try {
  225. $response = $client->post($url, ['body' => ['password' => $password]]);
  226. } catch (\GuzzleHttp\Exception\RequestException $e) {
  227. if ($e->getCode() === 401 || $e->getCode() === 403) {
  228. throw new ForbiddenException();
  229. }
  230. // throw this to be on the safe side: the share will still be visible
  231. // in the UI in case the failure is intermittent, and the user will
  232. // be able to decide whether to remove it if it's really gone
  233. throw new StorageNotAvailableException();
  234. }
  235. return json_decode($response->getBody(), true);
  236. }
  237. }