Completed
Push — stable7 ( 35746e...825360 )
by
unknown
29:41
created

functions.php ➔ style()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 9
rs 9.6666
1
<?php
2
/**
3
 * Copyright (c) 2013 Bart Visscher <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
/**
10
 * Prints a sanitized string
11
 * @param string|array $string the string which will be escaped and printed
12
 */
13
function p($string) {
14
	print(OC_Util::sanitizeHTML($string));
15
}
16
17
/**
18
 * Prints an unsanitized string - usage of this function may result into XSS.
19
 * Consider using p() instead.
20
 * @param string|array $string the string which will be printed as it is
21
 */
22
function print_unescaped($string) {
23
	print($string);
24
}
25
26
/**
27
 * Shortcut for adding scripts to a page
28
 * @param string $app the appname
29
 * @param string|string[] $file the filename,
30
 * if an array is given it will add all scripts
31
 */
32
function script($app, $file) {
33
	if(is_array($file)) {
34
		foreach($file as $f) {
35
			OC_Util::addScript($app, $f);
36
		}
37
	} else {
38
		OC_Util::addScript($app, $file);
39
	}
40
}
41
42
/**
43
 * Shortcut for adding styles to a page
44
 * @param string $app the appname
45
 * @param string|string[] $file the filename,
46
 * if an array is given it will add all styles
47
 */
48
function style($app, $file) {
49
	if(is_array($file)) {
50
		foreach($file as $f) {
51
			OC_Util::addStyle($app, $f);
52
		}
53
	} else {
54
		OC_Util::addStyle($app, $file);
55
	}
56
}
57
58
/**
59
 * Shortcut for HTML imports
60
 * @param string $app the appname
61
 * @param string|string[] $file the path relative to the app's component folder,
62
 * if an array is given it will add all components
63
 */
64
function component($app, $file) {
65
	if(is_array($file)) {
66
		foreach($file as $f) {
67
			$url = link_to($app, 'component/' . $f . '.html');
68
			OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
69
		}
70
	} else {
71
		$url = link_to($app, 'component/' . $file . '.html');
72
		OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url));
73
	}
74
}
75
76
/**
77
 * make OC_Helper::linkTo available as a simple function
78
 * @param string $app app
79
 * @param string $file file
80
 * @param array $args array with param=>value, will be appended to the returned url
81
 * @return string link to the file
82
 *
83
 * For further information have a look at OC_Helper::linkTo
84
 */
85
function link_to( $app, $file, $args = array() ) {
86
	return OC_Helper::linkTo( $app, $file, $args );
87
}
88
89
/**
90
 * @param $key
91
 * @return string url to the online documentation
92
 */
93
function link_to_docs($key) {
94
	return OC_Helper::linkToDocs($key);
95
}
96
97
/**
98
 * make OC_Helper::imagePath available as a simple function
99
 * @param string $app app
100
 * @param string $image image
101
 * @return string link to the image
102
 *
103
 * For further information have a look at OC_Helper::imagePath
104
 */
105
function image_path( $app, $image ) {
106
	return OC_Helper::imagePath( $app, $image );
107
}
108
109
/**
110
 * make OC_Helper::mimetypeIcon available as a simple function
111
 * @param string $mimetype mimetype
112
 * @return string link to the image
113
 *
114
 * For further information have a look at OC_Helper::mimetypeIcon
115
 */
116
function mimetype_icon( $mimetype ) {
117
	return OC_Helper::mimetypeIcon( $mimetype );
118
}
119
120
/**
121
 * make preview_icon available as a simple function
122
 * Returns the path to the preview of the image.
123
 * @param string $path path of file
124
 * @return link to the preview
125
 *
126
 * For further information have a look at OC_Helper::previewIcon
127
 */
128
function preview_icon( $path ) {
129
	return OC_Helper::previewIcon( $path );
130
}
131
132
/**
133
 * @param string $path
134
 */
135
function publicPreview_icon ( $path, $token ) {
136
	return OC_Helper::publicPreviewIcon( $path, $token );
137
}
138
139
/**
140
 * make OC_Helper::humanFileSize available as a simple function
141
 * @param int $bytes size in bytes
142
 * @return string size as string
143
 *
144
 * For further information have a look at OC_Helper::humanFileSize
145
 */
146
function human_file_size( $bytes ) {
147
	return OC_Helper::humanFileSize( $bytes );
148
}
149
150
/**
151
 * Strips the timestamp of its time value
152
 * @param int $timestamp UNIX timestamp to strip
153
 * @return $timestamp without time value
0 ignored issues
show
Documentation introduced by
The doc-type $timestamp could not be parsed: Unknown type name "$timestamp" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
154
 */
155
function strip_time($timestamp){
156
	$date = new \DateTime("@{$timestamp}");
157
	$date->setTime(0, 0, 0);
158
	return intval($date->format('U'));
159
}
160
161
/**
162
 * Formats timestamp relatively to the current time using
163
 * a human-friendly format like "x minutes ago" or "yesterday"
164
 * @param int $timestamp timestamp to format
165
 * @param int $fromTime timestamp to compare from, defaults to current time
166
 * @param bool $dateOnly whether to strip time information
167
 * @return OC_L10N_String timestamp
168
 */
169
function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
170
	$l=OC_L10N::get('lib');
171
	if (!isset($fromTime) || $fromTime === null){
172
		$fromTime = time();
173
	}
174
	if ($dateOnly){
175
		$fromTime = strip_time($fromTime);
176
		$timestamp = strip_time($timestamp);
177
	}
178
	$timediff = $fromTime - $timestamp;
179
	$diffminutes = round($timediff/60);
180
	$diffhours = round($diffminutes/60);
181
	$diffdays = round($diffhours/24);
182
	$diffmonths = round($diffdays/31);
183
184
	if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); }
185
	else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
186
	else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
187
	else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); }
188
	else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); }
189
	// 86400 * 31 days = 2678400
190
	else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); }
191
	// 86400 * 60 days = 518400
192
	else if($timediff < 5184000) { return $l->t('last month'); }
193
	else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
194
	// 86400 * 365.25 days * 2 = 63113852
195
	else if($timediff < 63113852) { return $l->t('last year'); }
196
	else { return $l->t('years ago'); }
197
}
198
199
function html_select_options($options, $selected, $params=array()) {
200
	if (!is_array($selected)) {
201
		$selected=array($selected);
202
	}
203
	if (isset($params['combine']) && $params['combine']) {
204
		$options = array_combine($options, $options);
205
	}
206
	$value_name = $label_name = false;
207
	if (isset($params['value'])) {
208
		$value_name = $params['value'];
209
	}
210
	if (isset($params['label'])) {
211
		$label_name = $params['label'];
212
	}
213
	$html = '';
214
	foreach($options as $value => $label) {
215
		if ($value_name && is_array($label)) {
216
			$value = $label[$value_name];
217
		}
218
		if ($label_name && is_array($label)) {
219
			$label = $label[$label_name];
220
		}
221
		$select = in_array($value, $selected) ? ' selected="selected"' : '';
222
		$html .= '<option value="' . OC_Util::sanitizeHTML($value) . '"' . $select . '>' . OC_Util::sanitizeHTML($label) . '</option>'."\n";
223
	}
224
	return $html;
225
}
226