From eb79b8383107c71f8def60d195de09a1152af263 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 12 May 2016 13:32:02 +0200 Subject: Move functions.php to legacy This file should really be properly namespaced etc! --- lib/private/legacy/template.php | 2 +- lib/private/legacy/template/functions.php | 265 ++++++++++++++++++++++++++++++ lib/private/template/functions.php | 265 ------------------------------ tests/lib/testcase.php | 2 +- 4 files changed, 267 insertions(+), 267 deletions(-) create mode 100644 lib/private/legacy/template/functions.php delete mode 100644 lib/private/template/functions.php diff --git a/lib/private/legacy/template.php b/lib/private/legacy/template.php index 84b963e4ab6..73725529702 100644 --- a/lib/private/legacy/template.php +++ b/lib/private/legacy/template.php @@ -36,7 +36,7 @@ use OC\TemplateLayout; -require_once __DIR__.'/../template/functions.php'; +require_once __DIR__.'/template/functions.php'; /** * This class provides the templates for ownCloud. diff --git a/lib/private/legacy/template/functions.php b/lib/private/legacy/template/functions.php new file mode 100644 index 00000000000..a0540420e29 --- /dev/null +++ b/lib/private/legacy/template/functions.php @@ -0,0 +1,265 @@ + + * @author Bernhard Posselt + * @author Georg Ehrke + * @author Joas Schilling + * @author Jörn Friedrich Dreyer + * @author Lukas Reschke + * @author Morris Jobke + * @author Robin McCorkell + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +/** + * Prints a sanitized string + * @param string $string the string which will be escaped and printed + */ +function p($string) { + print(\OCP\Util::sanitizeHTML($string)); +} + +/** + * Prints an unsanitized string - usage of this function may result into XSS. + * Consider using p() instead. + * @param string|array $string the string which will be printed as it is + */ +function print_unescaped($string) { + print($string); +} + +/** + * Shortcut for adding scripts to a page + * @param string $app the appname + * @param string|string[] $file the filename, + * if an array is given it will add all scripts + */ +function script($app, $file = null) { + if(is_array($file)) { + foreach($file as $f) { + OC_Util::addScript($app, $f); + } + } else { + OC_Util::addScript($app, $file); + } +} + +/** + * Shortcut for adding vendor scripts to a page + * @param string $app the appname + * @param string|string[] $file the filename, + * if an array is given it will add all scripts + */ +function vendor_script($app, $file = null) { + if(is_array($file)) { + foreach($file as $f) { + OC_Util::addVendorScript($app, $f); + } + } else { + OC_Util::addVendorScript($app, $file); + } +} + +/** + * Shortcut for adding styles to a page + * @param string $app the appname + * @param string|string[] $file the filename, + * if an array is given it will add all styles + */ +function style($app, $file = null) { + if(is_array($file)) { + foreach($file as $f) { + OC_Util::addStyle($app, $f); + } + } else { + OC_Util::addStyle($app, $file); + } +} + +/** + * Shortcut for adding vendor styles to a page + * @param string $app the appname + * @param string|string[] $file the filename, + * if an array is given it will add all styles + */ +function vendor_style($app, $file = null) { + if(is_array($file)) { + foreach($file as $f) { + OC_Util::addVendorStyle($app, $f); + } + } else { + OC_Util::addVendorStyle($app, $file); + } +} + +/** + * Shortcut for adding translations to a page + * @param string $app the appname + * if an array is given it will add all styles + */ +function translation($app) { + OC_Util::addTranslations($app); +} + +/** + * Shortcut for HTML imports + * @param string $app the appname + * @param string|string[] $file the path relative to the app's component folder, + * if an array is given it will add all components + */ +function component($app, $file) { + if(is_array($file)) { + foreach($file as $f) { + $url = link_to($app, 'component/' . $f . '.html'); + OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); + } + } else { + $url = link_to($app, 'component/' . $file . '.html'); + OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); + } +} + +/** + * make \OCP\IURLGenerator::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 \OCP\IURLGenerator::linkTo + */ +function link_to( $app, $file, $args = array() ) { + return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); +} + +/** + * @param $key + * @return string url to the online documentation + */ +function link_to_docs($key) { + return \OC::$server->getURLGenerator()->linkToDocs($key); +} + +/** + * make \OCP\IURLGenerator::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 \OCP\IURLGenerator::imagePath + */ +function image_path( $app, $image ) { + return \OC::$server->getURLGenerator()->imagePath( $app, $image ); +} + +/** + * make OC_Helper::mimetypeIcon available as a simple function + * @param string $mimetype mimetype + * @return string link to the image + */ +function mimetype_icon( $mimetype ) { + return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype ); +} + +/** + * make preview_icon available as a simple function + * Returns the path to the preview of the image. + * @param string $path path of file + * @return link to the preview + */ +function preview_icon( $path ) { + return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_preview', ['x' => 32, 'y' => 32, 'file' => $path]); +} + +/** + * @param string $path + */ +function publicPreview_icon ( $path, $token ) { + return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_public_preview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]); +} + +/** + * 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 ); +} + +/** + * Strips the timestamp of its time value + * @param int $timestamp UNIX timestamp to strip + * @return $timestamp without time value + */ +function strip_time($timestamp){ + $date = new \DateTime("@{$timestamp}"); + $date->setTime(0, 0, 0); + return intval($date->format('U')); +} + +/** + * Formats timestamp relatively to the current time using + * a human-friendly format like "x minutes ago" or "yesterday" + * @param int $timestamp timestamp to format + * @param int $fromTime timestamp to compare from, defaults to current time + * @param bool $dateOnly whether to strip time information + * @return string timestamp + */ +function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) { + /** @var \OC\DateTimeFormatter $formatter */ + $formatter = \OC::$server->query('DateTimeFormatter'); + + if ($dateOnly){ + return $formatter->formatDateSpan($timestamp, $fromTime); + } + return $formatter->formatTimeSpan($timestamp, $fromTime); +} + +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 .= ''."\n"; + } + return $html; +} diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php deleted file mode 100644 index a0540420e29..00000000000 --- a/lib/private/template/functions.php +++ /dev/null @@ -1,265 +0,0 @@ - - * @author Bernhard Posselt - * @author Georg Ehrke - * @author Joas Schilling - * @author Jörn Friedrich Dreyer - * @author Lukas Reschke - * @author Morris Jobke - * @author Robin McCorkell - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -/** - * Prints a sanitized string - * @param string $string the string which will be escaped and printed - */ -function p($string) { - print(\OCP\Util::sanitizeHTML($string)); -} - -/** - * Prints an unsanitized string - usage of this function may result into XSS. - * Consider using p() instead. - * @param string|array $string the string which will be printed as it is - */ -function print_unescaped($string) { - print($string); -} - -/** - * Shortcut for adding scripts to a page - * @param string $app the appname - * @param string|string[] $file the filename, - * if an array is given it will add all scripts - */ -function script($app, $file = null) { - if(is_array($file)) { - foreach($file as $f) { - OC_Util::addScript($app, $f); - } - } else { - OC_Util::addScript($app, $file); - } -} - -/** - * Shortcut for adding vendor scripts to a page - * @param string $app the appname - * @param string|string[] $file the filename, - * if an array is given it will add all scripts - */ -function vendor_script($app, $file = null) { - if(is_array($file)) { - foreach($file as $f) { - OC_Util::addVendorScript($app, $f); - } - } else { - OC_Util::addVendorScript($app, $file); - } -} - -/** - * Shortcut for adding styles to a page - * @param string $app the appname - * @param string|string[] $file the filename, - * if an array is given it will add all styles - */ -function style($app, $file = null) { - if(is_array($file)) { - foreach($file as $f) { - OC_Util::addStyle($app, $f); - } - } else { - OC_Util::addStyle($app, $file); - } -} - -/** - * Shortcut for adding vendor styles to a page - * @param string $app the appname - * @param string|string[] $file the filename, - * if an array is given it will add all styles - */ -function vendor_style($app, $file = null) { - if(is_array($file)) { - foreach($file as $f) { - OC_Util::addVendorStyle($app, $f); - } - } else { - OC_Util::addVendorStyle($app, $file); - } -} - -/** - * Shortcut for adding translations to a page - * @param string $app the appname - * if an array is given it will add all styles - */ -function translation($app) { - OC_Util::addTranslations($app); -} - -/** - * Shortcut for HTML imports - * @param string $app the appname - * @param string|string[] $file the path relative to the app's component folder, - * if an array is given it will add all components - */ -function component($app, $file) { - if(is_array($file)) { - foreach($file as $f) { - $url = link_to($app, 'component/' . $f . '.html'); - OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); - } - } else { - $url = link_to($app, 'component/' . $file . '.html'); - OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); - } -} - -/** - * make \OCP\IURLGenerator::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 \OCP\IURLGenerator::linkTo - */ -function link_to( $app, $file, $args = array() ) { - return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); -} - -/** - * @param $key - * @return string url to the online documentation - */ -function link_to_docs($key) { - return \OC::$server->getURLGenerator()->linkToDocs($key); -} - -/** - * make \OCP\IURLGenerator::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 \OCP\IURLGenerator::imagePath - */ -function image_path( $app, $image ) { - return \OC::$server->getURLGenerator()->imagePath( $app, $image ); -} - -/** - * make OC_Helper::mimetypeIcon available as a simple function - * @param string $mimetype mimetype - * @return string link to the image - */ -function mimetype_icon( $mimetype ) { - return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype ); -} - -/** - * make preview_icon available as a simple function - * Returns the path to the preview of the image. - * @param string $path path of file - * @return link to the preview - */ -function preview_icon( $path ) { - return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_preview', ['x' => 32, 'y' => 32, 'file' => $path]); -} - -/** - * @param string $path - */ -function publicPreview_icon ( $path, $token ) { - return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_public_preview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]); -} - -/** - * 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 ); -} - -/** - * Strips the timestamp of its time value - * @param int $timestamp UNIX timestamp to strip - * @return $timestamp without time value - */ -function strip_time($timestamp){ - $date = new \DateTime("@{$timestamp}"); - $date->setTime(0, 0, 0); - return intval($date->format('U')); -} - -/** - * Formats timestamp relatively to the current time using - * a human-friendly format like "x minutes ago" or "yesterday" - * @param int $timestamp timestamp to format - * @param int $fromTime timestamp to compare from, defaults to current time - * @param bool $dateOnly whether to strip time information - * @return string timestamp - */ -function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) { - /** @var \OC\DateTimeFormatter $formatter */ - $formatter = \OC::$server->query('DateTimeFormatter'); - - if ($dateOnly){ - return $formatter->formatDateSpan($timestamp, $fromTime); - } - return $formatter->formatTimeSpan($timestamp, $fromTime); -} - -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 .= ''."\n"; - } - return $html; -} diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index d7573337f87..7ed121d3556 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -423,7 +423,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { */ protected function assertTemplate($expectedHtml, $template, $vars = []) { - require_once __DIR__.'/../../lib/private/template/functions.php'; + require_once __DIR__.'/../../lib/private/legacy/template/functions.php'; $requestToken = 12345; $theme = new OC_Defaults(); -- cgit v1.2.3