url.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Elgg URL display
  4. * Displays a URL as a link
  5. *
  6. * @package Elgg
  7. * @subpackage Core
  8. *
  9. * @uses string $vars['text'] The string between the <a></a> tags.
  10. * @uses string $vars['href'] The unencoded url string
  11. * @uses bool $vars['encode_text'] Run $vars['text'] through htmlspecialchars() (false)
  12. * @uses bool $vars['is_action'] Is this a link to an action (false)
  13. * @uses bool $vars['is_trusted'] Is this link trusted (false)
  14. * @uses mixed $vars['confirm'] Confirmation dialog text | (bool) true
  15. *
  16. * Note: if confirm is set to true or has dialog text 'is_action' will default to true
  17. *
  18. */
  19. if (!empty($vars['confirm']) && !isset($vars['is_action'])) {
  20. $vars['is_action'] = true;
  21. }
  22. if (!empty($vars['confirm'])) {
  23. $vars['data-confirm'] = elgg_extract('confirm', $vars, elgg_echo('question:areyousure'));
  24. // if (bool) true use defaults
  25. if ($vars['data-confirm'] === true) {
  26. $vars['data-confirm'] = elgg_echo('question:areyousure');
  27. }
  28. }
  29. $url = elgg_extract('href', $vars, null);
  30. if (!$url && isset($vars['value'])) {
  31. $url = trim($vars['value']);
  32. unset($vars['value']);
  33. }
  34. if (isset($vars['text'])) {
  35. if (elgg_extract('encode_text', $vars, false)) {
  36. $text = htmlspecialchars($vars['text'], ENT_QUOTES, 'UTF-8', false);
  37. } else {
  38. $text = $vars['text'];
  39. }
  40. unset($vars['text']);
  41. } else {
  42. $text = htmlspecialchars($url, ENT_QUOTES, 'UTF-8', false);
  43. }
  44. unset($vars['encode_text']);
  45. if ($url) {
  46. $url = elgg_normalize_url($url);
  47. if (elgg_extract('is_action', $vars, false)) {
  48. $url = elgg_add_action_tokens_to_url($url, false);
  49. }
  50. $is_trusted = elgg_extract('is_trusted', $vars);
  51. if (!$is_trusted) {
  52. $url = strip_tags($url);
  53. if (!isset($vars['rel'])) {
  54. if ($is_trusted === null) {
  55. $url_host = parse_url($url, PHP_URL_HOST);
  56. $site_url = elgg_get_site_url();
  57. $site_url_host = parse_url($site_url, PHP_URL_HOST);
  58. $is_trusted = $url_host == $site_url_host;
  59. }
  60. if ($is_trusted === false) {
  61. // this is an external URL, which we do not want to be indexed by crawlers
  62. $vars['rel'] = 'nofollow';
  63. }
  64. }
  65. }
  66. $vars['href'] = $url;
  67. }
  68. if (!isset($vars['title']) && isset($vars['data-confirm'])) {
  69. $vars['title'] = $vars['data-confirm'];
  70. }
  71. unset($vars['is_action']);
  72. unset($vars['is_trusted']);
  73. unset($vars['confirm']);
  74. $attributes = elgg_format_attributes($vars);
  75. echo "<a $attributes>$text</a>";