[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / inc / distant.php
index 31b5e51..f998959 100644 (file)
@@ -3,7 +3,7 @@
 /***************************************************************************\
  *  SPIP, Systeme de publication pour l'internet                           *
  *                                                                         *
- *  Copyright (c) 2001-2014                                                *
+ *  Copyright (c) 2001-2016                                                *
  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
  *                                                                         *
  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
@@ -45,9 +45,11 @@ define('_REGEXP_COPIE_LOCALE', ',' .
  *   'force' - charger toujours (mettre a jour)
  * @param string $local
  *   permet de specifier le nom du fichier local (stockage d'un cache par exemple, et non document IMG)
+ * @param int $taille_max
+ *   taille maxi de la copie local, par defaut _COPIE_LOCALE_MAX_SIZE
  * @return bool|string
  */
-function copie_locale($source, $mode='auto', $local = null) {
+function copie_locale($source, $mode='auto', $local=null, $taille_max=null){
 
        // si c'est la protection de soi-meme, retourner le path
        if ($mode !== 'force' AND preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) {
@@ -82,7 +84,8 @@ function copie_locale($source, $mode='auto', $local = null) {
                // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation
                // et des eventuelles recuperations concurantes
                include_spip("inc/acces");
-               $res = recuperer_page($source, $localrac, false, _COPIE_LOCALE_MAX_SIZE, '', '', false, $t ? filemtime($localrac) : '');
+               if (!$taille_max) $taille_max = _COPIE_LOCALE_MAX_SIZE;
+               $res = recuperer_page($source, $localrac, false, $taille_max, '', '', false, $t ? filemtime($localrac) : '');
                if (!$res) {
                        if (!$t) // si $t c'est sans doute juste un not-modified-since qui fait renvoyer false
                                spip_log("copie_locale : Echec recuperation $source sur $localrac",_LOG_INFO_IMPORTANTE);
@@ -191,6 +194,31 @@ function prepare_donnees_post($donnees, $boundary = '') {
        return array($entete, $chaine);
 }
 
+/**
+ * Convertir une URL dont le host est en utf8 en ascii
+ * Utilise la librairie https://github.com/phlylabs/idna-convert/tree/v0.9.1
+ * dans sa derniere version compatible toutes version PHP 5
+ * La fonction PHP idn_to_ascii depend d'un package php5-intl et est rarement disponible
+ *
+ * @param string $url_idn
+ * @return array|string
+ */
+function url_to_ascii($url_idn) {
+
+       if ($parts = parse_url($url_idn)) {
+               $host = $parts['host'];
+               if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) {
+                       include_spip('inc/idna_convert.class');
+                       $IDN = new idna_convert();
+                       $host_ascii = $IDN->encode($host);
+                       $url_idn = explode($host, $url_idn, 2);
+                       $url_idn = implode($host_ascii, $url_idn);
+               }
+       }
+
+       return $url_idn;
+}
+
 //
 // Recupere une page sur le net
 // et au besoin l'encode dans le charset local
@@ -222,6 +250,7 @@ function recuperer_page($url, $trans = false, $get_headers = false,
        // 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;
+       $url = url_to_ascii($url);
 
        if ($taille_max==0)
                $get = 'HEAD';
@@ -503,6 +532,11 @@ function fichier_copie_locale($source){
 // http://doc.spip.org/@recuperer_infos_distantes
 function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image = true){
 
+       // pas la peine de perdre son temps
+       if (!tester_url_absolue($source)) {
+               return false;
+       }
+       
        # charger les alias des types mime
        include_spip('base/typedoc');
        global $mime_alias;
@@ -581,6 +615,11 @@ function recuperer_infos_distantes($source, $max = 0, $charger_si_petite_image =
                $a = recuperer_infos_distantes($source, _INC_DISTANT_MAX_SIZE);
        }
 
+       // si on a rien trouve pas la peine d'insister
+       if (!$a) {
+               return false;
+       }
+
        // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller
        // recharger le document en GET et recuperer des donnees supplementaires...
        if (preg_match(',^image/(jpeg|gif|png|swf),', $mime_type)){