functions.php 9.6 KB

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