1 | <?php |
---|
2 | |
---|
3 | /*! \brief filtre à utiliser dans les squelettes |
---|
4 | * |
---|
5 | * Définition de la fonction de filtre |
---|
6 | * Vérifie que le plugin est activé et qu'il n'existe pas ailleurs deja ce filtre |
---|
7 | * Rappel : dans le cadre d'une utilisation SPIP, il n'y a pas de paramètre à donner. $url correspond à la balise appelant le filtre |
---|
8 | * |
---|
9 | * \param $url_site url du site à consulter |
---|
10 | * \return url de l'image générée par le serveur |
---|
11 | */ |
---|
12 | if (!function_exists('url_thumbsite')) { |
---|
13 | function url_thumbsite($url_site) { |
---|
14 | $url_serveur = ''; |
---|
15 | //determine le serveur d'aperçu a utiliser, defaut thumbshots.com |
---|
16 | include_spip("inc/filtres"); |
---|
17 | $serveur = sinon(lire_config('thumbsites/serveur'), "thumbshots"); |
---|
18 | //Charge le fichier de conf specifique au serveur |
---|
19 | include_spip('serveurs/'.$serveur); |
---|
20 | //execute la surcharge |
---|
21 | if ($url_site) |
---|
22 | $url_serveur = url_thumbsite_serveur($url_site); |
---|
23 | return $url_serveur; |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | // fonction de recherche de logo |
---|
28 | // SPIP 2.0 |
---|
29 | function calcule_logo_ou_thumbshot($url) { |
---|
30 | $a = func_get_args(); |
---|
31 | $url = array_shift($a); |
---|
32 | |
---|
33 | // la fonction normale |
---|
34 | $c = call_user_func_array('calcule_logo',$a); |
---|
35 | |
---|
36 | // si elle repond pas, on va chercher la vignette |
---|
37 | if (!$c[0]) |
---|
38 | $c[0] = thumbshot($url); |
---|
39 | |
---|
40 | return $c; |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | // fonction de recherche de logo |
---|
45 | // SPIP 2.1 : on se contente de produire un tag IMG |
---|
46 | function thumbshot_img($url) { |
---|
47 | if (!$url OR !$g = thumbshot($url)) |
---|
48 | return ''; |
---|
49 | |
---|
50 | return '<img src="'.$g.'" alt="" class="spip_logos" />'; |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | // fonction de creation d'un index des vignettes |
---|
55 | function creer_index_thumbshots($tmp) { |
---|
56 | static $done = false; |
---|
57 | if ($done) return; |
---|
58 | $done = true; |
---|
59 | if (!file_exists($tmp.'index.php')) |
---|
60 | ecrire_fichier ($tmp.'index.php', '<?php |
---|
61 | foreach(glob(\'./*.jpg\') as $i) |
---|
62 | echo "<img src=\'$i\' />\n"; |
---|
63 | ?>' |
---|
64 | ); |
---|
65 | } |
---|
66 | |
---|
67 | // Cree le fichier cache du thumbshot et renvoie le fichier |
---|
68 | function thumbshot($url_site, $refresh=false) { |
---|
69 | static $nb=5; // ne pas en charger plus de 5 anciens par tour |
---|
70 | |
---|
71 | if (!strlen($url_site) OR !parse_url($url_site)) |
---|
72 | return ''; |
---|
73 | |
---|
74 | $tmp = sous_repertoire(_DIR_VAR, 'cache-thumbsites'); |
---|
75 | $md5_url = md5(strtolower($url_site)); |
---|
76 | $thumb_cache = $tmp.$md5_url.'.jpg'; |
---|
77 | |
---|
78 | if( $refresh AND file_exists($thumb_cache)) { |
---|
79 | $ret=supprimer_fichier($thumb_cache); |
---|
80 | spip_log("thumbshot demande de rafraichissement url $url_site file $thumb_cache suppression reussie ? $ret"); |
---|
81 | } |
---|
82 | |
---|
83 | include_spip("inc/filtres"); |
---|
84 | $duree = intval(sinon(lire_config('thumbsites/duree_cache'),30)) ; |
---|
85 | |
---|
86 | if ((!file_exists($thumb_cache) OR ((time()-3600*24*$duree > filemtime($thumb_cache)) AND $nb > 0))) { |
---|
87 | |
---|
88 | $nb--; |
---|
89 | include_spip("inc/distant"); |
---|
90 | if ($thumb = recuperer_page(url_thumbsite($url_site))) { |
---|
91 | spip_log('thumbshot ok pour '.$url_site); |
---|
92 | ecrire_fichier($thumb_cache, $thumb); |
---|
93 | // si c'est un png, le convertir en jpg |
---|
94 | $a = @getimagesize($thumb_cache); |
---|
95 | if ($a[2] == 3) // png |
---|
96 | { |
---|
97 | rename($thumb_cache, $thumb_cache.'.png'); |
---|
98 | include_spip('inc/filtres_images'); |
---|
99 | $img = imagecreatefrompng($thumb_cache.'.png'); |
---|
100 | if (function_exists('image_imagejpg')) { |
---|
101 | image_imagejpg($img, $thumb_cache); |
---|
102 | } else { |
---|
103 | /* Depuis SPIP 2.1, les filtres images changent de nom */ |
---|
104 | _image_imagejpg($img, $thumb_cache); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | creer_index_thumbshots($tmp); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | // On verifie si le thumbshot existe en controlant la taille du fichier |
---|
113 | if (@filesize($thumb_cache)) |
---|
114 | return $thumb_cache; |
---|
115 | else |
---|
116 | return ''; |
---|
117 | } |
---|
118 | |
---|
119 | ?> |
---|