Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

152 řádky
3.7KB

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