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