diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2013-09-25 13:36:30 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2013-09-30 16:36:59 +0200 |
commit | 9c9dc276b7a1d2592c4fb0a887888632dc1f1e29 (patch) | |
tree | bbe3aed3e09c31c68806bdb8acffef70ba08f51c /lib/private/template | |
parent | a711399e62d5a9f14d4b748efe4354ee37e61f13 (diff) | |
download | nextcloud-server-9c9dc276b7a1d2592c4fb0a887888632dc1f1e29.tar.gz nextcloud-server-9c9dc276b7a1d2592c4fb0a887888632dc1f1e29.zip |
move the private namespace OC into lib/private - OCP will stay in lib/public
Conflicts:
lib/private/vcategories.php
Diffstat (limited to 'lib/private/template')
-rw-r--r-- | lib/private/template/base.php | 134 | ||||
-rw-r--r-- | lib/private/template/cssresourcelocator.php | 43 | ||||
-rw-r--r-- | lib/private/template/functions.php | 134 | ||||
-rw-r--r-- | lib/private/template/jsresourcelocator.php | 43 | ||||
-rw-r--r-- | lib/private/template/resourcelocator.php | 70 | ||||
-rw-r--r-- | lib/private/template/templatefilelocator.php | 44 |
6 files changed, 468 insertions, 0 deletions
diff --git a/lib/private/template/base.php b/lib/private/template/base.php new file mode 100644 index 00000000000..88941bc7132 --- /dev/null +++ b/lib/private/template/base.php @@ -0,0 +1,134 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class Base { + private $template; // The template + private $vars; // Vars + private $l10n; // The l10n-Object + private $theme; // theme defaults + + public function __construct( $template, $requesttoken, $l10n, $theme ) { + $this->vars = array(); + $this->vars['requesttoken'] = $requesttoken; + $this->l10n = $l10n; + $this->template = $template; + $this->theme = $theme; + } + + protected function getAppTemplateDirs($theme, $app, $serverroot, $app_dir) { + // Check if the app is in the app folder or in the root + if( file_exists($app_dir.'/templates/' )) { + return array( + $serverroot.'/themes/'.$theme.'/apps/'.$app.'/templates/', + $app_dir.'/templates/', + ); + } + return array( + $serverroot.'/themes/'.$theme.'/'.$app.'/templates/', + $serverroot.'/'.$app.'/templates/', + ); + } + + protected function getCoreTemplateDirs($theme, $serverroot) { + return array( + $serverroot.'/themes/'.$theme.'/core/templates/', + $serverroot.'/core/templates/', + ); + } + + /** + * @brief Assign variables + * @param string $key key + * @param string $value value + * @return bool + * + * This function assigns a variable. It can be accessed via $_[$key] in + * the template. + * + * If the key existed before, it will be overwritten + */ + public function assign( $key, $value) { + $this->vars[$key] = $value; + return true; + } + + /** + * @brief Appends a variable + * @param string $key key + * @param string $value value + * @return bool + * + * This function assigns a variable in an array context. If the key already + * exists, the value will be appended. It can be accessed via + * $_[$key][$position] in the template. + */ + public function append( $key, $value ) { + if( array_key_exists( $key, $this->vars )) { + $this->vars[$key][] = $value; + } + else{ + $this->vars[$key] = array( $value ); + } + } + + /** + * @brief Prints the proceeded template + * @return bool + * + * This function proceeds the template and prints its output. + */ + public function printPage() { + $data = $this->fetchPage(); + if( $data === false ) { + return false; + } + else{ + print $data; + return true; + } + } + + /** + * @brief Process the template + * @return bool + * + * This function processes the template. + */ + public function fetchPage() { + return $this->load($this->template); + } + + /** + * @brief doing the actual work + * @return string content + * + * Includes the template file, fetches its output + */ + protected function load( $file, $additionalparams = null ) { + // Register the variables + $_ = $this->vars; + $l = $this->l10n; + $theme = $this->theme; + + if( !is_null($additionalparams)) { + $_ = array_merge( $additionalparams, $this->vars ); + } + + // Include + ob_start(); + include $file; + $data = ob_get_contents(); + @ob_end_clean(); + + // Return data + return $data; + } + +} diff --git a/lib/private/template/cssresourcelocator.php b/lib/private/template/cssresourcelocator.php new file mode 100644 index 00000000000..8e7831ca549 --- /dev/null +++ b/lib/private/template/cssresourcelocator.php @@ -0,0 +1,43 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class CSSResourceLocator extends ResourceLocator { + public function doFind( $style ) { + if (strpos($style, '3rdparty') === 0 + && $this->appendIfExist($this->thirdpartyroot, $style.'.css') + || $this->appendIfExist($this->serverroot, $style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $style.'.css') + || $this->appendIfExist($this->serverroot, 'core/'.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') + ) { + return; + } + $app = substr($style, 0, strpos($style, '/')); + $style = substr($style, strpos($style, '/')+1); + $app_path = \OC_App::getAppPath($app); + $app_url = $this->webroot . '/index.php/apps/' . $app; + if ($this->appendIfExist($app_path, $style.$this->form_factor.'.css', $app_url) + || $this->appendIfExist($app_path, $style.'.css', $app_url) + ) { + return; + } + throw new \Exception('css file not found: style:'.$style); + } + + public function doFindTheme( $style ) { + $theme_dir = 'themes/'.$this->theme.'/'; + $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); + } +} diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php new file mode 100644 index 00000000000..501f8081bff --- /dev/null +++ b/lib/private/template/functions.php @@ -0,0 +1,134 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +/** + * Prints an XSS escaped string + * @param string $string the string which will be escaped and printed + */ +function p($string) { + print(OC_Util::sanitizeHTML($string)); +} + +/** + * Prints an unescaped string + * @param string $string the string which will be printed as it is + */ +function print_unescaped($string) { + print($string); +} + +/** + * @brief make OC_Helper::linkTo available as a simple function + * @param string $app app + * @param string $file file + * @param array $args array with param=>value, will be appended to the returned url + * @return string link to the file + * + * For further information have a look at OC_Helper::linkTo + */ +function link_to( $app, $file, $args = array() ) { + return OC_Helper::linkTo( $app, $file, $args ); +} + +/** + * @brief make OC_Helper::imagePath available as a simple function + * @param string $app app + * @param string $image image + * @return string link to the image + * + * For further information have a look at OC_Helper::imagePath + */ +function image_path( $app, $image ) { + return OC_Helper::imagePath( $app, $image ); +} + +/** + * @brief make OC_Helper::mimetypeIcon available as a simple function + * @param string $mimetype mimetype + * @return string link to the image + * + * For further information have a look at OC_Helper::mimetypeIcon + */ +function mimetype_icon( $mimetype ) { + return OC_Helper::mimetypeIcon( $mimetype ); +} + +/** + * @brief make preview_icon available as a simple function + * Returns the path to the preview of the image. + * @param $path path of file + * @returns link to the preview + * + * For further information have a look at OC_Helper::previewIcon + */ +function preview_icon( $path ) { + return OC_Helper::previewIcon( $path ); +} + +function publicPreview_icon ( $path, $token ) { + return OC_Helper::publicPreviewIcon( $path, $token ); +} + +/** + * @brief make OC_Helper::humanFileSize available as a simple function + * @param int $bytes size in bytes + * @return string size as string + * + * For further information have a look at OC_Helper::humanFileSize + */ +function human_file_size( $bytes ) { + return OC_Helper::humanFileSize( $bytes ); +} + +function relative_modified_date($timestamp) { + $l=OC_L10N::get('lib'); + $timediff = time() - $timestamp; + $diffminutes = round($timediff/60); + $diffhours = round($diffminutes/60); + $diffdays = round($diffhours/24); + $diffmonths = round($diffdays/31); + + if($timediff < 60) { return $l->t('seconds ago'); } + else if($timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); } + else if($timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); } + else if((date('G')-$diffhours) > 0) { return $l->t('today'); } + else if((date('G')-$diffhours) > -24) { return $l->t('yesterday'); } + else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); } + else if($timediff < 5184000) { return $l->t('last month'); } + else if((date('n')-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); } + else if($timediff < 63113852) { return $l->t('last year'); } + else { return $l->t('years ago'); } +} + +function html_select_options($options, $selected, $params=array()) { + if (!is_array($selected)) { + $selected=array($selected); + } + if (isset($params['combine']) && $params['combine']) { + $options = array_combine($options, $options); + } + $value_name = $label_name = false; + if (isset($params['value'])) { + $value_name = $params['value']; + } + if (isset($params['label'])) { + $label_name = $params['label']; + } + $html = ''; + foreach($options as $value => $label) { + if ($value_name && is_array($label)) { + $value = $label[$value_name]; + } + if ($label_name && is_array($label)) { + $label = $label[$label_name]; + } + $select = in_array($value, $selected) ? ' selected="selected"' : ''; + $html .= '<option value="' . OC_Util::sanitizeHTML($value) . '"' . $select . '>' . OC_Util::sanitizeHTML($label) . '</option>'."\n"; + } + return $html; +} diff --git a/lib/private/template/jsresourcelocator.php b/lib/private/template/jsresourcelocator.php new file mode 100644 index 00000000000..f8fe3817ce6 --- /dev/null +++ b/lib/private/template/jsresourcelocator.php @@ -0,0 +1,43 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class JSResourceLocator extends ResourceLocator { + public function doFind( $script ) { + $theme_dir = 'themes/'.$this->theme.'/'; + if (strpos($script, '3rdparty') === 0 + && $this->appendIfExist($this->thirdpartyroot, $script.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js') + || $this->appendIfExist($this->serverroot, $script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $script.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js') + || $this->appendIfExist($this->serverroot, 'core/'.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js') + ) { + return; + } + $app = substr($script, 0, strpos($script, '/')); + $script = substr($script, strpos($script, '/')+1); + $app_path = \OC_App::getAppPath($app); + $app_url = \OC_App::getAppWebPath($app); + if ($this->appendIfExist($app_path, $script.$this->form_factor.'.js', $app_url) + || $this->appendIfExist($app_path, $script.'.js', $app_url) + ) { + return; + } + throw new \Exception('js file not found: script:'.$script); + } + + public function doFindTheme( $script ) { + } +} diff --git a/lib/private/template/resourcelocator.php b/lib/private/template/resourcelocator.php new file mode 100644 index 00000000000..9f83673664d --- /dev/null +++ b/lib/private/template/resourcelocator.php @@ -0,0 +1,70 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +abstract class ResourceLocator { + protected $theme; + protected $form_factor; + + protected $mapping; + protected $serverroot; + protected $thirdpartyroot; + protected $webroot; + + protected $resources = array(); + + public function __construct( $theme, $form_factor, $core_map, $party_map ) { + $this->theme = $theme; + $this->form_factor = $form_factor; + $this->mapping = $core_map + $party_map; + $this->serverroot = key($core_map); + $this->thirdpartyroot = key($party_map); + $this->webroot = $this->mapping[$this->serverroot]; + } + + abstract public function doFind( $resource ); + abstract public function doFindTheme( $resource ); + + public function find( $resources ) { + try { + foreach($resources as $resource) { + $this->doFind($resource); + } + if (!empty($this->theme)) { + foreach($resources as $resource) { + $this->doFindTheme($resource); + } + } + } catch (\Exception $e) { + throw new \Exception($e->getMessage().' formfactor:'.$this->form_factor + .' serverroot:'.$this->serverroot); + } + } + + /* + * @brief append the $file resource if exist at $root + * @param $root path to check + * @param $file the filename + * @param $web base for path, default map $root to $webroot + */ + protected function appendIfExist($root, $file, $webroot = null) { + if (is_file($root.'/'.$file)) { + if (!$webroot) { + $webroot = $this->mapping[$root]; + } + $this->resources[] = array($root, $webroot, $file); + return true; + } + return false; + } + + public function getResources() { + return $this->resources; + } +} diff --git a/lib/private/template/templatefilelocator.php b/lib/private/template/templatefilelocator.php new file mode 100644 index 00000000000..d5a484b1a14 --- /dev/null +++ b/lib/private/template/templatefilelocator.php @@ -0,0 +1,44 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class TemplateFileLocator { + protected $form_factor; + protected $dirs; + private $path; + + public function __construct( $form_factor, $dirs ) { + $this->form_factor = $form_factor; + $this->dirs = $dirs; + } + + public function find( $template ) { + if ($template === '') { + throw new \InvalidArgumentException('Empty template name'); + } + + foreach($this->dirs as $dir) { + $file = $dir.$template.$this->form_factor.'.php'; + if (is_file($file)) { + $this->path = $dir; + return $file; + } + $file = $dir.$template.'.php'; + if (is_file($file)) { + $this->path = $dir; + return $file; + } + } + throw new \Exception('template file not found: template:'.$template.' formfactor:'.$this->form_factor); + } + + public function getPath() { + return $this->path; + } +} |