Passed
Push — master ( e5d2e8...4f6533 )
by Maxence
07:45 queued 05:02
created

PicoService::setupPico()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use Exception;
30
use OC\App\AppManager;
31
use OCA\CMSPico\AppInfo\Application;
32
use OCA\CMSPico\Exceptions\AssetDoesNotExistException;
33
use OCA\CMSPico\Exceptions\PicoRuntimeException;
34
use OCA\CMSPico\Exceptions\WebsiteIsPrivateException;
35
use OCA\CMSPico\Model\Website;
36
use OCA\CMSPico\Pico;
37
use OCP\Files\File;
0 ignored issues
show
Bug introduced by
The type OCP\Files\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
use OCP\Files\IRootFolder;
39
40
class PicoService {
41
42
	const DIR_CONFIG = 'Pico/config/';
43
	const DIR_PLUGINS = 'Pico/plugins/';
44
	const DIR_THEMES = 'Pico/themes/';
45
46
	const DIR_ASSETS = 'assets/';
47
48
	private $userId;
49
50
	/** @var AppManager */
51
	private $appManager;
52
53
	/** @var IRootFolder */
54
	private $rootFolder;
55
56
	/** @var ThemesService */
57
	private $themesService;
58
59
	/** @var MiscService */
60
	private $miscService;
61
62
	/**
63
	 * PicoService constructor.
64
	 *
65
	 * @param string $userId
66
	 * @param AppManager $appManager
67
	 * @param IRootFolder $rootFolder
68
	 * @param ThemesService $themesService
69
	 * @param MiscService $miscService
70
	 */
71
	function __construct(
72
		$userId, AppManager $appManager, IRootFolder $rootFolder, ThemesService $themesService,
73
		MiscService $miscService
74
	) {
75
		$this->userId = $userId;
76
		$this->appManager = $appManager;
77
		$this->rootFolder = $rootFolder;
78
		$this->themesService = $themesService;
79
		$this->miscService = $miscService;
80
	}
81
82
83
	/**
84
	 * getContent();
85
	 *
86
	 * @param Website $website
87
	 *
88
	 * @return string
89
	 */
90
	public function getContent(Website $website) {
91
92
		if (strpos($website->getPage(), self::DIR_ASSETS) === 0) {
93
			return $this->getContentFromAssets(
94
				$website, substr($website->getPage(), strlen(self::DIR_ASSETS))
95
			);
96
		} else {
97
			return $this->getContentFromPico($website);
98
		}
99
	}
100
101
102
	/**
103
	 * @param Website $website
104
	 * @param $asset
105
	 *
106
	 * @return string
107
	 * @throws AssetDoesNotExistException
108
	 * @throws WebsiteIsPrivateException
109
	 */
110
	public function getContentFromAssets(Website $website, $asset) {
111
		$website->pathCantContainSpecificFolders($asset);
112
113
		try {
114
			$website->viewerMustHaveAccess(self::DIR_ASSETS . $asset);
115
			$userFolder = $this->rootFolder->getUserFolder($website->getUserId());
116
117
			/** @var File $file */
118
			$file = $userFolder->get($website->getPath() . self::DIR_ASSETS . $asset);
119
			$content = $file->getContent();
120
121
			header('Content-type: ' . $file->getMimeType());
122
123
			return $content;
124
		} catch (WebsiteIsPrivateException $e) {
125
			throw $e;
126
		} catch (Exception $e) {
127
			throw new AssetDoesNotExistException("404");
128
		}
129
	}
130
131
132
	/**
133
	 * getContentFromPico();
134
	 *
135
	 * main method that will create a Pico object, feed it with settings and get the content to be
136
	 * displayed.
137
	 * We check that the Nextcloud plugin is loaded, that the content location is a valid directory.
138
	 * In case of a private page, we check the viewer have a read access to the source files.
139
	 *
140
	 * @param Website $website
141
	 *
142
	 * @return string
143
	 * @throws PicoRuntimeException
144
	 */
145
	public function getContentFromPico(Website $website) {
146
147
		$appPath = MiscService::endSlash($this->appManager->getAppPath(Application::APP_NAME));
148
		$pico = new Pico(
149
			$website->getAbsolutePath(),
150
			$appPath . self::DIR_CONFIG,
151
			$appPath . self::DIR_PLUGINS,
152
			$appPath . self::DIR_THEMES
153
		);
154
155
		$this->setupPico($pico, $website);
156
		try {
157
			$content = $pico->run();
158
		} catch (Exception $e) {
159
			throw new PicoRuntimeException($e->getMessage());
160
		}
161
162
		$absolutePath = $this->getAbsolutePathFromPico($pico);
163
		$website->contentMustBeLocal($absolutePath);
164
165
		$website->viewerMustHaveAccess($website->getRelativePath($absolutePath), $pico->getFileMeta());
166
167
		return $content;
168
	}
169
170
171
	/**
172
	 * @param Pico $pico
173
	 * @param Website $website
174
	 */
175
	private function setupPico(Pico $pico, Website $website) {
176
		$pico->setRequestedUrl($website->getPage());
0 ignored issues
show
Bug introduced by
The method setRequestedUrl() does not exist on OCA\CMSPico\Pico. Did you maybe mean setRequestUrl()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
		$pico->/** @scrutinizer ignore-call */ setRequestedUrl($website->getPage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
178
		$this->themesService->hasToBeAValidTheme($website->getTheme());
179
180
		$appBaseUrl = \OC::$WEBROOT . '/index.php/apps/' . Application::APP_NAME;
181
		$pico->setConfig(
182
			[
183
				'site_title'     => $website->getName(),
184
				'base_url'       => $appBaseUrl . '/pico/' . $website->getSite(),
185
				'theme'          => $website->getTheme(),
186
				'content_dir'    => 'content/',
187
				'content_ext'    => '.md',
188
				'nextcloud_site' => $website->getSite()
189
			]
190
		);
191
	}
192
193
194
	/**
195
	 * @param Pico $pico
196
	 *
197
	 * @return string
198
	 */
199
	private function getAbsolutePathFromPico(Pico $pico) {
200
		return $pico->getRequestFile() ?: '';
201
	}
202
}
203