Browse Source

Prise en charge amelioree et etendu de l'oembed des pouets mastodons : on recupere la version atom du pouet pour avoir ses infos, et on met en forme via un modeles/toot.html, y compris en prenant en charge les emoji qui peuvent etre presents dans le screen_name ou dans le content

yohooo-patch-1
cedric@yterium.com 6 years ago
parent
commit
9146311add
  1. 4
      .gitattributes
  2. 16
      css/oembed.css
  3. 4
      inc/oembed.php
  4. 37
      inc/oembed_recuperer_url.php
  5. 1325
      lib/php-emoji/emoji.css
  6. 9996
      lib/php-emoji/emoji.php
  7. BIN
      lib/php-emoji/emoji.png
  8. 18
      modeles/toot.html
  9. 82
      oembed/input/posttraite_mastodon.php
  10. 2
      paquet.xml

4
.gitattributes vendored

@ -18,6 +18,9 @@ lang/paquet-oembed_en.php -text
lang/paquet-oembed_es.php -text
lang/paquet-oembed_fr.php -text
lang/paquet-oembed_sk.php -text
lib/php-emoji/emoji.css -text
lib/php-emoji/emoji.php -text
lib/php-emoji/emoji.png -text
modeles/oembed.html -text
modeles/oembed_fonctions.php -text
modeles/oembed_link.html -text
@ -25,6 +28,7 @@ modeles/oembed_photo.html -text
modeles/oembed_rich.html -text
modeles/oembed_video.html -text
modeles/text_oembed.html -text
modeles/toot.html -text
oembed/input/posttraite_huffduffer_rich.php -text
oembed/input/posttraite_instagram_rich.php -text
oembed/input/posttraite_issuu.php -text

16
css/oembed.css

