]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
Revert "Use last entry for Content-Type"
[friendica.git] / mod / parse_url.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * This module does parse an url for embeddable content (audio, video, image files or link)
21  * information and does format this information to BBCode
22  *
23  * @see ParseUrl::getSiteinfo() for more information about scraping embeddable content
24  */
25
26 use Friendica\App;
27 use Friendica\Content\PageInfo;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Logger;
30 use Friendica\Core\System;
31 use Friendica\DI;
32 use Friendica\Util\ParseUrl;
33 use Friendica\Util\Strings;
34
35 function parse_url_content(App $a)
36 {
37         $text = null;
38         $str_tags = '';
39         $format = '';
40         $ret= ['success' => false, 'contentType' => ''];
41
42         $br = "\n";
43
44         if (!empty($_GET['binurl']) && Strings::isHex($_GET['binurl'])) {
45                 $url = trim(hex2bin($_GET['binurl']));
46         } elseif (!empty($_GET['url'])) {
47                 $url = trim($_GET['url']);
48         // fallback in case no url is valid
49         } else {
50                 Logger::info('No url given');
51                 exit();
52         }
53
54         if (!empty($_GET['title'])) {
55                 $title = strip_tags(trim($_GET['title']));
56         }
57
58         if (!empty($_GET['description'])) {
59                 $text = strip_tags(trim($_GET['description']));
60         }
61
62         if (!empty($_GET['tags'])) {
63                 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
64                 if (count($arr_tags)) {
65                         $str_tags = $br . implode(' ', $arr_tags) . $br;
66                 }
67         }
68
69         if (isset($_GET['format']) && $_GET['format'] == 'json') {
70                 $format = 'json';
71         }
72
73         // Add url scheme if it is missing
74         $arrurl = parse_url($url);
75         if (empty($arrurl['scheme'])) {
76                 if (!empty($arrurl['host'])) {
77                         $url = 'http:' . $url;
78                 } else {
79                         $url = 'http://' . $url;
80                 }
81         }
82
83         Logger::log($url);
84
85         // Check if the URL is an image, video or audio file. If so format
86         // the URL with the corresponding BBCode media tag
87         // Fetch the header of the URL
88         $curlResponse = DI::httpRequest()->get($url, false, ['novalidate' => true, 'nobody' => true]);
89
90         if ($curlResponse->isSuccess()) {
91                 // Convert the header fields into an array
92                 $hdrs = [];
93                 $h = $curlResponse->getHeaders();
94                 foreach ($h as $l) {
95                         foreach ($l as $k => $v) {
96                                 if (empty($hdrs[$k])) {
97                                         $hdrs[$k] = $v;
98                                 }
99                                 $hdrs[$k] .= " " . $v;
100                         }
101                 }
102                 $type = null;
103                 $content_type = '';
104                 $bbcode = '';
105                 if (array_key_exists('Content-Type', $hdrs)) {
106                         $type = $hdrs['Content-Type'];
107                 }
108                 if ($type) {
109                         if (stripos($type, 'image/') !== false) {
110                                 $content_type = 'image';
111                                 $bbcode = $br . '[img]' . $url . '[/img]' . $br;
112                         }
113                         if (stripos($type, 'video/') !== false) {
114                                 $content_type = 'video';
115                                 $bbcode = $br . '[video]' . $url . '[/video]' . $br;
116                         }
117                         if (stripos($type, 'audio/') !== false) {
118                                 $content_type = 'audio';
119                                 $bbcode = $br . '[audio]' . $url . '[/audio]' . $br;
120                         }
121                 }
122                 if (!empty($content_type)) {
123                         if ($format == 'json') {
124                                 $ret['contentType'] = $content_type;
125                                 $ret['data'] = ['url' => $url];
126                                 $ret['success'] = true;
127                                 System::jsonExit($ret);
128                         }
129
130                         echo $bbcode;
131                         exit();
132                 }
133         }
134
135
136         $template = '[bookmark=%s]%s[/bookmark]%s';
137
138         $arr = ['url' => $url, 'text' => ''];
139
140         Hook::callAll('parse_link', $arr);
141
142         if (strlen($arr['text'])) {
143                 echo $arr['text'];
144                 exit();
145         }
146
147         // If there is already some content information submitted we don't
148         // need to parse the url for content.
149         if (!empty($url) && !empty($title) && !empty($text)) {
150                 $title = str_replace(["\r", "\n"], ['', ''], $title);
151
152                 $text = '[quote]' . trim($text) . '[/quote]' . $br;
153
154                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
155
156                 Logger::log('(unparsed): returns: ' . $result);
157
158                 echo $result;
159                 exit();
160         }
161
162         // Fetch the information directly from the webpage
163         $siteinfo = ParseUrl::getSiteinfo($url);
164
165         unset($siteinfo['keywords']);
166
167         // Bypass attachment if parse url for a comment
168         if (!empty($_GET['noAttachment'])) {
169                 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
170                 exit();
171         }
172
173         if ($format == 'json') {
174                 $ret['data'] = $siteinfo;
175                 $ret['contentType'] = 'attachment';
176                 $ret['success'] = true;
177
178                 System::jsonExit($ret);
179         }
180
181         // Format it as BBCode attachment
182         $info = "\n" . PageInfo::getFooterFromData($siteinfo);
183
184         echo $info;
185
186         exit();
187 }
188
189 /**
190  * Legacy function to call ParseUrl::getSiteinfoCached
191  *
192  * Note: We have moved the function to ParseUrl.php. This function is only for
193  * legacy support and will be remove in the future
194  *
195  * @param string $url         The url of the page which should be scraped
196  * @param bool   $no_guessing If true the parse doens't search for
197  *                            preview pictures
198  * @param bool   $do_oembed   The false option is used by the function fetch_oembed()
199  *                            to avoid endless loops
200  *
201  * @return array which contains needed data for embedding
202  *
203  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
204  * @see   ParseUrl::getSiteinfoCached()
205  *
206  * @deprecated since version 3.6 use ParseUrl::getSiteinfoCached instead
207  */
208 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
209 {
210         $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);
211         return $siteinfo;
212 }