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