Blame view

sources/lib/private/template/functions.php 5.23 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
  <?php
  /**
   * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
10
11
   * Prints a sanitized string
   * @param string|array $string the string which will be escaped and printed
31b7f2792   Kload   Upgrade to ownclo...
12
13
14
15
16
17
   */
  function p($string) {
  	print(OC_Util::sanitizeHTML($string));
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
18
19
20
   * Prints an unsanitized string - usage of this function may result into XSS.
   * Consider using p() instead.
   * @param string|array $string the string which will be printed as it is
31b7f2792   Kload   Upgrade to ownclo...
21
22
23
24
25
26
   */
  function print_unescaped($string) {
  	print($string);
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
27
   * make OC_Helper::linkTo available as a simple function
31b7f2792   Kload   Upgrade to ownclo...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
   * @param string $app app
   * @param string $file file
   * @param array $args array with param=>value, will be appended to the returned url
   * @return string link to the file
   *
   * For further information have a look at OC_Helper::linkTo
   */
  function link_to( $app, $file, $args = array() ) {
  	return OC_Helper::linkTo( $app, $file, $args );
  }
  
  /**
   * @param $key
   * @return string url to the online documentation
   */
  function link_to_docs($key) {
  	return OC_Helper::linkToDocs($key);
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
48
   * make OC_Helper::imagePath available as a simple function
31b7f2792   Kload   Upgrade to ownclo...
49
50
51
52
53
54
55
56
57
58
59
   * @param string $app app
   * @param string $image image
   * @return string link to the image
   *
   * For further information have a look at OC_Helper::imagePath
   */
  function image_path( $app, $image ) {
  	return OC_Helper::imagePath( $app, $image );
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
60
   * make OC_Helper::mimetypeIcon available as a simple function
31b7f2792   Kload   Upgrade to ownclo...
61
62
63
64
65
66
67
68
69
70
   * @param string $mimetype mimetype
   * @return string link to the image
   *
   * For further information have a look at OC_Helper::mimetypeIcon
   */
  function mimetype_icon( $mimetype ) {
  	return OC_Helper::mimetypeIcon( $mimetype );
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
71
   * make preview_icon available as a simple function
31b7f2792   Kload   Upgrade to ownclo...
72
   * Returns the path to the preview of the image.
6d9380f96   Cédric Dupont   Update sources OC...
73
74
   * @param string $path path of file
   * @return link to the preview
31b7f2792   Kload   Upgrade to ownclo...
75
76
77
78
79
80
   *
   * For further information have a look at OC_Helper::previewIcon
   */
  function preview_icon( $path ) {
  	return OC_Helper::previewIcon( $path );
  }
6d9380f96   Cédric Dupont   Update sources OC...
81
82
83
  /**
   * @param string $path
   */
31b7f2792   Kload   Upgrade to ownclo...
84
85
86
87
88
  function publicPreview_icon ( $path, $token ) {
  	return OC_Helper::publicPreviewIcon( $path, $token );
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
89
   * make OC_Helper::humanFileSize available as a simple function
31b7f2792   Kload   Upgrade to ownclo...
90
91
92
93
94
95
96
97
98
99
   * @param int $bytes size in bytes
   * @return string size as string
   *
   * For further information have a look at OC_Helper::humanFileSize
   */
  function human_file_size( $bytes ) {
  	return OC_Helper::humanFileSize( $bytes );
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
100
   * Strips the timestamp of its time value
31b7f2792   Kload   Upgrade to ownclo...
101
102
103
104
105
106
107
108
109
110
   * @param int $timestamp UNIX timestamp to strip
   * @return $timestamp without time value
   */
  function strip_time($timestamp){
  	$date = new \DateTime("@{$timestamp}");
  	$date->setTime(0, 0, 0);
  	return intval($date->format('U'));
  }
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
111
   * Formats timestamp relatively to the current time using
31b7f2792   Kload   Upgrade to ownclo...
112
113
114
115
   * a human-friendly format like "x minutes ago" or "yesterday"
   * @param int $timestamp timestamp to format
   * @param int $fromTime timestamp to compare from, defaults to current time
   * @param bool $dateOnly whether to strip time information
6d9380f96   Cédric Dupont   Update sources OC...
116
   * @return OC_L10N_String timestamp
31b7f2792   Kload   Upgrade to ownclo...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
   */
  function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
  	$l=OC_L10N::get('lib');
  	if (!isset($fromTime) || $fromTime === null){
  		$fromTime = time();
  	}
  	if ($dateOnly){
  		$fromTime = strip_time($fromTime);
  		$timestamp = strip_time($timestamp);
  	}
  	$timediff = $fromTime - $timestamp;
  	$diffminutes = round($timediff/60);
  	$diffhours = round($diffminutes/60);
  	$diffdays = round($diffhours/24);
  	$diffmonths = round($diffdays/31);
  
  	if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); }
  	else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
  	else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
  	else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); }
  	else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); }
  	// 86400 * 31 days = 2678400
  	else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); }
  	// 86400 * 60 days = 518400
  	else if($timediff < 5184000) { return $l->t('last month'); }
  	else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
  	// 86400 * 365.25 days * 2 = 63113852
  	else if($timediff < 63113852) { return $l->t('last year'); }
  	else { return $l->t('years ago'); }
  }
  
  function html_select_options($options, $selected, $params=array()) {
  	if (!is_array($selected)) {
  		$selected=array($selected);
  	}
  	if (isset($params['combine']) && $params['combine']) {
  		$options = array_combine($options, $options);
  	}
  	$value_name = $label_name = false;
  	if (isset($params['value'])) {
  		$value_name = $params['value'];
  	}
  	if (isset($params['label'])) {
  		$label_name = $params['label'];
  	}
  	$html = '';
  	foreach($options as $value => $label) {
  		if ($value_name && is_array($label)) {
  			$value = $label[$value_name];
  		}
  		if ($label_name && is_array($label)) {
  			$label = $label[$label_name];
  		}
  		$select = in_array($value, $selected) ? ' selected="selected"' : '';
  		$html .= '<option value="' . OC_Util::sanitizeHTML($value) . '"' . $select . '>' . OC_Util::sanitizeHTML($label) . '</option>'."
  ";
  	}
  	return $html;
  }