functions.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use OCP\IDateTimeFormatter;
  8. use OCP\Util;
  9. function p($string) {
  10. print(\OCP\Util::sanitizeHTML($string));
  11. }
  12. /**
  13. * Prints a <link> tag for loading css
  14. * @param string $href the source URL, ignored when empty
  15. * @param string $opts, additional optional options
  16. */
  17. function emit_css_tag($href, $opts = '') {
  18. $s = '<link rel="stylesheet"';
  19. if (!empty($href)) {
  20. $s .= ' href="' . $href . '"';
  21. }
  22. if (!empty($opts)) {
  23. $s .= ' ' . $opts;
  24. }
  25. print_unescaped($s . ">\n");
  26. }
  27. /**
  28. * Prints all tags for CSS loading
  29. * @param array $obj all the script information from template
  30. */
  31. function emit_css_loading_tags($obj) {
  32. foreach ($obj['cssfiles'] as $css) {
  33. emit_css_tag($css);
  34. }
  35. foreach ($obj['printcssfiles'] as $css) {
  36. emit_css_tag($css, 'media="print"');
  37. }
  38. }
  39. /**
  40. * Prints a <script> tag with nonce and defer depending on config
  41. * @param string $src the source URL, ignored when empty
  42. * @param string $script_content the inline script content, ignored when empty
  43. * @param string $content_type the type of the source (e.g. 'module')
  44. */
  45. function emit_script_tag(string $src, string $script_content = '', string $content_type = '') {
  46. $nonceManager = \OC::$server->get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class);
  47. $defer_str = ' defer';
  48. $type = $content_type !== '' ? ' type="' . $content_type . '"' : '';
  49. $s = '<script nonce="' . $nonceManager->getNonce() . '"';
  50. if (!empty($src)) {
  51. // emit script tag for deferred loading from $src
  52. $s .= $defer_str . ' src="' . $src . '"' . $type . '>';
  53. } elseif ($script_content !== '') {
  54. // emit script tag for inline script from $script_content without defer (see MDN)
  55. $s .= ">\n" . $script_content . "\n";
  56. } else {
  57. // no $src nor $src_content, really useless empty tag
  58. $s .= '>';
  59. }
  60. $s .= '</script>';
  61. print_unescaped($s . "\n");
  62. }
  63. /**
  64. * Print all <script> tags for loading JS
  65. * @param array $obj all the script information from template
  66. */
  67. function emit_script_loading_tags($obj) {
  68. foreach ($obj['jsfiles'] as $jsfile) {
  69. $fileName = explode('?', $jsfile, 2)[0];
  70. $type = str_ends_with($fileName, '.mjs') ? 'module' : '';
  71. emit_script_tag($jsfile, '', $type);
  72. }
  73. if (!empty($obj['inline_ocjs'])) {
  74. emit_script_tag('', $obj['inline_ocjs']);
  75. }
  76. }
  77. /**
  78. * Prints an unsanitized string - usage of this function may result into XSS.
  79. * Consider using p() instead.
  80. * @param string|array $string the string which will be printed as it is
  81. */
  82. function print_unescaped($string) {
  83. print($string);
  84. }
  85. /**
  86. * Shortcut for adding scripts to a page
  87. * All scripts are forced to be loaded after core since
  88. * they are coming from a template registration.
  89. * Please consider moving them into the relevant controller
  90. *
  91. * @deprecated 24.0.0 - Use \OCP\Util::addScript
  92. *
  93. * @param string $app the appname
  94. * @param string|string[] $file the filename,
  95. * if an array is given it will add all scripts
  96. */
  97. function script($app, $file = null) {
  98. if (is_array($file)) {
  99. foreach ($file as $script) {
  100. Util::addScript($app, $script, 'core');
  101. }
  102. } else {
  103. Util::addScript($app, $file, 'core');
  104. }
  105. }
  106. /**
  107. * Shortcut for adding vendor scripts to a page
  108. * @param string $app the appname
  109. * @param string|string[] $file the filename,
  110. * if an array is given it will add all scripts
  111. */
  112. function vendor_script($app, $file = null) {
  113. if (is_array($file)) {
  114. foreach ($file as $f) {
  115. OC_Util::addVendorScript($app, $f);
  116. }
  117. } else {
  118. OC_Util::addVendorScript($app, $file);
  119. }
  120. }
  121. /**
  122. * Shortcut for adding styles to a page
  123. * @param string $app the appname
  124. * @param string|string[] $file the filename,
  125. * if an array is given it will add all styles
  126. */
  127. function style($app, $file = null) {
  128. if (is_array($file)) {
  129. foreach ($file as $f) {
  130. OC_Util::addStyle($app, $f);
  131. }
  132. } else {
  133. OC_Util::addStyle($app, $file);
  134. }
  135. }
  136. /**
  137. * Shortcut for adding vendor styles to a page
  138. * @param string $app the appname
  139. * @param string|string[] $file the filename,
  140. * if an array is given it will add all styles
  141. */
  142. function vendor_style($app, $file = null) {
  143. if (is_array($file)) {
  144. foreach ($file as $f) {
  145. OC_Util::addVendorStyle($app, $f);
  146. }
  147. } else {
  148. OC_Util::addVendorStyle($app, $file);
  149. }
  150. }
  151. /**
  152. * Shortcut for adding translations to a page
  153. * @param string $app the appname
  154. * if an array is given it will add all styles
  155. */
  156. function translation($app) {
  157. OC_Util::addTranslations($app);
  158. }
  159. /**
  160. * Shortcut for HTML imports
  161. * @param string $app the appname
  162. * @param string|string[] $file the path relative to the app's component folder,
  163. * if an array is given it will add all components
  164. */
  165. function component($app, $file) {
  166. if (is_array($file)) {
  167. foreach ($file as $f) {
  168. $url = link_to($app, 'component/' . $f . '.html');
  169. OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
  170. }
  171. } else {
  172. $url = link_to($app, 'component/' . $file . '.html');
  173. OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
  174. }
  175. }
  176. /**
  177. * make \OCP\IURLGenerator::linkTo available as a simple function
  178. * @param string $app app
  179. * @param string $file file
  180. * @param array $args array with param=>value, will be appended to the returned url
  181. * @return string link to the file
  182. *
  183. * For further information have a look at \OCP\IURLGenerator::linkTo
  184. */
  185. function link_to($app, $file, $args = []) {
  186. return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
  187. }
  188. /**
  189. * @param $key
  190. * @return string url to the online documentation
  191. */
  192. function link_to_docs($key) {
  193. return \OC::$server->getURLGenerator()->linkToDocs($key);
  194. }
  195. /**
  196. * make \OCP\IURLGenerator::imagePath available as a simple function
  197. * @param string $app app
  198. * @param string $image image
  199. * @return string link to the image
  200. *
  201. * For further information have a look at \OCP\IURLGenerator::imagePath
  202. */
  203. function image_path($app, $image) {
  204. return \OC::$server->getURLGenerator()->imagePath($app, $image);
  205. }
  206. /**
  207. * make OC_Helper::mimetypeIcon available as a simple function
  208. * @param string $mimetype mimetype
  209. * @return string link to the image
  210. */
  211. function mimetype_icon($mimetype) {
  212. return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype);
  213. }
  214. /**
  215. * make preview_icon available as a simple function
  216. * Returns the path to the preview of the image.
  217. * @param string $path path of file
  218. * @return string link to the preview
  219. */
  220. function preview_icon($path) {
  221. return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
  222. }
  223. /**
  224. * @param string $path
  225. * @param string $token
  226. * @return string
  227. */
  228. function publicPreview_icon($path, $token) {
  229. return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 'token' => $token]);
  230. }
  231. /**
  232. * make OC_Helper::humanFileSize available as a simple function
  233. * @param int $bytes size in bytes
  234. * @return string size as string
  235. *
  236. * For further information have a look at OC_Helper::humanFileSize
  237. */
  238. function human_file_size($bytes) {
  239. return OC_Helper::humanFileSize($bytes);
  240. }
  241. /**
  242. * Strips the timestamp of its time value
  243. * @param int $timestamp UNIX timestamp to strip
  244. * @return int timestamp without time value
  245. */
  246. function strip_time($timestamp) {
  247. $date = new \DateTime("@{$timestamp}");
  248. $date->setTime(0, 0, 0);
  249. return (int)$date->format('U');
  250. }
  251. /**
  252. * Formats timestamp relatively to the current time using
  253. * a human-friendly format like "x minutes ago" or "yesterday"
  254. * @param int $timestamp timestamp to format
  255. * @param int|null $fromTime timestamp to compare from, defaults to current time
  256. * @param bool|null $dateOnly whether to strip time information
  257. * @return string timestamp
  258. */
  259. function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false): string {
  260. $formatter = \OCP\Server::get(IDateTimeFormatter::class);
  261. if ($dateOnly) {
  262. return $formatter->formatDateSpan($timestamp, $fromTime);
  263. }
  264. return $formatter->formatTimeSpan($timestamp, $fromTime);
  265. }
  266. function html_select_options($options, $selected, $params = []) {
  267. if (!is_array($selected)) {
  268. $selected = [$selected];
  269. }
  270. if (isset($params['combine']) && $params['combine']) {
  271. $options = array_combine($options, $options);
  272. }
  273. $value_name = $label_name = false;
  274. if (isset($params['value'])) {
  275. $value_name = $params['value'];
  276. }
  277. if (isset($params['label'])) {
  278. $label_name = $params['label'];
  279. }
  280. $html = '';
  281. foreach ($options as $value => $label) {
  282. if ($value_name && is_array($label)) {
  283. $value = $label[$value_name];
  284. }
  285. if ($label_name && is_array($label)) {
  286. $label = $label[$label_name];
  287. }
  288. $select = in_array($value, $selected) ? ' selected="selected"' : '';
  289. $html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>' . "\n";
  290. }
  291. return $html;
  292. }