[SPIP] v3.2.7-->v3.2.9
[lhc/web/www.git] / www / ecrire / inc / utils.php
index 1e5375f..a89658a 100644 (file)
@@ -3,7 +3,7 @@
 /***************************************************************************\
  *  SPIP, Systeme de publication pour l'internet                           *
  *                                                                         *
- *  Copyright (c) 2001-2017                                                *
+ *  Copyright (c) 2001-2019                                                *
  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
  *                                                                         *
  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
@@ -446,6 +446,43 @@ function set_request($var, $val = null, $c = false) {
        return false; # n'affecte pas $c
 }
 
+/**
+ * Sanitizer une valeur *SI* elle provient du GET ou POST
+ * Utile dans les squelettes pour les valeurs qu'on attrape dans le env,
+ * dont on veut permettre à un squelette de confiance appelant de fournir une valeur complexe
+ * mais qui doit etre nettoyee si elle provient de l'URL
+ *
+ * On peut sanitizer
+ * - une valeur simple : `$where = spip_sanitize_from_request($value, 'where')`
+ * - un tableau en partie : `$env = spip_sanitize_from_request($env, ['key1','key2'])`
+ * - un tableau complet : `$env = spip_sanitize_from_request($env, '*')`
+ *
+ * @param string|array $value
+ * @param string|array $key
+ * @param string $sanitize_function
+ * @return array|mixed|string
+ */
+function spip_sanitize_from_request($value, $key, $sanitize_function='entites_html') {
+       if (is_array($value)) {
+               if ($key=='*') {
+                       $key = array_keys($value);
+               }
+               if (!is_array($key)) {
+                       $key = [$key];
+               }
+               foreach ($key as $k) {
+                       if (!empty($value[$k])) {
+                               $value[$k] = spip_sanitize_from_request($value[$k], $k, $sanitize_function);
+                       }
+               }
+               return $value;
+       }
+       // si la valeur vient des GET ou POST on la sanitize
+       if (!empty($value) and $value == _request($key)) {
+               $value = $sanitize_function($value);
+       }
+       return $value;
+}
 
 /**
  * Tester si une URL est absolue
@@ -687,7 +724,7 @@ function self($amp = '&', $root = false) {
        include_spip('inc/filtres_mini');
        $url = spip_htmlspecialchars($url);
        
-       $url = str_replace(array("'", '"', '<', '[', ']'), array('%27', '%22', '%3C', '%5B', '%5D'), $url);
+       $url = str_replace(array("'", '"', '<', '[', ']', ':'), array('%27', '%22', '%3C', '%5B', '%5D', '%3A'), $url);
 
        // &amp; ?
        if ($amp != '&amp;') {
@@ -836,7 +873,7 @@ function _L($text, $args = array(), $options = array()) {
                $options = $defaut_options;
        }
 
-       if (is_array($args)) {
+       if (is_array($args) and count($args)) {
                if (!function_exists('interdire_scripts')) {
                        include_spip('inc/texte');
                }
@@ -1641,14 +1678,24 @@ function find_all_in_path($dir, $pattern, $recurs = false) {
 
 /**
  * Prédicat sur les scripts de ecrire qui n'authentifient pas par cookie
+ * et beneficient d'une exception
+ *
  * @param string $nom
+ * @param bool $strict
  * @return bool
  */
-function autoriser_sans_cookie($nom) {
+function autoriser_sans_cookie($nom, $strict = false) {
        static $autsanscookie = array('install', 'base_repair');
-       $nom = preg_replace('/.php[3]?$/', '', basename($nom));
 
-       return in_array($nom, $autsanscookie);
+       if (in_array($nom, $autsanscookie)) {
+               if (test_espace_prive()){
+                       include_spip('base/connect_sql');
+                       if (!$strict or !spip_connect()){
+                               return true;
+                       }
+               }
+       }
+       return false;
 }
 
 /**
@@ -2567,7 +2614,7 @@ function spip_initialisation_core($pi = null, $pa = null, $ti = null, $ta = null
 
                        if (isset($GLOBALS['meta']['adresse_site'])) {
                                $uri_ref = parse_url($GLOBALS['meta']['adresse_site']);
-                               $uri_ref = $uri_ref['path'] . '/';
+                               $uri_ref = (isset($uri_ref['path']) ? $uri_ref['path'] : '') . '/';
                        } else {
                                $uri_ref = "";
                        }
@@ -2812,8 +2859,8 @@ function init_var_mode() {
                                } elseif (in_array('calcul', $var_mode)) {
                                        define('_VAR_MODE', 'calcul');
                                }
-                               $var_mode = array_diff($var_mode, array('calcul', 'recalcul'));
                        }
+                       $var_mode = array_diff($var_mode, array('calcul', 'recalcul'));
                        if ($var_mode) {
                                include_spip('inc/autoriser');
                                // autoriser preview si preview seulement, et sinon autoriser debug
@@ -3115,7 +3162,18 @@ function exec_info_dist() {
 
        include_spip('inc/autoriser');
        if (autoriser('webmestre')) {
+               $cookies_masques = ['spip_session', 'PHPSESSID'];
+               $cookies_backup = [];
+               foreach ($cookies_masques as $k) {
+                       if (!empty($_COOKIE[$k])) {
+                               $cookies_backup[$k] = $_COOKIE[$k];
+                               $_COOKIE[$k] = '******************************';
+                       }
+               }
                phpinfo();
+               foreach ($cookies_backup as $k => $v) {
+                       $_COOKIE[$k] = $v;
+               }
        } else {
                include_spip('inc/filtres');
                sinon_interdire_acces();
@@ -3575,4 +3633,21 @@ if (PHP_VERSION_ID < 50500) {
                }
 
        }
+}
+
+/**
+ * Nettoie une chaine pour servir comme classes CSS.
+ *
+ * @note
+ *     les classes CSS acceptent théoriquement tous les caractères sauf NUL.
+ *     Ici, on limite (enlève) les caractères autres qu’alphanumérique, espace, - + _ @
+ *
+ * @param string|string[] $classes
+ * @return string|string[]
+ */
+function spip_sanitize_classname($classes) {
+       if (is_array($classes)) {
+               return array_map('spip_sanitize_classname', $classes);
+       }
+       return preg_replace("/[^ 0-9a-z_\-+@]/i", "", $classes);
 }
\ No newline at end of file