functions.php 7.4 KB

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