2013-03-18 20:36:52 +04:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-04-13 18:18:05 +04:00
|
|
|
* Prints a sanitized string
|
|
|
|
* @param string|array $string the string which will be escaped and printed
|
2013-03-18 20:36:52 +04:00
|
|
|
*/
|
|
|
|
function p($string) {
|
|
|
|
print(OC_Util::sanitizeHTML($string));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
function script($app, $file) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
function style($app, $file) {
|
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-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');
|
|
|
|
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));
|
|
|
|
}
|
2014-09-08 23:19:44 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:36:52 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* make OC_Helper::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
|
|
|
|
*
|
|
|
|
* For further information have a look at OC_Helper::linkTo
|
|
|
|
*/
|
|
|
|
function link_to( $app, $file, $args = array() ) {
|
|
|
|
return OC_Helper::linkTo( $app, $file, $args );
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-10-22 00:01:27 +04:00
|
|
|
return OC_Helper::linkToDocs($key);
|
2013-10-21 23:29:45 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:36:52 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* make OC_Helper::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
|
|
|
|
*
|
|
|
|
* For further information have a look at OC_Helper::imagePath
|
|
|
|
*/
|
|
|
|
function image_path( $app, $image ) {
|
|
|
|
return OC_Helper::imagePath( $app, $image );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*
|
|
|
|
* For further information have a look at OC_Helper::mimetypeIcon
|
|
|
|
*/
|
|
|
|
function mimetype_icon( $mimetype ) {
|
|
|
|
return OC_Helper::mimetypeIcon( $mimetype );
|
|
|
|
}
|
|
|
|
|
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
|
2014-05-11 21:05:28 +04:00
|
|
|
* @return link to the preview
|
2013-07-30 02:34:36 +04:00
|
|
|
*
|
|
|
|
* For further information have a look at OC_Helper::previewIcon
|
|
|
|
*/
|
|
|
|
function preview_icon( $path ) {
|
|
|
|
return OC_Helper::previewIcon( $path );
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2013-07-30 02:34:36 +04:00
|
|
|
function publicPreview_icon ( $path, $token ) {
|
2013-07-30 14:35:39 +04:00
|
|
|
return OC_Helper::publicPreviewIcon( $path, $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
|
|
|
|
* @return $timestamp without time value
|
|
|
|
*/
|
|
|
|
function strip_time($timestamp){
|
|
|
|
$date = new \DateTime("@{$timestamp}");
|
|
|
|
$date->setTime(0, 0, 0);
|
|
|
|
return intval($date->format('U'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2013-10-02 22:16:35 +04:00
|
|
|
* @param int $fromTime timestamp to compare from, defaults to current time
|
2013-10-02 17:44:44 +04:00
|
|
|
* @param bool $dateOnly whether to strip time information
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return OC_L10N_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-08-31 12:05:59 +04:00
|
|
|
$l = \OC::$server->getL10N('lib');
|
2013-10-03 16:21:18 +04:00
|
|
|
if (!isset($fromTime) || $fromTime === null){
|
2013-10-02 22:16:35 +04:00
|
|
|
$fromTime = time();
|
|
|
|
}
|
2013-10-02 17:44:44 +04:00
|
|
|
if ($dateOnly){
|
2013-10-02 22:16:35 +04:00
|
|
|
$fromTime = strip_time($fromTime);
|
2013-10-02 17:44:44 +04:00
|
|
|
$timestamp = strip_time($timestamp);
|
|
|
|
}
|
2013-10-02 22:16:35 +04:00
|
|
|
$timediff = $fromTime - $timestamp;
|
2013-03-18 20:36:52 +04:00
|
|
|
$diffminutes = round($timediff/60);
|
|
|
|
$diffhours = round($diffminutes/60);
|
|
|
|
$diffdays = round($diffhours/24);
|
|
|
|
$diffmonths = round($diffdays/31);
|
|
|
|
|
2013-10-02 17:44:44 +04:00
|
|
|
if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); }
|
|
|
|
else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
|
|
|
|
else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
|
2013-10-02 22:16:35 +04:00
|
|
|
else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); }
|
|
|
|
else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); }
|
|
|
|
// 86400 * 31 days = 2678400
|
2013-08-09 22:37:18 +04:00
|
|
|
else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); }
|
2013-10-02 22:16:35 +04:00
|
|
|
// 86400 * 60 days = 518400
|
2013-03-18 20:36:52 +04:00
|
|
|
else if($timediff < 5184000) { return $l->t('last month'); }
|
2013-10-02 22:16:35 +04:00
|
|
|
else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
|
|
|
|
// 86400 * 365.25 days * 2 = 63113852
|
2013-03-18 20:36:52 +04:00
|
|
|
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;
|
|
|
|
}
|