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