Passed
Push — master ( c2190f...d26a14 )
by Maxence
02:26
created

Pico::loadConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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 Daniel rudolf <www.daniel-rudolf.de>
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
namespace OCA\CMSPico;
27
28
use HTMLPurifier;
29
use HTMLPurifier_Config;
30
use OC;
31
use OC_App;
0 ignored issues
show
Bug introduced by
The type OC_App 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...
32
use OCA\CMSPico\AppInfo\Application;
33
34
class Pico extends \Pico {
35
	/** @var HTMLPurifier */
36
	protected $htmlPurifier;
37
38
	/**
39
	 * Loads the config.php from Pico::$configDir.
40
	 *
41
	 * We force enabled URL rewriting due to the support of Nextcloud's
42
	 * `PATH_INFO`-based routing method ({@see self::evaluateRequestUrl()}).
43
	 */
44
	protected function loadConfig() {
45
		parent::loadConfig();
46
47
		$this->config['rewrite_url'] = true;
48
49
		if (empty($this->config['nextcloud_site'])) {
50
			$this->config['nextcloud_site'] = 'default';
51
		}
52
	}
53
54
	/**
55
	 * Evaluates the requested URL.
56
	 *
57
	 * Besides Pico's built-in `QUERY_STRING`-based routing (e.g. `?sub/page`),
58
	 * we additionally fully support Nextcloud's `PATH_INFO`-based routing
59
	 * (e.g. `/index.php/apps/cms_pico/pico/sub/page`).
60
	 */
61
	protected function evaluateRequestUrl() {
62
		parent::evaluateRequestUrl();
63
64
		if (!$this->requestUrl) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->requestUrl of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
			$pathInfo = OC::$server->getRequest()->getRawPathInfo();
66
			if ($pathInfo) {
67
				$basePathInfo = '/apps/' . Application::APP_NAME . '/pico/' . $this->getConfig('nextcloud_site') . '/';
68
				$basePathInfoLength = strlen($basePathInfo);
69
				if (substr($pathInfo, 0, $basePathInfoLength) === $basePathInfo) {
70
					$this->requestUrl = substr($pathInfo, $basePathInfoLength);
71
					$this->requestUrl = trim($this->requestUrl, '/');
72
				}
73
			}
74
		}
75
	}
76
77
	/**
78
	 * Returns the parsed and purified file meta from raw file contents.
79
	 *
80
	 * @param  string   $rawContent
81
	 * @param  string[] $headers
82
	 * @return array
83
	 * @throws \Symfony\Component\Yaml\Exception\ParseException
84
	 */
85
	public function parseFileMeta($rawContent, array $headers) {
86
		$meta = parent::parseFileMeta($rawContent, $headers);
87
		return $this->purifyFileMeta($meta);
88
	}
89
90
	/**
91
	 * Purifies file meta.
92
	 *
93
	 * @param array $meta
94
	 * @return array
95
	 */
96
	protected function purifyFileMeta(array $meta) {
97
		$newMeta = [];
98
		foreach ($meta as $key => $value) {
99
			if (is_array($value)) {
100
				$newMeta[$key] = $this->purifyFileMeta($value);
101
			} else {
102
				$newMeta[$key] = $this->getHtmlPurifier()->purify($value);
103
			}
104
		}
105
106
		return $newMeta;
107
	}
108
109
	/**
110
	 * Returns the parsed and purified contents of a page.
111
	 *
112
	 * @param  string $markdown
113
	 * @return string
114
	 */
115
	public function parseFileContent($markdown) {
116
		$content = parent::parseFileContent($markdown);
117
		return $this->getHtmlPurifier()->purify($content);
118
	}
119
120
	/**
121
	 * Returns the variables passed to the template.
122
	 *
123
	 * Let Pico's `theme_url` point directly to the app's themes directory.
124
	 *
125
	 * @return array
126
	 */
127
	protected function getTwigVariables() {
128
		$twigVariables = parent::getTwigVariables();
129
		$twigVariables['theme_url'] = OC_App::getAppWebPath(Application::APP_NAME) . '/Pico/themes/' . $this->getConfig('theme');
130
		return $twigVariables;
131
	}
132
133
	/**
134
	 * Returns the HTMLPurifier instance.
135
	 *
136
	 * @return HTMLPurifier
137
	 */
138
	public function getHtmlPurifier() {
139
		if ($this->htmlPurifier === null) {
140
			$this->htmlPurifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
141
		}
142
143
		return $this->htmlPurifier;
144
	}
145
}
146