Elgg  Version 6.1
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 
39 unset($vars['confirm']);
40 
41 $allowed_schemes = elgg_extract('allowed_schemes', $vars, ['http', 'https', 'ftp', 'sftp', 'ssh', 'file', 'mailto', 'tel']);
42 if ($allowed_schemes !== false && !is_array($allowed_schemes)) {
44 }
45 
46 unset($vars['allowed_schemes']);
47 
48 $url = elgg_extract('href', $vars);
49 if ($url === false) {
50  $url = 'javascript:void(0);';
51  $allowed_schemes = false;
52 }
53 
54 if (!$url && isset($vars['value'])) {
55  $url = trim($vars['value']);
56  unset($vars['value']);
57 }
58 
59 if (elgg_is_empty($url)) {
60  return;
61 }
62 
63 if (isset($vars['text'])) {
64  if (elgg_extract('encode_text', $vars, false)) {
65  $text = htmlspecialchars((string) $vars['text'], ENT_QUOTES, 'UTF-8', false);
66  } else {
67  $text = elgg_extract('text', $vars);
68  }
69 
70  unset($vars['text']);
71 } else {
72  $text = htmlspecialchars(elgg_get_excerpt((string) $url, $excerpt_length), ENT_QUOTES, 'UTF-8', false);
73 }
74 
75 unset($vars['encode_text']);
76 
77 if ($url) {
79 
80  // make sure the scheme of the URL is allowed
81  $scheme = parse_url($url, PHP_URL_SCHEME);
82  if (!empty($scheme) && $allowed_schemes !== false && !in_array($scheme, $allowed_schemes)) {
83  $url = "denied:{$url}";
84  }
85 
86  if (elgg_extract('is_action', $vars, false)) {
88  }
89 
90  $is_trusted = elgg_extract('is_trusted', $vars);
91  if (!$is_trusted) {
92  $url = strip_tags($url);
93  if (!isset($vars['rel'])) {
94  if ($is_trusted === null) {
95  $url_host = parse_url($url, PHP_URL_HOST);
97  $site_url_host = parse_url($site_url, PHP_URL_HOST);
98  $is_trusted = $url_host == $site_url_host;
99  }
100 
101  if ($is_trusted === false) {
102  // this is an external URL, which we do not want to be indexed by crawlers
103  $vars['rel'] = 'nofollow';
104  }
105  }
106  }
107 
108  $vars['href'] = $url;
109 }
110 
111 if (!isset($vars['title']) && isset($vars['data-confirm'])) {
112  $vars['title'] = elgg_extract('data-confirm', $vars);
113 }
114 
115 unset($vars['is_action']);
116 unset($vars['is_trusted']);
117 
118 $vars['class'] = elgg_extract_class($vars, 'elgg-anchor');
119 
120 if (!isset($vars['aria-label']) && !isset($vars['aria-labelledby']) && !isset($vars['title']) && empty(elgg_strip_tags((string) $text))) {
121  elgg_log('An output/url should have a discernible text (text, title, aria-label or aria-labelledby)', \Psr\Log\LogLevel::NOTICE);
122 }
123 
124 if (!elgg_is_empty($text)) {
125  $text = elgg_format_element('span', [
126  'class' => 'elgg-anchor-label',
127  ], $text);
128 }
129 
130 $icon = elgg_extract('icon', $vars, '');
131 unset($vars['icon']);
132 
133 if ($icon && !str_starts_with($icon, '<')) {
135  'class' => 'elgg-anchor-icon',
136  ]);
137 }
138 
139 $icon_alt = elgg_extract('icon_alt', $vars, '');
140 unset($vars['icon_alt']);
141 
142 if ($icon_alt && !str_starts_with($icon_alt, '<')) {
144  'class' => 'elgg-anchor-icon-alt',
145  ]);
146 }
147 
149 unset($vars['badge']);
150 
151 if (!is_null($badge)) {
152  $badge = elgg_format_element('span', ['class' => 'elgg-badge'], $badge);
153 }
154 
156 if (in_array('elgg-popup', $classes)) {
157  elgg_import_esm('elgg/popup');
158 }
159 
160 if (in_array('elgg-toggle', $classes)) {
161  elgg_import_esm('elgg/toggle');
162 }
163 
164 echo elgg_format_element('a', $vars, $icon . $text . $icon_alt . $badge);
if($icon &&!str_starts_with($icon, '<')) $icon_alt
Definition: url.php:139
elgg_get_excerpt(string $text, int $num_chars=250)
Returns an excerpt.
Definition: output.php:83
elgg_add_action_tokens_to_url(string $url, bool $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:154
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$excerpt_length
Elgg URL display Displays a URL as a link.
Definition: url.php:23
$allowed_schemes
Definition: url.php:41
$url
Definition: url.php:48
elgg_is_empty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: input.php:176
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
if(!is_null($badge)) $classes
Definition: url.php:155
elgg_import_esm(string $name)
Helper functions for external files like css/js.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
if(!isset($vars['aria-label'])&&!isset($vars['aria-labelledby'])&&!isset($vars['title'])&&empty(elgg_strip_tags((string) $text))) if(!elgg_is_empty($text)) $icon
Definition: url.php:130
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
$vars['class']
Definition: url.php:11
elgg_view_icon(string $name, array $vars=[])
View an icon glyph.
Definition: views.php:1261
if($icon_alt &&!str_starts_with($icon_alt, '<')) $badge
Definition: url.php:148
$site_url
Definition: upgrade.php:3
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:279
elgg_normalize_url(string $url)
Definition: output.php:163
$text
Definition: button.php:33
elgg_strip_tags(string $string, string $allowable_tags=null)
Strip tags and offer plugins the chance.
Definition: output.php:323