Elgg  Version 4.3
url.php
Go to the documentation of this file.
1 <?php
23 $excerpt_length = elgg_extract('excerpt_length', $vars, 100);
24 unset($vars['excerpt_length']);
25 
26 if (!empty($vars['confirm']) && !isset($vars['is_action'])) {
27  $vars['is_action'] = true;
28 }
29 
30 if (!empty($vars['confirm'])) {
31  $vars['data-confirm'] = elgg_extract('confirm', $vars);
32 
33  // if (bool) true use defaults
34  if ($vars['data-confirm'] === true) {
35  $vars['data-confirm'] = elgg_echo('question:areyousure');
36  }
37 }
38 unset($vars['confirm']);
39 
40 $allowed_schemes = elgg_extract('allowed_schemes', $vars, ['http', 'https', 'ftp', 'sftp', 'ssh', 'file']);
41 if ($allowed_schemes !== false && !is_array($allowed_schemes)) {
43 }
44 unset($vars['allowed_schemes']);
45 
46 $url = elgg_extract('href', $vars, null);
47 if ($url === false) {
48  $url = 'javascript:void(0);';
49  $allowed_schemes = false;
50 }
51 
52 if (!$url && isset($vars['value'])) {
53  $url = trim($vars['value']);
54  unset($vars['value']);
55 }
56 
57 if (isset($vars['text'])) {
58  if (elgg_extract('encode_text', $vars, false)) {
59  $text = htmlspecialchars($vars['text'], ENT_QUOTES, 'UTF-8', false);
60  } else {
61  $text = elgg_extract('text', $vars);
62  }
63  unset($vars['text']);
64 } else {
65  $text = htmlspecialchars(elgg_get_excerpt($url, $excerpt_length), ENT_QUOTES, 'UTF-8', false);
66 }
67 
68 unset($vars['encode_text']);
69 
70 if ($url) {
72 
73  // make sure the scheme of the URL is allowed
74  $scheme = parse_url($url, PHP_URL_SCHEME);
75  if (!empty($scheme) && $allowed_schemes !== false && !in_array($scheme, $allowed_schemes)) {
76  $url = "denied:{$url}";
77  }
78 
79  if (elgg_extract('is_action', $vars, false)) {
81  }
82 
83  $is_trusted = elgg_extract('is_trusted', $vars);
84  if (!$is_trusted) {
85  $url = strip_tags($url);
86  if (!isset($vars['rel'])) {
87  if ($is_trusted === null) {
88  $url_host = parse_url($url, PHP_URL_HOST);
90  $site_url_host = parse_url($site_url, PHP_URL_HOST);
91  $is_trusted = $url_host == $site_url_host;
92  }
93  if ($is_trusted === false) {
94  // this is an external URL, which we do not want to be indexed by crawlers
95  $vars['rel'] = 'nofollow';
96  }
97  }
98  }
99 
100  $vars['href'] = $url;
101 }
102 
103 if (!isset($vars['title']) && isset($vars['data-confirm'])) {
104  $vars['title'] = elgg_extract('data-confirm', $vars);
105 }
106 
107 unset($vars['is_action']);
108 unset($vars['is_trusted']);
109 
110 $vars['class'] = elgg_extract_class($vars, 'elgg-anchor');
111 
112 if ($text !== false && $text !== '') {
113  $text = elgg_format_element('span', [
114  'class' => 'elgg-anchor-label',
115  ], $text);
116 }
117 
118 $icon = elgg_extract('icon', $vars, '');
119 unset($vars['icon']);
120 
121 if ($icon && !preg_match('/^</', $icon)) {
123  'class' => 'elgg-anchor-icon',
124  ]);
125 }
126 
127 $icon_alt = elgg_extract('icon_alt', $vars, '');
128 unset($vars['icon_alt']);
129 
130 if ($icon_alt && !preg_match('/^</', $icon_alt)) {
132  'class' => 'elgg-anchor-icon-alt',
133  ]);
134 }
135 
137 unset($vars['badge']);
138 
139 if (!is_null($badge)) {
141  '#tag_name' => 'span',
142  '#text' => $badge,
143  'class' => 'elgg-badge',
144  ]);
145 }
146 
148 if (in_array('elgg-popup', $classes)) {
149  elgg_require_js('elgg/popup');
150 }
151 if (in_array('elgg-toggle', $classes)) {
152  elgg_require_js('elgg/toggle');
153 }
154 
156 if ($rel === 'popup' || $rel === 'toggle') {
157  elgg_deprecated_notice("Using rel='{$rel}' has been deprecated. Use class='elgg-{$rel}' and require 'elgg\\{$rel}' if not using the 'output/url' view.", '4.3');
158 }
159 
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:452
if($text!==false &&$text!== '') $icon
Definition: url.php:118
elgg_normalize_url($url)
Definition: output.php:153
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: deprecation.php:52
$excerpt_length
Elgg URL display Displays a URL as a link.
Definition: url.php:23
if($icon_alt &&!preg_match('/^</', $icon_alt) $badge)
Definition: url.php:136
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:135
$allowed_schemes
Definition: url.php:40
$url
Definition: url.php:46
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:569
if($icon &&!preg_match('/^</', $icon) $icon_alt)
Definition: url.php:127
if(!is_null($badge)) $classes
Definition: url.php:147
elgg_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:135
if(in_array('elgg-popup', $classes)) if(in_array('elgg-toggle', $classes)) $rel
Definition: url.php:155
elgg_require_js($name)
Request that Elgg load an AMD module onto the page.
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
$vars['class']
Definition: url.php:11
elgg_view_icon($name, $vars=[])
View an icon glyph.
Definition: views.php:1297
$site_url
Definition: upgrade.php:3
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:72
elgg echo
Translates a string.
Definition: deprecated.js:530
$text
Definition: button.php:32