Total Complexity | 34 |
Total Lines | 255 |
Duplicated Lines | 0 % |
Coverage | 35.29% |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
49 | class Website extends WebsiteCore |
||
50 | { |
||
51 | /** @var int */ |
||
52 | public const SITE_LENGTH_MIN = 3; |
||
53 | |||
54 | /** @var int */ |
||
55 | public const SITE_LENGTH_MAX = 255; |
||
56 | |||
57 | /** @var string */ |
||
58 | public const SITE_REGEX = '^[a-z0-9][a-z0-9_-]+[a-z0-9]$'; |
||
59 | |||
60 | /** @var int */ |
||
61 | public const NAME_LENGTH_MIN = 3; |
||
62 | |||
63 | /** @var int */ |
||
64 | public const NAME_LENGTH_MAX = 255; |
||
65 | |||
66 | /** @var IConfig */ |
||
67 | private $config; |
||
68 | |||
69 | /** @var IL10N */ |
||
70 | private $l10n; |
||
71 | |||
72 | /** @var IUserManager */ |
||
73 | private $userManager; |
||
74 | |||
75 | /** @var IURLGenerator */ |
||
76 | private $urlGenerator; |
||
77 | |||
78 | /** @var WebsitesService */ |
||
79 | private $websitesService; |
||
80 | |||
81 | /** @var ThemesService */ |
||
82 | private $themesService; |
||
83 | |||
84 | /** @var TemplatesService */ |
||
85 | private $templatesService; |
||
86 | |||
87 | /** @var MiscService */ |
||
88 | private $miscService; |
||
89 | |||
90 | /** @var StorageFolder */ |
||
91 | private $folder; |
||
92 | |||
93 | /** |
||
94 | * Website constructor. |
||
95 | * |
||
96 | * @param array|string|null $data |
||
97 | */ |
||
98 | public function __construct($data = null) |
||
99 | { |
||
100 | $this->config = \OC::$server->getConfig(); |
||
|
|||
101 | $this->l10n = \OC::$server->getL10N(Application::APP_NAME); |
||
102 | $this->userManager = \OC::$server->getUserManager(); |
||
103 | $this->urlGenerator = \OC::$server->getURLGenerator(); |
||
104 | $this->websitesService = \OC::$server->query(WebsitesService::class); |
||
1 ignored issue
–
show
|
|||
105 | $this->themesService = \OC::$server->query(ThemesService::class); |
||
1 ignored issue
–
show
|
|||
106 | $this->templatesService = \OC::$server->query(TemplatesService::class); |
||
1 ignored issue
–
show
|
|||
107 | 3 | $this->miscService = \OC::$server->query(MiscService::class); |
|
1 ignored issue
–
show
|
|||
108 | |||
109 | 3 | parent::__construct($data); |
|
110 | 3 | } |
|
111 | 3 | ||
112 | 3 | /** |
|
113 | 3 | * @return string |
|
114 | 3 | */ |
|
115 | 3 | public function getTimeZone(): string |
|
116 | 3 | { |
|
117 | 3 | $serverTimeZone = date_default_timezone_get() ?: 'UTC'; |
|
118 | return $this->config->getUserValue($this->getUserId(), 'core', 'timezone', $serverTimeZone); |
||
119 | 3 | } |
|
120 | 3 | ||
121 | /** |
||
122 | * @throws WebsiteInvalidOwnerException |
||
123 | */ |
||
124 | public function assertValidOwner(): void |
||
125 | { |
||
126 | $user = $this->userManager->get($this->getUserId()); |
||
127 | if ($user === null) { |
||
128 | throw new WebsiteInvalidOwnerException(); |
||
129 | } |
||
130 | if (!$user->isEnabled()) { |
||
131 | throw new WebsiteInvalidOwnerException(); |
||
132 | } |
||
133 | if (!$this->websitesService->isUserAllowed($this->getUserId())) { |
||
134 | throw new WebsiteInvalidOwnerException(); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @throws WebsiteInvalidDataException |
||
140 | */ |
||
141 | public function assertValidName(): void |
||
142 | { |
||
143 | if (strlen($this->getName()) < self::NAME_LENGTH_MIN) { |
||
144 | throw new WebsiteInvalidDataException('name', $this->l10n->t('The name of the website must be longer.')); |
||
145 | } |
||
146 | if (strlen($this->getName()) > self::NAME_LENGTH_MAX) { |
||
147 | throw new WebsiteInvalidDataException('name', $this->l10n->t('The name of the website is too long.')); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @throws WebsiteInvalidDataException |
||
153 | */ |
||
154 | public function assertValidSite(): void |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @throws WebsiteInvalidDataException |
||
172 | */ |
||
173 | public function assertValidPath(): void |
||
174 | { |
||
175 | try { |
||
176 | $path = $this->miscService->normalizePath($this->getPath()); |
||
177 | if ($path === '') { |
||
178 | throw new InvalidPathException(); |
||
179 | } |
||
180 | } catch (InvalidPathException $e) { |
||
181 | throw new WebsiteInvalidDataException( |
||
182 | 'path', |
||
183 | $this->l10n->t('The path of the website is invalid.') |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | $userFolder = new StorageFolder(\OC::$server->getUserFolder($this->getUserId())); |
||
188 | |||
189 | try { |
||
190 | $websiteBaseFolder = $userFolder->getFolder(dirname($path)); |
||
191 | |||
192 | try { |
||
193 | $websiteFolder = $websiteBaseFolder->getFolder(basename($path)); |
||
194 | |||
195 | if (!$websiteFolder->isLocal()) { |
||
196 | throw new WebsiteInvalidDataException( |
||
197 | 'path', |
||
198 | $this->l10n->t('The website\'s path is stored on a non-local storage.') |
||
199 | ); |
||
200 | } |
||
201 | } catch (NotFoundException $e) { |
||
202 | if (!$websiteBaseFolder->isLocal()) { |
||
203 | throw new WebsiteInvalidDataException( |
||
204 | 'path', |
||
205 | $this->l10n->t('The website\'s path is stored on a non-local storage.') |
||
206 | ); |
||
207 | } |
||
208 | } |
||
209 | } catch (InvalidPathException | NotFoundException $e) { |
||
210 | throw new WebsiteInvalidDataException( |
||
211 | 'path', |
||
212 | $this->l10n->t('Parent folder of the website\'s path not found.') |
||
213 | ); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @throws ThemeNotFoundException |
||
219 | * @throws ThemeNotCompatibleException |
||
220 | */ |
||
221 | public function assertValidTheme(): void |
||
222 | { |
||
223 | $this->themesService->assertValidTheme($this->getTheme()); |
||
224 | } |
||
225 | 3 | ||
226 | /** |
||
227 | 3 | * @throws TemplateNotFoundException |
|
228 | 3 | * @throws TemplateNotCompatibleException |
|
229 | */ |
||
230 | public function assertValidTemplate(): void |
||
231 | 3 | { |
|
232 | $this->templatesService->assertValidTemplate($this->getTemplateSource()); |
||
233 | } |
||
234 | 3 | ||
235 | /** |
||
236 | * @param string $userId |
||
237 | 3 | * |
|
238 | * @throws WebsiteForeignOwnerException |
||
239 | */ |
||
240 | public function assertOwnedBy(string $userId): void |
||
241 | { |
||
242 | 3 | if ($this->getUserId() !== $userId) { |
|
243 | throw new WebsiteForeignOwnerException(); |
||
244 | 3 | } |
|
245 | } |
||
246 | |||
247 | 3 | /** |
|
248 | * @return StorageFolder |
||
249 | * @throws WebsiteInvalidFilesystemException |
||
250 | 3 | */ |
|
251 | public function getWebsiteFolder(): StorageFolder |
||
252 | { |
||
253 | if ($this->folder !== null) { |
||
254 | try { |
||
255 | 3 | // NC doesn't guarantee that mounts are present for the whole request lifetime |
|
256 | // for example, if you call \OC\Files\Utils\Scanner::scan(), all mounts are reset |
||
257 | 3 | // this makes OCNode instances, which rely on mounts of different users than the current, unusable |
|
258 | // by calling OCFolder::get('') we can detect this situation and re-init the required mounts |
||
259 | $this->folder->get(''); |
||
260 | } catch (\Exception $e) { |
||
261 | 3 | $this->folder = null; |
|
262 | } |
||
263 | } |
||
264 | |||
265 | 3 | if ($this->folder === null) { |
|
266 | try { |
||
267 | $ocUserFolder = \OC::$server->getUserFolder($this->getUserId()); |
||
268 | $userFolder = new StorageFolder($ocUserFolder); |
||
269 | 3 | ||
270 | $websiteFolder = $userFolder->getFolder($this->getPath()); |
||
271 | $this->folder = $websiteFolder->fakeRoot(); |
||
272 | } catch (InvalidPathException | NotFoundException $e) { |
||
273 | throw new WebsiteInvalidFilesystemException($e); |
||
274 | 3 | } |
|
275 | } |
||
276 | |||
277 | 3 | return $this->folder; |
|
278 | 3 | } |
|
279 | 3 | ||
280 | /** |
||
281 | * @return string |
||
282 | * @throws WebsiteInvalidFilesystemException |
||
283 | */ |
||
284 | public function getWebsitePath(): string |
||
290 | } |
||
291 | 3 | } |
|
292 | |||
293 | /** |
||
294 | 3 | * @return string |
|
295 | */ |
||
296 | public function getWebsiteUrl(bool $proxyRequest = false): string |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.