Completed
Push — master ( 0cff70...dba08f )
by Morris
09:32
created

Base::load()   B

Complexity

Conditions 3
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 3
eloc 15
nc 6
nop 2
1
<?php
2
/**
3
 * @author Bart Visscher <[email protected]>
4
 * @author Björn Schießle <[email protected]>
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, ownCloud, Inc.
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
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, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Template;
28
29
class Base {
30
	private $template; // The template
31
	private $vars; // Vars
32
33
	/** @var \OCP\IL10N */
34
	private $l10n;
35
36
	/** @var \OC_Defaults */
37
	private $theme;
38
39
	/**
40
	 * @param string $template
41
	 * @param string $requestToken
42
	 * @param \OCP\IL10N $l10n
43
	 * @param \OC_Defaults $theme
44
	 */
45
	public function __construct($template, $requestToken, $l10n, $theme ) {
46
		$this->vars = array();
47
		$this->vars['requesttoken'] = $requestToken;
48
		$this->l10n = $l10n;
49
		$this->template = $template;
50
		$this->theme = $theme;
51
	}
52
53
	/**
54
	 * @param string $serverRoot
55
	 * @param string|false $app_dir
56
	 * @param string $theme
57
	 * @param string $app
58
	 * @return string[]
59
	 */
60
	protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
61
		// Check if the app is in the app folder or in the root
62
		if( file_exists($app_dir.'/templates/' )) {
63
			return [
64
				$serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
65
				$app_dir.'/templates/',
66
			];
67
		}
68
		return [
69
			$serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
70
			$serverRoot.'/'.$app.'/templates/',
71
		];
72
	}
73
74
	/**
75
	 * @param string $serverRoot
76
	 * @param string $theme
77
	 * @return string[]
78
	 */
79
	protected function getCoreTemplateDirs($theme, $serverRoot) {
80
		return [
81
			$serverRoot.'/themes/'.$theme.'/core/templates/',
82
			$serverRoot.'/core/templates/',
83
		];
84
	}
85
86
	/**
87
	 * Assign variables
88
	 * @param string $key key
89
	 * @param array|bool|integer|string $value value
90
	 * @return bool
91
	 *
92
	 * This function assigns a variable. It can be accessed via $_[$key] in
93
	 * the template.
94
	 *
95
	 * If the key existed before, it will be overwritten
96
	 */
97
	public function assign( $key, $value) {
98
		$this->vars[$key] = $value;
99
		return true;
100
	}
101
102
	/**
103
	 * Appends a variable
104
	 * @param string $key key
105
	 * @param mixed $value value
106
	 * @return boolean|null
107
	 *
108
	 * This function assigns a variable in an array context. If the key already
109
	 * exists, the value will be appended. It can be accessed via
110
	 * $_[$key][$position] in the template.
111
	 */
112
	public function append( $key, $value ) {
113
		if( array_key_exists( $key, $this->vars )) {
114
			$this->vars[$key][] = $value;
115
		}
116
		else{
117
			$this->vars[$key] = array( $value );
118
		}
119
	}
120
121
	/**
122
	 * Prints the proceeded template
123
	 * @return bool
124
	 *
125
	 * This function proceeds the template and prints its output.
126
	 */
127
	public function printPage() {
128
		$data = $this->fetchPage();
129
		if( $data === false ) {
130
			return false;
131
		}
132
		else{
133
			print $data;
134
			return true;
135
		}
136
	}
137
138
	/**
139
	 * Process the template
140
	 *
141
	 * @param array|null $additionalParams
142
	 * @return string This function processes the template.
143
	 *
144
	 * This function processes the template.
145
	 */
146
	public function fetchPage($additionalParams = null) {
147
		return $this->load($this->template, $additionalParams);
148
	}
149
150
	/**
151
	 * doing the actual work
152
	 *
153
	 * @param string $file
154
	 * @param array|null $additionalParams
155
	 * @return string content
156
	 *
157
	 * Includes the template file, fetches its output
158
	 */
159
	protected function load($file, $additionalParams = null) {
160
		// Register the variables
161
		$_ = $this->vars;
162
		$l = $this->l10n;
163
		$theme = $this->theme;
164
165
		if( !is_null($additionalParams)) {
166
			$_ = array_merge( $additionalParams, $this->vars );
167
		}
168
169
		// Include
170
		ob_start();
171
		try {
172
			include $file;
173
			$data = ob_get_contents();
174
		} catch (\Exception $e) {
175
			@ob_end_clean();
176
			throw $e;
177
		}
178
		@ob_end_clean();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
179
180
		// Return data
181
		return $data;
182
	}
183
184
}
185