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