You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * Prints a sanitized string
  10. * @param string|array $string the string which will be escaped and printed
  11. */
  12. function p($string) {
  13. print(OC_Util::sanitizeHTML($string));
  14. }
  15. /**
  16. * Prints an unsanitized string - usage of this function may result into XSS.
  17. * Consider using p() instead.
  18. * @param string|array $string the string which will be printed as it is
  19. */
  20. function print_unescaped($string) {
  21. print($string);
  22. }
  23. /**
  24. * Shortcut for adding scripts to a page
  25. * @param string $app the appname
  26. * @param string|string[] $file the filename,
  27. * if an array is given it will add all scripts
  28. */
  29. function script($app, $file) {
  30. if(is_array($file)) {
  31. foreach($file as $f) {
  32. OC_Util::addScript($app, $f);
  33. }
  34. } else {
  35. OC_Util::addScript($app, $file);
  36. }
  37. }
  38. /**
  39. * Shortcut for adding vendor scripts to a page
  40. * @param string $app the appname
  41. * @param string|string[] $file the filename,
  42. * if an array is given it will add all scripts
  43. */
  44. function vendorScript($app, $file) {
  45. if(is_array($file)) {
  46. foreach($file as $f) {
  47. OC_Util::addVendorScript($app, $f);
  48. }
  49. } else {
  50. OC_Util::addVendorScript($app, $file);
  51. }
  52. }
  53. /**
  54. * Shortcut for adding styles to a page
  55. * @param string $app the appname
  56. * @param string|string[] $file the filename,
  57. * if an array is given it will add all styles
  58. */
  59. function style($app, $file) {
  60. if(is_array($file)) {
  61. foreach($file as $f) {
  62. OC_Util::addStyle($app, $f);
  63. }
  64. } else {
  65. OC_Util::addStyle($app, $file);
  66. }
  67. }
  68. /**
  69. * Shortcut for adding vendor styles to a page
  70. * @param string $app the appname
  71. * @param string|string[] $file the filename,
  72. * if an array is given it will add all styles
  73. */
  74. function vendorStyle($app, $file) {
  75. if(is_array($file)) {
  76. foreach($file as $f) {
  77. OC_Util::addVendorStyle($app, $f);
  78. }
  79. } else {
  80. OC_Util::addVendorStyle($app, $file);
  81. }
  82. }
  83. /**
  84. * Shortcut for adding translations to a page
  85. * @param string $app the appname
  86. * if an array is given it will add all styles
  87. */
  88. function translation($app) {
  89. OC_Util::addTranslations($app);
  90. }
  91. /**
  92. * Shortcut for HTML imports
  93. * @param string $app the appname
  94. * @param string|string[] $file the path relative to the app's component folder,
  95. * if an array is given it will add all components
  96. */
  97. function component($app, $file) {
  98. if(is_array($file)) {
  99. foreach($file as $f) {
  100. $url = link_to($app, 'component/' . $f . '.html');
  101. OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
  102. }
  103. } else {
  104. $url = link_to($app, 'component/' . $file . '.html');
  105. OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
  106. }
  107. }
  108. /**
  109. * make OC_Helper::linkTo available as a simple function
  110. * @param string $app app
  111. * @param string $file file
  112. * @param array $args array with param=>value, will be appended to the returned url
  113. * @return string link to the file
  114. *
  115. * For further information have a look at OC_Helper::linkTo
  116. */
  117. function link_to( $app, $file, $args = array() ) {
  118. return OC_Helper::linkTo( $app, $file, $args );
  119. }
  120. /**
  121. * @param $key
  122. * @return string url to the online documentation
  123. */
  124. function link_to_docs($key) {
  125. return OC_Helper::linkToDocs($key);
  126. }
  127. /**
  128. * make OC_Helper::imagePath available as a simple function
  129. * @param string $app app
  130. * @param string $image image
  131. * @return string link to the image
  132. *
  133. * For further information have a look at OC_Helper::imagePath
  134. */
  135. function image_path( $app, $image ) {
  136. return OC_Helper::imagePath( $app, $image );
  137. }
  138. /**
  139. * make OC_Helper::mimetypeIcon available as a simple function
  140. * @param string $mimetype mimetype
  141. * @return string link to the image
  142. *
  143. * For further information have a look at OC_Helper::mimetypeIcon
  144. */
  145. function mimetype_icon( $mimetype ) {
  146. return OC_Helper::mimetypeIcon( $mimetype );
  147. }
  148. /**
  149. * make preview_icon available as a simple function
  150. * Returns the path to the preview of the image.
  151. * @param string $path path of file
  152. * @return link to the preview
  153. *
  154. * For further information have a look at OC_Helper::previewIcon
  155. */
  156. function preview_icon( $path ) {
  157. return OC_Helper::previewIcon( $path );
  158. }
  159. /**
  160. * @param string $path
  161. */
  162. function publicPreview_icon ( $path, $token ) {
  163. return OC_Helper::publicPreviewIcon( $path, $token );
  164. }
  165. /**
  166. * make OC_Helper::humanFileSize available as a simple function
  167. * @param int $bytes size in bytes
  168. * @return string size as string
  169. *
  170. * For further information have a look at OC_Helper::humanFileSize
  171. */
  172. function human_file_size( $bytes ) {
  173. return OC_Helper::humanFileSize( $bytes );
  174. }
  175. /**
  176. * Strips the timestamp of its time value
  177. * @param int $timestamp UNIX timestamp to strip
  178. * @return $timestamp without time value
  179. */
  180. function strip_time($timestamp){
  181. $date = new \DateTime("@{$timestamp}");
  182. $date->setTime(0, 0, 0);
  183. return intval($date->format('U'));
  184. }
  185. /**
  186. * Formats timestamp relatively to the current time using
  187. * a human-friendly format like "x minutes ago" or "yesterday"
  188. * @param int $timestamp timestamp to format
  189. * @param int $fromTime timestamp to compare from, defaults to current time
  190. * @param bool $dateOnly whether to strip time information
  191. * @return OC_L10N_String timestamp
  192. */
  193. function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
  194. $l = \OC::$server->getL10N('lib');
  195. if (!isset($fromTime) || $fromTime === null){
  196. $fromTime = time();
  197. }
  198. if ($dateOnly){
  199. $fromTime = strip_time($fromTime);
  200. $timestamp = strip_time($timestamp);
  201. }
  202. $timediff = $fromTime - $timestamp;
  203. $diffminutes = round($timediff/60);
  204. $diffhours = round($diffminutes/60);
  205. $diffdays = round($diffhours/24);
  206. $diffmonths = round($diffdays/31);
  207. if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); }
  208. else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
  209. else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
  210. else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); }
  211. else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); }
  212. // 86400 * 31 days = 2678400
  213. else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); }
  214. // 86400 * 60 days = 518400
  215. else if($timediff < 5184000) { return $l->t('last month'); }
  216. else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
  217. // 86400 * 365.25 days * 2 = 63113852
  218. else if($timediff < 63113852) { return $l->t('last year'); }
  219. else { return $l->t('years ago'); }
  220. }
  221. function html_select_options($options, $selected, $params=array()) {
  222. if (!is_array($selected)) {
  223. $selected=array($selected);
  224. }
  225. if (isset($params['combine']) && $params['combine']) {
  226. $options = array_combine($options, $options);
  227. }
  228. $value_name = $label_name = false;
  229. if (isset($params['value'])) {
  230. $value_name = $params['value'];
  231. }
  232. if (isset($params['label'])) {
  233. $label_name = $params['label'];
  234. }
  235. $html = '';
  236. foreach($options as $value => $label) {
  237. if ($value_name && is_array($label)) {
  238. $value = $label[$value_name];
  239. }
  240. if ($label_name && is_array($label)) {
  241. $label = $label[$label_name];
  242. }
  243. $select = in_array($value, $selected) ? ' selected="selected"' : '';
  244. $html .= '<option value="' . OC_Util::sanitizeHTML($value) . '"' . $select . '>' . OC_Util::sanitizeHTML($label) . '</option>'."\n";
  245. }
  246. return $html;
  247. }