1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | function embed_url($url) { |
---|
5 | $max_w = 440; |
---|
6 | $max_i = 300; |
---|
7 | |
---|
8 | $afficher_soundmanager = false ; |
---|
9 | //$afficher_soundmanager = true ; |
---|
10 | |
---|
11 | $url = str_replace("/#/", "/", $url); |
---|
12 | $url = str_replace("/#!/", "/", $url); |
---|
13 | |
---|
14 | |
---|
15 | $p = parse_url($url); |
---|
16 | $host = $p["host"]; |
---|
17 | $host = preg_replace(",^www\.,", "", $host); |
---|
18 | $host = str_replace(".", "-", $host); |
---|
19 | |
---|
20 | $fichier = md5($url).".php"; |
---|
21 | $dossier = substr(md5($url), 0, 3); |
---|
22 | |
---|
23 | // Si l'embed a deja été sauvegardé |
---|
24 | if (file_exists(_DIR_CACHE."$host/$dossier/$fichier")) { |
---|
25 | $html = implode("", file(_DIR_CACHE."$host/$dossier/$fichier")); |
---|
26 | if (strlen($html) > 0) return $html; |
---|
27 | } else { // Si pas sauvegardé |
---|
28 | // Gérer les images Flickr à part |
---|
29 | // car autoembed ne gère que les vidéos de Flickr |
---|
30 | // sets |
---|
31 | if (preg_match(",^https?\://(www\.)?flickr\.com/+photos/+[^/]*/+sets/[^/]+,i", $url, $r)) { |
---|
32 | if ($page = @join("",file($r[0]))) { |
---|
33 | if (preg_match(',<meta property="og:image" content="(.*)" />,', $page, $i1)) { |
---|
34 | $img = $i1[1]; |
---|
35 | $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>"; |
---|
36 | } |
---|
37 | } |
---|
38 | } |
---|
39 | if (preg_match("/^https?\:\/\/(www\.)?flickr\.com/i", $url)) { |
---|
40 | $oembed = "http://www.flickr.com/services/oembed/?format=json&url=".$url; |
---|
41 | $json = @join("",file($oembed)); |
---|
42 | |
---|
43 | $json = @json_decode($json); |
---|
44 | $img = $json->{'url'}; |
---|
45 | 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>"; |
---|
46 | } |
---|
47 | else if (preg_match("/^http\:\/\/(www\.)?pastebin\.com\/(.*)/i", $url, $regs)) { |
---|
48 | $val = $regs[2]; |
---|
49 | |
---|
50 | $html = "<iframe src='http://pastebin.com/embed_iframe.php?i=".$val."' style='border:none;width:100%;'></iframe>"; |
---|
51 | //$html = "<script src='http://pastebin.com/embed_js.php?i=".$val."'></script>"; |
---|
52 | $code_ae = "<div class='oembed-container oembed-code'>$html</div>"; |
---|
53 | |
---|
54 | } |
---|
55 | else if (preg_match("/^https?\:\/\/gist\.github\.com\/(.*)/i", $url, $regs)) { |
---|
56 | $html = file_get_contents($url); |
---|
57 | $tag = 'pre'; # extraire_balise |
---|
58 | if (preg_match( |
---|
59 | ",<$tag\b[^>]*(/>|>.*</$tag\b[^>]*>|>),UimsS", |
---|
60 | $html, $regs)) { |
---|
61 | $pre = $regs[0]; |
---|
62 | $code_ae = "<div class='oembed-container oembed-code'>$pre</div>"; |
---|
63 | } |
---|
64 | } |
---|
65 | else if (preg_match("/^http\:\/\/(www\.)?yfrog\.com/i", $url)) { |
---|
66 | $oembed = "http://www.yfrog.com/api/oembed?url?format=json&url=".$url; |
---|
67 | $json = join("",file($oembed)); |
---|
68 | $json = json_decode($json); |
---|
69 | $img = $json->{'url'}; |
---|
70 | 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>"; |
---|
71 | } |
---|
72 | else if (preg_match("/^http\:\/\/(www\.)?soundcloud\.com/i", $url)) { |
---|
73 | $oembed = "http://soundcloud.com/oembed/?format=json&url=".$url; |
---|
74 | $json = join("",file($oembed)); |
---|
75 | $json = json_decode($json); |
---|
76 | $html = $json->{'html'}; |
---|
77 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
78 | } |
---|
79 | else if (preg_match("/^http\:\/\/(www\.)?slideshare\.net/i", $url)) { |
---|
80 | // Le JSON ne se décode pas correction, |
---|
81 | // je passe donc en XML |
---|
82 | $oembed = "http://www.slideshare.net/api/oembed/2?format=xml&url=".$url."&maxwidth=".$max_w; |
---|
83 | $xml = trim(join("",file($oembed))); |
---|
84 | if (preg_match(",<html>(.*)</html>,i", $xml, $regs)){ |
---|
85 | $html = $regs[1]; |
---|
86 | $html = html_entity_decode($html, ENT_QUOTES, "UTF-8"); |
---|
87 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
88 | } |
---|
89 | } |
---|
90 | else if (preg_match(",^http://[^\"\'\`\<\>\@\*\$]*?\.mp3$,i", $url) and !$afficher_soundmanager) { |
---|
91 | |
---|
92 | $html = file_get_contents(dirname(__FILE__).'/modeles/mp3.html'); |
---|
93 | $html = str_replace('{source}', htmlspecialchars($url), $html); |
---|
94 | $url_dewplayer = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/modeles/dewplayer.swf'; |
---|
95 | $html = str_replace('{dewplayer}', $url_dewplayer, $html); |
---|
96 | if ($html) $code_ae = "<div class='oembed-container'>$html</div>"; |
---|
97 | } |
---|
98 | else { |
---|
99 | require_once "AutoEmbed.class.php"; |
---|
100 | $AE = new AutoEmbed(); |
---|
101 | |
---|
102 | |
---|
103 | // load the embed source from a remote url |
---|
104 | if (!$AE->parseUrl($url)) { |
---|
105 | $code_ae = ""; |
---|
106 | // No embeddable video found (or supported) |
---|
107 | } else { |
---|
108 | $AE->setParam('autoplay','false'); |
---|
109 | |
---|
110 | |
---|
111 | $attributs = $AE->getObjectAttribs(); |
---|
112 | $w = $attributs["width"]; |
---|
113 | $h = $attributs["height"]; |
---|
114 | |
---|
115 | //print_r($attributs); |
---|
116 | if ($w > $max_w) { |
---|
117 | $rapport = $w / $max_w; |
---|
118 | |
---|
119 | $w = round($w / $rapport); |
---|
120 | $h = round($h / $rapport); |
---|
121 | |
---|
122 | $AE->setWidth($w); |
---|
123 | $AE->setHeight($h); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | $embed = $AE->getEmbedCode(); |
---|
128 | $vignette = $AE->getImageURL(); |
---|
129 | |
---|
130 | $source = $AE->getStub("title"); |
---|
131 | |
---|
132 | if ($source == "YouTube") { |
---|
133 | $embed = rawurlencode($embed); |
---|
134 | $embed = "<div onclick=\"$(this).html(decodeURIComponent('$embed'));\" style='width: ".$w."px; height: ".$h."px; background: url($vignette) center center; cursor: pointer;'></div>"; |
---|
135 | } else if ($source == "Twitpic" OR $source == "500px") { |
---|
136 | $embed = "<a href='$url'><img src='$vignette' alt='' style='max-width:200px; max-height: 200px;' /></a>"; |
---|
137 | } |
---|
138 | |
---|
139 | // inserer une "class=oembed-source-mp3audio" |
---|
140 | $src = preg_replace(',[^\w]+,', '', strtolower($source)); |
---|
141 | |
---|
142 | $code_ae = "<div class='oembed-container oembed-source-$src'>".$embed."</div>"; |
---|
143 | |
---|
144 | |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | if ($code_ae) { |
---|
149 | // Créer dossier si nécessaire |
---|
150 | if (!is_dir(_DIR_CACHE."$host")) mkdir(_DIR_CACHE."$host"); |
---|
151 | if (!is_dir(_DIR_CACHE."$host/$dossier")) mkdir(_DIR_CACHE."$host/$dossier"); |
---|
152 | |
---|
153 | $f = fopen(_DIR_CACHE."$host/$dossier/$fichier", "w"); |
---|
154 | |
---|
155 | fwrite($f, $code_ae); |
---|
156 | fclose($f); |
---|
157 | } |
---|
158 | |
---|
159 | /* Si c'est un mp3, envoyer le modèle du plugin Soundmanager */ |
---|
160 | /* qui affiche un lecteur html5 en microformats. */ |
---|
161 | /* http://microformats.org/wiki/haudio */ |
---|
162 | /* http://www.schillmania.com/projects/soundmanager2/ */ |
---|
163 | |
---|
164 | if(preg_match(',(http://[^"\'\`\<\>\@\*\$]*?\.mp3)$,', $url) and $afficher_soundmanager){ |
---|
165 | $rand = rand(10, 1000); |
---|
166 | $code_ae = '<div class="haudio audio">' . |
---|
167 | '<div class="lecture">' . |
---|
168 | '<button class="play">play</button>' . |
---|
169 | '</div>' . |
---|
170 | '<div class="controles"> ' . |
---|
171 | '<div class="track sans_titre">' . |
---|
172 | '<a title="Ecouter" rel="enclosure" href="'.$url.'" data-soundid="'. $rand .'"><span class="fn"> </span></a>' . |
---|
173 | '</div>' . |
---|
174 | '<div class="progress_bar">' . |
---|
175 | '<div class="position"></div>' . |
---|
176 | '<div class="loading"></div>' . |
---|
177 | '</div>' . |
---|
178 | '<div class="lesinfos">' . |
---|
179 | '<div class="time">00:00</div> ' . |
---|
180 | '<div class="duration">/ 00:00</div>' . |
---|
181 | '</div> ' . |
---|
182 | '</div>' . |
---|
183 | '<br style="clear:both;">'. |
---|
184 | '</div>' ; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | return $code_ae; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | ?> |
---|