[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / sites / inc / site.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 /**
14 * Fonctions utiles au plugin sites
15 *
16 * @package SPIP\Sites\Fonctions
17 **/
18 if (!defined("_ECRIRE_INC_VERSION")) {
19 return;
20 }
21
22
23 /**
24 * Analyser une URL de site distant, qui peut être une syndication.
25 *
26 * @param string $url
27 * URL du site à analyser
28 * @return array|bool
29 * - array : informations du site
30 * - false : site impossible à récupérer
31 **/
32 function analyser_site($url) {
33 include_spip('inc/filtres');
34 include_spip('inc/distant');
35
36 // Accepter les URLs au format feed:// ou qui ont oublie le http://
37 $url = preg_replace(',^feed://,i', 'http://', $url);
38 if (!preg_match(',^[a-z]+://,i', $url)) {
39 $url = 'http://' . $url;
40 }
41
42 $texte = recuperer_page($url, true);
43 if (!$texte) {
44 return false;
45 }
46
47 include_spip('inc/syndic');
48 cdata_echappe($texte, $echappe_cdata);
49
50 if (preg_match(',<(channel|feed)([\:[:space:]][^>]*)?'
51 . '>(.*)</\1>,ims', $texte, $regs)) {
52 $result['syndication'] = 'oui';
53 $result['url_syndic'] = $url;
54 $channel = $regs[3];
55
56 // Pour recuperer l'entete, on supprime tous les items
57 $b = array_merge(
58 extraire_balises($channel, 'item'),
59 extraire_balises($channel, 'entry')
60 );
61 $header = str_replace($b, array(), $channel);
62
63 if ($t = extraire_balise($header, 'title')) {
64 cdata_echappe_retour($t, $echappe_cdata);
65 $result['nom_site'] = filtrer_entites(supprimer_tags($t));
66 }
67 if ($t = extraire_balises($header, 'link')) {
68 cdata_echappe_retour($t, $echappe_cdata);
69 foreach ($t as $link) {
70 $u = supprimer_tags(filtrer_entites($link));
71 if (!strlen($u)) {
72 $u = extraire_attribut($link, 'href');
73 }
74 if (strlen($u)) {
75 // on installe l'url comme url du site
76 // si c'est non vide, en donnant la priorite a rel=alternate
77 if (preg_match(',\balternate\b,', extraire_attribut($link, 'rel'))
78 or !isset($result['url_site'])
79 ) {
80 $result['url_site'] = filtrer_entites($u);
81 }
82 }
83 }
84 }
85 $result['url_site'] = url_absolue($result['url_site'], $url);
86
87 if ($a = extraire_balise($header, 'description')
88 or $a = extraire_balise($header, 'tagline')
89 ) {
90 cdata_echappe_retour($a, $echappe_cdata);
91 $result['descriptif'] = filtrer_entites(supprimer_tags($a));
92 }
93
94 if (preg_match(',<image.*<url.*>(.*)</url>.*</image>,Uims',
95 $header, $r)
96 and preg_match(',(https?://.*/.*(gif|png|jpg)),ims', $r[1], $r)
97 and $image = recuperer_infos_distantes($r[1])
98 ) {
99 if (in_array($image['extension'], array('gif', 'jpg', 'png'))) {
100 $result['format_logo'] = $image['extension'];
101 $result['logo'] = $r[1];
102 } else {
103 if ($image['fichier']) {
104 spip_unlink($image['fichier']);
105 }
106 }
107 }
108 } else {
109 $result['syndication'] = 'non';
110 $result['url_site'] = $url;
111 if (preg_match(',<head>(.*(description|title).*)</head>,Uims', $texte, $regs)) {
112 $head = filtrer_entites($regs[1]);
113 } else {
114 $head = $texte;
115 }
116
117 if (preg_match(',<title[^>]*>(.*),ims', $head, $regs)) {
118 $titre = trim($regs[1]);
119 if (!strlen($titre)) {
120 $titre = substr($head, strpos($head, $regs[0]));
121 }
122 $result['nom_site'] = filtrer_entites(supprimer_tags(preg_replace(',</title>.*$,ims', '', $titre)));
123 }
124
125 if ($a = array_merge(
126 extraire_balises($head, 'meta'),
127 extraire_balises($head, 'http-equiv')
128 )
129 ) {
130 foreach ($a as $meta) {
131 if (extraire_attribut($meta, 'name') == 'description') {
132 $desc = trim(extraire_attribut($meta, 'content'));
133 if (!strlen($desc)) {
134 $desc = trim(extraire_attribut($meta, 'value'));
135 }
136 $result['descriptif'] = $desc;
137 }
138 }
139 }
140
141 // Cherchons quand meme un backend
142 include_spip('inc/distant');
143 include_spip('inc/feedfinder');
144 $feeds = get_feed_from_url($url, $texte);
145 // si on a a trouve un (ou plusieurs) on le note avec select:
146 // ce qui constitue un signal pour exec=sites qui proposera de choisir
147 // si on syndique, et quelle url.
148 if (count($feeds) >= 1) {
149 spip_log("feedfinder.php :\n" . join("\n", $feeds));
150 $result['url_syndic'] = "select: " . join(' ', $feeds);
151 }
152 }
153
154 cdata_echappe_retour($result, $echappe_cdata);
155
156 return $result;
157 }