]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
Merge pull request #7893 from annando/api-attachments
[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         // Fetch the header of the URL
74         $curlResponse = Network::curl($url, false, ['novalidate' => true, 'nobody' => true]);
75
76         if ($curlResponse->isSuccess()) {
77                 // Convert the header fields into an array
78                 $hdrs = [];
79                 $h = explode("\n", $curlResponse->getHeader());
80                 foreach ($h as $l) {
81                         $header = array_map('trim', explode(':', trim($l), 2));
82                         if (count($header) == 2) {
83                                 list($k, $v) = $header;
84                                 $hdrs[$k] = $v;
85                         }
86                 }
87                 $type = null;
88                 $content_type = '';
89                 $bbcode = '';
90                 if (array_key_exists('Content-Type', $hdrs)) {
91                         $type = $hdrs['Content-Type'];
92                 }
93                 if ($type) {
94                         if (stripos($type, 'image/') !== false) {
95                                 $content_type = 'image';
96                                 $bbcode = $br . '[img]' . $url . '[/img]' . $br;
97                         }
98                         if (stripos($type, 'video/') !== false) {
99                                 $content_type = 'video';
100                                 $bbcode = $br . '[video]' . $url . '[/video]' . $br;
101                         }
102                         if (stripos($type, 'audio/') !== false) {
103                                 $content_type = 'audio';
104                                 $bbcode = $br . '[audio]' . $url . '[/audio]' . $br;
105                         }
106                 }
107                 if (!empty($content_type)) {
108                         if ($format == 'json') {
109                                 $ret['contentType'] = $content_type;
110                                 $ret['data'] = ['url' => $url];
111                                 $ret['success'] = true;
112                                 System::jsonExit($ret);
113                         }
114
115                         echo $bbcode;
116                         exit();
117                 }
118         }
119
120
121         $template = '[bookmark=%s]%s[/bookmark]%s';
122
123         $arr = ['url' => $url, 'text' => ''];
124
125         Hook::callAll('parse_link', $arr);
126
127         if (strlen($arr['text'])) {
128                 echo $arr['text'];
129                 exit();
130         }
131
132         // If there is already some content information submitted we don't
133         // need to parse the url for content.
134         if (!empty($url) && !empty($title) && !empty($text)) {
135                 $title = str_replace(["\r", "\n"], ['', ''], $title);
136
137                 $text = '[quote]' . trim($text) . '[/quote]' . $br;
138
139                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
140
141                 Logger::log('(unparsed): returns: ' . $result);
142
143                 echo $result;
144                 exit();
145         }
146
147         // Fetch the information directly from the webpage
148         $siteinfo = ParseUrl::getSiteinfo($url);
149
150         unset($siteinfo['keywords']);
151
152         // Bypass attachment if parse url for a comment
153         if (!empty($_GET['noAttachment'])) {
154                 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
155                 exit();
156         }
157
158         if ($format == 'json') {
159                 $ret['data'] = $siteinfo;
160                 $ret['contentType'] = 'attachment';
161                 $ret['success'] = true;
162
163                 System::jsonExit($ret);
164         }
165
166         // Format it as BBCode attachment
167         $info = add_page_info_data($siteinfo);
168
169         echo $info;
170
171         exit();
172 }
173
174 /**
175  * @brief Legacy function to call ParseUrl::getSiteinfoCached
176  *
177  * Note: We have moved the function to ParseUrl.php. This function is only for
178  * legacy support and will be remove in the future
179  *
180  * @param string $url         The url of the page which should be scraped
181  * @param bool   $no_guessing If true the parse doens't search for
182  *                            preview pictures
183  * @param bool   $do_oembed   The false option is used by the function fetch_oembed()
184  *                            to avoid endless loops
185  *
186  * @return array which contains needed data for embedding
187  *
188  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
189  * @see   ParseUrl::getSiteinfoCached()
190  *
191  * @deprecated since version 3.6 use ParseUrl::getSiteinfoCached instead
192  */
193 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
194 {
195         $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);
196         return $siteinfo;
197 }