storage.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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\Filesystem;
  27. use OC\Files\Storage\DAV;
  28. use OC\ForbiddenException;
  29. use OCA\Files_Sharing\ISharedStorage;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\StorageInvalidException;
  32. use OCP\Files\StorageNotAvailableException;
  33. class Storage extends DAV implements ISharedStorage {
  34. /**
  35. * @var string
  36. */
  37. private $remoteUser;
  38. /**
  39. * @var string
  40. */
  41. private $remote;
  42. /**
  43. * @var string
  44. */
  45. private $mountPoint;
  46. /**
  47. * @var string
  48. */
  49. private $token;
  50. /**
  51. * @var \OCP\ICertificateManager
  52. */
  53. private $certificateManager;
  54. private $updateChecked = false;
  55. /**
  56. * @var \OCA\Files_Sharing\External\Manager
  57. */
  58. private $manager;
  59. public function __construct($options) {
  60. $this->manager = $options['manager'];
  61. $this->certificateManager = $options['certificateManager'];
  62. $this->remote = $options['remote'];
  63. $this->remoteUser = $options['owner'];
  64. list($protocol, $remote) = explode('://', $this->remote);
  65. if (strpos($remote, '/')) {
  66. list($host, $root) = explode('/', $remote, 2);
  67. } else {
  68. $host = $remote;
  69. $root = '';
  70. }
  71. $secure = $protocol === 'https';
  72. $root = rtrim($root, '/') . '/public.php/webdav';
  73. $this->mountPoint = $options['mountpoint'];
  74. $this->token = $options['token'];
  75. parent::__construct(array(
  76. 'secure' => $secure,
  77. 'host' => $host,
  78. 'root' => $root,
  79. 'user' => $options['token'],
  80. 'password' => (string)$options['password']
  81. ));
  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 $e;
  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 (\Exception $e) {
  183. throw $e;
  184. }
  185. }
  186. public function file_exists($path) {
  187. if ($path === '') {
  188. return true;
  189. } else {
  190. return parent::file_exists($path);
  191. }
  192. }
  193. /**
  194. * check if the configured remote is a valid ownCloud instance
  195. *
  196. * @return bool
  197. */
  198. protected function testRemote() {
  199. try {
  200. $result = file_get_contents($this->remote . '/status.php');
  201. $data = json_decode($result);
  202. return is_object($data) and !empty($data->version);
  203. } catch (\Exception $e) {
  204. return false;
  205. }
  206. }
  207. /**
  208. * @return mixed
  209. * @throws ForbiddenException
  210. * @throws NotFoundException
  211. * @throws \Exception
  212. */
  213. public function getShareInfo() {
  214. $remote = $this->getRemote();
  215. $token = $this->getToken();
  216. $password = $this->getPassword();
  217. $url = rtrim($remote, '/') . '/index.php/apps/files_sharing/shareinfo?t=' . $token;
  218. // TODO: DI
  219. $client = \OC::$server->getHTTPClientService()->newClient();
  220. $response = $client->post($url, ['body' => ['password' => $password]]);
  221. switch ($response->getStatusCode()) {
  222. case 401:
  223. case 403:
  224. throw new ForbiddenException();
  225. case 404:
  226. throw new NotFoundException();
  227. case 500:
  228. throw new \Exception();
  229. }
  230. return json_decode($response->getBody(), true);
  231. }
  232. }