1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
<?php
/**
* Output functions
* Processing text for output such as pulling out URLs and extracting excerpts
*
* @package Elgg
* @subpackage Core
*/
/**
* Takes a string and turns any URLs into formatted links
*
* @param string $text The input string
*
* @return string The output string with formatted links
*/
function parse_urls($text) {
// URI specification: http://www.ietf.org/rfc/rfc3986.txt
// This varies from the specification in the following ways:
// * Supports non-ascii characters
// * Does not allow parentheses and single quotes
// * Cuts off commas, exclamation points, and periods off as last character
// @todo this causes problems with <attr = "val">
// must be in <attr="val"> format (no space).
// By default htmlawed rewrites tags to this format.
// if PHP supported conditional negative lookbehinds we could use this:
// $r = preg_replace_callback('/(?<!=)(?<![ ])?(?<!["\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\!\(\),]+)/i',
$r = preg_replace_callback('/(?<![=\/"\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\(\)]+)/i',
create_function(
'$matches',
'
$url = $matches[1];
$punc = \'\';
$last = substr($url, -1, 1);
if (in_array($last, array(".", "!", ","))) {
$punc = $last;
$url = rtrim($url, ".!,");
}
$urltext = str_replace("/", "/<wbr />", $url);
return "<a href=\"$url\" rel=\"nofollow\">$urltext</a>$punc";
'
), $text);
return $r;
}
/**
* Create paragraphs from text with line spacing
*
* @param string $pee The string
* @deprecated Use elgg_autop instead
* @todo Add deprecation warning in 1.9
*
* @return string
**/
function autop($pee) {
return elgg_autop($pee);
}
/**
* Create paragraphs from text with line spacing
*
* @param string $string The string
*
* @return string
**/
function elgg_autop($string) {
return ElggAutoP::getInstance()->process($string);
}
/**
* Returns an excerpt.
* Will return up to n chars stopping at the nearest space.
* If no spaces are found (like in Japanese) will crop off at the
* n char mark. Adds ... if any text was chopped.
*
* @param string $text The full text to excerpt
* @param int $num_chars Return a string up to $num_chars long
*
* @return string
* @since 1.7.2
*/
function elgg_get_excerpt($text, $num_chars = 250) {
$text = trim(elgg_strip_tags($text));
$string_length = elgg_strlen($text);
if ($string_length <= $num_chars) {
return $text;
}
// handle cases
$excerpt = elgg_substr($text, 0, $num_chars);
$space = elgg_strrpos($excerpt, ' ', 0);
// don't crop if can't find a space.
if ($space === FALSE) {
$space = $num_chars;
}
$excerpt = trim(elgg_substr($excerpt, 0, $space));
if ($string_length != elgg_strlen($excerpt)) {
$excerpt .= '...';
}
return $excerpt;
}
/**
* Handles formatting of ampersands in urls
*
* @param string $url The URL
*
* @return string
* @since 1.7.1
*/
function elgg_format_url($url) {
return preg_replace('/&(?!amp;)/', '&', $url);
}
/**
* Converts an associative array into a string of well-formed attributes
*
* @note usually for HTML, but could be useful for XML too...
*
* @param array $attrs An associative array of attr => val pairs
*
* @return string HTML attributes to be inserted into a tag (e.g., <tag $attrs>)
*/
function elgg_format_attributes(array $attrs) {
$attrs = elgg_clean_vars($attrs);
$attributes = array();
if (isset($attrs['js'])) {
//@todo deprecated notice?
if (!empty($attrs['js'])) {
$attributes[] = $attrs['js'];
}
unset($attrs['js']);
}
foreach ($attrs as $attr => $val) {
$attr = strtolower($attr);
if ($val === TRUE) {
$val = $attr; //e.g. checked => TRUE ==> checked="checked"
}
// ignore $vars['entity'] => ElggEntity stuff
if ($val !== NULL && $val !== false && (is_array($val) || !is_object($val))) {
// allow $vars['class'] => array('one', 'two');
// @todo what about $vars['style']? Needs to be semi-colon separated...
if (is_array($val)) {
$val = implode(' ', $val);
}
$val = htmlspecialchars($val, ENT_QUOTES, 'UTF-8', false);
$attributes[] = "$attr=\"$val\"";
}
}
return implode(' ', $attributes);
}
/**
* Preps an associative array for use in {@link elgg_format_attributes()}.
*
* Removes all the junk that {@link elgg_view()} puts into $vars.
* Maintains backward compatibility with attributes like 'internalname' and 'internalid'
*
* @note This function is called automatically by elgg_format_attributes(). No need to
* call it yourself before using elgg_format_attributes().
*
* @param array $vars The raw $vars array with all it's dirtiness (config, url, etc.)
*
* @return array The array, ready to be used in elgg_format_attributes().
* @access private
*/
function elgg_clean_vars(array $vars = array()) {
unset($vars['config']);
unset($vars['url']);
unset($vars['user']);
// backwards compatibility code
if (isset($vars['internalname'])) {
$vars['name'] = $vars['internalname'];
unset($vars['internalname']);
}
if (isset($vars['internalid'])) {
$vars['id'] = $vars['internalid'];
unset($vars['internalid']);
}
if (isset($vars['__ignoreInternalid'])) {
unset($vars['__ignoreInternalid']);
}
if (isset($vars['__ignoreInternalname'])) {
unset($vars['__ignoreInternalname']);
}
return $vars;
}
/**
* Converts shorthand urls to absolute urls.
*
* If the url is already absolute or protocol-relative, no change is made.
*
* @example
* elgg_normalize_url(''); // 'http://my.site.com/'
* elgg_normalize_url('dashboard'); // 'http://my.site.com/dashboard'
* elgg_normalize_url('http://google.com/'); // no change
* elgg_normalize_url('//google.com/'); // no change
*
* @param string $url The URL to normalize
*
* @return string The absolute url
*/
function elgg_normalize_url($url) {
// see https://bugs.php.net/bug.php?id=51192
// from the bookmarks save action.
$php_5_2_13_and_below = version_compare(PHP_VERSION, '5.2.14', '<');
$php_5_3_0_to_5_3_2 = version_compare(PHP_VERSION, '5.3.0', '>=') &&
version_compare(PHP_VERSION, '5.3.3', '<');
if ($php_5_2_13_and_below || $php_5_3_0_to_5_3_2) {
$tmp_address = str_replace("-", "", $url);
$validated = filter_var($tmp_address, FILTER_VALIDATE_URL);
} else {
$validated = filter_var($url, FILTER_VALIDATE_URL);
}
// work around for handling absoluate IRIs (RFC 3987) - see #4190
if (!$validated && (strpos($url, 'http:') === 0) || (strpos($url, 'https:') === 0)) {
$validated = true;
}
if ($validated) {
// all normal URLs including mailto:
return $url;
} elseif (preg_match("#^(\#|\?|//)#i", $url)) {
// '//example.com' (Shortcut for protocol.)
// '?query=test', #target
return $url;
} elseif (stripos($url, 'javascript:') === 0 || stripos($url, 'mailto:') === 0) {
// 'javascript:' and 'mailto:'
// Not covered in FILTER_VALIDATE_URL
return $url;
} elseif (preg_match("#^[^/]*\.php(\?.*)?$#i", $url)) {
// 'install.php', 'install.php?step=step'
return elgg_get_site_url() . $url;
} elseif (preg_match("#^[^/]*\.#i", $url)) {
// 'example.com', 'example.com/subpage'
return "http://$url";
} else {
// 'page/handler', 'mod/plugin/file.php'
// trim off any leading / because the site URL is stored
// with a trailing /
return elgg_get_site_url() . ltrim($url, '/');
}
}
/**
* When given a title, returns a version suitable for inclusion in a URL
*
* @param string $title The title
*
* @return string The optimised title
* @since 1.7.2
*/
function elgg_get_friendly_title($title) {
// return a URL friendly title to short circuit normal title formatting
$params = array('title' => $title);
$result = elgg_trigger_plugin_hook('format', 'friendly:title', $params, NULL);
if ($result) {
return $result;
}
// titles are often stored HTML encoded
$title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$title = ElggTranslit::urlize($title);
return $title;
}
/**
* Formats a UNIX timestamp in a friendly way (eg "less than a minute ago")
*
* @see elgg_view_friendly_time()
*
* @param int $time A UNIX epoch timestamp
*
* @return string The friendly time string
* @since 1.7.2
*/
function elgg_get_friendly_time($time) {
// return a time string to short circuit normal time formatting
$params = array('time' => $time);
$result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, NULL);
if ($result) {
return $result;
}
$diff = time() - (int)$time;
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
if ($diff < $minute) {
return elgg_echo("friendlytime:justnow");
} else if ($diff < $hour) {
$diff = round($diff / $minute);
if ($diff == 0) {
$diff = 1;
}
if ($diff > 1) {
return elgg_echo("friendlytime:minutes", array($diff));
} else {
return elgg_echo("friendlytime:minutes:singular", array($diff));
}
} else if ($diff < $day) {
$diff = round($diff / $hour);
if ($diff == 0) {
$diff = 1;
}
if ($diff > 1) {
return elgg_echo("friendlytime:hours", array($diff));
} else {
return elgg_echo("friendlytime:hours:singular", array($diff));
}
} else {
$diff = round($diff / $day);
if ($diff == 0) {
$diff = 1;
}
if ($diff > 1) {
return elgg_echo("friendlytime:days", array($diff));
} else {
return elgg_echo("friendlytime:days:singular", array($diff));
}
}
}
/**
* Strip tags and offer plugins the chance.
* Plugins register for output:strip_tags plugin hook.
* Original string included in $params['original_string']
*
* @param string $string Formatted string
*
* @return string String run through strip_tags() and any plugin hooks.
*/
function elgg_strip_tags($string) {
$params['original_string'] = $string;
$string = strip_tags($string);
$string = elgg_trigger_plugin_hook('format', 'strip_tags', $params, $string);
return $string;
}
/**
* Apply html_entity_decode() to a string while re-entitising HTML
* special char entities to prevent them from being decoded back to their
* unsafe original forms.
*
* This relies on html_entity_decode() not translating entities when
* doing so leaves behind another entity, e.g. &gt; if decoded would
* create > which is another entity itself. This seems to escape the
* usual behaviour where any two paired entities creating a HTML tag are
* usually decoded, i.e. a lone > is not decoded, but <foo> would
* be decoded to <foo> since it creates a full tag.
*
* Note: This function is poorly explained in the manual - which is really
* bad given its potential for misuse on user input already escaped elsewhere.
* Stackoverflow is littered with advice to use this function in the precise
* way that would lead to user input being capable of injecting arbitrary HTML.
*
* @param string $string
*
* @return string
*
* @author Pádraic Brady
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license Released under dual-license GPL2/MIT by explicit permission of Pádraic Brady
*
* @access private
*/
function _elgg_html_decode($string) {
$string = str_replace(
array('>', '<', '&', '"', '''),
array('&gt;', '&lt;', '&amp;', '&quot;', '&#039;'),
$string
);
$string = html_entity_decode($string, ENT_NOQUOTES, 'UTF-8');
$string = str_replace(
array('&gt;', '&lt;', '&amp;', '&quot;', '&#039;'),
array('>', '<', '&', '"', '''),
$string
);
return $string;
}
/**
* Unit tests for Output
*
* @param string $hook unit_test
* @param string $type system
* @param mixed $value Array of tests
* @param mixed $params Params
*
* @return array
* @access private
*/
function output_unit_test($hook, $type, $value, $params) {
global $CONFIG;
$value[] = $CONFIG->path . 'engine/tests/api/output.php';
return $value;
}
/**
* Initialise the Output subsystem.
*
* @return void
* @access private
*/
function output_init() {
elgg_register_plugin_hook_handler('unit_test', 'system', 'output_unit_test');
}
elgg_register_event_handler('init', 'system', 'output_init');
|