You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
9.4 KiB

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