Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.1KB

  1. <?php
  2. /**
  3. * @file mod/parse_url.php
  4. * @brief The parse_url module
  5. *
  6. * This module does parse an url for embeddable content (audio, video, image files or link)
  7. * information and does format this information to BBCode
  8. *
  9. * @see ParseUrl::getSiteinfo() for more information about scraping embeddable content
  10. */
  11. use Friendica\App;
  12. use Friendica\Core\Addon;
  13. use Friendica\Core\Logger;
  14. use Friendica\Util\Network;
  15. use Friendica\Util\ParseUrl;
  16. function parse_url_content(App $a)
  17. {
  18. $text = null;
  19. $str_tags = '';
  20. $br = "\n";
  21. if (!empty($_GET['binurl'])) {
  22. $url = trim(hex2bin($_GET['binurl']));
  23. } else {
  24. $url = trim($_GET['url']);
  25. }
  26. if (!empty($_GET['title'])) {
  27. $title = strip_tags(trim($_GET['title']));
  28. }
  29. if (!empty($_GET['description'])) {
  30. $text = strip_tags(trim($_GET['description']));
  31. }
  32. if (!empty($_GET['tags'])) {
  33. $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
  34. if (count($arr_tags)) {
  35. $str_tags = $br . implode(' ', $arr_tags) . $br;
  36. }
  37. }
  38. // Add url scheme if it is missing
  39. $arrurl = parse_url($url);
  40. if (empty($arrurl['scheme'])) {
  41. if (!empty($arrurl['host'])) {
  42. $url = 'http:' . $url;
  43. } else {
  44. $url = 'http://' . $url;
  45. }
  46. }
  47. Logger::log($url);
  48. // Check if the URL is an image, video or audio file. If so format
  49. // the URL with the corresponding BBCode media tag
  50. $redirects = 0;
  51. // Fetch the header of the URL
  52. $curlResponse = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
  53. if ($curlResponse->isSuccess()) {
  54. // Convert the header fields into an array
  55. $hdrs = [];
  56. $h = explode("\n", $curlResponse->getHeader());
  57. foreach ($h as $l) {
  58. $header = array_map('trim', explode(':', trim($l), 2));
  59. if (count($header) == 2) {
  60. list($k, $v) = $header;
  61. $hdrs[$k] = $v;
  62. }
  63. }
  64. $type = null;
  65. if (array_key_exists('Content-Type', $hdrs)) {
  66. $type = $hdrs['Content-Type'];
  67. }
  68. if ($type) {
  69. if (stripos($type, 'image/') !== false) {
  70. echo $br . '[img]' . $url . '[/img]' . $br;
  71. exit();
  72. }
  73. if (stripos($type, 'video/') !== false) {
  74. echo $br . '[video]' . $url . '[/video]' . $br;
  75. exit();
  76. }
  77. if (stripos($type, 'audio/') !== false) {
  78. echo $br . '[audio]' . $url . '[/audio]' . $br;
  79. exit();
  80. }
  81. }
  82. }
  83. $template = '[bookmark=%s]%s[/bookmark]%s';
  84. $arr = ['url' => $url, 'text' => ''];
  85. Addon::callHooks('parse_link', $arr);
  86. if (strlen($arr['text'])) {
  87. echo $arr['text'];
  88. exit();
  89. }
  90. // If there is already some content information submitted we don't
  91. // need to parse the url for content.
  92. if (!empty($url) && !empty($title) && !empty($text)) {
  93. $title = str_replace(["\r", "\n"], ['', ''], $title);
  94. $text = '[quote]' . trim($text) . '[/quote]' . $br;
  95. $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
  96. Logger::log('(unparsed): returns: ' . $result);
  97. echo $result;
  98. exit();
  99. }
  100. // Fetch the information directly from the webpage
  101. $siteinfo = ParseUrl::getSiteinfo($url);
  102. unset($siteinfo['keywords']);
  103. // Bypass attachment if parse url for a comment
  104. if (!empty($_GET['noAttachment'])) {
  105. echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
  106. exit();
  107. }
  108. // Format it as BBCode attachment
  109. $info = add_page_info_data($siteinfo);
  110. echo $info;
  111. exit();
  112. }
  113. /**
  114. * @brief Legacy function to call ParseUrl::getSiteinfoCached
  115. *
  116. * Note: We have moved the function to ParseUrl.php. This function is only for
  117. * legacy support and will be remove in the future
  118. *
  119. * @param type $url The url of the page which should be scraped
  120. * @param type $no_guessing If true the parse doens't search for
  121. * preview pictures
  122. * @param type $do_oembed The false option is used by the function fetch_oembed()
  123. * to avoid endless loops
  124. *
  125. * @return array which contains needed data for embedding
  126. *
  127. * @see ParseUrl::getSiteinfoCached()
  128. *
  129. * @todo Remove this function after all Addons has been changed to use
  130. * ParseUrl::getSiteinfoCached
  131. */
  132. function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
  133. {
  134. $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);
  135. return $siteinfo;
  136. }