template.php 8.29 KB
Newer Older
1
2
<?php
/**
3
 * ownCloud
4
 *
5
6
7
 * @author Frank Karlitschek
 * @author Jakob Sack
 * @copyright 2012 Frank Karlitschek frank@owncloud.org
8
 *
9
10
11
12
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
 *
19
20
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
22
 *
 */
23

24
require_once __DIR__.'/template/functions.php';
25

26
/**
27
 * This class provides the templates for ownCloud.
28
 */
29
class OC_Template extends \OC\Template\Base {
30
	private $renderas; // Create a full page?
31
	private $path; // The path to the template
32
	private $headers = array(); //custom headers
33
	protected $app; // app id
34

35
	/**
36
	 * Constructor
Robin Appelman's avatar
Robin Appelman committed
37
	 * @param string $app app providing the template
38
	 * @param string $name of the template file (without suffix)
Robin Appelman's avatar
Robin Appelman committed
39
	 * @param string $renderas = ""; produce a full page
40
	 * @param bool $registerCall = true
Robin Appelman's avatar
Robin Appelman committed
41
	 * @return OC_Template object
42
	 *
Jakob Sack's avatar
Jakob Sack committed
43
	 * This function creates an OC_Template object.
44
	 *
Jakob Sack's avatar
Jakob Sack committed
45
	 * If $renderas is set, OC_Template will try to produce a full page in the
46
47
48
	 * according layout. For now, renderas can be set to "guest", "user" or
	 * "admin".
	 */
49
	public function __construct( $app, $name, $renderas = "", $registerCall = true ) {
50
51
52
		// Read the selected theme from the config file
		$theme = OC_Util::getTheme();

53
		$requesttoken = (OC::$server->getSession() and $registerCall) ? OC_Util::callRegister() : '';
54
55

		$parts = explode('/', $app); // fix translation when app is something like core/lostpassword
56
		$l10n = \OC::$server->getL10N($parts[0]);
57
		$themeDefaults = new OC_Defaults();
58

59
		list($path, $template) = $this->findTemplate($theme, $app, $name);
60

61
62
		// Set the private data
		$this->renderas = $renderas;
63
		$this->path = $path;
64
		$this->app = $app;
65

66
		parent::__construct($template, $requesttoken, $l10n, $themeDefaults);
Lukas Reschke's avatar
Lukas Reschke committed
67
68
	}

69
	/**
70
	 * find the template with the given name
Robin Appelman's avatar
Robin Appelman committed
71
	 * @param string $name of the template file (without suffix)
72
	 *
73
	 * Will select the template file for the selected theme.
74
	 * Checking all the possible locations.
75
76
	 * @param string $theme
	 * @param string $app
Lukas Reschke's avatar
Lukas Reschke committed
77
	 * @return array
78
	 */
79
	protected function findTemplate($theme, $app, $name) {
80
		// Check if it is a app template or not.
Bart Visscher's avatar
Bart Visscher committed
81
		if( $app !== '' ) {
82
83
84
			$dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
		} else {
			$dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
85
		}
86
		$locator = new \OC\Template\TemplateFileLocator( $dirs );
87
88
89
		$template = $locator->find($name);
		$path = $locator->getPath();
		return array($path, $template);
90
	}
91

92
	/**
93
	 * Add a custom element to the header
Robin Appelman's avatar
Robin Appelman committed
94
	 * @param string $tag tag name of the element
95
	 * @param array $attributes array of attributes for the element
96
97
	 * @param string $text the text content for the element. If $text is null then the
	 * element will be written as empty element. So use "" to get a closing tag.
98
	 */
99
100
101
102
103
104
	public function addHeader($tag, $attributes, $text=null) {
		$this->headers[]= array(
			'tag' => $tag,
			'attributes' => $attributes,
			'text' => $text
		);
105
	}
106

107
	/**
108
	 * Process the template
109
	 * @return boolean|string
110
	 *
111
	 * This function process the template. If $this->renderas is set, it
112
113
	 * will produce a full page.
	 */
Thomas Mueller's avatar
Thomas Mueller committed
114
	public function fetchPage() {
115
		$data = parent::fetchPage();
116

Thomas Mueller's avatar
Thomas Mueller committed
117
		if( $this->renderas ) {
118
			$page = new OC_TemplateLayout($this->renderas, $this->app);
119

120
			// Add custom headers
121
			$headers = '';
Thomas Mueller's avatar
Thomas Mueller committed
122
			foreach(OC_Util::$headers as $header) {
123
124
				$headers .= '<'.OC_Util::sanitizeHTML($header['tag']);
				foreach($header['attributes'] as $name=>$value) {
125
					$headers .= ' '.OC_Util::sanitizeHTML($name).'="'.OC_Util::sanitizeHTML($value).'"';
126
127
128
129
130
131
				}
				if ($header['text'] !== null) {
					$headers .= '>'.OC_Util::sanitizeHTML($header['text']).'</'.OC_Util::sanitizeHTML($header['tag']).'>';
				} else {
					$headers .= '/>';
				}
132
			}
133

134
135
136
			$page->assign('headers', $headers, false);

			$page->assign('content', $data, false );
137
138
			return $page->fetchPage();
		}
Jakob Sack's avatar
Jakob Sack committed
139
		else{
140
141
142
143
			return $data;
		}
	}

144
	/**
145
	 * Include template
Robin Appelman's avatar
Robin Appelman committed
146
	 * @return string returns content of included template
147
148
149
150
	 *
	 * Includes another template. use <?php echo $this->inc('template'); ?> to
	 * do this.
	 */
151
152
	public function inc( $file, $additionalParams = null ) {
		return $this->load($this->path.$file.'.php', $additionalParams);
153
154
	}

155
	/**
156
	 * Shortcut to print a simple page for users
Robin Appelman's avatar
Robin Appelman committed
157
158
159
	 * @param string $application The application we render the template for
	 * @param string $name Name of the template
	 * @param array $parameters Parameters for the template
160
	 * @return boolean|null
161
	 */
Thomas Mueller's avatar
Thomas Mueller committed
162
	public static function printUserPage( $application, $name, $parameters = array() ) {
Jakob Sack's avatar
Jakob Sack committed
163
		$content = new OC_Template( $application, $name, "user" );
Thomas Mueller's avatar
Thomas Mueller committed
164
		foreach( $parameters as $key => $value ) {
Lukas Reschke's avatar
Lukas Reschke committed
165
			$content->assign( $key, $value );
Jakob Sack's avatar
Jakob Sack committed
166
		}
167
		print $content->printPage();
168
169
170
	}

