@@ -26,37 +26,37 @@ |
||
26 | 26 | namespace OC\Template; |
27 | 27 | |
28 | 28 | class TemplateFileLocator { |
29 | - protected $dirs; |
|
30 | - private $path; |
|
29 | + protected $dirs; |
|
30 | + private $path; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @param string[] $dirs |
|
34 | - */ |
|
35 | - public function __construct($dirs) { |
|
36 | - $this->dirs = $dirs; |
|
37 | - } |
|
32 | + /** |
|
33 | + * @param string[] $dirs |
|
34 | + */ |
|
35 | + public function __construct($dirs) { |
|
36 | + $this->dirs = $dirs; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $template |
|
41 | - * @return string |
|
42 | - * @throws \Exception |
|
43 | - */ |
|
44 | - public function find($template) { |
|
45 | - if ($template === '') { |
|
46 | - throw new \InvalidArgumentException('Empty template name'); |
|
47 | - } |
|
39 | + /** |
|
40 | + * @param string $template |
|
41 | + * @return string |
|
42 | + * @throws \Exception |
|
43 | + */ |
|
44 | + public function find($template) { |
|
45 | + if ($template === '') { |
|
46 | + throw new \InvalidArgumentException('Empty template name'); |
|
47 | + } |
|
48 | 48 | |
49 | - foreach ($this->dirs as $dir) { |
|
50 | - $file = $dir.$template.'.php'; |
|
51 | - if (is_file($file)) { |
|
52 | - $this->path = $dir; |
|
53 | - return $file; |
|
54 | - } |
|
55 | - } |
|
56 | - throw new \Exception('template file not found: template:'.$template); |
|
57 | - } |
|
49 | + foreach ($this->dirs as $dir) { |
|
50 | + $file = $dir.$template.'.php'; |
|
51 | + if (is_file($file)) { |
|
52 | + $this->path = $dir; |
|
53 | + return $file; |
|
54 | + } |
|
55 | + } |
|
56 | + throw new \Exception('template file not found: template:'.$template); |
|
57 | + } |
|
58 | 58 | |
59 | - public function getPath() { |
|
60 | - return $this->path; |
|
61 | - } |
|
59 | + public function getPath() { |
|
60 | + return $this->path; |
|
61 | + } |
|
62 | 62 | } |
@@ -65,7 +65,7 @@ |
||
65 | 65 | |
66 | 66 | if ($app_path === false && $app_url === false) { |
67 | 67 | $this->logger->error('Could not find resource {resource} to load', [ |
68 | - 'resource' => $app . '/' . $style . '.css', |
|
68 | + 'resource' => $app.'/'.$style.'.css', |
|
69 | 69 | 'app' => 'cssresourceloader', |
70 | 70 | ]); |
71 | 71 | return; |
@@ -34,74 +34,74 @@ |
||
34 | 34 | use Psr\Log\LoggerInterface; |
35 | 35 | |
36 | 36 | class CSSResourceLocator extends ResourceLocator { |
37 | - public function __construct(LoggerInterface $logger) { |
|
38 | - parent::__construct($logger); |
|
39 | - } |
|
37 | + public function __construct(LoggerInterface $logger) { |
|
38 | + parent::__construct($logger); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param string $style |
|
43 | - */ |
|
44 | - public function doFind($style) { |
|
45 | - $app = substr($style, 0, strpos($style, '/')); |
|
46 | - if ($this->appendIfExist($this->serverroot, $style.'.css') |
|
47 | - || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') |
|
48 | - ) { |
|
49 | - return; |
|
50 | - } |
|
51 | - $style = substr($style, strpos($style, '/') + 1); |
|
52 | - $app_path = \OC_App::getAppPath($app); |
|
53 | - $app_url = \OC_App::getAppWebPath($app); |
|
41 | + /** |
|
42 | + * @param string $style |
|
43 | + */ |
|
44 | + public function doFind($style) { |
|
45 | + $app = substr($style, 0, strpos($style, '/')); |
|
46 | + if ($this->appendIfExist($this->serverroot, $style.'.css') |
|
47 | + || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') |
|
48 | + ) { |
|
49 | + return; |
|
50 | + } |
|
51 | + $style = substr($style, strpos($style, '/') + 1); |
|
52 | + $app_path = \OC_App::getAppPath($app); |
|
53 | + $app_url = \OC_App::getAppWebPath($app); |
|
54 | 54 | |
55 | - if ($app_path === false && $app_url === false) { |
|
56 | - $this->logger->error('Could not find resource {resource} to load', [ |
|
57 | - 'resource' => $app . '/' . $style . '.css', |
|
58 | - 'app' => 'cssresourceloader', |
|
59 | - ]); |
|
60 | - return; |
|
61 | - } |
|
55 | + if ($app_path === false && $app_url === false) { |
|
56 | + $this->logger->error('Could not find resource {resource} to load', [ |
|
57 | + 'resource' => $app . '/' . $style . '.css', |
|
58 | + 'app' => 'cssresourceloader', |
|
59 | + ]); |
|
60 | + return; |
|
61 | + } |
|
62 | 62 | |
63 | - // Account for the possibility of having symlinks in app path. Doing |
|
64 | - // this here instead of above as an empty argument to realpath gets |
|
65 | - // turned into cwd. |
|
66 | - $app_path = realpath($app_path); |
|
63 | + // Account for the possibility of having symlinks in app path. Doing |
|
64 | + // this here instead of above as an empty argument to realpath gets |
|
65 | + // turned into cwd. |
|
66 | + $app_path = realpath($app_path); |
|
67 | 67 | |
68 | - $this->append($app_path, $style.'.css', $app_url); |
|
69 | - } |
|
68 | + $this->append($app_path, $style.'.css', $app_url); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @param string $style |
|
73 | - */ |
|
74 | - public function doFindTheme($style) { |
|
75 | - $theme_dir = 'themes/'.$this->theme.'/'; |
|
76 | - $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') |
|
77 | - || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') |
|
78 | - || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); |
|
79 | - } |
|
71 | + /** |
|
72 | + * @param string $style |
|
73 | + */ |
|
74 | + public function doFindTheme($style) { |
|
75 | + $theme_dir = 'themes/'.$this->theme.'/'; |
|
76 | + $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') |
|
77 | + || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') |
|
78 | + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); |
|
79 | + } |
|
80 | 80 | |
81 | - public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { |
|
82 | - if (!$scss) { |
|
83 | - parent::append($root, $file, $webRoot, $throw); |
|
84 | - } else { |
|
85 | - if (!$webRoot) { |
|
86 | - $webRoot = $this->findWebRoot($root); |
|
81 | + public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { |
|
82 | + if (!$scss) { |
|
83 | + parent::append($root, $file, $webRoot, $throw); |
|
84 | + } else { |
|
85 | + if (!$webRoot) { |
|
86 | + $webRoot = $this->findWebRoot($root); |
|
87 | 87 | |
88 | - if ($webRoot === null) { |
|
89 | - $webRoot = ''; |
|
90 | - $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
91 | - 'app' => 'lib', |
|
92 | - 'root' => $root, |
|
93 | - 'file' => $file, |
|
94 | - 'webRoot' => $webRoot, |
|
95 | - 'throw' => $throw ? 'true' : 'false' |
|
96 | - ]); |
|
88 | + if ($webRoot === null) { |
|
89 | + $webRoot = ''; |
|
90 | + $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
91 | + 'app' => 'lib', |
|
92 | + 'root' => $root, |
|
93 | + 'file' => $file, |
|
94 | + 'webRoot' => $webRoot, |
|
95 | + 'throw' => $throw ? 'true' : 'false' |
|
96 | + ]); |
|
97 | 97 | |
98 | - if ($throw && $root === '/') { |
|
99 | - throw new ResourceNotFoundException($file, $webRoot); |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
98 | + if ($throw && $root === '/') { |
|
99 | + throw new ResourceNotFoundException($file, $webRoot); |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file]; |
|
105 | - } |
|
106 | - } |
|
104 | + $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file]; |
|
105 | + } |
|
106 | + } |
|
107 | 107 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | $this->doFind($resource); |
76 | 76 | } catch (ResourceNotFoundException $e) { |
77 | 77 | $resourceApp = substr($resource, 0, strpos($resource, '/')); |
78 | - $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
78 | + $this->logger->debug('Could not find resource file "'.$e->getResourcePath().'"', ['app' => $resourceApp]); |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 | if (!empty($this->theme)) { |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | $this->doFindTheme($resource); |
85 | 85 | } catch (ResourceNotFoundException $e) { |
86 | 86 | $resourceApp = substr($resource, 0, strpos($resource, '/')); |
87 | - $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
87 | + $this->logger->debug('Could not find resource file in theme "'.$e->getResourcePath().'"', ['app' => $resourceApp]); |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | } |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | } |
159 | 159 | $this->resources[] = [$root, $webRoot, $file]; |
160 | 160 | |
161 | - if ($throw && !is_file($root . '/' . $file)) { |
|
161 | + if ($throw && !is_file($root.'/'.$file)) { |
|
162 | 162 | throw new ResourceNotFoundException($file, $webRoot); |
163 | 163 | } |
164 | 164 | } |
@@ -32,165 +32,165 @@ |
||
32 | 32 | use Psr\Log\LoggerInterface; |
33 | 33 | |
34 | 34 | abstract class ResourceLocator { |
35 | - protected $theme; |
|
36 | - |
|
37 | - protected $mapping; |
|
38 | - protected $serverroot; |
|
39 | - protected $webroot; |
|
40 | - |
|
41 | - protected $resources = []; |
|
42 | - |
|
43 | - protected LoggerInterface $logger; |
|
44 | - |
|
45 | - public function __construct(LoggerInterface $logger) { |
|
46 | - $this->logger = $logger; |
|
47 | - $this->mapping = [ |
|
48 | - \OC::$SERVERROOT => \OC::$WEBROOT |
|
49 | - ]; |
|
50 | - $this->serverroot = \OC::$SERVERROOT; |
|
51 | - $this->webroot = \OC::$WEBROOT; |
|
52 | - $this->theme = \OC_Util::getTheme(); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $resource |
|
57 | - */ |
|
58 | - abstract public function doFind($resource); |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $resource |
|
62 | - */ |
|
63 | - abstract public function doFindTheme($resource); |
|
64 | - |
|
65 | - /** |
|
66 | - * Finds the resources and adds them to the list |
|
67 | - * |
|
68 | - * @param array $resources |
|
69 | - */ |
|
70 | - public function find($resources) { |
|
71 | - foreach ($resources as $resource) { |
|
72 | - try { |
|
73 | - $this->doFind($resource); |
|
74 | - } catch (ResourceNotFoundException $e) { |
|
75 | - $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
76 | - $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
77 | - } |
|
78 | - } |
|
79 | - if (!empty($this->theme)) { |
|
80 | - foreach ($resources as $resource) { |
|
81 | - try { |
|
82 | - $this->doFindTheme($resource); |
|
83 | - } catch (ResourceNotFoundException $e) { |
|
84 | - $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
85 | - $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * append the $file resource if exist at $root |
|
93 | - * |
|
94 | - * @param string $root path to check |
|
95 | - * @param string $file the filename |
|
96 | - * @param string|null $webRoot base for path, default map $root to $webRoot |
|
97 | - * @return bool True if the resource was found, false otherwise |
|
98 | - */ |
|
99 | - protected function appendIfExist($root, $file, $webRoot = null) { |
|
100 | - if ($root !== false && is_file($root.'/'.$file)) { |
|
101 | - $this->append($root, $file, $webRoot, false); |
|
102 | - return true; |
|
103 | - } |
|
104 | - return false; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Attempt to find the webRoot |
|
109 | - * |
|
110 | - * traverse the potential web roots upwards in the path |
|
111 | - * |
|
112 | - * example: |
|
113 | - * - root: /srv/www/apps/myapp |
|
114 | - * - available mappings: ['/srv/www'] |
|
115 | - * |
|
116 | - * First we check if a mapping for /srv/www/apps/myapp is available, |
|
117 | - * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a |
|
118 | - * valid web root |
|
119 | - * |
|
120 | - * @param string $root |
|
121 | - * @return string|null The web root or null on failure |
|
122 | - */ |
|
123 | - protected function findWebRoot($root) { |
|
124 | - $webRoot = null; |
|
125 | - $tmpRoot = $root; |
|
126 | - |
|
127 | - while ($webRoot === null) { |
|
128 | - if (isset($this->mapping[$tmpRoot])) { |
|
129 | - $webRoot = $this->mapping[$tmpRoot]; |
|
130 | - break; |
|
131 | - } |
|
132 | - |
|
133 | - if ($tmpRoot === '/') { |
|
134 | - break; |
|
135 | - } |
|
136 | - |
|
137 | - $tmpRoot = dirname($tmpRoot); |
|
138 | - } |
|
139 | - |
|
140 | - if ($webRoot === null) { |
|
141 | - $realpath = realpath($root); |
|
142 | - |
|
143 | - if ($realpath && ($realpath !== $root)) { |
|
144 | - return $this->findWebRoot($realpath); |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - return $webRoot; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * append the $file resource at $root |
|
153 | - * |
|
154 | - * @param string $root path to check |
|
155 | - * @param string $file the filename |
|
156 | - * @param string|null $webRoot base for path, default map $root to $webRoot |
|
157 | - * @param bool $throw Throw an exception, when the route does not exist |
|
158 | - * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing |
|
159 | - */ |
|
160 | - protected function append($root, $file, $webRoot = null, $throw = true) { |
|
161 | - if (!is_string($root)) { |
|
162 | - if ($throw) { |
|
163 | - throw new ResourceNotFoundException($file, $webRoot); |
|
164 | - } |
|
165 | - return; |
|
166 | - } |
|
167 | - |
|
168 | - if (!$webRoot) { |
|
169 | - $webRoot = $this->findWebRoot($root); |
|
170 | - |
|
171 | - if ($webRoot === null) { |
|
172 | - $webRoot = ''; |
|
173 | - $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
174 | - 'app' => 'lib', |
|
175 | - 'root' => $root, |
|
176 | - 'file' => $file, |
|
177 | - 'webRoot' => $webRoot, |
|
178 | - 'throw' => $throw ? 'true' : 'false' |
|
179 | - ]); |
|
180 | - } |
|
181 | - } |
|
182 | - $this->resources[] = [$root, $webRoot, $file]; |
|
183 | - |
|
184 | - if ($throw && !is_file($root . '/' . $file)) { |
|
185 | - throw new ResourceNotFoundException($file, $webRoot); |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Returns the list of all resources that should be loaded |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - public function getResources() { |
|
194 | - return $this->resources; |
|
195 | - } |
|
35 | + protected $theme; |
|
36 | + |
|
37 | + protected $mapping; |
|
38 | + protected $serverroot; |
|
39 | + protected $webroot; |
|
40 | + |
|
41 | + protected $resources = []; |
|
42 | + |
|
43 | + protected LoggerInterface $logger; |
|
44 | + |
|
45 | + public function __construct(LoggerInterface $logger) { |
|
46 | + $this->logger = $logger; |
|
47 | + $this->mapping = [ |
|
48 | + \OC::$SERVERROOT => \OC::$WEBROOT |
|
49 | + ]; |
|
50 | + $this->serverroot = \OC::$SERVERROOT; |
|
51 | + $this->webroot = \OC::$WEBROOT; |
|
52 | + $this->theme = \OC_Util::getTheme(); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $resource |
|
57 | + */ |
|
58 | + abstract public function doFind($resource); |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $resource |
|
62 | + */ |
|
63 | + abstract public function doFindTheme($resource); |
|
64 | + |
|
65 | + /** |
|
66 | + * Finds the resources and adds them to the list |
|
67 | + * |
|
68 | + * @param array $resources |
|
69 | + */ |
|
70 | + public function find($resources) { |
|
71 | + foreach ($resources as $resource) { |
|
72 | + try { |
|
73 | + $this->doFind($resource); |
|
74 | + } catch (ResourceNotFoundException $e) { |
|
75 | + $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
76 | + $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
77 | + } |
|
78 | + } |
|
79 | + if (!empty($this->theme)) { |
|
80 | + foreach ($resources as $resource) { |
|
81 | + try { |
|
82 | + $this->doFindTheme($resource); |
|
83 | + } catch (ResourceNotFoundException $e) { |
|
84 | + $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
85 | + $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * append the $file resource if exist at $root |
|
93 | + * |
|
94 | + * @param string $root path to check |
|
95 | + * @param string $file the filename |
|
96 | + * @param string|null $webRoot base for path, default map $root to $webRoot |
|
97 | + * @return bool True if the resource was found, false otherwise |
|
98 | + */ |
|
99 | + protected function appendIfExist($root, $file, $webRoot = null) { |
|
100 | + if ($root !== false && is_file($root.'/'.$file)) { |
|
101 | + $this->append($root, $file, $webRoot, false); |
|
102 | + return true; |
|
103 | + } |
|
104 | + return false; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Attempt to find the webRoot |
|
109 | + * |
|
110 | + * traverse the potential web roots upwards in the path |
|
111 | + * |
|
112 | + * example: |
|
113 | + * - root: /srv/www/apps/myapp |
|
114 | + * - available mappings: ['/srv/www'] |
|
115 | + * |
|
116 | + * First we check if a mapping for /srv/www/apps/myapp is available, |
|
117 | + * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a |
|
118 | + * valid web root |
|
119 | + * |
|
120 | + * @param string $root |
|
121 | + * @return string|null The web root or null on failure |
|
122 | + */ |
|
123 | + protected function findWebRoot($root) { |
|
124 | + $webRoot = null; |
|
125 | + $tmpRoot = $root; |
|
126 | + |
|
127 | + while ($webRoot === null) { |
|
128 | + if (isset($this->mapping[$tmpRoot])) { |
|
129 | + $webRoot = $this->mapping[$tmpRoot]; |
|
130 | + break; |
|
131 | + } |
|
132 | + |
|
133 | + if ($tmpRoot === '/') { |
|
134 | + break; |
|
135 | + } |
|
136 | + |
|
137 | + $tmpRoot = dirname($tmpRoot); |
|
138 | + } |
|
139 | + |
|
140 | + if ($webRoot === null) { |
|
141 | + $realpath = realpath($root); |
|
142 | + |
|
143 | + if ($realpath && ($realpath !== $root)) { |
|
144 | + return $this->findWebRoot($realpath); |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + return $webRoot; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * append the $file resource at $root |
|
153 | + * |
|
154 | + * @param string $root path to check |
|
155 | + * @param string $file the filename |
|
156 | + * @param string|null $webRoot base for path, default map $root to $webRoot |
|
157 | + * @param bool $throw Throw an exception, when the route does not exist |
|
158 | + * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing |
|
159 | + */ |
|
160 | + protected function append($root, $file, $webRoot = null, $throw = true) { |
|
161 | + if (!is_string($root)) { |
|
162 | + if ($throw) { |
|
163 | + throw new ResourceNotFoundException($file, $webRoot); |
|
164 | + } |
|
165 | + return; |
|
166 | + } |
|
167 | + |
|
168 | + if (!$webRoot) { |
|
169 | + $webRoot = $this->findWebRoot($root); |
|
170 | + |
|
171 | + if ($webRoot === null) { |
|
172 | + $webRoot = ''; |
|
173 | + $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
174 | + 'app' => 'lib', |
|
175 | + 'root' => $root, |
|
176 | + 'file' => $file, |
|
177 | + 'webRoot' => $webRoot, |
|
178 | + 'throw' => $throw ? 'true' : 'false' |
|
179 | + ]); |
|
180 | + } |
|
181 | + } |
|
182 | + $this->resources[] = [$root, $webRoot, $file]; |
|
183 | + |
|
184 | + if ($throw && !is_file($root . '/' . $file)) { |
|
185 | + throw new ResourceNotFoundException($file, $webRoot); |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Returns the list of all resources that should be loaded |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + public function getResources() { |
|
194 | + return $this->resources; |
|
195 | + } |
|
196 | 196 | } |
@@ -117,7 +117,7 @@ |
||
117 | 117 | if (array_key_exists($key, $this->vars)) { |
118 | 118 | $this->vars[$key][] = $value; |
119 | 119 | } else { |
120 | - $this->vars[$key] = [ $value ]; |
|
120 | + $this->vars[$key] = [$value]; |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 |
@@ -33,159 +33,159 @@ |
||
33 | 33 | use Throwable; |
34 | 34 | |
35 | 35 | class Base { |
36 | - private $template; // The template |
|
37 | - private $vars; // Vars |
|
36 | + private $template; // The template |
|
37 | + private $vars; // Vars |
|
38 | 38 | |
39 | - /** @var \OCP\IL10N */ |
|
40 | - private $l10n; |
|
39 | + /** @var \OCP\IL10N */ |
|
40 | + private $l10n; |
|
41 | 41 | |
42 | - /** @var Defaults */ |
|
43 | - private $theme; |
|
42 | + /** @var Defaults */ |
|
43 | + private $theme; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $template |
|
47 | - * @param string $requestToken |
|
48 | - * @param \OCP\IL10N $l10n |
|
49 | - * @param Defaults $theme |
|
50 | - */ |
|
51 | - public function __construct($template, $requestToken, $l10n, $theme) { |
|
52 | - $this->vars = []; |
|
53 | - $this->vars['requesttoken'] = $requestToken; |
|
54 | - $this->l10n = $l10n; |
|
55 | - $this->template = $template; |
|
56 | - $this->theme = $theme; |
|
57 | - } |
|
45 | + /** |
|
46 | + * @param string $template |
|
47 | + * @param string $requestToken |
|
48 | + * @param \OCP\IL10N $l10n |
|
49 | + * @param Defaults $theme |
|
50 | + */ |
|
51 | + public function __construct($template, $requestToken, $l10n, $theme) { |
|
52 | + $this->vars = []; |
|
53 | + $this->vars['requesttoken'] = $requestToken; |
|
54 | + $this->l10n = $l10n; |
|
55 | + $this->template = $template; |
|
56 | + $this->theme = $theme; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @param string $serverRoot |
|
61 | - * @param string|false $app_dir |
|
62 | - * @param string $theme |
|
63 | - * @param string $app |
|
64 | - * @return string[] |
|
65 | - */ |
|
66 | - protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
67 | - // Check if the app is in the app folder or in the root |
|
68 | - if ($app_dir !== false && file_exists($app_dir.'/templates/')) { |
|
69 | - return [ |
|
70 | - $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
71 | - $app_dir.'/templates/', |
|
72 | - ]; |
|
73 | - } |
|
74 | - return [ |
|
75 | - $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
76 | - $serverRoot.'/'.$app.'/templates/', |
|
77 | - ]; |
|
78 | - } |
|
59 | + /** |
|
60 | + * @param string $serverRoot |
|
61 | + * @param string|false $app_dir |
|
62 | + * @param string $theme |
|
63 | + * @param string $app |
|
64 | + * @return string[] |
|
65 | + */ |
|
66 | + protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
67 | + // Check if the app is in the app folder or in the root |
|
68 | + if ($app_dir !== false && file_exists($app_dir.'/templates/')) { |
|
69 | + return [ |
|
70 | + $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
71 | + $app_dir.'/templates/', |
|
72 | + ]; |
|
73 | + } |
|
74 | + return [ |
|
75 | + $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
76 | + $serverRoot.'/'.$app.'/templates/', |
|
77 | + ]; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @param string $serverRoot |
|
82 | - * @param string $theme |
|
83 | - * @return string[] |
|
84 | - */ |
|
85 | - protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
86 | - return [ |
|
87 | - $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
88 | - $serverRoot.'/core/templates/', |
|
89 | - ]; |
|
90 | - } |
|
80 | + /** |
|
81 | + * @param string $serverRoot |
|
82 | + * @param string $theme |
|
83 | + * @return string[] |
|
84 | + */ |
|
85 | + protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
86 | + return [ |
|
87 | + $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
88 | + $serverRoot.'/core/templates/', |
|
89 | + ]; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Assign variables |
|
94 | - * @param string $key key |
|
95 | - * @param float|array|bool|integer|string|Throwable $value value |
|
96 | - * @return bool |
|
97 | - * |
|
98 | - * This function assigns a variable. It can be accessed via $_[$key] in |
|
99 | - * the template. |
|
100 | - * |
|
101 | - * If the key existed before, it will be overwritten |
|
102 | - */ |
|
103 | - public function assign($key, $value) { |
|
104 | - $this->vars[$key] = $value; |
|
105 | - return true; |
|
106 | - } |
|
92 | + /** |
|
93 | + * Assign variables |
|
94 | + * @param string $key key |
|
95 | + * @param float|array|bool|integer|string|Throwable $value value |
|
96 | + * @return bool |
|
97 | + * |
|
98 | + * This function assigns a variable. It can be accessed via $_[$key] in |
|
99 | + * the template. |
|
100 | + * |
|
101 | + * If the key existed before, it will be overwritten |
|
102 | + */ |
|
103 | + public function assign($key, $value) { |
|
104 | + $this->vars[$key] = $value; |
|
105 | + return true; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Appends a variable |
|
110 | - * @param string $key key |
|
111 | - * @param mixed $value value |
|
112 | - * |
|
113 | - * This function assigns a variable in an array context. If the key already |
|
114 | - * exists, the value will be appended. It can be accessed via |
|
115 | - * $_[$key][$position] in the template. |
|
116 | - */ |
|
117 | - public function append($key, $value) { |
|
118 | - if (array_key_exists($key, $this->vars)) { |
|
119 | - $this->vars[$key][] = $value; |
|
120 | - } else { |
|
121 | - $this->vars[$key] = [ $value ]; |
|
122 | - } |
|
123 | - } |
|
108 | + /** |
|
109 | + * Appends a variable |
|
110 | + * @param string $key key |
|
111 | + * @param mixed $value value |
|
112 | + * |
|
113 | + * This function assigns a variable in an array context. If the key already |
|
114 | + * exists, the value will be appended. It can be accessed via |
|
115 | + * $_[$key][$position] in the template. |
|
116 | + */ |
|
117 | + public function append($key, $value) { |
|
118 | + if (array_key_exists($key, $this->vars)) { |
|
119 | + $this->vars[$key][] = $value; |
|
120 | + } else { |
|
121 | + $this->vars[$key] = [ $value ]; |
|
122 | + } |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Prints the proceeded template |
|
127 | - * @return bool |
|
128 | - * |
|
129 | - * This function proceeds the template and prints its output. |
|
130 | - */ |
|
131 | - public function printPage() { |
|
132 | - $data = $this->fetchPage(); |
|
133 | - if ($data === false) { |
|
134 | - return false; |
|
135 | - } else { |
|
136 | - print $data; |
|
137 | - return true; |
|
138 | - } |
|
139 | - } |
|
125 | + /** |
|
126 | + * Prints the proceeded template |
|
127 | + * @return bool |
|
128 | + * |
|
129 | + * This function proceeds the template and prints its output. |
|
130 | + */ |
|
131 | + public function printPage() { |
|
132 | + $data = $this->fetchPage(); |
|
133 | + if ($data === false) { |
|
134 | + return false; |
|
135 | + } else { |
|
136 | + print $data; |
|
137 | + return true; |
|
138 | + } |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * Process the template |
|
143 | - * |
|
144 | - * @param array|null $additionalParams |
|
145 | - * @return string This function processes the template. |
|
146 | - * |
|
147 | - * This function processes the template. |
|
148 | - */ |
|
149 | - public function fetchPage($additionalParams = null) { |
|
150 | - return $this->load($this->template, $additionalParams); |
|
151 | - } |
|
141 | + /** |
|
142 | + * Process the template |
|
143 | + * |
|
144 | + * @param array|null $additionalParams |
|
145 | + * @return string This function processes the template. |
|
146 | + * |
|
147 | + * This function processes the template. |
|
148 | + */ |
|
149 | + public function fetchPage($additionalParams = null) { |
|
150 | + return $this->load($this->template, $additionalParams); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * doing the actual work |
|
155 | - * |
|
156 | - * @param string $file |
|
157 | - * @param array|null $additionalParams |
|
158 | - * @return string content |
|
159 | - * |
|
160 | - * Includes the template file, fetches its output |
|
161 | - */ |
|
162 | - protected function load($file, $additionalParams = null) { |
|
163 | - // Register the variables |
|
164 | - $_ = $this->vars; |
|
165 | - $l = $this->l10n; |
|
166 | - $theme = $this->theme; |
|
153 | + /** |
|
154 | + * doing the actual work |
|
155 | + * |
|
156 | + * @param string $file |
|
157 | + * @param array|null $additionalParams |
|
158 | + * @return string content |
|
159 | + * |
|
160 | + * Includes the template file, fetches its output |
|
161 | + */ |
|
162 | + protected function load($file, $additionalParams = null) { |
|
163 | + // Register the variables |
|
164 | + $_ = $this->vars; |
|
165 | + $l = $this->l10n; |
|
166 | + $theme = $this->theme; |
|
167 | 167 | |
168 | - if (!is_null($additionalParams)) { |
|
169 | - $_ = array_merge($additionalParams, $this->vars); |
|
170 | - foreach ($_ as $var => $value) { |
|
171 | - if (!isset(${$var})) { |
|
172 | - ${$var} = $value; |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
168 | + if (!is_null($additionalParams)) { |
|
169 | + $_ = array_merge($additionalParams, $this->vars); |
|
170 | + foreach ($_ as $var => $value) { |
|
171 | + if (!isset(${$var})) { |
|
172 | + ${$var} = $value; |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - // Include |
|
178 | - ob_start(); |
|
179 | - try { |
|
180 | - include $file; |
|
181 | - $data = ob_get_contents(); |
|
182 | - } catch (\Exception $e) { |
|
183 | - @ob_end_clean(); |
|
184 | - throw $e; |
|
185 | - } |
|
186 | - @ob_end_clean(); |
|
177 | + // Include |
|
178 | + ob_start(); |
|
179 | + try { |
|
180 | + include $file; |
|
181 | + $data = ob_get_contents(); |
|
182 | + } catch (\Exception $e) { |
|
183 | + @ob_end_clean(); |
|
184 | + throw $e; |
|
185 | + } |
|
186 | + @ob_end_clean(); |
|
187 | 187 | |
188 | - // Return data |
|
189 | - return $data; |
|
190 | - } |
|
188 | + // Return data |
|
189 | + return $data; |
|
190 | + } |
|
191 | 191 | } |
@@ -65,8 +65,8 @@ |
||
65 | 65 | |
66 | 66 | $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
67 | 67 | $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
68 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
69 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
68 | + $this->issueDate = new \DateTime('@'.$info['validFrom_time_t'], $gmt); |
|
69 | + $this->expireDate = new \DateTime('@'.$info['validTo_time_t'], $gmt); |
|
70 | 70 | $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
71 | 71 | $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
72 | 72 | } |
@@ -31,104 +31,104 @@ |
||
31 | 31 | use OCP\ICertificate; |
32 | 32 | |
33 | 33 | class Certificate implements ICertificate { |
34 | - protected $name; |
|
35 | - |
|
36 | - protected $commonName; |
|
37 | - |
|
38 | - protected $organization; |
|
39 | - |
|
40 | - protected $serial; |
|
41 | - |
|
42 | - protected $issueDate; |
|
43 | - |
|
44 | - protected $expireDate; |
|
45 | - |
|
46 | - protected $issuerName; |
|
47 | - |
|
48 | - protected $issuerOrganization; |
|
49 | - |
|
50 | - /** |
|
51 | - * @param string $data base64 encoded certificate |
|
52 | - * @param string $name |
|
53 | - * @throws \Exception If the certificate could not get parsed |
|
54 | - */ |
|
55 | - public function __construct(string $data, string $name) { |
|
56 | - $this->name = $name; |
|
57 | - $gmt = new \DateTimeZone('GMT'); |
|
58 | - |
|
59 | - // If string starts with "file://" ignore the certificate |
|
60 | - $query = 'file://'; |
|
61 | - if (strtolower(substr($data, 0, strlen($query))) === $query) { |
|
62 | - throw new \Exception('Certificate could not get parsed.'); |
|
63 | - } |
|
64 | - |
|
65 | - $info = openssl_x509_parse($data); |
|
66 | - if (!is_array($info)) { |
|
67 | - throw new \Exception('Certificate could not get parsed.'); |
|
68 | - } |
|
69 | - |
|
70 | - $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
71 | - $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
72 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
73 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
74 | - $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
75 | - $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function getName(): string { |
|
82 | - return $this->name; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @return string|null |
|
87 | - */ |
|
88 | - public function getCommonName(): ?string { |
|
89 | - return $this->commonName; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @return string|null |
|
94 | - */ |
|
95 | - public function getOrganization(): ?string { |
|
96 | - return $this->organization; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @return \DateTime |
|
101 | - */ |
|
102 | - public function getIssueDate(): \DateTime { |
|
103 | - return $this->issueDate; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return \DateTime |
|
108 | - */ |
|
109 | - public function getExpireDate(): \DateTime { |
|
110 | - return $this->expireDate; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return bool |
|
115 | - */ |
|
116 | - public function isExpired(): bool { |
|
117 | - $now = new \DateTime(); |
|
118 | - return $this->issueDate > $now or $now > $this->expireDate; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @return string|null |
|
123 | - */ |
|
124 | - public function getIssuerName(): ?string { |
|
125 | - return $this->issuerName; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @return string|null |
|
130 | - */ |
|
131 | - public function getIssuerOrganization(): ?string { |
|
132 | - return $this->issuerOrganization; |
|
133 | - } |
|
34 | + protected $name; |
|
35 | + |
|
36 | + protected $commonName; |
|
37 | + |
|
38 | + protected $organization; |
|
39 | + |
|
40 | + protected $serial; |
|
41 | + |
|
42 | + protected $issueDate; |
|
43 | + |
|
44 | + protected $expireDate; |
|
45 | + |
|
46 | + protected $issuerName; |
|
47 | + |
|
48 | + protected $issuerOrganization; |
|
49 | + |
|
50 | + /** |
|
51 | + * @param string $data base64 encoded certificate |
|
52 | + * @param string $name |
|
53 | + * @throws \Exception If the certificate could not get parsed |
|
54 | + */ |
|
55 | + public function __construct(string $data, string $name) { |
|
56 | + $this->name = $name; |
|
57 | + $gmt = new \DateTimeZone('GMT'); |
|
58 | + |
|
59 | + // If string starts with "file://" ignore the certificate |
|
60 | + $query = 'file://'; |
|
61 | + if (strtolower(substr($data, 0, strlen($query))) === $query) { |
|
62 | + throw new \Exception('Certificate could not get parsed.'); |
|
63 | + } |
|
64 | + |
|
65 | + $info = openssl_x509_parse($data); |
|
66 | + if (!is_array($info)) { |
|
67 | + throw new \Exception('Certificate could not get parsed.'); |
|
68 | + } |
|
69 | + |
|
70 | + $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
71 | + $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
72 | + $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
73 | + $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
74 | + $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
75 | + $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function getName(): string { |
|
82 | + return $this->name; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @return string|null |
|
87 | + */ |
|
88 | + public function getCommonName(): ?string { |
|
89 | + return $this->commonName; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @return string|null |
|
94 | + */ |
|
95 | + public function getOrganization(): ?string { |
|
96 | + return $this->organization; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @return \DateTime |
|
101 | + */ |
|
102 | + public function getIssueDate(): \DateTime { |
|
103 | + return $this->issueDate; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return \DateTime |
|
108 | + */ |
|
109 | + public function getExpireDate(): \DateTime { |
|
110 | + return $this->expireDate; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return bool |
|
115 | + */ |
|
116 | + public function isExpired(): bool { |
|
117 | + $now = new \DateTime(); |
|
118 | + return $this->issueDate > $now or $now > $this->expireDate; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @return string|null |
|
123 | + */ |
|
124 | + public function getIssuerName(): ?string { |
|
125 | + return $this->issuerName; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @return string|null |
|
130 | + */ |
|
131 | + public function getIssuerOrganization(): ?string { |
|
132 | + return $this->issuerOrganization; |
|
133 | + } |
|
134 | 134 | } |
@@ -89,7 +89,7 @@ |
||
89 | 89 | $user = $this->userManager->get($userId); |
90 | 90 | if ($user !== null) { |
91 | 91 | $key = $this->keyManager->getKey($user); |
92 | - return (bool)openssl_verify( |
|
92 | + return (bool) openssl_verify( |
|
93 | 93 | json_encode($data['message']), |
94 | 94 | base64_decode($data['signature']), |
95 | 95 | $key->getPublic(), |
@@ -32,76 +32,76 @@ |
||
32 | 32 | use OCP\IUserManager; |
33 | 33 | |
34 | 34 | class Signer { |
35 | - /** @var Manager */ |
|
36 | - private $keyManager; |
|
37 | - /** @var ITimeFactory */ |
|
38 | - private $timeFactory; |
|
39 | - /** @var IUserManager */ |
|
40 | - private $userManager; |
|
35 | + /** @var Manager */ |
|
36 | + private $keyManager; |
|
37 | + /** @var ITimeFactory */ |
|
38 | + private $timeFactory; |
|
39 | + /** @var IUserManager */ |
|
40 | + private $userManager; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param Manager $keyManager |
|
44 | - * @param ITimeFactory $timeFactory |
|
45 | - * @param IUserManager $userManager |
|
46 | - */ |
|
47 | - public function __construct(Manager $keyManager, |
|
48 | - ITimeFactory $timeFactory, |
|
49 | - IUserManager $userManager) { |
|
50 | - $this->keyManager = $keyManager; |
|
51 | - $this->timeFactory = $timeFactory; |
|
52 | - $this->userManager = $userManager; |
|
53 | - } |
|
42 | + /** |
|
43 | + * @param Manager $keyManager |
|
44 | + * @param ITimeFactory $timeFactory |
|
45 | + * @param IUserManager $userManager |
|
46 | + */ |
|
47 | + public function __construct(Manager $keyManager, |
|
48 | + ITimeFactory $timeFactory, |
|
49 | + IUserManager $userManager) { |
|
50 | + $this->keyManager = $keyManager; |
|
51 | + $this->timeFactory = $timeFactory; |
|
52 | + $this->userManager = $userManager; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Returns a signed blob for $data |
|
57 | - * |
|
58 | - * @param string $type |
|
59 | - * @param array $data |
|
60 | - * @param IUser $user |
|
61 | - * @return array ['message', 'signature'] |
|
62 | - */ |
|
63 | - public function sign(string $type, array $data, IUser $user): array { |
|
64 | - $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
65 | - $data = [ |
|
66 | - 'data' => $data, |
|
67 | - 'type' => $type, |
|
68 | - 'signer' => $user->getCloudId(), |
|
69 | - 'timestamp' => $this->timeFactory->getTime(), |
|
70 | - ]; |
|
71 | - openssl_sign(json_encode($data), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
55 | + /** |
|
56 | + * Returns a signed blob for $data |
|
57 | + * |
|
58 | + * @param string $type |
|
59 | + * @param array $data |
|
60 | + * @param IUser $user |
|
61 | + * @return array ['message', 'signature'] |
|
62 | + */ |
|
63 | + public function sign(string $type, array $data, IUser $user): array { |
|
64 | + $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
65 | + $data = [ |
|
66 | + 'data' => $data, |
|
67 | + 'type' => $type, |
|
68 | + 'signer' => $user->getCloudId(), |
|
69 | + 'timestamp' => $this->timeFactory->getTime(), |
|
70 | + ]; |
|
71 | + openssl_sign(json_encode($data), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
72 | 72 | |
73 | - return [ |
|
74 | - 'message' => $data, |
|
75 | - 'signature' => base64_encode($signature), |
|
76 | - ]; |
|
77 | - } |
|
73 | + return [ |
|
74 | + 'message' => $data, |
|
75 | + 'signature' => base64_encode($signature), |
|
76 | + ]; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Whether the data is signed properly |
|
81 | - * |
|
82 | - * @param array $data |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function verify(array $data): bool { |
|
86 | - if (isset($data['message']) |
|
87 | - && isset($data['signature']) |
|
88 | - && isset($data['message']['signer']) |
|
89 | - ) { |
|
90 | - $location = strrpos($data['message']['signer'], '@'); |
|
91 | - $userId = substr($data['message']['signer'], 0, $location); |
|
79 | + /** |
|
80 | + * Whether the data is signed properly |
|
81 | + * |
|
82 | + * @param array $data |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function verify(array $data): bool { |
|
86 | + if (isset($data['message']) |
|
87 | + && isset($data['signature']) |
|
88 | + && isset($data['message']['signer']) |
|
89 | + ) { |
|
90 | + $location = strrpos($data['message']['signer'], '@'); |
|
91 | + $userId = substr($data['message']['signer'], 0, $location); |
|
92 | 92 | |
93 | - $user = $this->userManager->get($userId); |
|
94 | - if ($user !== null) { |
|
95 | - $key = $this->keyManager->getKey($user); |
|
96 | - return (bool)openssl_verify( |
|
97 | - json_encode($data['message']), |
|
98 | - base64_decode($data['signature']), |
|
99 | - $key->getPublic(), |
|
100 | - OPENSSL_ALGO_SHA512 |
|
101 | - ); |
|
102 | - } |
|
103 | - } |
|
93 | + $user = $this->userManager->get($userId); |
|
94 | + if ($user !== null) { |
|
95 | + $key = $this->keyManager->getKey($user); |
|
96 | + return (bool)openssl_verify( |
|
97 | + json_encode($data['message']), |
|
98 | + base64_decode($data['signature']), |
|
99 | + $key->getPublic(), |
|
100 | + OPENSSL_ALGO_SHA512 |
|
101 | + ); |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | - return false; |
|
106 | - } |
|
105 | + return false; |
|
106 | + } |
|
107 | 107 | } |
@@ -27,7 +27,7 @@ |
||
27 | 27 | use OCP\Encryption\Exceptions\GenericEncryptionException; |
28 | 28 | |
29 | 29 | class EncryptionHeaderToLargeException extends GenericEncryptionException { |
30 | - public function __construct() { |
|
31 | - parent::__construct('max header size exceeded'); |
|
32 | - } |
|
30 | + public function __construct() { |
|
31 | + parent::__construct('max header size exceeded'); |
|
32 | + } |
|
33 | 33 | } |
@@ -73,7 +73,7 @@ |
||
73 | 73 | // always add owner to the list of users with access to the file |
74 | 74 | $userIds = [$owner]; |
75 | 75 | |
76 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
76 | + if (!$this->util->isFile($owner.'/'.$ownerPath)) { |
|
77 | 77 | return ['users' => $userIds, 'public' => false]; |
78 | 78 | } |
79 | 79 |
@@ -35,98 +35,98 @@ |
||
35 | 35 | use OCP\Share\IManager; |
36 | 36 | |
37 | 37 | class File implements \OCP\Encryption\IFile { |
38 | - protected Util $util; |
|
39 | - private IRootFolder $rootFolder; |
|
40 | - private IManager $shareManager; |
|
38 | + protected Util $util; |
|
39 | + private IRootFolder $rootFolder; |
|
40 | + private IManager $shareManager; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Cache results of already checked folders |
|
44 | - * @var CappedMemoryCache<array> |
|
45 | - */ |
|
46 | - protected CappedMemoryCache $cache; |
|
47 | - private ?IAppManager $appManager = null; |
|
42 | + /** |
|
43 | + * Cache results of already checked folders |
|
44 | + * @var CappedMemoryCache<array> |
|
45 | + */ |
|
46 | + protected CappedMemoryCache $cache; |
|
47 | + private ?IAppManager $appManager = null; |
|
48 | 48 | |
49 | - public function __construct(Util $util, |
|
50 | - IRootFolder $rootFolder, |
|
51 | - IManager $shareManager) { |
|
52 | - $this->util = $util; |
|
53 | - $this->cache = new CappedMemoryCache(); |
|
54 | - $this->rootFolder = $rootFolder; |
|
55 | - $this->shareManager = $shareManager; |
|
56 | - } |
|
49 | + public function __construct(Util $util, |
|
50 | + IRootFolder $rootFolder, |
|
51 | + IManager $shareManager) { |
|
52 | + $this->util = $util; |
|
53 | + $this->cache = new CappedMemoryCache(); |
|
54 | + $this->rootFolder = $rootFolder; |
|
55 | + $this->shareManager = $shareManager; |
|
56 | + } |
|
57 | 57 | |
58 | - public function getAppManager(): IAppManager { |
|
59 | - // Lazy evaluate app manager as it initialize the db too early otherwise |
|
60 | - if ($this->appManager) { |
|
61 | - return $this->appManager; |
|
62 | - } |
|
63 | - $this->appManager = \OCP\Server::get(IAppManager::class); |
|
64 | - return $this->appManager; |
|
65 | - } |
|
58 | + public function getAppManager(): IAppManager { |
|
59 | + // Lazy evaluate app manager as it initialize the db too early otherwise |
|
60 | + if ($this->appManager) { |
|
61 | + return $this->appManager; |
|
62 | + } |
|
63 | + $this->appManager = \OCP\Server::get(IAppManager::class); |
|
64 | + return $this->appManager; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Get list of users with access to the file |
|
69 | - * |
|
70 | - * @param string $path to the file |
|
71 | - * @return array{users: string[], public: bool} |
|
72 | - */ |
|
73 | - public function getAccessList($path) { |
|
74 | - // Make sure that a share key is generated for the owner too |
|
75 | - [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
67 | + /** |
|
68 | + * Get list of users with access to the file |
|
69 | + * |
|
70 | + * @param string $path to the file |
|
71 | + * @return array{users: string[], public: bool} |
|
72 | + */ |
|
73 | + public function getAccessList($path) { |
|
74 | + // Make sure that a share key is generated for the owner too |
|
75 | + [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
76 | 76 | |
77 | - // always add owner to the list of users with access to the file |
|
78 | - $userIds = [$owner]; |
|
77 | + // always add owner to the list of users with access to the file |
|
78 | + $userIds = [$owner]; |
|
79 | 79 | |
80 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
81 | - return ['users' => $userIds, 'public' => false]; |
|
82 | - } |
|
80 | + if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
81 | + return ['users' => $userIds, 'public' => false]; |
|
82 | + } |
|
83 | 83 | |
84 | - $ownerPath = substr($ownerPath, strlen('/files')); |
|
85 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
86 | - try { |
|
87 | - $file = $userFolder->get($ownerPath); |
|
88 | - } catch (NotFoundException $e) { |
|
89 | - $file = null; |
|
90 | - } |
|
91 | - $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
84 | + $ownerPath = substr($ownerPath, strlen('/files')); |
|
85 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
86 | + try { |
|
87 | + $file = $userFolder->get($ownerPath); |
|
88 | + } catch (NotFoundException $e) { |
|
89 | + $file = null; |
|
90 | + } |
|
91 | + $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
92 | 92 | |
93 | - // first get the shares for the parent and cache the result so that we don't |
|
94 | - // need to check all parents for every file |
|
95 | - $parent = dirname($ownerPath); |
|
96 | - $parentNode = $userFolder->get($parent); |
|
97 | - if (isset($this->cache[$parent])) { |
|
98 | - $resultForParents = $this->cache[$parent]; |
|
99 | - } else { |
|
100 | - $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
101 | - $this->cache[$parent] = $resultForParents; |
|
102 | - } |
|
103 | - $userIds = array_merge($userIds, $resultForParents['users']); |
|
104 | - $public = $resultForParents['public'] || $resultForParents['remote']; |
|
93 | + // first get the shares for the parent and cache the result so that we don't |
|
94 | + // need to check all parents for every file |
|
95 | + $parent = dirname($ownerPath); |
|
96 | + $parentNode = $userFolder->get($parent); |
|
97 | + if (isset($this->cache[$parent])) { |
|
98 | + $resultForParents = $this->cache[$parent]; |
|
99 | + } else { |
|
100 | + $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
101 | + $this->cache[$parent] = $resultForParents; |
|
102 | + } |
|
103 | + $userIds = array_merge($userIds, $resultForParents['users']); |
|
104 | + $public = $resultForParents['public'] || $resultForParents['remote']; |
|
105 | 105 | |
106 | 106 | |
107 | - // Find out who, if anyone, is sharing the file |
|
108 | - if ($file !== null) { |
|
109 | - $resultForFile = $this->shareManager->getAccessList($file, false); |
|
110 | - $userIds = array_merge($userIds, $resultForFile['users']); |
|
111 | - $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
112 | - } |
|
107 | + // Find out who, if anyone, is sharing the file |
|
108 | + if ($file !== null) { |
|
109 | + $resultForFile = $this->shareManager->getAccessList($file, false); |
|
110 | + $userIds = array_merge($userIds, $resultForFile['users']); |
|
111 | + $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
112 | + } |
|
113 | 113 | |
114 | - // check if it is a group mount |
|
115 | - if ($this->getAppManager()->isEnabledForUser("files_external")) { |
|
116 | - /** @var GlobalStoragesService $storageService */ |
|
117 | - $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
118 | - $storages = $storageService->getAllStorages(); |
|
119 | - foreach ($storages as $storage) { |
|
120 | - if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) { |
|
121 | - $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups()); |
|
122 | - $userIds = array_merge($userIds, $mountedFor); |
|
123 | - } |
|
124 | - } |
|
125 | - } |
|
114 | + // check if it is a group mount |
|
115 | + if ($this->getAppManager()->isEnabledForUser("files_external")) { |
|
116 | + /** @var GlobalStoragesService $storageService */ |
|
117 | + $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
118 | + $storages = $storageService->getAllStorages(); |
|
119 | + foreach ($storages as $storage) { |
|
120 | + if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) { |
|
121 | + $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups()); |
|
122 | + $userIds = array_merge($userIds, $mountedFor); |
|
123 | + } |
|
124 | + } |
|
125 | + } |
|
126 | 126 | |
127 | - // Remove duplicate UIDs |
|
128 | - $uniqueUserIds = array_unique($userIds); |
|
127 | + // Remove duplicate UIDs |
|
128 | + $uniqueUserIds = array_unique($userIds); |
|
129 | 129 | |
130 | - return ['users' => $uniqueUserIds, 'public' => $public]; |
|
131 | - } |
|
130 | + return ['users' => $uniqueUserIds, 'public' => $public]; |
|
131 | + } |
|
132 | 132 | } |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | */ |
67 | 67 | private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
68 | 68 | if ($postFix !== '') { |
69 | - $postFix = '.' . ltrim($postFix, '.'); |
|
69 | + $postFix = '.'.ltrim($postFix, '.'); |
|
70 | 70 | $postFix = str_replace(['\\', '/'], '', $postFix); |
71 | 71 | $absolutePath .= '-'; |
72 | 72 | } |
73 | 73 | |
74 | - return $absolutePath . $postFix; |
|
74 | + return $absolutePath.$postFix; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -127,11 +127,11 @@ discard block |
||
127 | 127 | $this->current[] = $uniqueFileName; |
128 | 128 | |
129 | 129 | // Build a name without postfix |
130 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
130 | + $path = $this->buildFileNameWithSuffix($uniqueFileName.'-folder', $postFix); |
|
131 | 131 | mkdir($path, 0700); |
132 | 132 | $this->current[] = $path; |
133 | 133 | |
134 | - return $path . '/'; |
|
134 | + return $path.'/'; |
|
135 | 135 | } else { |
136 | 136 | $this->log->warning( |
137 | 137 | 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | if ($dh) { |
191 | 191 | while (($file = readdir($dh)) !== false) { |
192 | 192 | if (substr($file, 0, 7) === self::TMP_PREFIX) { |
193 | - $path = $this->tmpBaseDir . '/' . $file; |
|
193 | + $path = $this->tmpBaseDir.'/'.$file; |
|
194 | 194 | $mtime = filemtime($path); |
195 | 195 | if ($mtime < $cutOfTime) { |
196 | 196 | $files[] = $path; |
@@ -37,244 +37,244 @@ |
||
37 | 37 | use Psr\Log\LoggerInterface; |
38 | 38 | |
39 | 39 | class TempManager implements ITempManager { |
40 | - /** @var string[] Current temporary files and folders, used for cleanup */ |
|
41 | - protected $current = []; |
|
42 | - /** @var string i.e. /tmp on linux systems */ |
|
43 | - protected $tmpBaseDir; |
|
44 | - /** @var LoggerInterface */ |
|
45 | - protected $log; |
|
46 | - /** @var IConfig */ |
|
47 | - protected $config; |
|
48 | - /** @var IniGetWrapper */ |
|
49 | - protected $iniGetWrapper; |
|
40 | + /** @var string[] Current temporary files and folders, used for cleanup */ |
|
41 | + protected $current = []; |
|
42 | + /** @var string i.e. /tmp on linux systems */ |
|
43 | + protected $tmpBaseDir; |
|
44 | + /** @var LoggerInterface */ |
|
45 | + protected $log; |
|
46 | + /** @var IConfig */ |
|
47 | + protected $config; |
|
48 | + /** @var IniGetWrapper */ |
|
49 | + protected $iniGetWrapper; |
|
50 | 50 | |
51 | - /** Prefix */ |
|
52 | - public const TMP_PREFIX = 'oc_tmp_'; |
|
51 | + /** Prefix */ |
|
52 | + public const TMP_PREFIX = 'oc_tmp_'; |
|
53 | 53 | |
54 | - public function __construct(LoggerInterface $logger, IConfig $config, IniGetWrapper $iniGetWrapper) { |
|
55 | - $this->log = $logger; |
|
56 | - $this->config = $config; |
|
57 | - $this->iniGetWrapper = $iniGetWrapper; |
|
58 | - $this->tmpBaseDir = $this->getTempBaseDir(); |
|
59 | - } |
|
54 | + public function __construct(LoggerInterface $logger, IConfig $config, IniGetWrapper $iniGetWrapper) { |
|
55 | + $this->log = $logger; |
|
56 | + $this->config = $config; |
|
57 | + $this->iniGetWrapper = $iniGetWrapper; |
|
58 | + $this->tmpBaseDir = $this->getTempBaseDir(); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Builds the filename with suffix and removes potential dangerous characters |
|
63 | - * such as directory separators. |
|
64 | - * |
|
65 | - * @param string $absolutePath Absolute path to the file / folder |
|
66 | - * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
70 | - if ($postFix !== '') { |
|
71 | - $postFix = '.' . ltrim($postFix, '.'); |
|
72 | - $postFix = str_replace(['\\', '/'], '', $postFix); |
|
73 | - $absolutePath .= '-'; |
|
74 | - } |
|
61 | + /** |
|
62 | + * Builds the filename with suffix and removes potential dangerous characters |
|
63 | + * such as directory separators. |
|
64 | + * |
|
65 | + * @param string $absolutePath Absolute path to the file / folder |
|
66 | + * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
70 | + if ($postFix !== '') { |
|
71 | + $postFix = '.' . ltrim($postFix, '.'); |
|
72 | + $postFix = str_replace(['\\', '/'], '', $postFix); |
|
73 | + $absolutePath .= '-'; |
|
74 | + } |
|
75 | 75 | |
76 | - return $absolutePath . $postFix; |
|
77 | - } |
|
76 | + return $absolutePath . $postFix; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Create a temporary file and return the path |
|
81 | - * |
|
82 | - * @param string $postFix Postfix appended to the temporary file name |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getTemporaryFile($postFix = '') { |
|
86 | - if (is_writable($this->tmpBaseDir)) { |
|
87 | - // To create an unique file and prevent the risk of race conditions |
|
88 | - // or duplicated temporary files by other means such as collisions |
|
89 | - // we need to create the file using `tempnam` and append a possible |
|
90 | - // postfix to it later |
|
91 | - $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
92 | - $this->current[] = $file; |
|
79 | + /** |
|
80 | + * Create a temporary file and return the path |
|
81 | + * |
|
82 | + * @param string $postFix Postfix appended to the temporary file name |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getTemporaryFile($postFix = '') { |
|
86 | + if (is_writable($this->tmpBaseDir)) { |
|
87 | + // To create an unique file and prevent the risk of race conditions |
|
88 | + // or duplicated temporary files by other means such as collisions |
|
89 | + // we need to create the file using `tempnam` and append a possible |
|
90 | + // postfix to it later |
|
91 | + $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
92 | + $this->current[] = $file; |
|
93 | 93 | |
94 | - // If a postfix got specified sanitize it and create a postfixed |
|
95 | - // temporary file |
|
96 | - if ($postFix !== '') { |
|
97 | - $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
98 | - touch($fileNameWithPostfix); |
|
99 | - chmod($fileNameWithPostfix, 0600); |
|
100 | - $this->current[] = $fileNameWithPostfix; |
|
101 | - return $fileNameWithPostfix; |
|
102 | - } |
|
94 | + // If a postfix got specified sanitize it and create a postfixed |
|
95 | + // temporary file |
|
96 | + if ($postFix !== '') { |
|
97 | + $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
98 | + touch($fileNameWithPostfix); |
|
99 | + chmod($fileNameWithPostfix, 0600); |
|
100 | + $this->current[] = $fileNameWithPostfix; |
|
101 | + return $fileNameWithPostfix; |
|
102 | + } |
|
103 | 103 | |
104 | - return $file; |
|
105 | - } else { |
|
106 | - $this->log->warning( |
|
107 | - 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
108 | - [ |
|
109 | - 'dir' => $this->tmpBaseDir, |
|
110 | - ] |
|
111 | - ); |
|
112 | - return false; |
|
113 | - } |
|
114 | - } |
|
104 | + return $file; |
|
105 | + } else { |
|
106 | + $this->log->warning( |
|
107 | + 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
108 | + [ |
|
109 | + 'dir' => $this->tmpBaseDir, |
|
110 | + ] |
|
111 | + ); |
|
112 | + return false; |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Create a temporary folder and return the path |
|
118 | - * |
|
119 | - * @param string $postFix Postfix appended to the temporary folder name |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public function getTemporaryFolder($postFix = '') { |
|
123 | - if (is_writable($this->tmpBaseDir)) { |
|
124 | - // To create an unique directory and prevent the risk of race conditions |
|
125 | - // or duplicated temporary files by other means such as collisions |
|
126 | - // we need to create the file using `tempnam` and append a possible |
|
127 | - // postfix to it later |
|
128 | - $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
129 | - $this->current[] = $uniqueFileName; |
|
116 | + /** |
|
117 | + * Create a temporary folder and return the path |
|
118 | + * |
|
119 | + * @param string $postFix Postfix appended to the temporary folder name |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public function getTemporaryFolder($postFix = '') { |
|
123 | + if (is_writable($this->tmpBaseDir)) { |
|
124 | + // To create an unique directory and prevent the risk of race conditions |
|
125 | + // or duplicated temporary files by other means such as collisions |
|
126 | + // we need to create the file using `tempnam` and append a possible |
|
127 | + // postfix to it later |
|
128 | + $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
129 | + $this->current[] = $uniqueFileName; |
|
130 | 130 | |
131 | - // Build a name without postfix |
|
132 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
133 | - mkdir($path, 0700); |
|
134 | - $this->current[] = $path; |
|
131 | + // Build a name without postfix |
|
132 | + $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
133 | + mkdir($path, 0700); |
|
134 | + $this->current[] = $path; |
|
135 | 135 | |
136 | - return $path . '/'; |
|
137 | - } else { |
|
138 | - $this->log->warning( |
|
139 | - 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
140 | - [ |
|
141 | - 'dir' => $this->tmpBaseDir, |
|
142 | - ] |
|
143 | - ); |
|
144 | - return false; |
|
145 | - } |
|
146 | - } |
|
136 | + return $path . '/'; |
|
137 | + } else { |
|
138 | + $this->log->warning( |
|
139 | + 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
140 | + [ |
|
141 | + 'dir' => $this->tmpBaseDir, |
|
142 | + ] |
|
143 | + ); |
|
144 | + return false; |
|
145 | + } |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Remove the temporary files and folders generated during this request |
|
150 | - */ |
|
151 | - public function clean() { |
|
152 | - $this->cleanFiles($this->current); |
|
153 | - } |
|
148 | + /** |
|
149 | + * Remove the temporary files and folders generated during this request |
|
150 | + */ |
|
151 | + public function clean() { |
|
152 | + $this->cleanFiles($this->current); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * @param string[] $files |
|
157 | - */ |
|
158 | - protected function cleanFiles($files) { |
|
159 | - foreach ($files as $file) { |
|
160 | - if (file_exists($file)) { |
|
161 | - try { |
|
162 | - \OC_Helper::rmdirr($file); |
|
163 | - } catch (\UnexpectedValueException $ex) { |
|
164 | - $this->log->warning( |
|
165 | - "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
166 | - [ |
|
167 | - 'file' => $file, |
|
168 | - 'error' => $ex->getMessage(), |
|
169 | - ] |
|
170 | - ); |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
174 | - } |
|
155 | + /** |
|
156 | + * @param string[] $files |
|
157 | + */ |
|
158 | + protected function cleanFiles($files) { |
|
159 | + foreach ($files as $file) { |
|
160 | + if (file_exists($file)) { |
|
161 | + try { |
|
162 | + \OC_Helper::rmdirr($file); |
|
163 | + } catch (\UnexpectedValueException $ex) { |
|
164 | + $this->log->warning( |
|
165 | + "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
166 | + [ |
|
167 | + 'file' => $file, |
|
168 | + 'error' => $ex->getMessage(), |
|
169 | + ] |
|
170 | + ); |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * Remove old temporary files and folders that were failed to be cleaned |
|
178 | - */ |
|
179 | - public function cleanOld() { |
|
180 | - $this->cleanFiles($this->getOldFiles()); |
|
181 | - } |
|
176 | + /** |
|
177 | + * Remove old temporary files and folders that were failed to be cleaned |
|
178 | + */ |
|
179 | + public function cleanOld() { |
|
180 | + $this->cleanFiles($this->getOldFiles()); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Get all temporary files and folders generated by oc older than an hour |
|
185 | - * |
|
186 | - * @return string[] |
|
187 | - */ |
|
188 | - protected function getOldFiles() { |
|
189 | - $cutOfTime = time() - 3600; |
|
190 | - $files = []; |
|
191 | - $dh = opendir($this->tmpBaseDir); |
|
192 | - if ($dh) { |
|
193 | - while (($file = readdir($dh)) !== false) { |
|
194 | - if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
195 | - $path = $this->tmpBaseDir . '/' . $file; |
|
196 | - $mtime = filemtime($path); |
|
197 | - if ($mtime < $cutOfTime) { |
|
198 | - $files[] = $path; |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - } |
|
203 | - return $files; |
|
204 | - } |
|
183 | + /** |
|
184 | + * Get all temporary files and folders generated by oc older than an hour |
|
185 | + * |
|
186 | + * @return string[] |
|
187 | + */ |
|
188 | + protected function getOldFiles() { |
|
189 | + $cutOfTime = time() - 3600; |
|
190 | + $files = []; |
|
191 | + $dh = opendir($this->tmpBaseDir); |
|
192 | + if ($dh) { |
|
193 | + while (($file = readdir($dh)) !== false) { |
|
194 | + if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
195 | + $path = $this->tmpBaseDir . '/' . $file; |
|
196 | + $mtime = filemtime($path); |
|
197 | + if ($mtime < $cutOfTime) { |
|
198 | + $files[] = $path; |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + } |
|
203 | + return $files; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Get the temporary base directory configured on the server |
|
208 | - * |
|
209 | - * @return string Path to the temporary directory or null |
|
210 | - * @throws \UnexpectedValueException |
|
211 | - */ |
|
212 | - public function getTempBaseDir() { |
|
213 | - if ($this->tmpBaseDir) { |
|
214 | - return $this->tmpBaseDir; |
|
215 | - } |
|
206 | + /** |
|
207 | + * Get the temporary base directory configured on the server |
|
208 | + * |
|
209 | + * @return string Path to the temporary directory or null |
|
210 | + * @throws \UnexpectedValueException |
|
211 | + */ |
|
212 | + public function getTempBaseDir() { |
|
213 | + if ($this->tmpBaseDir) { |
|
214 | + return $this->tmpBaseDir; |
|
215 | + } |
|
216 | 216 | |
217 | - $directories = []; |
|
218 | - if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
219 | - $directories[] = $temp; |
|
220 | - } |
|
221 | - if ($temp = $this->iniGetWrapper->get('upload_tmp_dir')) { |
|
222 | - $directories[] = $temp; |
|
223 | - } |
|
224 | - if ($temp = getenv('TMP')) { |
|
225 | - $directories[] = $temp; |
|
226 | - } |
|
227 | - if ($temp = getenv('TEMP')) { |
|
228 | - $directories[] = $temp; |
|
229 | - } |
|
230 | - if ($temp = getenv('TMPDIR')) { |
|
231 | - $directories[] = $temp; |
|
232 | - } |
|
233 | - if ($temp = sys_get_temp_dir()) { |
|
234 | - $directories[] = $temp; |
|
235 | - } |
|
217 | + $directories = []; |
|
218 | + if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
219 | + $directories[] = $temp; |
|
220 | + } |
|
221 | + if ($temp = $this->iniGetWrapper->get('upload_tmp_dir')) { |
|
222 | + $directories[] = $temp; |
|
223 | + } |
|
224 | + if ($temp = getenv('TMP')) { |
|
225 | + $directories[] = $temp; |
|
226 | + } |
|
227 | + if ($temp = getenv('TEMP')) { |
|
228 | + $directories[] = $temp; |
|
229 | + } |
|
230 | + if ($temp = getenv('TMPDIR')) { |
|
231 | + $directories[] = $temp; |
|
232 | + } |
|
233 | + if ($temp = sys_get_temp_dir()) { |
|
234 | + $directories[] = $temp; |
|
235 | + } |
|
236 | 236 | |
237 | - foreach ($directories as $dir) { |
|
238 | - if ($this->checkTemporaryDirectory($dir)) { |
|
239 | - return $dir; |
|
240 | - } |
|
241 | - } |
|
237 | + foreach ($directories as $dir) { |
|
238 | + if ($this->checkTemporaryDirectory($dir)) { |
|
239 | + return $dir; |
|
240 | + } |
|
241 | + } |
|
242 | 242 | |
243 | - $temp = tempnam(dirname(__FILE__), ''); |
|
244 | - if (file_exists($temp)) { |
|
245 | - unlink($temp); |
|
246 | - return dirname($temp); |
|
247 | - } |
|
248 | - throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
249 | - } |
|
243 | + $temp = tempnam(dirname(__FILE__), ''); |
|
244 | + if (file_exists($temp)) { |
|
245 | + unlink($temp); |
|
246 | + return dirname($temp); |
|
247 | + } |
|
248 | + throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * Check if a temporary directory is ready for use |
|
253 | - * |
|
254 | - * @param mixed $directory |
|
255 | - * @return bool |
|
256 | - */ |
|
257 | - private function checkTemporaryDirectory($directory) { |
|
258 | - // suppress any possible errors caused by is_writable |
|
259 | - // checks missing or invalid path or characters, wrong permissions etc |
|
260 | - try { |
|
261 | - if (is_writable($directory)) { |
|
262 | - return true; |
|
263 | - } |
|
264 | - } catch (\Exception $e) { |
|
265 | - } |
|
266 | - $this->log->warning('Temporary directory {dir} is not present or writable', |
|
267 | - ['dir' => $directory] |
|
268 | - ); |
|
269 | - return false; |
|
270 | - } |
|
251 | + /** |
|
252 | + * Check if a temporary directory is ready for use |
|
253 | + * |
|
254 | + * @param mixed $directory |
|
255 | + * @return bool |
|
256 | + */ |
|
257 | + private function checkTemporaryDirectory($directory) { |
|
258 | + // suppress any possible errors caused by is_writable |
|
259 | + // checks missing or invalid path or characters, wrong permissions etc |
|
260 | + try { |
|
261 | + if (is_writable($directory)) { |
|
262 | + return true; |
|
263 | + } |
|
264 | + } catch (\Exception $e) { |
|
265 | + } |
|
266 | + $this->log->warning('Temporary directory {dir} is not present or writable', |
|
267 | + ['dir' => $directory] |
|
268 | + ); |
|
269 | + return false; |
|
270 | + } |
|
271 | 271 | |
272 | - /** |
|
273 | - * Override the temporary base directory |
|
274 | - * |
|
275 | - * @param string $directory |
|
276 | - */ |
|
277 | - public function overrideTempBaseDir($directory) { |
|
278 | - $this->tmpBaseDir = $directory; |
|
279 | - } |
|
272 | + /** |
|
273 | + * Override the temporary base directory |
|
274 | + * |
|
275 | + * @param string $directory |
|
276 | + */ |
|
277 | + public function overrideTempBaseDir($directory) { |
|
278 | + $this->tmpBaseDir = $directory; |
|
279 | + } |
|
280 | 280 | } |