1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | function embed_url($url) { |
---|
5 | |
---|
6 | $max_w = 440; |
---|
7 | $max_i = 300; |
---|
8 | |
---|
9 | $url = str_replace("/#/", "/", $url); |
---|
10 | $url = str_replace("/#!/", "/", $url); |
---|
11 | |
---|
12 | |
---|
13 | $p = parse_url($url); |
---|
14 | $host = $p["host"]; |
---|
15 | $host = preg_replace(",^www\.,", "", $host); |
---|
16 | $host = str_replace(".", "-", $host); |
---|
17 | |
---|
18 | // détecter les cas de mixed content |
---|
19 | $mixed_content = false; |
---|
20 | if ($p['scheme'] == 'http' and (!empty($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == 'on')) { |
---|
21 | $mixed_content = true; |
---|
22 | } |
---|
23 | |
---|
24 | $fichier = md5($url).".php"; |
---|
25 | $dossier = substr(md5($url), 0, 3); |
---|
26 | |
---|
27 | // Gérer les elements de dropbox (remplacer www par dl) |
---|
28 | $url = preg_replace("/^(https\:\/\/)(www)(\.dropbox\.com\/.*\/.*\/.*?)(\?dl=[01])?$/", '\1dl\3', $url); |
---|
29 | |
---|
30 | // Gérer les elements de commons.wikimedia |
---|
31 | if (preg_match("/^https?\:\/\/commons\.wikimedia\.org\/wiki\/File\:(.*)/i", $url, $regs)) { |
---|
32 | $md5 = md5($regs[1]); |
---|
33 | $url = 'https://upload.wikimedia.org/wikipedia/commons/' . $md5[0] . '/' . $md5[0] . $md5[1] . '/' . urlencode($regs[1]); |
---|
34 | } |
---|
35 | |
---|
36 | // Si l'embed a deja été sauvegardé |
---|
37 | if (file_exists(_DIR_CACHE."$host/$dossier/$fichier")) { |
---|
38 | $html = implode("", file(_DIR_CACHE."$host/$dossier/$fichier")); |
---|
39 | if (strlen($html) > 0) return $html; |
---|
40 | } else { // Si pas sauvegardé |
---|
41 | // Gérer les images Flickr à part |
---|
42 | // car autoembed ne gère que les vidéos de Flickr |
---|
43 | // sets |
---|
44 | if (preg_match(",^https?\://(www\.)?flickr\.com/+photos/+[^/]*/+sets/[^/]+,i", $url, $r)) { |
---|
45 | if ($page = @join("",file($r[0]))) { |
---|
46 | if (preg_match(',<meta property="og:image" content="(.*)" />,', $page, $i1)) { |
---|
47 | $img = $i1[1]; |
---|
48 | $code_ae = "<div class='oembed-container oembed-img'><a href='$url'><img src='$img' alt='Flickr' style='max-width: ".$max_w."px; max-height: ".$max_i."px;'/></a></div>"; |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | if (preg_match("/^https?\:\/\/(www\.)?flickr\.com/i", $url)) { |
---|
53 | $oembed = "http://www.flickr.com/services/oembed/?format=json&url=".$url; |
---|
54 | $json = @join("",file($oembed)); |
---|
55 | |
---|
56 | $json = @json_decode($json); |
---|
57 | $img = $json->{'url'}; |
---|
58 | if ($img) $code_ae = "<div class='oembed-container oembed-img'><a href='$url'><img src='$img' alt='Flickr' style='max-width: ".$max_i."px; max-height: ".$max_i."px;'/></a></div>"; |
---|
59 | } |
---|
60 | else if (preg_match(",^https?\://(www\.)?instagram\.com/p/([a-z0-9]*)/,i", $url, $r)) { |
---|
61 | if ($page = @join("",file($r[0]))) { |
---|
62 | if (preg_match(',<meta property="og:image" content="(.*)" />,', $page, $i1)) { |
---|
63 | $img = $i1[1]; |
---|
64 | $code_ae = "<div class='oembed-container oembed-img'><a href='$url'><img src='$img' alt='Instagram' style='max-width: ".$max_w."px; max-height: ".$max_i."px;'/></a></div>"; |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | else if (preg_match("/^http\:\/\/(www\.)?pastebin\.com\/(.*)/i", $url, $regs)) { |
---|
69 | $val = $regs[2]; |
---|
70 | |
---|
71 | $html = "<iframe src='http://pastebin.com/embed_iframe.php?i=".$val."' style='border:none;width:100%;'></iframe>"; |
---|
72 | //$html = "<script src='http://pastebin.com/embed_js.php?i=".$val."'></script>"; |
---|
73 | $code_ae = "<div class='oembed-container oembed-code'>$html</div>"; |
---|
74 | |
---|
75 | } |
---|
76 | else if (preg_match("/^https?\:\/\/gist\.github\.com\/(.*)/i", $url, $regs)) { |
---|
77 | $html = file_get_contents($url); |
---|
78 | $tag = 'pre'; # extraire_balise |
---|
79 | if (preg_match( |
---|
80 | ",<$tag\b[^>]*(/>|>.*</$tag\b[^>]*>|>),UimsS", |
---|
81 | $html, $regs)) { |
---|
82 | $pre = $regs[0]; |
---|
83 | $code_ae = "<div class='oembed-container oembed-code'>$pre</div>"; |
---|
84 | } |
---|
85 | } |
---|
86 | else if (preg_match("/^https?\:\/\/(bl\.ocks|blockbuilder)\.org\/(\w+\/\w+)/i", $url, $regs)) { |
---|
87 | $urlb = "https://bl.ocks.org/".$regs[2]; |
---|
88 | $page = file_get_contents($urlb); |
---|
89 | if ($page) { |
---|
90 | if (preg_match(',<meta property="og:image" content="(.*)",Uims', $page, $i1)) { |
---|
91 | $thumbnail = $i1[1]; |
---|
92 | } |
---|
93 | if (preg_match(',<meta property="og:title" content="(.*)",Uims', $page, $i1)) { |
---|
94 | $title = $i1[1]; |
---|
95 | } |
---|
96 | if (preg_match(',<meta property="og:description" content="(.*)",Uims', $page, $i1)) { |
---|
97 | $author = preg_replace('/’s block.*$/i', '', $i1[1]); |
---|
98 | } |
---|
99 | |
---|
100 | if ($thumbnail) { |
---|
101 | $pre = "<figure> |
---|
102 | <a href=\"$url\" target=\"_blank\"><img src=\"$thumbnail\"></a> |
---|
103 | <figcaption> |
---|
104 | <a href=\"$url\">$title</a> <em class=\"author\"> - $author</em> |
---|
105 | </figcaption> |
---|
106 | </figure> |
---|
107 | "; |
---|
108 | } else { |
---|
109 | $pre = "<a href=\"$url\" target=\"_blank\">$title</a> <em class=\"author\"> - $author</em>"; |
---|
110 | } |
---|
111 | |
---|
112 | $code_ae = "<div class='oembed-container oembed-block'>$pre</div>"; |
---|
113 | } |
---|
114 | } |
---|
115 | else if (preg_match("/^https?\:\/\/(www\.)?soundcloud\.com/i", $url)) { |
---|
116 | $oembed = "https://soundcloud.com/oembed?format=json&url=".$url; |
---|
117 | $json = join("",file($oembed)); |
---|
118 | $json = json_decode($json); |
---|
119 | $html = $json->{'html'}; |
---|
120 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
121 | } |
---|
122 | else if (preg_match("/^http\:\/\/(www\.)?prezi\.com\/([^\/]+)\//i", $url, $r)) { |
---|
123 | $oembed = "http://prezi.com/api/embed/?id=".$r[2]; |
---|
124 | $json = join("",file($oembed)); |
---|
125 | $json = json_decode($json); |
---|
126 | $img = $json->{'embed_preview'}; |
---|
127 | if ($img) $code_ae = "<div class='oembed-container oembed-img'><a href='$url'><img src='$img' title='".str_replace("'", "'", $json->{'presentation'}->{'title'})."' /></a></div>"; |
---|
128 | } |
---|
129 | else if (preg_match("/^http\:\/\/(www\.)?slideshare\.net/i", $url)) { |
---|
130 | // Le JSON ne se décode pas correction, |
---|
131 | // je passe donc en XML |
---|
132 | $oembed = "http://www.slideshare.net/api/oembed/2?format=xml&url=".$url."&maxwidth=".$max_w; |
---|
133 | $xml = trim(join("",file($oembed))); |
---|
134 | if (preg_match(",<html>(.*)</html>,i", $xml, $regs)){ |
---|
135 | $html = $regs[1]; |
---|
136 | $html = html_entity_decode($html, ENT_QUOTES, "UTF-8"); |
---|
137 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
138 | } |
---|
139 | } |
---|
140 | else if (preg_match(",^https?://[^\"\'\`\<\>\@\*\$]*?\.mp3(\?.*)?$,i", $url) and !$mixed_content) { |
---|
141 | $html = file_get_contents(dirname(__FILE__).'/modeles/mp3.html'); |
---|
142 | $html = str_replace('{source}', htmlspecialchars($url), $html); |
---|
143 | $url_dewplayer = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/autoembed/modeles/dewplayer.swf'; |
---|
144 | $html = str_replace('{dewplayer}', $url_dewplayer, $html); |
---|
145 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
146 | } |
---|
147 | else if (preg_match(",^https?://[^\"\'\`\<\>\@\*\$]*?\.ogg(\?.*)?$,i", $url)) { |
---|
148 | $html = "<div class='audio'><audio controls><source src='$url' rel='enclosure'></audio></div>"; |
---|
149 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
150 | } |
---|
151 | else if (preg_match(",^https?://[^\"\'\`\<\>\@\*\$]*?\.mp4(\?.*)?$,i", $url)) { |
---|
152 | $html = "<div class='video' style='height:0;position: relative; padding-bottom: 56.25%;'><video controls style='max-width: 100%;max-height: 100%;position:absolute'><source src='$url' rel='enclosure'></video></div>"; |
---|
153 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
154 | |
---|
155 | } |
---|
156 | else { |
---|
157 | require_once "AutoEmbed.class.php"; |
---|
158 | $AE = new AutoEmbed(); |
---|
159 | |
---|
160 | |
---|
161 | // load the embed source from a remote url |
---|
162 | if (!$AE->parseUrl($url)) { |
---|
163 | $code_ae = ""; |
---|
164 | // No embeddable video found (or supported) |
---|
165 | } else { |
---|
166 | $AE->setParam('autoplay','false'); |
---|
167 | |
---|
168 | |
---|
169 | $attributs = $AE->getObjectAttribs(); |
---|
170 | $w = $attributs["width"]; |
---|
171 | $h = $attributs["height"]; |
---|
172 | |
---|
173 | //print_r($attributs); |
---|
174 | if ($w > $max_w) { |
---|
175 | $rapport = $w / $max_w; |
---|
176 | |
---|
177 | $w = round($w / $rapport); |
---|
178 | $h = round($h / $rapport); |
---|
179 | |
---|
180 | $AE->setWidth($w); |
---|
181 | $AE->setHeight($h); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | $embed = $AE->getEmbedCode(); |
---|
186 | $vignette = $AE->getImageURL(); |
---|
187 | |
---|
188 | $source = $AE->getStub("title"); |
---|
189 | |
---|
190 | if ($source == "YouTube") { |
---|
191 | $embed = rawurlencode($embed); |
---|
192 | $embed = "<div onclick=\"this.innerHTML = (decodeURIComponent('$embed'));\" style='position: relative; width:100%; height:0; padding-bottom:56.3636363636%; background: url($vignette) center center; background-size:cover; cursor: pointer;'></div>"; |
---|
193 | } |
---|
194 | |
---|
195 | if ($source == "Twitpic" OR $source == "500px") { |
---|
196 | $embed = "<a href='$url'><img src='$vignette' alt='' style='max-width:200px; max-height: 200px;' /></a>"; |
---|
197 | } |
---|
198 | |
---|
199 | // inserer une "class=oembed-source-mp3audio" |
---|
200 | $src = preg_replace(',[^\w]+,', '', strtolower($source)); |
---|
201 | |
---|
202 | $code_ae = "<div class='oembed-container oembed-source-$src'>".$embed."</div>"; |
---|
203 | |
---|
204 | |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | if ($code_ae) { |
---|
209 | // Créer dossier si nécessaire |
---|
210 | if (!is_dir(_DIR_CACHE."$host")) mkdir(_DIR_CACHE."$host"); |
---|
211 | if (!is_dir(_DIR_CACHE."$host/$dossier")) mkdir(_DIR_CACHE."$host/$dossier"); |
---|
212 | |
---|
213 | $f = fopen(_DIR_CACHE."$host/$dossier/$fichier", "w"); |
---|
214 | |
---|
215 | fwrite($f, $code_ae); |
---|
216 | fclose($f); |
---|
217 | } |
---|
218 | |
---|
219 | return $code_ae; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | ?> |
---|