functions.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. use OCP\Util;
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Michael Letzgus <www@chronos.michael-letzgus.de>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Vincent Petry <vincent@nextcloud.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. function p($string) {
  36. print(\OCP\Util::sanitizeHTML($string));
  37. }
  38. /**
  39. * Prints a <link> tag for loading css
  40. * @param string $href the source URL, ignored when empty
  41. * @param string $opts, additional optional options
  42. */
  43. function emit_css_tag($href, $opts = '') {
  44. $s = '<link rel="stylesheet"';
  45. if (!empty($href)) {
  46. $s .= ' href="' . $href .'"';
  47. }
  48. if (!empty($opts)) {
  49. $s .= ' '.$opts;
  50. }
  51. print_unescaped($s.">\n");
  52. }
  53. /**
  54. * Prints all tags for CSS loading
  55. * @param array $obj all the script information from template
  56. */
  57. function emit_css_loading_tags($obj) {
  58. foreach ($obj['cssfiles'] as $css) {
  59. emit_css_tag($css);
  60. }
  61. foreach ($obj['printcssfiles'] as $css) {
  62. emit_css_tag($css, 'media="print"');
  63. }
  64. }
  65. /**
  66. * Prints a <script> tag with nonce and defer depending on config
  67. * @param string $src the source URL, ignored when empty
  68. * @param string $script_content the inline script content, ignored when empty
  69. * @param string $content_type the type of the source (e.g. 'module')
  70. */
  71. function emit_script_tag(string $src, string $script_content = '', string $content_type = '') {
  72. $nonceManager = \OC::$server->get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class);
  73. $defer_str = ' defer';
  74. $type = $content_type !== '' ? ' type="' . $content_type . '"' : '';
  75. $s = '<script nonce="' . $nonceManager->getNonce() . '"';
  76. if (!empty($src)) {
  77. // emit script tag for deferred loading from $src
  78. $s .= $defer_str.' src="' . $src .'"' . $type . '>';
  79. } elseif ($script_content !== '') {
  80. // emit script tag for inline script from $script_content without defer (see MDN)
  81. $s .= ">\n".$script_content."\n";
  82. } else {
  83. // no $src nor $src_content, really useless empty tag
  84. $s .= '>';
  85. }
  86. $s .= '</script>';
  87. print_unescaped($s."\n");
  88. }
  89. /**
  90. * Print all <script> tags for loading JS
  91. * @param array $obj all the script information from template
  92. */
  93. function emit_script_loading_tags($obj) {
  94. foreach ($obj['jsfiles'] as $jsfile) {
  95. $fileName = explode('?', $jsfile, 2)[0];
  96. $type = str_ends_with($fileName, '.mjs') ? 'module' : '';
  97. emit_script_tag($jsfile, '', $type);
  98. }
  99. if (!empty($obj['inline_ocjs'])) {
  100. emit_script_tag('', $obj['inline_ocjs']);
  101. }
  102. }
  103. /**
  104. * Prints an unsanitized string - usage of this function may result into XSS.
  105. * Consider using p() instead.
  106. * @param string|array $string the string which will be printed as it is
  107. */
  108. function print_unescaped($string) {
  109. print($string);
  110. }
  111. /**
  112. * Shortcut for adding scripts to a page
  113. * All scripts are forced to be loaded after core since
  114. * they are coming from a template registration.
  115. * Please consider moving them into the relevant controller
  116. *
  117. * @deprecated 24.0.0 - Use \OCP\Util::addScript
  118. *
  119. * @param string $app the appname
  120. * @param string|string[] $file the filename,
  121. * if an array is given it will add all scripts
  122. */
  123. function script($app, $file = null) {
  124. if (is_array($file)) {
  125. foreach ($file as $script) {
  126. Util::addScript($app, $script, 'core');
  127. }
  128. } else {
  129. Util::addScript($app, $file, 'core');
  130. }
  131. }
  132. /**
  133. * Shortcut for adding vendor scripts to a page
  134. * @param string $app the appname
  135. * @param string|string[] $file the filename,
  136. * if an array is given it will add all scripts
  137. */
  138. function vendor_script($app, $file = null) {
  139. if (is_array($file)) {
  140. foreach ($file as $f) {
  141. OC_Util::addVendorScript($app, $f);
  142. }
  143. } else {
  144. OC_Util::addVendorScript($app, $file);
  145. }
  146. }
  147. /**
  148. * Shortcut for adding styles to a page
  149. * @param string $app the appname
  150. * @param string|string[] $file the filename,
  151. * if an array is given it will add all styles
  152. */
  153. function style($app, $file = null) {
  154. if (is_array($file)) {
  155. foreach ($file as $f) {
  156. OC_Util::addStyle($app, $f);
  157. }
  158. } else {
  159. OC_Util::addStyle($app, $file);
  160. }
  161. }
  162. /**
  163. * Shortcut for adding vendor styles to a page
  164. * @param string $app the appname
  165. * @param string|string[] $file the filename,
  166. * if an array is given it will add all styles
  167. */
  168. function vendor_style($app, $file = null) {
  169. if (is_array($file)) {
  170. foreach ($file as $f) {
  171. OC_Util::addVendorStyle($app, $f);
  172. }
  173. } else {
  174. OC_Util::addVendorStyle($app, $file);
  175. }
  176. }
  177. /**
  178. * Shortcut for adding translations to a page
  179. * @param string $app the appname
  180. * if an array is given it will add all styles
  181. */
  182. function translation($app) {
  183. OC_Util::addTranslations($app);
  184. }
  185. /**
  186. * Shortcut for HTML imports
  187. * @param string $app the appname
  188. * @param string|string[] $file the path relative to the app's component folder,
  189. * if an array is given it will add all components
  190. */
  191. function component($app, $file) {
  192. if (is_array($file)) {
  193. foreach ($file as $f) {
  194. $url = link_to($app, 'component/' . $f . '.html');
  195. OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
  196. }
  197. } else {
  198. $url = link_to($app, 'component/' . $file . '.html');
  199. OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
  200. }
  201. }
  202. /**
  203. * make \OCP\IURLGenerator::linkTo available as a simple function
  204. * @param string $app app
  205. * @param string $file file
  206. * @param array $args array with param=>value, will be appended to the returned url
  207. * @return string link to the file
  208. *
  209. * For further information have a look at \OCP\IURLGenerator::linkTo
  210. */
  211. function link_to($app, $file, $args = []) {
  212. return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
  213. }
  214. /**
  215. * @param $key
  216. * @return string url to the online documentation
  217. */
  218. function link_to_docs($key) {
  219. return \OC::$server->getURLGenerator()->linkToDocs($key);
  220. }
  221. /**
  222. * make \OCP\IURLGenerator::imagePath available as a simple function
  223. * @param string $app app
  224. * @param string $image image
  225. * @return string link to the image
  226. *
  227. * For further information have a look at \OCP\IURLGenerator::imagePath
  228. */
  229. function image_path($app, $image) {
  230. return \OC::$server->getURLGenerator()->imagePath($app, $image);
  231. }
  232. /**
  233. * make OC_Helper::mimetypeIcon available as a simple function
  234. * @param string $mimetype mimetype
  235. * @return string link to the image
  236. */
  237. function mimetype_icon($mimetype) {
  238. return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype);
  239. }
  240. /**
  241. * make preview_icon available as a simple function
  242. * Returns the path to the preview of the image.
  243. * @param string $path path of file
  244. * @return string link to the preview
  245. */
  246. function preview_icon($path) {
  247. return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
  248. }
  249. /**
  250. * @param string $path
  251. * @param string $token
  252. * @return string
  253. */
  254. function publicPreview_icon($path, $token) {
  255. return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 'token' => $token]);
  256. }
  257. /**
  258. * make OC_Helper::humanFileSize available as a simple function
  259. * @param int $bytes size in bytes
  260. * @return string size as string
  261. *
  262. * For further information have a look at OC_Helper::humanFileSize
  263. */
  264. function human_file_size($bytes) {
  265. return OC_Helper::humanFileSize($bytes);
  266. }
  267. /**
  268. * Strips the timestamp of its time value
  269. * @param int $timestamp UNIX timestamp to strip
  270. * @return int timestamp without time value
  271. */
  272. function strip_time($timestamp) {
  273. $date = new \DateTime("@{$timestamp}");
  274. $date->setTime(0, 0, 0);
  275. return (int)$date->format('U');
  276. }
  277. /**
  278. * Formats timestamp relatively to the current time using
  279. * a human-friendly format like "x minutes ago" or "yesterday"
  280. * @param int $timestamp timestamp to format
  281. * @param int|null $fromTime timestamp to compare from, defaults to current time
  282. * @param bool|null $dateOnly whether to strip time information
  283. * @return string timestamp
  284. */
  285. function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
  286. /** @var \OC\DateTimeFormatter $formatter */
  287. $formatter = \OCP\Server::get('DateTimeFormatter');
  288. if ($dateOnly) {
  289. return $formatter->formatDateSpan($timestamp, $fromTime);
  290. }
  291. return $formatter->formatTimeSpan($timestamp, $fromTime);
  292. }
  293. function html_select_options($options, $selected, $params = []) {
  294. if (!is_array($selected)) {
  295. $selected = [$selected];
  296. }
  297. if (isset($params['combine']) && $params['combine']) {
  298. $options = array_combine($options, $options);
  299. }
  300. $value_name = $label_name = false;
  301. if (isset($params['value'])) {
  302. $value_name = $params['value'];
  303. }
  304. if (isset($params['label'])) {
  305. $label_name = $params['label'];
  306. }
  307. $html = '';
  308. foreach ($options as $value => $label) {
  309. if ($value_name && is_array($label)) {
  310. $value = $label[$value_name];
  311. }
  312. if ($label_name && is_array($label)) {
  313. $label = $label[$label_name];
  314. }
  315. $select = in_array($value, $selected) ? ' selected="selected"' : '';
  316. $html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n";
  317. }
  318. return $html;
  319. }