@@ -36,242 +36,242 @@ |
||
| 36 | 36 | * @package OC\AppFramework\routing |
| 37 | 37 | */ |
| 38 | 38 | class RouteConfig { |
| 39 | - /** @var DIContainer */ |
|
| 40 | - private $container; |
|
| 41 | - |
|
| 42 | - /** @var IRouter */ |
|
| 43 | - private $router; |
|
| 44 | - |
|
| 45 | - /** @var array */ |
|
| 46 | - private $routes; |
|
| 47 | - |
|
| 48 | - /** @var string */ |
|
| 49 | - private $appName; |
|
| 50 | - |
|
| 51 | - /** @var string[] */ |
|
| 52 | - private $controllerNameCache = []; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param \OC\AppFramework\DependencyInjection\DIContainer $container |
|
| 56 | - * @param \OCP\Route\IRouter $router |
|
| 57 | - * @param array $routes |
|
| 58 | - * @internal param $appName |
|
| 59 | - */ |
|
| 60 | - public function __construct(DIContainer $container, IRouter $router, $routes) { |
|
| 61 | - $this->routes = $routes; |
|
| 62 | - $this->container = $container; |
|
| 63 | - $this->router = $router; |
|
| 64 | - $this->appName = $container['AppName']; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The routes and resource will be registered to the \OCP\Route\IRouter |
|
| 69 | - */ |
|
| 70 | - public function register() { |
|
| 71 | - |
|
| 72 | - // parse simple |
|
| 73 | - $this->processSimpleRoutes($this->routes); |
|
| 74 | - |
|
| 75 | - // parse resources |
|
| 76 | - $this->processResources($this->routes); |
|
| 77 | - |
|
| 78 | - /* |
|
| 39 | + /** @var DIContainer */ |
|
| 40 | + private $container; |
|
| 41 | + |
|
| 42 | + /** @var IRouter */ |
|
| 43 | + private $router; |
|
| 44 | + |
|
| 45 | + /** @var array */ |
|
| 46 | + private $routes; |
|
| 47 | + |
|
| 48 | + /** @var string */ |
|
| 49 | + private $appName; |
|
| 50 | + |
|
| 51 | + /** @var string[] */ |
|
| 52 | + private $controllerNameCache = []; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param \OC\AppFramework\DependencyInjection\DIContainer $container |
|
| 56 | + * @param \OCP\Route\IRouter $router |
|
| 57 | + * @param array $routes |
|
| 58 | + * @internal param $appName |
|
| 59 | + */ |
|
| 60 | + public function __construct(DIContainer $container, IRouter $router, $routes) { |
|
| 61 | + $this->routes = $routes; |
|
| 62 | + $this->container = $container; |
|
| 63 | + $this->router = $router; |
|
| 64 | + $this->appName = $container['AppName']; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The routes and resource will be registered to the \OCP\Route\IRouter |
|
| 69 | + */ |
|
| 70 | + public function register() { |
|
| 71 | + |
|
| 72 | + // parse simple |
|
| 73 | + $this->processSimpleRoutes($this->routes); |
|
| 74 | + |
|
| 75 | + // parse resources |
|
| 76 | + $this->processResources($this->routes); |
|
| 77 | + |
|
| 78 | + /* |
|
| 79 | 79 | * OCS routes go into a different collection |
| 80 | 80 | */ |
| 81 | - $oldCollection = $this->router->getCurrentCollection(); |
|
| 82 | - $this->router->useCollection($oldCollection.'.ocs'); |
|
| 83 | - |
|
| 84 | - // parse ocs simple routes |
|
| 85 | - $this->processOCS($this->routes); |
|
| 86 | - |
|
| 87 | - $this->router->useCollection($oldCollection); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - private function processOCS(array $routes) { |
|
| 91 | - $ocsRoutes = isset($routes['ocs']) ? $routes['ocs'] : []; |
|
| 92 | - foreach ($ocsRoutes as $ocsRoute) { |
|
| 93 | - $name = $ocsRoute['name']; |
|
| 94 | - $postfix = ''; |
|
| 95 | - |
|
| 96 | - if (isset($ocsRoute['postfix'])) { |
|
| 97 | - $postfix = $ocsRoute['postfix']; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - if (isset($ocsRoute['root'])) { |
|
| 101 | - $root = $ocsRoute['root']; |
|
| 102 | - } else { |
|
| 103 | - $root = '/apps/'.$this->appName; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $url = $root . $ocsRoute['url']; |
|
| 107 | - $verb = isset($ocsRoute['verb']) ? strtoupper($ocsRoute['verb']) : 'GET'; |
|
| 108 | - |
|
| 109 | - $split = explode('#', $name, 2); |
|
| 110 | - if (count($split) != 2) { |
|
| 111 | - throw new \UnexpectedValueException('Invalid route name'); |
|
| 112 | - } |
|
| 113 | - $controller = $split[0]; |
|
| 114 | - $action = $split[1]; |
|
| 115 | - |
|
| 116 | - $controllerName = $this->buildControllerName($controller); |
|
| 117 | - $actionName = $this->buildActionName($action); |
|
| 118 | - |
|
| 119 | - // register the route |
|
| 120 | - $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 121 | - |
|
| 122 | - $router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action . $postfix, $url) |
|
| 123 | - ->method($verb) |
|
| 124 | - ->action($handler); |
|
| 125 | - |
|
| 126 | - // optionally register requirements for route. This is used to |
|
| 127 | - // tell the route parser how url parameters should be matched |
|
| 128 | - if(array_key_exists('requirements', $ocsRoute)) { |
|
| 129 | - $router->requirements($ocsRoute['requirements']); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // optionally register defaults for route. This is used to |
|
| 133 | - // tell the route parser how url parameters should be default valued |
|
| 134 | - if(array_key_exists('defaults', $ocsRoute)) { |
|
| 135 | - $router->defaults($ocsRoute['defaults']); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Creates one route base on the give configuration |
|
| 142 | - * @param array $routes |
|
| 143 | - * @throws \UnexpectedValueException |
|
| 144 | - */ |
|
| 145 | - private function processSimpleRoutes($routes) |
|
| 146 | - { |
|
| 147 | - $simpleRoutes = isset($routes['routes']) ? $routes['routes'] : array(); |
|
| 148 | - foreach ($simpleRoutes as $simpleRoute) { |
|
| 149 | - $name = $simpleRoute['name']; |
|
| 150 | - $postfix = ''; |
|
| 151 | - |
|
| 152 | - if (isset($simpleRoute['postfix'])) { |
|
| 153 | - $postfix = $simpleRoute['postfix']; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $url = $simpleRoute['url']; |
|
| 157 | - $verb = isset($simpleRoute['verb']) ? strtoupper($simpleRoute['verb']) : 'GET'; |
|
| 158 | - |
|
| 159 | - $split = explode('#', $name, 2); |
|
| 160 | - if (count($split) != 2) { |
|
| 161 | - throw new \UnexpectedValueException('Invalid route name'); |
|
| 162 | - } |
|
| 163 | - $controller = $split[0]; |
|
| 164 | - $action = $split[1]; |
|
| 165 | - |
|
| 166 | - $controllerName = $this->buildControllerName($controller); |
|
| 167 | - $actionName = $this->buildActionName($action); |
|
| 168 | - |
|
| 169 | - // register the route |
|
| 170 | - $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 171 | - $router = $this->router->create($this->appName.'.'.$controller.'.'.$action . $postfix, $url) |
|
| 172 | - ->method($verb) |
|
| 173 | - ->action($handler); |
|
| 174 | - |
|
| 175 | - // optionally register requirements for route. This is used to |
|
| 176 | - // tell the route parser how url parameters should be matched |
|
| 177 | - if(array_key_exists('requirements', $simpleRoute)) { |
|
| 178 | - $router->requirements($simpleRoute['requirements']); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // optionally register defaults for route. This is used to |
|
| 182 | - // tell the route parser how url parameters should be default valued |
|
| 183 | - if(array_key_exists('defaults', $simpleRoute)) { |
|
| 184 | - $router->defaults($simpleRoute['defaults']); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * For a given name and url restful routes are created: |
|
| 191 | - * - index |
|
| 192 | - * - show |
|
| 193 | - * - new |
|
| 194 | - * - create |
|
| 195 | - * - update |
|
| 196 | - * - destroy |
|
| 197 | - * |
|
| 198 | - * @param array $routes |
|
| 199 | - */ |
|
| 200 | - private function processResources($routes) |
|
| 201 | - { |
|
| 202 | - // declaration of all restful actions |
|
| 203 | - $actions = array( |
|
| 204 | - array('name' => 'index', 'verb' => 'GET', 'on-collection' => true), |
|
| 205 | - array('name' => 'show', 'verb' => 'GET'), |
|
| 206 | - array('name' => 'create', 'verb' => 'POST', 'on-collection' => true), |
|
| 207 | - array('name' => 'update', 'verb' => 'PUT'), |
|
| 208 | - array('name' => 'destroy', 'verb' => 'DELETE'), |
|
| 209 | - ); |
|
| 210 | - |
|
| 211 | - $resources = isset($routes['resources']) ? $routes['resources'] : array(); |
|
| 212 | - foreach ($resources as $resource => $config) { |
|
| 213 | - |
|
| 214 | - // the url parameter used as id to the resource |
|
| 215 | - foreach($actions as $action) { |
|
| 216 | - $url = $config['url']; |
|
| 217 | - $method = $action['name']; |
|
| 218 | - $verb = isset($action['verb']) ? strtoupper($action['verb']) : 'GET'; |
|
| 219 | - $collectionAction = isset($action['on-collection']) ? $action['on-collection'] : false; |
|
| 220 | - if (!$collectionAction) { |
|
| 221 | - $url = $url . '/{id}'; |
|
| 222 | - } |
|
| 223 | - if (isset($action['url-postfix'])) { |
|
| 224 | - $url = $url . '/' . $action['url-postfix']; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - $controller = $resource; |
|
| 228 | - |
|
| 229 | - $controllerName = $this->buildControllerName($controller); |
|
| 230 | - $actionName = $this->buildActionName($method); |
|
| 231 | - |
|
| 232 | - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 233 | - |
|
| 234 | - $this->router->create($routeName, $url)->method($verb)->action( |
|
| 235 | - new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 236 | - ); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Based on a given route name the controller name is generated |
|
| 243 | - * @param string $controller |
|
| 244 | - * @return string |
|
| 245 | - */ |
|
| 246 | - private function buildControllerName($controller) |
|
| 247 | - { |
|
| 248 | - if (!isset($this->controllerNameCache[$controller])) { |
|
| 249 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 250 | - } |
|
| 251 | - return $this->controllerNameCache[$controller]; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Based on the action part of the route name the controller method name is generated |
|
| 256 | - * @param string $action |
|
| 257 | - * @return string |
|
| 258 | - */ |
|
| 259 | - private function buildActionName($action) { |
|
| 260 | - return $this->underScoreToCamelCase($action); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Underscored strings are converted to camel case strings |
|
| 265 | - * @param string $str |
|
| 266 | - * @return string |
|
| 267 | - */ |
|
| 268 | - private function underScoreToCamelCase($str) { |
|
| 269 | - $pattern = "/_[a-z]?/"; |
|
| 270 | - return preg_replace_callback( |
|
| 271 | - $pattern, |
|
| 272 | - function ($matches) { |
|
| 273 | - return strtoupper(ltrim($matches[0], "_")); |
|
| 274 | - }, |
|
| 275 | - $str); |
|
| 276 | - } |
|
| 81 | + $oldCollection = $this->router->getCurrentCollection(); |
|
| 82 | + $this->router->useCollection($oldCollection.'.ocs'); |
|
| 83 | + |
|
| 84 | + // parse ocs simple routes |
|
| 85 | + $this->processOCS($this->routes); |
|
| 86 | + |
|
| 87 | + $this->router->useCollection($oldCollection); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + private function processOCS(array $routes) { |
|
| 91 | + $ocsRoutes = isset($routes['ocs']) ? $routes['ocs'] : []; |
|
| 92 | + foreach ($ocsRoutes as $ocsRoute) { |
|
| 93 | + $name = $ocsRoute['name']; |
|
| 94 | + $postfix = ''; |
|
| 95 | + |
|
| 96 | + if (isset($ocsRoute['postfix'])) { |
|
| 97 | + $postfix = $ocsRoute['postfix']; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + if (isset($ocsRoute['root'])) { |
|
| 101 | + $root = $ocsRoute['root']; |
|
| 102 | + } else { |
|
| 103 | + $root = '/apps/'.$this->appName; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $url = $root . $ocsRoute['url']; |
|
| 107 | + $verb = isset($ocsRoute['verb']) ? strtoupper($ocsRoute['verb']) : 'GET'; |
|
| 108 | + |
|
| 109 | + $split = explode('#', $name, 2); |
|
| 110 | + if (count($split) != 2) { |
|
| 111 | + throw new \UnexpectedValueException('Invalid route name'); |
|
| 112 | + } |
|
| 113 | + $controller = $split[0]; |
|
| 114 | + $action = $split[1]; |
|
| 115 | + |
|
| 116 | + $controllerName = $this->buildControllerName($controller); |
|
| 117 | + $actionName = $this->buildActionName($action); |
|
| 118 | + |
|
| 119 | + // register the route |
|
| 120 | + $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 121 | + |
|
| 122 | + $router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action . $postfix, $url) |
|
| 123 | + ->method($verb) |
|
| 124 | + ->action($handler); |
|
| 125 | + |
|
| 126 | + // optionally register requirements for route. This is used to |
|
| 127 | + // tell the route parser how url parameters should be matched |
|
| 128 | + if(array_key_exists('requirements', $ocsRoute)) { |
|
| 129 | + $router->requirements($ocsRoute['requirements']); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // optionally register defaults for route. This is used to |
|
| 133 | + // tell the route parser how url parameters should be default valued |
|
| 134 | + if(array_key_exists('defaults', $ocsRoute)) { |
|
| 135 | + $router->defaults($ocsRoute['defaults']); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Creates one route base on the give configuration |
|
| 142 | + * @param array $routes |
|
| 143 | + * @throws \UnexpectedValueException |
|
| 144 | + */ |
|
| 145 | + private function processSimpleRoutes($routes) |
|
| 146 | + { |
|
| 147 | + $simpleRoutes = isset($routes['routes']) ? $routes['routes'] : array(); |
|
| 148 | + foreach ($simpleRoutes as $simpleRoute) { |
|
| 149 | + $name = $simpleRoute['name']; |
|
| 150 | + $postfix = ''; |
|
| 151 | + |
|
| 152 | + if (isset($simpleRoute['postfix'])) { |
|
| 153 | + $postfix = $simpleRoute['postfix']; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $url = $simpleRoute['url']; |
|
| 157 | + $verb = isset($simpleRoute['verb']) ? strtoupper($simpleRoute['verb']) : 'GET'; |
|
| 158 | + |
|
| 159 | + $split = explode('#', $name, 2); |
|
| 160 | + if (count($split) != 2) { |
|
| 161 | + throw new \UnexpectedValueException('Invalid route name'); |
|
| 162 | + } |
|
| 163 | + $controller = $split[0]; |
|
| 164 | + $action = $split[1]; |
|
| 165 | + |
|
| 166 | + $controllerName = $this->buildControllerName($controller); |
|
| 167 | + $actionName = $this->buildActionName($action); |
|
| 168 | + |
|
| 169 | + // register the route |
|
| 170 | + $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 171 | + $router = $this->router->create($this->appName.'.'.$controller.'.'.$action . $postfix, $url) |
|
| 172 | + ->method($verb) |
|
| 173 | + ->action($handler); |
|
| 174 | + |
|
| 175 | + // optionally register requirements for route. This is used to |
|
| 176 | + // tell the route parser how url parameters should be matched |
|
| 177 | + if(array_key_exists('requirements', $simpleRoute)) { |
|
| 178 | + $router->requirements($simpleRoute['requirements']); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // optionally register defaults for route. This is used to |
|
| 182 | + // tell the route parser how url parameters should be default valued |
|
| 183 | + if(array_key_exists('defaults', $simpleRoute)) { |
|
| 184 | + $router->defaults($simpleRoute['defaults']); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * For a given name and url restful routes are created: |
|
| 191 | + * - index |
|
| 192 | + * - show |
|
| 193 | + * - new |
|
| 194 | + * - create |
|
| 195 | + * - update |
|
| 196 | + * - destroy |
|
| 197 | + * |
|
| 198 | + * @param array $routes |
|
| 199 | + */ |
|
| 200 | + private function processResources($routes) |
|
| 201 | + { |
|
| 202 | + // declaration of all restful actions |
|
| 203 | + $actions = array( |
|
| 204 | + array('name' => 'index', 'verb' => 'GET', 'on-collection' => true), |
|
| 205 | + array('name' => 'show', 'verb' => 'GET'), |
|
| 206 | + array('name' => 'create', 'verb' => 'POST', 'on-collection' => true), |
|
| 207 | + array('name' => 'update', 'verb' => 'PUT'), |
|
| 208 | + array('name' => 'destroy', 'verb' => 'DELETE'), |
|
| 209 | + ); |
|
| 210 | + |
|
| 211 | + $resources = isset($routes['resources']) ? $routes['resources'] : array(); |
|
| 212 | + foreach ($resources as $resource => $config) { |
|
| 213 | + |
|
| 214 | + // the url parameter used as id to the resource |
|
| 215 | + foreach($actions as $action) { |
|
| 216 | + $url = $config['url']; |
|
| 217 | + $method = $action['name']; |
|
| 218 | + $verb = isset($action['verb']) ? strtoupper($action['verb']) : 'GET'; |
|
| 219 | + $collectionAction = isset($action['on-collection']) ? $action['on-collection'] : false; |
|
| 220 | + if (!$collectionAction) { |
|
| 221 | + $url = $url . '/{id}'; |
|
| 222 | + } |
|
| 223 | + if (isset($action['url-postfix'])) { |
|
| 224 | + $url = $url . '/' . $action['url-postfix']; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + $controller = $resource; |
|
| 228 | + |
|
| 229 | + $controllerName = $this->buildControllerName($controller); |
|
| 230 | + $actionName = $this->buildActionName($method); |
|
| 231 | + |
|
| 232 | + $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 233 | + |
|
| 234 | + $this->router->create($routeName, $url)->method($verb)->action( |
|
| 235 | + new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 236 | + ); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Based on a given route name the controller name is generated |
|
| 243 | + * @param string $controller |
|
| 244 | + * @return string |
|
| 245 | + */ |
|
| 246 | + private function buildControllerName($controller) |
|
| 247 | + { |
|
| 248 | + if (!isset($this->controllerNameCache[$controller])) { |
|
| 249 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 250 | + } |
|
| 251 | + return $this->controllerNameCache[$controller]; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Based on the action part of the route name the controller method name is generated |
|
| 256 | + * @param string $action |
|
| 257 | + * @return string |
|
| 258 | + */ |
|
| 259 | + private function buildActionName($action) { |
|
| 260 | + return $this->underScoreToCamelCase($action); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Underscored strings are converted to camel case strings |
|
| 265 | + * @param string $str |
|
| 266 | + * @return string |
|
| 267 | + */ |
|
| 268 | + private function underScoreToCamelCase($str) { |
|
| 269 | + $pattern = "/_[a-z]?/"; |
|
| 270 | + return preg_replace_callback( |
|
| 271 | + $pattern, |
|
| 272 | + function ($matches) { |
|
| 273 | + return strtoupper(ltrim($matches[0], "_")); |
|
| 274 | + }, |
|
| 275 | + $str); |
|
| 276 | + } |
|
| 277 | 277 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $root = '/apps/'.$this->appName; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - $url = $root . $ocsRoute['url']; |
|
| 106 | + $url = $root.$ocsRoute['url']; |
|
| 107 | 107 | $verb = isset($ocsRoute['verb']) ? strtoupper($ocsRoute['verb']) : 'GET'; |
| 108 | 108 | |
| 109 | 109 | $split = explode('#', $name, 2); |
@@ -119,19 +119,19 @@ discard block |
||
| 119 | 119 | // register the route |
| 120 | 120 | $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
| 121 | 121 | |
| 122 | - $router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action . $postfix, $url) |
|
| 122 | + $router = $this->router->create('ocs.'.$this->appName.'.'.$controller.'.'.$action.$postfix, $url) |
|
| 123 | 123 | ->method($verb) |
| 124 | 124 | ->action($handler); |
| 125 | 125 | |
| 126 | 126 | // optionally register requirements for route. This is used to |
| 127 | 127 | // tell the route parser how url parameters should be matched |
| 128 | - if(array_key_exists('requirements', $ocsRoute)) { |
|
| 128 | + if (array_key_exists('requirements', $ocsRoute)) { |
|
| 129 | 129 | $router->requirements($ocsRoute['requirements']); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | // optionally register defaults for route. This is used to |
| 133 | 133 | // tell the route parser how url parameters should be default valued |
| 134 | - if(array_key_exists('defaults', $ocsRoute)) { |
|
| 134 | + if (array_key_exists('defaults', $ocsRoute)) { |
|
| 135 | 135 | $router->defaults($ocsRoute['defaults']); |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -168,19 +168,19 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | // register the route |
| 170 | 170 | $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
| 171 | - $router = $this->router->create($this->appName.'.'.$controller.'.'.$action . $postfix, $url) |
|
| 171 | + $router = $this->router->create($this->appName.'.'.$controller.'.'.$action.$postfix, $url) |
|
| 172 | 172 | ->method($verb) |
| 173 | 173 | ->action($handler); |
| 174 | 174 | |
| 175 | 175 | // optionally register requirements for route. This is used to |
| 176 | 176 | // tell the route parser how url parameters should be matched |
| 177 | - if(array_key_exists('requirements', $simpleRoute)) { |
|
| 177 | + if (array_key_exists('requirements', $simpleRoute)) { |
|
| 178 | 178 | $router->requirements($simpleRoute['requirements']); |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // optionally register defaults for route. This is used to |
| 182 | 182 | // tell the route parser how url parameters should be default valued |
| 183 | - if(array_key_exists('defaults', $simpleRoute)) { |
|
| 183 | + if (array_key_exists('defaults', $simpleRoute)) { |
|
| 184 | 184 | $router->defaults($simpleRoute['defaults']); |
| 185 | 185 | } |
| 186 | 186 | } |
@@ -212,16 +212,16 @@ discard block |
||
| 212 | 212 | foreach ($resources as $resource => $config) { |
| 213 | 213 | |
| 214 | 214 | // the url parameter used as id to the resource |
| 215 | - foreach($actions as $action) { |
|
| 215 | + foreach ($actions as $action) { |
|
| 216 | 216 | $url = $config['url']; |
| 217 | 217 | $method = $action['name']; |
| 218 | 218 | $verb = isset($action['verb']) ? strtoupper($action['verb']) : 'GET'; |
| 219 | 219 | $collectionAction = isset($action['on-collection']) ? $action['on-collection'] : false; |
| 220 | 220 | if (!$collectionAction) { |
| 221 | - $url = $url . '/{id}'; |
|
| 221 | + $url = $url.'/{id}'; |
|
| 222 | 222 | } |
| 223 | 223 | if (isset($action['url-postfix'])) { |
| 224 | - $url = $url . '/' . $action['url-postfix']; |
|
| 224 | + $url = $url.'/'.$action['url-postfix']; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | $controller = $resource; |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | $controllerName = $this->buildControllerName($controller); |
| 230 | 230 | $actionName = $this->buildActionName($method); |
| 231 | 231 | |
| 232 | - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 232 | + $routeName = $this->appName.'.'.strtolower($resource).'.'.strtolower($method); |
|
| 233 | 233 | |
| 234 | 234 | $this->router->create($routeName, $url)->method($verb)->action( |
| 235 | 235 | new RouteActionHandler($this->container, $controllerName, $actionName) |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | private function buildControllerName($controller) |
| 247 | 247 | { |
| 248 | 248 | if (!isset($this->controllerNameCache[$controller])) { |
| 249 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 249 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)).'Controller'; |
|
| 250 | 250 | } |
| 251 | 251 | return $this->controllerNameCache[$controller]; |
| 252 | 252 | } |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | $pattern = "/_[a-z]?/"; |
| 270 | 270 | return preg_replace_callback( |
| 271 | 271 | $pattern, |
| 272 | - function ($matches) { |
|
| 272 | + function($matches) { |
|
| 273 | 273 | return strtoupper(ltrim($matches[0], "_")); |
| 274 | 274 | }, |
| 275 | 275 | $str); |
@@ -36,76 +36,76 @@ |
||
| 36 | 36 | |
| 37 | 37 | $application = new Application(); |
| 38 | 38 | $application->registerRoutes($this, [ |
| 39 | - 'resources' => [ |
|
| 40 | - 'users' => ['url' => '/settings/users/users'], |
|
| 41 | - 'AuthSettings' => ['url' => '/settings/personal/authtokens'], |
|
| 42 | - ], |
|
| 43 | - 'routes' => [ |
|
| 44 | - ['name' => 'MailSettings#setMailSettings', 'url' => '/settings/admin/mailsettings', 'verb' => 'POST'], |
|
| 45 | - ['name' => 'MailSettings#storeCredentials', 'url' => '/settings/admin/mailsettings/credentials', 'verb' => 'POST'], |
|
| 46 | - ['name' => 'MailSettings#sendTestMail', 'url' => '/settings/admin/mailtest', 'verb' => 'POST'], |
|
| 47 | - ['name' => 'Encryption#startMigration', 'url' => '/settings/admin/startmigration', 'verb' => 'POST'], |
|
| 48 | - ['name' => 'AppSettings#listCategories', 'url' => '/settings/apps/categories', 'verb' => 'GET'], |
|
| 49 | - ['name' => 'AppSettings#viewApps', 'url' => '/settings/apps', 'verb' => 'GET'], |
|
| 50 | - ['name' => 'AppSettings#listApps', 'url' => '/settings/apps/list', 'verb' => 'GET'], |
|
| 51 | - ['name' => 'SecuritySettings#trustedDomains', 'url' => '/settings/admin/security/trustedDomains', 'verb' => 'POST'], |
|
| 52 | - ['name' => 'Users#setDisplayName', 'url' => '/settings/users/{username}/displayName', 'verb' => 'POST'], |
|
| 53 | - ['name' => 'Users#setEMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'], |
|
| 54 | - ['name' => 'Users#setUserSettings', 'url' => '/settings/users/{username}/settings', 'verb' => 'PUT'], |
|
| 55 | - ['name' => 'Users#stats', 'url' => '/settings/users/stats', 'verb' => 'GET'], |
|
| 56 | - ['name' => 'LogSettings#setLogLevel', 'url' => '/settings/admin/log/level', 'verb' => 'POST'], |
|
| 57 | - ['name' => 'LogSettings#getEntries', 'url' => '/settings/admin/log/entries', 'verb' => 'GET'], |
|
| 58 | - ['name' => 'LogSettings#download', 'url' => '/settings/admin/log/download', 'verb' => 'GET'], |
|
| 59 | - ['name' => 'CheckSetup#check', 'url' => '/settings/ajax/checksetup', 'verb' => 'GET'], |
|
| 60 | - ['name' => 'CheckSetup#getFailedIntegrityCheckFiles', 'url' => '/settings/integrity/failed', 'verb' => 'GET'], |
|
| 61 | - ['name' => 'CheckSetup#rescanFailedIntegrityCheck', 'url' => '/settings/integrity/rescan', 'verb' => 'GET'], |
|
| 62 | - ['name' => 'Certificate#addPersonalRootCertificate', 'url' => '/settings/personal/certificate', 'verb' => 'POST'], |
|
| 63 | - ['name' => 'Certificate#removePersonalRootCertificate', 'url' => '/settings/personal/certificate/{certificateIdentifier}', 'verb' => 'DELETE'], |
|
| 64 | - ['name' => 'Certificate#addSystemRootCertificate', 'url' => '/settings/admin/certificate', 'verb' => 'POST'], |
|
| 65 | - ['name' => 'Certificate#removeSystemRootCertificate', 'url' => '/settings/admin/certificate/{certificateIdentifier}', 'verb' => 'DELETE'], |
|
| 66 | - ['name' => 'AdminSettings#index', 'url' => '/settings/admin/{section}', 'verb' => 'GET', 'defaults' => ['section' => 'server']], |
|
| 67 | - ['name' => 'AdminSettings#form', 'url' => '/settings/admin/{section}', 'verb' => 'GET'], |
|
| 68 | - ['name' => 'ChangePassword#changePersonalPassword', 'url' => '/settings/personal/changepassword', 'verb' => 'POST'], |
|
| 69 | - ['name' => 'ChangePassword#changeUserPassword', 'url' => '/settings/users/changepassword', 'verb' => 'POST'], |
|
| 70 | - ['name' => 'Personal#setLanguage', 'url' => '/settings/ajax/setlanguage.php', 'verb' => 'POST'], |
|
| 71 | - ['name' => 'Groups#index', 'url' => '/settings/users/groups', 'verb' => 'GET'], |
|
| 72 | - ['name' => 'Groups#show', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'GET'], |
|
| 73 | - ['name' => 'Groups#create', 'url' => '/settings/users/groups', 'verb' => 'POST'], |
|
| 74 | - ['name' => 'Groups#update', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'PUT'], |
|
| 75 | - ['name' => 'Groups#destroy', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'DELETE'], |
|
| 76 | - ] |
|
| 39 | + 'resources' => [ |
|
| 40 | + 'users' => ['url' => '/settings/users/users'], |
|
| 41 | + 'AuthSettings' => ['url' => '/settings/personal/authtokens'], |
|
| 42 | + ], |
|
| 43 | + 'routes' => [ |
|
| 44 | + ['name' => 'MailSettings#setMailSettings', 'url' => '/settings/admin/mailsettings', 'verb' => 'POST'], |
|
| 45 | + ['name' => 'MailSettings#storeCredentials', 'url' => '/settings/admin/mailsettings/credentials', 'verb' => 'POST'], |
|
| 46 | + ['name' => 'MailSettings#sendTestMail', 'url' => '/settings/admin/mailtest', 'verb' => 'POST'], |
|
| 47 | + ['name' => 'Encryption#startMigration', 'url' => '/settings/admin/startmigration', 'verb' => 'POST'], |
|
| 48 | + ['name' => 'AppSettings#listCategories', 'url' => '/settings/apps/categories', 'verb' => 'GET'], |
|
| 49 | + ['name' => 'AppSettings#viewApps', 'url' => '/settings/apps', 'verb' => 'GET'], |
|
| 50 | + ['name' => 'AppSettings#listApps', 'url' => '/settings/apps/list', 'verb' => 'GET'], |
|
| 51 | + ['name' => 'SecuritySettings#trustedDomains', 'url' => '/settings/admin/security/trustedDomains', 'verb' => 'POST'], |
|
| 52 | + ['name' => 'Users#setDisplayName', 'url' => '/settings/users/{username}/displayName', 'verb' => 'POST'], |
|
| 53 | + ['name' => 'Users#setEMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'], |
|
| 54 | + ['name' => 'Users#setUserSettings', 'url' => '/settings/users/{username}/settings', 'verb' => 'PUT'], |
|
| 55 | + ['name' => 'Users#stats', 'url' => '/settings/users/stats', 'verb' => 'GET'], |
|
| 56 | + ['name' => 'LogSettings#setLogLevel', 'url' => '/settings/admin/log/level', 'verb' => 'POST'], |
|
| 57 | + ['name' => 'LogSettings#getEntries', 'url' => '/settings/admin/log/entries', 'verb' => 'GET'], |
|
| 58 | + ['name' => 'LogSettings#download', 'url' => '/settings/admin/log/download', 'verb' => 'GET'], |
|
| 59 | + ['name' => 'CheckSetup#check', 'url' => '/settings/ajax/checksetup', 'verb' => 'GET'], |
|
| 60 | + ['name' => 'CheckSetup#getFailedIntegrityCheckFiles', 'url' => '/settings/integrity/failed', 'verb' => 'GET'], |
|
| 61 | + ['name' => 'CheckSetup#rescanFailedIntegrityCheck', 'url' => '/settings/integrity/rescan', 'verb' => 'GET'], |
|
| 62 | + ['name' => 'Certificate#addPersonalRootCertificate', 'url' => '/settings/personal/certificate', 'verb' => 'POST'], |
|
| 63 | + ['name' => 'Certificate#removePersonalRootCertificate', 'url' => '/settings/personal/certificate/{certificateIdentifier}', 'verb' => 'DELETE'], |
|
| 64 | + ['name' => 'Certificate#addSystemRootCertificate', 'url' => '/settings/admin/certificate', 'verb' => 'POST'], |
|
| 65 | + ['name' => 'Certificate#removeSystemRootCertificate', 'url' => '/settings/admin/certificate/{certificateIdentifier}', 'verb' => 'DELETE'], |
|
| 66 | + ['name' => 'AdminSettings#index', 'url' => '/settings/admin/{section}', 'verb' => 'GET', 'defaults' => ['section' => 'server']], |
|
| 67 | + ['name' => 'AdminSettings#form', 'url' => '/settings/admin/{section}', 'verb' => 'GET'], |
|
| 68 | + ['name' => 'ChangePassword#changePersonalPassword', 'url' => '/settings/personal/changepassword', 'verb' => 'POST'], |
|
| 69 | + ['name' => 'ChangePassword#changeUserPassword', 'url' => '/settings/users/changepassword', 'verb' => 'POST'], |
|
| 70 | + ['name' => 'Personal#setLanguage', 'url' => '/settings/ajax/setlanguage.php', 'verb' => 'POST'], |
|
| 71 | + ['name' => 'Groups#index', 'url' => '/settings/users/groups', 'verb' => 'GET'], |
|
| 72 | + ['name' => 'Groups#show', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'GET'], |
|
| 73 | + ['name' => 'Groups#create', 'url' => '/settings/users/groups', 'verb' => 'POST'], |
|
| 74 | + ['name' => 'Groups#update', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'PUT'], |
|
| 75 | + ['name' => 'Groups#destroy', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'DELETE'], |
|
| 76 | + ] |
|
| 77 | 77 | ]); |
| 78 | 78 | |
| 79 | 79 | /** @var $this \OCP\Route\IRouter */ |
| 80 | 80 | |
| 81 | 81 | // Settings pages |
| 82 | 82 | $this->create('settings_help', '/settings/help') |
| 83 | - ->actionInclude('settings/help.php'); |
|
| 83 | + ->actionInclude('settings/help.php'); |
|
| 84 | 84 | $this->create('settings_personal', '/settings/personal') |
| 85 | - ->actionInclude('settings/personal.php'); |
|
| 85 | + ->actionInclude('settings/personal.php'); |
|
| 86 | 86 | $this->create('settings_users', '/settings/users') |
| 87 | - ->actionInclude('settings/users.php'); |
|
| 87 | + ->actionInclude('settings/users.php'); |
|
| 88 | 88 | // Settings ajax actions |
| 89 | 89 | // users |
| 90 | 90 | $this->create('settings_ajax_setquota', '/settings/ajax/setquota.php') |
| 91 | - ->actionInclude('settings/ajax/setquota.php'); |
|
| 91 | + ->actionInclude('settings/ajax/setquota.php'); |
|
| 92 | 92 | $this->create('settings_ajax_togglegroups', '/settings/ajax/togglegroups.php') |
| 93 | - ->actionInclude('settings/ajax/togglegroups.php'); |
|
| 93 | + ->actionInclude('settings/ajax/togglegroups.php'); |
|
| 94 | 94 | $this->create('settings_ajax_togglesubadmins', '/settings/ajax/togglesubadmins.php') |
| 95 | - ->actionInclude('settings/ajax/togglesubadmins.php'); |
|
| 95 | + ->actionInclude('settings/ajax/togglesubadmins.php'); |
|
| 96 | 96 | $this->create('settings_ajax_changegorupname', '/settings/ajax/changegroupname.php') |
| 97 | - ->actionInclude('settings/ajax/changegroupname.php'); |
|
| 97 | + ->actionInclude('settings/ajax/changegroupname.php'); |
|
| 98 | 98 | // apps |
| 99 | 99 | $this->create('settings_ajax_enableapp', '/settings/ajax/enableapp.php') |
| 100 | - ->actionInclude('settings/ajax/enableapp.php'); |
|
| 100 | + ->actionInclude('settings/ajax/enableapp.php'); |
|
| 101 | 101 | $this->create('settings_ajax_disableapp', '/settings/ajax/disableapp.php') |
| 102 | - ->actionInclude('settings/ajax/disableapp.php'); |
|
| 102 | + ->actionInclude('settings/ajax/disableapp.php'); |
|
| 103 | 103 | $this->create('settings_ajax_updateapp', '/settings/ajax/updateapp.php') |
| 104 | - ->actionInclude('settings/ajax/updateapp.php'); |
|
| 104 | + ->actionInclude('settings/ajax/updateapp.php'); |
|
| 105 | 105 | $this->create('settings_ajax_uninstallapp', '/settings/ajax/uninstallapp.php') |
| 106 | - ->actionInclude('settings/ajax/uninstallapp.php'); |
|
| 106 | + ->actionInclude('settings/ajax/uninstallapp.php'); |
|
| 107 | 107 | $this->create('settings_ajax_navigationdetect', '/settings/ajax/navigationdetect.php') |
| 108 | - ->actionInclude('settings/ajax/navigationdetect.php'); |
|
| 108 | + ->actionInclude('settings/ajax/navigationdetect.php'); |
|
| 109 | 109 | // admin |
| 110 | 110 | $this->create('settings_ajax_excludegroups', '/settings/ajax/excludegroups.php') |
| 111 | - ->actionInclude('settings/ajax/excludegroups.php'); |
|
| 111 | + ->actionInclude('settings/ajax/excludegroups.php'); |
|
@@ -34,133 +34,133 @@ |
||
| 34 | 34 | use OCP\Security\ISecureRandom; |
| 35 | 35 | |
| 36 | 36 | class NewUserMailHelper { |
| 37 | - /** @var Defaults */ |
|
| 38 | - private $themingDefaults; |
|
| 39 | - /** @var IURLGenerator */ |
|
| 40 | - private $urlGenerator; |
|
| 41 | - /** @var IL10N */ |
|
| 42 | - private $l10n; |
|
| 43 | - /** @var IMailer */ |
|
| 44 | - private $mailer; |
|
| 45 | - /** @var ISecureRandom */ |
|
| 46 | - private $secureRandom; |
|
| 47 | - /** @var ITimeFactory */ |
|
| 48 | - private $timeFactory; |
|
| 49 | - /** @var IConfig */ |
|
| 50 | - private $config; |
|
| 51 | - /** @var ICrypto */ |
|
| 52 | - private $crypto; |
|
| 53 | - /** @var string */ |
|
| 54 | - private $fromAddress; |
|
| 37 | + /** @var Defaults */ |
|
| 38 | + private $themingDefaults; |
|
| 39 | + /** @var IURLGenerator */ |
|
| 40 | + private $urlGenerator; |
|
| 41 | + /** @var IL10N */ |
|
| 42 | + private $l10n; |
|
| 43 | + /** @var IMailer */ |
|
| 44 | + private $mailer; |
|
| 45 | + /** @var ISecureRandom */ |
|
| 46 | + private $secureRandom; |
|
| 47 | + /** @var ITimeFactory */ |
|
| 48 | + private $timeFactory; |
|
| 49 | + /** @var IConfig */ |
|
| 50 | + private $config; |
|
| 51 | + /** @var ICrypto */ |
|
| 52 | + private $crypto; |
|
| 53 | + /** @var string */ |
|
| 54 | + private $fromAddress; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param Defaults $themingDefaults |
|
| 58 | - * @param IURLGenerator $urlGenerator |
|
| 59 | - * @param IL10N $l10n |
|
| 60 | - * @param IMailer $mailer |
|
| 61 | - * @param ISecureRandom $secureRandom |
|
| 62 | - * @param ITimeFactory $timeFactory |
|
| 63 | - * @param IConfig $config |
|
| 64 | - * @param ICrypto $crypto |
|
| 65 | - * @param string $fromAddress |
|
| 66 | - */ |
|
| 67 | - public function __construct(Defaults $themingDefaults, |
|
| 68 | - IURLGenerator $urlGenerator, |
|
| 69 | - IL10N $l10n, |
|
| 70 | - IMailer $mailer, |
|
| 71 | - ISecureRandom $secureRandom, |
|
| 72 | - ITimeFactory $timeFactory, |
|
| 73 | - IConfig $config, |
|
| 74 | - ICrypto $crypto, |
|
| 75 | - $fromAddress) { |
|
| 76 | - $this->themingDefaults = $themingDefaults; |
|
| 77 | - $this->urlGenerator = $urlGenerator; |
|
| 78 | - $this->l10n = $l10n; |
|
| 79 | - $this->mailer = $mailer; |
|
| 80 | - $this->secureRandom = $secureRandom; |
|
| 81 | - $this->timeFactory = $timeFactory; |
|
| 82 | - $this->config = $config; |
|
| 83 | - $this->crypto = $crypto; |
|
| 84 | - $this->fromAddress = $fromAddress; |
|
| 85 | - } |
|
| 56 | + /** |
|
| 57 | + * @param Defaults $themingDefaults |
|
| 58 | + * @param IURLGenerator $urlGenerator |
|
| 59 | + * @param IL10N $l10n |
|
| 60 | + * @param IMailer $mailer |
|
| 61 | + * @param ISecureRandom $secureRandom |
|
| 62 | + * @param ITimeFactory $timeFactory |
|
| 63 | + * @param IConfig $config |
|
| 64 | + * @param ICrypto $crypto |
|
| 65 | + * @param string $fromAddress |
|
| 66 | + */ |
|
| 67 | + public function __construct(Defaults $themingDefaults, |
|
| 68 | + IURLGenerator $urlGenerator, |
|
| 69 | + IL10N $l10n, |
|
| 70 | + IMailer $mailer, |
|
| 71 | + ISecureRandom $secureRandom, |
|
| 72 | + ITimeFactory $timeFactory, |
|
| 73 | + IConfig $config, |
|
| 74 | + ICrypto $crypto, |
|
| 75 | + $fromAddress) { |
|
| 76 | + $this->themingDefaults = $themingDefaults; |
|
| 77 | + $this->urlGenerator = $urlGenerator; |
|
| 78 | + $this->l10n = $l10n; |
|
| 79 | + $this->mailer = $mailer; |
|
| 80 | + $this->secureRandom = $secureRandom; |
|
| 81 | + $this->timeFactory = $timeFactory; |
|
| 82 | + $this->config = $config; |
|
| 83 | + $this->crypto = $crypto; |
|
| 84 | + $this->fromAddress = $fromAddress; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Set the IL10N object |
|
| 89 | - * |
|
| 90 | - * @param IL10N $l10n |
|
| 91 | - */ |
|
| 92 | - public function setL10N(IL10N $l10n) { |
|
| 93 | - $this->l10n = $l10n; |
|
| 94 | - } |
|
| 87 | + /** |
|
| 88 | + * Set the IL10N object |
|
| 89 | + * |
|
| 90 | + * @param IL10N $l10n |
|
| 91 | + */ |
|
| 92 | + public function setL10N(IL10N $l10n) { |
|
| 93 | + $this->l10n = $l10n; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @param IUser $user |
|
| 98 | - * @param bool $generatePasswordResetToken |
|
| 99 | - * @return EMailTemplate |
|
| 100 | - */ |
|
| 101 | - public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { |
|
| 102 | - if ($generatePasswordResetToken) { |
|
| 103 | - $token = $this->secureRandom->generate( |
|
| 104 | - 21, |
|
| 105 | - ISecureRandom::CHAR_DIGITS . |
|
| 106 | - ISecureRandom::CHAR_LOWER . |
|
| 107 | - ISecureRandom::CHAR_UPPER |
|
| 108 | - ); |
|
| 109 | - $tokenValue = $this->timeFactory->getTime() . ':' . $token; |
|
| 110 | - $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 111 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); |
|
| 112 | - $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 113 | - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]); |
|
| 114 | - } else { |
|
| 115 | - $link = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 116 | - } |
|
| 96 | + /** |
|
| 97 | + * @param IUser $user |
|
| 98 | + * @param bool $generatePasswordResetToken |
|
| 99 | + * @return EMailTemplate |
|
| 100 | + */ |
|
| 101 | + public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { |
|
| 102 | + if ($generatePasswordResetToken) { |
|
| 103 | + $token = $this->secureRandom->generate( |
|
| 104 | + 21, |
|
| 105 | + ISecureRandom::CHAR_DIGITS . |
|
| 106 | + ISecureRandom::CHAR_LOWER . |
|
| 107 | + ISecureRandom::CHAR_UPPER |
|
| 108 | + ); |
|
| 109 | + $tokenValue = $this->timeFactory->getTime() . ':' . $token; |
|
| 110 | + $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 111 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); |
|
| 112 | + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 113 | + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]); |
|
| 114 | + } else { |
|
| 115 | + $link = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - $emailTemplate = new EMailTemplate( |
|
| 119 | - $this->themingDefaults, |
|
| 120 | - $this->urlGenerator, |
|
| 121 | - $this->l10n |
|
| 122 | - ); |
|
| 123 | - $emailTemplate->addHeader(); |
|
| 124 | - $displayName = $user->getDisplayName(); |
|
| 125 | - $userName = $user->getUID(); |
|
| 126 | - if ($displayName === $userName) { |
|
| 127 | - $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); |
|
| 128 | - } else { |
|
| 129 | - $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); |
|
| 130 | - } |
|
| 131 | - $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); |
|
| 132 | - $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName])); |
|
| 133 | - if ($generatePasswordResetToken) { |
|
| 134 | - $leftButtonText = $this->l10n->t('Set your password'); |
|
| 135 | - } else { |
|
| 136 | - $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); |
|
| 137 | - } |
|
| 138 | - $emailTemplate->addBodyButtonGroup( |
|
| 139 | - $leftButtonText, |
|
| 140 | - $link, |
|
| 141 | - $this->l10n->t('Install Client'), |
|
| 142 | - 'https://nextcloud.com/install/#install-clients' |
|
| 143 | - ); |
|
| 144 | - $emailTemplate->addFooter(); |
|
| 118 | + $emailTemplate = new EMailTemplate( |
|
| 119 | + $this->themingDefaults, |
|
| 120 | + $this->urlGenerator, |
|
| 121 | + $this->l10n |
|
| 122 | + ); |
|
| 123 | + $emailTemplate->addHeader(); |
|
| 124 | + $displayName = $user->getDisplayName(); |
|
| 125 | + $userName = $user->getUID(); |
|
| 126 | + if ($displayName === $userName) { |
|
| 127 | + $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); |
|
| 128 | + } else { |
|
| 129 | + $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); |
|
| 130 | + } |
|
| 131 | + $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); |
|
| 132 | + $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName])); |
|
| 133 | + if ($generatePasswordResetToken) { |
|
| 134 | + $leftButtonText = $this->l10n->t('Set your password'); |
|
| 135 | + } else { |
|
| 136 | + $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); |
|
| 137 | + } |
|
| 138 | + $emailTemplate->addBodyButtonGroup( |
|
| 139 | + $leftButtonText, |
|
| 140 | + $link, |
|
| 141 | + $this->l10n->t('Install Client'), |
|
| 142 | + 'https://nextcloud.com/install/#install-clients' |
|
| 143 | + ); |
|
| 144 | + $emailTemplate->addFooter(); |
|
| 145 | 145 | |
| 146 | - return $emailTemplate; |
|
| 147 | - } |
|
| 146 | + return $emailTemplate; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Sends a welcome mail to $user |
|
| 151 | - * |
|
| 152 | - * @param IUser $user |
|
| 153 | - * @param IEmailTemplate $emailTemplate |
|
| 154 | - * @throws \Exception If mail could not be sent |
|
| 155 | - */ |
|
| 156 | - public function sendMail(IUser $user, |
|
| 157 | - IEMailTemplate $emailTemplate) { |
|
| 158 | - $message = $this->mailer->createMessage(); |
|
| 159 | - $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
|
| 160 | - $message->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); |
|
| 161 | - $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 162 | - $message->setPlainBody($emailTemplate->renderText()); |
|
| 163 | - $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); |
|
| 164 | - $this->mailer->send($message); |
|
| 165 | - } |
|
| 149 | + /** |
|
| 150 | + * Sends a welcome mail to $user |
|
| 151 | + * |
|
| 152 | + * @param IUser $user |
|
| 153 | + * @param IEmailTemplate $emailTemplate |
|
| 154 | + * @throws \Exception If mail could not be sent |
|
| 155 | + */ |
|
| 156 | + public function sendMail(IUser $user, |
|
| 157 | + IEMailTemplate $emailTemplate) { |
|
| 158 | + $message = $this->mailer->createMessage(); |
|
| 159 | + $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
|
| 160 | + $message->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); |
|
| 161 | + $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 162 | + $message->setPlainBody($emailTemplate->renderText()); |
|
| 163 | + $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); |
|
| 164 | + $this->mailer->send($message); |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -32,134 +32,134 @@ |
||
| 32 | 32 | |
| 33 | 33 | class Hooks { |
| 34 | 34 | |
| 35 | - /** @var IActivityManager */ |
|
| 36 | - protected $activityManager; |
|
| 37 | - /** @var IUserManager */ |
|
| 38 | - protected $userManager; |
|
| 39 | - /** @var IUserSession */ |
|
| 40 | - protected $userSession; |
|
| 41 | - /** @var IURLGenerator */ |
|
| 42 | - protected $urlGenerator; |
|
| 43 | - /** @var IMailer */ |
|
| 44 | - protected $mailer; |
|
| 45 | - /** @var IL10N */ |
|
| 46 | - protected $l; |
|
| 47 | - |
|
| 48 | - public function __construct(IActivityManager $activityManager, IUserManager $userManager, IUserSession $userSession, IURLGenerator $urlGenerator, IMailer $mailer, IL10N $l) { |
|
| 49 | - $this->activityManager = $activityManager; |
|
| 50 | - $this->userManager = $userManager; |
|
| 51 | - $this->userSession = $userSession; |
|
| 52 | - $this->urlGenerator = $urlGenerator; |
|
| 53 | - $this->mailer = $mailer; |
|
| 54 | - $this->l = $l; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $uid |
|
| 59 | - * @throws \InvalidArgumentException |
|
| 60 | - * @throws \BadMethodCallException |
|
| 61 | - * @throws \Exception |
|
| 62 | - */ |
|
| 63 | - public function onChangePassword($uid) { |
|
| 64 | - $user = $this->userManager->get($uid); |
|
| 65 | - |
|
| 66 | - if (!$user instanceof IUser || $user->getEMailAddress() === null) { |
|
| 67 | - return; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $event = $this->activityManager->generateEvent(); |
|
| 71 | - $event->setApp('settings') |
|
| 72 | - ->setType('personal_settings') |
|
| 73 | - ->setAffectedUser($user->getUID()); |
|
| 74 | - |
|
| 75 | - $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 76 | - |
|
| 77 | - $actor = $this->userSession->getUser(); |
|
| 78 | - if ($actor instanceof IUser) { |
|
| 79 | - if ($actor->getUID() !== $user->getUID()) { |
|
| 80 | - $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 81 | - $event->setAuthor($actor->getUID()) |
|
| 82 | - ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
|
| 83 | - } else { |
|
| 84 | - $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]); |
|
| 85 | - $event->setAuthor($actor->getUID()) |
|
| 86 | - ->setSubject(Provider::PASSWORD_CHANGED_SELF); |
|
| 87 | - } |
|
| 88 | - } else { |
|
| 89 | - $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
|
| 90 | - $event->setSubject(Provider::PASSWORD_RESET); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $this->activityManager->publish($event); |
|
| 94 | - |
|
| 95 | - if ($user->getEMailAddress() !== null) { |
|
| 96 | - $template = $this->mailer->createEMailTemplate(); |
|
| 97 | - $template->addHeader(); |
|
| 98 | - $template->addHeading($this->l->t('Password changed for %s', $user->getDisplayName()), false); |
|
| 99 | - $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 100 | - $template->addFooter(); |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - $message = $this->mailer->createMessage(); |
|
| 104 | - $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
|
| 105 | - $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 106 | - $message->setBody($template->renderText(), 'text/plain'); |
|
| 107 | - $message->setHtmlBody($template->renderHtml()); |
|
| 108 | - |
|
| 109 | - $this->mailer->send($message); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param IUser $user |
|
| 115 | - * @param string|null $oldMailAddress |
|
| 116 | - * @throws \InvalidArgumentException |
|
| 117 | - * @throws \BadMethodCallException |
|
| 118 | - */ |
|
| 119 | - public function onChangeEmail(IUser $user, $oldMailAddress) { |
|
| 120 | - $event = $this->activityManager->generateEvent(); |
|
| 121 | - $event->setApp('settings') |
|
| 122 | - ->setType('personal_settings') |
|
| 123 | - ->setAffectedUser($user->getUID()); |
|
| 124 | - |
|
| 125 | - $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 126 | - |
|
| 127 | - $actor = $this->userSession->getUser(); |
|
| 128 | - if ($actor instanceof IUser) { |
|
| 129 | - if ($actor->getUID() !== $user->getUID()) { |
|
| 130 | - $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 131 | - $event->setAuthor($actor->getUID()) |
|
| 132 | - ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]); |
|
| 133 | - } else { |
|
| 134 | - $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); |
|
| 135 | - $event->setAuthor($actor->getUID()) |
|
| 136 | - ->setSubject(Provider::EMAIL_CHANGED_SELF); |
|
| 137 | - } |
|
| 138 | - } else { |
|
| 139 | - $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
|
| 140 | - $event->setSubject(Provider::EMAIL_CHANGED); |
|
| 141 | - } |
|
| 142 | - $this->activityManager->publish($event); |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - if ($oldMailAddress !== null) { |
|
| 146 | - $template = $this->mailer->createEMailTemplate(); |
|
| 147 | - $template->addHeader(); |
|
| 148 | - $template->addHeading($this->l->t('Email address changed for %s', $user->getDisplayName()), false); |
|
| 149 | - $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 150 | - if ($user->getEMailAddress()) { |
|
| 151 | - $template->addBodyText($this->l->t('The new email address is %s', $user->getEMailAddress())); |
|
| 152 | - } |
|
| 153 | - $template->addFooter(); |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - $message = $this->mailer->createMessage(); |
|
| 157 | - $message->setTo([$oldMailAddress => $user->getDisplayName()]); |
|
| 158 | - $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 159 | - $message->setBody($template->renderText(), 'text/plain'); |
|
| 160 | - $message->setHtmlBody($template->renderHtml()); |
|
| 161 | - |
|
| 162 | - $this->mailer->send($message); |
|
| 163 | - } |
|
| 164 | - } |
|
| 35 | + /** @var IActivityManager */ |
|
| 36 | + protected $activityManager; |
|
| 37 | + /** @var IUserManager */ |
|
| 38 | + protected $userManager; |
|
| 39 | + /** @var IUserSession */ |
|
| 40 | + protected $userSession; |
|
| 41 | + /** @var IURLGenerator */ |
|
| 42 | + protected $urlGenerator; |
|
| 43 | + /** @var IMailer */ |
|
| 44 | + protected $mailer; |
|
| 45 | + /** @var IL10N */ |
|
| 46 | + protected $l; |
|
| 47 | + |
|
| 48 | + public function __construct(IActivityManager $activityManager, IUserManager $userManager, IUserSession $userSession, IURLGenerator $urlGenerator, IMailer $mailer, IL10N $l) { |
|
| 49 | + $this->activityManager = $activityManager; |
|
| 50 | + $this->userManager = $userManager; |
|
| 51 | + $this->userSession = $userSession; |
|
| 52 | + $this->urlGenerator = $urlGenerator; |
|
| 53 | + $this->mailer = $mailer; |
|
| 54 | + $this->l = $l; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $uid |
|
| 59 | + * @throws \InvalidArgumentException |
|
| 60 | + * @throws \BadMethodCallException |
|
| 61 | + * @throws \Exception |
|
| 62 | + */ |
|
| 63 | + public function onChangePassword($uid) { |
|
| 64 | + $user = $this->userManager->get($uid); |
|
| 65 | + |
|
| 66 | + if (!$user instanceof IUser || $user->getEMailAddress() === null) { |
|
| 67 | + return; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $event = $this->activityManager->generateEvent(); |
|
| 71 | + $event->setApp('settings') |
|
| 72 | + ->setType('personal_settings') |
|
| 73 | + ->setAffectedUser($user->getUID()); |
|
| 74 | + |
|
| 75 | + $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 76 | + |
|
| 77 | + $actor = $this->userSession->getUser(); |
|
| 78 | + if ($actor instanceof IUser) { |
|
| 79 | + if ($actor->getUID() !== $user->getUID()) { |
|
| 80 | + $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 81 | + $event->setAuthor($actor->getUID()) |
|
| 82 | + ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
|
| 83 | + } else { |
|
| 84 | + $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]); |
|
| 85 | + $event->setAuthor($actor->getUID()) |
|
| 86 | + ->setSubject(Provider::PASSWORD_CHANGED_SELF); |
|
| 87 | + } |
|
| 88 | + } else { |
|
| 89 | + $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
|
| 90 | + $event->setSubject(Provider::PASSWORD_RESET); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $this->activityManager->publish($event); |
|
| 94 | + |
|
| 95 | + if ($user->getEMailAddress() !== null) { |
|
| 96 | + $template = $this->mailer->createEMailTemplate(); |
|
| 97 | + $template->addHeader(); |
|
| 98 | + $template->addHeading($this->l->t('Password changed for %s', $user->getDisplayName()), false); |
|
| 99 | + $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 100 | + $template->addFooter(); |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + $message = $this->mailer->createMessage(); |
|
| 104 | + $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
|
| 105 | + $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 106 | + $message->setBody($template->renderText(), 'text/plain'); |
|
| 107 | + $message->setHtmlBody($template->renderHtml()); |
|
| 108 | + |
|
| 109 | + $this->mailer->send($message); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param IUser $user |
|
| 115 | + * @param string|null $oldMailAddress |
|
| 116 | + * @throws \InvalidArgumentException |
|
| 117 | + * @throws \BadMethodCallException |
|
| 118 | + */ |
|
| 119 | + public function onChangeEmail(IUser $user, $oldMailAddress) { |
|
| 120 | + $event = $this->activityManager->generateEvent(); |
|
| 121 | + $event->setApp('settings') |
|
| 122 | + ->setType('personal_settings') |
|
| 123 | + ->setAffectedUser($user->getUID()); |
|
| 124 | + |
|
| 125 | + $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
|
| 126 | + |
|
| 127 | + $actor = $this->userSession->getUser(); |
|
| 128 | + if ($actor instanceof IUser) { |
|
| 129 | + if ($actor->getUID() !== $user->getUID()) { |
|
| 130 | + $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
|
| 131 | + $event->setAuthor($actor->getUID()) |
|
| 132 | + ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]); |
|
| 133 | + } else { |
|
| 134 | + $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); |
|
| 135 | + $event->setAuthor($actor->getUID()) |
|
| 136 | + ->setSubject(Provider::EMAIL_CHANGED_SELF); |
|
| 137 | + } |
|
| 138 | + } else { |
|
| 139 | + $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
|
| 140 | + $event->setSubject(Provider::EMAIL_CHANGED); |
|
| 141 | + } |
|
| 142 | + $this->activityManager->publish($event); |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + if ($oldMailAddress !== null) { |
|
| 146 | + $template = $this->mailer->createEMailTemplate(); |
|
| 147 | + $template->addHeader(); |
|
| 148 | + $template->addHeading($this->l->t('Email address changed for %s', $user->getDisplayName()), false); |
|
| 149 | + $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
|
| 150 | + if ($user->getEMailAddress()) { |
|
| 151 | + $template->addBodyText($this->l->t('The new email address is %s', $user->getEMailAddress())); |
|
| 152 | + } |
|
| 153 | + $template->addFooter(); |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + $message = $this->mailer->createMessage(); |
|
| 157 | + $message->setTo([$oldMailAddress => $user->getDisplayName()]); |
|
| 158 | + $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
|
| 159 | + $message->setBody($template->renderText(), 'text/plain'); |
|
| 160 | + $message->setHtmlBody($template->renderHtml()); |
|
| 161 | + |
|
| 162 | + $this->mailer->send($message); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -37,124 +37,124 @@ |
||
| 37 | 37 | * @package OC\Settings\Controller |
| 38 | 38 | */ |
| 39 | 39 | class AdminSettingsController extends Controller { |
| 40 | - /** @var INavigationManager */ |
|
| 41 | - private $navigationManager; |
|
| 42 | - /** @var ISettingsManager */ |
|
| 43 | - private $settingsManager; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param string $appName |
|
| 47 | - * @param IRequest $request |
|
| 48 | - * @param INavigationManager $navigationManager |
|
| 49 | - * @param ISettingsManager $settingsManager |
|
| 50 | - */ |
|
| 51 | - public function __construct( |
|
| 52 | - $appName, |
|
| 53 | - IRequest $request, |
|
| 54 | - INavigationManager $navigationManager, |
|
| 55 | - ISettingsManager $settingsManager |
|
| 56 | - ) { |
|
| 57 | - parent::__construct($appName, $request); |
|
| 58 | - $this->navigationManager = $navigationManager; |
|
| 59 | - $this->settingsManager = $settingsManager; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param string $section |
|
| 64 | - * @return TemplateResponse |
|
| 65 | - * |
|
| 66 | - * @NoCSRFRequired |
|
| 67 | - */ |
|
| 68 | - public function index($section) { |
|
| 69 | - $this->navigationManager->setActiveEntry('admin'); |
|
| 70 | - |
|
| 71 | - $templateParams = []; |
|
| 72 | - $templateParams = array_merge($templateParams, $this->getNavigationParameters($section)); |
|
| 73 | - $templateParams = array_merge($templateParams, $this->getSettings($section)); |
|
| 74 | - |
|
| 75 | - return new TemplateResponse('settings', 'admin/frame', $templateParams); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param string $section |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - private function getSettings($section) { |
|
| 83 | - $html = ''; |
|
| 84 | - $settings = $this->settingsManager->getAdminSettings($section); |
|
| 85 | - foreach ($settings as $prioritizedSettings) { |
|
| 86 | - foreach ($prioritizedSettings as $setting) { |
|
| 87 | - /** @var \OCP\Settings\ISettings $setting */ |
|
| 88 | - $form = $setting->getForm(); |
|
| 89 | - $html .= $form->renderAs('')->render(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - if($section === 'additional') { |
|
| 93 | - $html .= $this->getLegacyForms(); |
|
| 94 | - } |
|
| 95 | - return ['content' => $html]; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @return bool|string |
|
| 100 | - */ |
|
| 101 | - private function getLegacyForms() { |
|
| 102 | - $forms = \OC_App::getForms('admin'); |
|
| 103 | - |
|
| 104 | - $forms = array_map(function ($form) { |
|
| 105 | - if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) { |
|
| 106 | - $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]); |
|
| 107 | - $sectionName = str_replace('</h2>', '', $sectionName); |
|
| 108 | - $anchor = strtolower($sectionName); |
|
| 109 | - $anchor = str_replace(' ', '-', $anchor); |
|
| 110 | - |
|
| 111 | - return array( |
|
| 112 | - 'anchor' => $anchor, |
|
| 113 | - 'section-name' => $sectionName, |
|
| 114 | - 'form' => $form |
|
| 115 | - ); |
|
| 116 | - } |
|
| 117 | - return array( |
|
| 118 | - 'form' => $form |
|
| 119 | - ); |
|
| 120 | - }, $forms); |
|
| 121 | - |
|
| 122 | - $out = new Template('settings', 'admin/additional'); |
|
| 123 | - $out->assign('forms', $forms); |
|
| 124 | - |
|
| 125 | - return $out->fetchPage(); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param string $currentSection |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - private function getNavigationParameters($currentSection) { |
|
| 133 | - $sections = $this->settingsManager->getAdminSections(); |
|
| 134 | - $templateParameters = []; |
|
| 135 | - /** @var \OC\Settings\Section[] $prioritizedSections */ |
|
| 136 | - foreach($sections as $prioritizedSections) { |
|
| 137 | - foreach ($prioritizedSections as $section) { |
|
| 138 | - if (empty($this->settingsManager->getAdminSettings($section->getID()))) { |
|
| 139 | - continue; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $icon = ''; |
|
| 143 | - if ($section instanceof IIconSection) { |
|
| 144 | - $icon = $section->getIcon(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $templateParameters[] = [ |
|
| 148 | - 'anchor' => $section->getID(), |
|
| 149 | - 'section-name' => $section->getName(), |
|
| 150 | - 'active' => $section->getID() === $currentSection, |
|
| 151 | - 'icon' => $icon, |
|
| 152 | - ]; |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return [ |
|
| 157 | - 'forms' => $templateParameters |
|
| 158 | - ]; |
|
| 159 | - } |
|
| 40 | + /** @var INavigationManager */ |
|
| 41 | + private $navigationManager; |
|
| 42 | + /** @var ISettingsManager */ |
|
| 43 | + private $settingsManager; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param string $appName |
|
| 47 | + * @param IRequest $request |
|
| 48 | + * @param INavigationManager $navigationManager |
|
| 49 | + * @param ISettingsManager $settingsManager |
|
| 50 | + */ |
|
| 51 | + public function __construct( |
|
| 52 | + $appName, |
|
| 53 | + IRequest $request, |
|
| 54 | + INavigationManager $navigationManager, |
|
| 55 | + ISettingsManager $settingsManager |
|
| 56 | + ) { |
|
| 57 | + parent::__construct($appName, $request); |
|
| 58 | + $this->navigationManager = $navigationManager; |
|
| 59 | + $this->settingsManager = $settingsManager; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param string $section |
|
| 64 | + * @return TemplateResponse |
|
| 65 | + * |
|
| 66 | + * @NoCSRFRequired |
|
| 67 | + */ |
|
| 68 | + public function index($section) { |
|
| 69 | + $this->navigationManager->setActiveEntry('admin'); |
|
| 70 | + |
|
| 71 | + $templateParams = []; |
|
| 72 | + $templateParams = array_merge($templateParams, $this->getNavigationParameters($section)); |
|
| 73 | + $templateParams = array_merge($templateParams, $this->getSettings($section)); |
|
| 74 | + |
|
| 75 | + return new TemplateResponse('settings', 'admin/frame', $templateParams); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param string $section |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + private function getSettings($section) { |
|
| 83 | + $html = ''; |
|
| 84 | + $settings = $this->settingsManager->getAdminSettings($section); |
|
| 85 | + foreach ($settings as $prioritizedSettings) { |
|
| 86 | + foreach ($prioritizedSettings as $setting) { |
|
| 87 | + /** @var \OCP\Settings\ISettings $setting */ |
|
| 88 | + $form = $setting->getForm(); |
|
| 89 | + $html .= $form->renderAs('')->render(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + if($section === 'additional') { |
|
| 93 | + $html .= $this->getLegacyForms(); |
|
| 94 | + } |
|
| 95 | + return ['content' => $html]; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @return bool|string |
|
| 100 | + */ |
|
| 101 | + private function getLegacyForms() { |
|
| 102 | + $forms = \OC_App::getForms('admin'); |
|
| 103 | + |
|
| 104 | + $forms = array_map(function ($form) { |
|
| 105 | + if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) { |
|
| 106 | + $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]); |
|
| 107 | + $sectionName = str_replace('</h2>', '', $sectionName); |
|
| 108 | + $anchor = strtolower($sectionName); |
|
| 109 | + $anchor = str_replace(' ', '-', $anchor); |
|
| 110 | + |
|
| 111 | + return array( |
|
| 112 | + 'anchor' => $anchor, |
|
| 113 | + 'section-name' => $sectionName, |
|
| 114 | + 'form' => $form |
|
| 115 | + ); |
|
| 116 | + } |
|
| 117 | + return array( |
|
| 118 | + 'form' => $form |
|
| 119 | + ); |
|
| 120 | + }, $forms); |
|
| 121 | + |
|
| 122 | + $out = new Template('settings', 'admin/additional'); |
|
| 123 | + $out->assign('forms', $forms); |
|
| 124 | + |
|
| 125 | + return $out->fetchPage(); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param string $currentSection |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + private function getNavigationParameters($currentSection) { |
|
| 133 | + $sections = $this->settingsManager->getAdminSections(); |
|
| 134 | + $templateParameters = []; |
|
| 135 | + /** @var \OC\Settings\Section[] $prioritizedSections */ |
|
| 136 | + foreach($sections as $prioritizedSections) { |
|
| 137 | + foreach ($prioritizedSections as $section) { |
|
| 138 | + if (empty($this->settingsManager->getAdminSettings($section->getID()))) { |
|
| 139 | + continue; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $icon = ''; |
|
| 143 | + if ($section instanceof IIconSection) { |
|
| 144 | + $icon = $section->getIcon(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $templateParameters[] = [ |
|
| 148 | + 'anchor' => $section->getID(), |
|
| 149 | + 'section-name' => $section->getName(), |
|
| 150 | + 'active' => $section->getID() === $currentSection, |
|
| 151 | + 'icon' => $icon, |
|
| 152 | + ]; |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return [ |
|
| 157 | + 'forms' => $templateParameters |
|
| 158 | + ]; |
|
| 159 | + } |
|
| 160 | 160 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $html .= $form->renderAs('')->render(); |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | - if($section === 'additional') { |
|
| 92 | + if ($section === 'additional') { |
|
| 93 | 93 | $html .= $this->getLegacyForms(); |
| 94 | 94 | } |
| 95 | 95 | return ['content' => $html]; |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | private function getLegacyForms() { |
| 102 | 102 | $forms = \OC_App::getForms('admin'); |
| 103 | 103 | |
| 104 | - $forms = array_map(function ($form) { |
|
| 104 | + $forms = array_map(function($form) { |
|
| 105 | 105 | if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) { |
| 106 | - $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]); |
|
| 106 | + $sectionName = str_replace('<h2'.$regs['class'].'>', '', $regs[0]); |
|
| 107 | 107 | $sectionName = str_replace('</h2>', '', $sectionName); |
| 108 | 108 | $anchor = strtolower($sectionName); |
| 109 | 109 | $anchor = str_replace(' ', '-', $anchor); |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | $sections = $this->settingsManager->getAdminSections(); |
| 134 | 134 | $templateParameters = []; |
| 135 | 135 | /** @var \OC\Settings\Section[] $prioritizedSections */ |
| 136 | - foreach($sections as $prioritizedSections) { |
|
| 136 | + foreach ($sections as $prioritizedSections) { |
|
| 137 | 137 | foreach ($prioritizedSections as $section) { |
| 138 | 138 | if (empty($this->settingsManager->getAdminSettings($section->getID()))) { |
| 139 | 139 | continue; |
@@ -44,310 +44,310 @@ |
||
| 44 | 44 | |
| 45 | 45 | class Scan extends Base { |
| 46 | 46 | |
| 47 | - /** @var IUserManager $userManager */ |
|
| 48 | - private $userManager; |
|
| 49 | - /** @var float */ |
|
| 50 | - protected $execTime = 0; |
|
| 51 | - /** @var int */ |
|
| 52 | - protected $foldersCounter = 0; |
|
| 53 | - /** @var int */ |
|
| 54 | - protected $filesCounter = 0; |
|
| 47 | + /** @var IUserManager $userManager */ |
|
| 48 | + private $userManager; |
|
| 49 | + /** @var float */ |
|
| 50 | + protected $execTime = 0; |
|
| 51 | + /** @var int */ |
|
| 52 | + protected $foldersCounter = 0; |
|
| 53 | + /** @var int */ |
|
| 54 | + protected $filesCounter = 0; |
|
| 55 | 55 | |
| 56 | - public function __construct(IUserManager $userManager) { |
|
| 57 | - $this->userManager = $userManager; |
|
| 58 | - parent::__construct(); |
|
| 59 | - } |
|
| 56 | + public function __construct(IUserManager $userManager) { |
|
| 57 | + $this->userManager = $userManager; |
|
| 58 | + parent::__construct(); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - protected function configure() { |
|
| 62 | - parent::configure(); |
|
| 61 | + protected function configure() { |
|
| 62 | + parent::configure(); |
|
| 63 | 63 | |
| 64 | - $this |
|
| 65 | - ->setName('files:scan') |
|
| 66 | - ->setDescription('rescan filesystem') |
|
| 67 | - ->addArgument( |
|
| 68 | - 'user_id', |
|
| 69 | - InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
|
| 70 | - 'will rescan all files of the given user(s)' |
|
| 71 | - ) |
|
| 72 | - ->addOption( |
|
| 73 | - 'path', |
|
| 74 | - 'p', |
|
| 75 | - InputArgument::OPTIONAL, |
|
| 76 | - 'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored' |
|
| 77 | - ) |
|
| 78 | - ->addOption( |
|
| 79 | - 'quiet', |
|
| 80 | - 'q', |
|
| 81 | - InputOption::VALUE_NONE, |
|
| 82 | - 'suppress any output' |
|
| 83 | - ) |
|
| 84 | - ->addOption( |
|
| 85 | - 'verbose', |
|
| 86 | - '-v|vv|vvv', |
|
| 87 | - InputOption::VALUE_NONE, |
|
| 88 | - 'verbose the output' |
|
| 89 | - ) |
|
| 90 | - ->addOption( |
|
| 91 | - 'all', |
|
| 92 | - null, |
|
| 93 | - InputOption::VALUE_NONE, |
|
| 94 | - 'will rescan all files of all known users' |
|
| 95 | - )->addOption( |
|
| 96 | - 'unscanned', |
|
| 97 | - null, |
|
| 98 | - InputOption::VALUE_NONE, |
|
| 99 | - 'only scan files which are marked as not fully scanned' |
|
| 100 | - ); |
|
| 101 | - } |
|
| 64 | + $this |
|
| 65 | + ->setName('files:scan') |
|
| 66 | + ->setDescription('rescan filesystem') |
|
| 67 | + ->addArgument( |
|
| 68 | + 'user_id', |
|
| 69 | + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
|
| 70 | + 'will rescan all files of the given user(s)' |
|
| 71 | + ) |
|
| 72 | + ->addOption( |
|
| 73 | + 'path', |
|
| 74 | + 'p', |
|
| 75 | + InputArgument::OPTIONAL, |
|
| 76 | + 'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored' |
|
| 77 | + ) |
|
| 78 | + ->addOption( |
|
| 79 | + 'quiet', |
|
| 80 | + 'q', |
|
| 81 | + InputOption::VALUE_NONE, |
|
| 82 | + 'suppress any output' |
|
| 83 | + ) |
|
| 84 | + ->addOption( |
|
| 85 | + 'verbose', |
|
| 86 | + '-v|vv|vvv', |
|
| 87 | + InputOption::VALUE_NONE, |
|
| 88 | + 'verbose the output' |
|
| 89 | + ) |
|
| 90 | + ->addOption( |
|
| 91 | + 'all', |
|
| 92 | + null, |
|
| 93 | + InputOption::VALUE_NONE, |
|
| 94 | + 'will rescan all files of all known users' |
|
| 95 | + )->addOption( |
|
| 96 | + 'unscanned', |
|
| 97 | + null, |
|
| 98 | + InputOption::VALUE_NONE, |
|
| 99 | + 'only scan files which are marked as not fully scanned' |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - public function checkScanWarning($fullPath, OutputInterface $output) { |
|
| 104 | - $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); |
|
| 105 | - $path = basename($fullPath); |
|
| 103 | + public function checkScanWarning($fullPath, OutputInterface $output) { |
|
| 104 | + $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); |
|
| 105 | + $path = basename($fullPath); |
|
| 106 | 106 | |
| 107 | - if ($normalizedPath !== $path) { |
|
| 108 | - $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 109 | - } |
|
| 110 | - } |
|
| 107 | + if ($normalizedPath !== $path) { |
|
| 108 | + $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) { |
|
| 113 | - $connection = $this->reconnectToDatabase($output); |
|
| 114 | - $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger()); |
|
| 115 | - # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
|
| 116 | - # printout and count |
|
| 117 | - if ($verbose) { |
|
| 118 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 119 | - $output->writeln("\tFile <info>$path</info>"); |
|
| 120 | - $this->filesCounter += 1; |
|
| 121 | - if ($this->hasBeenInterrupted()) { |
|
| 122 | - throw new InterruptedException(); |
|
| 123 | - } |
|
| 124 | - }); |
|
| 125 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 126 | - $output->writeln("\tFolder <info>$path</info>"); |
|
| 127 | - $this->foldersCounter += 1; |
|
| 128 | - if ($this->hasBeenInterrupted()) { |
|
| 129 | - throw new InterruptedException(); |
|
| 130 | - } |
|
| 131 | - }); |
|
| 132 | - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 133 | - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 134 | - }); |
|
| 135 | - # count only |
|
| 136 | - } else { |
|
| 137 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 138 | - $this->filesCounter += 1; |
|
| 139 | - if ($this->hasBeenInterrupted()) { |
|
| 140 | - throw new InterruptedException(); |
|
| 141 | - } |
|
| 142 | - }); |
|
| 143 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 144 | - $this->foldersCounter += 1; |
|
| 145 | - if ($this->hasBeenInterrupted()) { |
|
| 146 | - throw new InterruptedException(); |
|
| 147 | - } |
|
| 148 | - }); |
|
| 149 | - } |
|
| 150 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 151 | - $this->checkScanWarning($path, $output); |
|
| 152 | - }); |
|
| 153 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 154 | - $this->checkScanWarning($path, $output); |
|
| 155 | - }); |
|
| 112 | + protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) { |
|
| 113 | + $connection = $this->reconnectToDatabase($output); |
|
| 114 | + $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger()); |
|
| 115 | + # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
|
| 116 | + # printout and count |
|
| 117 | + if ($verbose) { |
|
| 118 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 119 | + $output->writeln("\tFile <info>$path</info>"); |
|
| 120 | + $this->filesCounter += 1; |
|
| 121 | + if ($this->hasBeenInterrupted()) { |
|
| 122 | + throw new InterruptedException(); |
|
| 123 | + } |
|
| 124 | + }); |
|
| 125 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 126 | + $output->writeln("\tFolder <info>$path</info>"); |
|
| 127 | + $this->foldersCounter += 1; |
|
| 128 | + if ($this->hasBeenInterrupted()) { |
|
| 129 | + throw new InterruptedException(); |
|
| 130 | + } |
|
| 131 | + }); |
|
| 132 | + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 133 | + $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 134 | + }); |
|
| 135 | + # count only |
|
| 136 | + } else { |
|
| 137 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 138 | + $this->filesCounter += 1; |
|
| 139 | + if ($this->hasBeenInterrupted()) { |
|
| 140 | + throw new InterruptedException(); |
|
| 141 | + } |
|
| 142 | + }); |
|
| 143 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 144 | + $this->foldersCounter += 1; |
|
| 145 | + if ($this->hasBeenInterrupted()) { |
|
| 146 | + throw new InterruptedException(); |
|
| 147 | + } |
|
| 148 | + }); |
|
| 149 | + } |
|
| 150 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 151 | + $this->checkScanWarning($path, $output); |
|
| 152 | + }); |
|
| 153 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 154 | + $this->checkScanWarning($path, $output); |
|
| 155 | + }); |
|
| 156 | 156 | |
| 157 | - try { |
|
| 158 | - if ($backgroundScan) { |
|
| 159 | - $scanner->backgroundScan($path); |
|
| 160 | - } else { |
|
| 161 | - $scanner->scan($path); |
|
| 162 | - } |
|
| 163 | - } catch (ForbiddenException $e) { |
|
| 164 | - $output->writeln("<error>Home storage for user $user not writable</error>"); |
|
| 165 | - $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); |
|
| 166 | - } catch (InterruptedException $e) { |
|
| 167 | - # exit the function if ctrl-c has been pressed |
|
| 168 | - $output->writeln('Interrupted by user'); |
|
| 169 | - } catch (NotFoundException $e) { |
|
| 170 | - $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 171 | - } catch (\Exception $e) { |
|
| 172 | - $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 173 | - $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 174 | - } |
|
| 175 | - } |
|
| 157 | + try { |
|
| 158 | + if ($backgroundScan) { |
|
| 159 | + $scanner->backgroundScan($path); |
|
| 160 | + } else { |
|
| 161 | + $scanner->scan($path); |
|
| 162 | + } |
|
| 163 | + } catch (ForbiddenException $e) { |
|
| 164 | + $output->writeln("<error>Home storage for user $user not writable</error>"); |
|
| 165 | + $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); |
|
| 166 | + } catch (InterruptedException $e) { |
|
| 167 | + # exit the function if ctrl-c has been pressed |
|
| 168 | + $output->writeln('Interrupted by user'); |
|
| 169 | + } catch (NotFoundException $e) { |
|
| 170 | + $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 171 | + } catch (\Exception $e) { |
|
| 172 | + $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 173 | + $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | 177 | |
| 178 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 179 | - $inputPath = $input->getOption('path'); |
|
| 180 | - if ($inputPath) { |
|
| 181 | - $inputPath = '/' . trim($inputPath, '/'); |
|
| 182 | - list (, $user,) = explode('/', $inputPath, 3); |
|
| 183 | - $users = array($user); |
|
| 184 | - } else if ($input->getOption('all')) { |
|
| 185 | - $users = $this->userManager->search(''); |
|
| 186 | - } else { |
|
| 187 | - $users = $input->getArgument('user_id'); |
|
| 188 | - } |
|
| 178 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 179 | + $inputPath = $input->getOption('path'); |
|
| 180 | + if ($inputPath) { |
|
| 181 | + $inputPath = '/' . trim($inputPath, '/'); |
|
| 182 | + list (, $user,) = explode('/', $inputPath, 3); |
|
| 183 | + $users = array($user); |
|
| 184 | + } else if ($input->getOption('all')) { |
|
| 185 | + $users = $this->userManager->search(''); |
|
| 186 | + } else { |
|
| 187 | + $users = $input->getArgument('user_id'); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - # no messaging level option means: no full printout but statistics |
|
| 191 | - # $quiet means no print at all |
|
| 192 | - # $verbose means full printout including statistics |
|
| 193 | - # -q -v full stat |
|
| 194 | - # 0 0 no yes |
|
| 195 | - # 0 1 yes yes |
|
| 196 | - # 1 -- no no (quiet overrules verbose) |
|
| 197 | - $verbose = $input->getOption('verbose'); |
|
| 198 | - $quiet = $input->getOption('quiet'); |
|
| 199 | - # restrict the verbosity level to VERBOSITY_VERBOSE |
|
| 200 | - if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
| 201 | - $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
|
| 202 | - } |
|
| 203 | - if ($quiet) { |
|
| 204 | - $verbose = false; |
|
| 205 | - } |
|
| 190 | + # no messaging level option means: no full printout but statistics |
|
| 191 | + # $quiet means no print at all |
|
| 192 | + # $verbose means full printout including statistics |
|
| 193 | + # -q -v full stat |
|
| 194 | + # 0 0 no yes |
|
| 195 | + # 0 1 yes yes |
|
| 196 | + # 1 -- no no (quiet overrules verbose) |
|
| 197 | + $verbose = $input->getOption('verbose'); |
|
| 198 | + $quiet = $input->getOption('quiet'); |
|
| 199 | + # restrict the verbosity level to VERBOSITY_VERBOSE |
|
| 200 | + if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
| 201 | + $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
|
| 202 | + } |
|
| 203 | + if ($quiet) { |
|
| 204 | + $verbose = false; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - # check quantity of users to be process and show it on the command line |
|
| 208 | - $users_total = count($users); |
|
| 209 | - if ($users_total === 0) { |
|
| 210 | - $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>"); |
|
| 211 | - return; |
|
| 212 | - } else { |
|
| 213 | - if ($users_total > 1) { |
|
| 214 | - $output->writeln("\nScanning files for $users_total users"); |
|
| 215 | - } |
|
| 216 | - } |
|
| 207 | + # check quantity of users to be process and show it on the command line |
|
| 208 | + $users_total = count($users); |
|
| 209 | + if ($users_total === 0) { |
|
| 210 | + $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>"); |
|
| 211 | + return; |
|
| 212 | + } else { |
|
| 213 | + if ($users_total > 1) { |
|
| 214 | + $output->writeln("\nScanning files for $users_total users"); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - $this->initTools(); |
|
| 218 | + $this->initTools(); |
|
| 219 | 219 | |
| 220 | - $user_count = 0; |
|
| 221 | - foreach ($users as $user) { |
|
| 222 | - if (is_object($user)) { |
|
| 223 | - $user = $user->getUID(); |
|
| 224 | - } |
|
| 225 | - $path = $inputPath ? $inputPath : '/' . $user; |
|
| 226 | - $user_count += 1; |
|
| 227 | - if ($this->userManager->userExists($user)) { |
|
| 228 | - # add an extra line when verbose is set to optical separate users |
|
| 229 | - if ($verbose) { |
|
| 230 | - $output->writeln(""); |
|
| 231 | - } |
|
| 232 | - $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); |
|
| 233 | - # full: printout data if $verbose was set |
|
| 234 | - $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned')); |
|
| 235 | - } else { |
|
| 236 | - $output->writeln("<error>Unknown user $user_count $user</error>"); |
|
| 237 | - } |
|
| 238 | - # check on each user if there was a user interrupt (ctrl-c) and exit foreach |
|
| 239 | - if ($this->hasBeenInterrupted()) { |
|
| 240 | - break; |
|
| 241 | - } |
|
| 242 | - } |
|
| 220 | + $user_count = 0; |
|
| 221 | + foreach ($users as $user) { |
|
| 222 | + if (is_object($user)) { |
|
| 223 | + $user = $user->getUID(); |
|
| 224 | + } |
|
| 225 | + $path = $inputPath ? $inputPath : '/' . $user; |
|
| 226 | + $user_count += 1; |
|
| 227 | + if ($this->userManager->userExists($user)) { |
|
| 228 | + # add an extra line when verbose is set to optical separate users |
|
| 229 | + if ($verbose) { |
|
| 230 | + $output->writeln(""); |
|
| 231 | + } |
|
| 232 | + $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); |
|
| 233 | + # full: printout data if $verbose was set |
|
| 234 | + $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned')); |
|
| 235 | + } else { |
|
| 236 | + $output->writeln("<error>Unknown user $user_count $user</error>"); |
|
| 237 | + } |
|
| 238 | + # check on each user if there was a user interrupt (ctrl-c) and exit foreach |
|
| 239 | + if ($this->hasBeenInterrupted()) { |
|
| 240 | + break; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - # stat: printout statistics if $quiet was not set |
|
| 245 | - if (!$quiet) { |
|
| 246 | - $this->presentStats($output); |
|
| 247 | - } |
|
| 248 | - } |
|
| 244 | + # stat: printout statistics if $quiet was not set |
|
| 245 | + if (!$quiet) { |
|
| 246 | + $this->presentStats($output); |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Initialises some useful tools for the Command |
|
| 252 | - */ |
|
| 253 | - protected function initTools() { |
|
| 254 | - // Start the timer |
|
| 255 | - $this->execTime = -microtime(true); |
|
| 256 | - // Convert PHP errors to exceptions |
|
| 257 | - set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
|
| 258 | - } |
|
| 250 | + /** |
|
| 251 | + * Initialises some useful tools for the Command |
|
| 252 | + */ |
|
| 253 | + protected function initTools() { |
|
| 254 | + // Start the timer |
|
| 255 | + $this->execTime = -microtime(true); |
|
| 256 | + // Convert PHP errors to exceptions |
|
| 257 | + set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Processes PHP errors as exceptions in order to be able to keep track of problems |
|
| 262 | - * |
|
| 263 | - * @see https://secure.php.net/manual/en/function.set-error-handler.php |
|
| 264 | - * |
|
| 265 | - * @param int $severity the level of the error raised |
|
| 266 | - * @param string $message |
|
| 267 | - * @param string $file the filename that the error was raised in |
|
| 268 | - * @param int $line the line number the error was raised |
|
| 269 | - * |
|
| 270 | - * @throws \ErrorException |
|
| 271 | - */ |
|
| 272 | - public function exceptionErrorHandler($severity, $message, $file, $line) { |
|
| 273 | - if (!(error_reporting() & $severity)) { |
|
| 274 | - // This error code is not included in error_reporting |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - throw new \ErrorException($message, 0, $severity, $file, $line); |
|
| 278 | - } |
|
| 260 | + /** |
|
| 261 | + * Processes PHP errors as exceptions in order to be able to keep track of problems |
|
| 262 | + * |
|
| 263 | + * @see https://secure.php.net/manual/en/function.set-error-handler.php |
|
| 264 | + * |
|
| 265 | + * @param int $severity the level of the error raised |
|
| 266 | + * @param string $message |
|
| 267 | + * @param string $file the filename that the error was raised in |
|
| 268 | + * @param int $line the line number the error was raised |
|
| 269 | + * |
|
| 270 | + * @throws \ErrorException |
|
| 271 | + */ |
|
| 272 | + public function exceptionErrorHandler($severity, $message, $file, $line) { |
|
| 273 | + if (!(error_reporting() & $severity)) { |
|
| 274 | + // This error code is not included in error_reporting |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + throw new \ErrorException($message, 0, $severity, $file, $line); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * @param OutputInterface $output |
|
| 282 | - */ |
|
| 283 | - protected function presentStats(OutputInterface $output) { |
|
| 284 | - // Stop the timer |
|
| 285 | - $this->execTime += microtime(true); |
|
| 286 | - $output->writeln(""); |
|
| 280 | + /** |
|
| 281 | + * @param OutputInterface $output |
|
| 282 | + */ |
|
| 283 | + protected function presentStats(OutputInterface $output) { |
|
| 284 | + // Stop the timer |
|
| 285 | + $this->execTime += microtime(true); |
|
| 286 | + $output->writeln(""); |
|
| 287 | 287 | |
| 288 | - $headers = [ |
|
| 289 | - 'Folders', 'Files', 'Elapsed time' |
|
| 290 | - ]; |
|
| 288 | + $headers = [ |
|
| 289 | + 'Folders', 'Files', 'Elapsed time' |
|
| 290 | + ]; |
|
| 291 | 291 | |
| 292 | - $this->showSummary($headers, null, $output); |
|
| 293 | - } |
|
| 292 | + $this->showSummary($headers, null, $output); |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Shows a summary of operations |
|
| 297 | - * |
|
| 298 | - * @param string[] $headers |
|
| 299 | - * @param string[] $rows |
|
| 300 | - * @param OutputInterface $output |
|
| 301 | - */ |
|
| 302 | - protected function showSummary($headers, $rows, OutputInterface $output) { |
|
| 303 | - $niceDate = $this->formatExecTime(); |
|
| 304 | - if (!$rows) { |
|
| 305 | - $rows = [ |
|
| 306 | - $this->foldersCounter, |
|
| 307 | - $this->filesCounter, |
|
| 308 | - $niceDate, |
|
| 309 | - ]; |
|
| 310 | - } |
|
| 311 | - $table = new Table($output); |
|
| 312 | - $table |
|
| 313 | - ->setHeaders($headers) |
|
| 314 | - ->setRows([$rows]); |
|
| 315 | - $table->render(); |
|
| 316 | - } |
|
| 295 | + /** |
|
| 296 | + * Shows a summary of operations |
|
| 297 | + * |
|
| 298 | + * @param string[] $headers |
|
| 299 | + * @param string[] $rows |
|
| 300 | + * @param OutputInterface $output |
|
| 301 | + */ |
|
| 302 | + protected function showSummary($headers, $rows, OutputInterface $output) { |
|
| 303 | + $niceDate = $this->formatExecTime(); |
|
| 304 | + if (!$rows) { |
|
| 305 | + $rows = [ |
|
| 306 | + $this->foldersCounter, |
|
| 307 | + $this->filesCounter, |
|
| 308 | + $niceDate, |
|
| 309 | + ]; |
|
| 310 | + } |
|
| 311 | + $table = new Table($output); |
|
| 312 | + $table |
|
| 313 | + ->setHeaders($headers) |
|
| 314 | + ->setRows([$rows]); |
|
| 315 | + $table->render(); |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | 318 | |
| 319 | - /** |
|
| 320 | - * Formats microtime into a human readable format |
|
| 321 | - * |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - protected function formatExecTime() { |
|
| 325 | - list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime))); |
|
| 319 | + /** |
|
| 320 | + * Formats microtime into a human readable format |
|
| 321 | + * |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + protected function formatExecTime() { |
|
| 325 | + list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime))); |
|
| 326 | 326 | |
| 327 | - # if you want to have microseconds add this: . '.' . $tens; |
|
| 328 | - return date('H:i:s', $secs); |
|
| 329 | - } |
|
| 327 | + # if you want to have microseconds add this: . '.' . $tens; |
|
| 328 | + return date('H:i:s', $secs); |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - /** |
|
| 332 | - * @return \OCP\IDBConnection |
|
| 333 | - */ |
|
| 334 | - protected function reconnectToDatabase(OutputInterface $output) { |
|
| 335 | - /** @var Connection | IDBConnection $connection */ |
|
| 336 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 337 | - try { |
|
| 338 | - $connection->close(); |
|
| 339 | - } catch (\Exception $ex) { |
|
| 340 | - $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
| 341 | - } |
|
| 342 | - while (!$connection->isConnected()) { |
|
| 343 | - try { |
|
| 344 | - $connection->connect(); |
|
| 345 | - } catch (\Exception $ex) { |
|
| 346 | - $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
| 347 | - sleep(60); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - return $connection; |
|
| 351 | - } |
|
| 331 | + /** |
|
| 332 | + * @return \OCP\IDBConnection |
|
| 333 | + */ |
|
| 334 | + protected function reconnectToDatabase(OutputInterface $output) { |
|
| 335 | + /** @var Connection | IDBConnection $connection */ |
|
| 336 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 337 | + try { |
|
| 338 | + $connection->close(); |
|
| 339 | + } catch (\Exception $ex) { |
|
| 340 | + $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
| 341 | + } |
|
| 342 | + while (!$connection->isConnected()) { |
|
| 343 | + try { |
|
| 344 | + $connection->connect(); |
|
| 345 | + } catch (\Exception $ex) { |
|
| 346 | + $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
| 347 | + sleep(60); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + return $connection; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | 353 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | $path = basename($fullPath); |
| 106 | 106 | |
| 107 | 107 | if ($normalizedPath !== $path) { |
| 108 | - $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>'); |
|
| 108 | + $output->writeln("\t<error>Entry \"".$fullPath.'" will not be accessible due to incompatible encoding</error>'); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | |
@@ -115,42 +115,42 @@ discard block |
||
| 115 | 115 | # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception |
| 116 | 116 | # printout and count |
| 117 | 117 | if ($verbose) { |
| 118 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 118 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 119 | 119 | $output->writeln("\tFile <info>$path</info>"); |
| 120 | 120 | $this->filesCounter += 1; |
| 121 | 121 | if ($this->hasBeenInterrupted()) { |
| 122 | 122 | throw new InterruptedException(); |
| 123 | 123 | } |
| 124 | 124 | }); |
| 125 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 125 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 126 | 126 | $output->writeln("\tFolder <info>$path</info>"); |
| 127 | 127 | $this->foldersCounter += 1; |
| 128 | 128 | if ($this->hasBeenInterrupted()) { |
| 129 | 129 | throw new InterruptedException(); |
| 130 | 130 | } |
| 131 | 131 | }); |
| 132 | - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { |
|
| 133 | - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); |
|
| 132 | + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function(StorageNotAvailableException $e) use ($output) { |
|
| 133 | + $output->writeln("Error while scanning, storage not available (".$e->getMessage().")"); |
|
| 134 | 134 | }); |
| 135 | 135 | # count only |
| 136 | 136 | } else { |
| 137 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { |
|
| 137 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function() use ($output) { |
|
| 138 | 138 | $this->filesCounter += 1; |
| 139 | 139 | if ($this->hasBeenInterrupted()) { |
| 140 | 140 | throw new InterruptedException(); |
| 141 | 141 | } |
| 142 | 142 | }); |
| 143 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { |
|
| 143 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function() use ($output) { |
|
| 144 | 144 | $this->foldersCounter += 1; |
| 145 | 145 | if ($this->hasBeenInterrupted()) { |
| 146 | 146 | throw new InterruptedException(); |
| 147 | 147 | } |
| 148 | 148 | }); |
| 149 | 149 | } |
| 150 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { |
|
| 150 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { |
|
| 151 | 151 | $this->checkScanWarning($path, $output); |
| 152 | 152 | }); |
| 153 | - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { |
|
| 153 | + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { |
|
| 154 | 154 | $this->checkScanWarning($path, $output); |
| 155 | 155 | }); |
| 156 | 156 | |
@@ -167,10 +167,10 @@ discard block |
||
| 167 | 167 | # exit the function if ctrl-c has been pressed |
| 168 | 168 | $output->writeln('Interrupted by user'); |
| 169 | 169 | } catch (NotFoundException $e) { |
| 170 | - $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
|
| 170 | + $output->writeln('<error>Path not found: '.$e->getMessage().'</error>'); |
|
| 171 | 171 | } catch (\Exception $e) { |
| 172 | - $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
|
| 173 | - $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
|
| 172 | + $output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>'); |
|
| 173 | + $output->writeln('<error>'.$e->getTraceAsString().'</error>'); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | protected function execute(InputInterface $input, OutputInterface $output) { |
| 179 | 179 | $inputPath = $input->getOption('path'); |
| 180 | 180 | if ($inputPath) { |
| 181 | - $inputPath = '/' . trim($inputPath, '/'); |
|
| 181 | + $inputPath = '/'.trim($inputPath, '/'); |
|
| 182 | 182 | list (, $user,) = explode('/', $inputPath, 3); |
| 183 | 183 | $users = array($user); |
| 184 | 184 | } else if ($input->getOption('all')) { |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | if (is_object($user)) { |
| 223 | 223 | $user = $user->getUID(); |
| 224 | 224 | } |
| 225 | - $path = $inputPath ? $inputPath : '/' . $user; |
|
| 225 | + $path = $inputPath ? $inputPath : '/'.$user; |
|
| 226 | 226 | $user_count += 1; |
| 227 | 227 | if ($this->userManager->userExists($user)) { |
| 228 | 228 | # add an extra line when verbose is set to optical separate users |
@@ -58,1383 +58,1383 @@ |
||
| 58 | 58 | */ |
| 59 | 59 | class Manager implements IManager { |
| 60 | 60 | |
| 61 | - /** @var IProviderFactory */ |
|
| 62 | - private $factory; |
|
| 63 | - /** @var ILogger */ |
|
| 64 | - private $logger; |
|
| 65 | - /** @var IConfig */ |
|
| 66 | - private $config; |
|
| 67 | - /** @var ISecureRandom */ |
|
| 68 | - private $secureRandom; |
|
| 69 | - /** @var IHasher */ |
|
| 70 | - private $hasher; |
|
| 71 | - /** @var IMountManager */ |
|
| 72 | - private $mountManager; |
|
| 73 | - /** @var IGroupManager */ |
|
| 74 | - private $groupManager; |
|
| 75 | - /** @var IL10N */ |
|
| 76 | - private $l; |
|
| 77 | - /** @var IUserManager */ |
|
| 78 | - private $userManager; |
|
| 79 | - /** @var IRootFolder */ |
|
| 80 | - private $rootFolder; |
|
| 81 | - /** @var CappedMemoryCache */ |
|
| 82 | - private $sharingDisabledForUsersCache; |
|
| 83 | - /** @var EventDispatcher */ |
|
| 84 | - private $eventDispatcher; |
|
| 85 | - /** @var LegacyHooks */ |
|
| 86 | - private $legacyHooks; |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Manager constructor. |
|
| 91 | - * |
|
| 92 | - * @param ILogger $logger |
|
| 93 | - * @param IConfig $config |
|
| 94 | - * @param ISecureRandom $secureRandom |
|
| 95 | - * @param IHasher $hasher |
|
| 96 | - * @param IMountManager $mountManager |
|
| 97 | - * @param IGroupManager $groupManager |
|
| 98 | - * @param IL10N $l |
|
| 99 | - * @param IProviderFactory $factory |
|
| 100 | - * @param IUserManager $userManager |
|
| 101 | - * @param IRootFolder $rootFolder |
|
| 102 | - * @param EventDispatcher $eventDispatcher |
|
| 103 | - */ |
|
| 104 | - public function __construct( |
|
| 105 | - ILogger $logger, |
|
| 106 | - IConfig $config, |
|
| 107 | - ISecureRandom $secureRandom, |
|
| 108 | - IHasher $hasher, |
|
| 109 | - IMountManager $mountManager, |
|
| 110 | - IGroupManager $groupManager, |
|
| 111 | - IL10N $l, |
|
| 112 | - IProviderFactory $factory, |
|
| 113 | - IUserManager $userManager, |
|
| 114 | - IRootFolder $rootFolder, |
|
| 115 | - EventDispatcher $eventDispatcher |
|
| 116 | - ) { |
|
| 117 | - $this->logger = $logger; |
|
| 118 | - $this->config = $config; |
|
| 119 | - $this->secureRandom = $secureRandom; |
|
| 120 | - $this->hasher = $hasher; |
|
| 121 | - $this->mountManager = $mountManager; |
|
| 122 | - $this->groupManager = $groupManager; |
|
| 123 | - $this->l = $l; |
|
| 124 | - $this->factory = $factory; |
|
| 125 | - $this->userManager = $userManager; |
|
| 126 | - $this->rootFolder = $rootFolder; |
|
| 127 | - $this->eventDispatcher = $eventDispatcher; |
|
| 128 | - $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
|
| 129 | - $this->legacyHooks = new LegacyHooks($this->eventDispatcher); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Convert from a full share id to a tuple (providerId, shareId) |
|
| 134 | - * |
|
| 135 | - * @param string $id |
|
| 136 | - * @return string[] |
|
| 137 | - */ |
|
| 138 | - private function splitFullId($id) { |
|
| 139 | - return explode(':', $id, 2); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Verify if a password meets all requirements |
|
| 144 | - * |
|
| 145 | - * @param string $password |
|
| 146 | - * @throws \Exception |
|
| 147 | - */ |
|
| 148 | - protected function verifyPassword($password) { |
|
| 149 | - if ($password === null) { |
|
| 150 | - // No password is set, check if this is allowed. |
|
| 151 | - if ($this->shareApiLinkEnforcePassword()) { |
|
| 152 | - throw new \InvalidArgumentException('Passwords are enforced for link shares'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // Let others verify the password |
|
| 159 | - try { |
|
| 160 | - $event = new GenericEvent($password); |
|
| 161 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 162 | - } catch (HintException $e) { |
|
| 163 | - throw new \Exception($e->getHint()); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Check for generic requirements before creating a share |
|
| 169 | - * |
|
| 170 | - * @param \OCP\Share\IShare $share |
|
| 171 | - * @throws \InvalidArgumentException |
|
| 172 | - * @throws GenericShareException |
|
| 173 | - */ |
|
| 174 | - protected function generalCreateChecks(\OCP\Share\IShare $share) { |
|
| 175 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 176 | - // We expect a valid user as sharedWith for user shares |
|
| 177 | - if (!$this->userManager->userExists($share->getSharedWith())) { |
|
| 178 | - throw new \InvalidArgumentException('SharedWith is not a valid user'); |
|
| 179 | - } |
|
| 180 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 181 | - // We expect a valid group as sharedWith for group shares |
|
| 182 | - if (!$this->groupManager->groupExists($share->getSharedWith())) { |
|
| 183 | - throw new \InvalidArgumentException('SharedWith is not a valid group'); |
|
| 184 | - } |
|
| 185 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 186 | - if ($share->getSharedWith() !== null) { |
|
| 187 | - throw new \InvalidArgumentException('SharedWith should be empty'); |
|
| 188 | - } |
|
| 189 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
| 190 | - if ($share->getSharedWith() === null) { |
|
| 191 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 192 | - } |
|
| 193 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 194 | - if ($share->getSharedWith() === null) { |
|
| 195 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 196 | - } |
|
| 197 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) { |
|
| 198 | - $circle = \OCA\Circles\Api\Circles::detailsCircle($share->getSharedWith()); |
|
| 199 | - if ($circle === null) { |
|
| 200 | - throw new \InvalidArgumentException('SharedWith is not a valid circle'); |
|
| 201 | - } |
|
| 202 | - } else { |
|
| 203 | - // We can't handle other types yet |
|
| 204 | - throw new \InvalidArgumentException('unknown share type'); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // Verify the initiator of the share is set |
|
| 208 | - if ($share->getSharedBy() === null) { |
|
| 209 | - throw new \InvalidArgumentException('SharedBy should be set'); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // Cannot share with yourself |
|
| 213 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 214 | - $share->getSharedWith() === $share->getSharedBy()) { |
|
| 215 | - throw new \InvalidArgumentException('Can\'t share with yourself'); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - // The path should be set |
|
| 219 | - if ($share->getNode() === null) { |
|
| 220 | - throw new \InvalidArgumentException('Path should be set'); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // And it should be a file or a folder |
|
| 224 | - if (!($share->getNode() instanceof \OCP\Files\File) && |
|
| 225 | - !($share->getNode() instanceof \OCP\Files\Folder)) { |
|
| 226 | - throw new \InvalidArgumentException('Path should be either a file or a folder'); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // And you can't share your rootfolder |
|
| 230 | - if ($this->userManager->userExists($share->getSharedBy())) { |
|
| 231 | - $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); |
|
| 232 | - } else { |
|
| 233 | - $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); |
|
| 234 | - } |
|
| 235 | - if ($sharedPath === $share->getNode()->getPath()) { |
|
| 236 | - throw new \InvalidArgumentException('You can\'t share your root folder'); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // Check if we actually have share permissions |
|
| 240 | - if (!$share->getNode()->isShareable()) { |
|
| 241 | - $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]); |
|
| 242 | - throw new GenericShareException($message_t, $message_t, 404); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - // Permissions should be set |
|
| 246 | - if ($share->getPermissions() === null) { |
|
| 247 | - throw new \InvalidArgumentException('A share requires permissions'); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /* |
|
| 61 | + /** @var IProviderFactory */ |
|
| 62 | + private $factory; |
|
| 63 | + /** @var ILogger */ |
|
| 64 | + private $logger; |
|
| 65 | + /** @var IConfig */ |
|
| 66 | + private $config; |
|
| 67 | + /** @var ISecureRandom */ |
|
| 68 | + private $secureRandom; |
|
| 69 | + /** @var IHasher */ |
|
| 70 | + private $hasher; |
|
| 71 | + /** @var IMountManager */ |
|
| 72 | + private $mountManager; |
|
| 73 | + /** @var IGroupManager */ |
|
| 74 | + private $groupManager; |
|
| 75 | + /** @var IL10N */ |
|
| 76 | + private $l; |
|
| 77 | + /** @var IUserManager */ |
|
| 78 | + private $userManager; |
|
| 79 | + /** @var IRootFolder */ |
|
| 80 | + private $rootFolder; |
|
| 81 | + /** @var CappedMemoryCache */ |
|
| 82 | + private $sharingDisabledForUsersCache; |
|
| 83 | + /** @var EventDispatcher */ |
|
| 84 | + private $eventDispatcher; |
|
| 85 | + /** @var LegacyHooks */ |
|
| 86 | + private $legacyHooks; |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Manager constructor. |
|
| 91 | + * |
|
| 92 | + * @param ILogger $logger |
|
| 93 | + * @param IConfig $config |
|
| 94 | + * @param ISecureRandom $secureRandom |
|
| 95 | + * @param IHasher $hasher |
|
| 96 | + * @param IMountManager $mountManager |
|
| 97 | + * @param IGroupManager $groupManager |
|
| 98 | + * @param IL10N $l |
|
| 99 | + * @param IProviderFactory $factory |
|
| 100 | + * @param IUserManager $userManager |
|
| 101 | + * @param IRootFolder $rootFolder |
|
| 102 | + * @param EventDispatcher $eventDispatcher |
|
| 103 | + */ |
|
| 104 | + public function __construct( |
|
| 105 | + ILogger $logger, |
|
| 106 | + IConfig $config, |
|
| 107 | + ISecureRandom $secureRandom, |
|
| 108 | + IHasher $hasher, |
|
| 109 | + IMountManager $mountManager, |
|
| 110 | + IGroupManager $groupManager, |
|
| 111 | + IL10N $l, |
|
| 112 | + IProviderFactory $factory, |
|
| 113 | + IUserManager $userManager, |
|
| 114 | + IRootFolder $rootFolder, |
|
| 115 | + EventDispatcher $eventDispatcher |
|
| 116 | + ) { |
|
| 117 | + $this->logger = $logger; |
|
| 118 | + $this->config = $config; |
|
| 119 | + $this->secureRandom = $secureRandom; |
|
| 120 | + $this->hasher = $hasher; |
|
| 121 | + $this->mountManager = $mountManager; |
|
| 122 | + $this->groupManager = $groupManager; |
|
| 123 | + $this->l = $l; |
|
| 124 | + $this->factory = $factory; |
|
| 125 | + $this->userManager = $userManager; |
|
| 126 | + $this->rootFolder = $rootFolder; |
|
| 127 | + $this->eventDispatcher = $eventDispatcher; |
|
| 128 | + $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
|
| 129 | + $this->legacyHooks = new LegacyHooks($this->eventDispatcher); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Convert from a full share id to a tuple (providerId, shareId) |
|
| 134 | + * |
|
| 135 | + * @param string $id |
|
| 136 | + * @return string[] |
|
| 137 | + */ |
|
| 138 | + private function splitFullId($id) { |
|
| 139 | + return explode(':', $id, 2); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Verify if a password meets all requirements |
|
| 144 | + * |
|
| 145 | + * @param string $password |
|
| 146 | + * @throws \Exception |
|
| 147 | + */ |
|
| 148 | + protected function verifyPassword($password) { |
|
| 149 | + if ($password === null) { |
|
| 150 | + // No password is set, check if this is allowed. |
|
| 151 | + if ($this->shareApiLinkEnforcePassword()) { |
|
| 152 | + throw new \InvalidArgumentException('Passwords are enforced for link shares'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // Let others verify the password |
|
| 159 | + try { |
|
| 160 | + $event = new GenericEvent($password); |
|
| 161 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
| 162 | + } catch (HintException $e) { |
|
| 163 | + throw new \Exception($e->getHint()); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Check for generic requirements before creating a share |
|
| 169 | + * |
|
| 170 | + * @param \OCP\Share\IShare $share |
|
| 171 | + * @throws \InvalidArgumentException |
|
| 172 | + * @throws GenericShareException |
|
| 173 | + */ |
|
| 174 | + protected function generalCreateChecks(\OCP\Share\IShare $share) { |
|
| 175 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 176 | + // We expect a valid user as sharedWith for user shares |
|
| 177 | + if (!$this->userManager->userExists($share->getSharedWith())) { |
|
| 178 | + throw new \InvalidArgumentException('SharedWith is not a valid user'); |
|
| 179 | + } |
|
| 180 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 181 | + // We expect a valid group as sharedWith for group shares |
|
| 182 | + if (!$this->groupManager->groupExists($share->getSharedWith())) { |
|
| 183 | + throw new \InvalidArgumentException('SharedWith is not a valid group'); |
|
| 184 | + } |
|
| 185 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 186 | + if ($share->getSharedWith() !== null) { |
|
| 187 | + throw new \InvalidArgumentException('SharedWith should be empty'); |
|
| 188 | + } |
|
| 189 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
| 190 | + if ($share->getSharedWith() === null) { |
|
| 191 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 192 | + } |
|
| 193 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 194 | + if ($share->getSharedWith() === null) { |
|
| 195 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
| 196 | + } |
|
| 197 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) { |
|
| 198 | + $circle = \OCA\Circles\Api\Circles::detailsCircle($share->getSharedWith()); |
|
| 199 | + if ($circle === null) { |
|
| 200 | + throw new \InvalidArgumentException('SharedWith is not a valid circle'); |
|
| 201 | + } |
|
| 202 | + } else { |
|
| 203 | + // We can't handle other types yet |
|
| 204 | + throw new \InvalidArgumentException('unknown share type'); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // Verify the initiator of the share is set |
|
| 208 | + if ($share->getSharedBy() === null) { |
|
| 209 | + throw new \InvalidArgumentException('SharedBy should be set'); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // Cannot share with yourself |
|
| 213 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 214 | + $share->getSharedWith() === $share->getSharedBy()) { |
|
| 215 | + throw new \InvalidArgumentException('Can\'t share with yourself'); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + // The path should be set |
|
| 219 | + if ($share->getNode() === null) { |
|
| 220 | + throw new \InvalidArgumentException('Path should be set'); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // And it should be a file or a folder |
|
| 224 | + if (!($share->getNode() instanceof \OCP\Files\File) && |
|
| 225 | + !($share->getNode() instanceof \OCP\Files\Folder)) { |
|
| 226 | + throw new \InvalidArgumentException('Path should be either a file or a folder'); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // And you can't share your rootfolder |
|
| 230 | + if ($this->userManager->userExists($share->getSharedBy())) { |
|
| 231 | + $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); |
|
| 232 | + } else { |
|
| 233 | + $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); |
|
| 234 | + } |
|
| 235 | + if ($sharedPath === $share->getNode()->getPath()) { |
|
| 236 | + throw new \InvalidArgumentException('You can\'t share your root folder'); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // Check if we actually have share permissions |
|
| 240 | + if (!$share->getNode()->isShareable()) { |
|
| 241 | + $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]); |
|
| 242 | + throw new GenericShareException($message_t, $message_t, 404); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + // Permissions should be set |
|
| 246 | + if ($share->getPermissions() === null) { |
|
| 247 | + throw new \InvalidArgumentException('A share requires permissions'); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /* |
|
| 251 | 251 | * Quick fix for #23536 |
| 252 | 252 | * Non moveable mount points do not have update and delete permissions |
| 253 | 253 | * while we 'most likely' do have that on the storage. |
| 254 | 254 | */ |
| 255 | - $permissions = $share->getNode()->getPermissions(); |
|
| 256 | - $mount = $share->getNode()->getMountPoint(); |
|
| 257 | - if (!($mount instanceof MoveableMount)) { |
|
| 258 | - $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - // Check that we do not share with more permissions than we have |
|
| 262 | - if ($share->getPermissions() & ~$permissions) { |
|
| 263 | - $message_t = $this->l->t('Cannot increase permissions of %s', [$share->getNode()->getPath()]); |
|
| 264 | - throw new GenericShareException($message_t, $message_t, 404); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - // Check that read permissions are always set |
|
| 269 | - // Link shares are allowed to have no read permissions to allow upload to hidden folders |
|
| 270 | - $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK |
|
| 271 | - || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 272 | - if (!$noReadPermissionRequired && |
|
| 273 | - ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { |
|
| 274 | - throw new \InvalidArgumentException('Shares need at least read permissions'); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 278 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { |
|
| 279 | - $message_t = $this->l->t('Files can\'t be shared with delete permissions'); |
|
| 280 | - throw new GenericShareException($message_t); |
|
| 281 | - } |
|
| 282 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { |
|
| 283 | - $message_t = $this->l->t('Files can\'t be shared with create permissions'); |
|
| 284 | - throw new GenericShareException($message_t); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Validate if the expiration date fits the system settings |
|
| 291 | - * |
|
| 292 | - * @param \OCP\Share\IShare $share The share to validate the expiration date of |
|
| 293 | - * @return \OCP\Share\IShare The modified share object |
|
| 294 | - * @throws GenericShareException |
|
| 295 | - * @throws \InvalidArgumentException |
|
| 296 | - * @throws \Exception |
|
| 297 | - */ |
|
| 298 | - protected function validateExpirationDate(\OCP\Share\IShare $share) { |
|
| 299 | - |
|
| 300 | - $expirationDate = $share->getExpirationDate(); |
|
| 301 | - |
|
| 302 | - if ($expirationDate !== null) { |
|
| 303 | - //Make sure the expiration date is a date |
|
| 304 | - $expirationDate->setTime(0, 0, 0); |
|
| 305 | - |
|
| 306 | - $date = new \DateTime(); |
|
| 307 | - $date->setTime(0, 0, 0); |
|
| 308 | - if ($date >= $expirationDate) { |
|
| 309 | - $message = $this->l->t('Expiration date is in the past'); |
|
| 310 | - throw new GenericShareException($message, $message, 404); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - // If expiredate is empty set a default one if there is a default |
|
| 315 | - $fullId = null; |
|
| 316 | - try { |
|
| 317 | - $fullId = $share->getFullId(); |
|
| 318 | - } catch (\UnexpectedValueException $e) { |
|
| 319 | - // This is a new share |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
|
| 323 | - $expirationDate = new \DateTime(); |
|
| 324 | - $expirationDate->setTime(0,0,0); |
|
| 325 | - $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - // If we enforce the expiration date check that is does not exceed |
|
| 329 | - if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
|
| 330 | - if ($expirationDate === null) { |
|
| 331 | - throw new \InvalidArgumentException('Expiration date is enforced'); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $date = new \DateTime(); |
|
| 335 | - $date->setTime(0, 0, 0); |
|
| 336 | - $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
| 337 | - if ($date < $expirationDate) { |
|
| 338 | - $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
|
| 339 | - throw new GenericShareException($message, $message, 404); |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - $accepted = true; |
|
| 344 | - $message = ''; |
|
| 345 | - \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
| 346 | - 'expirationDate' => &$expirationDate, |
|
| 347 | - 'accepted' => &$accepted, |
|
| 348 | - 'message' => &$message, |
|
| 349 | - 'passwordSet' => $share->getPassword() !== null, |
|
| 350 | - ]); |
|
| 351 | - |
|
| 352 | - if (!$accepted) { |
|
| 353 | - throw new \Exception($message); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $share->setExpirationDate($expirationDate); |
|
| 357 | - |
|
| 358 | - return $share; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Check for pre share requirements for user shares |
|
| 363 | - * |
|
| 364 | - * @param \OCP\Share\IShare $share |
|
| 365 | - * @throws \Exception |
|
| 366 | - */ |
|
| 367 | - protected function userCreateChecks(\OCP\Share\IShare $share) { |
|
| 368 | - // Check if we can share with group members only |
|
| 369 | - if ($this->shareWithGroupMembersOnly()) { |
|
| 370 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 371 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 372 | - // Verify we can share with this user |
|
| 373 | - $groups = array_intersect( |
|
| 374 | - $this->groupManager->getUserGroupIds($sharedBy), |
|
| 375 | - $this->groupManager->getUserGroupIds($sharedWith) |
|
| 376 | - ); |
|
| 377 | - if (empty($groups)) { |
|
| 378 | - throw new \Exception('Only sharing with group members is allowed'); |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /* |
|
| 255 | + $permissions = $share->getNode()->getPermissions(); |
|
| 256 | + $mount = $share->getNode()->getMountPoint(); |
|
| 257 | + if (!($mount instanceof MoveableMount)) { |
|
| 258 | + $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + // Check that we do not share with more permissions than we have |
|
| 262 | + if ($share->getPermissions() & ~$permissions) { |
|
| 263 | + $message_t = $this->l->t('Cannot increase permissions of %s', [$share->getNode()->getPath()]); |
|
| 264 | + throw new GenericShareException($message_t, $message_t, 404); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + // Check that read permissions are always set |
|
| 269 | + // Link shares are allowed to have no read permissions to allow upload to hidden folders |
|
| 270 | + $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK |
|
| 271 | + || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 272 | + if (!$noReadPermissionRequired && |
|
| 273 | + ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { |
|
| 274 | + throw new \InvalidArgumentException('Shares need at least read permissions'); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 278 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { |
|
| 279 | + $message_t = $this->l->t('Files can\'t be shared with delete permissions'); |
|
| 280 | + throw new GenericShareException($message_t); |
|
| 281 | + } |
|
| 282 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { |
|
| 283 | + $message_t = $this->l->t('Files can\'t be shared with create permissions'); |
|
| 284 | + throw new GenericShareException($message_t); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Validate if the expiration date fits the system settings |
|
| 291 | + * |
|
| 292 | + * @param \OCP\Share\IShare $share The share to validate the expiration date of |
|
| 293 | + * @return \OCP\Share\IShare The modified share object |
|
| 294 | + * @throws GenericShareException |
|
| 295 | + * @throws \InvalidArgumentException |
|
| 296 | + * @throws \Exception |
|
| 297 | + */ |
|
| 298 | + protected function validateExpirationDate(\OCP\Share\IShare $share) { |
|
| 299 | + |
|
| 300 | + $expirationDate = $share->getExpirationDate(); |
|
| 301 | + |
|
| 302 | + if ($expirationDate !== null) { |
|
| 303 | + //Make sure the expiration date is a date |
|
| 304 | + $expirationDate->setTime(0, 0, 0); |
|
| 305 | + |
|
| 306 | + $date = new \DateTime(); |
|
| 307 | + $date->setTime(0, 0, 0); |
|
| 308 | + if ($date >= $expirationDate) { |
|
| 309 | + $message = $this->l->t('Expiration date is in the past'); |
|
| 310 | + throw new GenericShareException($message, $message, 404); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + // If expiredate is empty set a default one if there is a default |
|
| 315 | + $fullId = null; |
|
| 316 | + try { |
|
| 317 | + $fullId = $share->getFullId(); |
|
| 318 | + } catch (\UnexpectedValueException $e) { |
|
| 319 | + // This is a new share |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
|
| 323 | + $expirationDate = new \DateTime(); |
|
| 324 | + $expirationDate->setTime(0,0,0); |
|
| 325 | + $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + // If we enforce the expiration date check that is does not exceed |
|
| 329 | + if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
|
| 330 | + if ($expirationDate === null) { |
|
| 331 | + throw new \InvalidArgumentException('Expiration date is enforced'); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $date = new \DateTime(); |
|
| 335 | + $date->setTime(0, 0, 0); |
|
| 336 | + $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
| 337 | + if ($date < $expirationDate) { |
|
| 338 | + $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
|
| 339 | + throw new GenericShareException($message, $message, 404); |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + $accepted = true; |
|
| 344 | + $message = ''; |
|
| 345 | + \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
| 346 | + 'expirationDate' => &$expirationDate, |
|
| 347 | + 'accepted' => &$accepted, |
|
| 348 | + 'message' => &$message, |
|
| 349 | + 'passwordSet' => $share->getPassword() !== null, |
|
| 350 | + ]); |
|
| 351 | + |
|
| 352 | + if (!$accepted) { |
|
| 353 | + throw new \Exception($message); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $share->setExpirationDate($expirationDate); |
|
| 357 | + |
|
| 358 | + return $share; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Check for pre share requirements for user shares |
|
| 363 | + * |
|
| 364 | + * @param \OCP\Share\IShare $share |
|
| 365 | + * @throws \Exception |
|
| 366 | + */ |
|
| 367 | + protected function userCreateChecks(\OCP\Share\IShare $share) { |
|
| 368 | + // Check if we can share with group members only |
|
| 369 | + if ($this->shareWithGroupMembersOnly()) { |
|
| 370 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 371 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 372 | + // Verify we can share with this user |
|
| 373 | + $groups = array_intersect( |
|
| 374 | + $this->groupManager->getUserGroupIds($sharedBy), |
|
| 375 | + $this->groupManager->getUserGroupIds($sharedWith) |
|
| 376 | + ); |
|
| 377 | + if (empty($groups)) { |
|
| 378 | + throw new \Exception('Only sharing with group members is allowed'); |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /* |
|
| 383 | 383 | * TODO: Could be costly, fix |
| 384 | 384 | * |
| 385 | 385 | * Also this is not what we want in the future.. then we want to squash identical shares. |
| 386 | 386 | */ |
| 387 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); |
|
| 388 | - $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 389 | - foreach($existingShares as $existingShare) { |
|
| 390 | - // Ignore if it is the same share |
|
| 391 | - try { |
|
| 392 | - if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 393 | - continue; |
|
| 394 | - } |
|
| 395 | - } catch (\UnexpectedValueException $e) { |
|
| 396 | - //Shares are not identical |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // Identical share already existst |
|
| 400 | - if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 401 | - throw new \Exception('Path already shared with this user'); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - // The share is already shared with this user via a group share |
|
| 405 | - if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 406 | - $group = $this->groupManager->get($existingShare->getSharedWith()); |
|
| 407 | - if (!is_null($group)) { |
|
| 408 | - $user = $this->userManager->get($share->getSharedWith()); |
|
| 409 | - |
|
| 410 | - if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { |
|
| 411 | - throw new \Exception('Path already shared with this user'); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Check for pre share requirements for group shares |
|
| 420 | - * |
|
| 421 | - * @param \OCP\Share\IShare $share |
|
| 422 | - * @throws \Exception |
|
| 423 | - */ |
|
| 424 | - protected function groupCreateChecks(\OCP\Share\IShare $share) { |
|
| 425 | - // Verify group shares are allowed |
|
| 426 | - if (!$this->allowGroupSharing()) { |
|
| 427 | - throw new \Exception('Group sharing is now allowed'); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // Verify if the user can share with this group |
|
| 431 | - if ($this->shareWithGroupMembersOnly()) { |
|
| 432 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 433 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 434 | - if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
|
| 435 | - throw new \Exception('Only sharing within your own groups is allowed'); |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /* |
|
| 387 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); |
|
| 388 | + $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 389 | + foreach($existingShares as $existingShare) { |
|
| 390 | + // Ignore if it is the same share |
|
| 391 | + try { |
|
| 392 | + if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 393 | + continue; |
|
| 394 | + } |
|
| 395 | + } catch (\UnexpectedValueException $e) { |
|
| 396 | + //Shares are not identical |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // Identical share already existst |
|
| 400 | + if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 401 | + throw new \Exception('Path already shared with this user'); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + // The share is already shared with this user via a group share |
|
| 405 | + if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 406 | + $group = $this->groupManager->get($existingShare->getSharedWith()); |
|
| 407 | + if (!is_null($group)) { |
|
| 408 | + $user = $this->userManager->get($share->getSharedWith()); |
|
| 409 | + |
|
| 410 | + if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { |
|
| 411 | + throw new \Exception('Path already shared with this user'); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Check for pre share requirements for group shares |
|
| 420 | + * |
|
| 421 | + * @param \OCP\Share\IShare $share |
|
| 422 | + * @throws \Exception |
|
| 423 | + */ |
|
| 424 | + protected function groupCreateChecks(\OCP\Share\IShare $share) { |
|
| 425 | + // Verify group shares are allowed |
|
| 426 | + if (!$this->allowGroupSharing()) { |
|
| 427 | + throw new \Exception('Group sharing is now allowed'); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // Verify if the user can share with this group |
|
| 431 | + if ($this->shareWithGroupMembersOnly()) { |
|
| 432 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 433 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 434 | + if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
|
| 435 | + throw new \Exception('Only sharing within your own groups is allowed'); |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /* |
|
| 440 | 440 | * TODO: Could be costly, fix |
| 441 | 441 | * |
| 442 | 442 | * Also this is not what we want in the future.. then we want to squash identical shares. |
| 443 | 443 | */ |
| 444 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 445 | - $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 446 | - foreach($existingShares as $existingShare) { |
|
| 447 | - try { |
|
| 448 | - if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 449 | - continue; |
|
| 450 | - } |
|
| 451 | - } catch (\UnexpectedValueException $e) { |
|
| 452 | - //It is a new share so just continue |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 456 | - throw new \Exception('Path already shared with this group'); |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Check for pre share requirements for link shares |
|
| 463 | - * |
|
| 464 | - * @param \OCP\Share\IShare $share |
|
| 465 | - * @throws \Exception |
|
| 466 | - */ |
|
| 467 | - protected function linkCreateChecks(\OCP\Share\IShare $share) { |
|
| 468 | - // Are link shares allowed? |
|
| 469 | - if (!$this->shareApiAllowLinks()) { |
|
| 470 | - throw new \Exception('Link sharing not allowed'); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // Link shares by definition can't have share permissions |
|
| 474 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { |
|
| 475 | - throw new \InvalidArgumentException('Link shares can\'t have reshare permissions'); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - // Check if public upload is allowed |
|
| 479 | - if (!$this->shareApiLinkAllowPublicUpload() && |
|
| 480 | - ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
|
| 481 | - throw new \InvalidArgumentException('Public upload not allowed'); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * To make sure we don't get invisible link shares we set the parent |
|
| 487 | - * of a link if it is a reshare. This is a quick word around |
|
| 488 | - * until we can properly display multiple link shares in the UI |
|
| 489 | - * |
|
| 490 | - * See: https://github.com/owncloud/core/issues/22295 |
|
| 491 | - * |
|
| 492 | - * FIXME: Remove once multiple link shares can be properly displayed |
|
| 493 | - * |
|
| 494 | - * @param \OCP\Share\IShare $share |
|
| 495 | - */ |
|
| 496 | - protected function setLinkParent(\OCP\Share\IShare $share) { |
|
| 497 | - |
|
| 498 | - // No sense in checking if the method is not there. |
|
| 499 | - if (method_exists($share, 'setParent')) { |
|
| 500 | - $storage = $share->getNode()->getStorage(); |
|
| 501 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 502 | - /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
| 503 | - $share->setParent($storage->getShareId()); |
|
| 504 | - } |
|
| 505 | - }; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * @param File|Folder $path |
|
| 510 | - */ |
|
| 511 | - protected function pathCreateChecks($path) { |
|
| 512 | - // Make sure that we do not share a path that contains a shared mountpoint |
|
| 513 | - if ($path instanceof \OCP\Files\Folder) { |
|
| 514 | - $mounts = $this->mountManager->findIn($path->getPath()); |
|
| 515 | - foreach($mounts as $mount) { |
|
| 516 | - if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 517 | - throw new \InvalidArgumentException('Path contains files shared with you'); |
|
| 518 | - } |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Check if the user that is sharing can actually share |
|
| 525 | - * |
|
| 526 | - * @param \OCP\Share\IShare $share |
|
| 527 | - * @throws \Exception |
|
| 528 | - */ |
|
| 529 | - protected function canShare(\OCP\Share\IShare $share) { |
|
| 530 | - if (!$this->shareApiEnabled()) { |
|
| 531 | - throw new \Exception('The share API is disabled'); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - if ($this->sharingDisabledForUser($share->getSharedBy())) { |
|
| 535 | - throw new \Exception('You are not allowed to share'); |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * Share a path |
|
| 541 | - * |
|
| 542 | - * @param \OCP\Share\IShare $share |
|
| 543 | - * @return Share The share object |
|
| 544 | - * @throws \Exception |
|
| 545 | - * |
|
| 546 | - * TODO: handle link share permissions or check them |
|
| 547 | - */ |
|
| 548 | - public function createShare(\OCP\Share\IShare $share) { |
|
| 549 | - $this->canShare($share); |
|
| 550 | - |
|
| 551 | - $this->generalCreateChecks($share); |
|
| 552 | - |
|
| 553 | - // Verify if there are any issues with the path |
|
| 554 | - $this->pathCreateChecks($share->getNode()); |
|
| 555 | - |
|
| 556 | - /* |
|
| 444 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 445 | + $existingShares = $provider->getSharesByPath($share->getNode()); |
|
| 446 | + foreach($existingShares as $existingShare) { |
|
| 447 | + try { |
|
| 448 | + if ($existingShare->getFullId() === $share->getFullId()) { |
|
| 449 | + continue; |
|
| 450 | + } |
|
| 451 | + } catch (\UnexpectedValueException $e) { |
|
| 452 | + //It is a new share so just continue |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
|
| 456 | + throw new \Exception('Path already shared with this group'); |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Check for pre share requirements for link shares |
|
| 463 | + * |
|
| 464 | + * @param \OCP\Share\IShare $share |
|
| 465 | + * @throws \Exception |
|
| 466 | + */ |
|
| 467 | + protected function linkCreateChecks(\OCP\Share\IShare $share) { |
|
| 468 | + // Are link shares allowed? |
|
| 469 | + if (!$this->shareApiAllowLinks()) { |
|
| 470 | + throw new \Exception('Link sharing not allowed'); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // Link shares by definition can't have share permissions |
|
| 474 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { |
|
| 475 | + throw new \InvalidArgumentException('Link shares can\'t have reshare permissions'); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + // Check if public upload is allowed |
|
| 479 | + if (!$this->shareApiLinkAllowPublicUpload() && |
|
| 480 | + ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
|
| 481 | + throw new \InvalidArgumentException('Public upload not allowed'); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * To make sure we don't get invisible link shares we set the parent |
|
| 487 | + * of a link if it is a reshare. This is a quick word around |
|
| 488 | + * until we can properly display multiple link shares in the UI |
|
| 489 | + * |
|
| 490 | + * See: https://github.com/owncloud/core/issues/22295 |
|
| 491 | + * |
|
| 492 | + * FIXME: Remove once multiple link shares can be properly displayed |
|
| 493 | + * |
|
| 494 | + * @param \OCP\Share\IShare $share |
|
| 495 | + */ |
|
| 496 | + protected function setLinkParent(\OCP\Share\IShare $share) { |
|
| 497 | + |
|
| 498 | + // No sense in checking if the method is not there. |
|
| 499 | + if (method_exists($share, 'setParent')) { |
|
| 500 | + $storage = $share->getNode()->getStorage(); |
|
| 501 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 502 | + /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
| 503 | + $share->setParent($storage->getShareId()); |
|
| 504 | + } |
|
| 505 | + }; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * @param File|Folder $path |
|
| 510 | + */ |
|
| 511 | + protected function pathCreateChecks($path) { |
|
| 512 | + // Make sure that we do not share a path that contains a shared mountpoint |
|
| 513 | + if ($path instanceof \OCP\Files\Folder) { |
|
| 514 | + $mounts = $this->mountManager->findIn($path->getPath()); |
|
| 515 | + foreach($mounts as $mount) { |
|
| 516 | + if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 517 | + throw new \InvalidArgumentException('Path contains files shared with you'); |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Check if the user that is sharing can actually share |
|
| 525 | + * |
|
| 526 | + * @param \OCP\Share\IShare $share |
|
| 527 | + * @throws \Exception |
|
| 528 | + */ |
|
| 529 | + protected function canShare(\OCP\Share\IShare $share) { |
|
| 530 | + if (!$this->shareApiEnabled()) { |
|
| 531 | + throw new \Exception('The share API is disabled'); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + if ($this->sharingDisabledForUser($share->getSharedBy())) { |
|
| 535 | + throw new \Exception('You are not allowed to share'); |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * Share a path |
|
| 541 | + * |
|
| 542 | + * @param \OCP\Share\IShare $share |
|
| 543 | + * @return Share The share object |
|
| 544 | + * @throws \Exception |
|
| 545 | + * |
|
| 546 | + * TODO: handle link share permissions or check them |
|
| 547 | + */ |
|
| 548 | + public function createShare(\OCP\Share\IShare $share) { |
|
| 549 | + $this->canShare($share); |
|
| 550 | + |
|
| 551 | + $this->generalCreateChecks($share); |
|
| 552 | + |
|
| 553 | + // Verify if there are any issues with the path |
|
| 554 | + $this->pathCreateChecks($share->getNode()); |
|
| 555 | + |
|
| 556 | + /* |
|
| 557 | 557 | * On creation of a share the owner is always the owner of the path |
| 558 | 558 | * Except for mounted federated shares. |
| 559 | 559 | */ |
| 560 | - $storage = $share->getNode()->getStorage(); |
|
| 561 | - if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 562 | - $parent = $share->getNode()->getParent(); |
|
| 563 | - while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 564 | - $parent = $parent->getParent(); |
|
| 565 | - } |
|
| 566 | - $share->setShareOwner($parent->getOwner()->getUID()); |
|
| 567 | - } else { |
|
| 568 | - $share->setShareOwner($share->getNode()->getOwner()->getUID()); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - //Verify share type |
|
| 572 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 573 | - $this->userCreateChecks($share); |
|
| 574 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 575 | - $this->groupCreateChecks($share); |
|
| 576 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 577 | - $this->linkCreateChecks($share); |
|
| 578 | - $this->setLinkParent($share); |
|
| 579 | - |
|
| 580 | - /* |
|
| 560 | + $storage = $share->getNode()->getStorage(); |
|
| 561 | + if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 562 | + $parent = $share->getNode()->getParent(); |
|
| 563 | + while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 564 | + $parent = $parent->getParent(); |
|
| 565 | + } |
|
| 566 | + $share->setShareOwner($parent->getOwner()->getUID()); |
|
| 567 | + } else { |
|
| 568 | + $share->setShareOwner($share->getNode()->getOwner()->getUID()); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + //Verify share type |
|
| 572 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 573 | + $this->userCreateChecks($share); |
|
| 574 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 575 | + $this->groupCreateChecks($share); |
|
| 576 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 577 | + $this->linkCreateChecks($share); |
|
| 578 | + $this->setLinkParent($share); |
|
| 579 | + |
|
| 580 | + /* |
|
| 581 | 581 | * For now ignore a set token. |
| 582 | 582 | */ |
| 583 | - $share->setToken( |
|
| 584 | - $this->secureRandom->generate( |
|
| 585 | - \OC\Share\Constants::TOKEN_LENGTH, |
|
| 586 | - \OCP\Security\ISecureRandom::CHAR_LOWER. |
|
| 587 | - \OCP\Security\ISecureRandom::CHAR_UPPER. |
|
| 588 | - \OCP\Security\ISecureRandom::CHAR_DIGITS |
|
| 589 | - ) |
|
| 590 | - ); |
|
| 591 | - |
|
| 592 | - //Verify the expiration date |
|
| 593 | - $this->validateExpirationDate($share); |
|
| 594 | - |
|
| 595 | - //Verify the password |
|
| 596 | - $this->verifyPassword($share->getPassword()); |
|
| 597 | - |
|
| 598 | - // If a password is set. Hash it! |
|
| 599 | - if ($share->getPassword() !== null) { |
|
| 600 | - $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 601 | - } |
|
| 602 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 603 | - $share->setToken( |
|
| 604 | - $this->secureRandom->generate( |
|
| 605 | - \OC\Share\Constants::TOKEN_LENGTH, |
|
| 606 | - \OCP\Security\ISecureRandom::CHAR_LOWER. |
|
| 607 | - \OCP\Security\ISecureRandom::CHAR_UPPER. |
|
| 608 | - \OCP\Security\ISecureRandom::CHAR_DIGITS |
|
| 609 | - ) |
|
| 610 | - ); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - // Cannot share with the owner |
|
| 614 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 615 | - $share->getSharedWith() === $share->getShareOwner()) { |
|
| 616 | - throw new \InvalidArgumentException('Can\'t share with the share owner'); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - // Generate the target |
|
| 620 | - $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); |
|
| 621 | - $target = \OC\Files\Filesystem::normalizePath($target); |
|
| 622 | - $share->setTarget($target); |
|
| 623 | - |
|
| 624 | - // Pre share hook |
|
| 625 | - $run = true; |
|
| 626 | - $error = ''; |
|
| 627 | - $preHookData = [ |
|
| 628 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 629 | - 'itemSource' => $share->getNode()->getId(), |
|
| 630 | - 'shareType' => $share->getShareType(), |
|
| 631 | - 'uidOwner' => $share->getSharedBy(), |
|
| 632 | - 'permissions' => $share->getPermissions(), |
|
| 633 | - 'fileSource' => $share->getNode()->getId(), |
|
| 634 | - 'expiration' => $share->getExpirationDate(), |
|
| 635 | - 'token' => $share->getToken(), |
|
| 636 | - 'itemTarget' => $share->getTarget(), |
|
| 637 | - 'shareWith' => $share->getSharedWith(), |
|
| 638 | - 'run' => &$run, |
|
| 639 | - 'error' => &$error, |
|
| 640 | - ]; |
|
| 641 | - \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); |
|
| 642 | - |
|
| 643 | - if ($run === false) { |
|
| 644 | - throw new \Exception($error); |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - $oldShare = $share; |
|
| 648 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 649 | - $share = $provider->create($share); |
|
| 650 | - //reuse the node we already have |
|
| 651 | - $share->setNode($oldShare->getNode()); |
|
| 652 | - |
|
| 653 | - // Post share hook |
|
| 654 | - $postHookData = [ |
|
| 655 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 656 | - 'itemSource' => $share->getNode()->getId(), |
|
| 657 | - 'shareType' => $share->getShareType(), |
|
| 658 | - 'uidOwner' => $share->getSharedBy(), |
|
| 659 | - 'permissions' => $share->getPermissions(), |
|
| 660 | - 'fileSource' => $share->getNode()->getId(), |
|
| 661 | - 'expiration' => $share->getExpirationDate(), |
|
| 662 | - 'token' => $share->getToken(), |
|
| 663 | - 'id' => $share->getId(), |
|
| 664 | - 'shareWith' => $share->getSharedWith(), |
|
| 665 | - 'itemTarget' => $share->getTarget(), |
|
| 666 | - 'fileTarget' => $share->getTarget(), |
|
| 667 | - ]; |
|
| 668 | - |
|
| 669 | - \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); |
|
| 670 | - |
|
| 671 | - return $share; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Update a share |
|
| 676 | - * |
|
| 677 | - * @param \OCP\Share\IShare $share |
|
| 678 | - * @return \OCP\Share\IShare The share object |
|
| 679 | - * @throws \InvalidArgumentException |
|
| 680 | - */ |
|
| 681 | - public function updateShare(\OCP\Share\IShare $share) { |
|
| 682 | - $expirationDateUpdated = false; |
|
| 683 | - |
|
| 684 | - $this->canShare($share); |
|
| 685 | - |
|
| 686 | - try { |
|
| 687 | - $originalShare = $this->getShareById($share->getFullId()); |
|
| 688 | - } catch (\UnexpectedValueException $e) { |
|
| 689 | - throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - // We can't change the share type! |
|
| 693 | - if ($share->getShareType() !== $originalShare->getShareType()) { |
|
| 694 | - throw new \InvalidArgumentException('Can\'t change share type'); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - // We can only change the recipient on user shares |
|
| 698 | - if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
|
| 699 | - $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { |
|
| 700 | - throw new \InvalidArgumentException('Can only update recipient on user shares'); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - // Cannot share with the owner |
|
| 704 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 705 | - $share->getSharedWith() === $share->getShareOwner()) { |
|
| 706 | - throw new \InvalidArgumentException('Can\'t share with the share owner'); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - $this->generalCreateChecks($share); |
|
| 710 | - |
|
| 711 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 712 | - $this->userCreateChecks($share); |
|
| 713 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 714 | - $this->groupCreateChecks($share); |
|
| 715 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 716 | - $this->linkCreateChecks($share); |
|
| 717 | - |
|
| 718 | - // Password updated. |
|
| 719 | - if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 720 | - //Verify the password |
|
| 721 | - $this->verifyPassword($share->getPassword()); |
|
| 722 | - |
|
| 723 | - // If a password is set. Hash it! |
|
| 724 | - if ($share->getPassword() !== null) { |
|
| 725 | - $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 726 | - } |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
| 730 | - //Verify the expiration date |
|
| 731 | - $this->validateExpirationDate($share); |
|
| 732 | - $expirationDateUpdated = true; |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - $plainTextPassword = null; |
|
| 737 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 738 | - // Password updated. |
|
| 739 | - if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 740 | - //Verify the password |
|
| 741 | - $this->verifyPassword($share->getPassword()); |
|
| 742 | - |
|
| 743 | - // If a password is set. Hash it! |
|
| 744 | - if ($share->getPassword() !== null) { |
|
| 745 | - $plainTextPassword = $share->getPassword(); |
|
| 746 | - $share->setPassword($this->hasher->hash($plainTextPassword)); |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - $this->pathCreateChecks($share->getNode()); |
|
| 752 | - |
|
| 753 | - // Now update the share! |
|
| 754 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 755 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 756 | - $share = $provider->update($share, $plainTextPassword); |
|
| 757 | - } else { |
|
| 758 | - $share = $provider->update($share); |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - if ($expirationDateUpdated === true) { |
|
| 762 | - \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [ |
|
| 763 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 764 | - 'itemSource' => $share->getNode()->getId(), |
|
| 765 | - 'date' => $share->getExpirationDate(), |
|
| 766 | - 'uidOwner' => $share->getSharedBy(), |
|
| 767 | - ]); |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 771 | - \OC_Hook::emit('OCP\Share', 'post_update_password', [ |
|
| 772 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 773 | - 'itemSource' => $share->getNode()->getId(), |
|
| 774 | - 'uidOwner' => $share->getSharedBy(), |
|
| 775 | - 'token' => $share->getToken(), |
|
| 776 | - 'disabled' => is_null($share->getPassword()), |
|
| 777 | - ]); |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - if ($share->getPermissions() !== $originalShare->getPermissions()) { |
|
| 781 | - if ($this->userManager->userExists($share->getShareOwner())) { |
|
| 782 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 783 | - } else { |
|
| 784 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 785 | - } |
|
| 786 | - \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 787 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 788 | - 'itemSource' => $share->getNode()->getId(), |
|
| 789 | - 'shareType' => $share->getShareType(), |
|
| 790 | - 'shareWith' => $share->getSharedWith(), |
|
| 791 | - 'uidOwner' => $share->getSharedBy(), |
|
| 792 | - 'permissions' => $share->getPermissions(), |
|
| 793 | - 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
|
| 794 | - )); |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - return $share; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * Delete all the children of this share |
|
| 802 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 803 | - * |
|
| 804 | - * @param \OCP\Share\IShare $share |
|
| 805 | - * @return \OCP\Share\IShare[] List of deleted shares |
|
| 806 | - */ |
|
| 807 | - protected function deleteChildren(\OCP\Share\IShare $share) { |
|
| 808 | - $deletedShares = []; |
|
| 809 | - |
|
| 810 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 811 | - |
|
| 812 | - foreach ($provider->getChildren($share) as $child) { |
|
| 813 | - $deletedChildren = $this->deleteChildren($child); |
|
| 814 | - $deletedShares = array_merge($deletedShares, $deletedChildren); |
|
| 815 | - |
|
| 816 | - $provider->delete($child); |
|
| 817 | - $deletedShares[] = $child; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - return $deletedShares; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Delete a share |
|
| 825 | - * |
|
| 826 | - * @param \OCP\Share\IShare $share |
|
| 827 | - * @throws ShareNotFound |
|
| 828 | - * @throws \InvalidArgumentException |
|
| 829 | - */ |
|
| 830 | - public function deleteShare(\OCP\Share\IShare $share) { |
|
| 831 | - |
|
| 832 | - try { |
|
| 833 | - $share->getFullId(); |
|
| 834 | - } catch (\UnexpectedValueException $e) { |
|
| 835 | - throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - $event = new GenericEvent($share); |
|
| 839 | - $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); |
|
| 840 | - |
|
| 841 | - // Get all children and delete them as well |
|
| 842 | - $deletedShares = $this->deleteChildren($share); |
|
| 843 | - |
|
| 844 | - // Do the actual delete |
|
| 845 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 846 | - $provider->delete($share); |
|
| 847 | - |
|
| 848 | - // All the deleted shares caused by this delete |
|
| 849 | - $deletedShares[] = $share; |
|
| 850 | - |
|
| 851 | - // Emit post hook |
|
| 852 | - $event->setArgument('deletedShares', $deletedShares); |
|
| 853 | - $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * Unshare a file as the recipient. |
|
| 859 | - * This can be different from a regular delete for example when one of |
|
| 860 | - * the users in a groups deletes that share. But the provider should |
|
| 861 | - * handle this. |
|
| 862 | - * |
|
| 863 | - * @param \OCP\Share\IShare $share |
|
| 864 | - * @param string $recipientId |
|
| 865 | - */ |
|
| 866 | - public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
|
| 867 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 868 | - $provider = $this->factory->getProvider($providerId); |
|
| 869 | - |
|
| 870 | - $provider->deleteFromSelf($share, $recipientId); |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * @inheritdoc |
|
| 875 | - */ |
|
| 876 | - public function moveShare(\OCP\Share\IShare $share, $recipientId) { |
|
| 877 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 878 | - throw new \InvalidArgumentException('Can\'t change target of link share'); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { |
|
| 882 | - throw new \InvalidArgumentException('Invalid recipient'); |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 886 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 887 | - if (is_null($sharedWith)) { |
|
| 888 | - throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 889 | - } |
|
| 890 | - $recipient = $this->userManager->get($recipientId); |
|
| 891 | - if (!$sharedWith->inGroup($recipient)) { |
|
| 892 | - throw new \InvalidArgumentException('Invalid recipient'); |
|
| 893 | - } |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 897 | - $provider = $this->factory->getProvider($providerId); |
|
| 898 | - |
|
| 899 | - $provider->move($share, $recipientId); |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
|
| 903 | - $providers = $this->factory->getAllProviders(); |
|
| 904 | - |
|
| 905 | - return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
|
| 906 | - $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
|
| 907 | - foreach ($newShares as $fid => $data) { |
|
| 908 | - if (!isset($shares[$fid])) { |
|
| 909 | - $shares[$fid] = []; |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - $shares[$fid] = array_merge($shares[$fid], $data); |
|
| 913 | - } |
|
| 914 | - return $shares; |
|
| 915 | - }, []); |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * @inheritdoc |
|
| 920 | - */ |
|
| 921 | - public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
|
| 922 | - if ($path !== null && |
|
| 923 | - !($path instanceof \OCP\Files\File) && |
|
| 924 | - !($path instanceof \OCP\Files\Folder)) { |
|
| 925 | - throw new \InvalidArgumentException('invalid path'); |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - try { |
|
| 929 | - $provider = $this->factory->getProviderForType($shareType); |
|
| 930 | - } catch (ProviderException $e) { |
|
| 931 | - return []; |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 935 | - |
|
| 936 | - /* |
|
| 583 | + $share->setToken( |
|
| 584 | + $this->secureRandom->generate( |
|
| 585 | + \OC\Share\Constants::TOKEN_LENGTH, |
|
| 586 | + \OCP\Security\ISecureRandom::CHAR_LOWER. |
|
| 587 | + \OCP\Security\ISecureRandom::CHAR_UPPER. |
|
| 588 | + \OCP\Security\ISecureRandom::CHAR_DIGITS |
|
| 589 | + ) |
|
| 590 | + ); |
|
| 591 | + |
|
| 592 | + //Verify the expiration date |
|
| 593 | + $this->validateExpirationDate($share); |
|
| 594 | + |
|
| 595 | + //Verify the password |
|
| 596 | + $this->verifyPassword($share->getPassword()); |
|
| 597 | + |
|
| 598 | + // If a password is set. Hash it! |
|
| 599 | + if ($share->getPassword() !== null) { |
|
| 600 | + $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 601 | + } |
|
| 602 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 603 | + $share->setToken( |
|
| 604 | + $this->secureRandom->generate( |
|
| 605 | + \OC\Share\Constants::TOKEN_LENGTH, |
|
| 606 | + \OCP\Security\ISecureRandom::CHAR_LOWER. |
|
| 607 | + \OCP\Security\ISecureRandom::CHAR_UPPER. |
|
| 608 | + \OCP\Security\ISecureRandom::CHAR_DIGITS |
|
| 609 | + ) |
|
| 610 | + ); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + // Cannot share with the owner |
|
| 614 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 615 | + $share->getSharedWith() === $share->getShareOwner()) { |
|
| 616 | + throw new \InvalidArgumentException('Can\'t share with the share owner'); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + // Generate the target |
|
| 620 | + $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); |
|
| 621 | + $target = \OC\Files\Filesystem::normalizePath($target); |
|
| 622 | + $share->setTarget($target); |
|
| 623 | + |
|
| 624 | + // Pre share hook |
|
| 625 | + $run = true; |
|
| 626 | + $error = ''; |
|
| 627 | + $preHookData = [ |
|
| 628 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 629 | + 'itemSource' => $share->getNode()->getId(), |
|
| 630 | + 'shareType' => $share->getShareType(), |
|
| 631 | + 'uidOwner' => $share->getSharedBy(), |
|
| 632 | + 'permissions' => $share->getPermissions(), |
|
| 633 | + 'fileSource' => $share->getNode()->getId(), |
|
| 634 | + 'expiration' => $share->getExpirationDate(), |
|
| 635 | + 'token' => $share->getToken(), |
|
| 636 | + 'itemTarget' => $share->getTarget(), |
|
| 637 | + 'shareWith' => $share->getSharedWith(), |
|
| 638 | + 'run' => &$run, |
|
| 639 | + 'error' => &$error, |
|
| 640 | + ]; |
|
| 641 | + \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); |
|
| 642 | + |
|
| 643 | + if ($run === false) { |
|
| 644 | + throw new \Exception($error); |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + $oldShare = $share; |
|
| 648 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 649 | + $share = $provider->create($share); |
|
| 650 | + //reuse the node we already have |
|
| 651 | + $share->setNode($oldShare->getNode()); |
|
| 652 | + |
|
| 653 | + // Post share hook |
|
| 654 | + $postHookData = [ |
|
| 655 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 656 | + 'itemSource' => $share->getNode()->getId(), |
|
| 657 | + 'shareType' => $share->getShareType(), |
|
| 658 | + 'uidOwner' => $share->getSharedBy(), |
|
| 659 | + 'permissions' => $share->getPermissions(), |
|
| 660 | + 'fileSource' => $share->getNode()->getId(), |
|
| 661 | + 'expiration' => $share->getExpirationDate(), |
|
| 662 | + 'token' => $share->getToken(), |
|
| 663 | + 'id' => $share->getId(), |
|
| 664 | + 'shareWith' => $share->getSharedWith(), |
|
| 665 | + 'itemTarget' => $share->getTarget(), |
|
| 666 | + 'fileTarget' => $share->getTarget(), |
|
| 667 | + ]; |
|
| 668 | + |
|
| 669 | + \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); |
|
| 670 | + |
|
| 671 | + return $share; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Update a share |
|
| 676 | + * |
|
| 677 | + * @param \OCP\Share\IShare $share |
|
| 678 | + * @return \OCP\Share\IShare The share object |
|
| 679 | + * @throws \InvalidArgumentException |
|
| 680 | + */ |
|
| 681 | + public function updateShare(\OCP\Share\IShare $share) { |
|
| 682 | + $expirationDateUpdated = false; |
|
| 683 | + |
|
| 684 | + $this->canShare($share); |
|
| 685 | + |
|
| 686 | + try { |
|
| 687 | + $originalShare = $this->getShareById($share->getFullId()); |
|
| 688 | + } catch (\UnexpectedValueException $e) { |
|
| 689 | + throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + // We can't change the share type! |
|
| 693 | + if ($share->getShareType() !== $originalShare->getShareType()) { |
|
| 694 | + throw new \InvalidArgumentException('Can\'t change share type'); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + // We can only change the recipient on user shares |
|
| 698 | + if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
|
| 699 | + $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { |
|
| 700 | + throw new \InvalidArgumentException('Can only update recipient on user shares'); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + // Cannot share with the owner |
|
| 704 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
| 705 | + $share->getSharedWith() === $share->getShareOwner()) { |
|
| 706 | + throw new \InvalidArgumentException('Can\'t share with the share owner'); |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + $this->generalCreateChecks($share); |
|
| 710 | + |
|
| 711 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 712 | + $this->userCreateChecks($share); |
|
| 713 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 714 | + $this->groupCreateChecks($share); |
|
| 715 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 716 | + $this->linkCreateChecks($share); |
|
| 717 | + |
|
| 718 | + // Password updated. |
|
| 719 | + if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 720 | + //Verify the password |
|
| 721 | + $this->verifyPassword($share->getPassword()); |
|
| 722 | + |
|
| 723 | + // If a password is set. Hash it! |
|
| 724 | + if ($share->getPassword() !== null) { |
|
| 725 | + $share->setPassword($this->hasher->hash($share->getPassword())); |
|
| 726 | + } |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
| 730 | + //Verify the expiration date |
|
| 731 | + $this->validateExpirationDate($share); |
|
| 732 | + $expirationDateUpdated = true; |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + $plainTextPassword = null; |
|
| 737 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 738 | + // Password updated. |
|
| 739 | + if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 740 | + //Verify the password |
|
| 741 | + $this->verifyPassword($share->getPassword()); |
|
| 742 | + |
|
| 743 | + // If a password is set. Hash it! |
|
| 744 | + if ($share->getPassword() !== null) { |
|
| 745 | + $plainTextPassword = $share->getPassword(); |
|
| 746 | + $share->setPassword($this->hasher->hash($plainTextPassword)); |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + $this->pathCreateChecks($share->getNode()); |
|
| 752 | + |
|
| 753 | + // Now update the share! |
|
| 754 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 755 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 756 | + $share = $provider->update($share, $plainTextPassword); |
|
| 757 | + } else { |
|
| 758 | + $share = $provider->update($share); |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + if ($expirationDateUpdated === true) { |
|
| 762 | + \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [ |
|
| 763 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 764 | + 'itemSource' => $share->getNode()->getId(), |
|
| 765 | + 'date' => $share->getExpirationDate(), |
|
| 766 | + 'uidOwner' => $share->getSharedBy(), |
|
| 767 | + ]); |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + if ($share->getPassword() !== $originalShare->getPassword()) { |
|
| 771 | + \OC_Hook::emit('OCP\Share', 'post_update_password', [ |
|
| 772 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 773 | + 'itemSource' => $share->getNode()->getId(), |
|
| 774 | + 'uidOwner' => $share->getSharedBy(), |
|
| 775 | + 'token' => $share->getToken(), |
|
| 776 | + 'disabled' => is_null($share->getPassword()), |
|
| 777 | + ]); |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + if ($share->getPermissions() !== $originalShare->getPermissions()) { |
|
| 781 | + if ($this->userManager->userExists($share->getShareOwner())) { |
|
| 782 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 783 | + } else { |
|
| 784 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 785 | + } |
|
| 786 | + \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 787 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
| 788 | + 'itemSource' => $share->getNode()->getId(), |
|
| 789 | + 'shareType' => $share->getShareType(), |
|
| 790 | + 'shareWith' => $share->getSharedWith(), |
|
| 791 | + 'uidOwner' => $share->getSharedBy(), |
|
| 792 | + 'permissions' => $share->getPermissions(), |
|
| 793 | + 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
|
| 794 | + )); |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + return $share; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * Delete all the children of this share |
|
| 802 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 803 | + * |
|
| 804 | + * @param \OCP\Share\IShare $share |
|
| 805 | + * @return \OCP\Share\IShare[] List of deleted shares |
|
| 806 | + */ |
|
| 807 | + protected function deleteChildren(\OCP\Share\IShare $share) { |
|
| 808 | + $deletedShares = []; |
|
| 809 | + |
|
| 810 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 811 | + |
|
| 812 | + foreach ($provider->getChildren($share) as $child) { |
|
| 813 | + $deletedChildren = $this->deleteChildren($child); |
|
| 814 | + $deletedShares = array_merge($deletedShares, $deletedChildren); |
|
| 815 | + |
|
| 816 | + $provider->delete($child); |
|
| 817 | + $deletedShares[] = $child; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + return $deletedShares; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Delete a share |
|
| 825 | + * |
|
| 826 | + * @param \OCP\Share\IShare $share |
|
| 827 | + * @throws ShareNotFound |
|
| 828 | + * @throws \InvalidArgumentException |
|
| 829 | + */ |
|
| 830 | + public function deleteShare(\OCP\Share\IShare $share) { |
|
| 831 | + |
|
| 832 | + try { |
|
| 833 | + $share->getFullId(); |
|
| 834 | + } catch (\UnexpectedValueException $e) { |
|
| 835 | + throw new \InvalidArgumentException('Share does not have a full id'); |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + $event = new GenericEvent($share); |
|
| 839 | + $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); |
|
| 840 | + |
|
| 841 | + // Get all children and delete them as well |
|
| 842 | + $deletedShares = $this->deleteChildren($share); |
|
| 843 | + |
|
| 844 | + // Do the actual delete |
|
| 845 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 846 | + $provider->delete($share); |
|
| 847 | + |
|
| 848 | + // All the deleted shares caused by this delete |
|
| 849 | + $deletedShares[] = $share; |
|
| 850 | + |
|
| 851 | + // Emit post hook |
|
| 852 | + $event->setArgument('deletedShares', $deletedShares); |
|
| 853 | + $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * Unshare a file as the recipient. |
|
| 859 | + * This can be different from a regular delete for example when one of |
|
| 860 | + * the users in a groups deletes that share. But the provider should |
|
| 861 | + * handle this. |
|
| 862 | + * |
|
| 863 | + * @param \OCP\Share\IShare $share |
|
| 864 | + * @param string $recipientId |
|
| 865 | + */ |
|
| 866 | + public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
|
| 867 | + list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 868 | + $provider = $this->factory->getProvider($providerId); |
|
| 869 | + |
|
| 870 | + $provider->deleteFromSelf($share, $recipientId); |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * @inheritdoc |
|
| 875 | + */ |
|
| 876 | + public function moveShare(\OCP\Share\IShare $share, $recipientId) { |
|
| 877 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 878 | + throw new \InvalidArgumentException('Can\'t change target of link share'); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { |
|
| 882 | + throw new \InvalidArgumentException('Invalid recipient'); |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 886 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 887 | + if (is_null($sharedWith)) { |
|
| 888 | + throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 889 | + } |
|
| 890 | + $recipient = $this->userManager->get($recipientId); |
|
| 891 | + if (!$sharedWith->inGroup($recipient)) { |
|
| 892 | + throw new \InvalidArgumentException('Invalid recipient'); |
|
| 893 | + } |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 897 | + $provider = $this->factory->getProvider($providerId); |
|
| 898 | + |
|
| 899 | + $provider->move($share, $recipientId); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
|
| 903 | + $providers = $this->factory->getAllProviders(); |
|
| 904 | + |
|
| 905 | + return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
|
| 906 | + $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
|
| 907 | + foreach ($newShares as $fid => $data) { |
|
| 908 | + if (!isset($shares[$fid])) { |
|
| 909 | + $shares[$fid] = []; |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + $shares[$fid] = array_merge($shares[$fid], $data); |
|
| 913 | + } |
|
| 914 | + return $shares; |
|
| 915 | + }, []); |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * @inheritdoc |
|
| 920 | + */ |
|
| 921 | + public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
|
| 922 | + if ($path !== null && |
|
| 923 | + !($path instanceof \OCP\Files\File) && |
|
| 924 | + !($path instanceof \OCP\Files\Folder)) { |
|
| 925 | + throw new \InvalidArgumentException('invalid path'); |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + try { |
|
| 929 | + $provider = $this->factory->getProviderForType($shareType); |
|
| 930 | + } catch (ProviderException $e) { |
|
| 931 | + return []; |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 935 | + |
|
| 936 | + /* |
|
| 937 | 937 | * Work around so we don't return expired shares but still follow |
| 938 | 938 | * proper pagination. |
| 939 | 939 | */ |
| 940 | 940 | |
| 941 | - $shares2 = []; |
|
| 942 | - |
|
| 943 | - while(true) { |
|
| 944 | - $added = 0; |
|
| 945 | - foreach ($shares as $share) { |
|
| 946 | - |
|
| 947 | - try { |
|
| 948 | - $this->checkExpireDate($share); |
|
| 949 | - } catch (ShareNotFound $e) { |
|
| 950 | - //Ignore since this basically means the share is deleted |
|
| 951 | - continue; |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - $added++; |
|
| 955 | - $shares2[] = $share; |
|
| 956 | - |
|
| 957 | - if (count($shares2) === $limit) { |
|
| 958 | - break; |
|
| 959 | - } |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - if (count($shares2) === $limit) { |
|
| 963 | - break; |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - // If there was no limit on the select we are done |
|
| 967 | - if ($limit === -1) { |
|
| 968 | - break; |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - $offset += $added; |
|
| 972 | - |
|
| 973 | - // Fetch again $limit shares |
|
| 974 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 975 | - |
|
| 976 | - // No more shares means we are done |
|
| 977 | - if (empty($shares)) { |
|
| 978 | - break; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - $shares = $shares2; |
|
| 983 | - |
|
| 984 | - return $shares; |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - /** |
|
| 988 | - * @inheritdoc |
|
| 989 | - */ |
|
| 990 | - public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
| 991 | - try { |
|
| 992 | - $provider = $this->factory->getProviderForType($shareType); |
|
| 993 | - } catch (ProviderException $e) { |
|
| 994 | - return []; |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
| 998 | - |
|
| 999 | - // remove all shares which are already expired |
|
| 1000 | - foreach ($shares as $key => $share) { |
|
| 1001 | - try { |
|
| 1002 | - $this->checkExpireDate($share); |
|
| 1003 | - } catch (ShareNotFound $e) { |
|
| 1004 | - unset($shares[$key]); |
|
| 1005 | - } |
|
| 1006 | - } |
|
| 1007 | - |
|
| 1008 | - return $shares; |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - /** |
|
| 1012 | - * @inheritdoc |
|
| 1013 | - */ |
|
| 1014 | - public function getShareById($id, $recipient = null) { |
|
| 1015 | - if ($id === null) { |
|
| 1016 | - throw new ShareNotFound(); |
|
| 1017 | - } |
|
| 1018 | - |
|
| 1019 | - list($providerId, $id) = $this->splitFullId($id); |
|
| 1020 | - |
|
| 1021 | - try { |
|
| 1022 | - $provider = $this->factory->getProvider($providerId); |
|
| 1023 | - } catch (ProviderException $e) { |
|
| 1024 | - throw new ShareNotFound(); |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - $share = $provider->getShareById($id, $recipient); |
|
| 1028 | - |
|
| 1029 | - $this->checkExpireDate($share); |
|
| 1030 | - |
|
| 1031 | - return $share; |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - /** |
|
| 1035 | - * Get all the shares for a given path |
|
| 1036 | - * |
|
| 1037 | - * @param \OCP\Files\Node $path |
|
| 1038 | - * @param int $page |
|
| 1039 | - * @param int $perPage |
|
| 1040 | - * |
|
| 1041 | - * @return Share[] |
|
| 1042 | - */ |
|
| 1043 | - public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
|
| 1044 | - return []; |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - /** |
|
| 1048 | - * Get the share by token possible with password |
|
| 1049 | - * |
|
| 1050 | - * @param string $token |
|
| 1051 | - * @return Share |
|
| 1052 | - * |
|
| 1053 | - * @throws ShareNotFound |
|
| 1054 | - */ |
|
| 1055 | - public function getShareByToken($token) { |
|
| 1056 | - $share = null; |
|
| 1057 | - try { |
|
| 1058 | - if($this->shareApiAllowLinks()) { |
|
| 1059 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
|
| 1060 | - $share = $provider->getShareByToken($token); |
|
| 1061 | - } |
|
| 1062 | - } catch (ProviderException $e) { |
|
| 1063 | - } catch (ShareNotFound $e) { |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - |
|
| 1067 | - // If it is not a link share try to fetch a federated share by token |
|
| 1068 | - if ($share === null) { |
|
| 1069 | - try { |
|
| 1070 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); |
|
| 1071 | - $share = $provider->getShareByToken($token); |
|
| 1072 | - } catch (ProviderException $e) { |
|
| 1073 | - } catch (ShareNotFound $e) { |
|
| 1074 | - } |
|
| 1075 | - } |
|
| 1076 | - |
|
| 1077 | - // If it is not a link share try to fetch a mail share by token |
|
| 1078 | - if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { |
|
| 1079 | - try { |
|
| 1080 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); |
|
| 1081 | - $share = $provider->getShareByToken($token); |
|
| 1082 | - } catch (ProviderException $e) { |
|
| 1083 | - } catch (ShareNotFound $e) { |
|
| 1084 | - } |
|
| 1085 | - } |
|
| 1086 | - |
|
| 1087 | - if ($share === null) { |
|
| 1088 | - throw new ShareNotFound(); |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - $this->checkExpireDate($share); |
|
| 1092 | - |
|
| 1093 | - /* |
|
| 941 | + $shares2 = []; |
|
| 942 | + |
|
| 943 | + while(true) { |
|
| 944 | + $added = 0; |
|
| 945 | + foreach ($shares as $share) { |
|
| 946 | + |
|
| 947 | + try { |
|
| 948 | + $this->checkExpireDate($share); |
|
| 949 | + } catch (ShareNotFound $e) { |
|
| 950 | + //Ignore since this basically means the share is deleted |
|
| 951 | + continue; |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + $added++; |
|
| 955 | + $shares2[] = $share; |
|
| 956 | + |
|
| 957 | + if (count($shares2) === $limit) { |
|
| 958 | + break; |
|
| 959 | + } |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + if (count($shares2) === $limit) { |
|
| 963 | + break; |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + // If there was no limit on the select we are done |
|
| 967 | + if ($limit === -1) { |
|
| 968 | + break; |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + $offset += $added; |
|
| 972 | + |
|
| 973 | + // Fetch again $limit shares |
|
| 974 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
| 975 | + |
|
| 976 | + // No more shares means we are done |
|
| 977 | + if (empty($shares)) { |
|
| 978 | + break; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + $shares = $shares2; |
|
| 983 | + |
|
| 984 | + return $shares; |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + /** |
|
| 988 | + * @inheritdoc |
|
| 989 | + */ |
|
| 990 | + public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
| 991 | + try { |
|
| 992 | + $provider = $this->factory->getProviderForType($shareType); |
|
| 993 | + } catch (ProviderException $e) { |
|
| 994 | + return []; |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
| 998 | + |
|
| 999 | + // remove all shares which are already expired |
|
| 1000 | + foreach ($shares as $key => $share) { |
|
| 1001 | + try { |
|
| 1002 | + $this->checkExpireDate($share); |
|
| 1003 | + } catch (ShareNotFound $e) { |
|
| 1004 | + unset($shares[$key]); |
|
| 1005 | + } |
|
| 1006 | + } |
|
| 1007 | + |
|
| 1008 | + return $shares; |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + /** |
|
| 1012 | + * @inheritdoc |
|
| 1013 | + */ |
|
| 1014 | + public function getShareById($id, $recipient = null) { |
|
| 1015 | + if ($id === null) { |
|
| 1016 | + throw new ShareNotFound(); |
|
| 1017 | + } |
|
| 1018 | + |
|
| 1019 | + list($providerId, $id) = $this->splitFullId($id); |
|
| 1020 | + |
|
| 1021 | + try { |
|
| 1022 | + $provider = $this->factory->getProvider($providerId); |
|
| 1023 | + } catch (ProviderException $e) { |
|
| 1024 | + throw new ShareNotFound(); |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + $share = $provider->getShareById($id, $recipient); |
|
| 1028 | + |
|
| 1029 | + $this->checkExpireDate($share); |
|
| 1030 | + |
|
| 1031 | + return $share; |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + /** |
|
| 1035 | + * Get all the shares for a given path |
|
| 1036 | + * |
|
| 1037 | + * @param \OCP\Files\Node $path |
|
| 1038 | + * @param int $page |
|
| 1039 | + * @param int $perPage |
|
| 1040 | + * |
|
| 1041 | + * @return Share[] |
|
| 1042 | + */ |
|
| 1043 | + public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
|
| 1044 | + return []; |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + /** |
|
| 1048 | + * Get the share by token possible with password |
|
| 1049 | + * |
|
| 1050 | + * @param string $token |
|
| 1051 | + * @return Share |
|
| 1052 | + * |
|
| 1053 | + * @throws ShareNotFound |
|
| 1054 | + */ |
|
| 1055 | + public function getShareByToken($token) { |
|
| 1056 | + $share = null; |
|
| 1057 | + try { |
|
| 1058 | + if($this->shareApiAllowLinks()) { |
|
| 1059 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
|
| 1060 | + $share = $provider->getShareByToken($token); |
|
| 1061 | + } |
|
| 1062 | + } catch (ProviderException $e) { |
|
| 1063 | + } catch (ShareNotFound $e) { |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + |
|
| 1067 | + // If it is not a link share try to fetch a federated share by token |
|
| 1068 | + if ($share === null) { |
|
| 1069 | + try { |
|
| 1070 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); |
|
| 1071 | + $share = $provider->getShareByToken($token); |
|
| 1072 | + } catch (ProviderException $e) { |
|
| 1073 | + } catch (ShareNotFound $e) { |
|
| 1074 | + } |
|
| 1075 | + } |
|
| 1076 | + |
|
| 1077 | + // If it is not a link share try to fetch a mail share by token |
|
| 1078 | + if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { |
|
| 1079 | + try { |
|
| 1080 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); |
|
| 1081 | + $share = $provider->getShareByToken($token); |
|
| 1082 | + } catch (ProviderException $e) { |
|
| 1083 | + } catch (ShareNotFound $e) { |
|
| 1084 | + } |
|
| 1085 | + } |
|
| 1086 | + |
|
| 1087 | + if ($share === null) { |
|
| 1088 | + throw new ShareNotFound(); |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + $this->checkExpireDate($share); |
|
| 1092 | + |
|
| 1093 | + /* |
|
| 1094 | 1094 | * Reduce the permissions for link shares if public upload is not enabled |
| 1095 | 1095 | */ |
| 1096 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && |
|
| 1097 | - !$this->shareApiLinkAllowPublicUpload()) { |
|
| 1098 | - $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
|
| 1099 | - } |
|
| 1100 | - |
|
| 1101 | - return $share; |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - protected function checkExpireDate($share) { |
|
| 1105 | - if ($share->getExpirationDate() !== null && |
|
| 1106 | - $share->getExpirationDate() <= new \DateTime()) { |
|
| 1107 | - $this->deleteShare($share); |
|
| 1108 | - throw new ShareNotFound(); |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - /** |
|
| 1114 | - * Verify the password of a public share |
|
| 1115 | - * |
|
| 1116 | - * @param \OCP\Share\IShare $share |
|
| 1117 | - * @param string $password |
|
| 1118 | - * @return bool |
|
| 1119 | - */ |
|
| 1120 | - public function checkPassword(\OCP\Share\IShare $share, $password) { |
|
| 1121 | - $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK |
|
| 1122 | - || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 1123 | - if (!$passwordProtected) { |
|
| 1124 | - //TODO maybe exception? |
|
| 1125 | - return false; |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - if ($password === null || $share->getPassword() === null) { |
|
| 1129 | - return false; |
|
| 1130 | - } |
|
| 1131 | - |
|
| 1132 | - $newHash = ''; |
|
| 1133 | - if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
|
| 1134 | - return false; |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - if (!empty($newHash)) { |
|
| 1138 | - $share->setPassword($newHash); |
|
| 1139 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 1140 | - $provider->update($share); |
|
| 1141 | - } |
|
| 1142 | - |
|
| 1143 | - return true; |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * @inheritdoc |
|
| 1148 | - */ |
|
| 1149 | - public function userDeleted($uid) { |
|
| 1150 | - $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; |
|
| 1151 | - |
|
| 1152 | - foreach ($types as $type) { |
|
| 1153 | - try { |
|
| 1154 | - $provider = $this->factory->getProviderForType($type); |
|
| 1155 | - } catch (ProviderException $e) { |
|
| 1156 | - continue; |
|
| 1157 | - } |
|
| 1158 | - $provider->userDeleted($uid, $type); |
|
| 1159 | - } |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - /** |
|
| 1163 | - * @inheritdoc |
|
| 1164 | - */ |
|
| 1165 | - public function groupDeleted($gid) { |
|
| 1166 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1167 | - $provider->groupDeleted($gid); |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - /** |
|
| 1171 | - * @inheritdoc |
|
| 1172 | - */ |
|
| 1173 | - public function userDeletedFromGroup($uid, $gid) { |
|
| 1174 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1175 | - $provider->userDeletedFromGroup($uid, $gid); |
|
| 1176 | - } |
|
| 1177 | - |
|
| 1178 | - /** |
|
| 1179 | - * Get access list to a path. This means |
|
| 1180 | - * all the users that can access a given path. |
|
| 1181 | - * |
|
| 1182 | - * Consider: |
|
| 1183 | - * -root |
|
| 1184 | - * |-folder1 (23) |
|
| 1185 | - * |-folder2 (32) |
|
| 1186 | - * |-fileA (42) |
|
| 1187 | - * |
|
| 1188 | - * fileA is shared with user1 and user1@server1 |
|
| 1189 | - * folder2 is shared with group2 (user4 is a member of group2) |
|
| 1190 | - * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
|
| 1191 | - * |
|
| 1192 | - * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
|
| 1193 | - * [ |
|
| 1194 | - * users => [ |
|
| 1195 | - * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
|
| 1196 | - * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
|
| 1197 | - * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
|
| 1198 | - * ], |
|
| 1199 | - * remote => [ |
|
| 1200 | - * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
|
| 1201 | - * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
|
| 1202 | - * ], |
|
| 1203 | - * public => bool |
|
| 1204 | - * mail => bool |
|
| 1205 | - * ] |
|
| 1206 | - * |
|
| 1207 | - * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
|
| 1208 | - * [ |
|
| 1209 | - * users => ['user1', 'user2', 'user4'], |
|
| 1210 | - * remote => bool, |
|
| 1211 | - * public => bool |
|
| 1212 | - * mail => bool |
|
| 1213 | - * ] |
|
| 1214 | - * |
|
| 1215 | - * This is required for encryption/activity |
|
| 1216 | - * |
|
| 1217 | - * @param \OCP\Files\Node $path |
|
| 1218 | - * @param bool $recursive Should we check all parent folders as well |
|
| 1219 | - * @param bool $currentAccess Should the user have currently access to the file |
|
| 1220 | - * @return array |
|
| 1221 | - */ |
|
| 1222 | - public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
|
| 1223 | - $owner = $path->getOwner()->getUID(); |
|
| 1224 | - |
|
| 1225 | - if ($currentAccess) { |
|
| 1226 | - $al = ['users' => [], 'remote' => [], 'public' => false]; |
|
| 1227 | - } else { |
|
| 1228 | - $al = ['users' => [], 'remote' => false, 'public' => false]; |
|
| 1229 | - } |
|
| 1230 | - if (!$this->userManager->userExists($owner)) { |
|
| 1231 | - return $al; |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - //Get node for the owner |
|
| 1235 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1236 | - if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
|
| 1237 | - $path = $userFolder->getById($path->getId())[0]; |
|
| 1238 | - } |
|
| 1239 | - |
|
| 1240 | - $providers = $this->factory->getAllProviders(); |
|
| 1241 | - |
|
| 1242 | - /** @var Node[] $nodes */ |
|
| 1243 | - $nodes = []; |
|
| 1244 | - |
|
| 1245 | - |
|
| 1246 | - if ($currentAccess) { |
|
| 1247 | - $ownerPath = $path->getPath(); |
|
| 1248 | - $ownerPath = explode('/', $ownerPath, 4); |
|
| 1249 | - if (count($ownerPath) < 4) { |
|
| 1250 | - $ownerPath = ''; |
|
| 1251 | - } else { |
|
| 1252 | - $ownerPath = $ownerPath[3]; |
|
| 1253 | - } |
|
| 1254 | - $al['users'][$owner] = [ |
|
| 1255 | - 'node_id' => $path->getId(), |
|
| 1256 | - 'node_path' => '/' . $ownerPath, |
|
| 1257 | - ]; |
|
| 1258 | - } else { |
|
| 1259 | - $al['users'][] = $owner; |
|
| 1260 | - } |
|
| 1261 | - |
|
| 1262 | - // Collect all the shares |
|
| 1263 | - while ($path->getPath() !== $userFolder->getPath()) { |
|
| 1264 | - $nodes[] = $path; |
|
| 1265 | - if (!$recursive) { |
|
| 1266 | - break; |
|
| 1267 | - } |
|
| 1268 | - $path = $path->getParent(); |
|
| 1269 | - } |
|
| 1270 | - |
|
| 1271 | - foreach ($providers as $provider) { |
|
| 1272 | - $tmp = $provider->getAccessList($nodes, $currentAccess); |
|
| 1273 | - |
|
| 1274 | - foreach ($tmp as $k => $v) { |
|
| 1275 | - if (isset($al[$k])) { |
|
| 1276 | - if (is_array($al[$k])) { |
|
| 1277 | - $al[$k] = array_merge($al[$k], $v); |
|
| 1278 | - } else { |
|
| 1279 | - $al[$k] = $al[$k] || $v; |
|
| 1280 | - } |
|
| 1281 | - } else { |
|
| 1282 | - $al[$k] = $v; |
|
| 1283 | - } |
|
| 1284 | - } |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - return $al; |
|
| 1288 | - } |
|
| 1289 | - |
|
| 1290 | - /** |
|
| 1291 | - * Create a new share |
|
| 1292 | - * @return \OCP\Share\IShare; |
|
| 1293 | - */ |
|
| 1294 | - public function newShare() { |
|
| 1295 | - return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
|
| 1296 | - } |
|
| 1297 | - |
|
| 1298 | - /** |
|
| 1299 | - * Is the share API enabled |
|
| 1300 | - * |
|
| 1301 | - * @return bool |
|
| 1302 | - */ |
|
| 1303 | - public function shareApiEnabled() { |
|
| 1304 | - return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * Is public link sharing enabled |
|
| 1309 | - * |
|
| 1310 | - * @return bool |
|
| 1311 | - */ |
|
| 1312 | - public function shareApiAllowLinks() { |
|
| 1313 | - return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; |
|
| 1314 | - } |
|
| 1315 | - |
|
| 1316 | - /** |
|
| 1317 | - * Is password on public link requires |
|
| 1318 | - * |
|
| 1319 | - * @return bool |
|
| 1320 | - */ |
|
| 1321 | - public function shareApiLinkEnforcePassword() { |
|
| 1322 | - return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
|
| 1323 | - } |
|
| 1324 | - |
|
| 1325 | - /** |
|
| 1326 | - * Is default expire date enabled |
|
| 1327 | - * |
|
| 1328 | - * @return bool |
|
| 1329 | - */ |
|
| 1330 | - public function shareApiLinkDefaultExpireDate() { |
|
| 1331 | - return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
|
| 1332 | - } |
|
| 1333 | - |
|
| 1334 | - /** |
|
| 1335 | - * Is default expire date enforced |
|
| 1336 | - *` |
|
| 1337 | - * @return bool |
|
| 1338 | - */ |
|
| 1339 | - public function shareApiLinkDefaultExpireDateEnforced() { |
|
| 1340 | - return $this->shareApiLinkDefaultExpireDate() && |
|
| 1341 | - $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - /** |
|
| 1345 | - * Number of default expire days |
|
| 1346 | - *shareApiLinkAllowPublicUpload |
|
| 1347 | - * @return int |
|
| 1348 | - */ |
|
| 1349 | - public function shareApiLinkDefaultExpireDays() { |
|
| 1350 | - return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1351 | - } |
|
| 1352 | - |
|
| 1353 | - /** |
|
| 1354 | - * Allow public upload on link shares |
|
| 1355 | - * |
|
| 1356 | - * @return bool |
|
| 1357 | - */ |
|
| 1358 | - public function shareApiLinkAllowPublicUpload() { |
|
| 1359 | - return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
|
| 1360 | - } |
|
| 1361 | - |
|
| 1362 | - /** |
|
| 1363 | - * check if user can only share with group members |
|
| 1364 | - * @return bool |
|
| 1365 | - */ |
|
| 1366 | - public function shareWithGroupMembersOnly() { |
|
| 1367 | - return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 1368 | - } |
|
| 1369 | - |
|
| 1370 | - /** |
|
| 1371 | - * Check if users can share with groups |
|
| 1372 | - * @return bool |
|
| 1373 | - */ |
|
| 1374 | - public function allowGroupSharing() { |
|
| 1375 | - return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
|
| 1376 | - } |
|
| 1377 | - |
|
| 1378 | - /** |
|
| 1379 | - * Copied from \OC_Util::isSharingDisabledForUser |
|
| 1380 | - * |
|
| 1381 | - * TODO: Deprecate fuction from OC_Util |
|
| 1382 | - * |
|
| 1383 | - * @param string $userId |
|
| 1384 | - * @return bool |
|
| 1385 | - */ |
|
| 1386 | - public function sharingDisabledForUser($userId) { |
|
| 1387 | - if ($userId === null) { |
|
| 1388 | - return false; |
|
| 1389 | - } |
|
| 1390 | - |
|
| 1391 | - if (isset($this->sharingDisabledForUsersCache[$userId])) { |
|
| 1392 | - return $this->sharingDisabledForUsersCache[$userId]; |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
| 1396 | - $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 1397 | - $excludedGroups = json_decode($groupsList); |
|
| 1398 | - if (is_null($excludedGroups)) { |
|
| 1399 | - $excludedGroups = explode(',', $groupsList); |
|
| 1400 | - $newValue = json_encode($excludedGroups); |
|
| 1401 | - $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
| 1402 | - } |
|
| 1403 | - $user = $this->userManager->get($userId); |
|
| 1404 | - $usersGroups = $this->groupManager->getUserGroupIds($user); |
|
| 1405 | - if (!empty($usersGroups)) { |
|
| 1406 | - $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
| 1407 | - // if the user is only in groups which are disabled for sharing then |
|
| 1408 | - // sharing is also disabled for the user |
|
| 1409 | - if (empty($remainingGroups)) { |
|
| 1410 | - $this->sharingDisabledForUsersCache[$userId] = true; |
|
| 1411 | - return true; |
|
| 1412 | - } |
|
| 1413 | - } |
|
| 1414 | - } |
|
| 1415 | - |
|
| 1416 | - $this->sharingDisabledForUsersCache[$userId] = false; |
|
| 1417 | - return false; |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - /** |
|
| 1421 | - * @inheritdoc |
|
| 1422 | - */ |
|
| 1423 | - public function outgoingServer2ServerSharesAllowed() { |
|
| 1424 | - return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; |
|
| 1425 | - } |
|
| 1426 | - |
|
| 1427 | - /** |
|
| 1428 | - * @inheritdoc |
|
| 1429 | - */ |
|
| 1430 | - public function shareProviderExists($shareType) { |
|
| 1431 | - try { |
|
| 1432 | - $this->factory->getProviderForType($shareType); |
|
| 1433 | - } catch (ProviderException $e) { |
|
| 1434 | - return false; |
|
| 1435 | - } |
|
| 1436 | - |
|
| 1437 | - return true; |
|
| 1438 | - } |
|
| 1096 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && |
|
| 1097 | + !$this->shareApiLinkAllowPublicUpload()) { |
|
| 1098 | + $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
|
| 1099 | + } |
|
| 1100 | + |
|
| 1101 | + return $share; |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + protected function checkExpireDate($share) { |
|
| 1105 | + if ($share->getExpirationDate() !== null && |
|
| 1106 | + $share->getExpirationDate() <= new \DateTime()) { |
|
| 1107 | + $this->deleteShare($share); |
|
| 1108 | + throw new ShareNotFound(); |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + /** |
|
| 1114 | + * Verify the password of a public share |
|
| 1115 | + * |
|
| 1116 | + * @param \OCP\Share\IShare $share |
|
| 1117 | + * @param string $password |
|
| 1118 | + * @return bool |
|
| 1119 | + */ |
|
| 1120 | + public function checkPassword(\OCP\Share\IShare $share, $password) { |
|
| 1121 | + $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK |
|
| 1122 | + || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; |
|
| 1123 | + if (!$passwordProtected) { |
|
| 1124 | + //TODO maybe exception? |
|
| 1125 | + return false; |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + if ($password === null || $share->getPassword() === null) { |
|
| 1129 | + return false; |
|
| 1130 | + } |
|
| 1131 | + |
|
| 1132 | + $newHash = ''; |
|
| 1133 | + if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
|
| 1134 | + return false; |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + if (!empty($newHash)) { |
|
| 1138 | + $share->setPassword($newHash); |
|
| 1139 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 1140 | + $provider->update($share); |
|
| 1141 | + } |
|
| 1142 | + |
|
| 1143 | + return true; |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * @inheritdoc |
|
| 1148 | + */ |
|
| 1149 | + public function userDeleted($uid) { |
|
| 1150 | + $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; |
|
| 1151 | + |
|
| 1152 | + foreach ($types as $type) { |
|
| 1153 | + try { |
|
| 1154 | + $provider = $this->factory->getProviderForType($type); |
|
| 1155 | + } catch (ProviderException $e) { |
|
| 1156 | + continue; |
|
| 1157 | + } |
|
| 1158 | + $provider->userDeleted($uid, $type); |
|
| 1159 | + } |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + /** |
|
| 1163 | + * @inheritdoc |
|
| 1164 | + */ |
|
| 1165 | + public function groupDeleted($gid) { |
|
| 1166 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1167 | + $provider->groupDeleted($gid); |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + /** |
|
| 1171 | + * @inheritdoc |
|
| 1172 | + */ |
|
| 1173 | + public function userDeletedFromGroup($uid, $gid) { |
|
| 1174 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
|
| 1175 | + $provider->userDeletedFromGroup($uid, $gid); |
|
| 1176 | + } |
|
| 1177 | + |
|
| 1178 | + /** |
|
| 1179 | + * Get access list to a path. This means |
|
| 1180 | + * all the users that can access a given path. |
|
| 1181 | + * |
|
| 1182 | + * Consider: |
|
| 1183 | + * -root |
|
| 1184 | + * |-folder1 (23) |
|
| 1185 | + * |-folder2 (32) |
|
| 1186 | + * |-fileA (42) |
|
| 1187 | + * |
|
| 1188 | + * fileA is shared with user1 and user1@server1 |
|
| 1189 | + * folder2 is shared with group2 (user4 is a member of group2) |
|
| 1190 | + * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
|
| 1191 | + * |
|
| 1192 | + * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
|
| 1193 | + * [ |
|
| 1194 | + * users => [ |
|
| 1195 | + * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
|
| 1196 | + * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
|
| 1197 | + * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
|
| 1198 | + * ], |
|
| 1199 | + * remote => [ |
|
| 1200 | + * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
|
| 1201 | + * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
|
| 1202 | + * ], |
|
| 1203 | + * public => bool |
|
| 1204 | + * mail => bool |
|
| 1205 | + * ] |
|
| 1206 | + * |
|
| 1207 | + * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
|
| 1208 | + * [ |
|
| 1209 | + * users => ['user1', 'user2', 'user4'], |
|
| 1210 | + * remote => bool, |
|
| 1211 | + * public => bool |
|
| 1212 | + * mail => bool |
|
| 1213 | + * ] |
|
| 1214 | + * |
|
| 1215 | + * This is required for encryption/activity |
|
| 1216 | + * |
|
| 1217 | + * @param \OCP\Files\Node $path |
|
| 1218 | + * @param bool $recursive Should we check all parent folders as well |
|
| 1219 | + * @param bool $currentAccess Should the user have currently access to the file |
|
| 1220 | + * @return array |
|
| 1221 | + */ |
|
| 1222 | + public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
|
| 1223 | + $owner = $path->getOwner()->getUID(); |
|
| 1224 | + |
|
| 1225 | + if ($currentAccess) { |
|
| 1226 | + $al = ['users' => [], 'remote' => [], 'public' => false]; |
|
| 1227 | + } else { |
|
| 1228 | + $al = ['users' => [], 'remote' => false, 'public' => false]; |
|
| 1229 | + } |
|
| 1230 | + if (!$this->userManager->userExists($owner)) { |
|
| 1231 | + return $al; |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + //Get node for the owner |
|
| 1235 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1236 | + if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
|
| 1237 | + $path = $userFolder->getById($path->getId())[0]; |
|
| 1238 | + } |
|
| 1239 | + |
|
| 1240 | + $providers = $this->factory->getAllProviders(); |
|
| 1241 | + |
|
| 1242 | + /** @var Node[] $nodes */ |
|
| 1243 | + $nodes = []; |
|
| 1244 | + |
|
| 1245 | + |
|
| 1246 | + if ($currentAccess) { |
|
| 1247 | + $ownerPath = $path->getPath(); |
|
| 1248 | + $ownerPath = explode('/', $ownerPath, 4); |
|
| 1249 | + if (count($ownerPath) < 4) { |
|
| 1250 | + $ownerPath = ''; |
|
| 1251 | + } else { |
|
| 1252 | + $ownerPath = $ownerPath[3]; |
|
| 1253 | + } |
|
| 1254 | + $al['users'][$owner] = [ |
|
| 1255 | + 'node_id' => $path->getId(), |
|
| 1256 | + 'node_path' => '/' . $ownerPath, |
|
| 1257 | + ]; |
|
| 1258 | + } else { |
|
| 1259 | + $al['users'][] = $owner; |
|
| 1260 | + } |
|
| 1261 | + |
|
| 1262 | + // Collect all the shares |
|
| 1263 | + while ($path->getPath() !== $userFolder->getPath()) { |
|
| 1264 | + $nodes[] = $path; |
|
| 1265 | + if (!$recursive) { |
|
| 1266 | + break; |
|
| 1267 | + } |
|
| 1268 | + $path = $path->getParent(); |
|
| 1269 | + } |
|
| 1270 | + |
|
| 1271 | + foreach ($providers as $provider) { |
|
| 1272 | + $tmp = $provider->getAccessList($nodes, $currentAccess); |
|
| 1273 | + |
|
| 1274 | + foreach ($tmp as $k => $v) { |
|
| 1275 | + if (isset($al[$k])) { |
|
| 1276 | + if (is_array($al[$k])) { |
|
| 1277 | + $al[$k] = array_merge($al[$k], $v); |
|
| 1278 | + } else { |
|
| 1279 | + $al[$k] = $al[$k] || $v; |
|
| 1280 | + } |
|
| 1281 | + } else { |
|
| 1282 | + $al[$k] = $v; |
|
| 1283 | + } |
|
| 1284 | + } |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + return $al; |
|
| 1288 | + } |
|
| 1289 | + |
|
| 1290 | + /** |
|
| 1291 | + * Create a new share |
|
| 1292 | + * @return \OCP\Share\IShare; |
|
| 1293 | + */ |
|
| 1294 | + public function newShare() { |
|
| 1295 | + return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
|
| 1296 | + } |
|
| 1297 | + |
|
| 1298 | + /** |
|
| 1299 | + * Is the share API enabled |
|
| 1300 | + * |
|
| 1301 | + * @return bool |
|
| 1302 | + */ |
|
| 1303 | + public function shareApiEnabled() { |
|
| 1304 | + return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; |
|
| 1305 | + } |
|
| 1306 | + |
|
| 1307 | + /** |
|
| 1308 | + * Is public link sharing enabled |
|
| 1309 | + * |
|
| 1310 | + * @return bool |
|
| 1311 | + */ |
|
| 1312 | + public function shareApiAllowLinks() { |
|
| 1313 | + return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; |
|
| 1314 | + } |
|
| 1315 | + |
|
| 1316 | + /** |
|
| 1317 | + * Is password on public link requires |
|
| 1318 | + * |
|
| 1319 | + * @return bool |
|
| 1320 | + */ |
|
| 1321 | + public function shareApiLinkEnforcePassword() { |
|
| 1322 | + return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
|
| 1323 | + } |
|
| 1324 | + |
|
| 1325 | + /** |
|
| 1326 | + * Is default expire date enabled |
|
| 1327 | + * |
|
| 1328 | + * @return bool |
|
| 1329 | + */ |
|
| 1330 | + public function shareApiLinkDefaultExpireDate() { |
|
| 1331 | + return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
|
| 1332 | + } |
|
| 1333 | + |
|
| 1334 | + /** |
|
| 1335 | + * Is default expire date enforced |
|
| 1336 | + *` |
|
| 1337 | + * @return bool |
|
| 1338 | + */ |
|
| 1339 | + public function shareApiLinkDefaultExpireDateEnforced() { |
|
| 1340 | + return $this->shareApiLinkDefaultExpireDate() && |
|
| 1341 | + $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + /** |
|
| 1345 | + * Number of default expire days |
|
| 1346 | + *shareApiLinkAllowPublicUpload |
|
| 1347 | + * @return int |
|
| 1348 | + */ |
|
| 1349 | + public function shareApiLinkDefaultExpireDays() { |
|
| 1350 | + return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1351 | + } |
|
| 1352 | + |
|
| 1353 | + /** |
|
| 1354 | + * Allow public upload on link shares |
|
| 1355 | + * |
|
| 1356 | + * @return bool |
|
| 1357 | + */ |
|
| 1358 | + public function shareApiLinkAllowPublicUpload() { |
|
| 1359 | + return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
|
| 1360 | + } |
|
| 1361 | + |
|
| 1362 | + /** |
|
| 1363 | + * check if user can only share with group members |
|
| 1364 | + * @return bool |
|
| 1365 | + */ |
|
| 1366 | + public function shareWithGroupMembersOnly() { |
|
| 1367 | + return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 1368 | + } |
|
| 1369 | + |
|
| 1370 | + /** |
|
| 1371 | + * Check if users can share with groups |
|
| 1372 | + * @return bool |
|
| 1373 | + */ |
|
| 1374 | + public function allowGroupSharing() { |
|
| 1375 | + return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
|
| 1376 | + } |
|
| 1377 | + |
|
| 1378 | + /** |
|
| 1379 | + * Copied from \OC_Util::isSharingDisabledForUser |
|
| 1380 | + * |
|
| 1381 | + * TODO: Deprecate fuction from OC_Util |
|
| 1382 | + * |
|
| 1383 | + * @param string $userId |
|
| 1384 | + * @return bool |
|
| 1385 | + */ |
|
| 1386 | + public function sharingDisabledForUser($userId) { |
|
| 1387 | + if ($userId === null) { |
|
| 1388 | + return false; |
|
| 1389 | + } |
|
| 1390 | + |
|
| 1391 | + if (isset($this->sharingDisabledForUsersCache[$userId])) { |
|
| 1392 | + return $this->sharingDisabledForUsersCache[$userId]; |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
| 1396 | + $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 1397 | + $excludedGroups = json_decode($groupsList); |
|
| 1398 | + if (is_null($excludedGroups)) { |
|
| 1399 | + $excludedGroups = explode(',', $groupsList); |
|
| 1400 | + $newValue = json_encode($excludedGroups); |
|
| 1401 | + $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
| 1402 | + } |
|
| 1403 | + $user = $this->userManager->get($userId); |
|
| 1404 | + $usersGroups = $this->groupManager->getUserGroupIds($user); |
|
| 1405 | + if (!empty($usersGroups)) { |
|
| 1406 | + $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
| 1407 | + // if the user is only in groups which are disabled for sharing then |
|
| 1408 | + // sharing is also disabled for the user |
|
| 1409 | + if (empty($remainingGroups)) { |
|
| 1410 | + $this->sharingDisabledForUsersCache[$userId] = true; |
|
| 1411 | + return true; |
|
| 1412 | + } |
|
| 1413 | + } |
|
| 1414 | + } |
|
| 1415 | + |
|
| 1416 | + $this->sharingDisabledForUsersCache[$userId] = false; |
|
| 1417 | + return false; |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + /** |
|
| 1421 | + * @inheritdoc |
|
| 1422 | + */ |
|
| 1423 | + public function outgoingServer2ServerSharesAllowed() { |
|
| 1424 | + return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; |
|
| 1425 | + } |
|
| 1426 | + |
|
| 1427 | + /** |
|
| 1428 | + * @inheritdoc |
|
| 1429 | + */ |
|
| 1430 | + public function shareProviderExists($shareType) { |
|
| 1431 | + try { |
|
| 1432 | + $this->factory->getProviderForType($shareType); |
|
| 1433 | + } catch (ProviderException $e) { |
|
| 1434 | + return false; |
|
| 1435 | + } |
|
| 1436 | + |
|
| 1437 | + return true; |
|
| 1438 | + } |
|
| 1439 | 1439 | |
| 1440 | 1440 | } |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | |
| 322 | 322 | if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
| 323 | 323 | $expirationDate = new \DateTime(); |
| 324 | - $expirationDate->setTime(0,0,0); |
|
| 324 | + $expirationDate->setTime(0, 0, 0); |
|
| 325 | 325 | $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
| 326 | 326 | } |
| 327 | 327 | |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | |
| 334 | 334 | $date = new \DateTime(); |
| 335 | 335 | $date->setTime(0, 0, 0); |
| 336 | - $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
| 336 | + $date->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
|
| 337 | 337 | if ($date < $expirationDate) { |
| 338 | 338 | $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
| 339 | 339 | throw new GenericShareException($message, $message, 404); |
@@ -386,7 +386,7 @@ discard block |
||
| 386 | 386 | */ |
| 387 | 387 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); |
| 388 | 388 | $existingShares = $provider->getSharesByPath($share->getNode()); |
| 389 | - foreach($existingShares as $existingShare) { |
|
| 389 | + foreach ($existingShares as $existingShare) { |
|
| 390 | 390 | // Ignore if it is the same share |
| 391 | 391 | try { |
| 392 | 392 | if ($existingShare->getFullId() === $share->getFullId()) { |
@@ -443,7 +443,7 @@ discard block |
||
| 443 | 443 | */ |
| 444 | 444 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
| 445 | 445 | $existingShares = $provider->getSharesByPath($share->getNode()); |
| 446 | - foreach($existingShares as $existingShare) { |
|
| 446 | + foreach ($existingShares as $existingShare) { |
|
| 447 | 447 | try { |
| 448 | 448 | if ($existingShare->getFullId() === $share->getFullId()) { |
| 449 | 449 | continue; |
@@ -512,7 +512,7 @@ discard block |
||
| 512 | 512 | // Make sure that we do not share a path that contains a shared mountpoint |
| 513 | 513 | if ($path instanceof \OCP\Files\Folder) { |
| 514 | 514 | $mounts = $this->mountManager->findIn($path->getPath()); |
| 515 | - foreach($mounts as $mount) { |
|
| 515 | + foreach ($mounts as $mount) { |
|
| 516 | 516 | if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
| 517 | 517 | throw new \InvalidArgumentException('Path contains files shared with you'); |
| 518 | 518 | } |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | $storage = $share->getNode()->getStorage(); |
| 561 | 561 | if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
| 562 | 562 | $parent = $share->getNode()->getParent(); |
| 563 | - while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 563 | + while ($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
| 564 | 564 | $parent = $parent->getParent(); |
| 565 | 565 | } |
| 566 | 566 | $share->setShareOwner($parent->getOwner()->getUID()); |
@@ -617,7 +617,7 @@ discard block |
||
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | // Generate the target |
| 620 | - $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); |
|
| 620 | + $target = $this->config->getSystemValue('share_folder', '/').'/'.$share->getNode()->getName(); |
|
| 621 | 621 | $target = \OC\Files\Filesystem::normalizePath($target); |
| 622 | 622 | $share->setTarget($target); |
| 623 | 623 | |
@@ -864,7 +864,7 @@ discard block |
||
| 864 | 864 | * @param string $recipientId |
| 865 | 865 | */ |
| 866 | 866 | public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
| 867 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 867 | + list($providerId,) = $this->splitFullId($share->getFullId()); |
|
| 868 | 868 | $provider = $this->factory->getProvider($providerId); |
| 869 | 869 | |
| 870 | 870 | $provider->deleteFromSelf($share, $recipientId); |
@@ -885,7 +885,7 @@ discard block |
||
| 885 | 885 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
| 886 | 886 | $sharedWith = $this->groupManager->get($share->getSharedWith()); |
| 887 | 887 | if (is_null($sharedWith)) { |
| 888 | - throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 888 | + throw new \InvalidArgumentException('Group "'.$share->getSharedWith().'" does not exist'); |
|
| 889 | 889 | } |
| 890 | 890 | $recipient = $this->userManager->get($recipientId); |
| 891 | 891 | if (!$sharedWith->inGroup($recipient)) { |
@@ -893,7 +893,7 @@ discard block |
||
| 893 | 893 | } |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | - list($providerId, ) = $this->splitFullId($share->getFullId()); |
|
| 896 | + list($providerId,) = $this->splitFullId($share->getFullId()); |
|
| 897 | 897 | $provider = $this->factory->getProvider($providerId); |
| 898 | 898 | |
| 899 | 899 | $provider->move($share, $recipientId); |
@@ -940,7 +940,7 @@ discard block |
||
| 940 | 940 | |
| 941 | 941 | $shares2 = []; |
| 942 | 942 | |
| 943 | - while(true) { |
|
| 943 | + while (true) { |
|
| 944 | 944 | $added = 0; |
| 945 | 945 | foreach ($shares as $share) { |
| 946 | 946 | |
@@ -1040,7 +1040,7 @@ discard block |
||
| 1040 | 1040 | * |
| 1041 | 1041 | * @return Share[] |
| 1042 | 1042 | */ |
| 1043 | - public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
|
| 1043 | + public function getSharesByPath(\OCP\Files\Node $path, $page = 0, $perPage = 50) { |
|
| 1044 | 1044 | return []; |
| 1045 | 1045 | } |
| 1046 | 1046 | |
@@ -1055,7 +1055,7 @@ discard block |
||
| 1055 | 1055 | public function getShareByToken($token) { |
| 1056 | 1056 | $share = null; |
| 1057 | 1057 | try { |
| 1058 | - if($this->shareApiAllowLinks()) { |
|
| 1058 | + if ($this->shareApiAllowLinks()) { |
|
| 1059 | 1059 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
| 1060 | 1060 | $share = $provider->getShareByToken($token); |
| 1061 | 1061 | } |
@@ -1253,7 +1253,7 @@ discard block |
||
| 1253 | 1253 | } |
| 1254 | 1254 | $al['users'][$owner] = [ |
| 1255 | 1255 | 'node_id' => $path->getId(), |
| 1256 | - 'node_path' => '/' . $ownerPath, |
|
| 1256 | + 'node_path' => '/'.$ownerPath, |
|
| 1257 | 1257 | ]; |
| 1258 | 1258 | } else { |
| 1259 | 1259 | $al['users'][] = $owner; |
@@ -1347,7 +1347,7 @@ discard block |
||
| 1347 | 1347 | * @return int |
| 1348 | 1348 | */ |
| 1349 | 1349 | public function shareApiLinkDefaultExpireDays() { |
| 1350 | - return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1350 | + return (int) $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1351 | 1351 | } |
| 1352 | 1352 | |
| 1353 | 1353 | /** |
@@ -39,136 +39,136 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class MailSettingsController extends Controller { |
| 41 | 41 | |
| 42 | - /** @var IL10N */ |
|
| 43 | - private $l10n; |
|
| 44 | - /** @var IConfig */ |
|
| 45 | - private $config; |
|
| 46 | - /** @var IUserSession */ |
|
| 47 | - private $userSession; |
|
| 48 | - /** @var IMailer */ |
|
| 49 | - private $mailer; |
|
| 50 | - /** @var string */ |
|
| 51 | - private $defaultMailAddress; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param string $appName |
|
| 55 | - * @param IRequest $request |
|
| 56 | - * @param IL10N $l10n |
|
| 57 | - * @param IConfig $config |
|
| 58 | - * @param IUserSession $userSession |
|
| 59 | - * @param IMailer $mailer |
|
| 60 | - * @param string $fromMailAddress |
|
| 61 | - */ |
|
| 62 | - public function __construct($appName, |
|
| 63 | - IRequest $request, |
|
| 64 | - IL10N $l10n, |
|
| 65 | - IConfig $config, |
|
| 66 | - IUserSession $userSession, |
|
| 67 | - IMailer $mailer, |
|
| 68 | - $fromMailAddress) { |
|
| 69 | - parent::__construct($appName, $request); |
|
| 70 | - $this->l10n = $l10n; |
|
| 71 | - $this->config = $config; |
|
| 72 | - $this->userSession = $userSession; |
|
| 73 | - $this->mailer = $mailer; |
|
| 74 | - $this->defaultMailAddress = $fromMailAddress; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Sets the email settings |
|
| 79 | - * |
|
| 80 | - * @PasswordConfirmationRequired |
|
| 81 | - * |
|
| 82 | - * @param string $mail_domain |
|
| 83 | - * @param string $mail_from_address |
|
| 84 | - * @param string $mail_smtpmode |
|
| 85 | - * @param string $mail_smtpsecure |
|
| 86 | - * @param string $mail_smtphost |
|
| 87 | - * @param string $mail_smtpauthtype |
|
| 88 | - * @param int $mail_smtpauth |
|
| 89 | - * @param string $mail_smtpport |
|
| 90 | - * @return DataResponse |
|
| 91 | - */ |
|
| 92 | - public function setMailSettings($mail_domain, |
|
| 93 | - $mail_from_address, |
|
| 94 | - $mail_smtpmode, |
|
| 95 | - $mail_smtpsecure, |
|
| 96 | - $mail_smtphost, |
|
| 97 | - $mail_smtpauthtype, |
|
| 98 | - $mail_smtpauth, |
|
| 99 | - $mail_smtpport) { |
|
| 100 | - |
|
| 101 | - $params = get_defined_vars(); |
|
| 102 | - $configs = []; |
|
| 103 | - foreach($params as $key => $value) { |
|
| 104 | - $configs[$key] = (empty($value)) ? null : $value; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Delete passwords from config in case no auth is specified |
|
| 108 | - if ($params['mail_smtpauth'] !== 1) { |
|
| 109 | - $configs['mail_smtpname'] = null; |
|
| 110 | - $configs['mail_smtppassword'] = null; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->config->setSystemValues($configs); |
|
| 114 | - |
|
| 115 | - return new DataResponse(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Store the credentials used for SMTP in the config |
|
| 120 | - * |
|
| 121 | - * @PasswordConfirmationRequired |
|
| 122 | - * |
|
| 123 | - * @param string $mail_smtpname |
|
| 124 | - * @param string $mail_smtppassword |
|
| 125 | - * @return DataResponse |
|
| 126 | - */ |
|
| 127 | - public function storeCredentials($mail_smtpname, $mail_smtppassword) { |
|
| 128 | - if ($mail_smtppassword === '********') { |
|
| 129 | - return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $this->config->setSystemValues([ |
|
| 133 | - 'mail_smtpname' => $mail_smtpname, |
|
| 134 | - 'mail_smtppassword' => $mail_smtppassword, |
|
| 135 | - ]); |
|
| 136 | - |
|
| 137 | - return new DataResponse(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Send a mail to test the settings |
|
| 142 | - * @return DataResponse |
|
| 143 | - */ |
|
| 144 | - public function sendTestMail() { |
|
| 145 | - $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', ''); |
|
| 146 | - if (!empty($email)) { |
|
| 147 | - try { |
|
| 148 | - $displayName = $this->userSession->getUser()->getDisplayName(); |
|
| 149 | - |
|
| 150 | - $template = $this->mailer->createEMailTemplate(); |
|
| 151 | - $template->addHeader(); |
|
| 152 | - $template->addHeading($this->l10n->t('Well done, %s!', [$displayName])); |
|
| 153 | - $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.')); |
|
| 154 | - $template->addFooter(); |
|
| 155 | - |
|
| 156 | - $message = $this->mailer->createMessage(); |
|
| 157 | - $message->setTo([$email => $displayName]); |
|
| 158 | - $message->setSubject($this->l10n->t('Email setting test')); |
|
| 159 | - $message->setHtmlBody($template->renderHtml()); |
|
| 160 | - $message->setPlainBody($template->renderText()); |
|
| 161 | - $errors = $this->mailer->send($message); |
|
| 162 | - if (!empty($errors)) { |
|
| 163 | - throw new \RuntimeException($this->l10n->t('Mail could not be sent. Check your mail server log')); |
|
| 164 | - } |
|
| 165 | - return new DataResponse(); |
|
| 166 | - } catch (\Exception $e) { |
|
| 167 | - return new DataResponse($this->l10n->t('A problem occurred while sending the email. Please revise your settings. (Error: %s)', [$e->getMessage()]), Http::STATUS_BAD_REQUEST); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST); |
|
| 172 | - } |
|
| 42 | + /** @var IL10N */ |
|
| 43 | + private $l10n; |
|
| 44 | + /** @var IConfig */ |
|
| 45 | + private $config; |
|
| 46 | + /** @var IUserSession */ |
|
| 47 | + private $userSession; |
|
| 48 | + /** @var IMailer */ |
|
| 49 | + private $mailer; |
|
| 50 | + /** @var string */ |
|
| 51 | + private $defaultMailAddress; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $appName |
|
| 55 | + * @param IRequest $request |
|
| 56 | + * @param IL10N $l10n |
|
| 57 | + * @param IConfig $config |
|
| 58 | + * @param IUserSession $userSession |
|
| 59 | + * @param IMailer $mailer |
|
| 60 | + * @param string $fromMailAddress |
|
| 61 | + */ |
|
| 62 | + public function __construct($appName, |
|
| 63 | + IRequest $request, |
|
| 64 | + IL10N $l10n, |
|
| 65 | + IConfig $config, |
|
| 66 | + IUserSession $userSession, |
|
| 67 | + IMailer $mailer, |
|
| 68 | + $fromMailAddress) { |
|
| 69 | + parent::__construct($appName, $request); |
|
| 70 | + $this->l10n = $l10n; |
|
| 71 | + $this->config = $config; |
|
| 72 | + $this->userSession = $userSession; |
|
| 73 | + $this->mailer = $mailer; |
|
| 74 | + $this->defaultMailAddress = $fromMailAddress; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Sets the email settings |
|
| 79 | + * |
|
| 80 | + * @PasswordConfirmationRequired |
|
| 81 | + * |
|
| 82 | + * @param string $mail_domain |
|
| 83 | + * @param string $mail_from_address |
|
| 84 | + * @param string $mail_smtpmode |
|
| 85 | + * @param string $mail_smtpsecure |
|
| 86 | + * @param string $mail_smtphost |
|
| 87 | + * @param string $mail_smtpauthtype |
|
| 88 | + * @param int $mail_smtpauth |
|
| 89 | + * @param string $mail_smtpport |
|
| 90 | + * @return DataResponse |
|
| 91 | + */ |
|
| 92 | + public function setMailSettings($mail_domain, |
|
| 93 | + $mail_from_address, |
|
| 94 | + $mail_smtpmode, |
|
| 95 | + $mail_smtpsecure, |
|
| 96 | + $mail_smtphost, |
|
| 97 | + $mail_smtpauthtype, |
|
| 98 | + $mail_smtpauth, |
|
| 99 | + $mail_smtpport) { |
|
| 100 | + |
|
| 101 | + $params = get_defined_vars(); |
|
| 102 | + $configs = []; |
|
| 103 | + foreach($params as $key => $value) { |
|
| 104 | + $configs[$key] = (empty($value)) ? null : $value; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Delete passwords from config in case no auth is specified |
|
| 108 | + if ($params['mail_smtpauth'] !== 1) { |
|
| 109 | + $configs['mail_smtpname'] = null; |
|
| 110 | + $configs['mail_smtppassword'] = null; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->config->setSystemValues($configs); |
|
| 114 | + |
|
| 115 | + return new DataResponse(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Store the credentials used for SMTP in the config |
|
| 120 | + * |
|
| 121 | + * @PasswordConfirmationRequired |
|
| 122 | + * |
|
| 123 | + * @param string $mail_smtpname |
|
| 124 | + * @param string $mail_smtppassword |
|
| 125 | + * @return DataResponse |
|
| 126 | + */ |
|
| 127 | + public function storeCredentials($mail_smtpname, $mail_smtppassword) { |
|
| 128 | + if ($mail_smtppassword === '********') { |
|
| 129 | + return new DataResponse($this->l10n->t('Invalid SMTP password.'), Http::STATUS_BAD_REQUEST); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $this->config->setSystemValues([ |
|
| 133 | + 'mail_smtpname' => $mail_smtpname, |
|
| 134 | + 'mail_smtppassword' => $mail_smtppassword, |
|
| 135 | + ]); |
|
| 136 | + |
|
| 137 | + return new DataResponse(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Send a mail to test the settings |
|
| 142 | + * @return DataResponse |
|
| 143 | + */ |
|
| 144 | + public function sendTestMail() { |
|
| 145 | + $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', ''); |
|
| 146 | + if (!empty($email)) { |
|
| 147 | + try { |
|
| 148 | + $displayName = $this->userSession->getUser()->getDisplayName(); |
|
| 149 | + |
|
| 150 | + $template = $this->mailer->createEMailTemplate(); |
|
| 151 | + $template->addHeader(); |
|
| 152 | + $template->addHeading($this->l10n->t('Well done, %s!', [$displayName])); |
|
| 153 | + $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.')); |
|
| 154 | + $template->addFooter(); |
|
| 155 | + |
|
| 156 | + $message = $this->mailer->createMessage(); |
|
| 157 | + $message->setTo([$email => $displayName]); |
|
| 158 | + $message->setSubject($this->l10n->t('Email setting test')); |
|
| 159 | + $message->setHtmlBody($template->renderHtml()); |
|
| 160 | + $message->setPlainBody($template->renderText()); |
|
| 161 | + $errors = $this->mailer->send($message); |
|
| 162 | + if (!empty($errors)) { |
|
| 163 | + throw new \RuntimeException($this->l10n->t('Mail could not be sent. Check your mail server log')); |
|
| 164 | + } |
|
| 165 | + return new DataResponse(); |
|
| 166 | + } catch (\Exception $e) { |
|
| 167 | + return new DataResponse($this->l10n->t('A problem occurred while sending the email. Please revise your settings. (Error: %s)', [$e->getMessage()]), Http::STATUS_BAD_REQUEST); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return new DataResponse($this->l10n->t('You need to set your user email before being able to send test emails.'), Http::STATUS_BAD_REQUEST); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | } |
@@ -51,856 +51,856 @@ |
||
| 51 | 51 | */ |
| 52 | 52 | class ShareByMailProvider implements IShareProvider { |
| 53 | 53 | |
| 54 | - /** @var IDBConnection */ |
|
| 55 | - private $dbConnection; |
|
| 56 | - |
|
| 57 | - /** @var ILogger */ |
|
| 58 | - private $logger; |
|
| 59 | - |
|
| 60 | - /** @var ISecureRandom */ |
|
| 61 | - private $secureRandom; |
|
| 62 | - |
|
| 63 | - /** @var IUserManager */ |
|
| 64 | - private $userManager; |
|
| 65 | - |
|
| 66 | - /** @var IRootFolder */ |
|
| 67 | - private $rootFolder; |
|
| 68 | - |
|
| 69 | - /** @var IL10N */ |
|
| 70 | - private $l; |
|
| 71 | - |
|
| 72 | - /** @var IMailer */ |
|
| 73 | - private $mailer; |
|
| 74 | - |
|
| 75 | - /** @var IURLGenerator */ |
|
| 76 | - private $urlGenerator; |
|
| 77 | - |
|
| 78 | - /** @var IManager */ |
|
| 79 | - private $activityManager; |
|
| 80 | - |
|
| 81 | - /** @var SettingsManager */ |
|
| 82 | - private $settingsManager; |
|
| 83 | - |
|
| 84 | - /** @var Defaults */ |
|
| 85 | - private $defaults; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Return the identifier of this provider. |
|
| 89 | - * |
|
| 90 | - * @return string Containing only [a-zA-Z0-9] |
|
| 91 | - */ |
|
| 92 | - public function identifier() { |
|
| 93 | - return 'ocMailShare'; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * DefaultShareProvider constructor. |
|
| 98 | - * |
|
| 99 | - * @param IDBConnection $connection |
|
| 100 | - * @param ISecureRandom $secureRandom |
|
| 101 | - * @param IUserManager $userManager |
|
| 102 | - * @param IRootFolder $rootFolder |
|
| 103 | - * @param IL10N $l |
|
| 104 | - * @param ILogger $logger |
|
| 105 | - * @param IMailer $mailer |
|
| 106 | - * @param IURLGenerator $urlGenerator |
|
| 107 | - * @param IManager $activityManager |
|
| 108 | - * @param SettingsManager $settingsManager |
|
| 109 | - * @param Defaults $defaults |
|
| 110 | - */ |
|
| 111 | - public function __construct( |
|
| 112 | - IDBConnection $connection, |
|
| 113 | - ISecureRandom $secureRandom, |
|
| 114 | - IUserManager $userManager, |
|
| 115 | - IRootFolder $rootFolder, |
|
| 116 | - IL10N $l, |
|
| 117 | - ILogger $logger, |
|
| 118 | - IMailer $mailer, |
|
| 119 | - IURLGenerator $urlGenerator, |
|
| 120 | - IManager $activityManager, |
|
| 121 | - SettingsManager $settingsManager, |
|
| 122 | - Defaults $defaults |
|
| 123 | - ) { |
|
| 124 | - $this->dbConnection = $connection; |
|
| 125 | - $this->secureRandom = $secureRandom; |
|
| 126 | - $this->userManager = $userManager; |
|
| 127 | - $this->rootFolder = $rootFolder; |
|
| 128 | - $this->l = $l; |
|
| 129 | - $this->logger = $logger; |
|
| 130 | - $this->mailer = $mailer; |
|
| 131 | - $this->urlGenerator = $urlGenerator; |
|
| 132 | - $this->activityManager = $activityManager; |
|
| 133 | - $this->settingsManager = $settingsManager; |
|
| 134 | - $this->defaults = $defaults; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Share a path |
|
| 139 | - * |
|
| 140 | - * @param IShare $share |
|
| 141 | - * @return IShare The share object |
|
| 142 | - * @throws ShareNotFound |
|
| 143 | - * @throws \Exception |
|
| 144 | - */ |
|
| 145 | - public function create(IShare $share) { |
|
| 146 | - |
|
| 147 | - $shareWith = $share->getSharedWith(); |
|
| 148 | - /* |
|
| 54 | + /** @var IDBConnection */ |
|
| 55 | + private $dbConnection; |
|
| 56 | + |
|
| 57 | + /** @var ILogger */ |
|
| 58 | + private $logger; |
|
| 59 | + |
|
| 60 | + /** @var ISecureRandom */ |
|
| 61 | + private $secureRandom; |
|
| 62 | + |
|
| 63 | + /** @var IUserManager */ |
|
| 64 | + private $userManager; |
|
| 65 | + |
|
| 66 | + /** @var IRootFolder */ |
|
| 67 | + private $rootFolder; |
|
| 68 | + |
|
| 69 | + /** @var IL10N */ |
|
| 70 | + private $l; |
|
| 71 | + |
|
| 72 | + /** @var IMailer */ |
|
| 73 | + private $mailer; |
|
| 74 | + |
|
| 75 | + /** @var IURLGenerator */ |
|
| 76 | + private $urlGenerator; |
|
| 77 | + |
|
| 78 | + /** @var IManager */ |
|
| 79 | + private $activityManager; |
|
| 80 | + |
|
| 81 | + /** @var SettingsManager */ |
|
| 82 | + private $settingsManager; |
|
| 83 | + |
|
| 84 | + /** @var Defaults */ |
|
| 85 | + private $defaults; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Return the identifier of this provider. |
|
| 89 | + * |
|
| 90 | + * @return string Containing only [a-zA-Z0-9] |
|
| 91 | + */ |
|
| 92 | + public function identifier() { |
|
| 93 | + return 'ocMailShare'; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * DefaultShareProvider constructor. |
|
| 98 | + * |
|
| 99 | + * @param IDBConnection $connection |
|
| 100 | + * @param ISecureRandom $secureRandom |
|
| 101 | + * @param IUserManager $userManager |
|
| 102 | + * @param IRootFolder $rootFolder |
|
| 103 | + * @param IL10N $l |
|
| 104 | + * @param ILogger $logger |
|
| 105 | + * @param IMailer $mailer |
|
| 106 | + * @param IURLGenerator $urlGenerator |
|
| 107 | + * @param IManager $activityManager |
|
| 108 | + * @param SettingsManager $settingsManager |
|
| 109 | + * @param Defaults $defaults |
|
| 110 | + */ |
|
| 111 | + public function __construct( |
|
| 112 | + IDBConnection $connection, |
|
| 113 | + ISecureRandom $secureRandom, |
|
| 114 | + IUserManager $userManager, |
|
| 115 | + IRootFolder $rootFolder, |
|
| 116 | + IL10N $l, |
|
| 117 | + ILogger $logger, |
|
| 118 | + IMailer $mailer, |
|
| 119 | + IURLGenerator $urlGenerator, |
|
| 120 | + IManager $activityManager, |
|
| 121 | + SettingsManager $settingsManager, |
|
| 122 | + Defaults $defaults |
|
| 123 | + ) { |
|
| 124 | + $this->dbConnection = $connection; |
|
| 125 | + $this->secureRandom = $secureRandom; |
|
| 126 | + $this->userManager = $userManager; |
|
| 127 | + $this->rootFolder = $rootFolder; |
|
| 128 | + $this->l = $l; |
|
| 129 | + $this->logger = $logger; |
|
| 130 | + $this->mailer = $mailer; |
|
| 131 | + $this->urlGenerator = $urlGenerator; |
|
| 132 | + $this->activityManager = $activityManager; |
|
| 133 | + $this->settingsManager = $settingsManager; |
|
| 134 | + $this->defaults = $defaults; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Share a path |
|
| 139 | + * |
|
| 140 | + * @param IShare $share |
|
| 141 | + * @return IShare The share object |
|
| 142 | + * @throws ShareNotFound |
|
| 143 | + * @throws \Exception |
|
| 144 | + */ |
|
| 145 | + public function create(IShare $share) { |
|
| 146 | + |
|
| 147 | + $shareWith = $share->getSharedWith(); |
|
| 148 | + /* |
|
| 149 | 149 | * Check if file is not already shared with the remote user |
| 150 | 150 | */ |
| 151 | - $alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0); |
|
| 152 | - if (!empty($alreadyShared)) { |
|
| 153 | - $message = 'Sharing %s failed, this item is already shared with %s'; |
|
| 154 | - $message_t = $this->l->t('Sharing %s failed, this item is already shared with %s', array($share->getNode()->getName(), $shareWith)); |
|
| 155 | - $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']); |
|
| 156 | - throw new \Exception($message_t); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $shareId = $this->createMailShare($share); |
|
| 160 | - $this->createActivity($share); |
|
| 161 | - $data = $this->getRawShare($shareId); |
|
| 162 | - return $this->createShareObject($data); |
|
| 163 | - |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * create activity if a file/folder was shared by mail |
|
| 168 | - * |
|
| 169 | - * @param IShare $share |
|
| 170 | - */ |
|
| 171 | - protected function createActivity(IShare $share) { |
|
| 172 | - |
|
| 173 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 174 | - |
|
| 175 | - $this->publishActivity( |
|
| 176 | - Activity::SUBJECT_SHARED_EMAIL_SELF, |
|
| 177 | - [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()], |
|
| 178 | - $share->getSharedBy(), |
|
| 179 | - $share->getNode()->getId(), |
|
| 180 | - $userFolder->getRelativePath($share->getNode()->getPath()) |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 184 | - $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 185 | - $fileId = $share->getNode()->getId(); |
|
| 186 | - $nodes = $ownerFolder->getById($fileId); |
|
| 187 | - $ownerPath = $nodes[0]->getPath(); |
|
| 188 | - $this->publishActivity( |
|
| 189 | - Activity::SUBJECT_SHARED_EMAIL_BY, |
|
| 190 | - [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()], |
|
| 191 | - $share->getShareOwner(), |
|
| 192 | - $fileId, |
|
| 193 | - $ownerFolder->getRelativePath($ownerPath) |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * publish activity if a file/folder was shared by mail |
|
| 201 | - * |
|
| 202 | - * @param $subject |
|
| 203 | - * @param $parameters |
|
| 204 | - * @param $affectedUser |
|
| 205 | - * @param $fileId |
|
| 206 | - * @param $filePath |
|
| 207 | - */ |
|
| 208 | - protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) { |
|
| 209 | - $event = $this->activityManager->generateEvent(); |
|
| 210 | - $event->setApp('sharebymail') |
|
| 211 | - ->setType('shared') |
|
| 212 | - ->setSubject($subject, $parameters) |
|
| 213 | - ->setAffectedUser($affectedUser) |
|
| 214 | - ->setObject('files', $fileId, $filePath); |
|
| 215 | - $this->activityManager->publish($event); |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @param IShare $share |
|
| 221 | - * @return int |
|
| 222 | - * @throws \Exception |
|
| 223 | - */ |
|
| 224 | - protected function createMailShare(IShare $share) { |
|
| 225 | - $share->setToken($this->generateToken()); |
|
| 226 | - $shareId = $this->addShareToDB( |
|
| 227 | - $share->getNodeId(), |
|
| 228 | - $share->getNodeType(), |
|
| 229 | - $share->getSharedWith(), |
|
| 230 | - $share->getSharedBy(), |
|
| 231 | - $share->getShareOwner(), |
|
| 232 | - $share->getPermissions(), |
|
| 233 | - $share->getToken() |
|
| 234 | - ); |
|
| 235 | - |
|
| 236 | - try { |
|
| 237 | - $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', |
|
| 238 | - ['token' => $share->getToken()]); |
|
| 239 | - $this->sendMailNotification( |
|
| 240 | - $share->getNode()->getName(), |
|
| 241 | - $link, |
|
| 242 | - $share->getShareOwner(), |
|
| 243 | - $share->getSharedBy(), |
|
| 244 | - $share->getSharedWith() |
|
| 245 | - ); |
|
| 246 | - } catch (HintException $hintException) { |
|
| 247 | - $this->logger->error('Failed to send share by mail: ' . $hintException->getMessage()); |
|
| 248 | - $this->removeShareFromTable($shareId); |
|
| 249 | - throw $hintException; |
|
| 250 | - } catch (\Exception $e) { |
|
| 251 | - $this->logger->error('Failed to send share by mail: ' . $e->getMessage()); |
|
| 252 | - $this->removeShareFromTable($shareId); |
|
| 253 | - throw new HintException('Failed to send share by mail', |
|
| 254 | - $this->l->t('Failed to send share by E-mail')); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $shareId; |
|
| 258 | - |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * @param string $filename |
|
| 263 | - * @param string $link |
|
| 264 | - * @param string $owner |
|
| 265 | - * @param string $initiator |
|
| 266 | - * @param string $shareWith |
|
| 267 | - * @throws \Exception If mail couldn't be sent |
|
| 268 | - */ |
|
| 269 | - protected function sendMailNotification($filename, |
|
| 270 | - $link, |
|
| 271 | - $owner, |
|
| 272 | - $initiator, |
|
| 273 | - $shareWith) { |
|
| 274 | - $ownerUser = $this->userManager->get($owner); |
|
| 275 | - $initiatorUser = $this->userManager->get($initiator); |
|
| 276 | - $ownerDisplayName = ($ownerUser instanceof IUser) ? $ownerUser->getDisplayName() : $owner; |
|
| 277 | - $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
| 278 | - if ($owner === $initiator) { |
|
| 279 | - $subject = (string)$this->l->t('%s shared »%s« with you', array($ownerDisplayName, $filename)); |
|
| 280 | - } else { |
|
| 281 | - $subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($ownerDisplayName, $filename, $initiatorDisplayName)); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $message = $this->mailer->createMessage(); |
|
| 285 | - |
|
| 286 | - $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 287 | - |
|
| 288 | - $emailTemplate->addHeader(); |
|
| 289 | - $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$ownerDisplayName, $filename]), false); |
|
| 290 | - |
|
| 291 | - if ($owner === $initiator) { |
|
| 292 | - $text = $this->l->t('%s shared »%s« with you.', [$ownerDisplayName, $filename]); |
|
| 293 | - } else { |
|
| 294 | - $text= $this->l->t('%s shared »%s« with you on behalf of %s.', [$ownerDisplayName, $filename, $initiator]); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - $emailTemplate->addBodyText( |
|
| 298 | - $text . ' ' . $this->l->t('Click the button below to open it.'), |
|
| 299 | - $text |
|
| 300 | - ); |
|
| 301 | - |
|
| 302 | - $emailTemplate->addBodyButton( |
|
| 303 | - $this->l->t('Open »%s«', [$filename]), |
|
| 304 | - $link |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - $message->setTo([$shareWith]); |
|
| 308 | - |
|
| 309 | - // The "From" contains the sharers name |
|
| 310 | - $instanceName = $this->defaults->getName(); |
|
| 311 | - $senderName = $this->l->t( |
|
| 312 | - '%s via %s', |
|
| 313 | - [ |
|
| 314 | - $ownerDisplayName, |
|
| 315 | - $instanceName |
|
| 316 | - ] |
|
| 317 | - ); |
|
| 318 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
| 319 | - |
|
| 320 | - // The "Reply-To" is set to the sharer if an mail address is configured |
|
| 321 | - // also the default footer contains a "Do not reply" which needs to be adjusted. |
|
| 322 | - $ownerEmail = $ownerUser->getEMailAddress(); |
|
| 323 | - if($ownerEmail !== null) { |
|
| 324 | - $message->setReplyTo([$ownerEmail => $ownerDisplayName]); |
|
| 325 | - $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
|
| 326 | - } else { |
|
| 327 | - $emailTemplate->addFooter(); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - $message->setSubject($subject); |
|
| 331 | - $message->setPlainBody($emailTemplate->renderText()); |
|
| 332 | - $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 333 | - $this->mailer->send($message); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * send password to recipient of a mail share |
|
| 338 | - * |
|
| 339 | - * @param string $filename |
|
| 340 | - * @param string $initiator |
|
| 341 | - * @param string $shareWith |
|
| 342 | - */ |
|
| 343 | - protected function sendPassword($filename, $initiator, $shareWith, $password) { |
|
| 344 | - |
|
| 345 | - if ($this->settingsManager->sendPasswordByMail() === false) { |
|
| 346 | - return; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - $initiatorUser = $this->userManager->get($initiator); |
|
| 350 | - $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
| 351 | - $subject = (string)$this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]); |
|
| 352 | - |
|
| 353 | - $message = $this->mailer->createMessage(); |
|
| 354 | - |
|
| 355 | - $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 356 | - |
|
| 357 | - $emailTemplate->addHeader(); |
|
| 358 | - $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename])); |
|
| 359 | - |
|
| 360 | - $emailTemplate->addBodyText($this->l->t( |
|
| 361 | - '%s shared »%s« with you. You should have already received a separate mail with a link to access it.', |
|
| 362 | - [$initiatorDisplayName, $filename] |
|
| 363 | - )); |
|
| 364 | - $emailTemplate->addBodyText($this->l->t('It is protected with the following password: %s', [$password])); |
|
| 365 | - |
|
| 366 | - $emailTemplate->addFooter(); |
|
| 367 | - |
|
| 368 | - $message->setTo([$shareWith]); |
|
| 369 | - $message->setSubject($subject); |
|
| 370 | - $message->setBody($emailTemplate->renderText(), 'text/plain'); |
|
| 371 | - $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 372 | - $this->mailer->send($message); |
|
| 373 | - |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * generate share token |
|
| 379 | - * |
|
| 380 | - * @return string |
|
| 381 | - */ |
|
| 382 | - protected function generateToken() { |
|
| 383 | - $token = $this->secureRandom->generate( |
|
| 384 | - 15, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS); |
|
| 385 | - return $token; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Get all children of this share |
|
| 390 | - * |
|
| 391 | - * @param IShare $parent |
|
| 392 | - * @return IShare[] |
|
| 393 | - */ |
|
| 394 | - public function getChildren(IShare $parent) { |
|
| 395 | - $children = []; |
|
| 396 | - |
|
| 397 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 398 | - $qb->select('*') |
|
| 399 | - ->from('share') |
|
| 400 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 401 | - ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 402 | - ->orderBy('id'); |
|
| 403 | - |
|
| 404 | - $cursor = $qb->execute(); |
|
| 405 | - while($data = $cursor->fetch()) { |
|
| 406 | - $children[] = $this->createShareObject($data); |
|
| 407 | - } |
|
| 408 | - $cursor->closeCursor(); |
|
| 409 | - |
|
| 410 | - return $children; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * add share to the database and return the ID |
|
| 415 | - * |
|
| 416 | - * @param int $itemSource |
|
| 417 | - * @param string $itemType |
|
| 418 | - * @param string $shareWith |
|
| 419 | - * @param string $sharedBy |
|
| 420 | - * @param string $uidOwner |
|
| 421 | - * @param int $permissions |
|
| 422 | - * @param string $token |
|
| 423 | - * @return int |
|
| 424 | - */ |
|
| 425 | - protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) { |
|
| 426 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 427 | - $qb->insert('share') |
|
| 428 | - ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)) |
|
| 429 | - ->setValue('item_type', $qb->createNamedParameter($itemType)) |
|
| 430 | - ->setValue('item_source', $qb->createNamedParameter($itemSource)) |
|
| 431 | - ->setValue('file_source', $qb->createNamedParameter($itemSource)) |
|
| 432 | - ->setValue('share_with', $qb->createNamedParameter($shareWith)) |
|
| 433 | - ->setValue('uid_owner', $qb->createNamedParameter($uidOwner)) |
|
| 434 | - ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy)) |
|
| 435 | - ->setValue('permissions', $qb->createNamedParameter($permissions)) |
|
| 436 | - ->setValue('token', $qb->createNamedParameter($token)) |
|
| 437 | - ->setValue('stime', $qb->createNamedParameter(time())); |
|
| 438 | - |
|
| 439 | - /* |
|
| 151 | + $alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0); |
|
| 152 | + if (!empty($alreadyShared)) { |
|
| 153 | + $message = 'Sharing %s failed, this item is already shared with %s'; |
|
| 154 | + $message_t = $this->l->t('Sharing %s failed, this item is already shared with %s', array($share->getNode()->getName(), $shareWith)); |
|
| 155 | + $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']); |
|
| 156 | + throw new \Exception($message_t); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $shareId = $this->createMailShare($share); |
|
| 160 | + $this->createActivity($share); |
|
| 161 | + $data = $this->getRawShare($shareId); |
|
| 162 | + return $this->createShareObject($data); |
|
| 163 | + |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * create activity if a file/folder was shared by mail |
|
| 168 | + * |
|
| 169 | + * @param IShare $share |
|
| 170 | + */ |
|
| 171 | + protected function createActivity(IShare $share) { |
|
| 172 | + |
|
| 173 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 174 | + |
|
| 175 | + $this->publishActivity( |
|
| 176 | + Activity::SUBJECT_SHARED_EMAIL_SELF, |
|
| 177 | + [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()], |
|
| 178 | + $share->getSharedBy(), |
|
| 179 | + $share->getNode()->getId(), |
|
| 180 | + $userFolder->getRelativePath($share->getNode()->getPath()) |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 184 | + $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 185 | + $fileId = $share->getNode()->getId(); |
|
| 186 | + $nodes = $ownerFolder->getById($fileId); |
|
| 187 | + $ownerPath = $nodes[0]->getPath(); |
|
| 188 | + $this->publishActivity( |
|
| 189 | + Activity::SUBJECT_SHARED_EMAIL_BY, |
|
| 190 | + [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()], |
|
| 191 | + $share->getShareOwner(), |
|
| 192 | + $fileId, |
|
| 193 | + $ownerFolder->getRelativePath($ownerPath) |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * publish activity if a file/folder was shared by mail |
|
| 201 | + * |
|
| 202 | + * @param $subject |
|
| 203 | + * @param $parameters |
|
| 204 | + * @param $affectedUser |
|
| 205 | + * @param $fileId |
|
| 206 | + * @param $filePath |
|
| 207 | + */ |
|
| 208 | + protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) { |
|
| 209 | + $event = $this->activityManager->generateEvent(); |
|
| 210 | + $event->setApp('sharebymail') |
|
| 211 | + ->setType('shared') |
|
| 212 | + ->setSubject($subject, $parameters) |
|
| 213 | + ->setAffectedUser($affectedUser) |
|
| 214 | + ->setObject('files', $fileId, $filePath); |
|
| 215 | + $this->activityManager->publish($event); |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @param IShare $share |
|
| 221 | + * @return int |
|
| 222 | + * @throws \Exception |
|
| 223 | + */ |
|
| 224 | + protected function createMailShare(IShare $share) { |
|
| 225 | + $share->setToken($this->generateToken()); |
|
| 226 | + $shareId = $this->addShareToDB( |
|
| 227 | + $share->getNodeId(), |
|
| 228 | + $share->getNodeType(), |
|
| 229 | + $share->getSharedWith(), |
|
| 230 | + $share->getSharedBy(), |
|
| 231 | + $share->getShareOwner(), |
|
| 232 | + $share->getPermissions(), |
|
| 233 | + $share->getToken() |
|
| 234 | + ); |
|
| 235 | + |
|
| 236 | + try { |
|
| 237 | + $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', |
|
| 238 | + ['token' => $share->getToken()]); |
|
| 239 | + $this->sendMailNotification( |
|
| 240 | + $share->getNode()->getName(), |
|
| 241 | + $link, |
|
| 242 | + $share->getShareOwner(), |
|
| 243 | + $share->getSharedBy(), |
|
| 244 | + $share->getSharedWith() |
|
| 245 | + ); |
|
| 246 | + } catch (HintException $hintException) { |
|
| 247 | + $this->logger->error('Failed to send share by mail: ' . $hintException->getMessage()); |
|
| 248 | + $this->removeShareFromTable($shareId); |
|
| 249 | + throw $hintException; |
|
| 250 | + } catch (\Exception $e) { |
|
| 251 | + $this->logger->error('Failed to send share by mail: ' . $e->getMessage()); |
|
| 252 | + $this->removeShareFromTable($shareId); |
|
| 253 | + throw new HintException('Failed to send share by mail', |
|
| 254 | + $this->l->t('Failed to send share by E-mail')); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $shareId; |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * @param string $filename |
|
| 263 | + * @param string $link |
|
| 264 | + * @param string $owner |
|
| 265 | + * @param string $initiator |
|
| 266 | + * @param string $shareWith |
|
| 267 | + * @throws \Exception If mail couldn't be sent |
|
| 268 | + */ |
|
| 269 | + protected function sendMailNotification($filename, |
|
| 270 | + $link, |
|
| 271 | + $owner, |
|
| 272 | + $initiator, |
|
| 273 | + $shareWith) { |
|
| 274 | + $ownerUser = $this->userManager->get($owner); |
|
| 275 | + $initiatorUser = $this->userManager->get($initiator); |
|
| 276 | + $ownerDisplayName = ($ownerUser instanceof IUser) ? $ownerUser->getDisplayName() : $owner; |
|
| 277 | + $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
| 278 | + if ($owner === $initiator) { |
|
| 279 | + $subject = (string)$this->l->t('%s shared »%s« with you', array($ownerDisplayName, $filename)); |
|
| 280 | + } else { |
|
| 281 | + $subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($ownerDisplayName, $filename, $initiatorDisplayName)); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $message = $this->mailer->createMessage(); |
|
| 285 | + |
|
| 286 | + $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 287 | + |
|
| 288 | + $emailTemplate->addHeader(); |
|
| 289 | + $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$ownerDisplayName, $filename]), false); |
|
| 290 | + |
|
| 291 | + if ($owner === $initiator) { |
|
| 292 | + $text = $this->l->t('%s shared »%s« with you.', [$ownerDisplayName, $filename]); |
|
| 293 | + } else { |
|
| 294 | + $text= $this->l->t('%s shared »%s« with you on behalf of %s.', [$ownerDisplayName, $filename, $initiator]); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + $emailTemplate->addBodyText( |
|
| 298 | + $text . ' ' . $this->l->t('Click the button below to open it.'), |
|
| 299 | + $text |
|
| 300 | + ); |
|
| 301 | + |
|
| 302 | + $emailTemplate->addBodyButton( |
|
| 303 | + $this->l->t('Open »%s«', [$filename]), |
|
| 304 | + $link |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + $message->setTo([$shareWith]); |
|
| 308 | + |
|
| 309 | + // The "From" contains the sharers name |
|
| 310 | + $instanceName = $this->defaults->getName(); |
|
| 311 | + $senderName = $this->l->t( |
|
| 312 | + '%s via %s', |
|
| 313 | + [ |
|
| 314 | + $ownerDisplayName, |
|
| 315 | + $instanceName |
|
| 316 | + ] |
|
| 317 | + ); |
|
| 318 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
| 319 | + |
|
| 320 | + // The "Reply-To" is set to the sharer if an mail address is configured |
|
| 321 | + // also the default footer contains a "Do not reply" which needs to be adjusted. |
|
| 322 | + $ownerEmail = $ownerUser->getEMailAddress(); |
|
| 323 | + if($ownerEmail !== null) { |
|
| 324 | + $message->setReplyTo([$ownerEmail => $ownerDisplayName]); |
|
| 325 | + $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
|
| 326 | + } else { |
|
| 327 | + $emailTemplate->addFooter(); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + $message->setSubject($subject); |
|
| 331 | + $message->setPlainBody($emailTemplate->renderText()); |
|
| 332 | + $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 333 | + $this->mailer->send($message); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * send password to recipient of a mail share |
|
| 338 | + * |
|
| 339 | + * @param string $filename |
|
| 340 | + * @param string $initiator |
|
| 341 | + * @param string $shareWith |
|
| 342 | + */ |
|
| 343 | + protected function sendPassword($filename, $initiator, $shareWith, $password) { |
|
| 344 | + |
|
| 345 | + if ($this->settingsManager->sendPasswordByMail() === false) { |
|
| 346 | + return; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + $initiatorUser = $this->userManager->get($initiator); |
|
| 350 | + $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
| 351 | + $subject = (string)$this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]); |
|
| 352 | + |
|
| 353 | + $message = $this->mailer->createMessage(); |
|
| 354 | + |
|
| 355 | + $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 356 | + |
|
| 357 | + $emailTemplate->addHeader(); |
|
| 358 | + $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename])); |
|
| 359 | + |
|
| 360 | + $emailTemplate->addBodyText($this->l->t( |
|
| 361 | + '%s shared »%s« with you. You should have already received a separate mail with a link to access it.', |
|
| 362 | + [$initiatorDisplayName, $filename] |
|
| 363 | + )); |
|
| 364 | + $emailTemplate->addBodyText($this->l->t('It is protected with the following password: %s', [$password])); |
|
| 365 | + |
|
| 366 | + $emailTemplate->addFooter(); |
|
| 367 | + |
|
| 368 | + $message->setTo([$shareWith]); |
|
| 369 | + $message->setSubject($subject); |
|
| 370 | + $message->setBody($emailTemplate->renderText(), 'text/plain'); |
|
| 371 | + $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 372 | + $this->mailer->send($message); |
|
| 373 | + |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * generate share token |
|
| 379 | + * |
|
| 380 | + * @return string |
|
| 381 | + */ |
|
| 382 | + protected function generateToken() { |
|
| 383 | + $token = $this->secureRandom->generate( |
|
| 384 | + 15, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS); |
|
| 385 | + return $token; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Get all children of this share |
|
| 390 | + * |
|
| 391 | + * @param IShare $parent |
|
| 392 | + * @return IShare[] |
|
| 393 | + */ |
|
| 394 | + public function getChildren(IShare $parent) { |
|
| 395 | + $children = []; |
|
| 396 | + |
|
| 397 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 398 | + $qb->select('*') |
|
| 399 | + ->from('share') |
|
| 400 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 401 | + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 402 | + ->orderBy('id'); |
|
| 403 | + |
|
| 404 | + $cursor = $qb->execute(); |
|
| 405 | + while($data = $cursor->fetch()) { |
|
| 406 | + $children[] = $this->createShareObject($data); |
|
| 407 | + } |
|
| 408 | + $cursor->closeCursor(); |
|
| 409 | + |
|
| 410 | + return $children; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * add share to the database and return the ID |
|
| 415 | + * |
|
| 416 | + * @param int $itemSource |
|
| 417 | + * @param string $itemType |
|
| 418 | + * @param string $shareWith |
|
| 419 | + * @param string $sharedBy |
|
| 420 | + * @param string $uidOwner |
|
| 421 | + * @param int $permissions |
|
| 422 | + * @param string $token |
|
| 423 | + * @return int |
|
| 424 | + */ |
|
| 425 | + protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) { |
|
| 426 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 427 | + $qb->insert('share') |
|
| 428 | + ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)) |
|
| 429 | + ->setValue('item_type', $qb->createNamedParameter($itemType)) |
|
| 430 | + ->setValue('item_source', $qb->createNamedParameter($itemSource)) |
|
| 431 | + ->setValue('file_source', $qb->createNamedParameter($itemSource)) |
|
| 432 | + ->setValue('share_with', $qb->createNamedParameter($shareWith)) |
|
| 433 | + ->setValue('uid_owner', $qb->createNamedParameter($uidOwner)) |
|
| 434 | + ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy)) |
|
| 435 | + ->setValue('permissions', $qb->createNamedParameter($permissions)) |
|
| 436 | + ->setValue('token', $qb->createNamedParameter($token)) |
|
| 437 | + ->setValue('stime', $qb->createNamedParameter(time())); |
|
| 438 | + |
|
| 439 | + /* |
|
| 440 | 440 | * Added to fix https://github.com/owncloud/core/issues/22215 |
| 441 | 441 | * Can be removed once we get rid of ajax/share.php |
| 442 | 442 | */ |
| 443 | - $qb->setValue('file_target', $qb->createNamedParameter('')); |
|
| 443 | + $qb->setValue('file_target', $qb->createNamedParameter('')); |
|
| 444 | 444 | |
| 445 | - $qb->execute(); |
|
| 446 | - $id = $qb->getLastInsertId(); |
|
| 445 | + $qb->execute(); |
|
| 446 | + $id = $qb->getLastInsertId(); |
|
| 447 | 447 | |
| 448 | - return (int)$id; |
|
| 449 | - } |
|
| 448 | + return (int)$id; |
|
| 449 | + } |
|
| 450 | 450 | |
| 451 | - /** |
|
| 452 | - * Update a share |
|
| 453 | - * |
|
| 454 | - * @param IShare $share |
|
| 455 | - * @param string|null $plainTextPassword |
|
| 456 | - * @return IShare The share object |
|
| 457 | - */ |
|
| 458 | - public function update(IShare $share, $plainTextPassword = null) { |
|
| 451 | + /** |
|
| 452 | + * Update a share |
|
| 453 | + * |
|
| 454 | + * @param IShare $share |
|
| 455 | + * @param string|null $plainTextPassword |
|
| 456 | + * @return IShare The share object |
|
| 457 | + */ |
|
| 458 | + public function update(IShare $share, $plainTextPassword = null) { |
|
| 459 | 459 | |
| 460 | - $originalShare = $this->getShareById($share->getId()); |
|
| 460 | + $originalShare = $this->getShareById($share->getId()); |
|
| 461 | 461 | |
| 462 | - // a real password was given |
|
| 463 | - $validPassword = $plainTextPassword !== null && $plainTextPassword !== ''; |
|
| 462 | + // a real password was given |
|
| 463 | + $validPassword = $plainTextPassword !== null && $plainTextPassword !== ''; |
|
| 464 | 464 | |
| 465 | - if($validPassword && $originalShare->getPassword() !== $share->getPassword()) { |
|
| 466 | - $this->sendPassword($share->getNode()->getName(), $share->getSharedBy(), $share->getSharedWith(), $plainTextPassword); |
|
| 467 | - } |
|
| 468 | - /* |
|
| 465 | + if($validPassword && $originalShare->getPassword() !== $share->getPassword()) { |
|
| 466 | + $this->sendPassword($share->getNode()->getName(), $share->getSharedBy(), $share->getSharedWith(), $plainTextPassword); |
|
| 467 | + } |
|
| 468 | + /* |
|
| 469 | 469 | * We allow updating the permissions and password of mail shares |
| 470 | 470 | */ |
| 471 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 472 | - $qb->update('share') |
|
| 473 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 474 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 475 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 476 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 477 | - ->set('password', $qb->createNamedParameter($share->getPassword())) |
|
| 478 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 479 | - ->execute(); |
|
| 480 | - |
|
| 481 | - return $share; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * @inheritdoc |
|
| 486 | - */ |
|
| 487 | - public function move(IShare $share, $recipient) { |
|
| 488 | - /** |
|
| 489 | - * nothing to do here, mail shares are only outgoing shares |
|
| 490 | - */ |
|
| 491 | - return $share; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Delete a share (owner unShares the file) |
|
| 496 | - * |
|
| 497 | - * @param IShare $share |
|
| 498 | - */ |
|
| 499 | - public function delete(IShare $share) { |
|
| 500 | - $this->removeShareFromTable($share->getId()); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * @inheritdoc |
|
| 505 | - */ |
|
| 506 | - public function deleteFromSelf(IShare $share, $recipient) { |
|
| 507 | - // nothing to do here, mail shares are only outgoing shares |
|
| 508 | - return; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * @inheritdoc |
|
| 513 | - */ |
|
| 514 | - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 515 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 516 | - $qb->select('*') |
|
| 517 | - ->from('share'); |
|
| 518 | - |
|
| 519 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))); |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Reshares for this user are shares where they are the owner. |
|
| 523 | - */ |
|
| 524 | - if ($reshares === false) { |
|
| 525 | - //Special case for old shares created via the web UI |
|
| 526 | - $or1 = $qb->expr()->andX( |
|
| 527 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 528 | - $qb->expr()->isNull('uid_initiator') |
|
| 529 | - ); |
|
| 530 | - |
|
| 531 | - $qb->andWhere( |
|
| 532 | - $qb->expr()->orX( |
|
| 533 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)), |
|
| 534 | - $or1 |
|
| 535 | - ) |
|
| 536 | - ); |
|
| 537 | - } else { |
|
| 538 | - $qb->andWhere( |
|
| 539 | - $qb->expr()->orX( |
|
| 540 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 541 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 542 | - ) |
|
| 543 | - ); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - if ($node !== null) { |
|
| 547 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - if ($limit !== -1) { |
|
| 551 | - $qb->setMaxResults($limit); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - $qb->setFirstResult($offset); |
|
| 555 | - $qb->orderBy('id'); |
|
| 556 | - |
|
| 557 | - $cursor = $qb->execute(); |
|
| 558 | - $shares = []; |
|
| 559 | - while($data = $cursor->fetch()) { |
|
| 560 | - $shares[] = $this->createShareObject($data); |
|
| 561 | - } |
|
| 562 | - $cursor->closeCursor(); |
|
| 563 | - |
|
| 564 | - return $shares; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * @inheritdoc |
|
| 569 | - */ |
|
| 570 | - public function getShareById($id, $recipientId = null) { |
|
| 571 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 572 | - |
|
| 573 | - $qb->select('*') |
|
| 574 | - ->from('share') |
|
| 575 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 576 | - ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))); |
|
| 577 | - |
|
| 578 | - $cursor = $qb->execute(); |
|
| 579 | - $data = $cursor->fetch(); |
|
| 580 | - $cursor->closeCursor(); |
|
| 581 | - |
|
| 582 | - if ($data === false) { |
|
| 583 | - throw new ShareNotFound(); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - try { |
|
| 587 | - $share = $this->createShareObject($data); |
|
| 588 | - } catch (InvalidShare $e) { |
|
| 589 | - throw new ShareNotFound(); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - return $share; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Get shares for a given path |
|
| 597 | - * |
|
| 598 | - * @param \OCP\Files\Node $path |
|
| 599 | - * @return IShare[] |
|
| 600 | - */ |
|
| 601 | - public function getSharesByPath(Node $path) { |
|
| 602 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 603 | - |
|
| 604 | - $cursor = $qb->select('*') |
|
| 605 | - ->from('share') |
|
| 606 | - ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 607 | - ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 608 | - ->execute(); |
|
| 609 | - |
|
| 610 | - $shares = []; |
|
| 611 | - while($data = $cursor->fetch()) { |
|
| 612 | - $shares[] = $this->createShareObject($data); |
|
| 613 | - } |
|
| 614 | - $cursor->closeCursor(); |
|
| 615 | - |
|
| 616 | - return $shares; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * @inheritdoc |
|
| 621 | - */ |
|
| 622 | - public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 623 | - /** @var IShare[] $shares */ |
|
| 624 | - $shares = []; |
|
| 625 | - |
|
| 626 | - //Get shares directly with this user |
|
| 627 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 628 | - $qb->select('*') |
|
| 629 | - ->from('share'); |
|
| 630 | - |
|
| 631 | - // Order by id |
|
| 632 | - $qb->orderBy('id'); |
|
| 633 | - |
|
| 634 | - // Set limit and offset |
|
| 635 | - if ($limit !== -1) { |
|
| 636 | - $qb->setMaxResults($limit); |
|
| 637 | - } |
|
| 638 | - $qb->setFirstResult($offset); |
|
| 639 | - |
|
| 640 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))); |
|
| 641 | - $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))); |
|
| 642 | - |
|
| 643 | - // Filter by node if provided |
|
| 644 | - if ($node !== null) { |
|
| 645 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - $cursor = $qb->execute(); |
|
| 649 | - |
|
| 650 | - while($data = $cursor->fetch()) { |
|
| 651 | - $shares[] = $this->createShareObject($data); |
|
| 652 | - } |
|
| 653 | - $cursor->closeCursor(); |
|
| 654 | - |
|
| 655 | - |
|
| 656 | - return $shares; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Get a share by token |
|
| 661 | - * |
|
| 662 | - * @param string $token |
|
| 663 | - * @return IShare |
|
| 664 | - * @throws ShareNotFound |
|
| 665 | - */ |
|
| 666 | - public function getShareByToken($token) { |
|
| 667 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 668 | - |
|
| 669 | - $cursor = $qb->select('*') |
|
| 670 | - ->from('share') |
|
| 671 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 672 | - ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 673 | - ->execute(); |
|
| 674 | - |
|
| 675 | - $data = $cursor->fetch(); |
|
| 676 | - |
|
| 677 | - if ($data === false) { |
|
| 678 | - throw new ShareNotFound('Share not found', $this->l->t('Could not find share')); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - try { |
|
| 682 | - $share = $this->createShareObject($data); |
|
| 683 | - } catch (InvalidShare $e) { |
|
| 684 | - throw new ShareNotFound('Share not found', $this->l->t('Could not find share')); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - return $share; |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * remove share from table |
|
| 692 | - * |
|
| 693 | - * @param string $shareId |
|
| 694 | - */ |
|
| 695 | - protected function removeShareFromTable($shareId) { |
|
| 696 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 697 | - $qb->delete('share') |
|
| 698 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId))); |
|
| 699 | - $qb->execute(); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Create a share object from an database row |
|
| 704 | - * |
|
| 705 | - * @param array $data |
|
| 706 | - * @return IShare |
|
| 707 | - * @throws InvalidShare |
|
| 708 | - * @throws ShareNotFound |
|
| 709 | - */ |
|
| 710 | - protected function createShareObject($data) { |
|
| 711 | - |
|
| 712 | - $share = new Share($this->rootFolder, $this->userManager); |
|
| 713 | - $share->setId((int)$data['id']) |
|
| 714 | - ->setShareType((int)$data['share_type']) |
|
| 715 | - ->setPermissions((int)$data['permissions']) |
|
| 716 | - ->setTarget($data['file_target']) |
|
| 717 | - ->setMailSend((bool)$data['mail_send']) |
|
| 718 | - ->setToken($data['token']); |
|
| 719 | - |
|
| 720 | - $shareTime = new \DateTime(); |
|
| 721 | - $shareTime->setTimestamp((int)$data['stime']); |
|
| 722 | - $share->setShareTime($shareTime); |
|
| 723 | - $share->setSharedWith($data['share_with']); |
|
| 724 | - $share->setPassword($data['password']); |
|
| 725 | - |
|
| 726 | - if ($data['uid_initiator'] !== null) { |
|
| 727 | - $share->setShareOwner($data['uid_owner']); |
|
| 728 | - $share->setSharedBy($data['uid_initiator']); |
|
| 729 | - } else { |
|
| 730 | - //OLD SHARE |
|
| 731 | - $share->setSharedBy($data['uid_owner']); |
|
| 732 | - $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']); |
|
| 733 | - |
|
| 734 | - $owner = $path->getOwner(); |
|
| 735 | - $share->setShareOwner($owner->getUID()); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - if ($data['expiration'] !== null) { |
|
| 739 | - $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 740 | - if ($expiration !== false) { |
|
| 741 | - $share->setExpirationDate($expiration); |
|
| 742 | - } |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - $share->setNodeId((int)$data['file_source']); |
|
| 746 | - $share->setNodeType($data['item_type']); |
|
| 747 | - |
|
| 748 | - $share->setProviderId($this->identifier()); |
|
| 749 | - |
|
| 750 | - return $share; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Get the node with file $id for $user |
|
| 755 | - * |
|
| 756 | - * @param string $userId |
|
| 757 | - * @param int $id |
|
| 758 | - * @return \OCP\Files\File|\OCP\Files\Folder |
|
| 759 | - * @throws InvalidShare |
|
| 760 | - */ |
|
| 761 | - private function getNode($userId, $id) { |
|
| 762 | - try { |
|
| 763 | - $userFolder = $this->rootFolder->getUserFolder($userId); |
|
| 764 | - } catch (NotFoundException $e) { |
|
| 765 | - throw new InvalidShare(); |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - $nodes = $userFolder->getById($id); |
|
| 769 | - |
|
| 770 | - if (empty($nodes)) { |
|
| 771 | - throw new InvalidShare(); |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - return $nodes[0]; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * A user is deleted from the system |
|
| 779 | - * So clean up the relevant shares. |
|
| 780 | - * |
|
| 781 | - * @param string $uid |
|
| 782 | - * @param int $shareType |
|
| 783 | - */ |
|
| 784 | - public function userDeleted($uid, $shareType) { |
|
| 785 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 786 | - |
|
| 787 | - $qb->delete('share') |
|
| 788 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 789 | - ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))) |
|
| 790 | - ->execute(); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - /** |
|
| 794 | - * This provider does not support group shares |
|
| 795 | - * |
|
| 796 | - * @param string $gid |
|
| 797 | - */ |
|
| 798 | - public function groupDeleted($gid) { |
|
| 799 | - return; |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - /** |
|
| 803 | - * This provider does not support group shares |
|
| 804 | - * |
|
| 805 | - * @param string $uid |
|
| 806 | - * @param string $gid |
|
| 807 | - */ |
|
| 808 | - public function userDeletedFromGroup($uid, $gid) { |
|
| 809 | - return; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - /** |
|
| 813 | - * get database row of a give share |
|
| 814 | - * |
|
| 815 | - * @param $id |
|
| 816 | - * @return array |
|
| 817 | - * @throws ShareNotFound |
|
| 818 | - */ |
|
| 819 | - protected function getRawShare($id) { |
|
| 820 | - |
|
| 821 | - // Now fetch the inserted share and create a complete share object |
|
| 822 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 823 | - $qb->select('*') |
|
| 824 | - ->from('share') |
|
| 825 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 826 | - |
|
| 827 | - $cursor = $qb->execute(); |
|
| 828 | - $data = $cursor->fetch(); |
|
| 829 | - $cursor->closeCursor(); |
|
| 830 | - |
|
| 831 | - if ($data === false) { |
|
| 832 | - throw new ShareNotFound; |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - return $data; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 839 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 840 | - $qb->select('*') |
|
| 841 | - ->from('share', 's') |
|
| 842 | - ->andWhere($qb->expr()->orX( |
|
| 843 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 844 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 845 | - )) |
|
| 846 | - ->andWhere( |
|
| 847 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)) |
|
| 848 | - ); |
|
| 849 | - |
|
| 850 | - /** |
|
| 851 | - * Reshares for this user are shares where they are the owner. |
|
| 852 | - */ |
|
| 853 | - if ($reshares === false) { |
|
| 854 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 855 | - } else { |
|
| 856 | - $qb->andWhere( |
|
| 857 | - $qb->expr()->orX( |
|
| 858 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 859 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 860 | - ) |
|
| 861 | - ); |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 865 | - $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 866 | - |
|
| 867 | - $qb->orderBy('id'); |
|
| 868 | - |
|
| 869 | - $cursor = $qb->execute(); |
|
| 870 | - $shares = []; |
|
| 871 | - while ($data = $cursor->fetch()) { |
|
| 872 | - $shares[$data['fileid']][] = $this->createShareObject($data); |
|
| 873 | - } |
|
| 874 | - $cursor->closeCursor(); |
|
| 875 | - |
|
| 876 | - return $shares; |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * @inheritdoc |
|
| 881 | - */ |
|
| 882 | - public function getAccessList($nodes, $currentAccess) { |
|
| 883 | - $ids = []; |
|
| 884 | - foreach ($nodes as $node) { |
|
| 885 | - $ids[] = $node->getId(); |
|
| 886 | - } |
|
| 887 | - |
|
| 888 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 889 | - $qb->select('share_with') |
|
| 890 | - ->from('share') |
|
| 891 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 892 | - ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 893 | - ->andWhere($qb->expr()->orX( |
|
| 894 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 895 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 896 | - )) |
|
| 897 | - ->setMaxResults(1); |
|
| 898 | - $cursor = $qb->execute(); |
|
| 899 | - |
|
| 900 | - $mail = $cursor->fetch() !== false; |
|
| 901 | - $cursor->closeCursor(); |
|
| 902 | - |
|
| 903 | - return ['public' => $mail]; |
|
| 904 | - } |
|
| 471 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 472 | + $qb->update('share') |
|
| 473 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 474 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 475 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 476 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 477 | + ->set('password', $qb->createNamedParameter($share->getPassword())) |
|
| 478 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 479 | + ->execute(); |
|
| 480 | + |
|
| 481 | + return $share; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * @inheritdoc |
|
| 486 | + */ |
|
| 487 | + public function move(IShare $share, $recipient) { |
|
| 488 | + /** |
|
| 489 | + * nothing to do here, mail shares are only outgoing shares |
|
| 490 | + */ |
|
| 491 | + return $share; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Delete a share (owner unShares the file) |
|
| 496 | + * |
|
| 497 | + * @param IShare $share |
|
| 498 | + */ |
|
| 499 | + public function delete(IShare $share) { |
|
| 500 | + $this->removeShareFromTable($share->getId()); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * @inheritdoc |
|
| 505 | + */ |
|
| 506 | + public function deleteFromSelf(IShare $share, $recipient) { |
|
| 507 | + // nothing to do here, mail shares are only outgoing shares |
|
| 508 | + return; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * @inheritdoc |
|
| 513 | + */ |
|
| 514 | + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 515 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 516 | + $qb->select('*') |
|
| 517 | + ->from('share'); |
|
| 518 | + |
|
| 519 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))); |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Reshares for this user are shares where they are the owner. |
|
| 523 | + */ |
|
| 524 | + if ($reshares === false) { |
|
| 525 | + //Special case for old shares created via the web UI |
|
| 526 | + $or1 = $qb->expr()->andX( |
|
| 527 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 528 | + $qb->expr()->isNull('uid_initiator') |
|
| 529 | + ); |
|
| 530 | + |
|
| 531 | + $qb->andWhere( |
|
| 532 | + $qb->expr()->orX( |
|
| 533 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)), |
|
| 534 | + $or1 |
|
| 535 | + ) |
|
| 536 | + ); |
|
| 537 | + } else { |
|
| 538 | + $qb->andWhere( |
|
| 539 | + $qb->expr()->orX( |
|
| 540 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 541 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 542 | + ) |
|
| 543 | + ); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + if ($node !== null) { |
|
| 547 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + if ($limit !== -1) { |
|
| 551 | + $qb->setMaxResults($limit); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + $qb->setFirstResult($offset); |
|
| 555 | + $qb->orderBy('id'); |
|
| 556 | + |
|
| 557 | + $cursor = $qb->execute(); |
|
| 558 | + $shares = []; |
|
| 559 | + while($data = $cursor->fetch()) { |
|
| 560 | + $shares[] = $this->createShareObject($data); |
|
| 561 | + } |
|
| 562 | + $cursor->closeCursor(); |
|
| 563 | + |
|
| 564 | + return $shares; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * @inheritdoc |
|
| 569 | + */ |
|
| 570 | + public function getShareById($id, $recipientId = null) { |
|
| 571 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 572 | + |
|
| 573 | + $qb->select('*') |
|
| 574 | + ->from('share') |
|
| 575 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 576 | + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))); |
|
| 577 | + |
|
| 578 | + $cursor = $qb->execute(); |
|
| 579 | + $data = $cursor->fetch(); |
|
| 580 | + $cursor->closeCursor(); |
|
| 581 | + |
|
| 582 | + if ($data === false) { |
|
| 583 | + throw new ShareNotFound(); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + try { |
|
| 587 | + $share = $this->createShareObject($data); |
|
| 588 | + } catch (InvalidShare $e) { |
|
| 589 | + throw new ShareNotFound(); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + return $share; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Get shares for a given path |
|
| 597 | + * |
|
| 598 | + * @param \OCP\Files\Node $path |
|
| 599 | + * @return IShare[] |
|
| 600 | + */ |
|
| 601 | + public function getSharesByPath(Node $path) { |
|
| 602 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 603 | + |
|
| 604 | + $cursor = $qb->select('*') |
|
| 605 | + ->from('share') |
|
| 606 | + ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 607 | + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 608 | + ->execute(); |
|
| 609 | + |
|
| 610 | + $shares = []; |
|
| 611 | + while($data = $cursor->fetch()) { |
|
| 612 | + $shares[] = $this->createShareObject($data); |
|
| 613 | + } |
|
| 614 | + $cursor->closeCursor(); |
|
| 615 | + |
|
| 616 | + return $shares; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * @inheritdoc |
|
| 621 | + */ |
|
| 622 | + public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 623 | + /** @var IShare[] $shares */ |
|
| 624 | + $shares = []; |
|
| 625 | + |
|
| 626 | + //Get shares directly with this user |
|
| 627 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 628 | + $qb->select('*') |
|
| 629 | + ->from('share'); |
|
| 630 | + |
|
| 631 | + // Order by id |
|
| 632 | + $qb->orderBy('id'); |
|
| 633 | + |
|
| 634 | + // Set limit and offset |
|
| 635 | + if ($limit !== -1) { |
|
| 636 | + $qb->setMaxResults($limit); |
|
| 637 | + } |
|
| 638 | + $qb->setFirstResult($offset); |
|
| 639 | + |
|
| 640 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))); |
|
| 641 | + $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))); |
|
| 642 | + |
|
| 643 | + // Filter by node if provided |
|
| 644 | + if ($node !== null) { |
|
| 645 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + $cursor = $qb->execute(); |
|
| 649 | + |
|
| 650 | + while($data = $cursor->fetch()) { |
|
| 651 | + $shares[] = $this->createShareObject($data); |
|
| 652 | + } |
|
| 653 | + $cursor->closeCursor(); |
|
| 654 | + |
|
| 655 | + |
|
| 656 | + return $shares; |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Get a share by token |
|
| 661 | + * |
|
| 662 | + * @param string $token |
|
| 663 | + * @return IShare |
|
| 664 | + * @throws ShareNotFound |
|
| 665 | + */ |
|
| 666 | + public function getShareByToken($token) { |
|
| 667 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 668 | + |
|
| 669 | + $cursor = $qb->select('*') |
|
| 670 | + ->from('share') |
|
| 671 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 672 | + ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 673 | + ->execute(); |
|
| 674 | + |
|
| 675 | + $data = $cursor->fetch(); |
|
| 676 | + |
|
| 677 | + if ($data === false) { |
|
| 678 | + throw new ShareNotFound('Share not found', $this->l->t('Could not find share')); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + try { |
|
| 682 | + $share = $this->createShareObject($data); |
|
| 683 | + } catch (InvalidShare $e) { |
|
| 684 | + throw new ShareNotFound('Share not found', $this->l->t('Could not find share')); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + return $share; |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * remove share from table |
|
| 692 | + * |
|
| 693 | + * @param string $shareId |
|
| 694 | + */ |
|
| 695 | + protected function removeShareFromTable($shareId) { |
|
| 696 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 697 | + $qb->delete('share') |
|
| 698 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId))); |
|
| 699 | + $qb->execute(); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Create a share object from an database row |
|
| 704 | + * |
|
| 705 | + * @param array $data |
|
| 706 | + * @return IShare |
|
| 707 | + * @throws InvalidShare |
|
| 708 | + * @throws ShareNotFound |
|
| 709 | + */ |
|
| 710 | + protected function createShareObject($data) { |
|
| 711 | + |
|
| 712 | + $share = new Share($this->rootFolder, $this->userManager); |
|
| 713 | + $share->setId((int)$data['id']) |
|
| 714 | + ->setShareType((int)$data['share_type']) |
|
| 715 | + ->setPermissions((int)$data['permissions']) |
|
| 716 | + ->setTarget($data['file_target']) |
|
| 717 | + ->setMailSend((bool)$data['mail_send']) |
|
| 718 | + ->setToken($data['token']); |
|
| 719 | + |
|
| 720 | + $shareTime = new \DateTime(); |
|
| 721 | + $shareTime->setTimestamp((int)$data['stime']); |
|
| 722 | + $share->setShareTime($shareTime); |
|
| 723 | + $share->setSharedWith($data['share_with']); |
|
| 724 | + $share->setPassword($data['password']); |
|
| 725 | + |
|
| 726 | + if ($data['uid_initiator'] !== null) { |
|
| 727 | + $share->setShareOwner($data['uid_owner']); |
|
| 728 | + $share->setSharedBy($data['uid_initiator']); |
|
| 729 | + } else { |
|
| 730 | + //OLD SHARE |
|
| 731 | + $share->setSharedBy($data['uid_owner']); |
|
| 732 | + $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']); |
|
| 733 | + |
|
| 734 | + $owner = $path->getOwner(); |
|
| 735 | + $share->setShareOwner($owner->getUID()); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + if ($data['expiration'] !== null) { |
|
| 739 | + $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 740 | + if ($expiration !== false) { |
|
| 741 | + $share->setExpirationDate($expiration); |
|
| 742 | + } |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + $share->setNodeId((int)$data['file_source']); |
|
| 746 | + $share->setNodeType($data['item_type']); |
|
| 747 | + |
|
| 748 | + $share->setProviderId($this->identifier()); |
|
| 749 | + |
|
| 750 | + return $share; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Get the node with file $id for $user |
|
| 755 | + * |
|
| 756 | + * @param string $userId |
|
| 757 | + * @param int $id |
|
| 758 | + * @return \OCP\Files\File|\OCP\Files\Folder |
|
| 759 | + * @throws InvalidShare |
|
| 760 | + */ |
|
| 761 | + private function getNode($userId, $id) { |
|
| 762 | + try { |
|
| 763 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
|
| 764 | + } catch (NotFoundException $e) { |
|
| 765 | + throw new InvalidShare(); |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + $nodes = $userFolder->getById($id); |
|
| 769 | + |
|
| 770 | + if (empty($nodes)) { |
|
| 771 | + throw new InvalidShare(); |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + return $nodes[0]; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + /** |
|
| 778 | + * A user is deleted from the system |
|
| 779 | + * So clean up the relevant shares. |
|
| 780 | + * |
|
| 781 | + * @param string $uid |
|
| 782 | + * @param int $shareType |
|
| 783 | + */ |
|
| 784 | + public function userDeleted($uid, $shareType) { |
|
| 785 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 786 | + |
|
| 787 | + $qb->delete('share') |
|
| 788 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 789 | + ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))) |
|
| 790 | + ->execute(); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + /** |
|
| 794 | + * This provider does not support group shares |
|
| 795 | + * |
|
| 796 | + * @param string $gid |
|
| 797 | + */ |
|
| 798 | + public function groupDeleted($gid) { |
|
| 799 | + return; |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + /** |
|
| 803 | + * This provider does not support group shares |
|
| 804 | + * |
|
| 805 | + * @param string $uid |
|
| 806 | + * @param string $gid |
|
| 807 | + */ |
|
| 808 | + public function userDeletedFromGroup($uid, $gid) { |
|
| 809 | + return; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + /** |
|
| 813 | + * get database row of a give share |
|
| 814 | + * |
|
| 815 | + * @param $id |
|
| 816 | + * @return array |
|
| 817 | + * @throws ShareNotFound |
|
| 818 | + */ |
|
| 819 | + protected function getRawShare($id) { |
|
| 820 | + |
|
| 821 | + // Now fetch the inserted share and create a complete share object |
|
| 822 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 823 | + $qb->select('*') |
|
| 824 | + ->from('share') |
|
| 825 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 826 | + |
|
| 827 | + $cursor = $qb->execute(); |
|
| 828 | + $data = $cursor->fetch(); |
|
| 829 | + $cursor->closeCursor(); |
|
| 830 | + |
|
| 831 | + if ($data === false) { |
|
| 832 | + throw new ShareNotFound; |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + return $data; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 839 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 840 | + $qb->select('*') |
|
| 841 | + ->from('share', 's') |
|
| 842 | + ->andWhere($qb->expr()->orX( |
|
| 843 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 844 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 845 | + )) |
|
| 846 | + ->andWhere( |
|
| 847 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)) |
|
| 848 | + ); |
|
| 849 | + |
|
| 850 | + /** |
|
| 851 | + * Reshares for this user are shares where they are the owner. |
|
| 852 | + */ |
|
| 853 | + if ($reshares === false) { |
|
| 854 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 855 | + } else { |
|
| 856 | + $qb->andWhere( |
|
| 857 | + $qb->expr()->orX( |
|
| 858 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 859 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 860 | + ) |
|
| 861 | + ); |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 865 | + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 866 | + |
|
| 867 | + $qb->orderBy('id'); |
|
| 868 | + |
|
| 869 | + $cursor = $qb->execute(); |
|
| 870 | + $shares = []; |
|
| 871 | + while ($data = $cursor->fetch()) { |
|
| 872 | + $shares[$data['fileid']][] = $this->createShareObject($data); |
|
| 873 | + } |
|
| 874 | + $cursor->closeCursor(); |
|
| 875 | + |
|
| 876 | + return $shares; |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * @inheritdoc |
|
| 881 | + */ |
|
| 882 | + public function getAccessList($nodes, $currentAccess) { |
|
| 883 | + $ids = []; |
|
| 884 | + foreach ($nodes as $node) { |
|
| 885 | + $ids[] = $node->getId(); |
|
| 886 | + } |
|
| 887 | + |
|
| 888 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 889 | + $qb->select('share_with') |
|
| 890 | + ->from('share') |
|
| 891 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) |
|
| 892 | + ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 893 | + ->andWhere($qb->expr()->orX( |
|
| 894 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 895 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 896 | + )) |
|
| 897 | + ->setMaxResults(1); |
|
| 898 | + $cursor = $qb->execute(); |
|
| 899 | + |
|
| 900 | + $mail = $cursor->fetch() !== false; |
|
| 901 | + $cursor->closeCursor(); |
|
| 902 | + |
|
| 903 | + return ['public' => $mail]; |
|
| 904 | + } |
|
| 905 | 905 | |
| 906 | 906 | } |