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