	/**
171
	 * Shortcut to print a simple page for admins
Robin Appelman's avatar
Robin Appelman committed
172
173
174
175
	 * @param string $application The application we render the template for
	 * @param string $name Name of the template
	 * @param array $parameters Parameters for the template
	 * @return bool
176
	 */
Thomas Mueller's avatar
Thomas Mueller committed
177
	public static function printAdminPage( $application, $name, $parameters = array() ) {
Jakob Sack's avatar
Jakob Sack committed
178
		$content = new OC_Template( $application, $name, "admin" );
Thomas Mueller's avatar
Thomas Mueller committed
179
		foreach( $parameters as $key => $value ) {
Lukas Reschke's avatar
Lukas Reschke committed
180
			$content->assign( $key, $value );
Jakob Sack's avatar
Jakob Sack committed
181
		}
182
183
184
185
		return $content->printPage();
	}

	/**
186
	 * Shortcut to print a simple page for guests
Robin Appelman's avatar
Robin Appelman committed
187
188
	 * @param string $application The application we render the template for
	 * @param string $name Name of the template
Lukas Reschke's avatar
Lukas Reschke committed
189
	 * @param array|string $parameters Parameters for the template
Robin Appelman's avatar
Robin Appelman committed
190
	 * @return bool
191
	 */
Thomas Mueller's avatar
Thomas Mueller committed
192
	public static function printGuestPage( $application, $name, $parameters = array() ) {
Jakob Sack's avatar
Jakob Sack committed
193
		$content = new OC_Template( $application, $name, "guest" );
Thomas Mueller's avatar
Thomas Mueller committed
194
		foreach( $parameters as $key => $value ) {
Lukas Reschke's avatar
Lukas Reschke committed
195
			$content->assign( $key, $value );
Jakob Sack's avatar
Jakob Sack committed
196
		}
197
198
		return $content->printPage();
	}
199

200
	/**
201
		* Print a fatal error page and terminates the script
202
		* @param string $error_msg The error message to show
203
		* @param string $hint An optional hint message - needs to be properly escaped
204
205
		*/
	public static function printErrorPage( $error_msg, $hint = '' ) {
206
		$content = new \OC_Template( '', 'error', 'error', false );
207
		$errors = array(array('error' => $error_msg, 'hint' => $hint));
Lukas Reschke's avatar
Lukas Reschke committed
208
		$content->assign( 'errors', $errors );
209
		$content->printPage();
210
211
		die();
	}
212

213
214
215
216
217
	/**
	 * print error page using Exception details
	 * @param Exception $exception
	 */
	public static function printExceptionErrorPage(Exception $exception) {
218
		$request = \OC::$server->getRequest();
219
		$content = new \OC_Template('', 'exception', 'error', false);
220
		$content->assign('errorClass', get_class($exception));
221
222
223
224
225
226
		$content->assign('errorMsg', $exception->getMessage());
		$content->assign('errorCode', $exception->getCode());
		$content->assign('file', $exception->getFile());
		$content->assign('line', $exception->getLine());
		$content->assign('trace', $exception->getTraceAsString());
		$content->assign('debugMode', defined('DEBUG') && DEBUG === true);
227
228
		$content->assign('remoteAddr', $request->getRemoteAddress());
		$content->assign('requestID', $request->getId());
229
230
		$content->printPage();
		die();
231
	}
232
233
234
235
236
237

	/**
	 * @return bool
	 */
	public static function isAssetPipelineEnabled() {
		// asset management enabled?
238
239
		$config = \OC::$server->getConfig();
		$useAssetPipeline = $config->getSystemValue('asset-pipeline.enabled', false);
240
241
242
243
244
		if (!$useAssetPipeline) {
			return false;
		}

		// assets folder exists?
245
		$assetDir = $config->getSystemValue('assetdirectory', \OC::$SERVERROOT) . '/assets';
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
		if (!is_dir($assetDir)) {
			if (!mkdir($assetDir)) {
				\OCP\Util::writeLog('assets',
					"Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
				return false;
			}
		}

		// assets folder can be accessed?
		if (!touch($assetDir."/.oc")) {
			\OCP\Util::writeLog('assets',
				"Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
			return false;
		}
		return $useAssetPipeline;
	}

263
}