@ -89,3 +89,19 @@ figure.oe-image-block figcaption {background:#eee;padding: 10px;text-align: left
.oembed_mastodon .oembed-source {display: none;}
.oembed_mastodon .spip_doc_titre {display: none;}
.oembed_mastodon .oe-rich {max-width:none !important;}
.oembed_mastodon .oembed-source {display: none;}
.oembed_mastodon .oe-rich {background: #f8f8f8;padding: 15px;border-radius: 4px;margin: 1em auto;}
.oembed_mastodon .rwd-rich-container {height: auto !important;}
.oembed_mastodon .spip_doc_titre {display: none;}
.oembed_mastodon .oe-rich {max-width:none !important;}
.mastodon-toot {padding:0;margin:0;padding-left:63px;font-family: Arial,Helvetica,Sans-Serif;}
.mastodon-toot .toot-published {float: right;color:#9baec8;text-decoration: none !important;}
.mastodon-toot .toot-published:active,.mastodon-toot .toot-published:focus,.mastodon-toot .toot-published:hover {text-decoration: underline !important;}
.mastodon-toot .toot-author-url {color:#9baec8;text-decoration: none !important;}
.mastodon-toot .toot-author-thumbnail {float: left;margin-left:-63px;width: 48px;height: 48px;border-radius: 4px;}
.mastodon-toot .toot-author-screen-name {color:#282c37;}
.mastodon-toot .toot-author-url:active .toot-author-screen-name,.mastodon-toot .toot-author-url:focus .toot-author-screen-name,.mastodon-toot .toot-author-url:hover .toot-author-screen-name {text-decoration: underline;}
.mastodon-toot .toot-content a {color:#2b90d9;text-decoration: none !important;}
.mastodon-toot .toot-content a:active,.mastodon-toot .toot-content a:focus,.mastodon-toot .toot-content a:hover {text-decoration: underline !important;}
.mastodon-toot .invisible {display: none}
.mastodon-toot .ellipsis::after {display: inline;content:'...'}

4
inc/oembed.php

@ -250,9 +250,7 @@ function oembed_detecter_lien($url) {
$oembed_recuperer_url = charger_fonction('oembed_recuperer_url', 'inc');
// on recupere le contenu de la page
include_spip('inc/distant');
if ($html = recuperer_page($url)) {
if ($html = $oembed_recuperer_url($url, $url, 'html')) {
// types de liens oembed à détecter
$linktypes = array(
'application/json+oembed' => 'json',

37
inc/oembed_recuperer_url.php

@ -18,23 +18,24 @@ if (!defined('_ECRIRE_INC_VERSION')) {
* @return bool|mixed|string
*/
function inc_oembed_recuperer_url($oembed_url, $url, $format) {
$data = false;
$erreur = "";
// on recupere le contenu de la page
// si possible via curl en IPv4 car youtube bug en IPv6
// uniquement si PHP >= 5.3.0 pour utiliser l'option CURLOPT_IPRESOLVE
if (function_exists('curl_init') and version_compare(phpversion(), '5.3.0', '>=')) {
spip_log('Requete oembed (curl) pour '.$url.' : '.$oembed_url, 'oembed.'._LOG_DEBUG);
if (function_exists('curl_init') and version_compare(phpversion(), '5.3.0', '>=')){
spip_log('Requete oembed (curl) pour ' . $url . ' : ' . $oembed_url, 'oembed.' . _LOG_DEBUG);
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $oembed_url);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($c, CURLOPT_SSLVERSION, 1 );
if (isset($GLOBALS['meta']['http_proxy']) and $GLOBALS['meta']['http_proxy']) {
curl_setopt($c, CURLOPT_PROXY, $GLOBALS['meta']['http_proxy']);
if (isset($GLOBALS['meta']['http_noproxy'])) {
if (isset($GLOBALS['meta']['http_proxy']) and $GLOBALS['meta']['http_proxy']){
curl_setopt($c, CURLOPT_PROXY, $GLOBALS['meta']['http_proxy']);
if (isset($GLOBALS['meta']['http_noproxy'])){
curl_setopt($c, CURLOPT_NOPROXY, $GLOBALS['meta']['http_proxy']);
}
}
@ -44,14 +45,34 @@ function inc_oembed_recuperer_url($oembed_url, $url, $format) {
curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$data = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
if (!$data){
$errno = curl_errno($c);
$erreur = "Status $status Error " . curl_errno($c) . " " . curl_error($c);
// si c'est une erreur de protocole SSL, on tente avec un exec mechant car ca peut venir de la version de CURL PHP
// (ca marche au moins en local)
if (!$data and $errno == 35) {
exec('curl "'.$oembed_url.'"',$output);
$data = implode("\n", $output);
}
}
curl_close($c);
} else {
}
else {
spip_log('Requete oembed (recuperer_page) pour '.$url.' : '.$oembed_url, 'oembed.'._LOG_DEBUG);
include_spip('inc/distant');
$data = recuperer_page($oembed_url);
}
spip_log('infos oembed brutes pour '.$url.' : '.($data?$data:'ECHEC'), 'oembed.'._LOG_DEBUG);
if (!$data) {
spip_log('infos oembed brutes pour '."$url | $oembed_url".' : ' . "ECHEC $erreur", 'oembed.'._LOG_ERREUR);
}
else {
spip_log('infos oembed brutes pour '."$url | $oembed_url".' : '.(($format == 'html')?substr($data,0,100):$data), 'oembed.'._LOG_DEBUG);
}
if ($data) {
if ($format == 'json') {
$data = json_decode($data, true);

1325
lib/php-emoji/emoji.css

File diff suppressed because it is too large Load Diff

9996
lib/php-emoji/emoji.php

File diff suppressed because one or more lines are too long

BIN
lib/php-emoji/emoji.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 MiB

18
modeles/toot.html

@ -0,0 +1,18 @@
<blockquote class="mastodon-toot" style="[max-width:(#ENV{width});]">
<span class="toot-author">
[<a href="#ENV{url}" class="toot-published">(#ENV{published}|affdate_heure)</a>]
<a href="#ENV{author_url}" class="toot-author-url">
[(#ENV{author_thumbnail}|image_reduire{48,48}|inserer_attribut{class,toot-author-thumbnail})]
[<span class="toot-author-screen-name">(#ENV*{author_screen_name})</span>]
[<span class="toot-author-name">(#ENV{author_name})</span>]
</a>
[<span class="toot-content">(#ENV*{content})</span>]
</span>
</blockquote>
[(#ENV{need_emoji}|oui)
<style type="text/css">@import url("[(#CHEMIN{lib/php-emoji/emoji.css}|url_absolue)]");</style>
]

82
oembed/input/posttraite_mastodon.php

@ -9,6 +9,29 @@ if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
include_spip('inc/charsets');
function emojify_names($name) {
$name = ':'.str_replace(' ','_',strtolower($name)).':';
return $name;
}
function emojify($texte, &$need_emoji) {
if (
(strpos($texte, ':')!==false and preg_match(',:\w+:,',$texte))
or is_utf8($texte)) {
if (!function_exists('emoji_convert')) {
include_spip('lib/php-emoji/emoji');
$GLOBALS['emoji_maps']['names_to_unified'] = array_flip(array_map('emojify_names',$GLOBALS['emoji_maps']['names']));
}
// convertir les emoji nommes type :satellite: en utf
$texte = emoji_convert($texte, 'names_to_unified');
// convertir les emoji utf en html
$texte = emoji_unified_to_html($texte);
$need_emoji = (strpos($texte, 'emoji-sizer') !== false);
}
return $texte;
}
function oembed_input_posttraite_mastodon_dist($data) {
if ($iframe = extraire_balise($data['html'], 'iframe')) {
@ -19,33 +42,46 @@ function oembed_input_posttraite_mastodon_dist($data) {
$data['html'] = str_replace($data['html'], $iframe, $iframe_cor);
$src = extraire_attribut($iframe_cor, 'src');
// si on sait mieux faire en faisant un extract du contenu de l'iframe on y go, car les iframe c'est moche
if ($html = recuperer_page($src)) {
$texte = $date_link = "";
if ($p = strpos($html, 'status__content')
and $p1 = strpos($html, '<p', $p)
and $p2 = strpos($html, '</p>', $p1)
) {
$texte = substr($html, $p1, $p2 - $p1 +4);
}
if ($p = strpos($html, 'dt-published')
and $p1 = strpos($html, '<a', $p)
and $p2 = strpos($html, '</a>', $p1)
) {
$date_link = substr($html, $p1, $p2 - $p1 +4);
}
if ($texte and $date_link) {
$html = "<blockquote class=\"twitter-tweet\">".$texte."\n&mdash; ".$data['author_name'];
$author_account = explode('/users/', $data['author_url']);
$a = "@".end($author_account)."@".trim(protocole_implicite(reset($author_account)),'/');
$html .= " ($a) $date_link\n</blockquote>";
$oembed_recuperer_url = charger_fonction('oembed_recuperer_url', 'inc');
$url = preg_replace(',/embed$,','', $src);
$src_atom = $url.'.atom';
if ($xml = $oembed_recuperer_url($src_atom, $src_atom, 'xml')) {
$need_emoji = false;
$name = "@".strip_tags(extraire_balise($xml, 'email'));
$content = strip_tags(extraire_balise($xml, 'content'));
$content = emojify(filtrer_entites($content), $need_emoji);
$date = strip_tags(extraire_balise($xml, 'published'));
$date = date('Y-m-d H:i:s',strtotime($date));
$screen_name = emojify($data['author_name'], $need_emoji);
$data['html'] = $html;
$data['height'] = null;
$contexte = array(
'url' => $url,
'width' => $data['width'],
'height' => $data['height'],
'author_screen_name' => $screen_name,
'author_name' => $name,
'author_url' => $data['author_url'],
'author_thumbnail' => '',
'content' => $content,
'published' => $date,
'need_emoji' => ($need_emoji?' ':''),
);
$links = extraire_balises($xml, 'link');
foreach ($links as $link) {
if (extraire_attribut($link, 'rel') === 'avatar') {
$contexte['author_thumbnail'] = extraire_attribut($link, 'href');
$contexte['author_thumbnail_width'] = extraire_attribut($link, 'media:width');
$contexte['author_thumbnail_height'] = extraire_attribut($link, 'media:height');
}
}
}
$data['html'] = recuperer_fond('modeles/toot', $contexte);
}
}
$data['provider_name'] = 'Mastodon';

2
paquet.xml

@ -1,7 +1,7 @@
<paquet
prefix="oembed"
categorie="multimedia"
version="1.7.0"
version="1.8.0"
etat="stable"
compatibilite="[3.0.0;3.2.*]"
logo="prive/themes/spip/images/oembed-32.png"

Loading…
Cancel
Save