absolute_url.inc

  1. nittany7 modules/contrib/feeds_tamper/plugins/absolute_url.inc
  2. cis7 modules/contrib/feeds_tamper/plugins/absolute_url.inc
  3. mooc7 modules/contrib/feeds_tamper/plugins/absolute_url.inc

Make URLs absolute.

Functions

File

modules/contrib/feeds_tamper/plugins/absolute_url.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Make URLs absolute.
  5. */
  6. $plugin = array(
  7. 'form' => 'feeds_tamper_absolute_url_form',
  8. 'callback' => 'feeds_tamper_absolute_url_callback',
  9. 'name' => 'Make URLs absolute',
  10. 'category' => 'HTML',
  11. );
  12. function feeds_tamper_absolute_url_form($importer, $element_key, $settings) {
  13. $form = array();
  14. $links = htmlentities('<a href="/stuff/things"> to <a href="http://example.com/stuff/things">');
  15. $form['info'] = array(
  16. '#markup' => t('Make URLs in markup absolute. (i.e. !links).', array('!links' => $links)),
  17. );
  18. return $form;
  19. }
  20. function feeds_tamper_absolute_url_callback($result, $item_key, $element_key, &$field, $settings) {
  21. static $dom;
  22. $b = parse_url($result->link);
  23. if (!isset($dom)) {
  24. $dom = new DOMDocument();
  25. }
  26. $dom->loadHTML($field);
  27. $urls = array();
  28. $tags = array('a' => 'href', 'img' => 'src', 'iframe' => 'src',
  29. 'script' => 'src', 'object' => 'codebase', 'link' => 'href',
  30. 'applet' => 'code', 'base' => 'href');
  31. foreach ($tags as $tag => $attr) {
  32. foreach ($dom->getElementsByTagName($tag) as $t) {
  33. $value = trim($t->getAttribute($attr));
  34. _feeds_tamper_absolute_url($value, $urls, $b);
  35. }
  36. }
  37. $field = strtr($field, $urls);
  38. }
  39. function _feeds_tamper_absolute_url($r_url, &$urls, $b) {
  40. $r = parse_url($r_url);
  41. if ($r == FALSE) {
  42. return;
  43. }
  44. if (!empty($r['scheme']) || !empty($r['host'])) {
  45. return;
  46. }
  47. $r['scheme'] = $b['scheme'];
  48. unset($r['port']);
  49. unset($r['user']);
  50. unset($r['pass']);
  51. // Copy base authority.
  52. $r['host'] = $b['host'];
  53. if (isset($b['port'])) {
  54. $r['port'] = $b['port'];
  55. }
  56. if (isset($b['user'])) {
  57. $r['user'] = $b['user'];
  58. }
  59. if (isset($b['pass'])) {
  60. $r['pass'] = $b['pass'];
  61. }
  62. // If relative URL has no path, use base path
  63. if (empty($r['path'])) {
  64. if (!empty($b['path'])) {
  65. $r['path'] = $b['path'];
  66. }
  67. if (!isset($r['query']) && isset($b['query'])) {
  68. $r['query'] = $b['query'];
  69. }
  70. $urls[$r_url] = _feeds_tamper_join_url($r);
  71. return;
  72. }
  73. // If relative URL path doesn't start with /, merge with base path
  74. if (strpos($r['path'], '/') !== 0) {
  75. if (empty($b['path'])) {
  76. $b['path'] = '';
  77. }
  78. $r['path'] = $b['path'] . '/' . $r['path'];
  79. }
  80. $urls[$r_url] = _feeds_tamper_join_url($r);
  81. }
  82. function _feeds_tamper_join_url($parts) {
  83. $url = '';
  84. if (!empty($parts['scheme'])) {
  85. $url .= $parts['scheme'] . ':';
  86. }
  87. if (isset($parts['host'])) {
  88. $url .= '//';
  89. if (isset($parts['user'])) {
  90. $url .= $parts['user'];
  91. if (isset($parts['pass'])) {
  92. $url .= ':' . $parts['pass'];
  93. }
  94. $url .= '@';
  95. }
  96. if (preg_match('/!^[\da-f]*:[\da-f.:]+$!ui/', $parts['host'])) {
  97. $url .= '[' . $parts['host'] . ']'; // IPv6
  98. }
  99. else
  100. $url .= $parts['host']; // IPv4 or name
  101. if (isset($parts['port'])) {
  102. $url .= ':' . $parts['port'];
  103. }
  104. if (!empty($parts['path']) && $parts['path'][0] != '/') {
  105. $url .= '/';
  106. }
  107. }
  108. if (!empty($parts['path'])) {
  109. $url .= $parts['path'];
  110. }
  111. if (isset($parts['query'])) {
  112. $url .= '?' . $parts['query'];
  113. }
  114. if (isset($parts['fragment'])) {
  115. $url .= '#' . $parts['fragment'];
  116. }
  117. return $url;
  118. }