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