]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
Change fallback logic
[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\Hook;
15 use Friendica\Core\Logger;
16 use Friendica\Core\System;
17 use Friendica\Util\Network;
18 use Friendica\Util\ParseUrl;
19 use Friendica\Util\Strings;
20
21 function parse_url_content(App $a)
22 {
23         $text = null;
24         $str_tags = '';
25         $format = '';
26         $ret= ['success' => false, 'contentType' => ''];
27
28         $br = "\n";
29
30         if (!empty($_GET['binurl']) && Strings::isHex($_GET['binurl'])) {
31                 $url = trim(hex2bin($_GET['binurl']));
32         } elseif (!empty($_GET['url'])) {
33                 $url = trim($_GET['url']);
34         // fallback in case no url is valid
35         } else {
36                 Logger::info('No url given');
37                 exit();
38         }
39
40         if (!empty($_GET['title'])) {
41                 $title = strip_tags(trim($_GET['title']));
42         }
43
44         if (!empty($_GET['description'])) {
45                 $text = strip_tags(trim($_GET['description']));
46         }
47
48         if (!empty($_GET['tags'])) {
49                 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
50                 if (count($arr_tags)) {
51                         $str_tags = $br . implode(' ', $arr_tags) . $br;
52                 }
53         }
54
55         if (isset($_GET['format']) && $_GET['format'] == 'json') {
56                 $format = 'json';
57         }
58
59         // Add url scheme if it is missing
60         $arrurl = parse_url($url);
61         if (empty($arrurl['scheme'])) {
62                 if (!empty($arrurl['host'])) {
63                         $url = 'http:' . $url;
64                 } else {
65                         $url = 'http://' . $url;
66                 }
67         }
68
69         Logger::log($url);
70
71         // Check if the URL is an image, video or audio file. If so format
72         // the URL with the corresponding BBCode media tag
73         $redirects = 0;
74         // Fetch the header of the URL
75         $curlResponse = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
76
77         if ($curlResponse->isSuccess()) {
78                 // Convert the header fields into an array
79                 $hdrs = [];
80                 $h = explode("\n", $curlResponse->getHeader());
81                 foreach ($h as $l) {
82                         $header = array_map('trim', explode(':', trim($l), 2));
83                         if (count($header) == 2) {
84                                 list($k, $v) = $header;
85                                 $hdrs[$k] = $v;
86                         }
87                 }
88                 $type = null;
89                 $content_type = '';
90                 $bbcode = '';
91                 if (array_key_exists('Content-Type', $hdrs)) {
92                         $type = $hdrs['Content-Type'];
93                 }
94                 if ($type) {
95                         if (stripos($type, 'image/') !== false) {
96                                 $content_type = 'image';
97                                 $bbcode = $br . '[img]' . $url . '[/img]' . $br;
98                         }
99                         if (stripos($type, 'video/') !== false) {
100                                 $content_type = 'video';
101                                 $bbcode = $br . '[video]' . $url . '[/video]' . $br;
102                         }
103                         if (stripos($type, 'audio/') !== false) {
104                                 $content_type = 'audio';
105                                 $bbcode = $br . '[audio]' . $url . '[/audio]' . $br;
106                         }
107                 }
108                 if (!empty($content_type)) {
109                         if ($format == 'json') {
110                                 $ret['contentType'] = $content_type;
111                                 $ret['data'] = ['url' => $url];
112                                 $ret['success'] = true;
113                                 System::jsonExit($ret);
114                         }
115
116                         echo $bbcode;
117                         exit();
118                 }
119         }
120
121
122         $template = '[bookmark=%s]%s[/bookmark]%s';
123
124         $arr = ['url' => $url, 'text' => ''];
125
126         Hook::callAll('parse_link', $arr);
127
128         if (strlen($arr['text'])) {
129                 echo $arr['text'];
130                 exit();
131         }
132
133         // If there is already some content information submitted we don't
134         // need to parse the url for content.
135         if (!empty($url) && !empty($title) && !empty($text)) {
136                 $title = str_replace(["\r", "\n"], ['', ''], $title);
137
138                 $text = '[quote]' . trim($text) . '[/quote]' . $br;
139
140                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
141
142                 Logger::log('(unparsed): returns: ' . $result);
143
144                 echo $result;
145                 exit();
146         }
147
148         // Fetch the information directly from the webpage
149         $siteinfo = ParseUrl::getSiteinfo($url);
150
151         unset($siteinfo['keywords']);
152
153         // Bypass attachment if parse url for a comment
154         if (!empty($_GET['noAttachment'])) {
155                 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
156                 exit();
157         }
158
159         if ($format == 'json') {
160                 $ret['data'] = $siteinfo;
161                 $ret['contentType'] = 'attachment';
162                 $ret['success'] = true;
163
164                 System::jsonExit($ret);
165         }
166
167         // Format it as BBCode attachment
168         $info = add_page_info_data($siteinfo);
169
170         echo $info;
171
172         exit();
173 }
174
175 /**
176  * @brief Legacy function to call ParseUrl::getSiteinfoCached
177  *
178  * Note: We have moved the function to ParseUrl.php. This function is only for
179  * legacy support and will be remove in the future
180  *
181  * @param string $url         The url of the page which should be scraped
182  * @param bool   $no_guessing If true the parse doens't search for
183  *                            preview pictures
184  * @param bool   $do_oembed   The false option is used by the function fetch_oembed()
185  *                            to avoid endless loops
186  *
187  * @return array which contains needed data for embedding
188  *
189  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
190  * @see   ParseUrl::getSiteinfoCached()
191  *
192  * @deprecated since version 3.6 use ParseUrl::getSiteinfoCached instead
193  */
194 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
195 {
196         $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);
197         return $siteinfo;
198 }