functions.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. /**
  32. * Prints a sanitized string
  33. * @param string $string the string which will be escaped and printed
  34. */
  35. function p($string) {
  36. print(\OCP\Util::sanitizeHTML($string));
  37. }
  38. /**
  39. * Prints an unsanitized string - usage of this function may result into XSS.
  40. * Consider using p() instead.
  41. * @param string|array $string the string which will be printed as it is
  42. */
  43. function print_unescaped($string) {
  44. print($string);
  45. }
  46. /**
  47. * Shortcut for adding scripts to a page
  48. * @param string $app the appname
  49. * @param string|string[] $file the filename,
  50. * if an array is given it will add all scripts
  51. */
  52. function script($app, $file = null) {
  53. if(is_array($file)) {
  54. foreach($file as $f) {
  55. OC_Util::addScript($app, $f);
  56. }
  57. } else {
  58. OC_Util::addScript($app, $file);
  59. }
  60. }
  61. /**
  62. * Shortcut for adding vendor scripts to a page
  63. * @param string $app the appname
  64. * @param string|string[] $file the filename,
  65. * if an array is given it will add all scripts
  66. */
  67. function vendor_script($app, $file = null) {
  68. if(is_array($file)) {
  69. foreach($file as $f) {
  70. OC_Util::addVendorScript($app, $f);
  71. }
  72. } else {
  73. OC_Util::addVendorScript($app, $file);
  74. }
  75. }
  76. /**
  77. * Shortcut for adding styles to a page
  78. * @param string $app the appname
  79. * @param string|string[] $file the filename,
  80. * if an array is given it will add all styles
  81. */
  82. function style($app, $file = null) {
  83. if(is_array($file)) {
  84. foreach($file as $f) {
  85. OC_Util::addStyle($app, $f);
  86. }
  87. } else {
  88. OC_Util::addStyle($app, $file);
  89. }
  90. }
  91. /**
  92. * Shortcut for adding vendor styles to a page
  93. * @param string $app the appname
  94. * @param string|string[] $file the filename,
  95. * if an array is given it will add all styles
  96. */
  97. function vendor_style($app, $file = null) {
  98. if(is_array($file)) {
  99. foreach($file as $f) {
  100. OC_Util::addVendorStyle($app, $f);
  101. }
  102. } else {
  103. OC_Util::addVendorStyle($app, $file);
  104. }
  105. }
  106. /**
  107. * Shortcut for adding translations to a page
  108. * @param string $app the appname
  109. * if an array is given it will add all styles
  110. */
  111. function translation($app) {
  112. OC_Util::addTranslations($app);
  113. }
  114. /**
  115. * Shortcut for HTML imports
  116. * @param string $app the appname
  117. * @param string|string[] $file the path relative to the app's component folder,
  118. * if an array is given it will add all components
  119. */
  120. function component($app, $file) {
  121. if(is_array($file)) {
  122. foreach($file as $f) {
  123. $url = link_to($app, 'component/' . $f . '.html');
  124. OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
  125. }
  126. } else {
  127. $url = link_to($app, 'component/' . $file . '.html');
  128. OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
  129. }
  130. }
  131. /**
  132. * make \OCP\IURLGenerator::linkTo available as a simple function
  133. * @param string $app app
  134. * @param string $file file
  135. * @param array $args array with param=>value, will be appended to the returned url
  136. * @return string link to the file
  137. *
  138. * For further information have a look at \OCP\IURLGenerator::linkTo
  139. */
  140. function link_to( $app, $file, $args = array() ) {
  141. return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
  142. }
  143. /**
  144. * @param $key
  145. * @return string url to the online documentation
  146. */
  147. function link_to_docs($key) {
  148. return \OC::$server->getURLGenerator()->linkToDocs($key);
  149. }
  150. /**
  151. * make \OCP\IURLGenerator::imagePath available as a simple function
  152. * @param string $app app
  153. * @param string $image image
  154. * @return string link to the image
  155. *
  156. * For further information have a look at \OCP\IURLGenerator::imagePath
  157. */
  158. function image_path( $app, $image ) {
  159. return \OC::$server->getURLGenerator()->imagePath( $app, $image );
  160. }
  161. /**
  162. * make OC_Helper::mimetypeIcon available as a simple function
  163. * @param string $mimetype mimetype
  164. * @return string link to the image
  165. */
  166. function mimetype_icon( $mimetype ) {
  167. return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype );
  168. }
  169. /**
  170. * make preview_icon available as a simple function
  171. * Returns the path to the preview of the image.
  172. * @param string $path path of file
  173. * @return link to the preview
  174. */
  175. function preview_icon( $path ) {
  176. return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
  177. }
  178. /**
  179. * @param string $path
  180. */
  181. function publicPreview_icon ( $path, $token ) {
  182. return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]);
  183. }
  184. /**
  185. * make OC_Helper::humanFileSize available as a simple function
  186. * @param int $bytes size in bytes
  187. * @return string size as string
  188. *
  189. * For further information have a look at OC_Helper::humanFileSize
  190. */
  191. function human_file_size( $bytes ) {
  192. return OC_Helper::humanFileSize( $bytes );
  193. }
  194. /**
  195. * Strips the timestamp of its time value
  196. * @param int $timestamp UNIX timestamp to strip
  197. * @return $timestamp without time value
  198. */
  199. function strip_time($timestamp){
  200. $date = new \DateTime("@{$timestamp}");
  201. $date->setTime(0, 0, 0);
  202. return intval($date->format('U'));
  203. }
  204. /**
  205. * Formats timestamp relatively to the current time using
  206. * a human-friendly format like "x minutes ago" or "yesterday"
  207. * @param int $timestamp timestamp to format
  208. * @param int $fromTime timestamp to compare from, defaults to current time
  209. * @param bool $dateOnly whether to strip time information
  210. * @return string timestamp
  211. */
  212. function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
  213. /** @var \OC\DateTimeFormatter $formatter */
  214. $formatter = \OC::$server->query('DateTimeFormatter');
  215. if ($dateOnly){
  216. return $formatter->formatDateSpan($timestamp, $fromTime);
  217. }
  218. return $formatter->formatTimeSpan($timestamp, $fromTime);
  219. }
  220. function html_select_options($options, $selected, $params=array()) {
  221. if (!is_array($selected)) {
  222. $selected=array($selected);
  223. }
  224. if (isset($params['combine']) && $params['combine']) {
  225. $options = array_combine($options, $options);
  226. }
  227. $value_name = $label_name = false;
  228. if (isset($params['value'])) {
  229. $value_name = $params['value'];
  230. }
  231. if (isset($params['label'])) {
  232. $label_name = $params['label'];
  233. }
  234. $html = '';
  235. foreach($options as $value => $label) {
  236. if ($value_name && is_array($label)) {
  237. $value = $label[$value_name];
  238. }
  239. if ($label_name && is_array($label)) {
  240. $label = $label[$label_name];
  241. }
  242. $select = in_array($value, $selected) ? ' selected="selected"' : '';
  243. $html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n";
  244. }
  245. return $html;
  246. }