forgotten $
[friendica.git/.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         $result = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
64
65         if ($result['success']) {
66                 // Convert the header fields into an array
67                 $hdrs = [];
68                 $h = explode("\n", $result['header']);
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                 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         $template = '[bookmark=%s]%s[/bookmark]%s';
96
97         $arr = ['url' => $url, 'text' => ''];
98
99         Addon::callHooks('parse_link', $arr);
100
101         if (strlen($arr['text'])) {
102                 echo $arr['text'];
103                 exit();
104         }
105
106         // If there is already some content information submitted we don't
107         // need to parse the url for content.
108         if (!empty($url) && !empty($title) && !empty($text)) {
109                 $title = str_replace(["\r", "\n"], ['', ''], $title);
110
111                 $text = '[quote]' . trim($text) . '[/quote]' . $br;
112
113                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
114
115                 logger('(unparsed): returns: ' . $result);
116
117                 echo $result;
118                 exit();
119         }
120
121         // Fetch the information directly from the webpage
122         $siteinfo = ParseUrl::getSiteinfo($url);
123
124         unset($siteinfo['keywords']);
125
126         // Format it as BBCode attachment
127         $info = add_page_info_data($siteinfo);
128
129         echo $info;
130
131         exit();
132 }
133
134 /**
135  * @brief Legacy function to call ParseUrl::getSiteinfoCached
136  *
137  * Note: We have moved the function to ParseUrl.php. This function is only for
138  * legacy support and will be remove in the future
139  *
140  * @param type $url The url of the page which should be scraped
141  * @param type $no_guessing If true the parse doens't search for
142  *    preview pictures
143  * @param type $do_oembed The false option is used by the function fetch_oembed()
144  *    to avoid endless loops
145  *
146  * @return array which contains needed data for embedding
147  *
148  * @see ParseUrl::getSiteinfoCached()
149  *
150  * @todo Remove this function after all Addons has been changed to use
151  *    ParseUrl::getSiteinfoCached
152  */
153 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
154 {
155         $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);
156         return $siteinfo;
157 }