2013-03-18 20:36:52 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Michael Letzgus <www@chronos.michael-letzgus.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2015-02-26 13:37:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints a sanitized string
|
2015-11-23 14:11:23 +03:00
|
|
|
* @param string $string the string which will be escaped and printed
|
2013-03-18 20:36:52 +04:00
|
|
|
*/
|
|
|
|
function p($string) {
|
2015-12-08 10:28:15 +03:00
|
|
|
print(\OCP\Util::sanitizeHTML($string));
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
|
2017-05-20 13:16:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints a <link> tag for loading css
|
|
|
|
* @param string $href the source URL, ignored when empty
|
|
|
|
* @param string $opts, additional optional options
|
|
|
|
*/
|
|
|
|
function emit_css_tag($href, $opts = '') {
|
|
|
|
$s='<link rel="stylesheet"';
|
|
|
|
if (!empty($href)) {
|
|
|
|
$s.=' href="' . $href .'"';
|
|
|
|
}
|
|
|
|
if (!empty($opts)) {
|
|
|
|
$s.=' '.$opts;
|
|
|
|
}
|
|
|
|
print_unescaped($s.">\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints all tags for CSS loading
|
2017-07-19 20:44:10 +03:00
|
|
|
* @param array $obj all the script information from template
|
2017-05-20 13:16:44 +03:00
|
|
|
*/
|
|
|
|
function emit_css_loading_tags($obj) {
|
|
|
|
foreach($obj['cssfiles'] as $css) {
|
|
|
|
emit_css_tag($css);
|
|
|
|
}
|
|
|
|
foreach($obj['printcssfiles'] as $css) {
|
|
|
|
emit_css_tag($css, 'media="print"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 11:43:51 +03:00
|
|
|
/**
|
|
|
|
* Prints a <script> tag with nonce and defer depending on config
|
|
|
|
* @param string $src the source URL, ignored when empty
|
|
|
|
* @param string $script_content the inline script content, ignored when empty
|
|
|
|
*/
|
2017-05-20 13:16:44 +03:00
|
|
|
function emit_script_tag($src, $script_content='') {
|
2017-03-08 11:43:51 +03:00
|
|
|
$defer_str=' defer';
|
|
|
|
$s='<script nonce="' . \OC::$server->getContentSecurityPolicyNonceManager()->getNonce() . '"';
|
|
|
|
if (!empty($src)) {
|
|
|
|
// emit script tag for deferred loading from $src
|
|
|
|
$s.=$defer_str.' src="' . $src .'">';
|
|
|
|
} else if (!empty($script_content)) {
|
|
|
|
// emit script tag for inline script from $script_content without defer (see MDN)
|
|
|
|
$s.=">\n".$script_content."\n";
|
|
|
|
} else {
|
|
|
|
// no $src nor $src_content, really useless empty tag
|
|
|
|
$s.='>';
|
|
|
|
}
|
|
|
|
$s.='</script>';
|
|
|
|
print_unescaped($s."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print all <script> tags for loading JS
|
2017-07-19 20:44:10 +03:00
|
|
|
* @param array $obj all the script information from template
|
2017-03-08 11:43:51 +03:00
|
|
|
*/
|
|
|
|
function emit_script_loading_tags($obj) {
|
|
|
|
foreach($obj['jsfiles'] as $jsfile) {
|
|
|
|
emit_script_tag($jsfile, '');
|
|
|
|
}
|
2017-06-24 12:35:40 +03:00
|
|
|
if (!empty($obj['inline_ocjs'])) {
|
|
|
|
emit_script_tag('', $obj['inline_ocjs']);
|
|
|
|
}
|
2017-03-08 11:43:51 +03:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:36:52 +04:00
|
|
|
/**
|
2014-04-13 18:18:05 +04:00
|
|
|
* 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
|
2013-03-18 20:36:52 +04:00
|
|
|
*/
|
|
|
|
function print_unescaped($string) {
|
|
|
|
print($string);
|
|
|
|
}
|
|
|
|
|
2014-09-06 16:25:28 +04:00
|
|
|
/**
|
|
|
|
* Shortcut for adding scripts to a page
|
|
|
|
* @param string $app the appname
|
2014-09-09 17:47:42 +04:00
|
|
|
* @param string|string[] $file the filename,
|
|
|
|
* if an array is given it will add all scripts
|
2014-09-06 16:25:28 +04:00
|
|
|
*/
|
2014-11-06 23:26:50 +03:00
|
|
|
function script($app, $file = null) {
|
2014-09-09 17:47:42 +04:00
|
|
|
if(is_array($file)) {
|
|
|
|
foreach($file as $f) {
|
|
|
|
OC_Util::addScript($app, $f);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Util::addScript($app, $file);
|
|
|
|
}
|
2014-09-06 16:25:28 +04:00
|
|
|
}
|
|
|
|
|
2014-11-03 13:18:17 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-11-06 20:17:21 +03:00
|
|
|
function vendor_script($app, $file = null) {
|
2014-11-03 13:18:17 +03:00
|
|
|
if(is_array($file)) {
|
|
|
|
foreach($file as $f) {
|
|
|
|
OC_Util::addVendorScript($app, $f);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Util::addVendorScript($app, $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 16:25:28 +04:00
|
|
|
/**
|
|
|
|
* Shortcut for adding styles to a page
|
|
|
|
* @param string $app the appname
|
2014-09-09 17:47:42 +04:00
|
|
|
* @param string|string[] $file the filename,
|
|
|
|
* if an array is given it will add all styles
|
2014-09-06 16:25:28 +04:00
|
|
|
*/
|
2014-11-06 23:26:50 +03:00
|
|
|
function style($app, $file = null) {
|
2014-09-09 17:47:42 +04:00
|
|
|
if(is_array($file)) {
|
|
|
|
foreach($file as $f) {
|
|
|
|
OC_Util::addStyle($app, $f);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Util::addStyle($app, $file);
|
|
|
|
}
|
2014-09-06 16:25:28 +04:00
|
|
|
}
|
|
|
|
|
2014-11-03 13:18:17 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-11-06 20:17:21 +03:00
|
|
|
function vendor_style($app, $file = null) {
|
2014-11-03 13:18:17 +03:00
|
|
|
if(is_array($file)) {
|
|
|
|
foreach($file as $f) {
|
|
|
|
OC_Util::addVendorStyle($app, $f);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OC_Util::addVendorStyle($app, $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 21:47:37 +04:00
|
|
|
/**
|
|
|
|
* Shortcut for adding translations to a page
|
|
|
|
* @param string $app the appname
|
|
|
|
* if an array is given it will add all styles
|
|
|
|
*/
|
2014-10-29 12:58:10 +03:00
|
|
|
function translation($app) {
|
|
|
|
OC_Util::addTranslations($app);
|
2014-10-17 21:47:37 +04:00
|
|
|
}
|
|
|
|
|
2014-09-08 23:19:44 +04:00
|
|
|
/**
|
|
|
|
* Shortcut for HTML imports
|
|
|
|
* @param string $app the appname
|
2014-09-09 17:47:42 +04:00
|
|
|
* @param string|string[] $file the path relative to the app's component folder,
|
|
|
|
* if an array is given it will add all components
|
2014-09-08 23:19:44 +04:00
|
|
|
*/
|
|
|
|
function component($app, $file) {
|
2014-09-09 17:47:42 +04:00
|
|
|
if(is_array($file)) {
|
|
|
|
foreach($file as $f) {
|
|
|
|
$url = link_to($app, 'component/' . $f . '.html');
|
2020-03-26 11:30:18 +03:00
|
|
|
OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
|
2014-09-09 17:47:42 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$url = link_to($app, 'component/' . $file . '.html');
|
2020-03-26 11:30:18 +03:00
|
|
|
OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
|
2014-09-09 17:47:42 +04:00
|
|
|
}
|
2014-09-08 23:19:44 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:36:52 +04:00
|
|
|
/**
|
2016-01-22 13:18:42 +03:00
|
|
|
* make \OCP\IURLGenerator::linkTo available as a simple function
|
2013-03-18 20:36:52 +04:00
|
|
|
* @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
|
|
|
|
*
|
2016-01-22 13:18:42 +03:00
|
|
|
* For further information have a look at \OCP\IURLGenerator::linkTo
|
2013-03-18 20:36:52 +04:00
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
function link_to( $app, $file, $args = [] ) {
|
2015-12-17 12:53:21 +03:00
|
|
|
return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 00:01:27 +04:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @return string url to the online documentation
|
|
|
|
*/
|
2013-10-21 23:29:45 +04:00
|
|
|
function link_to_docs($key) {
|
2015-11-26 12:48:08 +03:00
|
|
|
return \OC::$server->getURLGenerator()->linkToDocs($key);
|
2013-10-21 23:29:45 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:36:52 +04:00
|
|
|
/**
|
2016-01-22 13:18:42 +03:00
|
|
|
* make \OCP\IURLGenerator::imagePath available as a simple function
|
2013-03-18 20:36:52 +04:00
|
|
|
* @param string $app app
|
|
|
|
* @param string $image image
|
|
|
|
* @return string link to the image
|
|
|
|
*
|
2016-01-22 13:18:42 +03:00
|
|
|
* For further information have a look at \OCP\IURLGenerator::imagePath
|
2013-03-18 20:36:52 +04:00
|
|
|
*/
|
|
|
|
function image_path( $app, $image ) {
|
2016-01-22 13:18:42 +03:00
|
|
|
return \OC::$server->getURLGenerator()->imagePath( $app, $image );
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* make OC_Helper::mimetypeIcon available as a simple function
|
2013-03-18 20:36:52 +04:00
|
|
|
* @param string $mimetype mimetype
|
|
|
|
* @return string link to the image
|
|
|
|
*/
|
|
|
|
function mimetype_icon( $mimetype ) {
|
2015-12-18 16:00:20 +03:00
|
|
|
return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype );
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
|
2013-07-30 02:34:36 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* make preview_icon available as a simple function
|
2013-07-30 02:34:36 +04:00
|
|
|
* Returns the path to the preview of the image.
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $path path of file
|
2017-07-19 19:45:40 +03:00
|
|
|
* @return string link to the preview
|
2013-07-30 02:34:36 +04:00
|
|
|
*/
|
|
|
|
function preview_icon( $path ) {
|
2016-11-07 20:26:18 +03:00
|
|
|
return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
|
2013-07-30 02:34:36 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2017-07-19 19:45:40 +03:00
|
|
|
* @param string $token
|
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2013-07-30 02:34:36 +04:00
|
|
|
function publicPreview_icon ( $path, $token ) {
|
2018-05-23 11:50:44 +03:00
|
|
|
return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 'token' => $token]);
|
2013-07-30 02:34:36 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:36:52 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* make OC_Helper::humanFileSize available as a simple function
|
2013-03-18 20:36:52 +04:00
|
|
|
* @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 );
|
|
|
|
}
|
|
|
|
|
2013-10-02 17:44:44 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Strips the timestamp of its time value
|
2013-10-02 17:44:44 +04:00
|
|
|
* @param int $timestamp UNIX timestamp to strip
|
2017-10-18 15:15:03 +03:00
|
|
|
* @return int timestamp without time value
|
2013-10-02 17:44:44 +04:00
|
|
|
*/
|
|
|
|
function strip_time($timestamp){
|
|
|
|
$date = new \DateTime("@{$timestamp}");
|
|
|
|
$date->setTime(0, 0, 0);
|
2018-01-26 01:06:53 +03:00
|
|
|
return (int)$date->format('U');
|
2013-10-02 17:44:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Formats timestamp relatively to the current time using
|
2013-10-02 17:44:44 +04:00
|
|
|
* a human-friendly format like "x minutes ago" or "yesterday"
|
|
|
|
* @param int $timestamp timestamp to format
|
2017-07-19 19:45:40 +03:00
|
|
|
* @param int|null $fromTime timestamp to compare from, defaults to current time
|
|
|
|
* @param bool|null $dateOnly whether to strip time information
|
2014-11-24 18:37:04 +03:00
|
|
|
* @return string timestamp
|
2013-10-02 17:44:44 +04:00
|
|
|
*/
|
2013-10-03 16:21:18 +04:00
|
|
|
function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
|
2014-11-24 18:37:04 +03:00
|
|
|
/** @var \OC\DateTimeFormatter $formatter */
|
|
|
|
$formatter = \OC::$server->query('DateTimeFormatter');
|
|
|
|
|
2013-10-02 17:44:44 +04:00
|
|
|
if ($dateOnly){
|
2014-11-24 18:37:04 +03:00
|
|
|
return $formatter->formatDateSpan($timestamp, $fromTime);
|
2013-10-02 17:44:44 +04:00
|
|
|
}
|
2014-11-24 18:37:04 +03:00
|
|
|
return $formatter->formatTimeSpan($timestamp, $fromTime);
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
function html_select_options($options, $selected, $params=[]) {
|
2013-03-18 20:36:52 +04:00
|
|
|
if (!is_array($selected)) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$selected=[$selected];
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
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"' : '';
|
2015-12-08 10:28:15 +03:00
|
|
|
$html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n";
|
2013-03-18 20:36:52 +04:00
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|