1
0

functions.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. $type = str_ends_with($jsfile, '.mjs') ? 'module' : '';
  96. emit_script_tag($jsfile, '', $type);
  97. }
  98. if (!empty($obj['inline_ocjs'])) {
  99. emit_script_tag('', $obj['inline_ocjs']);
  100. }
  101. }
  102. /**
  103. * Prints an unsanitized string - usage of this function may result into XSS.
  104. * Consider using p() instead.
  105. * @param string|array $string the string which will be printed as it is
  106. */
  107. function print_unescaped($string) {
  108. print($string);
  109. }
  110. /**
  111. * Shortcut for adding scripts to a page
  112. * All scripts are forced to be loaded after core since
  113. * they are coming from a template registration.
  114. * Please consider moving them into the relevant controller
  115. *
  116. * @deprecated 24.0.0 - Use \OCP\Util::addScript
  117. *
  118. * @param string $app the appname
  119. * @param string|string[] $file the filename,
  120. * if an array is given it will add all scripts
  121. */
  122. function script($app, $file = null) {
  123. if (is_array($file)) {
  124. foreach ($file as $script) {
  125. Util::addScript($app, $script, 'core');
  126. }
  127. } else {
  128. Util::addScript($app, $file, 'core');
  129. }
  130. }
  131. /**
  132. * Shortcut for adding vendor scripts to a page
  133. * @param string $app the appname
  134. * @param string|string[] $file the filename,
  135. * if an array is given it will add all scripts
  136. */
  137. function vendor_script($app, $file = null) {
  138. if (is_array($file)) {
  139. foreach ($file as $f) {
  140. OC_Util::addVendorScript($app, $f);
  141. }
  142. } else {
  143. OC_Util::addVendorScript($app, $file);
  144. }
  145. }
  146. /**
  147. * Shortcut for adding styles to a page
  148. * @param string $app the appname
  149. * @param string|string[] $file the filename,
  150. * if an array is given it will add all styles
  151. */
  152. function style($app, $file = null) {
  153. if (is_array($file)) {
  154. foreach ($file as $f) {
  155. OC_Util::addStyle($app, $f);
  156. }
  157. } else {
  158. OC_Util::addStyle($app, $file);
  159. }
  160. }
  161. /**
  162. * Shortcut for adding vendor styles to a page
  163. * @param string $app the appname
  164. * @param string|string[] $file the filename,
  165. * if an array is given it will add all styles
  166. */
  167. function vendor_style($app, $file = null) {
  168. if (is_array($file)) {
  169. foreach ($file as $f) {
  170. OC_Util::addVendorStyle($app, $f);
  171. }
  172. } else {
  173. OC_Util::addVendorStyle($app, $file);
  174. }
  175. }
  176. /**
  177. * Shortcut for adding translations to a page
  178. * @param string $app the appname
  179. * if an array is given it will add all styles
  180. */
  181. function translation($app) {
  182. OC_Util::addTranslations($app);
  183. }
  184. /**
  185. * Shortcut for HTML imports
  186. * @param string $app the appname
  187. * @param string|string[] $file the path relative to the app's component folder,
  188. * if an array is given it will add all components
  189. */
  190. function component($app, $file) {
  191. if (is_array($file)) {
  192. foreach ($file as $f) {
  193. $url = link_to($app, 'component/' . $f . '.html');
  194. OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
  195. }
  196. } else {
  197. $url = link_to($app, 'component/' . $file . '.html');
  198. OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
  199. }
  200. }
  201. /**
  202. * make \OCP\IURLGenerator::linkTo available as a simple function
  203. * @param string $app app
  204. * @param string $file file
  205. * @param array $args array with param=>value, will be appended to the returned url
  206. * @return string link to the file
  207. *
  208. * For further information have a look at \OCP\IURLGenerator::linkTo
  209. */
  210. function link_to($app, $file, $args = []) {
  211. return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
  212. }
  213. /**
  214. * @param $key
  215. * @return string url to the online documentation
  216. */
  217. function link_to_docs($key) {
  218. return \OC::$server->getURLGenerator()->linkToDocs($key);
  219. }
  220. /**
  221. * make \OCP\IURLGenerator::imagePath available as a simple function
  222. * @param string $app app
  223. * @param string $image image
  224. * @return string link to the image
  225. *
  226. * For further information have a look at \OCP\IURLGenerator::imagePath
  227. */
  228. function image_path($app, $image) {
  229. return \OC::$server->getURLGenerator()->imagePath($app, $image);
  230. }
  231. /**
  232. * make OC_Helper::mimetypeIcon available as a simple function
  233. * @param string $mimetype mimetype
  234. * @return string link to the image
  235. */
  236. function mimetype_icon($mimetype) {
  237. return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype);
  238. }
  239. /**
  240. * make preview_icon available as a simple function
  241. * Returns the path to the preview of the image.
  242. * @param string $path path of file
  243. * @return string link to the preview
  244. */
  245. function preview_icon($path) {
  246. return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
  247. }
  248. /**
  249. * @param string $path
  250. * @param string $token
  251. * @return string
  252. */
  253. function publicPreview_icon($path, $token) {
  254. return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 'token' => $token]);
  255. }
  256. /**
  257. * make OC_Helper::humanFileSize available as a simple function
  258. * @param int $bytes size in bytes
  259. * @return string size as string
  260. *
  261. * For further information have a look at OC_Helper::humanFileSize
  262. */
  263. function human_file_size($bytes) {
  264. return OC_Helper::humanFileSize($bytes);
  265. }
  266. /**
  267. * Strips the timestamp of its time value
  268. * @param int $timestamp UNIX timestamp to strip
  269. * @return int timestamp without time value
  270. */
  271. function strip_time($timestamp) {
  272. $date = new \DateTime("@{$timestamp}");
  273. $date->setTime(0, 0, 0);
  274. return (int)$date->format('U');
  275. }
  276. /**
  277. * Formats timestamp relatively to the current time using
  278. * a human-friendly format like "x minutes ago" or "yesterday"
  279. * @param int $timestamp timestamp to format
  280. * @param int|null $fromTime timestamp to compare from, defaults to current time
  281. * @param bool|null $dateOnly whether to strip time information
  282. * @return string timestamp
  283. */
  284. function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
  285. /** @var \OC\DateTimeFormatter $formatter */
  286. $formatter = \OC::$server->query('DateTimeFormatter');
  287. if ($dateOnly) {
  288. return $formatter->formatDateSpan($timestamp, $fromTime);
  289. }
  290. return $formatter->formatTimeSpan($timestamp, $fromTime);
  291. }
  292. function html_select_options($options, $selected, $params = []) {
  293. if (!is_array($selected)) {
  294. $selected = [$selected];
  295. }
  296. if (isset($params['combine']) && $params['combine']) {
  297. $options = array_combine($options, $options);
  298. }
  299. $value_name = $label_name = false;
  300. if (isset($params['value'])) {
  301. $value_name = $params['value'];
  302. }
  303. if (isset($params['label'])) {
  304. $label_name = $params['label'];
  305. }
  306. $html = '';
  307. foreach ($options as $value => $label) {
  308. if ($value_name && is_array($label)) {
  309. $value = $label[$value_name];
  310. }
  311. if ($label_name && is_array($label)) {
  312. $label = $label[$label_name];
  313. }
  314. $select = in_array($value, $selected) ? ' selected="selected"' : '';
  315. $html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n";
  316. }
  317. return $html;
  318. }