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