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