@@ -1,47 +1,47 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - use \OCA\Files_External\Lib\Backend\Backend; |
|
| 3 | - use \OCA\Files_External\Lib\Auth\AuthMechanism; |
|
| 4 | - use \OCA\Files_External\Lib\DefinitionParameter; |
|
| 5 | - use \OCA\Files_External\Service\BackendService; |
|
| 2 | + use \OCA\Files_External\Lib\Backend\Backend; |
|
| 3 | + use \OCA\Files_External\Lib\Auth\AuthMechanism; |
|
| 4 | + use \OCA\Files_External\Lib\DefinitionParameter; |
|
| 5 | + use \OCA\Files_External\Service\BackendService; |
|
| 6 | 6 | |
| 7 | - $canCreateMounts = $_['visibilityType'] === BackendService::VISIBILITY_ADMIN || $_['allowUserMounting']; |
|
| 7 | + $canCreateMounts = $_['visibilityType'] === BackendService::VISIBILITY_ADMIN || $_['allowUserMounting']; |
|
| 8 | 8 | |
| 9 | - $l->t("Enable encryption"); |
|
| 10 | - $l->t("Enable previews"); |
|
| 11 | - $l->t("Enable sharing"); |
|
| 12 | - $l->t("Check for changes"); |
|
| 13 | - $l->t("Never"); |
|
| 14 | - $l->t("Once every direct access"); |
|
| 9 | + $l->t("Enable encryption"); |
|
| 10 | + $l->t("Enable previews"); |
|
| 11 | + $l->t("Enable sharing"); |
|
| 12 | + $l->t("Check for changes"); |
|
| 13 | + $l->t("Never"); |
|
| 14 | + $l->t("Once every direct access"); |
|
| 15 | 15 | |
| 16 | - script('files_external', 'settings'); |
|
| 17 | - style('files_external', 'settings'); |
|
| 16 | + script('files_external', 'settings'); |
|
| 17 | + style('files_external', 'settings'); |
|
| 18 | 18 | |
| 19 | - // load custom JS |
|
| 20 | - foreach ($_['backends'] as $backend) { |
|
| 21 | - /** @var Backend $backend */ |
|
| 22 | - $scripts = $backend->getCustomJs(); |
|
| 23 | - foreach ($scripts as $script) { |
|
| 24 | - script('files_external', $script); |
|
| 25 | - } |
|
| 26 | - } |
|
| 27 | - foreach ($_['authMechanisms'] as $authMechanism) { |
|
| 28 | - /** @var AuthMechanism $authMechanism */ |
|
| 29 | - $scripts = $authMechanism->getCustomJs(); |
|
| 30 | - foreach ($scripts as $script) { |
|
| 31 | - script('files_external', $script); |
|
| 32 | - } |
|
| 33 | - } |
|
| 19 | + // load custom JS |
|
| 20 | + foreach ($_['backends'] as $backend) { |
|
| 21 | + /** @var Backend $backend */ |
|
| 22 | + $scripts = $backend->getCustomJs(); |
|
| 23 | + foreach ($scripts as $script) { |
|
| 24 | + script('files_external', $script); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | + foreach ($_['authMechanisms'] as $authMechanism) { |
|
| 28 | + /** @var AuthMechanism $authMechanism */ |
|
| 29 | + $scripts = $authMechanism->getCustomJs(); |
|
| 30 | + foreach ($scripts as $script) { |
|
| 31 | + script('files_external', $script); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - function writeParameterInput($parameter, $options, $classes = []) { |
|
| 36 | - $value = ''; |
|
| 37 | - if (isset($options[$parameter->getName()])) { |
|
| 38 | - $value = $options[$parameter->getName()]; |
|
| 39 | - } |
|
| 40 | - $placeholder = $parameter->getText(); |
|
| 41 | - $is_optional = $parameter->isFlagSet(DefinitionParameter::FLAG_OPTIONAL); |
|
| 35 | + function writeParameterInput($parameter, $options, $classes = []) { |
|
| 36 | + $value = ''; |
|
| 37 | + if (isset($options[$parameter->getName()])) { |
|
| 38 | + $value = $options[$parameter->getName()]; |
|
| 39 | + } |
|
| 40 | + $placeholder = $parameter->getText(); |
|
| 41 | + $is_optional = $parameter->isFlagSet(DefinitionParameter::FLAG_OPTIONAL); |
|
| 42 | 42 | |
| 43 | - switch ($parameter->getType()) { |
|
| 44 | - case DefinitionParameter::VALUE_PASSWORD: ?> |
|
| 43 | + switch ($parameter->getType()) { |
|
| 44 | + case DefinitionParameter::VALUE_PASSWORD: ?> |
|
| 45 | 45 | <?php if ($is_optional) { $classes[] = 'optional'; } ?> |
| 46 | 46 | <input type="password" |
| 47 | 47 | <?php if (!empty($classes)): ?> class="<?php p(implode(' ', $classes)); ?>"<?php endif; ?> |
@@ -50,8 +50,8 @@ discard block |
||
| 50 | 50 | placeholder="<?php p($placeholder); ?>" |
| 51 | 51 | /> |
| 52 | 52 | <?php |
| 53 | - break; |
|
| 54 | - case DefinitionParameter::VALUE_BOOLEAN: ?> |
|
| 53 | + break; |
|
| 54 | + case DefinitionParameter::VALUE_BOOLEAN: ?> |
|
| 55 | 55 | <?php $checkboxId = uniqid("checkbox_"); ?> |
| 56 | 56 | <div> |
| 57 | 57 | <label> |
@@ -65,16 +65,16 @@ discard block |
||
| 65 | 65 | </label> |
| 66 | 66 | </div> |
| 67 | 67 | <?php |
| 68 | - break; |
|
| 69 | - case DefinitionParameter::VALUE_HIDDEN: ?> |
|
| 68 | + break; |
|
| 69 | + case DefinitionParameter::VALUE_HIDDEN: ?> |
|
| 70 | 70 | <input type="hidden" |
| 71 | 71 | <?php if (!empty($classes)): ?> class="<?php p(implode(' ', $classes)); ?>"<?php endif; ?> |
| 72 | 72 | data-parameter="<?php p($parameter->getName()); ?>" |
| 73 | 73 | value="<?php p($value); ?>" |
| 74 | 74 | /> |
| 75 | 75 | <?php |
| 76 | - break; |
|
| 77 | - default: ?> |
|
| 76 | + break; |
|
| 77 | + default: ?> |
|
| 78 | 78 | <?php if ($is_optional) { $classes[] = 'optional'; } ?> |
| 79 | 79 | <input type="text" |
| 80 | 80 | <?php if (!empty($classes)): ?> class="<?php p(implode(' ', $classes)); ?>"<?php endif; ?> |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | placeholder="<?php p($placeholder); ?>" |
| 84 | 84 | /> |
| 85 | 85 | <?php |
| 86 | - } |
|
| 87 | - } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | ?> |
| 89 | 89 | |
| 90 | 90 | <form data-can-create="<?php echo $canCreateMounts?'true':'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled']?'true': 'false'; ?>"> |
@@ -122,13 +122,13 @@ discard block |
||
| 122 | 122 | <?php p($l->t('Add storage')); ?> |
| 123 | 123 | </option> |
| 124 | 124 | <?php |
| 125 | - $sortedBackends = array_filter($_['backends'], function($backend) use ($_) { |
|
| 126 | - return $backend->isVisibleFor($_['visibilityType']); |
|
| 127 | - }); |
|
| 128 | - uasort($sortedBackends, function($a, $b) { |
|
| 129 | - return strcasecmp($a->getText(), $b->getText()); |
|
| 130 | - }); |
|
| 131 | - ?> |
|
| 125 | + $sortedBackends = array_filter($_['backends'], function($backend) use ($_) { |
|
| 126 | + return $backend->isVisibleFor($_['visibilityType']); |
|
| 127 | + }); |
|
| 128 | + uasort($sortedBackends, function($a, $b) { |
|
| 129 | + return strcasecmp($a->getText(), $b->getText()); |
|
| 130 | + }); |
|
| 131 | + ?> |
|
| 132 | 132 | <?php foreach ($sortedBackends as $backend): ?> |
| 133 | 133 | <?php if ($backend->getDeprecateTo()) continue; // ignore deprecated backends ?> |
| 134 | 134 | <option value="<?php p($backend->getIdentifier()); ?>"><?php p($backend->getText()); ?></option> |
@@ -175,10 +175,10 @@ discard block |
||
| 175 | 175 | <p id="userMountingBackends"<?php if ($_['allowUserMounting'] !== 'yes'): ?> class="hidden"<?php endif; ?>> |
| 176 | 176 | <?php p($l->t('Allow users to mount the following external storage')); ?><br /> |
| 177 | 177 | <?php |
| 178 | - $userBackends = array_filter($_['backends'], function($backend) { |
|
| 179 | - return $backend->isAllowedVisibleFor(BackendService::VISIBILITY_PERSONAL); |
|
| 180 | - }); |
|
| 181 | - ?> |
|
| 178 | + $userBackends = array_filter($_['backends'], function($backend) { |
|
| 179 | + return $backend->isAllowedVisibleFor(BackendService::VISIBILITY_PERSONAL); |
|
| 180 | + }); |
|
| 181 | + ?> |
|
| 182 | 182 | <?php $i = 0; foreach ($userBackends as $backend): ?> |
| 183 | 183 | <?php if ($deprecateTo = $backend->getDeprecateTo()): ?> |
| 184 | 184 | <input type="hidden" id="allowUserMountingBackends<?php p($i); ?>" name="allowUserMountingBackends[]" value="<?php p($backend->getIdentifier()); ?>" data-deprecate-to="<?php p($deprecateTo->getIdentifier()); ?>" /> |
@@ -87,7 +87,7 @@ |
||
| 87 | 87 | } |
| 88 | 88 | ?> |
| 89 | 89 | |
| 90 | -<form data-can-create="<?php echo $canCreateMounts?'true':'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled']?'true': 'false'; ?>"> |
|
| 90 | +<form data-can-create="<?php echo $canCreateMounts ? 'true' : 'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled'] ? 'true' : 'false'; ?>"> |
|
| 91 | 91 | <h2 data-anchor-name="external-storage"><?php p($l->t('External storages')); ?></h2> |
| 92 | 92 | <?php if (isset($_['dependencies']) and ($_['dependencies'] !== '') and $canCreateMounts) print_unescaped(''.$_['dependencies'].''); ?> |
| 93 | 93 | <table id="externalStorage" class="grid" data-admin='<?php print_unescaped(json_encode($_['visibilityType'] === BackendService::VISIBILITY_ADMIN)); ?>'> |
@@ -89,7 +89,10 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | <form data-can-create="<?php echo $canCreateMounts?'true':'false' ?>" id="files_external" class="section" data-encryption-enabled="<?php echo $_['encryptionEnabled']?'true': 'false'; ?>"> |
| 91 | 91 | <h2 data-anchor-name="external-storage"><?php p($l->t('External storages')); ?></h2> |
| 92 | - <?php if (isset($_['dependencies']) and ($_['dependencies'] !== '') and $canCreateMounts) print_unescaped(''.$_['dependencies'].''); ?> |
|
| 92 | + <?php if (isset($_['dependencies']) and ($_['dependencies'] !== '') and $canCreateMounts) { |
|
| 93 | + print_unescaped(''.$_['dependencies'].''); |
|
| 94 | +} |
|
| 95 | +?> |
|
| 93 | 96 | <table id="externalStorage" class="grid" data-admin='<?php print_unescaped(json_encode($_['visibilityType'] === BackendService::VISIBILITY_ADMIN)); ?>'> |
| 94 | 97 | <thead> |
| 95 | 98 | <tr> |
@@ -98,7 +101,10 @@ discard block |
||
| 98 | 101 | <th><?php p($l->t('External storage')); ?></th> |
| 99 | 102 | <th><?php p($l->t('Authentication')); ?></th> |
| 100 | 103 | <th><?php p($l->t('Configuration')); ?></th> |
| 101 | - <?php if ($_['visibilityType'] === BackendService::VISIBILITY_ADMIN) print_unescaped('<th>'.$l->t('Available for').'</th>'); ?> |
|
| 104 | + <?php if ($_['visibilityType'] === BackendService::VISIBILITY_ADMIN) { |
|
| 105 | + print_unescaped('<th>'.$l->t('Available for').'</th>'); |
|
| 106 | +} |
|
| 107 | +?> |
|
| 102 | 108 | <th> </th> |
| 103 | 109 | <th> </th> |
| 104 | 110 | </tr> |
@@ -130,7 +136,10 @@ discard block |
||
| 130 | 136 | }); |
| 131 | 137 | ?> |
| 132 | 138 | <?php foreach ($sortedBackends as $backend): ?> |
| 133 | - <?php if ($backend->getDeprecateTo()) continue; // ignore deprecated backends ?> |
|
| 139 | + <?php if ($backend->getDeprecateTo()) { |
|
| 140 | + continue; |
|
| 141 | +} |
|
| 142 | +// ignore deprecated backends ?> |
|
| 134 | 143 | <option value="<?php p($backend->getIdentifier()); ?>"><?php p($backend->getText()); ?></option> |
| 135 | 144 | <?php endforeach; ?> |
| 136 | 145 | </select> |
@@ -169,7 +178,10 @@ discard block |
||
| 169 | 178 | |
| 170 | 179 | <?php if ($_['visibilityType'] === BackendService::VISIBILITY_ADMIN): ?> |
| 171 | 180 | <input type="checkbox" name="allowUserMounting" id="allowUserMounting" class="checkbox" |
| 172 | - value="1" <?php if ($_['allowUserMounting'] === 'yes') print_unescaped(' checked="checked"'); ?> /> |
|
| 181 | + value="1" <?php if ($_['allowUserMounting'] === 'yes') { |
|
| 182 | + print_unescaped(' checked="checked"'); |
|
| 183 | +} |
|
| 184 | +?> /> |
|
| 173 | 185 | <label for="allowUserMounting"><?php p($l->t('Allow users to mount external storage')); ?></label> <span id="userMountingMsg" class="msg"></span> |
| 174 | 186 | |
| 175 | 187 | <p id="userMountingBackends"<?php if ($_['allowUserMounting'] !== 'yes'): ?> class="hidden"<?php endif; ?>> |
@@ -182,8 +194,14 @@ discard block |
||
| 182 | 194 | <?php $i = 0; foreach ($userBackends as $backend): ?> |
| 183 | 195 | <?php if ($deprecateTo = $backend->getDeprecateTo()): ?> |
| 184 | 196 | <input type="hidden" id="allowUserMountingBackends<?php p($i); ?>" name="allowUserMountingBackends[]" value="<?php p($backend->getIdentifier()); ?>" data-deprecate-to="<?php p($deprecateTo->getIdentifier()); ?>" /> |
| 185 | - <?php else: ?> |
|
| 186 | - <input type="checkbox" id="allowUserMountingBackends<?php p($i); ?>" class="checkbox" name="allowUserMountingBackends[]" value="<?php p($backend->getIdentifier()); ?>" <?php if ($backend->isVisibleFor(BackendService::VISIBILITY_PERSONAL)) print_unescaped(' checked="checked"'); ?> /> |
|
| 197 | + <?php else { |
|
| 198 | + : ?> |
|
| 199 | + <input type="checkbox" id="allowUserMountingBackends<?php p($i); |
|
| 200 | +} |
|
| 201 | +?>" class="checkbox" name="allowUserMountingBackends[]" value="<?php p($backend->getIdentifier()); ?>" <?php if ($backend->isVisibleFor(BackendService::VISIBILITY_PERSONAL)) { |
|
| 202 | + print_unescaped(' checked="checked"'); |
|
| 203 | +} |
|
| 204 | +?> /> |
|
| 187 | 205 | <label for="allowUserMountingBackends<?php p($i); ?>"><?php p($backend->getText()); ?></label> <br /> |
| 188 | 206 | <?php endif; ?> |
| 189 | 207 | <?php $i++; ?> |
@@ -48,606 +48,606 @@ |
||
| 48 | 48 | |
| 49 | 49 | class Swift extends \OC\Files\Storage\Common { |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var \OpenCloud\ObjectStore\Service |
|
| 53 | - */ |
|
| 54 | - private $connection; |
|
| 55 | - /** |
|
| 56 | - * @var \OpenCloud\ObjectStore\Resource\Container |
|
| 57 | - */ |
|
| 58 | - private $container; |
|
| 59 | - /** |
|
| 60 | - * @var \OpenCloud\OpenStack |
|
| 61 | - */ |
|
| 62 | - private $anchor; |
|
| 63 | - /** |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - private $bucket; |
|
| 67 | - /** |
|
| 68 | - * Connection parameters |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - private $params; |
|
| 73 | - |
|
| 74 | - /** @var string */ |
|
| 75 | - private $id; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - private static $tmpFiles = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Key value cache mapping path to data object. Maps path to |
|
| 84 | - * \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject for existing |
|
| 85 | - * paths and path to false for not existing paths. |
|
| 86 | - * @var \OCP\ICache |
|
| 87 | - */ |
|
| 88 | - private $objectCache; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $path |
|
| 92 | - */ |
|
| 93 | - private function normalizePath($path) { |
|
| 94 | - $path = trim($path, '/'); |
|
| 95 | - |
|
| 96 | - if (!$path) { |
|
| 97 | - $path = '.'; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $path = str_replace('#', '%23', $path); |
|
| 101 | - |
|
| 102 | - return $path; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - const SUBCONTAINER_FILE = '.subcontainers'; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * translate directory path to container name |
|
| 109 | - * |
|
| 110 | - * @param string $path |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Fetches an object from the API. |
|
| 116 | - * If the object is cached already or a |
|
| 117 | - * failed "doesn't exist" response was cached, |
|
| 118 | - * that one will be returned. |
|
| 119 | - * |
|
| 120 | - * @param string $path |
|
| 121 | - * @return \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject|bool object |
|
| 122 | - * or false if the object did not exist |
|
| 123 | - */ |
|
| 124 | - private function fetchObject($path) { |
|
| 125 | - if ($this->objectCache->hasKey($path)) { |
|
| 126 | - // might be "false" if object did not exist from last check |
|
| 127 | - return $this->objectCache->get($path); |
|
| 128 | - } |
|
| 129 | - try { |
|
| 130 | - $object = $this->getContainer()->getPartialObject($path); |
|
| 131 | - $this->objectCache->set($path, $object); |
|
| 132 | - return $object; |
|
| 133 | - } catch (ClientErrorResponseException $e) { |
|
| 134 | - // this exception happens when the object does not exist, which |
|
| 135 | - // is expected in most cases |
|
| 136 | - $this->objectCache->set($path, false); |
|
| 137 | - return false; |
|
| 138 | - } catch (ClientErrorResponseException $e) { |
|
| 139 | - // Expected response is "404 Not Found", so only log if it isn't |
|
| 140 | - if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 141 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 142 | - } |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Returns whether the given path exists. |
|
| 149 | - * |
|
| 150 | - * @param string $path |
|
| 151 | - * |
|
| 152 | - * @return bool true if the object exist, false otherwise |
|
| 153 | - */ |
|
| 154 | - private function doesObjectExist($path) { |
|
| 155 | - return $this->fetchObject($path) !== false; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function __construct($params) { |
|
| 159 | - if ((empty($params['key']) and empty($params['password'])) |
|
| 160 | - or empty($params['user']) or empty($params['bucket']) |
|
| 161 | - or empty($params['region']) |
|
| 162 | - ) { |
|
| 163 | - throw new \Exception("API Key or password, Username, Bucket and Region have to be configured."); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $this->id = 'swift::' . $params['user'] . md5($params['bucket']); |
|
| 167 | - |
|
| 168 | - $bucketUrl = Url::factory($params['bucket']); |
|
| 169 | - if ($bucketUrl->isAbsolute()) { |
|
| 170 | - $this->bucket = end(($bucketUrl->getPathSegments())); |
|
| 171 | - $params['endpoint_url'] = $bucketUrl->addPath('..')->normalizePath(); |
|
| 172 | - } else { |
|
| 173 | - $this->bucket = $params['bucket']; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if (empty($params['url'])) { |
|
| 177 | - $params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/'; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - if (empty($params['service_name'])) { |
|
| 181 | - $params['service_name'] = 'cloudFiles'; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $this->params = $params; |
|
| 185 | - // FIXME: private class... |
|
| 186 | - $this->objectCache = new \OC\Cache\CappedMemoryCache(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - public function mkdir($path) { |
|
| 190 | - $path = $this->normalizePath($path); |
|
| 191 | - |
|
| 192 | - if ($this->is_dir($path)) { |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - if ($path !== '.') { |
|
| 197 | - $path .= '/'; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - try { |
|
| 201 | - $customHeaders = array('content-type' => 'httpd/unix-directory'); |
|
| 202 | - $metadataHeaders = DataObject::stockHeaders(array()); |
|
| 203 | - $allHeaders = $customHeaders + $metadataHeaders; |
|
| 204 | - $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 205 | - // invalidate so that the next access gets the real object |
|
| 206 | - // with all properties |
|
| 207 | - $this->objectCache->remove($path); |
|
| 208 | - } catch (Exceptions\CreateUpdateError $e) { |
|
| 209 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return true; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - public function file_exists($path) { |
|
| 217 | - $path = $this->normalizePath($path); |
|
| 218 | - |
|
| 219 | - if ($path !== '.' && $this->is_dir($path)) { |
|
| 220 | - $path .= '/'; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return $this->doesObjectExist($path); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - public function rmdir($path) { |
|
| 227 | - $path = $this->normalizePath($path); |
|
| 228 | - |
|
| 229 | - if (!$this->is_dir($path) || !$this->isDeletable($path)) { |
|
| 230 | - return false; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $dh = $this->opendir($path); |
|
| 234 | - while ($file = readdir($dh)) { |
|
| 235 | - if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 236 | - continue; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if ($this->is_dir($path . '/' . $file)) { |
|
| 240 | - $this->rmdir($path . '/' . $file); |
|
| 241 | - } else { |
|
| 242 | - $this->unlink($path . '/' . $file); |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - try { |
|
| 247 | - $this->getContainer()->dataObject()->setName($path . '/')->delete(); |
|
| 248 | - $this->objectCache->remove($path . '/'); |
|
| 249 | - } catch (Exceptions\DeleteError $e) { |
|
| 250 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return true; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - public function opendir($path) { |
|
| 258 | - $path = $this->normalizePath($path); |
|
| 259 | - |
|
| 260 | - if ($path === '.') { |
|
| 261 | - $path = ''; |
|
| 262 | - } else { |
|
| 263 | - $path .= '/'; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of # |
|
| 267 | - |
|
| 268 | - try { |
|
| 269 | - $files = array(); |
|
| 270 | - /** @var OpenCloud\Common\Collection $objects */ |
|
| 271 | - $objects = $this->getContainer()->objectList(array( |
|
| 272 | - 'prefix' => $path, |
|
| 273 | - 'delimiter' => '/' |
|
| 274 | - )); |
|
| 275 | - |
|
| 276 | - /** @var OpenCloud\ObjectStore\Resource\DataObject $object */ |
|
| 277 | - foreach ($objects as $object) { |
|
| 278 | - $file = basename($object->getName()); |
|
| 279 | - if ($file !== basename($path)) { |
|
| 280 | - $files[] = $file; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return IteratorDirectory::wrap($files); |
|
| 285 | - } catch (\Exception $e) { |
|
| 286 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 287 | - return false; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - public function stat($path) { |
|
| 293 | - $path = $this->normalizePath($path); |
|
| 294 | - |
|
| 295 | - if ($path === '.') { |
|
| 296 | - $path = ''; |
|
| 297 | - } else if ($this->is_dir($path)) { |
|
| 298 | - $path .= '/'; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - try { |
|
| 302 | - /** @var DataObject $object */ |
|
| 303 | - $object = $this->fetchObject($path); |
|
| 304 | - if (!$object) { |
|
| 305 | - return false; |
|
| 306 | - } |
|
| 307 | - } catch (ClientErrorResponseException $e) { |
|
| 308 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $dateTime = \DateTime::createFromFormat(\DateTime::RFC1123, $object->getLastModified()); |
|
| 313 | - if ($dateTime !== false) { |
|
| 314 | - $mtime = $dateTime->getTimestamp(); |
|
| 315 | - } else { |
|
| 316 | - $mtime = null; |
|
| 317 | - } |
|
| 318 | - $objectMetadata = $object->getMetadata(); |
|
| 319 | - $metaTimestamp = $objectMetadata->getProperty('timestamp'); |
|
| 320 | - if (isset($metaTimestamp)) { |
|
| 321 | - $mtime = $metaTimestamp; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if (!empty($mtime)) { |
|
| 325 | - $mtime = floor($mtime); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - $stat = array(); |
|
| 329 | - $stat['size'] = (int)$object->getContentLength(); |
|
| 330 | - $stat['mtime'] = $mtime; |
|
| 331 | - $stat['atime'] = time(); |
|
| 332 | - return $stat; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - public function filetype($path) { |
|
| 336 | - $path = $this->normalizePath($path); |
|
| 337 | - |
|
| 338 | - if ($path !== '.' && $this->doesObjectExist($path)) { |
|
| 339 | - return 'file'; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - if ($path !== '.') { |
|
| 343 | - $path .= '/'; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - if ($this->doesObjectExist($path)) { |
|
| 347 | - return 'dir'; |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - public function unlink($path) { |
|
| 352 | - $path = $this->normalizePath($path); |
|
| 353 | - |
|
| 354 | - if ($this->is_dir($path)) { |
|
| 355 | - return $this->rmdir($path); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - try { |
|
| 359 | - $this->getContainer()->dataObject()->setName($path)->delete(); |
|
| 360 | - $this->objectCache->remove($path); |
|
| 361 | - $this->objectCache->remove($path . '/'); |
|
| 362 | - } catch (ClientErrorResponseException $e) { |
|
| 363 | - if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 364 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 365 | - } |
|
| 366 | - return false; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return true; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - public function fopen($path, $mode) { |
|
| 373 | - $path = $this->normalizePath($path); |
|
| 374 | - |
|
| 375 | - switch ($mode) { |
|
| 376 | - case 'a': |
|
| 377 | - case 'ab': |
|
| 378 | - case 'a+': |
|
| 379 | - return false; |
|
| 380 | - case 'r': |
|
| 381 | - case 'rb': |
|
| 382 | - try { |
|
| 383 | - $c = $this->getContainer(); |
|
| 384 | - $streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); |
|
| 385 | - $streamInterface = $streamFactory->fromRequest( |
|
| 386 | - $c->getClient() |
|
| 387 | - ->get($c->getUrl($path))); |
|
| 388 | - $streamInterface->rewind(); |
|
| 389 | - $stream = $streamInterface->getStream(); |
|
| 390 | - stream_context_set_option($stream, 'swift','content', $streamInterface); |
|
| 391 | - if(!strrpos($streamInterface |
|
| 392 | - ->getMetaData('wrapper_data')[0], '404 Not Found')) { |
|
| 393 | - return $stream; |
|
| 394 | - } |
|
| 395 | - return false; |
|
| 396 | - } catch (\Guzzle\Http\Exception\BadResponseException $e) { |
|
| 397 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - case 'w': |
|
| 401 | - case 'wb': |
|
| 402 | - case 'r+': |
|
| 403 | - case 'w+': |
|
| 404 | - case 'wb+': |
|
| 405 | - case 'x': |
|
| 406 | - case 'x+': |
|
| 407 | - case 'c': |
|
| 408 | - case 'c+': |
|
| 409 | - if (strrpos($path, '.') !== false) { |
|
| 410 | - $ext = substr($path, strrpos($path, '.')); |
|
| 411 | - } else { |
|
| 412 | - $ext = ''; |
|
| 413 | - } |
|
| 414 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 415 | - // Fetch existing file if required |
|
| 416 | - if ($mode[0] !== 'w' && $this->file_exists($path)) { |
|
| 417 | - if ($mode[0] === 'x') { |
|
| 418 | - // File cannot already exist |
|
| 419 | - return false; |
|
| 420 | - } |
|
| 421 | - $source = $this->fopen($path, 'r'); |
|
| 422 | - file_put_contents($tmpFile, $source); |
|
| 423 | - } |
|
| 424 | - $handle = fopen($tmpFile, $mode); |
|
| 425 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 426 | - $this->writeBack($tmpFile, $path); |
|
| 427 | - }); |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - public function touch($path, $mtime = null) { |
|
| 432 | - $path = $this->normalizePath($path); |
|
| 433 | - if (is_null($mtime)) { |
|
| 434 | - $mtime = time(); |
|
| 435 | - } |
|
| 436 | - $metadata = array('timestamp' => $mtime); |
|
| 437 | - if ($this->file_exists($path)) { |
|
| 438 | - if ($this->is_dir($path) && $path !== '.') { |
|
| 439 | - $path .= '/'; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $object = $this->fetchObject($path); |
|
| 443 | - if ($object->saveMetadata($metadata)) { |
|
| 444 | - // invalidate target object to force repopulation on fetch |
|
| 445 | - $this->objectCache->remove($path); |
|
| 446 | - } |
|
| 447 | - return true; |
|
| 448 | - } else { |
|
| 449 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 450 | - $customHeaders = array('content-type' => $mimeType); |
|
| 451 | - $metadataHeaders = DataObject::stockHeaders($metadata); |
|
| 452 | - $allHeaders = $customHeaders + $metadataHeaders; |
|
| 453 | - $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 454 | - // invalidate target object to force repopulation on fetch |
|
| 455 | - $this->objectCache->remove($path); |
|
| 456 | - return true; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - public function copy($path1, $path2) { |
|
| 461 | - $path1 = $this->normalizePath($path1); |
|
| 462 | - $path2 = $this->normalizePath($path2); |
|
| 463 | - |
|
| 464 | - $fileType = $this->filetype($path1); |
|
| 465 | - if ($fileType === 'file') { |
|
| 466 | - |
|
| 467 | - // make way |
|
| 468 | - $this->unlink($path2); |
|
| 469 | - |
|
| 470 | - try { |
|
| 471 | - $source = $this->fetchObject($path1); |
|
| 472 | - $source->copy($this->bucket . '/' . $path2); |
|
| 473 | - // invalidate target object to force repopulation on fetch |
|
| 474 | - $this->objectCache->remove($path2); |
|
| 475 | - $this->objectCache->remove($path2 . '/'); |
|
| 476 | - } catch (ClientErrorResponseException $e) { |
|
| 477 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 478 | - return false; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - } else if ($fileType === 'dir') { |
|
| 482 | - |
|
| 483 | - // make way |
|
| 484 | - $this->unlink($path2); |
|
| 485 | - |
|
| 486 | - try { |
|
| 487 | - $source = $this->fetchObject($path1 . '/'); |
|
| 488 | - $source->copy($this->bucket . '/' . $path2 . '/'); |
|
| 489 | - // invalidate target object to force repopulation on fetch |
|
| 490 | - $this->objectCache->remove($path2); |
|
| 491 | - $this->objectCache->remove($path2 . '/'); |
|
| 492 | - } catch (ClientErrorResponseException $e) { |
|
| 493 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 494 | - return false; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $dh = $this->opendir($path1); |
|
| 498 | - while ($file = readdir($dh)) { |
|
| 499 | - if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 500 | - continue; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - $source = $path1 . '/' . $file; |
|
| 504 | - $target = $path2 . '/' . $file; |
|
| 505 | - $this->copy($source, $target); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - } else { |
|
| 509 | - //file does not exist |
|
| 510 | - return false; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - return true; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - public function rename($path1, $path2) { |
|
| 517 | - $path1 = $this->normalizePath($path1); |
|
| 518 | - $path2 = $this->normalizePath($path2); |
|
| 519 | - |
|
| 520 | - $fileType = $this->filetype($path1); |
|
| 521 | - |
|
| 522 | - if ($fileType === 'dir' || $fileType === 'file') { |
|
| 523 | - // copy |
|
| 524 | - if ($this->copy($path1, $path2) === false) { |
|
| 525 | - return false; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - // cleanup |
|
| 529 | - if ($this->unlink($path1) === false) { |
|
| 530 | - $this->unlink($path2); |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - return true; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - return false; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - public function getId() { |
|
| 541 | - return $this->id; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * Returns the connection |
|
| 546 | - * |
|
| 547 | - * @return OpenCloud\ObjectStore\Service connected client |
|
| 548 | - * @throws \Exception if connection could not be made |
|
| 549 | - */ |
|
| 550 | - public function getConnection() { |
|
| 551 | - if (!is_null($this->connection)) { |
|
| 552 | - return $this->connection; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - $settings = array( |
|
| 556 | - 'username' => $this->params['user'], |
|
| 557 | - ); |
|
| 558 | - |
|
| 559 | - if (!empty($this->params['password'])) { |
|
| 560 | - $settings['password'] = $this->params['password']; |
|
| 561 | - } else if (!empty($this->params['key'])) { |
|
| 562 | - $settings['apiKey'] = $this->params['key']; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - if (!empty($this->params['tenant'])) { |
|
| 566 | - $settings['tenantName'] = $this->params['tenant']; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - if (!empty($this->params['timeout'])) { |
|
| 570 | - $settings['timeout'] = $this->params['timeout']; |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - if (isset($settings['apiKey'])) { |
|
| 574 | - $this->anchor = new Rackspace($this->params['url'], $settings); |
|
| 575 | - } else { |
|
| 576 | - $this->anchor = new OpenStack($this->params['url'], $settings); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - $connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']); |
|
| 580 | - |
|
| 581 | - if (!empty($this->params['endpoint_url'])) { |
|
| 582 | - $endpoint = $connection->getEndpoint(); |
|
| 583 | - $endpoint->setPublicUrl($this->params['endpoint_url']); |
|
| 584 | - $endpoint->setPrivateUrl($this->params['endpoint_url']); |
|
| 585 | - $connection->setEndpoint($endpoint); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - $this->connection = $connection; |
|
| 589 | - |
|
| 590 | - return $this->connection; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Returns the initialized object store container. |
|
| 595 | - * |
|
| 596 | - * @return OpenCloud\ObjectStore\Resource\Container |
|
| 597 | - */ |
|
| 598 | - public function getContainer() { |
|
| 599 | - if (!is_null($this->container)) { |
|
| 600 | - return $this->container; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - try { |
|
| 604 | - $this->container = $this->getConnection()->getContainer($this->bucket); |
|
| 605 | - } catch (ClientErrorResponseException $e) { |
|
| 606 | - $this->container = $this->getConnection()->createContainer($this->bucket); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - if (!$this->file_exists('.')) { |
|
| 610 | - $this->mkdir('.'); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - return $this->container; |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - public function writeBack($tmpFile, $path) { |
|
| 617 | - $fileData = fopen($tmpFile, 'r'); |
|
| 618 | - $this->getContainer()->uploadObject($path, $fileData); |
|
| 619 | - // invalidate target object to force repopulation on fetch |
|
| 620 | - $this->objectCache->remove(self::$tmpFiles[$tmpFile]); |
|
| 621 | - unlink($tmpFile); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - public function hasUpdated($path, $time) { |
|
| 625 | - if ($this->is_file($path)) { |
|
| 626 | - return parent::hasUpdated($path, $time); |
|
| 627 | - } |
|
| 628 | - $path = $this->normalizePath($path); |
|
| 629 | - $dh = $this->opendir($path); |
|
| 630 | - $content = array(); |
|
| 631 | - while (($file = readdir($dh)) !== false) { |
|
| 632 | - $content[] = $file; |
|
| 633 | - } |
|
| 634 | - if ($path === '.') { |
|
| 635 | - $path = ''; |
|
| 636 | - } |
|
| 637 | - $cachedContent = $this->getCache()->getFolderContents($path); |
|
| 638 | - $cachedNames = array_map(function ($content) { |
|
| 639 | - return $content['name']; |
|
| 640 | - }, $cachedContent); |
|
| 641 | - sort($cachedNames); |
|
| 642 | - sort($content); |
|
| 643 | - return $cachedNames !== $content; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * check if curl is installed |
|
| 648 | - */ |
|
| 649 | - public static function checkDependencies() { |
|
| 650 | - return true; |
|
| 651 | - } |
|
| 51 | + /** |
|
| 52 | + * @var \OpenCloud\ObjectStore\Service |
|
| 53 | + */ |
|
| 54 | + private $connection; |
|
| 55 | + /** |
|
| 56 | + * @var \OpenCloud\ObjectStore\Resource\Container |
|
| 57 | + */ |
|
| 58 | + private $container; |
|
| 59 | + /** |
|
| 60 | + * @var \OpenCloud\OpenStack |
|
| 61 | + */ |
|
| 62 | + private $anchor; |
|
| 63 | + /** |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + private $bucket; |
|
| 67 | + /** |
|
| 68 | + * Connection parameters |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + private $params; |
|
| 73 | + |
|
| 74 | + /** @var string */ |
|
| 75 | + private $id; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + private static $tmpFiles = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Key value cache mapping path to data object. Maps path to |
|
| 84 | + * \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject for existing |
|
| 85 | + * paths and path to false for not existing paths. |
|
| 86 | + * @var \OCP\ICache |
|
| 87 | + */ |
|
| 88 | + private $objectCache; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $path |
|
| 92 | + */ |
|
| 93 | + private function normalizePath($path) { |
|
| 94 | + $path = trim($path, '/'); |
|
| 95 | + |
|
| 96 | + if (!$path) { |
|
| 97 | + $path = '.'; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $path = str_replace('#', '%23', $path); |
|
| 101 | + |
|
| 102 | + return $path; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + const SUBCONTAINER_FILE = '.subcontainers'; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * translate directory path to container name |
|
| 109 | + * |
|
| 110 | + * @param string $path |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Fetches an object from the API. |
|
| 116 | + * If the object is cached already or a |
|
| 117 | + * failed "doesn't exist" response was cached, |
|
| 118 | + * that one will be returned. |
|
| 119 | + * |
|
| 120 | + * @param string $path |
|
| 121 | + * @return \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject|bool object |
|
| 122 | + * or false if the object did not exist |
|
| 123 | + */ |
|
| 124 | + private function fetchObject($path) { |
|
| 125 | + if ($this->objectCache->hasKey($path)) { |
|
| 126 | + // might be "false" if object did not exist from last check |
|
| 127 | + return $this->objectCache->get($path); |
|
| 128 | + } |
|
| 129 | + try { |
|
| 130 | + $object = $this->getContainer()->getPartialObject($path); |
|
| 131 | + $this->objectCache->set($path, $object); |
|
| 132 | + return $object; |
|
| 133 | + } catch (ClientErrorResponseException $e) { |
|
| 134 | + // this exception happens when the object does not exist, which |
|
| 135 | + // is expected in most cases |
|
| 136 | + $this->objectCache->set($path, false); |
|
| 137 | + return false; |
|
| 138 | + } catch (ClientErrorResponseException $e) { |
|
| 139 | + // Expected response is "404 Not Found", so only log if it isn't |
|
| 140 | + if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 141 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 142 | + } |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Returns whether the given path exists. |
|
| 149 | + * |
|
| 150 | + * @param string $path |
|
| 151 | + * |
|
| 152 | + * @return bool true if the object exist, false otherwise |
|
| 153 | + */ |
|
| 154 | + private function doesObjectExist($path) { |
|
| 155 | + return $this->fetchObject($path) !== false; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function __construct($params) { |
|
| 159 | + if ((empty($params['key']) and empty($params['password'])) |
|
| 160 | + or empty($params['user']) or empty($params['bucket']) |
|
| 161 | + or empty($params['region']) |
|
| 162 | + ) { |
|
| 163 | + throw new \Exception("API Key or password, Username, Bucket and Region have to be configured."); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $this->id = 'swift::' . $params['user'] . md5($params['bucket']); |
|
| 167 | + |
|
| 168 | + $bucketUrl = Url::factory($params['bucket']); |
|
| 169 | + if ($bucketUrl->isAbsolute()) { |
|
| 170 | + $this->bucket = end(($bucketUrl->getPathSegments())); |
|
| 171 | + $params['endpoint_url'] = $bucketUrl->addPath('..')->normalizePath(); |
|
| 172 | + } else { |
|
| 173 | + $this->bucket = $params['bucket']; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if (empty($params['url'])) { |
|
| 177 | + $params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/'; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + if (empty($params['service_name'])) { |
|
| 181 | + $params['service_name'] = 'cloudFiles'; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $this->params = $params; |
|
| 185 | + // FIXME: private class... |
|
| 186 | + $this->objectCache = new \OC\Cache\CappedMemoryCache(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + public function mkdir($path) { |
|
| 190 | + $path = $this->normalizePath($path); |
|
| 191 | + |
|
| 192 | + if ($this->is_dir($path)) { |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + if ($path !== '.') { |
|
| 197 | + $path .= '/'; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + try { |
|
| 201 | + $customHeaders = array('content-type' => 'httpd/unix-directory'); |
|
| 202 | + $metadataHeaders = DataObject::stockHeaders(array()); |
|
| 203 | + $allHeaders = $customHeaders + $metadataHeaders; |
|
| 204 | + $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 205 | + // invalidate so that the next access gets the real object |
|
| 206 | + // with all properties |
|
| 207 | + $this->objectCache->remove($path); |
|
| 208 | + } catch (Exceptions\CreateUpdateError $e) { |
|
| 209 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return true; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + public function file_exists($path) { |
|
| 217 | + $path = $this->normalizePath($path); |
|
| 218 | + |
|
| 219 | + if ($path !== '.' && $this->is_dir($path)) { |
|
| 220 | + $path .= '/'; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return $this->doesObjectExist($path); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + public function rmdir($path) { |
|
| 227 | + $path = $this->normalizePath($path); |
|
| 228 | + |
|
| 229 | + if (!$this->is_dir($path) || !$this->isDeletable($path)) { |
|
| 230 | + return false; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $dh = $this->opendir($path); |
|
| 234 | + while ($file = readdir($dh)) { |
|
| 235 | + if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 236 | + continue; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if ($this->is_dir($path . '/' . $file)) { |
|
| 240 | + $this->rmdir($path . '/' . $file); |
|
| 241 | + } else { |
|
| 242 | + $this->unlink($path . '/' . $file); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + try { |
|
| 247 | + $this->getContainer()->dataObject()->setName($path . '/')->delete(); |
|
| 248 | + $this->objectCache->remove($path . '/'); |
|
| 249 | + } catch (Exceptions\DeleteError $e) { |
|
| 250 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return true; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + public function opendir($path) { |
|
| 258 | + $path = $this->normalizePath($path); |
|
| 259 | + |
|
| 260 | + if ($path === '.') { |
|
| 261 | + $path = ''; |
|
| 262 | + } else { |
|
| 263 | + $path .= '/'; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of # |
|
| 267 | + |
|
| 268 | + try { |
|
| 269 | + $files = array(); |
|
| 270 | + /** @var OpenCloud\Common\Collection $objects */ |
|
| 271 | + $objects = $this->getContainer()->objectList(array( |
|
| 272 | + 'prefix' => $path, |
|
| 273 | + 'delimiter' => '/' |
|
| 274 | + )); |
|
| 275 | + |
|
| 276 | + /** @var OpenCloud\ObjectStore\Resource\DataObject $object */ |
|
| 277 | + foreach ($objects as $object) { |
|
| 278 | + $file = basename($object->getName()); |
|
| 279 | + if ($file !== basename($path)) { |
|
| 280 | + $files[] = $file; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return IteratorDirectory::wrap($files); |
|
| 285 | + } catch (\Exception $e) { |
|
| 286 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + public function stat($path) { |
|
| 293 | + $path = $this->normalizePath($path); |
|
| 294 | + |
|
| 295 | + if ($path === '.') { |
|
| 296 | + $path = ''; |
|
| 297 | + } else if ($this->is_dir($path)) { |
|
| 298 | + $path .= '/'; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + try { |
|
| 302 | + /** @var DataObject $object */ |
|
| 303 | + $object = $this->fetchObject($path); |
|
| 304 | + if (!$object) { |
|
| 305 | + return false; |
|
| 306 | + } |
|
| 307 | + } catch (ClientErrorResponseException $e) { |
|
| 308 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $dateTime = \DateTime::createFromFormat(\DateTime::RFC1123, $object->getLastModified()); |
|
| 313 | + if ($dateTime !== false) { |
|
| 314 | + $mtime = $dateTime->getTimestamp(); |
|
| 315 | + } else { |
|
| 316 | + $mtime = null; |
|
| 317 | + } |
|
| 318 | + $objectMetadata = $object->getMetadata(); |
|
| 319 | + $metaTimestamp = $objectMetadata->getProperty('timestamp'); |
|
| 320 | + if (isset($metaTimestamp)) { |
|
| 321 | + $mtime = $metaTimestamp; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if (!empty($mtime)) { |
|
| 325 | + $mtime = floor($mtime); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + $stat = array(); |
|
| 329 | + $stat['size'] = (int)$object->getContentLength(); |
|
| 330 | + $stat['mtime'] = $mtime; |
|
| 331 | + $stat['atime'] = time(); |
|
| 332 | + return $stat; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + public function filetype($path) { |
|
| 336 | + $path = $this->normalizePath($path); |
|
| 337 | + |
|
| 338 | + if ($path !== '.' && $this->doesObjectExist($path)) { |
|
| 339 | + return 'file'; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + if ($path !== '.') { |
|
| 343 | + $path .= '/'; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + if ($this->doesObjectExist($path)) { |
|
| 347 | + return 'dir'; |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + public function unlink($path) { |
|
| 352 | + $path = $this->normalizePath($path); |
|
| 353 | + |
|
| 354 | + if ($this->is_dir($path)) { |
|
| 355 | + return $this->rmdir($path); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + try { |
|
| 359 | + $this->getContainer()->dataObject()->setName($path)->delete(); |
|
| 360 | + $this->objectCache->remove($path); |
|
| 361 | + $this->objectCache->remove($path . '/'); |
|
| 362 | + } catch (ClientErrorResponseException $e) { |
|
| 363 | + if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 364 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 365 | + } |
|
| 366 | + return false; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return true; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + public function fopen($path, $mode) { |
|
| 373 | + $path = $this->normalizePath($path); |
|
| 374 | + |
|
| 375 | + switch ($mode) { |
|
| 376 | + case 'a': |
|
| 377 | + case 'ab': |
|
| 378 | + case 'a+': |
|
| 379 | + return false; |
|
| 380 | + case 'r': |
|
| 381 | + case 'rb': |
|
| 382 | + try { |
|
| 383 | + $c = $this->getContainer(); |
|
| 384 | + $streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); |
|
| 385 | + $streamInterface = $streamFactory->fromRequest( |
|
| 386 | + $c->getClient() |
|
| 387 | + ->get($c->getUrl($path))); |
|
| 388 | + $streamInterface->rewind(); |
|
| 389 | + $stream = $streamInterface->getStream(); |
|
| 390 | + stream_context_set_option($stream, 'swift','content', $streamInterface); |
|
| 391 | + if(!strrpos($streamInterface |
|
| 392 | + ->getMetaData('wrapper_data')[0], '404 Not Found')) { |
|
| 393 | + return $stream; |
|
| 394 | + } |
|
| 395 | + return false; |
|
| 396 | + } catch (\Guzzle\Http\Exception\BadResponseException $e) { |
|
| 397 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + case 'w': |
|
| 401 | + case 'wb': |
|
| 402 | + case 'r+': |
|
| 403 | + case 'w+': |
|
| 404 | + case 'wb+': |
|
| 405 | + case 'x': |
|
| 406 | + case 'x+': |
|
| 407 | + case 'c': |
|
| 408 | + case 'c+': |
|
| 409 | + if (strrpos($path, '.') !== false) { |
|
| 410 | + $ext = substr($path, strrpos($path, '.')); |
|
| 411 | + } else { |
|
| 412 | + $ext = ''; |
|
| 413 | + } |
|
| 414 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 415 | + // Fetch existing file if required |
|
| 416 | + if ($mode[0] !== 'w' && $this->file_exists($path)) { |
|
| 417 | + if ($mode[0] === 'x') { |
|
| 418 | + // File cannot already exist |
|
| 419 | + return false; |
|
| 420 | + } |
|
| 421 | + $source = $this->fopen($path, 'r'); |
|
| 422 | + file_put_contents($tmpFile, $source); |
|
| 423 | + } |
|
| 424 | + $handle = fopen($tmpFile, $mode); |
|
| 425 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 426 | + $this->writeBack($tmpFile, $path); |
|
| 427 | + }); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + public function touch($path, $mtime = null) { |
|
| 432 | + $path = $this->normalizePath($path); |
|
| 433 | + if (is_null($mtime)) { |
|
| 434 | + $mtime = time(); |
|
| 435 | + } |
|
| 436 | + $metadata = array('timestamp' => $mtime); |
|
| 437 | + if ($this->file_exists($path)) { |
|
| 438 | + if ($this->is_dir($path) && $path !== '.') { |
|
| 439 | + $path .= '/'; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $object = $this->fetchObject($path); |
|
| 443 | + if ($object->saveMetadata($metadata)) { |
|
| 444 | + // invalidate target object to force repopulation on fetch |
|
| 445 | + $this->objectCache->remove($path); |
|
| 446 | + } |
|
| 447 | + return true; |
|
| 448 | + } else { |
|
| 449 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 450 | + $customHeaders = array('content-type' => $mimeType); |
|
| 451 | + $metadataHeaders = DataObject::stockHeaders($metadata); |
|
| 452 | + $allHeaders = $customHeaders + $metadataHeaders; |
|
| 453 | + $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 454 | + // invalidate target object to force repopulation on fetch |
|
| 455 | + $this->objectCache->remove($path); |
|
| 456 | + return true; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + public function copy($path1, $path2) { |
|
| 461 | + $path1 = $this->normalizePath($path1); |
|
| 462 | + $path2 = $this->normalizePath($path2); |
|
| 463 | + |
|
| 464 | + $fileType = $this->filetype($path1); |
|
| 465 | + if ($fileType === 'file') { |
|
| 466 | + |
|
| 467 | + // make way |
|
| 468 | + $this->unlink($path2); |
|
| 469 | + |
|
| 470 | + try { |
|
| 471 | + $source = $this->fetchObject($path1); |
|
| 472 | + $source->copy($this->bucket . '/' . $path2); |
|
| 473 | + // invalidate target object to force repopulation on fetch |
|
| 474 | + $this->objectCache->remove($path2); |
|
| 475 | + $this->objectCache->remove($path2 . '/'); |
|
| 476 | + } catch (ClientErrorResponseException $e) { |
|
| 477 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 478 | + return false; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + } else if ($fileType === 'dir') { |
|
| 482 | + |
|
| 483 | + // make way |
|
| 484 | + $this->unlink($path2); |
|
| 485 | + |
|
| 486 | + try { |
|
| 487 | + $source = $this->fetchObject($path1 . '/'); |
|
| 488 | + $source->copy($this->bucket . '/' . $path2 . '/'); |
|
| 489 | + // invalidate target object to force repopulation on fetch |
|
| 490 | + $this->objectCache->remove($path2); |
|
| 491 | + $this->objectCache->remove($path2 . '/'); |
|
| 492 | + } catch (ClientErrorResponseException $e) { |
|
| 493 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 494 | + return false; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $dh = $this->opendir($path1); |
|
| 498 | + while ($file = readdir($dh)) { |
|
| 499 | + if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 500 | + continue; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + $source = $path1 . '/' . $file; |
|
| 504 | + $target = $path2 . '/' . $file; |
|
| 505 | + $this->copy($source, $target); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + } else { |
|
| 509 | + //file does not exist |
|
| 510 | + return false; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + return true; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + public function rename($path1, $path2) { |
|
| 517 | + $path1 = $this->normalizePath($path1); |
|
| 518 | + $path2 = $this->normalizePath($path2); |
|
| 519 | + |
|
| 520 | + $fileType = $this->filetype($path1); |
|
| 521 | + |
|
| 522 | + if ($fileType === 'dir' || $fileType === 'file') { |
|
| 523 | + // copy |
|
| 524 | + if ($this->copy($path1, $path2) === false) { |
|
| 525 | + return false; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + // cleanup |
|
| 529 | + if ($this->unlink($path1) === false) { |
|
| 530 | + $this->unlink($path2); |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + return true; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + return false; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + public function getId() { |
|
| 541 | + return $this->id; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * Returns the connection |
|
| 546 | + * |
|
| 547 | + * @return OpenCloud\ObjectStore\Service connected client |
|
| 548 | + * @throws \Exception if connection could not be made |
|
| 549 | + */ |
|
| 550 | + public function getConnection() { |
|
| 551 | + if (!is_null($this->connection)) { |
|
| 552 | + return $this->connection; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + $settings = array( |
|
| 556 | + 'username' => $this->params['user'], |
|
| 557 | + ); |
|
| 558 | + |
|
| 559 | + if (!empty($this->params['password'])) { |
|
| 560 | + $settings['password'] = $this->params['password']; |
|
| 561 | + } else if (!empty($this->params['key'])) { |
|
| 562 | + $settings['apiKey'] = $this->params['key']; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + if (!empty($this->params['tenant'])) { |
|
| 566 | + $settings['tenantName'] = $this->params['tenant']; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + if (!empty($this->params['timeout'])) { |
|
| 570 | + $settings['timeout'] = $this->params['timeout']; |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + if (isset($settings['apiKey'])) { |
|
| 574 | + $this->anchor = new Rackspace($this->params['url'], $settings); |
|
| 575 | + } else { |
|
| 576 | + $this->anchor = new OpenStack($this->params['url'], $settings); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + $connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']); |
|
| 580 | + |
|
| 581 | + if (!empty($this->params['endpoint_url'])) { |
|
| 582 | + $endpoint = $connection->getEndpoint(); |
|
| 583 | + $endpoint->setPublicUrl($this->params['endpoint_url']); |
|
| 584 | + $endpoint->setPrivateUrl($this->params['endpoint_url']); |
|
| 585 | + $connection->setEndpoint($endpoint); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + $this->connection = $connection; |
|
| 589 | + |
|
| 590 | + return $this->connection; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Returns the initialized object store container. |
|
| 595 | + * |
|
| 596 | + * @return OpenCloud\ObjectStore\Resource\Container |
|
| 597 | + */ |
|
| 598 | + public function getContainer() { |
|
| 599 | + if (!is_null($this->container)) { |
|
| 600 | + return $this->container; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + try { |
|
| 604 | + $this->container = $this->getConnection()->getContainer($this->bucket); |
|
| 605 | + } catch (ClientErrorResponseException $e) { |
|
| 606 | + $this->container = $this->getConnection()->createContainer($this->bucket); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + if (!$this->file_exists('.')) { |
|
| 610 | + $this->mkdir('.'); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + return $this->container; |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + public function writeBack($tmpFile, $path) { |
|
| 617 | + $fileData = fopen($tmpFile, 'r'); |
|
| 618 | + $this->getContainer()->uploadObject($path, $fileData); |
|
| 619 | + // invalidate target object to force repopulation on fetch |
|
| 620 | + $this->objectCache->remove(self::$tmpFiles[$tmpFile]); |
|
| 621 | + unlink($tmpFile); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + public function hasUpdated($path, $time) { |
|
| 625 | + if ($this->is_file($path)) { |
|
| 626 | + return parent::hasUpdated($path, $time); |
|
| 627 | + } |
|
| 628 | + $path = $this->normalizePath($path); |
|
| 629 | + $dh = $this->opendir($path); |
|
| 630 | + $content = array(); |
|
| 631 | + while (($file = readdir($dh)) !== false) { |
|
| 632 | + $content[] = $file; |
|
| 633 | + } |
|
| 634 | + if ($path === '.') { |
|
| 635 | + $path = ''; |
|
| 636 | + } |
|
| 637 | + $cachedContent = $this->getCache()->getFolderContents($path); |
|
| 638 | + $cachedNames = array_map(function ($content) { |
|
| 639 | + return $content['name']; |
|
| 640 | + }, $cachedContent); |
|
| 641 | + sort($cachedNames); |
|
| 642 | + sort($content); |
|
| 643 | + return $cachedNames !== $content; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * check if curl is installed |
|
| 648 | + */ |
|
| 649 | + public static function checkDependencies() { |
|
| 650 | + return true; |
|
| 651 | + } |
|
| 652 | 652 | |
| 653 | 653 | } |
@@ -51,475 +51,475 @@ |
||
| 51 | 51 | use OCP\Files\StorageNotAvailableException; |
| 52 | 52 | |
| 53 | 53 | class SMB extends Common implements INotifyStorage { |
| 54 | - /** |
|
| 55 | - * @var \Icewind\SMB\Server |
|
| 56 | - */ |
|
| 57 | - protected $server; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var \Icewind\SMB\Share |
|
| 61 | - */ |
|
| 62 | - protected $share; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - protected $root; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var \Icewind\SMB\FileInfo[] |
|
| 71 | - */ |
|
| 72 | - protected $statCache; |
|
| 73 | - |
|
| 74 | - public function __construct($params) { |
|
| 75 | - if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
| 76 | - if (Server::NativeAvailable()) { |
|
| 77 | - $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
|
| 78 | - } else { |
|
| 79 | - $this->server = new Server($params['host'], $params['user'], $params['password']); |
|
| 80 | - } |
|
| 81 | - $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
| 82 | - |
|
| 83 | - $this->root = isset($params['root']) ? $params['root'] : '/'; |
|
| 84 | - if (!$this->root || $this->root[0] !== '/') { |
|
| 85 | - $this->root = '/' . $this->root; |
|
| 86 | - } |
|
| 87 | - if (substr($this->root, -1, 1) !== '/') { |
|
| 88 | - $this->root .= '/'; |
|
| 89 | - } |
|
| 90 | - } else { |
|
| 91 | - throw new \Exception('Invalid configuration'); |
|
| 92 | - } |
|
| 93 | - $this->statCache = new CappedMemoryCache(); |
|
| 94 | - parent::__construct($params); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function getId() { |
|
| 101 | - // FIXME: double slash to keep compatible with the old storage ids, |
|
| 102 | - // failure to do so will lead to creation of a new storage id and |
|
| 103 | - // loss of shares from the storage |
|
| 104 | - return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $path |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - protected function buildPath($path) { |
|
| 112 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - protected function relativePath($fullPath) { |
|
| 116 | - if ($fullPath === $this->root) { |
|
| 117 | - return ''; |
|
| 118 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 119 | - return substr($fullPath, strlen($this->root)); |
|
| 120 | - } else { |
|
| 121 | - return null; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param string $path |
|
| 127 | - * @return \Icewind\SMB\IFileInfo |
|
| 128 | - * @throws StorageNotAvailableException |
|
| 129 | - */ |
|
| 130 | - protected function getFileInfo($path) { |
|
| 131 | - try { |
|
| 132 | - $path = $this->buildPath($path); |
|
| 133 | - if (!isset($this->statCache[$path])) { |
|
| 134 | - $this->statCache[$path] = $this->share->stat($path); |
|
| 135 | - } |
|
| 136 | - return $this->statCache[$path]; |
|
| 137 | - } catch (ConnectException $e) { |
|
| 138 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param string $path |
|
| 144 | - * @return \Icewind\SMB\IFileInfo[] |
|
| 145 | - * @throws StorageNotAvailableException |
|
| 146 | - */ |
|
| 147 | - protected function getFolderContents($path) { |
|
| 148 | - try { |
|
| 149 | - $path = $this->buildPath($path); |
|
| 150 | - $files = $this->share->dir($path); |
|
| 151 | - foreach ($files as $file) { |
|
| 152 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
| 153 | - } |
|
| 154 | - return array_filter($files, function (IFileInfo $file) { |
|
| 155 | - return !$file->isHidden(); |
|
| 156 | - }); |
|
| 157 | - } catch (ConnectException $e) { |
|
| 158 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param \Icewind\SMB\IFileInfo $info |
|
| 164 | - * @return array |
|
| 165 | - */ |
|
| 166 | - protected function formatInfo($info) { |
|
| 167 | - $result = [ |
|
| 168 | - 'size' => $info->getSize(), |
|
| 169 | - 'mtime' => $info->getMTime(), |
|
| 170 | - ]; |
|
| 171 | - if ($info->isDirectory()) { |
|
| 172 | - $result['type'] = 'dir'; |
|
| 173 | - } else { |
|
| 174 | - $result['type'] = 'file'; |
|
| 175 | - } |
|
| 176 | - return $result; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Rename the files. If the source or the target is the root, the rename won't happen. |
|
| 181 | - * |
|
| 182 | - * @param string $source the old name of the path |
|
| 183 | - * @param string $target the new name of the path |
|
| 184 | - * @return bool true if the rename is successful, false otherwise |
|
| 185 | - */ |
|
| 186 | - public function rename($source, $target) { |
|
| 187 | - if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $absoluteSource = $this->buildPath($source); |
|
| 192 | - $absoluteTarget = $this->buildPath($target); |
|
| 193 | - try { |
|
| 194 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 195 | - } catch (AlreadyExistsException $e) { |
|
| 196 | - $this->remove($target); |
|
| 197 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 198 | - } catch (\Exception $e) { |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
| 202 | - return $result; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param string $path |
|
| 207 | - * @return array |
|
| 208 | - */ |
|
| 209 | - public function stat($path) { |
|
| 210 | - $result = $this->formatInfo($this->getFileInfo($path)); |
|
| 211 | - if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
| 212 | - $result['mtime'] = $this->shareMTime(); |
|
| 213 | - } |
|
| 214 | - return $result; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * get the best guess for the modification time of the share |
|
| 219 | - * |
|
| 220 | - * @return int |
|
| 221 | - */ |
|
| 222 | - private function shareMTime() { |
|
| 223 | - $highestMTime = 0; |
|
| 224 | - $files = $this->share->dir($this->root); |
|
| 225 | - foreach ($files as $fileInfo) { |
|
| 226 | - if ($fileInfo->getMTime() > $highestMTime) { |
|
| 227 | - $highestMTime = $fileInfo->getMTime(); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - return $highestMTime; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Check if the path is our root dir (not the smb one) |
|
| 235 | - * |
|
| 236 | - * @param string $path the path |
|
| 237 | - * @return bool |
|
| 238 | - */ |
|
| 239 | - private function isRootDir($path) { |
|
| 240 | - return $path === '' || $path === '/' || $path === '.'; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Check if our root points to a smb share |
|
| 245 | - * |
|
| 246 | - * @return bool true if our root points to a share false otherwise |
|
| 247 | - */ |
|
| 248 | - private function remoteIsShare() { |
|
| 249 | - return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * @param string $path |
|
| 254 | - * @return bool |
|
| 255 | - */ |
|
| 256 | - public function unlink($path) { |
|
| 257 | - if ($this->isRootDir($path)) { |
|
| 258 | - return false; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - try { |
|
| 262 | - if ($this->is_dir($path)) { |
|
| 263 | - return $this->rmdir($path); |
|
| 264 | - } else { |
|
| 265 | - $path = $this->buildPath($path); |
|
| 266 | - unset($this->statCache[$path]); |
|
| 267 | - $this->share->del($path); |
|
| 268 | - return true; |
|
| 269 | - } |
|
| 270 | - } catch (NotFoundException $e) { |
|
| 271 | - return false; |
|
| 272 | - } catch (ForbiddenException $e) { |
|
| 273 | - return false; |
|
| 274 | - } catch (ConnectException $e) { |
|
| 275 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * check if a file or folder has been updated since $time |
|
| 281 | - * |
|
| 282 | - * @param string $path |
|
| 283 | - * @param int $time |
|
| 284 | - * @return bool |
|
| 285 | - */ |
|
| 286 | - public function hasUpdated($path, $time) { |
|
| 287 | - if (!$path and $this->root === '/') { |
|
| 288 | - // mtime doesn't work for shares, but giving the nature of the backend, |
|
| 289 | - // doing a full update is still just fast enough |
|
| 290 | - return true; |
|
| 291 | - } else { |
|
| 292 | - $actualTime = $this->filemtime($path); |
|
| 293 | - return $actualTime > $time; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @param string $path |
|
| 299 | - * @param string $mode |
|
| 300 | - * @return resource|false |
|
| 301 | - */ |
|
| 302 | - public function fopen($path, $mode) { |
|
| 303 | - $fullPath = $this->buildPath($path); |
|
| 304 | - try { |
|
| 305 | - switch ($mode) { |
|
| 306 | - case 'r': |
|
| 307 | - case 'rb': |
|
| 308 | - if (!$this->file_exists($path)) { |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - return $this->share->read($fullPath); |
|
| 312 | - case 'w': |
|
| 313 | - case 'wb': |
|
| 314 | - $source = $this->share->write($fullPath); |
|
| 315 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
| 316 | - unset($this->statCache[$fullPath]); |
|
| 317 | - }); |
|
| 318 | - case 'a': |
|
| 319 | - case 'ab': |
|
| 320 | - case 'r+': |
|
| 321 | - case 'w+': |
|
| 322 | - case 'wb+': |
|
| 323 | - case 'a+': |
|
| 324 | - case 'x': |
|
| 325 | - case 'x+': |
|
| 326 | - case 'c': |
|
| 327 | - case 'c+': |
|
| 328 | - //emulate these |
|
| 329 | - if (strrpos($path, '.') !== false) { |
|
| 330 | - $ext = substr($path, strrpos($path, '.')); |
|
| 331 | - } else { |
|
| 332 | - $ext = ''; |
|
| 333 | - } |
|
| 334 | - if ($this->file_exists($path)) { |
|
| 335 | - if (!$this->isUpdatable($path)) { |
|
| 336 | - return false; |
|
| 337 | - } |
|
| 338 | - $tmpFile = $this->getCachedFile($path); |
|
| 339 | - } else { |
|
| 340 | - if (!$this->isCreatable(dirname($path))) { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 344 | - } |
|
| 345 | - $source = fopen($tmpFile, $mode); |
|
| 346 | - $share = $this->share; |
|
| 347 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
| 348 | - unset($this->statCache[$fullPath]); |
|
| 349 | - $share->put($tmpFile, $fullPath); |
|
| 350 | - unlink($tmpFile); |
|
| 351 | - }); |
|
| 352 | - } |
|
| 353 | - return false; |
|
| 354 | - } catch (NotFoundException $e) { |
|
| 355 | - return false; |
|
| 356 | - } catch (ForbiddenException $e) { |
|
| 357 | - return false; |
|
| 358 | - } catch (ConnectException $e) { |
|
| 359 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - public function rmdir($path) { |
|
| 364 | - if ($this->isRootDir($path)) { |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - try { |
|
| 369 | - $this->statCache = array(); |
|
| 370 | - $content = $this->share->dir($this->buildPath($path)); |
|
| 371 | - foreach ($content as $file) { |
|
| 372 | - if ($file->isDirectory()) { |
|
| 373 | - $this->rmdir($path . '/' . $file->getName()); |
|
| 374 | - } else { |
|
| 375 | - $this->share->del($file->getPath()); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - $this->share->rmdir($this->buildPath($path)); |
|
| 379 | - return true; |
|
| 380 | - } catch (NotFoundException $e) { |
|
| 381 | - return false; |
|
| 382 | - } catch (ForbiddenException $e) { |
|
| 383 | - return false; |
|
| 384 | - } catch (ConnectException $e) { |
|
| 385 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - public function touch($path, $time = null) { |
|
| 390 | - try { |
|
| 391 | - if (!$this->file_exists($path)) { |
|
| 392 | - $fh = $this->share->write($this->buildPath($path)); |
|
| 393 | - fclose($fh); |
|
| 394 | - return true; |
|
| 395 | - } |
|
| 396 | - return false; |
|
| 397 | - } catch (ConnectException $e) { |
|
| 398 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - public function opendir($path) { |
|
| 403 | - try { |
|
| 404 | - $files = $this->getFolderContents($path); |
|
| 405 | - } catch (NotFoundException $e) { |
|
| 406 | - return false; |
|
| 407 | - } catch (ForbiddenException $e) { |
|
| 408 | - return false; |
|
| 409 | - } |
|
| 410 | - $names = array_map(function ($info) { |
|
| 411 | - /** @var \Icewind\SMB\IFileInfo $info */ |
|
| 412 | - return $info->getName(); |
|
| 413 | - }, $files); |
|
| 414 | - return IteratorDirectory::wrap($names); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - public function filetype($path) { |
|
| 418 | - try { |
|
| 419 | - return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
| 420 | - } catch (NotFoundException $e) { |
|
| 421 | - return false; |
|
| 422 | - } catch (ForbiddenException $e) { |
|
| 423 | - return false; |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - public function mkdir($path) { |
|
| 428 | - $path = $this->buildPath($path); |
|
| 429 | - try { |
|
| 430 | - $this->share->mkdir($path); |
|
| 431 | - return true; |
|
| 432 | - } catch (ConnectException $e) { |
|
| 433 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 434 | - } catch (Exception $e) { |
|
| 435 | - return false; |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - public function file_exists($path) { |
|
| 440 | - try { |
|
| 441 | - $this->getFileInfo($path); |
|
| 442 | - return true; |
|
| 443 | - } catch (NotFoundException $e) { |
|
| 444 | - return false; |
|
| 445 | - } catch (ForbiddenException $e) { |
|
| 446 | - return false; |
|
| 447 | - } catch (ConnectException $e) { |
|
| 448 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - public function isReadable($path) { |
|
| 453 | - try { |
|
| 454 | - $info = $this->getFileInfo($path); |
|
| 455 | - return !$info->isHidden(); |
|
| 456 | - } catch (NotFoundException $e) { |
|
| 457 | - return false; |
|
| 458 | - } catch (ForbiddenException $e) { |
|
| 459 | - return false; |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - public function isUpdatable($path) { |
|
| 464 | - try { |
|
| 465 | - $info = $this->getFileInfo($path); |
|
| 466 | - // following windows behaviour for read-only folders: they can be written into |
|
| 467 | - // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
| 468 | - return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
| 469 | - } catch (NotFoundException $e) { |
|
| 470 | - return false; |
|
| 471 | - } catch (ForbiddenException $e) { |
|
| 472 | - return false; |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - public function isDeletable($path) { |
|
| 477 | - try { |
|
| 478 | - $info = $this->getFileInfo($path); |
|
| 479 | - return !$info->isHidden() && !$info->isReadOnly(); |
|
| 480 | - } catch (NotFoundException $e) { |
|
| 481 | - return false; |
|
| 482 | - } catch (ForbiddenException $e) { |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * check if smbclient is installed |
|
| 489 | - */ |
|
| 490 | - public static function checkDependencies() { |
|
| 491 | - return ( |
|
| 492 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
| 493 | - || Server::NativeAvailable() |
|
| 494 | - ) ? true : ['smbclient']; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Test a storage for availability |
|
| 499 | - * |
|
| 500 | - * @return bool |
|
| 501 | - */ |
|
| 502 | - public function test() { |
|
| 503 | - try { |
|
| 504 | - return parent::test(); |
|
| 505 | - } catch (Exception $e) { |
|
| 506 | - return false; |
|
| 507 | - } |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - public function listen($path, callable $callback) { |
|
| 511 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
| 512 | - if ($change instanceof IRenameChange) { |
|
| 513 | - return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
| 514 | - } else { |
|
| 515 | - return $callback($change->getType(), $change->getPath()); |
|
| 516 | - } |
|
| 517 | - }); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - public function notify($path) { |
|
| 521 | - $path = '/' . ltrim($path, '/'); |
|
| 522 | - $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
| 523 | - return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
| 524 | - } |
|
| 54 | + /** |
|
| 55 | + * @var \Icewind\SMB\Server |
|
| 56 | + */ |
|
| 57 | + protected $server; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var \Icewind\SMB\Share |
|
| 61 | + */ |
|
| 62 | + protected $share; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + protected $root; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var \Icewind\SMB\FileInfo[] |
|
| 71 | + */ |
|
| 72 | + protected $statCache; |
|
| 73 | + |
|
| 74 | + public function __construct($params) { |
|
| 75 | + if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
| 76 | + if (Server::NativeAvailable()) { |
|
| 77 | + $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
|
| 78 | + } else { |
|
| 79 | + $this->server = new Server($params['host'], $params['user'], $params['password']); |
|
| 80 | + } |
|
| 81 | + $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
| 82 | + |
|
| 83 | + $this->root = isset($params['root']) ? $params['root'] : '/'; |
|
| 84 | + if (!$this->root || $this->root[0] !== '/') { |
|
| 85 | + $this->root = '/' . $this->root; |
|
| 86 | + } |
|
| 87 | + if (substr($this->root, -1, 1) !== '/') { |
|
| 88 | + $this->root .= '/'; |
|
| 89 | + } |
|
| 90 | + } else { |
|
| 91 | + throw new \Exception('Invalid configuration'); |
|
| 92 | + } |
|
| 93 | + $this->statCache = new CappedMemoryCache(); |
|
| 94 | + parent::__construct($params); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function getId() { |
|
| 101 | + // FIXME: double slash to keep compatible with the old storage ids, |
|
| 102 | + // failure to do so will lead to creation of a new storage id and |
|
| 103 | + // loss of shares from the storage |
|
| 104 | + return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $path |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + protected function buildPath($path) { |
|
| 112 | + return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + protected function relativePath($fullPath) { |
|
| 116 | + if ($fullPath === $this->root) { |
|
| 117 | + return ''; |
|
| 118 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 119 | + return substr($fullPath, strlen($this->root)); |
|
| 120 | + } else { |
|
| 121 | + return null; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param string $path |
|
| 127 | + * @return \Icewind\SMB\IFileInfo |
|
| 128 | + * @throws StorageNotAvailableException |
|
| 129 | + */ |
|
| 130 | + protected function getFileInfo($path) { |
|
| 131 | + try { |
|
| 132 | + $path = $this->buildPath($path); |
|
| 133 | + if (!isset($this->statCache[$path])) { |
|
| 134 | + $this->statCache[$path] = $this->share->stat($path); |
|
| 135 | + } |
|
| 136 | + return $this->statCache[$path]; |
|
| 137 | + } catch (ConnectException $e) { |
|
| 138 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param string $path |
|
| 144 | + * @return \Icewind\SMB\IFileInfo[] |
|
| 145 | + * @throws StorageNotAvailableException |
|
| 146 | + */ |
|
| 147 | + protected function getFolderContents($path) { |
|
| 148 | + try { |
|
| 149 | + $path = $this->buildPath($path); |
|
| 150 | + $files = $this->share->dir($path); |
|
| 151 | + foreach ($files as $file) { |
|
| 152 | + $this->statCache[$path . '/' . $file->getName()] = $file; |
|
| 153 | + } |
|
| 154 | + return array_filter($files, function (IFileInfo $file) { |
|
| 155 | + return !$file->isHidden(); |
|
| 156 | + }); |
|
| 157 | + } catch (ConnectException $e) { |
|
| 158 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param \Icewind\SMB\IFileInfo $info |
|
| 164 | + * @return array |
|
| 165 | + */ |
|
| 166 | + protected function formatInfo($info) { |
|
| 167 | + $result = [ |
|
| 168 | + 'size' => $info->getSize(), |
|
| 169 | + 'mtime' => $info->getMTime(), |
|
| 170 | + ]; |
|
| 171 | + if ($info->isDirectory()) { |
|
| 172 | + $result['type'] = 'dir'; |
|
| 173 | + } else { |
|
| 174 | + $result['type'] = 'file'; |
|
| 175 | + } |
|
| 176 | + return $result; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Rename the files. If the source or the target is the root, the rename won't happen. |
|
| 181 | + * |
|
| 182 | + * @param string $source the old name of the path |
|
| 183 | + * @param string $target the new name of the path |
|
| 184 | + * @return bool true if the rename is successful, false otherwise |
|
| 185 | + */ |
|
| 186 | + public function rename($source, $target) { |
|
| 187 | + if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $absoluteSource = $this->buildPath($source); |
|
| 192 | + $absoluteTarget = $this->buildPath($target); |
|
| 193 | + try { |
|
| 194 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 195 | + } catch (AlreadyExistsException $e) { |
|
| 196 | + $this->remove($target); |
|
| 197 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
| 198 | + } catch (\Exception $e) { |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
| 202 | + return $result; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param string $path |
|
| 207 | + * @return array |
|
| 208 | + */ |
|
| 209 | + public function stat($path) { |
|
| 210 | + $result = $this->formatInfo($this->getFileInfo($path)); |
|
| 211 | + if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
| 212 | + $result['mtime'] = $this->shareMTime(); |
|
| 213 | + } |
|
| 214 | + return $result; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * get the best guess for the modification time of the share |
|
| 219 | + * |
|
| 220 | + * @return int |
|
| 221 | + */ |
|
| 222 | + private function shareMTime() { |
|
| 223 | + $highestMTime = 0; |
|
| 224 | + $files = $this->share->dir($this->root); |
|
| 225 | + foreach ($files as $fileInfo) { |
|
| 226 | + if ($fileInfo->getMTime() > $highestMTime) { |
|
| 227 | + $highestMTime = $fileInfo->getMTime(); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + return $highestMTime; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Check if the path is our root dir (not the smb one) |
|
| 235 | + * |
|
| 236 | + * @param string $path the path |
|
| 237 | + * @return bool |
|
| 238 | + */ |
|
| 239 | + private function isRootDir($path) { |
|
| 240 | + return $path === '' || $path === '/' || $path === '.'; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Check if our root points to a smb share |
|
| 245 | + * |
|
| 246 | + * @return bool true if our root points to a share false otherwise |
|
| 247 | + */ |
|
| 248 | + private function remoteIsShare() { |
|
| 249 | + return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * @param string $path |
|
| 254 | + * @return bool |
|
| 255 | + */ |
|
| 256 | + public function unlink($path) { |
|
| 257 | + if ($this->isRootDir($path)) { |
|
| 258 | + return false; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + try { |
|
| 262 | + if ($this->is_dir($path)) { |
|
| 263 | + return $this->rmdir($path); |
|
| 264 | + } else { |
|
| 265 | + $path = $this->buildPath($path); |
|
| 266 | + unset($this->statCache[$path]); |
|
| 267 | + $this->share->del($path); |
|
| 268 | + return true; |
|
| 269 | + } |
|
| 270 | + } catch (NotFoundException $e) { |
|
| 271 | + return false; |
|
| 272 | + } catch (ForbiddenException $e) { |
|
| 273 | + return false; |
|
| 274 | + } catch (ConnectException $e) { |
|
| 275 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * check if a file or folder has been updated since $time |
|
| 281 | + * |
|
| 282 | + * @param string $path |
|
| 283 | + * @param int $time |
|
| 284 | + * @return bool |
|
| 285 | + */ |
|
| 286 | + public function hasUpdated($path, $time) { |
|
| 287 | + if (!$path and $this->root === '/') { |
|
| 288 | + // mtime doesn't work for shares, but giving the nature of the backend, |
|
| 289 | + // doing a full update is still just fast enough |
|
| 290 | + return true; |
|
| 291 | + } else { |
|
| 292 | + $actualTime = $this->filemtime($path); |
|
| 293 | + return $actualTime > $time; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @param string $path |
|
| 299 | + * @param string $mode |
|
| 300 | + * @return resource|false |
|
| 301 | + */ |
|
| 302 | + public function fopen($path, $mode) { |
|
| 303 | + $fullPath = $this->buildPath($path); |
|
| 304 | + try { |
|
| 305 | + switch ($mode) { |
|
| 306 | + case 'r': |
|
| 307 | + case 'rb': |
|
| 308 | + if (!$this->file_exists($path)) { |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + return $this->share->read($fullPath); |
|
| 312 | + case 'w': |
|
| 313 | + case 'wb': |
|
| 314 | + $source = $this->share->write($fullPath); |
|
| 315 | + return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
| 316 | + unset($this->statCache[$fullPath]); |
|
| 317 | + }); |
|
| 318 | + case 'a': |
|
| 319 | + case 'ab': |
|
| 320 | + case 'r+': |
|
| 321 | + case 'w+': |
|
| 322 | + case 'wb+': |
|
| 323 | + case 'a+': |
|
| 324 | + case 'x': |
|
| 325 | + case 'x+': |
|
| 326 | + case 'c': |
|
| 327 | + case 'c+': |
|
| 328 | + //emulate these |
|
| 329 | + if (strrpos($path, '.') !== false) { |
|
| 330 | + $ext = substr($path, strrpos($path, '.')); |
|
| 331 | + } else { |
|
| 332 | + $ext = ''; |
|
| 333 | + } |
|
| 334 | + if ($this->file_exists($path)) { |
|
| 335 | + if (!$this->isUpdatable($path)) { |
|
| 336 | + return false; |
|
| 337 | + } |
|
| 338 | + $tmpFile = $this->getCachedFile($path); |
|
| 339 | + } else { |
|
| 340 | + if (!$this->isCreatable(dirname($path))) { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 344 | + } |
|
| 345 | + $source = fopen($tmpFile, $mode); |
|
| 346 | + $share = $this->share; |
|
| 347 | + return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
| 348 | + unset($this->statCache[$fullPath]); |
|
| 349 | + $share->put($tmpFile, $fullPath); |
|
| 350 | + unlink($tmpFile); |
|
| 351 | + }); |
|
| 352 | + } |
|
| 353 | + return false; |
|
| 354 | + } catch (NotFoundException $e) { |
|
| 355 | + return false; |
|
| 356 | + } catch (ForbiddenException $e) { |
|
| 357 | + return false; |
|
| 358 | + } catch (ConnectException $e) { |
|
| 359 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + public function rmdir($path) { |
|
| 364 | + if ($this->isRootDir($path)) { |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + try { |
|
| 369 | + $this->statCache = array(); |
|
| 370 | + $content = $this->share->dir($this->buildPath($path)); |
|
| 371 | + foreach ($content as $file) { |
|
| 372 | + if ($file->isDirectory()) { |
|
| 373 | + $this->rmdir($path . '/' . $file->getName()); |
|
| 374 | + } else { |
|
| 375 | + $this->share->del($file->getPath()); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + $this->share->rmdir($this->buildPath($path)); |
|
| 379 | + return true; |
|
| 380 | + } catch (NotFoundException $e) { |
|
| 381 | + return false; |
|
| 382 | + } catch (ForbiddenException $e) { |
|
| 383 | + return false; |
|
| 384 | + } catch (ConnectException $e) { |
|
| 385 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + public function touch($path, $time = null) { |
|
| 390 | + try { |
|
| 391 | + if (!$this->file_exists($path)) { |
|
| 392 | + $fh = $this->share->write($this->buildPath($path)); |
|
| 393 | + fclose($fh); |
|
| 394 | + return true; |
|
| 395 | + } |
|
| 396 | + return false; |
|
| 397 | + } catch (ConnectException $e) { |
|
| 398 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + public function opendir($path) { |
|
| 403 | + try { |
|
| 404 | + $files = $this->getFolderContents($path); |
|
| 405 | + } catch (NotFoundException $e) { |
|
| 406 | + return false; |
|
| 407 | + } catch (ForbiddenException $e) { |
|
| 408 | + return false; |
|
| 409 | + } |
|
| 410 | + $names = array_map(function ($info) { |
|
| 411 | + /** @var \Icewind\SMB\IFileInfo $info */ |
|
| 412 | + return $info->getName(); |
|
| 413 | + }, $files); |
|
| 414 | + return IteratorDirectory::wrap($names); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + public function filetype($path) { |
|
| 418 | + try { |
|
| 419 | + return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
| 420 | + } catch (NotFoundException $e) { |
|
| 421 | + return false; |
|
| 422 | + } catch (ForbiddenException $e) { |
|
| 423 | + return false; |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + public function mkdir($path) { |
|
| 428 | + $path = $this->buildPath($path); |
|
| 429 | + try { |
|
| 430 | + $this->share->mkdir($path); |
|
| 431 | + return true; |
|
| 432 | + } catch (ConnectException $e) { |
|
| 433 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 434 | + } catch (Exception $e) { |
|
| 435 | + return false; |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + public function file_exists($path) { |
|
| 440 | + try { |
|
| 441 | + $this->getFileInfo($path); |
|
| 442 | + return true; |
|
| 443 | + } catch (NotFoundException $e) { |
|
| 444 | + return false; |
|
| 445 | + } catch (ForbiddenException $e) { |
|
| 446 | + return false; |
|
| 447 | + } catch (ConnectException $e) { |
|
| 448 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + public function isReadable($path) { |
|
| 453 | + try { |
|
| 454 | + $info = $this->getFileInfo($path); |
|
| 455 | + return !$info->isHidden(); |
|
| 456 | + } catch (NotFoundException $e) { |
|
| 457 | + return false; |
|
| 458 | + } catch (ForbiddenException $e) { |
|
| 459 | + return false; |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + public function isUpdatable($path) { |
|
| 464 | + try { |
|
| 465 | + $info = $this->getFileInfo($path); |
|
| 466 | + // following windows behaviour for read-only folders: they can be written into |
|
| 467 | + // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
| 468 | + return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
| 469 | + } catch (NotFoundException $e) { |
|
| 470 | + return false; |
|
| 471 | + } catch (ForbiddenException $e) { |
|
| 472 | + return false; |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + public function isDeletable($path) { |
|
| 477 | + try { |
|
| 478 | + $info = $this->getFileInfo($path); |
|
| 479 | + return !$info->isHidden() && !$info->isReadOnly(); |
|
| 480 | + } catch (NotFoundException $e) { |
|
| 481 | + return false; |
|
| 482 | + } catch (ForbiddenException $e) { |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * check if smbclient is installed |
|
| 489 | + */ |
|
| 490 | + public static function checkDependencies() { |
|
| 491 | + return ( |
|
| 492 | + (bool)\OC_Helper::findBinaryPath('smbclient') |
|
| 493 | + || Server::NativeAvailable() |
|
| 494 | + ) ? true : ['smbclient']; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Test a storage for availability |
|
| 499 | + * |
|
| 500 | + * @return bool |
|
| 501 | + */ |
|
| 502 | + public function test() { |
|
| 503 | + try { |
|
| 504 | + return parent::test(); |
|
| 505 | + } catch (Exception $e) { |
|
| 506 | + return false; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + public function listen($path, callable $callback) { |
|
| 511 | + $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
| 512 | + if ($change instanceof IRenameChange) { |
|
| 513 | + return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
| 514 | + } else { |
|
| 515 | + return $callback($change->getType(), $change->getPath()); |
|
| 516 | + } |
|
| 517 | + }); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + public function notify($path) { |
|
| 521 | + $path = '/' . ltrim($path, '/'); |
|
| 522 | + $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
| 523 | + return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
| 524 | + } |
|
| 525 | 525 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | $this->root = isset($params['root']) ? $params['root'] : '/'; |
| 84 | 84 | if (!$this->root || $this->root[0] !== '/') { |
| 85 | - $this->root = '/' . $this->root; |
|
| 85 | + $this->root = '/'.$this->root; |
|
| 86 | 86 | } |
| 87 | 87 | if (substr($this->root, -1, 1) !== '/') { |
| 88 | 88 | $this->root .= '/'; |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | // FIXME: double slash to keep compatible with the old storage ids, |
| 102 | 102 | // failure to do so will lead to creation of a new storage id and |
| 103 | 103 | // loss of shares from the storage |
| 104 | - return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
| 104 | + return 'smb::'.$this->server->getUser().'@'.$this->server->getHost().'//'.$this->share->getName().'/'.$this->root; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | * @return string |
| 110 | 110 | */ |
| 111 | 111 | protected function buildPath($path) { |
| 112 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
| 112 | + return Filesystem::normalizePath($this->root.'/'.$path, true, false, true); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | protected function relativePath($fullPath) { |
@@ -149,9 +149,9 @@ discard block |
||
| 149 | 149 | $path = $this->buildPath($path); |
| 150 | 150 | $files = $this->share->dir($path); |
| 151 | 151 | foreach ($files as $file) { |
| 152 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
| 152 | + $this->statCache[$path.'/'.$file->getName()] = $file; |
|
| 153 | 153 | } |
| 154 | - return array_filter($files, function (IFileInfo $file) { |
|
| 154 | + return array_filter($files, function(IFileInfo $file) { |
|
| 155 | 155 | return !$file->isHidden(); |
| 156 | 156 | }); |
| 157 | 157 | } catch (ConnectException $e) { |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | case 'w': |
| 313 | 313 | case 'wb': |
| 314 | 314 | $source = $this->share->write($fullPath); |
| 315 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
| 315 | + return CallBackWrapper::wrap($source, null, null, function() use ($fullPath) { |
|
| 316 | 316 | unset($this->statCache[$fullPath]); |
| 317 | 317 | }); |
| 318 | 318 | case 'a': |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | } |
| 345 | 345 | $source = fopen($tmpFile, $mode); |
| 346 | 346 | $share = $this->share; |
| 347 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
| 347 | + return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath, $share) { |
|
| 348 | 348 | unset($this->statCache[$fullPath]); |
| 349 | 349 | $share->put($tmpFile, $fullPath); |
| 350 | 350 | unlink($tmpFile); |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | $content = $this->share->dir($this->buildPath($path)); |
| 371 | 371 | foreach ($content as $file) { |
| 372 | 372 | if ($file->isDirectory()) { |
| 373 | - $this->rmdir($path . '/' . $file->getName()); |
|
| 373 | + $this->rmdir($path.'/'.$file->getName()); |
|
| 374 | 374 | } else { |
| 375 | 375 | $this->share->del($file->getPath()); |
| 376 | 376 | } |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | } catch (ForbiddenException $e) { |
| 408 | 408 | return false; |
| 409 | 409 | } |
| 410 | - $names = array_map(function ($info) { |
|
| 410 | + $names = array_map(function($info) { |
|
| 411 | 411 | /** @var \Icewind\SMB\IFileInfo $info */ |
| 412 | 412 | return $info->getName(); |
| 413 | 413 | }, $files); |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | */ |
| 490 | 490 | public static function checkDependencies() { |
| 491 | 491 | return ( |
| 492 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
| 492 | + (bool) \OC_Helper::findBinaryPath('smbclient') |
|
| 493 | 493 | || Server::NativeAvailable() |
| 494 | 494 | ) ? true : ['smbclient']; |
| 495 | 495 | } |
@@ -508,7 +508,7 @@ discard block |
||
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | public function listen($path, callable $callback) { |
| 511 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
| 511 | + $this->notify($path)->listen(function(IChange $change) use ($callback) { |
|
| 512 | 512 | if ($change instanceof IRenameChange) { |
| 513 | 513 | return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
| 514 | 514 | } else { |
@@ -518,7 +518,7 @@ discard block |
||
| 518 | 518 | } |
| 519 | 519 | |
| 520 | 520 | public function notify($path) { |
| 521 | - $path = '/' . ltrim($path, '/'); |
|
| 521 | + $path = '/'.ltrim($path, '/'); |
|
| 522 | 522 | $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
| 523 | 523 | return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
| 524 | 524 | } |
@@ -37,122 +37,122 @@ |
||
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | 39 | class FTP extends StreamWrapper{ |
| 40 | - private $password; |
|
| 41 | - private $user; |
|
| 42 | - private $host; |
|
| 43 | - private $secure; |
|
| 44 | - private $root; |
|
| 40 | + private $password; |
|
| 41 | + private $user; |
|
| 42 | + private $host; |
|
| 43 | + private $secure; |
|
| 44 | + private $root; |
|
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles=array(); |
|
| 47 | 47 | |
| 48 | - public function __construct($params) { |
|
| 49 | - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 50 | - $this->host=$params['host']; |
|
| 51 | - $this->user=$params['user']; |
|
| 52 | - $this->password=$params['password']; |
|
| 53 | - if (isset($params['secure'])) { |
|
| 54 | - $this->secure = $params['secure']; |
|
| 55 | - } else { |
|
| 56 | - $this->secure = false; |
|
| 57 | - } |
|
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 61 | - } |
|
| 62 | - if (substr($this->root, -1) !== '/') { |
|
| 63 | - $this->root .= '/'; |
|
| 64 | - } |
|
| 65 | - } else { |
|
| 66 | - throw new \Exception('Creating FTP storage failed'); |
|
| 67 | - } |
|
| 48 | + public function __construct($params) { |
|
| 49 | + if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 50 | + $this->host=$params['host']; |
|
| 51 | + $this->user=$params['user']; |
|
| 52 | + $this->password=$params['password']; |
|
| 53 | + if (isset($params['secure'])) { |
|
| 54 | + $this->secure = $params['secure']; |
|
| 55 | + } else { |
|
| 56 | + $this->secure = false; |
|
| 57 | + } |
|
| 58 | + $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | + if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | + $this->root='/'.$this->root; |
|
| 61 | + } |
|
| 62 | + if (substr($this->root, -1) !== '/') { |
|
| 63 | + $this->root .= '/'; |
|
| 64 | + } |
|
| 65 | + } else { |
|
| 66 | + throw new \Exception('Creating FTP storage failed'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | - } |
|
| 71 | + public function getId(){ |
|
| 72 | + return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * construct the ftp url |
|
| 77 | - * @param string $path |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public function constructUrl($path) { |
|
| 81 | - $url='ftp'; |
|
| 82 | - if ($this->secure) { |
|
| 83 | - $url.='s'; |
|
| 84 | - } |
|
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | - return $url; |
|
| 87 | - } |
|
| 75 | + /** |
|
| 76 | + * construct the ftp url |
|
| 77 | + * @param string $path |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public function constructUrl($path) { |
|
| 81 | + $url='ftp'; |
|
| 82 | + if ($this->secure) { |
|
| 83 | + $url.='s'; |
|
| 84 | + } |
|
| 85 | + $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | + return $url; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Unlinks file or directory |
|
| 91 | - * @param string $path |
|
| 92 | - */ |
|
| 93 | - public function unlink($path) { |
|
| 94 | - if ($this->is_dir($path)) { |
|
| 95 | - return $this->rmdir($path); |
|
| 96 | - } |
|
| 97 | - else { |
|
| 98 | - $url = $this->constructUrl($path); |
|
| 99 | - $result = unlink($url); |
|
| 100 | - clearstatcache(true, $url); |
|
| 101 | - return $result; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 106 | - case 'r': |
|
| 107 | - case 'rb': |
|
| 108 | - case 'w': |
|
| 109 | - case 'wb': |
|
| 110 | - case 'a': |
|
| 111 | - case 'ab': |
|
| 112 | - //these are supported by the wrapper |
|
| 113 | - $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | - return RetryWrapper::wrap($handle); |
|
| 116 | - case 'r+': |
|
| 117 | - case 'w+': |
|
| 118 | - case 'wb+': |
|
| 119 | - case 'a+': |
|
| 120 | - case 'x': |
|
| 121 | - case 'x+': |
|
| 122 | - case 'c': |
|
| 123 | - case 'c+': |
|
| 124 | - //emulate these |
|
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 127 | - } else { |
|
| 128 | - $ext=''; |
|
| 129 | - } |
|
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | - if ($this->file_exists($path)) { |
|
| 132 | - $this->getFile($path, $tmpFile); |
|
| 133 | - } |
|
| 134 | - $handle = fopen($tmpFile, $mode); |
|
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | - $this->writeBack($tmpFile, $path); |
|
| 137 | - }); |
|
| 138 | - } |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 89 | + /** |
|
| 90 | + * Unlinks file or directory |
|
| 91 | + * @param string $path |
|
| 92 | + */ |
|
| 93 | + public function unlink($path) { |
|
| 94 | + if ($this->is_dir($path)) { |
|
| 95 | + return $this->rmdir($path); |
|
| 96 | + } |
|
| 97 | + else { |
|
| 98 | + $url = $this->constructUrl($path); |
|
| 99 | + $result = unlink($url); |
|
| 100 | + clearstatcache(true, $url); |
|
| 101 | + return $result; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + public function fopen($path,$mode) { |
|
| 105 | + switch($mode) { |
|
| 106 | + case 'r': |
|
| 107 | + case 'rb': |
|
| 108 | + case 'w': |
|
| 109 | + case 'wb': |
|
| 110 | + case 'a': |
|
| 111 | + case 'ab': |
|
| 112 | + //these are supported by the wrapper |
|
| 113 | + $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | + return RetryWrapper::wrap($handle); |
|
| 116 | + case 'r+': |
|
| 117 | + case 'w+': |
|
| 118 | + case 'wb+': |
|
| 119 | + case 'a+': |
|
| 120 | + case 'x': |
|
| 121 | + case 'x+': |
|
| 122 | + case 'c': |
|
| 123 | + case 'c+': |
|
| 124 | + //emulate these |
|
| 125 | + if (strrpos($path, '.')!==false) { |
|
| 126 | + $ext=substr($path, strrpos($path, '.')); |
|
| 127 | + } else { |
|
| 128 | + $ext=''; |
|
| 129 | + } |
|
| 130 | + $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | + if ($this->file_exists($path)) { |
|
| 132 | + $this->getFile($path, $tmpFile); |
|
| 133 | + } |
|
| 134 | + $handle = fopen($tmpFile, $mode); |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | + $this->writeBack($tmpFile, $path); |
|
| 137 | + }); |
|
| 138 | + } |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - public function writeBack($tmpFile, $path) { |
|
| 143 | - $this->uploadFile($tmpFile, $path); |
|
| 144 | - unlink($tmpFile); |
|
| 145 | - } |
|
| 142 | + public function writeBack($tmpFile, $path) { |
|
| 143 | + $this->uploadFile($tmpFile, $path); |
|
| 144 | + unlink($tmpFile); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * check if php-ftp is installed |
|
| 149 | - */ |
|
| 150 | - public static function checkDependencies() { |
|
| 151 | - if (function_exists('ftp_login')) { |
|
| 152 | - return(true); |
|
| 153 | - } else { |
|
| 154 | - return array('ftp'); |
|
| 155 | - } |
|
| 156 | - } |
|
| 147 | + /** |
|
| 148 | + * check if php-ftp is installed |
|
| 149 | + */ |
|
| 150 | + public static function checkDependencies() { |
|
| 151 | + if (function_exists('ftp_login')) { |
|
| 152 | + return(true); |
|
| 153 | + } else { |
|
| 154 | + return array('ftp'); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | } |
@@ -36,28 +36,28 @@ discard block |
||
| 36 | 36 | use Icewind\Streams\CallbackWrapper; |
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | -class FTP extends StreamWrapper{ |
|
| 39 | +class FTP extends StreamWrapper { |
|
| 40 | 40 | private $password; |
| 41 | 41 | private $user; |
| 42 | 42 | private $host; |
| 43 | 43 | private $secure; |
| 44 | 44 | private $root; |
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles = array(); |
|
| 47 | 47 | |
| 48 | 48 | public function __construct($params) { |
| 49 | 49 | if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
| 50 | - $this->host=$params['host']; |
|
| 51 | - $this->user=$params['user']; |
|
| 52 | - $this->password=$params['password']; |
|
| 50 | + $this->host = $params['host']; |
|
| 51 | + $this->user = $params['user']; |
|
| 52 | + $this->password = $params['password']; |
|
| 53 | 53 | if (isset($params['secure'])) { |
| 54 | 54 | $this->secure = $params['secure']; |
| 55 | 55 | } else { |
| 56 | 56 | $this->secure = false; |
| 57 | 57 | } |
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 58 | + $this->root = isset($params['root']) ? $params['root'] : '/'; |
|
| 59 | + if (!$this->root || $this->root[0] !== '/') { |
|
| 60 | + $this->root = '/'.$this->root; |
|
| 61 | 61 | } |
| 62 | 62 | if (substr($this->root, -1) !== '/') { |
| 63 | 63 | $this->root .= '/'; |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 71 | + public function getId() { |
|
| 72 | + return 'ftp::'.$this->user.'@'.$this->host.'/'.$this->root; |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | * @return string |
| 79 | 79 | */ |
| 80 | 80 | public function constructUrl($path) { |
| 81 | - $url='ftp'; |
|
| 81 | + $url = 'ftp'; |
|
| 82 | 82 | if ($this->secure) { |
| 83 | - $url.='s'; |
|
| 83 | + $url .= 's'; |
|
| 84 | 84 | } |
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 85 | + $url .= '://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | 86 | return $url; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | return $result; |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 104 | + public function fopen($path, $mode) { |
|
| 105 | + switch ($mode) { |
|
| 106 | 106 | case 'r': |
| 107 | 107 | case 'rb': |
| 108 | 108 | case 'w': |
@@ -122,17 +122,17 @@ discard block |
||
| 122 | 122 | case 'c': |
| 123 | 123 | case 'c+': |
| 124 | 124 | //emulate these |
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 125 | + if (strrpos($path, '.') !== false) { |
|
| 126 | + $ext = substr($path, strrpos($path, '.')); |
|
| 127 | 127 | } else { |
| 128 | - $ext=''; |
|
| 128 | + $ext = ''; |
|
| 129 | 129 | } |
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 130 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 131 | 131 | if ($this->file_exists($path)) { |
| 132 | 132 | $this->getFile($path, $tmpFile); |
| 133 | 133 | } |
| 134 | 134 | $handle = fopen($tmpFile, $mode); |
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 136 | 136 | $this->writeBack($tmpFile, $path); |
| 137 | 137 | }); |
| 138 | 138 | } |
@@ -40,317 +40,317 @@ |
||
| 40 | 40 | |
| 41 | 41 | class Dropbox extends \OC\Files\Storage\Common { |
| 42 | 42 | |
| 43 | - private $dropbox; |
|
| 44 | - private $root; |
|
| 45 | - private $id; |
|
| 46 | - private $metaData = array(); |
|
| 47 | - private $oauth; |
|
| 43 | + private $dropbox; |
|
| 44 | + private $root; |
|
| 45 | + private $id; |
|
| 46 | + private $metaData = array(); |
|
| 47 | + private $oauth; |
|
| 48 | 48 | |
| 49 | - public function __construct($params) { |
|
| 50 | - if (isset($params['configured']) && $params['configured'] === 'true' |
|
| 51 | - && isset($params['app_key']) |
|
| 52 | - && isset($params['app_secret']) |
|
| 53 | - && isset($params['token']) |
|
| 54 | - && isset($params['token_secret']) |
|
| 55 | - ) { |
|
| 56 | - $this->root = isset($params['root']) ? $params['root'] : ''; |
|
| 57 | - $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
| 58 | - $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
|
| 59 | - $this->oauth->setToken($params['token'], $params['token_secret']); |
|
| 60 | - // note: Dropbox_API connection is lazy |
|
| 61 | - $this->dropbox = new \Dropbox_API($this->oauth, 'auto'); |
|
| 62 | - } else { |
|
| 63 | - throw new \Exception('Creating Dropbox storage failed'); |
|
| 64 | - } |
|
| 65 | - } |
|
| 49 | + public function __construct($params) { |
|
| 50 | + if (isset($params['configured']) && $params['configured'] === 'true' |
|
| 51 | + && isset($params['app_key']) |
|
| 52 | + && isset($params['app_secret']) |
|
| 53 | + && isset($params['token']) |
|
| 54 | + && isset($params['token_secret']) |
|
| 55 | + ) { |
|
| 56 | + $this->root = isset($params['root']) ? $params['root'] : ''; |
|
| 57 | + $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
| 58 | + $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
|
| 59 | + $this->oauth->setToken($params['token'], $params['token_secret']); |
|
| 60 | + // note: Dropbox_API connection is lazy |
|
| 61 | + $this->dropbox = new \Dropbox_API($this->oauth, 'auto'); |
|
| 62 | + } else { |
|
| 63 | + throw new \Exception('Creating Dropbox storage failed'); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @param string $path |
|
| 69 | - */ |
|
| 70 | - private function deleteMetaData($path) { |
|
| 71 | - $path = ltrim($this->root.$path, '/'); |
|
| 72 | - if (isset($this->metaData[$path])) { |
|
| 73 | - unset($this->metaData[$path]); |
|
| 74 | - return true; |
|
| 75 | - } |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 67 | + /** |
|
| 68 | + * @param string $path |
|
| 69 | + */ |
|
| 70 | + private function deleteMetaData($path) { |
|
| 71 | + $path = ltrim($this->root.$path, '/'); |
|
| 72 | + if (isset($this->metaData[$path])) { |
|
| 73 | + unset($this->metaData[$path]); |
|
| 74 | + return true; |
|
| 75 | + } |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - private function setMetaData($path, $metaData) { |
|
| 80 | - $this->metaData[ltrim($path, '/')] = $metaData; |
|
| 81 | - } |
|
| 79 | + private function setMetaData($path, $metaData) { |
|
| 80 | + $this->metaData[ltrim($path, '/')] = $metaData; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Returns the path's metadata |
|
| 85 | - * @param string $path path for which to return the metadata |
|
| 86 | - * @param bool $list if true, also return the directory's contents |
|
| 87 | - * @return mixed directory contents if $list is true, file metadata if $list is |
|
| 88 | - * false, null if the file doesn't exist or "false" if the operation failed |
|
| 89 | - */ |
|
| 90 | - private function getDropBoxMetaData($path, $list = false) { |
|
| 91 | - $path = ltrim($this->root.$path, '/'); |
|
| 92 | - if ( ! $list && isset($this->metaData[$path])) { |
|
| 93 | - return $this->metaData[$path]; |
|
| 94 | - } else { |
|
| 95 | - if ($list) { |
|
| 96 | - try { |
|
| 97 | - $response = $this->dropbox->getMetaData($path); |
|
| 98 | - } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 99 | - throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 100 | - } catch (\Exception $exception) { |
|
| 101 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - $contents = array(); |
|
| 105 | - if ($response && isset($response['contents'])) { |
|
| 106 | - // Cache folder's contents |
|
| 107 | - foreach ($response['contents'] as $file) { |
|
| 108 | - if (!isset($file['is_deleted']) || !$file['is_deleted']) { |
|
| 109 | - $this->setMetaData($path.'/'.basename($file['path']), $file); |
|
| 110 | - $contents[] = $file; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - unset($response['contents']); |
|
| 114 | - } |
|
| 115 | - if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 116 | - $this->setMetaData($path, $response); |
|
| 117 | - } |
|
| 118 | - // Return contents of folder only |
|
| 119 | - return $contents; |
|
| 120 | - } else { |
|
| 121 | - try { |
|
| 122 | - $requestPath = $path; |
|
| 123 | - if ($path === '.') { |
|
| 124 | - $requestPath = ''; |
|
| 125 | - } |
|
| 83 | + /** |
|
| 84 | + * Returns the path's metadata |
|
| 85 | + * @param string $path path for which to return the metadata |
|
| 86 | + * @param bool $list if true, also return the directory's contents |
|
| 87 | + * @return mixed directory contents if $list is true, file metadata if $list is |
|
| 88 | + * false, null if the file doesn't exist or "false" if the operation failed |
|
| 89 | + */ |
|
| 90 | + private function getDropBoxMetaData($path, $list = false) { |
|
| 91 | + $path = ltrim($this->root.$path, '/'); |
|
| 92 | + if ( ! $list && isset($this->metaData[$path])) { |
|
| 93 | + return $this->metaData[$path]; |
|
| 94 | + } else { |
|
| 95 | + if ($list) { |
|
| 96 | + try { |
|
| 97 | + $response = $this->dropbox->getMetaData($path); |
|
| 98 | + } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 99 | + throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 100 | + } catch (\Exception $exception) { |
|
| 101 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + $contents = array(); |
|
| 105 | + if ($response && isset($response['contents'])) { |
|
| 106 | + // Cache folder's contents |
|
| 107 | + foreach ($response['contents'] as $file) { |
|
| 108 | + if (!isset($file['is_deleted']) || !$file['is_deleted']) { |
|
| 109 | + $this->setMetaData($path.'/'.basename($file['path']), $file); |
|
| 110 | + $contents[] = $file; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + unset($response['contents']); |
|
| 114 | + } |
|
| 115 | + if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 116 | + $this->setMetaData($path, $response); |
|
| 117 | + } |
|
| 118 | + // Return contents of folder only |
|
| 119 | + return $contents; |
|
| 120 | + } else { |
|
| 121 | + try { |
|
| 122 | + $requestPath = $path; |
|
| 123 | + if ($path === '.') { |
|
| 124 | + $requestPath = ''; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - $response = $this->dropbox->getMetaData($requestPath, 'false'); |
|
| 128 | - if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 129 | - $this->setMetaData($path, $response); |
|
| 130 | - return $response; |
|
| 131 | - } |
|
| 132 | - return null; |
|
| 133 | - } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 134 | - throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 135 | - } catch (\Exception $exception) { |
|
| 136 | - if ($exception instanceof \Dropbox_Exception_NotFound) { |
|
| 137 | - // don't log, might be a file_exist check |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 127 | + $response = $this->dropbox->getMetaData($requestPath, 'false'); |
|
| 128 | + if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 129 | + $this->setMetaData($path, $response); |
|
| 130 | + return $response; |
|
| 131 | + } |
|
| 132 | + return null; |
|
| 133 | + } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 134 | + throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 135 | + } catch (\Exception $exception) { |
|
| 136 | + if ($exception instanceof \Dropbox_Exception_NotFound) { |
|
| 137 | + // don't log, might be a file_exist check |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - public function getId(){ |
|
| 148 | - return $this->id; |
|
| 149 | - } |
|
| 147 | + public function getId(){ |
|
| 148 | + return $this->id; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - public function mkdir($path) { |
|
| 152 | - $path = $this->root.$path; |
|
| 153 | - try { |
|
| 154 | - $this->dropbox->createFolder($path); |
|
| 155 | - return true; |
|
| 156 | - } catch (\Exception $exception) { |
|
| 157 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - } |
|
| 151 | + public function mkdir($path) { |
|
| 152 | + $path = $this->root.$path; |
|
| 153 | + try { |
|
| 154 | + $this->dropbox->createFolder($path); |
|
| 155 | + return true; |
|
| 156 | + } catch (\Exception $exception) { |
|
| 157 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - public function rmdir($path) { |
|
| 163 | - return $this->unlink($path); |
|
| 164 | - } |
|
| 162 | + public function rmdir($path) { |
|
| 163 | + return $this->unlink($path); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - public function opendir($path) { |
|
| 167 | - $contents = $this->getDropBoxMetaData($path, true); |
|
| 168 | - if ($contents !== false) { |
|
| 169 | - $files = array(); |
|
| 170 | - foreach ($contents as $file) { |
|
| 171 | - $files[] = basename($file['path']); |
|
| 172 | - } |
|
| 173 | - return IteratorDirectory::wrap($files); |
|
| 174 | - } |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 166 | + public function opendir($path) { |
|
| 167 | + $contents = $this->getDropBoxMetaData($path, true); |
|
| 168 | + if ($contents !== false) { |
|
| 169 | + $files = array(); |
|
| 170 | + foreach ($contents as $file) { |
|
| 171 | + $files[] = basename($file['path']); |
|
| 172 | + } |
|
| 173 | + return IteratorDirectory::wrap($files); |
|
| 174 | + } |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - public function stat($path) { |
|
| 179 | - $metaData = $this->getDropBoxMetaData($path); |
|
| 180 | - if ($metaData) { |
|
| 181 | - $stat['size'] = $metaData['bytes']; |
|
| 182 | - $stat['atime'] = time(); |
|
| 183 | - $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time(); |
|
| 184 | - return $stat; |
|
| 185 | - } |
|
| 186 | - return false; |
|
| 187 | - } |
|
| 178 | + public function stat($path) { |
|
| 179 | + $metaData = $this->getDropBoxMetaData($path); |
|
| 180 | + if ($metaData) { |
|
| 181 | + $stat['size'] = $metaData['bytes']; |
|
| 182 | + $stat['atime'] = time(); |
|
| 183 | + $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time(); |
|
| 184 | + return $stat; |
|
| 185 | + } |
|
| 186 | + return false; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - public function filetype($path) { |
|
| 190 | - if ($path === '' || $path === '/') { |
|
| 191 | - return 'dir'; |
|
| 192 | - } else { |
|
| 193 | - $metaData = $this->getDropBoxMetaData($path); |
|
| 194 | - if ($metaData) { |
|
| 195 | - if ($metaData['is_dir'] === 'true') { |
|
| 196 | - return 'dir'; |
|
| 197 | - } else { |
|
| 198 | - return 'file'; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 189 | + public function filetype($path) { |
|
| 190 | + if ($path === '' || $path === '/') { |
|
| 191 | + return 'dir'; |
|
| 192 | + } else { |
|
| 193 | + $metaData = $this->getDropBoxMetaData($path); |
|
| 194 | + if ($metaData) { |
|
| 195 | + if ($metaData['is_dir'] === 'true') { |
|
| 196 | + return 'dir'; |
|
| 197 | + } else { |
|
| 198 | + return 'file'; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - public function file_exists($path) { |
|
| 206 | - if ($path === '' || $path === '/') { |
|
| 207 | - return true; |
|
| 208 | - } |
|
| 209 | - if ($this->getDropBoxMetaData($path)) { |
|
| 210 | - return true; |
|
| 211 | - } |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 205 | + public function file_exists($path) { |
|
| 206 | + if ($path === '' || $path === '/') { |
|
| 207 | + return true; |
|
| 208 | + } |
|
| 209 | + if ($this->getDropBoxMetaData($path)) { |
|
| 210 | + return true; |
|
| 211 | + } |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - public function unlink($path) { |
|
| 216 | - try { |
|
| 217 | - $this->dropbox->delete($this->root.$path); |
|
| 218 | - $this->deleteMetaData($path); |
|
| 219 | - return true; |
|
| 220 | - } catch (\Exception $exception) { |
|
| 221 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 222 | - return false; |
|
| 223 | - } |
|
| 224 | - } |
|
| 215 | + public function unlink($path) { |
|
| 216 | + try { |
|
| 217 | + $this->dropbox->delete($this->root.$path); |
|
| 218 | + $this->deleteMetaData($path); |
|
| 219 | + return true; |
|
| 220 | + } catch (\Exception $exception) { |
|
| 221 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 222 | + return false; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - public function rename($path1, $path2) { |
|
| 227 | - try { |
|
| 228 | - // overwrite if target file exists and is not a directory |
|
| 229 | - $destMetaData = $this->getDropBoxMetaData($path2); |
|
| 230 | - if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) { |
|
| 231 | - $this->unlink($path2); |
|
| 232 | - } |
|
| 233 | - $this->dropbox->move($this->root.$path1, $this->root.$path2); |
|
| 234 | - $this->deleteMetaData($path1); |
|
| 235 | - return true; |
|
| 236 | - } catch (\Exception $exception) { |
|
| 237 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 238 | - return false; |
|
| 239 | - } |
|
| 240 | - } |
|
| 226 | + public function rename($path1, $path2) { |
|
| 227 | + try { |
|
| 228 | + // overwrite if target file exists and is not a directory |
|
| 229 | + $destMetaData = $this->getDropBoxMetaData($path2); |
|
| 230 | + if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) { |
|
| 231 | + $this->unlink($path2); |
|
| 232 | + } |
|
| 233 | + $this->dropbox->move($this->root.$path1, $this->root.$path2); |
|
| 234 | + $this->deleteMetaData($path1); |
|
| 235 | + return true; |
|
| 236 | + } catch (\Exception $exception) { |
|
| 237 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 238 | + return false; |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - public function copy($path1, $path2) { |
|
| 243 | - $path1 = $this->root.$path1; |
|
| 244 | - $path2 = $this->root.$path2; |
|
| 245 | - try { |
|
| 246 | - $this->dropbox->copy($path1, $path2); |
|
| 247 | - return true; |
|
| 248 | - } catch (\Exception $exception) { |
|
| 249 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - } |
|
| 242 | + public function copy($path1, $path2) { |
|
| 243 | + $path1 = $this->root.$path1; |
|
| 244 | + $path2 = $this->root.$path2; |
|
| 245 | + try { |
|
| 246 | + $this->dropbox->copy($path1, $path2); |
|
| 247 | + return true; |
|
| 248 | + } catch (\Exception $exception) { |
|
| 249 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - public function fopen($path, $mode) { |
|
| 255 | - $path = $this->root.$path; |
|
| 256 | - switch ($mode) { |
|
| 257 | - case 'r': |
|
| 258 | - case 'rb': |
|
| 259 | - try { |
|
| 260 | - // slashes need to stay |
|
| 261 | - $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
|
| 262 | - $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
| 263 | - $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
|
| 254 | + public function fopen($path, $mode) { |
|
| 255 | + $path = $this->root.$path; |
|
| 256 | + switch ($mode) { |
|
| 257 | + case 'r': |
|
| 258 | + case 'rb': |
|
| 259 | + try { |
|
| 260 | + // slashes need to stay |
|
| 261 | + $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
|
| 262 | + $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
| 263 | + $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
|
| 264 | 264 | |
| 265 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 266 | - try { |
|
| 267 | - $response = $client->get($downloadUrl, [ |
|
| 268 | - 'headers' => $headers, |
|
| 269 | - 'stream' => true, |
|
| 270 | - ]); |
|
| 271 | - } catch (RequestException $e) { |
|
| 272 | - if (!is_null($e->getResponse())) { |
|
| 273 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
| 274 | - return false; |
|
| 275 | - } else { |
|
| 276 | - throw $e; |
|
| 277 | - } |
|
| 278 | - } else { |
|
| 279 | - throw $e; |
|
| 280 | - } |
|
| 281 | - } |
|
| 265 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 266 | + try { |
|
| 267 | + $response = $client->get($downloadUrl, [ |
|
| 268 | + 'headers' => $headers, |
|
| 269 | + 'stream' => true, |
|
| 270 | + ]); |
|
| 271 | + } catch (RequestException $e) { |
|
| 272 | + if (!is_null($e->getResponse())) { |
|
| 273 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
| 274 | + return false; |
|
| 275 | + } else { |
|
| 276 | + throw $e; |
|
| 277 | + } |
|
| 278 | + } else { |
|
| 279 | + throw $e; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - $handle = $response->getBody(); |
|
| 284 | - return RetryWrapper::wrap($handle); |
|
| 285 | - } catch (\Exception $exception) { |
|
| 286 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 287 | - return false; |
|
| 288 | - } |
|
| 289 | - case 'w': |
|
| 290 | - case 'wb': |
|
| 291 | - case 'a': |
|
| 292 | - case 'ab': |
|
| 293 | - case 'r+': |
|
| 294 | - case 'w+': |
|
| 295 | - case 'wb+': |
|
| 296 | - case 'a+': |
|
| 297 | - case 'x': |
|
| 298 | - case 'x+': |
|
| 299 | - case 'c': |
|
| 300 | - case 'c+': |
|
| 301 | - if (strrpos($path, '.') !== false) { |
|
| 302 | - $ext = substr($path, strrpos($path, '.')); |
|
| 303 | - } else { |
|
| 304 | - $ext = ''; |
|
| 305 | - } |
|
| 306 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 307 | - if ($this->file_exists($path)) { |
|
| 308 | - $source = $this->fopen($path, 'r'); |
|
| 309 | - file_put_contents($tmpFile, $source); |
|
| 310 | - } |
|
| 311 | - $handle = fopen($tmpFile, $mode); |
|
| 312 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 313 | - $this->writeBack($tmpFile, $path); |
|
| 314 | - }); |
|
| 315 | - } |
|
| 316 | - return false; |
|
| 317 | - } |
|
| 283 | + $handle = $response->getBody(); |
|
| 284 | + return RetryWrapper::wrap($handle); |
|
| 285 | + } catch (\Exception $exception) { |
|
| 286 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | + case 'w': |
|
| 290 | + case 'wb': |
|
| 291 | + case 'a': |
|
| 292 | + case 'ab': |
|
| 293 | + case 'r+': |
|
| 294 | + case 'w+': |
|
| 295 | + case 'wb+': |
|
| 296 | + case 'a+': |
|
| 297 | + case 'x': |
|
| 298 | + case 'x+': |
|
| 299 | + case 'c': |
|
| 300 | + case 'c+': |
|
| 301 | + if (strrpos($path, '.') !== false) { |
|
| 302 | + $ext = substr($path, strrpos($path, '.')); |
|
| 303 | + } else { |
|
| 304 | + $ext = ''; |
|
| 305 | + } |
|
| 306 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 307 | + if ($this->file_exists($path)) { |
|
| 308 | + $source = $this->fopen($path, 'r'); |
|
| 309 | + file_put_contents($tmpFile, $source); |
|
| 310 | + } |
|
| 311 | + $handle = fopen($tmpFile, $mode); |
|
| 312 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 313 | + $this->writeBack($tmpFile, $path); |
|
| 314 | + }); |
|
| 315 | + } |
|
| 316 | + return false; |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - public function writeBack($tmpFile, $path) { |
|
| 320 | - $handle = fopen($tmpFile, 'r'); |
|
| 321 | - try { |
|
| 322 | - $this->dropbox->putFile($path, $handle); |
|
| 323 | - unlink($tmpFile); |
|
| 324 | - $this->deleteMetaData($path); |
|
| 325 | - } catch (\Exception $exception) { |
|
| 326 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 327 | - } |
|
| 328 | - } |
|
| 319 | + public function writeBack($tmpFile, $path) { |
|
| 320 | + $handle = fopen($tmpFile, 'r'); |
|
| 321 | + try { |
|
| 322 | + $this->dropbox->putFile($path, $handle); |
|
| 323 | + unlink($tmpFile); |
|
| 324 | + $this->deleteMetaData($path); |
|
| 325 | + } catch (\Exception $exception) { |
|
| 326 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - public function free_space($path) { |
|
| 331 | - try { |
|
| 332 | - $info = $this->dropbox->getAccountInfo(); |
|
| 333 | - return $info['quota_info']['quota'] - $info['quota_info']['normal']; |
|
| 334 | - } catch (\Exception $exception) { |
|
| 335 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 336 | - return false; |
|
| 337 | - } |
|
| 338 | - } |
|
| 330 | + public function free_space($path) { |
|
| 331 | + try { |
|
| 332 | + $info = $this->dropbox->getAccountInfo(); |
|
| 333 | + return $info['quota_info']['quota'] - $info['quota_info']['normal']; |
|
| 334 | + } catch (\Exception $exception) { |
|
| 335 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 336 | + return false; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | 339 | |
| 340 | - public function touch($path, $mtime = null) { |
|
| 341 | - if ($this->file_exists($path)) { |
|
| 342 | - return false; |
|
| 343 | - } else { |
|
| 344 | - $this->file_put_contents($path, ''); |
|
| 345 | - } |
|
| 346 | - return true; |
|
| 347 | - } |
|
| 340 | + public function touch($path, $mtime = null) { |
|
| 341 | + if ($this->file_exists($path)) { |
|
| 342 | + return false; |
|
| 343 | + } else { |
|
| 344 | + $this->file_put_contents($path, ''); |
|
| 345 | + } |
|
| 346 | + return true; |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * check if curl is installed |
|
| 351 | - */ |
|
| 352 | - public static function checkDependencies() { |
|
| 353 | - return true; |
|
| 354 | - } |
|
| 349 | + /** |
|
| 350 | + * check if curl is installed |
|
| 351 | + */ |
|
| 352 | + public static function checkDependencies() { |
|
| 353 | + return true; |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | 356 | } |
@@ -41,428 +41,428 @@ |
||
| 41 | 41 | * provide access to SFTP servers. |
| 42 | 42 | */ |
| 43 | 43 | class SFTP extends \OC\Files\Storage\Common { |
| 44 | - private $host; |
|
| 45 | - private $user; |
|
| 46 | - private $root; |
|
| 47 | - private $port = 22; |
|
| 48 | - |
|
| 49 | - private $auth; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var \phpseclib\Net\SFTP |
|
| 53 | - */ |
|
| 54 | - protected $client; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param string $host protocol://server:port |
|
| 58 | - * @return array [$server, $port] |
|
| 59 | - */ |
|
| 60 | - private function splitHost($host) { |
|
| 61 | - $input = $host; |
|
| 62 | - if (strpos($host, '://') === false) { |
|
| 63 | - // add a protocol to fix parse_url behavior with ipv6 |
|
| 64 | - $host = 'http://' . $host; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $parsed = parse_url($host); |
|
| 68 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
| 69 | - return [$parsed['host'], $parsed['port']]; |
|
| 70 | - } else if (is_array($parsed)) { |
|
| 71 | - return [$parsed['host'], 22]; |
|
| 72 | - } else { |
|
| 73 | - return [$input, 22]; |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * {@inheritdoc} |
|
| 79 | - */ |
|
| 80 | - public function __construct($params) { |
|
| 81 | - // Register sftp:// |
|
| 82 | - Stream::register(); |
|
| 83 | - |
|
| 84 | - $parsedHost = $this->splitHost($params['host']); |
|
| 85 | - |
|
| 86 | - $this->host = $parsedHost[0]; |
|
| 87 | - $this->port = $parsedHost[1]; |
|
| 88 | - |
|
| 89 | - if (!isset($params['user'])) { |
|
| 90 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 91 | - } |
|
| 92 | - $this->user = $params['user']; |
|
| 93 | - |
|
| 94 | - if (isset($params['public_key_auth'])) { |
|
| 95 | - $this->auth = $params['public_key_auth']; |
|
| 96 | - } elseif (isset($params['password'])) { |
|
| 97 | - $this->auth = $params['password']; |
|
| 98 | - } else { |
|
| 99 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $this->root |
|
| 103 | - = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
| 104 | - |
|
| 105 | - if ($this->root[0] !== '/') { |
|
| 106 | - $this->root = '/' . $this->root; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - if (substr($this->root, -1, 1) !== '/') { |
|
| 110 | - $this->root .= '/'; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Returns the connection. |
|
| 116 | - * |
|
| 117 | - * @return \phpseclib\Net\SFTP connected client instance |
|
| 118 | - * @throws \Exception when the connection failed |
|
| 119 | - */ |
|
| 120 | - public function getConnection() { |
|
| 121 | - if (!is_null($this->client)) { |
|
| 122 | - return $this->client; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $hostKeys = $this->readHostKeys(); |
|
| 126 | - $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
| 127 | - |
|
| 128 | - // The SSH Host Key MUST be verified before login(). |
|
| 129 | - $currentHostKey = $this->client->getServerPublicHostKey(); |
|
| 130 | - if (array_key_exists($this->host, $hostKeys)) { |
|
| 131 | - if ($hostKeys[$this->host] !== $currentHostKey) { |
|
| 132 | - throw new \Exception('Host public key does not match known key'); |
|
| 133 | - } |
|
| 134 | - } else { |
|
| 135 | - $hostKeys[$this->host] = $currentHostKey; |
|
| 136 | - $this->writeHostKeys($hostKeys); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if (!$this->client->login($this->user, $this->auth)) { |
|
| 140 | - throw new \Exception('Login failed'); |
|
| 141 | - } |
|
| 142 | - return $this->client; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * {@inheritdoc} |
|
| 147 | - */ |
|
| 148 | - public function test() { |
|
| 149 | - if ( |
|
| 150 | - !isset($this->host) |
|
| 151 | - || !isset($this->user) |
|
| 152 | - ) { |
|
| 153 | - return false; |
|
| 154 | - } |
|
| 155 | - return $this->getConnection()->nlist() !== false; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * {@inheritdoc} |
|
| 160 | - */ |
|
| 161 | - public function getId(){ |
|
| 162 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
| 163 | - if ($this->port !== 22) { |
|
| 164 | - $id .= ':' . $this->port; |
|
| 165 | - } |
|
| 166 | - // note: this will double the root slash, |
|
| 167 | - // we should not change it to keep compatible with |
|
| 168 | - // old storage ids |
|
| 169 | - $id .= '/' . $this->root; |
|
| 170 | - return $id; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @return string |
|
| 175 | - */ |
|
| 176 | - public function getHost() { |
|
| 177 | - return $this->host; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @return string |
|
| 182 | - */ |
|
| 183 | - public function getRoot() { |
|
| 184 | - return $this->root; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @return mixed |
|
| 189 | - */ |
|
| 190 | - public function getUser() { |
|
| 191 | - return $this->user; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param string $path |
|
| 196 | - * @return string |
|
| 197 | - */ |
|
| 198 | - private function absPath($path) { |
|
| 199 | - return $this->root . $this->cleanPath($path); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @return string|false |
|
| 204 | - */ |
|
| 205 | - private function hostKeysPath() { |
|
| 206 | - try { |
|
| 207 | - $storage_view = \OCP\Files::getStorage('files_external'); |
|
| 208 | - if ($storage_view) { |
|
| 209 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
| 210 | - $storage_view->getAbsolutePath('') . |
|
| 211 | - 'ssh_hostKeys'; |
|
| 212 | - } |
|
| 213 | - } catch (\Exception $e) { |
|
| 214 | - } |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @param $keys |
|
| 220 | - * @return bool |
|
| 221 | - */ |
|
| 222 | - protected function writeHostKeys($keys) { |
|
| 223 | - try { |
|
| 224 | - $keyPath = $this->hostKeysPath(); |
|
| 225 | - if ($keyPath && file_exists($keyPath)) { |
|
| 226 | - $fp = fopen($keyPath, 'w'); |
|
| 227 | - foreach ($keys as $host => $key) { |
|
| 228 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
| 229 | - } |
|
| 230 | - fclose($fp); |
|
| 231 | - return true; |
|
| 232 | - } |
|
| 233 | - } catch (\Exception $e) { |
|
| 234 | - } |
|
| 235 | - return false; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @return array |
|
| 240 | - */ |
|
| 241 | - protected function readHostKeys() { |
|
| 242 | - try { |
|
| 243 | - $keyPath = $this->hostKeysPath(); |
|
| 244 | - if (file_exists($keyPath)) { |
|
| 245 | - $hosts = array(); |
|
| 246 | - $keys = array(); |
|
| 247 | - $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
| 248 | - if ($lines) { |
|
| 249 | - foreach ($lines as $line) { |
|
| 250 | - $hostKeyArray = explode("::", $line, 2); |
|
| 251 | - if (count($hostKeyArray) === 2) { |
|
| 252 | - $hosts[] = $hostKeyArray[0]; |
|
| 253 | - $keys[] = $hostKeyArray[1]; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - return array_combine($hosts, $keys); |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - } catch (\Exception $e) { |
|
| 260 | - } |
|
| 261 | - return array(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * {@inheritdoc} |
|
| 266 | - */ |
|
| 267 | - public function mkdir($path) { |
|
| 268 | - try { |
|
| 269 | - return $this->getConnection()->mkdir($this->absPath($path)); |
|
| 270 | - } catch (\Exception $e) { |
|
| 271 | - return false; |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * {@inheritdoc} |
|
| 277 | - */ |
|
| 278 | - public function rmdir($path) { |
|
| 279 | - try { |
|
| 280 | - $result = $this->getConnection()->delete($this->absPath($path), true); |
|
| 281 | - // workaround: stray stat cache entry when deleting empty folders |
|
| 282 | - // see https://github.com/phpseclib/phpseclib/issues/706 |
|
| 283 | - $this->getConnection()->clearStatCache(); |
|
| 284 | - return $result; |
|
| 285 | - } catch (\Exception $e) { |
|
| 286 | - return false; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * {@inheritdoc} |
|
| 292 | - */ |
|
| 293 | - public function opendir($path) { |
|
| 294 | - try { |
|
| 295 | - $list = $this->getConnection()->nlist($this->absPath($path)); |
|
| 296 | - if ($list === false) { |
|
| 297 | - return false; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $id = md5('sftp:' . $path); |
|
| 301 | - $dirStream = array(); |
|
| 302 | - foreach($list as $file) { |
|
| 303 | - if ($file !== '.' && $file !== '..') { |
|
| 304 | - $dirStream[] = $file; |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - return IteratorDirectory::wrap($dirStream); |
|
| 308 | - } catch(\Exception $e) { |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * {@inheritdoc} |
|
| 315 | - */ |
|
| 316 | - public function filetype($path) { |
|
| 317 | - try { |
|
| 318 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 319 | - if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) { |
|
| 320 | - return 'file'; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) { |
|
| 324 | - return 'dir'; |
|
| 325 | - } |
|
| 326 | - } catch (\Exception $e) { |
|
| 327 | - |
|
| 328 | - } |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * {@inheritdoc} |
|
| 334 | - */ |
|
| 335 | - public function file_exists($path) { |
|
| 336 | - try { |
|
| 337 | - return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
| 338 | - } catch (\Exception $e) { |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * {@inheritdoc} |
|
| 345 | - */ |
|
| 346 | - public function unlink($path) { |
|
| 347 | - try { |
|
| 348 | - return $this->getConnection()->delete($this->absPath($path), true); |
|
| 349 | - } catch (\Exception $e) { |
|
| 350 | - return false; |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * {@inheritdoc} |
|
| 356 | - */ |
|
| 357 | - public function fopen($path, $mode) { |
|
| 358 | - try { |
|
| 359 | - $absPath = $this->absPath($path); |
|
| 360 | - switch($mode) { |
|
| 361 | - case 'r': |
|
| 362 | - case 'rb': |
|
| 363 | - if ( !$this->file_exists($path)) { |
|
| 364 | - return false; |
|
| 365 | - } |
|
| 366 | - case 'w': |
|
| 367 | - case 'wb': |
|
| 368 | - case 'a': |
|
| 369 | - case 'ab': |
|
| 370 | - case 'r+': |
|
| 371 | - case 'w+': |
|
| 372 | - case 'wb+': |
|
| 373 | - case 'a+': |
|
| 374 | - case 'x': |
|
| 375 | - case 'x+': |
|
| 376 | - case 'c': |
|
| 377 | - case 'c+': |
|
| 378 | - $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
| 379 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 380 | - return RetryWrapper::wrap($handle); |
|
| 381 | - } |
|
| 382 | - } catch (\Exception $e) { |
|
| 383 | - } |
|
| 384 | - return false; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * {@inheritdoc} |
|
| 389 | - */ |
|
| 390 | - public function touch($path, $mtime=null) { |
|
| 391 | - try { |
|
| 392 | - if (!is_null($mtime)) { |
|
| 393 | - return false; |
|
| 394 | - } |
|
| 395 | - if (!$this->file_exists($path)) { |
|
| 396 | - $this->getConnection()->put($this->absPath($path), ''); |
|
| 397 | - } else { |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - } catch (\Exception $e) { |
|
| 401 | - return false; |
|
| 402 | - } |
|
| 403 | - return true; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * @param string $path |
|
| 408 | - * @param string $target |
|
| 409 | - * @throws \Exception |
|
| 410 | - */ |
|
| 411 | - public function getFile($path, $target) { |
|
| 412 | - $this->getConnection()->get($path, $target); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @param string $path |
|
| 417 | - * @param string $target |
|
| 418 | - * @throws \Exception |
|
| 419 | - */ |
|
| 420 | - public function uploadFile($path, $target) { |
|
| 421 | - $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * {@inheritdoc} |
|
| 426 | - */ |
|
| 427 | - public function rename($source, $target) { |
|
| 428 | - try { |
|
| 429 | - if ($this->file_exists($target)) { |
|
| 430 | - $this->unlink($target); |
|
| 431 | - } |
|
| 432 | - return $this->getConnection()->rename( |
|
| 433 | - $this->absPath($source), |
|
| 434 | - $this->absPath($target) |
|
| 435 | - ); |
|
| 436 | - } catch (\Exception $e) { |
|
| 437 | - return false; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * {@inheritdoc} |
|
| 443 | - */ |
|
| 444 | - public function stat($path) { |
|
| 445 | - try { |
|
| 446 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 447 | - |
|
| 448 | - $mtime = $stat ? $stat['mtime'] : -1; |
|
| 449 | - $size = $stat ? $stat['size'] : 0; |
|
| 450 | - |
|
| 451 | - return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
| 452 | - } catch (\Exception $e) { |
|
| 453 | - return false; |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * @param string $path |
|
| 459 | - * @return string |
|
| 460 | - */ |
|
| 461 | - public function constructUrl($path) { |
|
| 462 | - // Do not pass the password here. We want to use the Net_SFTP object |
|
| 463 | - // supplied via stream context or fail. We only supply username and |
|
| 464 | - // hostname because this might show up in logs (they are not used). |
|
| 465 | - $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
| 466 | - return $url; |
|
| 467 | - } |
|
| 44 | + private $host; |
|
| 45 | + private $user; |
|
| 46 | + private $root; |
|
| 47 | + private $port = 22; |
|
| 48 | + |
|
| 49 | + private $auth; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var \phpseclib\Net\SFTP |
|
| 53 | + */ |
|
| 54 | + protected $client; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param string $host protocol://server:port |
|
| 58 | + * @return array [$server, $port] |
|
| 59 | + */ |
|
| 60 | + private function splitHost($host) { |
|
| 61 | + $input = $host; |
|
| 62 | + if (strpos($host, '://') === false) { |
|
| 63 | + // add a protocol to fix parse_url behavior with ipv6 |
|
| 64 | + $host = 'http://' . $host; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $parsed = parse_url($host); |
|
| 68 | + if(is_array($parsed) && isset($parsed['port'])) { |
|
| 69 | + return [$parsed['host'], $parsed['port']]; |
|
| 70 | + } else if (is_array($parsed)) { |
|
| 71 | + return [$parsed['host'], 22]; |
|
| 72 | + } else { |
|
| 73 | + return [$input, 22]; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * {@inheritdoc} |
|
| 79 | + */ |
|
| 80 | + public function __construct($params) { |
|
| 81 | + // Register sftp:// |
|
| 82 | + Stream::register(); |
|
| 83 | + |
|
| 84 | + $parsedHost = $this->splitHost($params['host']); |
|
| 85 | + |
|
| 86 | + $this->host = $parsedHost[0]; |
|
| 87 | + $this->port = $parsedHost[1]; |
|
| 88 | + |
|
| 89 | + if (!isset($params['user'])) { |
|
| 90 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 91 | + } |
|
| 92 | + $this->user = $params['user']; |
|
| 93 | + |
|
| 94 | + if (isset($params['public_key_auth'])) { |
|
| 95 | + $this->auth = $params['public_key_auth']; |
|
| 96 | + } elseif (isset($params['password'])) { |
|
| 97 | + $this->auth = $params['password']; |
|
| 98 | + } else { |
|
| 99 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $this->root |
|
| 103 | + = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
| 104 | + |
|
| 105 | + if ($this->root[0] !== '/') { |
|
| 106 | + $this->root = '/' . $this->root; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + if (substr($this->root, -1, 1) !== '/') { |
|
| 110 | + $this->root .= '/'; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Returns the connection. |
|
| 116 | + * |
|
| 117 | + * @return \phpseclib\Net\SFTP connected client instance |
|
| 118 | + * @throws \Exception when the connection failed |
|
| 119 | + */ |
|
| 120 | + public function getConnection() { |
|
| 121 | + if (!is_null($this->client)) { |
|
| 122 | + return $this->client; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $hostKeys = $this->readHostKeys(); |
|
| 126 | + $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
| 127 | + |
|
| 128 | + // The SSH Host Key MUST be verified before login(). |
|
| 129 | + $currentHostKey = $this->client->getServerPublicHostKey(); |
|
| 130 | + if (array_key_exists($this->host, $hostKeys)) { |
|
| 131 | + if ($hostKeys[$this->host] !== $currentHostKey) { |
|
| 132 | + throw new \Exception('Host public key does not match known key'); |
|
| 133 | + } |
|
| 134 | + } else { |
|
| 135 | + $hostKeys[$this->host] = $currentHostKey; |
|
| 136 | + $this->writeHostKeys($hostKeys); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if (!$this->client->login($this->user, $this->auth)) { |
|
| 140 | + throw new \Exception('Login failed'); |
|
| 141 | + } |
|
| 142 | + return $this->client; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * {@inheritdoc} |
|
| 147 | + */ |
|
| 148 | + public function test() { |
|
| 149 | + if ( |
|
| 150 | + !isset($this->host) |
|
| 151 | + || !isset($this->user) |
|
| 152 | + ) { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 155 | + return $this->getConnection()->nlist() !== false; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * {@inheritdoc} |
|
| 160 | + */ |
|
| 161 | + public function getId(){ |
|
| 162 | + $id = 'sftp::' . $this->user . '@' . $this->host; |
|
| 163 | + if ($this->port !== 22) { |
|
| 164 | + $id .= ':' . $this->port; |
|
| 165 | + } |
|
| 166 | + // note: this will double the root slash, |
|
| 167 | + // we should not change it to keep compatible with |
|
| 168 | + // old storage ids |
|
| 169 | + $id .= '/' . $this->root; |
|
| 170 | + return $id; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @return string |
|
| 175 | + */ |
|
| 176 | + public function getHost() { |
|
| 177 | + return $this->host; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @return string |
|
| 182 | + */ |
|
| 183 | + public function getRoot() { |
|
| 184 | + return $this->root; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @return mixed |
|
| 189 | + */ |
|
| 190 | + public function getUser() { |
|
| 191 | + return $this->user; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param string $path |
|
| 196 | + * @return string |
|
| 197 | + */ |
|
| 198 | + private function absPath($path) { |
|
| 199 | + return $this->root . $this->cleanPath($path); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @return string|false |
|
| 204 | + */ |
|
| 205 | + private function hostKeysPath() { |
|
| 206 | + try { |
|
| 207 | + $storage_view = \OCP\Files::getStorage('files_external'); |
|
| 208 | + if ($storage_view) { |
|
| 209 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
| 210 | + $storage_view->getAbsolutePath('') . |
|
| 211 | + 'ssh_hostKeys'; |
|
| 212 | + } |
|
| 213 | + } catch (\Exception $e) { |
|
| 214 | + } |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @param $keys |
|
| 220 | + * @return bool |
|
| 221 | + */ |
|
| 222 | + protected function writeHostKeys($keys) { |
|
| 223 | + try { |
|
| 224 | + $keyPath = $this->hostKeysPath(); |
|
| 225 | + if ($keyPath && file_exists($keyPath)) { |
|
| 226 | + $fp = fopen($keyPath, 'w'); |
|
| 227 | + foreach ($keys as $host => $key) { |
|
| 228 | + fwrite($fp, $host . '::' . $key . "\n"); |
|
| 229 | + } |
|
| 230 | + fclose($fp); |
|
| 231 | + return true; |
|
| 232 | + } |
|
| 233 | + } catch (\Exception $e) { |
|
| 234 | + } |
|
| 235 | + return false; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @return array |
|
| 240 | + */ |
|
| 241 | + protected function readHostKeys() { |
|
| 242 | + try { |
|
| 243 | + $keyPath = $this->hostKeysPath(); |
|
| 244 | + if (file_exists($keyPath)) { |
|
| 245 | + $hosts = array(); |
|
| 246 | + $keys = array(); |
|
| 247 | + $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
| 248 | + if ($lines) { |
|
| 249 | + foreach ($lines as $line) { |
|
| 250 | + $hostKeyArray = explode("::", $line, 2); |
|
| 251 | + if (count($hostKeyArray) === 2) { |
|
| 252 | + $hosts[] = $hostKeyArray[0]; |
|
| 253 | + $keys[] = $hostKeyArray[1]; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + return array_combine($hosts, $keys); |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + } catch (\Exception $e) { |
|
| 260 | + } |
|
| 261 | + return array(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * {@inheritdoc} |
|
| 266 | + */ |
|
| 267 | + public function mkdir($path) { |
|
| 268 | + try { |
|
| 269 | + return $this->getConnection()->mkdir($this->absPath($path)); |
|
| 270 | + } catch (\Exception $e) { |
|
| 271 | + return false; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * {@inheritdoc} |
|
| 277 | + */ |
|
| 278 | + public function rmdir($path) { |
|
| 279 | + try { |
|
| 280 | + $result = $this->getConnection()->delete($this->absPath($path), true); |
|
| 281 | + // workaround: stray stat cache entry when deleting empty folders |
|
| 282 | + // see https://github.com/phpseclib/phpseclib/issues/706 |
|
| 283 | + $this->getConnection()->clearStatCache(); |
|
| 284 | + return $result; |
|
| 285 | + } catch (\Exception $e) { |
|
| 286 | + return false; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * {@inheritdoc} |
|
| 292 | + */ |
|
| 293 | + public function opendir($path) { |
|
| 294 | + try { |
|
| 295 | + $list = $this->getConnection()->nlist($this->absPath($path)); |
|
| 296 | + if ($list === false) { |
|
| 297 | + return false; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $id = md5('sftp:' . $path); |
|
| 301 | + $dirStream = array(); |
|
| 302 | + foreach($list as $file) { |
|
| 303 | + if ($file !== '.' && $file !== '..') { |
|
| 304 | + $dirStream[] = $file; |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + return IteratorDirectory::wrap($dirStream); |
|
| 308 | + } catch(\Exception $e) { |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * {@inheritdoc} |
|
| 315 | + */ |
|
| 316 | + public function filetype($path) { |
|
| 317 | + try { |
|
| 318 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 319 | + if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) { |
|
| 320 | + return 'file'; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) { |
|
| 324 | + return 'dir'; |
|
| 325 | + } |
|
| 326 | + } catch (\Exception $e) { |
|
| 327 | + |
|
| 328 | + } |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * {@inheritdoc} |
|
| 334 | + */ |
|
| 335 | + public function file_exists($path) { |
|
| 336 | + try { |
|
| 337 | + return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
| 338 | + } catch (\Exception $e) { |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * {@inheritdoc} |
|
| 345 | + */ |
|
| 346 | + public function unlink($path) { |
|
| 347 | + try { |
|
| 348 | + return $this->getConnection()->delete($this->absPath($path), true); |
|
| 349 | + } catch (\Exception $e) { |
|
| 350 | + return false; |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * {@inheritdoc} |
|
| 356 | + */ |
|
| 357 | + public function fopen($path, $mode) { |
|
| 358 | + try { |
|
| 359 | + $absPath = $this->absPath($path); |
|
| 360 | + switch($mode) { |
|
| 361 | + case 'r': |
|
| 362 | + case 'rb': |
|
| 363 | + if ( !$this->file_exists($path)) { |
|
| 364 | + return false; |
|
| 365 | + } |
|
| 366 | + case 'w': |
|
| 367 | + case 'wb': |
|
| 368 | + case 'a': |
|
| 369 | + case 'ab': |
|
| 370 | + case 'r+': |
|
| 371 | + case 'w+': |
|
| 372 | + case 'wb+': |
|
| 373 | + case 'a+': |
|
| 374 | + case 'x': |
|
| 375 | + case 'x+': |
|
| 376 | + case 'c': |
|
| 377 | + case 'c+': |
|
| 378 | + $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
| 379 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 380 | + return RetryWrapper::wrap($handle); |
|
| 381 | + } |
|
| 382 | + } catch (\Exception $e) { |
|
| 383 | + } |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * {@inheritdoc} |
|
| 389 | + */ |
|
| 390 | + public function touch($path, $mtime=null) { |
|
| 391 | + try { |
|
| 392 | + if (!is_null($mtime)) { |
|
| 393 | + return false; |
|
| 394 | + } |
|
| 395 | + if (!$this->file_exists($path)) { |
|
| 396 | + $this->getConnection()->put($this->absPath($path), ''); |
|
| 397 | + } else { |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + } catch (\Exception $e) { |
|
| 401 | + return false; |
|
| 402 | + } |
|
| 403 | + return true; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * @param string $path |
|
| 408 | + * @param string $target |
|
| 409 | + * @throws \Exception |
|
| 410 | + */ |
|
| 411 | + public function getFile($path, $target) { |
|
| 412 | + $this->getConnection()->get($path, $target); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @param string $path |
|
| 417 | + * @param string $target |
|
| 418 | + * @throws \Exception |
|
| 419 | + */ |
|
| 420 | + public function uploadFile($path, $target) { |
|
| 421 | + $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * {@inheritdoc} |
|
| 426 | + */ |
|
| 427 | + public function rename($source, $target) { |
|
| 428 | + try { |
|
| 429 | + if ($this->file_exists($target)) { |
|
| 430 | + $this->unlink($target); |
|
| 431 | + } |
|
| 432 | + return $this->getConnection()->rename( |
|
| 433 | + $this->absPath($source), |
|
| 434 | + $this->absPath($target) |
|
| 435 | + ); |
|
| 436 | + } catch (\Exception $e) { |
|
| 437 | + return false; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * {@inheritdoc} |
|
| 443 | + */ |
|
| 444 | + public function stat($path) { |
|
| 445 | + try { |
|
| 446 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 447 | + |
|
| 448 | + $mtime = $stat ? $stat['mtime'] : -1; |
|
| 449 | + $size = $stat ? $stat['size'] : 0; |
|
| 450 | + |
|
| 451 | + return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
| 452 | + } catch (\Exception $e) { |
|
| 453 | + return false; |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * @param string $path |
|
| 459 | + * @return string |
|
| 460 | + */ |
|
| 461 | + public function constructUrl($path) { |
|
| 462 | + // Do not pass the password here. We want to use the Net_SFTP object |
|
| 463 | + // supplied via stream context or fail. We only supply username and |
|
| 464 | + // hostname because this might show up in logs (they are not used). |
|
| 465 | + $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
| 466 | + return $url; |
|
| 467 | + } |
|
| 468 | 468 | } |
@@ -61,11 +61,11 @@ discard block |
||
| 61 | 61 | $input = $host; |
| 62 | 62 | if (strpos($host, '://') === false) { |
| 63 | 63 | // add a protocol to fix parse_url behavior with ipv6 |
| 64 | - $host = 'http://' . $host; |
|
| 64 | + $host = 'http://'.$host; |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | $parsed = parse_url($host); |
| 68 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
| 68 | + if (is_array($parsed) && isset($parsed['port'])) { |
|
| 69 | 69 | return [$parsed['host'], $parsed['port']]; |
| 70 | 70 | } else if (is_array($parsed)) { |
| 71 | 71 | return [$parsed['host'], 22]; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | // Register sftp:// |
| 82 | 82 | Stream::register(); |
| 83 | 83 | |
| 84 | - $parsedHost = $this->splitHost($params['host']); |
|
| 84 | + $parsedHost = $this->splitHost($params['host']); |
|
| 85 | 85 | |
| 86 | 86 | $this->host = $parsedHost[0]; |
| 87 | 87 | $this->port = $parsedHost[1]; |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
| 104 | 104 | |
| 105 | 105 | if ($this->root[0] !== '/') { |
| 106 | - $this->root = '/' . $this->root; |
|
| 106 | + $this->root = '/'.$this->root; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | if (substr($this->root, -1, 1) !== '/') { |
@@ -158,15 +158,15 @@ discard block |
||
| 158 | 158 | /** |
| 159 | 159 | * {@inheritdoc} |
| 160 | 160 | */ |
| 161 | - public function getId(){ |
|
| 162 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
| 161 | + public function getId() { |
|
| 162 | + $id = 'sftp::'.$this->user.'@'.$this->host; |
|
| 163 | 163 | if ($this->port !== 22) { |
| 164 | - $id .= ':' . $this->port; |
|
| 164 | + $id .= ':'.$this->port; |
|
| 165 | 165 | } |
| 166 | 166 | // note: this will double the root slash, |
| 167 | 167 | // we should not change it to keep compatible with |
| 168 | 168 | // old storage ids |
| 169 | - $id .= '/' . $this->root; |
|
| 169 | + $id .= '/'.$this->root; |
|
| 170 | 170 | return $id; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * @return string |
| 197 | 197 | */ |
| 198 | 198 | private function absPath($path) { |
| 199 | - return $this->root . $this->cleanPath($path); |
|
| 199 | + return $this->root.$this->cleanPath($path); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | /** |
@@ -206,8 +206,8 @@ discard block |
||
| 206 | 206 | try { |
| 207 | 207 | $storage_view = \OCP\Files::getStorage('files_external'); |
| 208 | 208 | if ($storage_view) { |
| 209 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
| 210 | - $storage_view->getAbsolutePath('') . |
|
| 209 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data'). |
|
| 210 | + $storage_view->getAbsolutePath(''). |
|
| 211 | 211 | 'ssh_hostKeys'; |
| 212 | 212 | } |
| 213 | 213 | } catch (\Exception $e) { |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | if ($keyPath && file_exists($keyPath)) { |
| 226 | 226 | $fp = fopen($keyPath, 'w'); |
| 227 | 227 | foreach ($keys as $host => $key) { |
| 228 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
| 228 | + fwrite($fp, $host.'::'.$key."\n"); |
|
| 229 | 229 | } |
| 230 | 230 | fclose($fp); |
| 231 | 231 | return true; |
@@ -297,15 +297,15 @@ discard block |
||
| 297 | 297 | return false; |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | - $id = md5('sftp:' . $path); |
|
| 300 | + $id = md5('sftp:'.$path); |
|
| 301 | 301 | $dirStream = array(); |
| 302 | - foreach($list as $file) { |
|
| 302 | + foreach ($list as $file) { |
|
| 303 | 303 | if ($file !== '.' && $file !== '..') { |
| 304 | 304 | $dirStream[] = $file; |
| 305 | 305 | } |
| 306 | 306 | } |
| 307 | 307 | return IteratorDirectory::wrap($dirStream); |
| 308 | - } catch(\Exception $e) { |
|
| 308 | + } catch (\Exception $e) { |
|
| 309 | 309 | return false; |
| 310 | 310 | } |
| 311 | 311 | } |
@@ -357,10 +357,10 @@ discard block |
||
| 357 | 357 | public function fopen($path, $mode) { |
| 358 | 358 | try { |
| 359 | 359 | $absPath = $this->absPath($path); |
| 360 | - switch($mode) { |
|
| 360 | + switch ($mode) { |
|
| 361 | 361 | case 'r': |
| 362 | 362 | case 'rb': |
| 363 | - if ( !$this->file_exists($path)) { |
|
| 363 | + if (!$this->file_exists($path)) { |
|
| 364 | 364 | return false; |
| 365 | 365 | } |
| 366 | 366 | case 'w': |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | /** |
| 388 | 388 | * {@inheritdoc} |
| 389 | 389 | */ |
| 390 | - public function touch($path, $mtime=null) { |
|
| 390 | + public function touch($path, $mtime = null) { |
|
| 391 | 391 | try { |
| 392 | 392 | if (!is_null($mtime)) { |
| 393 | 393 | return false; |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | // Do not pass the password here. We want to use the Net_SFTP object |
| 463 | 463 | // supplied via stream context or fail. We only supply username and |
| 464 | 464 | // hostname because this might show up in logs (they are not used). |
| 465 | - $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
| 465 | + $url = 'sftp://'.urlencode($this->user).'@'.$this->host.':'.$this->port.$this->root.$path; |
|
| 466 | 466 | return $url; |
| 467 | 467 | } |
| 468 | 468 | } |
@@ -33,45 +33,45 @@ |
||
| 33 | 33 | * |
| 34 | 34 | */ |
| 35 | 35 | class OwnCloud extends \OC\Files\Storage\DAV{ |
| 36 | - const OC_URL_SUFFIX = 'remote.php/webdav'; |
|
| 36 | + const OC_URL_SUFFIX = 'remote.php/webdav'; |
|
| 37 | 37 | |
| 38 | - public function __construct($params) { |
|
| 39 | - // extract context path from host if specified |
|
| 40 | - // (owncloud install path on host) |
|
| 41 | - $host = $params['host']; |
|
| 42 | - // strip protocol |
|
| 43 | - if (substr($host, 0, 8) === "https://") { |
|
| 44 | - $host = substr($host, 8); |
|
| 45 | - $params['secure'] = true; |
|
| 46 | - } else if (substr($host, 0, 7) === "http://") { |
|
| 47 | - $host = substr($host, 7); |
|
| 48 | - $params['secure'] = false; |
|
| 49 | - } |
|
| 50 | - $contextPath = ''; |
|
| 51 | - $hostSlashPos = strpos($host, '/'); |
|
| 52 | - if ($hostSlashPos !== false){ |
|
| 53 | - $contextPath = substr($host, $hostSlashPos); |
|
| 54 | - $host = substr($host, 0, $hostSlashPos); |
|
| 55 | - } |
|
| 38 | + public function __construct($params) { |
|
| 39 | + // extract context path from host if specified |
|
| 40 | + // (owncloud install path on host) |
|
| 41 | + $host = $params['host']; |
|
| 42 | + // strip protocol |
|
| 43 | + if (substr($host, 0, 8) === "https://") { |
|
| 44 | + $host = substr($host, 8); |
|
| 45 | + $params['secure'] = true; |
|
| 46 | + } else if (substr($host, 0, 7) === "http://") { |
|
| 47 | + $host = substr($host, 7); |
|
| 48 | + $params['secure'] = false; |
|
| 49 | + } |
|
| 50 | + $contextPath = ''; |
|
| 51 | + $hostSlashPos = strpos($host, '/'); |
|
| 52 | + if ($hostSlashPos !== false){ |
|
| 53 | + $contextPath = substr($host, $hostSlashPos); |
|
| 54 | + $host = substr($host, 0, $hostSlashPos); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - if (substr($contextPath, -1) !== '/'){ |
|
| 58 | - $contextPath .= '/'; |
|
| 59 | - } |
|
| 57 | + if (substr($contextPath, -1) !== '/'){ |
|
| 58 | + $contextPath .= '/'; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - if (isset($params['root'])){ |
|
| 62 | - $root = $params['root']; |
|
| 63 | - if (substr($root, 0, 1) !== '/'){ |
|
| 64 | - $root = '/' . $root; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - else{ |
|
| 68 | - $root = '/'; |
|
| 69 | - } |
|
| 61 | + if (isset($params['root'])){ |
|
| 62 | + $root = $params['root']; |
|
| 63 | + if (substr($root, 0, 1) !== '/'){ |
|
| 64 | + $root = '/' . $root; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + else{ |
|
| 68 | + $root = '/'; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $params['host'] = $host; |
|
| 72 | - $params['root'] = $contextPath . self::OC_URL_SUFFIX . $root; |
|
| 73 | - $params['authType'] = Client::AUTH_BASIC; |
|
| 71 | + $params['host'] = $host; |
|
| 72 | + $params['root'] = $contextPath . self::OC_URL_SUFFIX . $root; |
|
| 73 | + $params['authType'] = Client::AUTH_BASIC; |
|
| 74 | 74 | |
| 75 | - parent::__construct($params); |
|
| 76 | - } |
|
| 75 | + parent::__construct($params); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -39,187 +39,187 @@ |
||
| 39 | 39 | use Symfony\Component\Console\Output\OutputInterface; |
| 40 | 40 | |
| 41 | 41 | class Import extends Base { |
| 42 | - /** |
|
| 43 | - * @var GlobalStoragesService |
|
| 44 | - */ |
|
| 45 | - private $globalService; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var UserStoragesService |
|
| 49 | - */ |
|
| 50 | - private $userService; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var IUserSession |
|
| 54 | - */ |
|
| 55 | - private $userSession; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var IUserManager |
|
| 59 | - */ |
|
| 60 | - private $userManager; |
|
| 61 | - |
|
| 62 | - /** @var ImportLegacyStoragesService */ |
|
| 63 | - private $importLegacyStorageService; |
|
| 64 | - |
|
| 65 | - /** @var BackendService */ |
|
| 66 | - private $backendService; |
|
| 67 | - |
|
| 68 | - function __construct(GlobalStoragesService $globalService, |
|
| 69 | - UserStoragesService $userService, |
|
| 70 | - IUserSession $userSession, |
|
| 71 | - IUserManager $userManager, |
|
| 72 | - ImportLegacyStoragesService $importLegacyStorageService, |
|
| 73 | - BackendService $backendService |
|
| 74 | - ) { |
|
| 75 | - parent::__construct(); |
|
| 76 | - $this->globalService = $globalService; |
|
| 77 | - $this->userService = $userService; |
|
| 78 | - $this->userSession = $userSession; |
|
| 79 | - $this->userManager = $userManager; |
|
| 80 | - $this->importLegacyStorageService = $importLegacyStorageService; |
|
| 81 | - $this->backendService = $backendService; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - protected function configure() { |
|
| 85 | - $this |
|
| 86 | - ->setName('files_external:import') |
|
| 87 | - ->setDescription('Import mount configurations') |
|
| 88 | - ->addOption( |
|
| 89 | - 'user', |
|
| 90 | - null, |
|
| 91 | - InputOption::VALUE_OPTIONAL, |
|
| 92 | - 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
| 93 | - ) |
|
| 94 | - ->addArgument( |
|
| 95 | - 'path', |
|
| 96 | - InputArgument::REQUIRED, |
|
| 97 | - 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
| 98 | - ) |
|
| 99 | - ->addOption( |
|
| 100 | - 'dry', |
|
| 101 | - null, |
|
| 102 | - InputOption::VALUE_NONE, |
|
| 103 | - 'Don\'t save the imported mounts, only list the new mounts' |
|
| 104 | - ); |
|
| 105 | - parent::configure(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 109 | - $user = $input->getOption('user'); |
|
| 110 | - $path = $input->getArgument('path'); |
|
| 111 | - if ($path === '-') { |
|
| 112 | - $json = file_get_contents('php://stdin'); |
|
| 113 | - } else { |
|
| 114 | - if (!file_exists($path)) { |
|
| 115 | - $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
| 116 | - return 1; |
|
| 117 | - } |
|
| 118 | - $json = file_get_contents($path); |
|
| 119 | - } |
|
| 120 | - if (!is_string($json) || strlen($json) < 2) { |
|
| 121 | - $output->writeln('<error>Error while reading json</error>'); |
|
| 122 | - return 1; |
|
| 123 | - } |
|
| 124 | - $data = json_decode($json, true); |
|
| 125 | - if (!is_array($data)) { |
|
| 126 | - $output->writeln('<error>Error while parsing json</error>'); |
|
| 127 | - return 1; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $isLegacy = isset($data['user']) || isset($data['group']); |
|
| 131 | - if ($isLegacy) { |
|
| 132 | - $this->importLegacyStorageService->setData($data); |
|
| 133 | - $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
| 134 | - foreach ($mounts as $mount) { |
|
| 135 | - if ($mount->getBackendOption('password') === false) { |
|
| 136 | - $output->writeln('<error>Failed to decrypt password</error>'); |
|
| 137 | - return 1; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - } else { |
|
| 141 | - if (!isset($data[0])) { //normalize to an array of mounts |
|
| 142 | - $data = [$data]; |
|
| 143 | - } |
|
| 144 | - $mounts = array_map([$this, 'parseData'], $data); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - if ($user) { |
|
| 148 | - // ensure applicables are correct for personal mounts |
|
| 149 | - foreach ($mounts as $mount) { |
|
| 150 | - $mount->setApplicableGroups([]); |
|
| 151 | - $mount->setApplicableUsers([$user]); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $storageService = $this->getStorageService($user); |
|
| 156 | - |
|
| 157 | - $existingMounts = $storageService->getAllStorages(); |
|
| 158 | - |
|
| 159 | - foreach ($mounts as $mount) { |
|
| 160 | - foreach ($existingMounts as $existingMount) { |
|
| 161 | - if ( |
|
| 162 | - $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
| 163 | - $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
| 164 | - $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
|
| 165 | - $existingMount->getBackendOptions() === $mount->getBackendOptions() |
|
| 166 | - ) { |
|
| 167 | - $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
| 168 | - return 1; |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if ($input->getOption('dry')) { |
|
| 174 | - if (count($mounts) === 0) { |
|
| 175 | - $output->writeln('<error>No mounts to be imported</error>'); |
|
| 176 | - return 1; |
|
| 177 | - } |
|
| 178 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 179 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 180 | - $listInput->setOption('output', $input->getOption('output')); |
|
| 181 | - $listInput->setOption('show-password', true); |
|
| 182 | - $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
| 183 | - } else { |
|
| 184 | - foreach ($mounts as $mount) { |
|
| 185 | - $storageService->addStorage($mount); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - return 0; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - private function parseData(array $data) { |
|
| 192 | - $mount = new StorageConfig($data['mount_id']); |
|
| 193 | - $mount->setMountPoint($data['mount_point']); |
|
| 194 | - $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
| 195 | - $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
| 196 | - $mount->setAuthMechanism($authBackend); |
|
| 197 | - $mount->setBackendOptions($data['configuration']); |
|
| 198 | - $mount->setMountOptions($data['options']); |
|
| 199 | - $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
| 200 | - $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
| 201 | - return $mount; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - private function getBackendByClass($className) { |
|
| 205 | - $backends = $this->backendService->getBackends(); |
|
| 206 | - foreach ($backends as $backend) { |
|
| 207 | - if ($backend->getStorageClass() === $className) { |
|
| 208 | - return $backend; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - protected function getStorageService($userId) { |
|
| 214 | - if (!empty($userId)) { |
|
| 215 | - $user = $this->userManager->get($userId); |
|
| 216 | - if (is_null($user)) { |
|
| 217 | - throw new NoUserException("user $userId not found"); |
|
| 218 | - } |
|
| 219 | - $this->userSession->setUser($user); |
|
| 220 | - return $this->userService; |
|
| 221 | - } else { |
|
| 222 | - return $this->globalService; |
|
| 223 | - } |
|
| 224 | - } |
|
| 42 | + /** |
|
| 43 | + * @var GlobalStoragesService |
|
| 44 | + */ |
|
| 45 | + private $globalService; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var UserStoragesService |
|
| 49 | + */ |
|
| 50 | + private $userService; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var IUserSession |
|
| 54 | + */ |
|
| 55 | + private $userSession; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var IUserManager |
|
| 59 | + */ |
|
| 60 | + private $userManager; |
|
| 61 | + |
|
| 62 | + /** @var ImportLegacyStoragesService */ |
|
| 63 | + private $importLegacyStorageService; |
|
| 64 | + |
|
| 65 | + /** @var BackendService */ |
|
| 66 | + private $backendService; |
|
| 67 | + |
|
| 68 | + function __construct(GlobalStoragesService $globalService, |
|
| 69 | + UserStoragesService $userService, |
|
| 70 | + IUserSession $userSession, |
|
| 71 | + IUserManager $userManager, |
|
| 72 | + ImportLegacyStoragesService $importLegacyStorageService, |
|
| 73 | + BackendService $backendService |
|
| 74 | + ) { |
|
| 75 | + parent::__construct(); |
|
| 76 | + $this->globalService = $globalService; |
|
| 77 | + $this->userService = $userService; |
|
| 78 | + $this->userSession = $userSession; |
|
| 79 | + $this->userManager = $userManager; |
|
| 80 | + $this->importLegacyStorageService = $importLegacyStorageService; |
|
| 81 | + $this->backendService = $backendService; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + protected function configure() { |
|
| 85 | + $this |
|
| 86 | + ->setName('files_external:import') |
|
| 87 | + ->setDescription('Import mount configurations') |
|
| 88 | + ->addOption( |
|
| 89 | + 'user', |
|
| 90 | + null, |
|
| 91 | + InputOption::VALUE_OPTIONAL, |
|
| 92 | + 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
| 93 | + ) |
|
| 94 | + ->addArgument( |
|
| 95 | + 'path', |
|
| 96 | + InputArgument::REQUIRED, |
|
| 97 | + 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
| 98 | + ) |
|
| 99 | + ->addOption( |
|
| 100 | + 'dry', |
|
| 101 | + null, |
|
| 102 | + InputOption::VALUE_NONE, |
|
| 103 | + 'Don\'t save the imported mounts, only list the new mounts' |
|
| 104 | + ); |
|
| 105 | + parent::configure(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 109 | + $user = $input->getOption('user'); |
|
| 110 | + $path = $input->getArgument('path'); |
|
| 111 | + if ($path === '-') { |
|
| 112 | + $json = file_get_contents('php://stdin'); |
|
| 113 | + } else { |
|
| 114 | + if (!file_exists($path)) { |
|
| 115 | + $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
| 116 | + return 1; |
|
| 117 | + } |
|
| 118 | + $json = file_get_contents($path); |
|
| 119 | + } |
|
| 120 | + if (!is_string($json) || strlen($json) < 2) { |
|
| 121 | + $output->writeln('<error>Error while reading json</error>'); |
|
| 122 | + return 1; |
|
| 123 | + } |
|
| 124 | + $data = json_decode($json, true); |
|
| 125 | + if (!is_array($data)) { |
|
| 126 | + $output->writeln('<error>Error while parsing json</error>'); |
|
| 127 | + return 1; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $isLegacy = isset($data['user']) || isset($data['group']); |
|
| 131 | + if ($isLegacy) { |
|
| 132 | + $this->importLegacyStorageService->setData($data); |
|
| 133 | + $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
| 134 | + foreach ($mounts as $mount) { |
|
| 135 | + if ($mount->getBackendOption('password') === false) { |
|
| 136 | + $output->writeln('<error>Failed to decrypt password</error>'); |
|
| 137 | + return 1; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + } else { |
|
| 141 | + if (!isset($data[0])) { //normalize to an array of mounts |
|
| 142 | + $data = [$data]; |
|
| 143 | + } |
|
| 144 | + $mounts = array_map([$this, 'parseData'], $data); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + if ($user) { |
|
| 148 | + // ensure applicables are correct for personal mounts |
|
| 149 | + foreach ($mounts as $mount) { |
|
| 150 | + $mount->setApplicableGroups([]); |
|
| 151 | + $mount->setApplicableUsers([$user]); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $storageService = $this->getStorageService($user); |
|
| 156 | + |
|
| 157 | + $existingMounts = $storageService->getAllStorages(); |
|
| 158 | + |
|
| 159 | + foreach ($mounts as $mount) { |
|
| 160 | + foreach ($existingMounts as $existingMount) { |
|
| 161 | + if ( |
|
| 162 | + $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
| 163 | + $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
| 164 | + $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
|
| 165 | + $existingMount->getBackendOptions() === $mount->getBackendOptions() |
|
| 166 | + ) { |
|
| 167 | + $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
| 168 | + return 1; |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if ($input->getOption('dry')) { |
|
| 174 | + if (count($mounts) === 0) { |
|
| 175 | + $output->writeln('<error>No mounts to be imported</error>'); |
|
| 176 | + return 1; |
|
| 177 | + } |
|
| 178 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 179 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 180 | + $listInput->setOption('output', $input->getOption('output')); |
|
| 181 | + $listInput->setOption('show-password', true); |
|
| 182 | + $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
| 183 | + } else { |
|
| 184 | + foreach ($mounts as $mount) { |
|
| 185 | + $storageService->addStorage($mount); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + return 0; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + private function parseData(array $data) { |
|
| 192 | + $mount = new StorageConfig($data['mount_id']); |
|
| 193 | + $mount->setMountPoint($data['mount_point']); |
|
| 194 | + $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
| 195 | + $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
| 196 | + $mount->setAuthMechanism($authBackend); |
|
| 197 | + $mount->setBackendOptions($data['configuration']); |
|
| 198 | + $mount->setMountOptions($data['options']); |
|
| 199 | + $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
| 200 | + $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
| 201 | + return $mount; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + private function getBackendByClass($className) { |
|
| 205 | + $backends = $this->backendService->getBackends(); |
|
| 206 | + foreach ($backends as $backend) { |
|
| 207 | + if ($backend->getStorageClass() === $className) { |
|
| 208 | + return $backend; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + protected function getStorageService($userId) { |
|
| 214 | + if (!empty($userId)) { |
|
| 215 | + $user = $this->userManager->get($userId); |
|
| 216 | + if (is_null($user)) { |
|
| 217 | + throw new NoUserException("user $userId not found"); |
|
| 218 | + } |
|
| 219 | + $this->userSession->setUser($user); |
|
| 220 | + return $this->userService; |
|
| 221 | + } else { |
|
| 222 | + return $this->globalService; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | 225 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | $json = file_get_contents('php://stdin'); |
| 113 | 113 | } else { |
| 114 | 114 | if (!file_exists($path)) { |
| 115 | - $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
| 115 | + $output->writeln('<error>File not found: '.$path.'</error>'); |
|
| 116 | 116 | return 1; |
| 117 | 117 | } |
| 118 | 118 | $json = file_get_contents($path); |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
| 165 | 165 | $existingMount->getBackendOptions() === $mount->getBackendOptions() |
| 166 | 166 | ) { |
| 167 | - $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
| 167 | + $output->writeln("<error>Duplicate mount (".$mount->getMountPoint().")</error>"); |
|
| 168 | 168 | return 1; |
| 169 | 169 | } |
| 170 | 170 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | */ |
| 30 | 30 | set_include_path(get_include_path().PATH_SEPARATOR. |
| 31 | - \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); |
|
| 31 | + \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); |
|
| 32 | 32 | require_once 'Google/autoload.php'; |
| 33 | 33 | |
| 34 | 34 | OCP\JSON::checkAppEnabled('files_external'); |
@@ -38,37 +38,37 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | // FIXME: currently hard-coded to Google Drive |
| 40 | 40 | if (isset($_POST['client_id']) && isset($_POST['client_secret']) && isset($_POST['redirect'])) { |
| 41 | - $client = new Google_Client(); |
|
| 42 | - $client->setClientId((string)$_POST['client_id']); |
|
| 43 | - $client->setClientSecret((string)$_POST['client_secret']); |
|
| 44 | - $client->setRedirectUri((string)$_POST['redirect']); |
|
| 45 | - $client->setScopes(array('https://www.googleapis.com/auth/drive')); |
|
| 46 | - $client->setApprovalPrompt('force'); |
|
| 47 | - $client->setAccessType('offline'); |
|
| 48 | - if (isset($_POST['step'])) { |
|
| 49 | - $step = (int) $_POST['step']; |
|
| 50 | - if ($step === 1) { |
|
| 51 | - try { |
|
| 52 | - $authUrl = $client->createAuthUrl(); |
|
| 53 | - OCP\JSON::success(array('data' => array( |
|
| 54 | - 'url' => $authUrl |
|
| 55 | - ))); |
|
| 56 | - } catch (Exception $exception) { |
|
| 57 | - OCP\JSON::error(array('data' => array( |
|
| 58 | - 'message' => $l->t('Step 1 failed. Exception: %s', array($exception->getMessage())) |
|
| 59 | - ))); |
|
| 60 | - } |
|
| 61 | - } else if ($step === 2 && isset($_POST['code'])) { |
|
| 62 | - try { |
|
| 63 | - $token = $client->authenticate((string)$_POST['code']); |
|
| 64 | - OCP\JSON::success(array('data' => array( |
|
| 65 | - 'token' => $token |
|
| 66 | - ))); |
|
| 67 | - } catch (Exception $exception) { |
|
| 68 | - OCP\JSON::error(array('data' => array( |
|
| 69 | - 'message' => $l->t('Step 2 failed. Exception: %s', array($exception->getMessage())) |
|
| 70 | - ))); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - } |
|
| 41 | + $client = new Google_Client(); |
|
| 42 | + $client->setClientId((string)$_POST['client_id']); |
|
| 43 | + $client->setClientSecret((string)$_POST['client_secret']); |
|
| 44 | + $client->setRedirectUri((string)$_POST['redirect']); |
|
| 45 | + $client->setScopes(array('https://www.googleapis.com/auth/drive')); |
|
| 46 | + $client->setApprovalPrompt('force'); |
|
| 47 | + $client->setAccessType('offline'); |
|
| 48 | + if (isset($_POST['step'])) { |
|
| 49 | + $step = (int) $_POST['step']; |
|
| 50 | + if ($step === 1) { |
|
| 51 | + try { |
|
| 52 | + $authUrl = $client->createAuthUrl(); |
|
| 53 | + OCP\JSON::success(array('data' => array( |
|
| 54 | + 'url' => $authUrl |
|
| 55 | + ))); |
|
| 56 | + } catch (Exception $exception) { |
|
| 57 | + OCP\JSON::error(array('data' => array( |
|
| 58 | + 'message' => $l->t('Step 1 failed. Exception: %s', array($exception->getMessage())) |
|
| 59 | + ))); |
|
| 60 | + } |
|
| 61 | + } else if ($step === 2 && isset($_POST['code'])) { |
|
| 62 | + try { |
|
| 63 | + $token = $client->authenticate((string)$_POST['code']); |
|
| 64 | + OCP\JSON::success(array('data' => array( |
|
| 65 | + 'token' => $token |
|
| 66 | + ))); |
|
| 67 | + } catch (Exception $exception) { |
|
| 68 | + OCP\JSON::error(array('data' => array( |
|
| 69 | + 'message' => $l->t('Step 2 failed. Exception: %s', array($exception->getMessage())) |
|
| 70 | + ))); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -39,9 +39,9 @@ discard block |
||
| 39 | 39 | // FIXME: currently hard-coded to Google Drive |
| 40 | 40 | if (isset($_POST['client_id']) && isset($_POST['client_secret']) && isset($_POST['redirect'])) { |
| 41 | 41 | $client = new Google_Client(); |
| 42 | - $client->setClientId((string)$_POST['client_id']); |
|
| 43 | - $client->setClientSecret((string)$_POST['client_secret']); |
|
| 44 | - $client->setRedirectUri((string)$_POST['redirect']); |
|
| 42 | + $client->setClientId((string) $_POST['client_id']); |
|
| 43 | + $client->setClientSecret((string) $_POST['client_secret']); |
|
| 44 | + $client->setRedirectUri((string) $_POST['redirect']); |
|
| 45 | 45 | $client->setScopes(array('https://www.googleapis.com/auth/drive')); |
| 46 | 46 | $client->setApprovalPrompt('force'); |
| 47 | 47 | $client->setAccessType('offline'); |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | } else if ($step === 2 && isset($_POST['code'])) { |
| 62 | 62 | try { |
| 63 | - $token = $client->authenticate((string)$_POST['code']); |
|
| 63 | + $token = $client->authenticate((string) $_POST['code']); |
|
| 64 | 64 | OCP\JSON::success(array('data' => array( |
| 65 | 65 | 'token' => $token |
| 66 | 66 | ))); |