forked from spip/sites
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.7 KiB
157 lines
4.7 KiB
<?php |
|
|
|
/***************************************************************************\ |
|
* SPIP, Systeme de publication pour l'internet * |
|
* * |
|
* Copyright (c) 2001-2020 * |
|
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * |
|
* * |
|
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. * |
|
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * |
|
\***************************************************************************/ |
|
|
|
/** |
|
* Fonctions utiles au plugin sites |
|
* |
|
* @package SPIP\Sites\Fonctions |
|
**/ |
|
if (!defined("_ECRIRE_INC_VERSION")) { |
|
return; |
|
} |
|
|
|
|
|
/** |
|
* Analyser une URL de site distant, qui peut être une syndication. |
|
* |
|
* @param string $url |
|
* URL du site à analyser |
|
* @return array|bool |
|
* - array : informations du site |
|
* - false : site impossible à récupérer |
|
**/ |
|
function analyser_site($url) { |
|
include_spip('inc/filtres'); |
|
include_spip('inc/distant'); |
|
|
|
// Accepter les URLs au format feed:// ou qui ont oublie le http:// |
|
$url = preg_replace(',^feed://,i', 'http://', $url); |
|
if (!preg_match(',^[a-z]+://,i', $url)) { |
|
$url = 'http://' . $url; |
|
} |
|
|
|
$texte = recuperer_page($url, true); |
|
if (!$texte) { |
|
return false; |
|
} |
|
|
|
include_spip('inc/syndic'); |
|
cdata_echappe($texte, $echappe_cdata); |
|
|
|
if (preg_match(',<(channel|feed)([\:[:space:]][^>]*)?' |
|
. '>(.*)</\1>,ims', $texte, $regs)) { |
|
$result['syndication'] = 'oui'; |
|
$result['url_syndic'] = $url; |
|
$channel = $regs[3]; |
|
|
|
// Pour recuperer l'entete, on supprime tous les items |
|
$b = array_merge( |
|
extraire_balises($channel, 'item'), |
|
extraire_balises($channel, 'entry') |
|
); |
|
$header = str_replace($b, array(), $channel); |
|
|
|
if ($t = extraire_balise($header, 'title')) { |
|
cdata_echappe_retour($t, $echappe_cdata); |
|
$result['nom_site'] = filtrer_entites(supprimer_tags($t)); |
|
} |
|
if ($t = extraire_balises($header, 'link')) { |
|
cdata_echappe_retour($t, $echappe_cdata); |
|
foreach ($t as $link) { |
|
$u = supprimer_tags(filtrer_entites($link)); |
|
if (!strlen($u)) { |
|
$u = extraire_attribut($link, 'href'); |
|
} |
|
if (strlen($u)) { |
|
// on installe l'url comme url du site |
|
// si c'est non vide, en donnant la priorite a rel=alternate |
|
if (preg_match(',\balternate\b,', extraire_attribut($link, 'rel')) |
|
or !isset($result['url_site']) |
|
) { |
|
$result['url_site'] = filtrer_entites($u); |
|
} |
|
} |
|
} |
|
} |
|
$result['url_site'] = url_absolue($result['url_site'], $url); |
|
|
|
if ($a = extraire_balise($header, 'description') |
|
or $a = extraire_balise($header, 'tagline') |
|
) { |
|
cdata_echappe_retour($a, $echappe_cdata); |
|
$result['descriptif'] = filtrer_entites(supprimer_tags($a)); |
|
} |
|
|
|
if (preg_match(',<image.*<url.*>(.*)</url>.*</image>,Uims', |
|
$header, $r) |
|
and preg_match(',(https?://.*/.*(gif|png|jpg)),ims', $r[1], $r) |
|
and $image = recuperer_infos_distantes($r[1]) |
|
) { |
|
if (in_array($image['extension'], array('gif', 'jpg', 'png'))) { |
|
$result['format_logo'] = $image['extension']; |
|
$result['logo'] = $r[1]; |
|
} else { |
|
if ($image['fichier']) { |
|
spip_unlink($image['fichier']); |
|
} |
|
} |
|
} |
|
} else { |
|
$result['syndication'] = 'non'; |
|
$result['url_site'] = $url; |
|
if (preg_match(',<head>(.*(description|title).*)</head>,Uims', $texte, $regs)) { |
|
$head = filtrer_entites($regs[1]); |
|
} else { |
|
$head = $texte; |
|
} |
|
|
|
if (preg_match(',<title[^>]*>(.*),ims', $head, $regs)) { |
|
$titre = trim($regs[1]); |
|
if (!strlen($titre)) { |
|
$titre = substr($head, strpos($head, $regs[0])); |
|
} |
|
$result['nom_site'] = filtrer_entites(supprimer_tags(preg_replace(',</title>.*$,ims', '', $titre))); |
|
} |
|
|
|
if ($a = array_merge( |
|
extraire_balises($head, 'meta'), |
|
extraire_balises($head, 'http-equiv') |
|
) |
|
) { |
|
foreach ($a as $meta) { |
|
if (extraire_attribut($meta, 'name') == 'description') { |
|
$desc = trim(extraire_attribut($meta, 'content')); |
|
if (!strlen($desc)) { |
|
$desc = trim(extraire_attribut($meta, 'value')); |
|
} |
|
$result['descriptif'] = $desc; |
|
} |
|
} |
|
} |
|
|
|
// Cherchons quand meme un backend |
|
include_spip('inc/distant'); |
|
include_spip('inc/feedfinder'); |
|
$feeds = get_feed_from_url($url, $texte); |
|
// si on a a trouve un (ou plusieurs) on le note avec select: |
|
// ce qui constitue un signal pour exec=sites qui proposera de choisir |
|
// si on syndique, et quelle url. |
|
if (count($feeds) >= 1) { |
|
spip_log("feedfinder.php :\n" . join("\n", $feeds)); |
|
$result['url_syndic'] = "select: " . join(' ', $feeds); |
|
} |
|
} |
|
|
|
cdata_echappe_retour($result, $echappe_cdata); |
|
|
|
return $result; |
|
}
|
|
|