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