]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
parse_url: recognize image/video/audio files + move functions into own class
[friendica.git] / mod / parse_url.php
1 <?php
2 /** 
3  * @file mod/parse_url.php
4  * 
5  * @todo https://developers.google.com/+/plugins/snippet/
6  * 
7  * @verbatim
8  * <meta itemprop="name" content="Toller Titel">
9  * <meta itemprop="description" content="Eine tolle Beschreibung">
10  * <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
11  * 
12  * <body itemscope itemtype="http://schema.org/Product">
13  *   <h1 itemprop="name">Shiny Trinket</h1>
14  *   <img itemprop="image" src="{image-url}" />
15  *   <p itemprop="description">Shiny trinkets are shiny.</p>
16  * </body>
17  * @endverbatim
18 */
19
20 use \Friendica\ParseUrl;
21
22 require_once("include/items.php");
23
24 function parse_url_content(&$a) {
25
26         $text = null;
27         $str_tags = "";
28
29         $textmode = false;
30
31         if (local_user() && (!feature_enabled(local_user(), "richtext"))) {
32                 $textmode = true;
33         }
34
35         $br = (($textmode) ? "\n" : "<br />");
36
37         if (x($_GET,"binurl")) {
38                 $url = trim(hex2bin($_GET["binurl"]));
39         } else {
40                 $url = trim($_GET["url"]);
41         }
42
43         if ($_GET["title"]) {
44                 $title = strip_tags(trim($_GET["title"]));
45         }
46
47         if ($_GET["description"]) {
48                 $text = strip_tags(trim($_GET["description"]));
49         }
50
51         if ($_GET["tags"]) {
52                 $arr_tags = ParseUrl::convertTagsToArray($_GET["tags"]);
53                 if (count($arr_tags)) {
54                         $str_tags = $br . implode(" ", $arr_tags) . $br;
55                 }
56         }
57
58         // Add url scheme if it is missing
59         $arrurl = parse_url($url);
60         if (!x($arrurl, "scheme")) {
61                 if (x($arrurl, "host")) {
62                         $url = "http:".$url;
63                 } else {
64                         $url = "http://".$url;
65                 }
66         }
67
68         logger("prse_url: " . $url);
69
70         // If the URL is a image, video or audio file format the URL with the corresponding
71         // BBCode media tag
72         $redirects = 0;
73         // Fetch the header of the URL
74         $result = z_fetch_url($url, false, $redirects, array("novalidate" => true, "nobody" => true));
75         if($result["success"]) {
76                 // Convert the header fields into an array
77                 $hdrs = array();
78                 $h = explode("\n", $result["header"]);
79                 foreach ($h as $l) {
80                         list($k,$v) = array_map("trim", explode(":", trim($l), 2));
81                         $hdrs[$k] = $v;
82                 }
83                 if (array_key_exists("Content-Type", $hdrs)) {
84                         $type = $hdrs["Content-Type"];
85                 }
86                 if ($type) {
87                         if(stripos($type, "image/") !== false) {
88                                 echo $br . "[img]" . $url . "[/img]" . $br;
89                                 killme();
90                         }
91                         if (stripos($type, "video/") !== false) {
92                                 echo $br . "[video]" . $url . "[/video]" . $br;
93                                 killme();
94                         }
95                         if (stripos($type, "audio/") !== false) {
96                                 echo $br . "[audio]" . $url . "[/audio]" . $br;
97                                 killme();
98                         }
99                 }
100         }
101
102         if ($textmode) {
103                 $template = "[bookmark=%s]%s[/bookmark]%s";
104         } else {
105                 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
106         }
107
108         $arr = array("url" => $url, "text" => "");
109
110         call_hooks("parse_link", $arr);
111
112         if (strlen($arr["text"])) {
113                 echo $arr["text"];
114                 killme();
115         }
116
117
118         if ($url && $title && $text) {
119
120                 $title = str_replace(array("\r","\n"),array("",""),$title);
121
122                 if ($textmode) {
123                         $text = "[quote]" . trim($text) . "[/quote]" . $br;
124                 } else {
125                         $text = "<blockquote>" . htmlspecialchars(trim($text)) . "</blockquote><br />";
126                         $title = htmlspecialchars($title);
127                 }
128
129                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
130
131                 logger("parse_url (unparsed): returns: " . $result);
132
133                 echo $result;
134                 killme();
135         }
136
137         // Fetch the information from the webpage
138         $siteinfo = ParseUrl::getSiteinfo($url);
139
140         unset($siteinfo["keywords"]);
141
142         // Format it as BBCode attachment
143         $info = add_page_info_data($siteinfo);
144
145         if (!$textmode) {
146                 // Replace ' with ’ - not perfect - but the richtext editor has problems otherwise
147                 $info = str_replace(array("&#039;"), array("&#8217;"), $info);
148         }
149
150         echo $info;
151
152         killme();
153 }