1
0

functions.php 8.8 KB

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