From 8cb5ec4f153ed07c9e53ecca3f14eef45f514e11 Mon Sep 17 00:00:00 2001
From: "real3t@gmail.com" <>
Date: Mon, 27 Aug 2018 15:33:43 +0000
Subject: [PATCH] =?UTF-8?q?Int=C3=A9gration=20d'un=20fork=20de=20IEConfig?=
 =?UTF-8?q?=20en=20attendant=20qu'il=20soit=20officiellement=20int=C3=A9gr?=
 =?UTF-8?q?=C3=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                                |   2 +
 formulaires/ieconfig_export.php               | 204 ++++++++++
 formulaires/ieconfig_import.php               | 359 ++++++++++++++++++
 paquet.xml                                    |   2 +-
 .../navigation/configurer_soyezcreateurs.html |   2 +-
 5 files changed, 567 insertions(+), 2 deletions(-)
 create mode 100644 formulaires/ieconfig_export.php
 create mode 100644 formulaires/ieconfig_import.php

diff --git a/.gitattributes b/.gitattributes
index 410ac6a0..e5676c6a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -236,6 +236,8 @@ formulaires/evenements_par_mots.html -text
 formulaires/evenements_par_mots.php -text
 formulaires/evenements_par_mots_compteur.html -text
 formulaires/forum.html -text
+formulaires/ieconfig_export.php -text
+formulaires/ieconfig_import.php -text
 formulaires/inc-forum_previsu.html -text
 formulaires/inc-graver-nom.html -text
 formulaires/inc-login_forum.html -text
diff --git a/formulaires/ieconfig_export.php b/formulaires/ieconfig_export.php
new file mode 100644
index 00000000..b5e80028
--- /dev/null
+++ b/formulaires/ieconfig_export.php
@@ -0,0 +1,204 @@
+<?php
+
+if (!defined('_ECRIRE_INC_VERSION')) {
+	return;
+}
+
+function formulaires_ieconfig_export_saisies_dist($prefixeaexporter = null) {
+	$saisies = array(
+		// Options d'export
+		array(
+			'saisie' => 'fieldset',
+			'options' => array(
+				'nom' => 'ieconfig_export',
+				'label' => '<:ieconfig:label_ieconfig_export:>',
+				'icone' => 'img/ieconfig-export.png',
+			),
+			'saisies' => array(
+				array(
+					'saisie' => 'input',
+					'options' => array(
+						'nom' => 'ieconfig_export_nom',
+						'label' => '<:ieconfig:label_ieconfig_export_nom:>',
+						'obligatoire' => 'oui',
+						'defaut' => $GLOBALS['meta']['nom_site'] . ' - ' . date('Y/m/d'),
+					),
+				),
+				array(
+					'saisie' => 'textarea',
+					'options' => array(
+						'nom' => 'ieconfig_export_description',
+						'label' => '<:ieconfig:label_ieconfig_export_description:>',
+						'rows' => 4,
+					),
+				),
+				array(
+					'saisie' => 'explication',
+					'options' => array(
+						'nom' => 'ieconfig_export_explication',
+						'texte' => '<:ieconfig:texte_ieconfig_export_explication:>',
+					),
+				),
+				array(
+					'saisie' => 'selection',
+					'options' => array(
+						'nom' => 'ieconfig_export_choix',
+						'label' => '<:ieconfig:label_ieconfig_export_choix:>',
+						'cacher_option_intro' => 'oui',
+						'defaut' => 'telecharger',
+						'datas' => array(
+							'sauvegarder' => '<:ieconfig:item_sauvegarder:>',
+							'telecharger' => '<:ieconfig:item_telecharger:>',
+						)
+					),
+				),
+			),
+		),
+	);
+	// Gestion des plugins utilisant le pipeline ieconfig_metas
+	$ieconfig_metas = array();
+	foreach (pipeline('ieconfig_metas', array()) as $prefixe => $data) {
+		if (!isset($prefixeaexporter) 
+			OR 
+			(isset($prefixeaexporter) && $prefixe === $prefixeaexporter)
+			) {
+			if (isset($data['icone'])) {
+				$icone = chemin_image($data['icone']);
+				if (!$icone) {
+					$icone = find_in_path($data['icone']);
+				}
+				if ($icone) {
+					$icone = '<img src="' . $icone . '" alt="" style="margin-left:0px; margin-right:0.5em;" />';
+				}
+			} else {
+				$icone = '';
+			}
+			$ieconfig_metas[$prefixe] = $icone . (isset($data['titre']) ? $data['titre'] : $prefixe);
+		}
+	}
+	ksort($ieconfig_metas);
+	if (count($ieconfig_metas) > 0) {
+		$saisies[] = array(
+			'saisie' => 'fieldset',
+			'options' => array(
+				'nom' => 'metas_fieldset',
+				'label' => _T('ieconfig:label_configurations_a_exporter'),
+				'icone' => 'config-export-16.png',
+			),
+			'saisies' => array(
+				array(
+					'saisie' => 'checkbox',
+					'options' => array(
+						'nom' => 'export_metas',
+						'label' => _T('ieconfig:label_configurations_a_exporter'),
+						'tout_selectionner' => (count($ieconfig_metas) > 1) ? 'oui':'',
+						'datas' => $ieconfig_metas,
+					),
+				),
+			),
+		);
+	}
+
+	// On passe via le pipeline ieconfig (pour les cas particuliers)
+	$saisies = pipeline('ieconfig', array(
+		'args' => array(
+			'action' => 'form_export',
+		),
+		'data' => $saisies,
+	));
+
+	return $saisies;
+}
+
+
+function formulaires_ieconfig_export_traiter_dist($prefixeaexporter = null) {
+	$export = array();
+	$export['nom'] = _request('ieconfig_export_nom');
+	if (_request('ieconfig_export_description') != '') {
+		$export['description'] = _request('ieconfig_export_description');
+	}
+
+	// On passe via le pipeline ieconfig
+	$export = pipeline('ieconfig', array(
+		'args' => array(
+			'action' => 'export',
+		),
+		'data' => $export,
+	));
+
+	// Gestion des plugins utilisant le pipeline ieconfig_metas
+	$export_metas = _request('export_metas');
+	if (!is_array($export_metas)) {
+		$export_metas = array();
+	}
+
+	foreach (pipeline('ieconfig_metas', array()) as $prefixe => $data) {
+		if (in_array($prefixe, $export_metas)) {
+			$export_plugin = array();
+			if (isset($data['metas_brutes'])) {
+				foreach (explode(',', $data['metas_brutes']) as $meta) {
+					// On teste le cas ou un prefixe est indique (dernier caractere est *)
+					if (substr($meta, -1) == '*') {
+						$p = substr($meta, 0, -1);
+						foreach ($GLOBALS['meta'] as $m => $v) {
+							if (substr($m, 0, strlen($p)) == $p) {
+								$export_plugin[$m] = $v;
+							}
+						}
+					} elseif (isset($GLOBALS['meta'][$meta])) {
+						$export_plugin[$meta] = $GLOBALS['meta'][$meta];
+					}
+				}
+			}
+			if (isset($data['metas_serialize'])) {
+				foreach (explode(',', $data['metas_serialize']) as $meta) {
+					// On teste le cas ou un prefixe est indique (dernier caractere est *)
+					if (substr($meta, -1) == '*') {
+						$p = substr($meta, 0, -1);
+						foreach ($GLOBALS['meta'] as $m => $v) {
+							if (substr($m, 0, strlen($p)) == $p) {
+								$export_plugin[$m] = unserialize($v);
+							}
+						}
+					} elseif (isset($GLOBALS['meta'][$meta])) {
+						$export_plugin[$meta] = unserialize($GLOBALS['meta'][$meta]);
+					}
+				}
+			}
+			if (count($export_plugin) > 0) {
+				$export[$prefixe] = $export_plugin;
+			}
+		}
+	}
+
+	// On encode en yaml
+	include_spip('inc/yaml');
+	$export = yaml_encode($export, array('inline' => 20));
+
+	// Nom du fichier
+	include_spip('inc/texte');
+	$site = isset($GLOBALS['meta']['nom_site']) ? preg_replace(array(",\W,is", ',_(?=_),', ',_$,'), array(
+		'_',
+		'',
+		'',
+	), couper(translitteration(trim($GLOBALS['meta']['nom_site'])), 30, '')) : 'spip';
+	$filename = $site . '_' . date('Y-m-d_H-i') . '.yaml';
+
+	// Si telechargement
+	if (_request('ieconfig_export_choix') == 'telecharger') {
+		refuser_traiter_formulaire_ajax();
+		set_request('action', 'courcircuiter_affichage_usage_memoire'); // Pour empêcher l'extension dev d'ajouter un div avec l'usage mémoire.
+		header('Content-Type: text/x-yaml;');
+		header("Content-Disposition: attachment; filename=$filename");
+		header('Content-Length: ' . strlen($export));
+		echo $export;
+		exit;
+	} else {
+		sous_repertoire(_DIR_TMP, 'ieconfig');
+		if (ecrire_fichier(_DIR_TMP . 'ieconfig/' . $filename, $export)) {
+			return array('message_ok' => _T('ieconfig:message_ok_export', array('filename' => $filename)));
+		} else {
+			return array('message_erreur' => _T('ieconfig:message_erreur_export', array('filename' => $filename)));
+		}
+	}
+}
diff --git a/formulaires/ieconfig_import.php b/formulaires/ieconfig_import.php
new file mode 100644
index 00000000..43adccef
--- /dev/null
+++ b/formulaires/ieconfig_import.php
@@ -0,0 +1,359 @@
+<?php
+
+if (!defined('_ECRIRE_INC_VERSION')) {
+	return;
+}
+
+function ieconfig_metas_liste($prefixeaimporter = null) {
+	static $ieconfig_metas;
+	if (!is_array($ieconfig_metas)) {
+		$ieconfig_metas = array();
+		include_spip('inc/yaml');
+		$config = yaml_decode(_request('_code_yaml'));
+		
+		foreach (pipeline('ieconfig_metas', array()) as $prefixe => $data) {
+			if (isset($config[$prefixe])) {
+				if (!isset($prefixeaimporter) 
+					OR 
+					(isset($prefixeaimporter) && $prefixe === $prefixeaimporter)
+					) {
+					if (isset($data['icone'])) {
+						$icone = chemin_image($data['icone']);
+						if (!$icone) {
+							$icone = find_in_path($data['icone']);
+						}
+						if ($icone) {
+							$icone = '<img src="' . $icone . '" alt="" style="margin-left:-50px; margin-right:34px;" />';
+						}
+					} else {
+						$icone = 'config-export-16.png';
+					}
+					$ieconfig_metas[$prefixe] = $icone . (isset($data['titre']) ? $data['titre'] : $prefixe);
+				}
+			}
+		}
+	}
+	return $ieconfig_metas;
+}
+
+function ieconfig_saisies_import($prefixeaimporter = null) {
+	// Etape de selection du fichier
+	if (!_request('_code_yaml') or _request('annuler') or _request('importer')) {
+		$saisies = array(
+			array(
+				'saisie' => 'fieldset',
+				'options' => array(
+					'nom' => 'ieconfig_import_choix_fichier',
+					'label' => '<:ieconfig:label_ieconfig_import_choix_fichier:>',
+					'icone' => 'img/ieconfig-import.png',
+				),
+				'saisies' => array(
+					array(
+						'saisie' => 'input',
+						'options' => array(
+							'type' => 'file',
+							'nom' => 'ieconfig_import_fichier',
+							'label' => '<:ieconfig:label_ieconfig_import_fichier:>',
+							'explication' => '<:ieconfig:explication_ieconfig_import_fichier:>',
+						),
+					),
+					array(
+						'saisie' => 'selection',
+						'options' => array(
+							'type' => 'selection',
+							'nom' => 'ieconfig_import_local',
+							'label' => '<:ieconfig:label_ieconfig_import_local:>',
+							'explication' => '<:ieconfig:explication_ieconfig_import_local:>',
+							'datas' => ieconfig_config_locales($prefixeaimporter),
+						),
+					),
+					array(
+						'saisie' => 'radio',
+						'options' => array(
+							'nom' => 'import_methode',
+							'label' => '<:ieconfig:ieconfig_import:>',
+							'datas' => array(
+								'fusion' => '<:ieconfig:ieconfig_import_fusionner:>',
+								'fusion_inv' => '<:ieconfig:ieconfig_import_fusionner_inv:>',
+								'ecrase' => '<:ieconfig:ieconfig_import_ecraser:>',
+								),
+							'defaut' => 'fusion',
+						),
+					),
+				),
+			),
+		);
+		// Options d'importations
+	} else {
+		include_spip('inc/yaml');
+		$config = yaml_decode(_request('_code_yaml'));
+		$texte_explication = '<b>' . _T('ieconfig:texte_nom') . '</b> ' . _T_ou_typo($config['nom']);
+		if ($config['description'] != '') {
+			$texte_explication .= '<br /><b>' . _T('ieconfig:texte_description') . '</b> ' . _T_ou_typo($config['description']);
+		}
+		// On identifie les entrées ne correspondant pas à un plugin
+		// Ou bien non déclarées dans ieconfig_metas
+		// Convention : les clés du tableau de config correspondent aux préfixes des plugins
+		$entrees = $config;
+		unset($entrees['nom']);
+		unset($entrees['description']);
+		unset($entrees['necessite']);
+		$entrees = array_map('strtolower', array_keys($entrees));
+		$plugins = array_map('strtolower', array_keys(unserialize($GLOBALS['meta']['plugin'])));
+		$entrees_prises_en_charge = array_merge(array_keys(pipeline('ieconfig_metas', array())), $plugins);
+		$plugins_manquants = array_diff($entrees, $entrees_prises_en_charge);
+		if (count($plugins_manquants) > 0) {
+			$texte_explication .= '<p class="reponse_formulaire reponse_formulaire_erreur">' . _T('ieconfig:texte_plugins_manquants', array('plugins' => implode(', ', $plugins_manquants))) . '</p>';
+		}
+
+
+		// Gestion des plugins utilisant le pipeline ieconfig_metas
+		$ieconfig_metas = ieconfig_metas_liste($prefixeaimporter);
+		if (count($ieconfig_metas) > 1) {
+			$saisies = array(
+				array(
+					'saisie' => 'explication',
+					'options' => array(
+						'nom' => 'import_details',
+						'texte' => $texte_explication,
+					),
+				),
+			);
+			$saisies[] = array(
+				'saisie' => 'fieldset',
+				'options' => array(
+					'nom' => 'metas_fieldset',
+					'label' => _T('ieconfig:label_importer_metas').' ('._request('import_methode').')',
+					'icone' => 'config-export-16.png',
+				),
+				'saisies' => array(
+					array(
+						'saisie' => 'checkbox',
+						'options' => array(
+							'nom' => 'import_metas',
+							'label' => _T('ieconfig:label_importer'),
+							'tout_selectionner' => (count($ieconfig_metas) > 1) ? 'oui':'',
+							'datas' => $ieconfig_metas,
+						),
+					),
+					array(
+						'saisie' => 'hidden',
+						'options' => array(
+							'default' => _request('import_methode'),
+							'nom' => 'import_methode',
+							'label' => 'import_methode',
+						),
+					),
+				)
+			);
+		}
+		
+		// On passe via le pipeline ieconfig
+		$saisies = pipeline('ieconfig', array(
+			'args' => array(
+				'action' => 'form_import',
+				'config' => $config,
+			),
+			'data' => $saisies,
+		));
+	}
+	return $saisies;
+}
+
+function formulaires_ieconfig_import_charger_dist($prefixeaimporter = null) {
+	include_spip('inc/saisies');
+	$saisies = ieconfig_saisies_import($prefixeaimporter);
+	$contexte = array(
+		'_saisies' => $saisies,
+	);
+	if (_request('_code_yaml') and !_request('annuler') and !_request('importer')) {
+		$contexte['_code_yaml'] = _request('_code_yaml');
+	}
+	
+
+	return array_merge(saisies_charger_champs($saisies), $contexte);
+}
+
+function formulaires_ieconfig_import_verifier_dist($prefixeaimporter = null) {
+	$erreurs = array();
+	// Etape de selection du fichier
+	if (!_request('_code_yaml')) {
+		// On a rien transmis et pas de fichier local
+		if (!_request('ieconfig_import_local') and $_FILES['ieconfig_import_fichier']['name'] == '') {
+			$erreurs['message_erreur'] = _T('ieconfig:message_erreur_fichier_import_manquant');
+		}
+	} // Options d'import
+	else {
+		include_spip('inc/saisies');
+		$erreurs = saisies_verifier(ieconfig_saisies_import($prefixeaimporter));
+	}
+
+	return $erreurs;
+}
+
+function formulaires_ieconfig_import_traiter_dist($prefixeaimporter = null) {
+
+	include_spip('inc/config');
+	// Si on est à l'étape de sélection d'un fichier de configuration
+	// On place le code YAML dans le contexte
+	if (!_request('_code_yaml')) {
+		if ($_FILES['ieconfig_import_fichier']['name'] != '') {
+			$dir = sous_repertoire(_DIR_TMP, 'ieconfig');
+			$hash = md5('ieimport-' . $GLOBALS['visiteur_session']['id_auteur'] . time());
+			$fichier = $dir . $hash . '-' . $_FILES['ieconfig_import_fichier']['name'];
+			move_uploaded_file($_FILES['ieconfig_import_fichier']['tmp_name'], $fichier);
+			lire_fichier($fichier, $code_yaml);
+			@unlink($fichier);
+		} else {
+			$fichier = _request('ieconfig_import_local');
+			lire_fichier($fichier, $code_yaml);
+		}
+		set_request('_code_yaml', $code_yaml);
+	} // Si on valide l'import
+	$ieconfig_metas = ieconfig_metas_liste($prefixeaimporter);
+	if (((count($ieconfig_metas) == 1) OR _request('importer')) && _request('_code_yaml')) {
+		include_spip('inc/yaml');
+		$config = yaml_decode(_request('_code_yaml'));
+
+		// On passe via le pipeline ieconfig
+		$message_erreur = pipeline('ieconfig', array(
+			'args' => array(
+				'action' => 'import',
+				'config' => $config,
+			),
+			'data' => '',
+		));
+
+		if (count($ieconfig_metas) == 1) {
+			$import_metas = array(key($ieconfig_metas));
+		} else {
+			$import_metas = _request('import_metas');
+		}
+		if (!is_array($import_metas)) {
+			$import_metas = array();
+		}
+		// Gestion des plugins utilisant le pipeline ieconfig_metas
+		$option = _request('import_methode');
+		$config_importee = '';
+		foreach (pipeline('ieconfig_metas', array()) as $prefixe => $data) {
+			if (in_array($prefixe, $import_metas) && isset($config[$prefixe])) {
+				$config_importee .= $prefixe . ' ';
+				if (isset($data['metas_brutes'])) {
+					foreach (explode(',', $data['metas_brutes']) as $meta) {
+						// On teste le cas ou un prefixe est indique (dernier caractere est *)
+						if (substr($meta, -1) == '*') {
+							$p = substr($meta, 0, -1);
+							foreach ($config[$prefixe] as $m => $v) {
+								if (substr($m, 0, strlen($p)) == $p) {
+									if (($option === 'ecrase') OR ($option === 'fusion')) {
+										ecrire_config($m);
+									}
+									if ($option === 'fusion_inv') {
+										if (is_null(lire_config($m . '/'))) {
+											ecrire_config($m . '/', $v);
+										}
+									}
+								}
+							}
+						} elseif (isset($config[$prefixe][$meta])) {
+							if (($option === 'ecrase') OR ($option === 'fusion')) {
+								ecrire_config($meta . '/', $config[$prefixe][$meta]);
+							}
+							if ($option === 'fusion_inv') {
+								if (is_null(lire_config($meta . '/'))) {
+									ecrire_config($meta . '/', $config[$prefixe][$meta]);
+								}
+							}
+						}
+					}
+				}
+				if (isset($data['metas_serialize'])) {
+					foreach (explode(',', $data['metas_serialize']) as $meta) {
+						// On teste le cas ou un prefixe est indique (dernier caractere est *)
+						if (substr($meta, -1) == '*') {
+							$p = substr($meta, 0, -1);
+							foreach ($config[$prefixe] as $m => $v) {
+								if (substr($m, 0, strlen($p)) == $p) {
+									$import = array();
+									if ($option === 'ecrase') {
+										$import = $v;
+									}
+									if ($option === 'fusion') {
+										$import = array_merge(lire_config($m . '/'), $v);
+									}
+									if ($option === 'fusion_inv') {
+										$import_plus = array_diff_key($v, lire_config($meta . '/'));
+										$import_base = array_diff_key(lire_config($meta . '/'), $import_plus);
+										$import = (array_merge($import_plus, $import_base));
+									}
+									ksort($import);
+									ecrire_config($m . '/', serialize($import));
+								}
+							}
+						} elseif (isset($config[$prefixe][$meta])) {
+							$import = array();
+							if ($option === 'ecrase') {
+								$import = $config[$prefixe][$meta];
+							}
+							if ($option === 'fusion') {
+								$import = array_merge(lire_config($meta . '/'), $config[$prefixe][$meta]);
+							}
+							if ($option === 'fusion_inv') {
+								$import_plus = array_diff_key($config[$prefixe][$meta], lire_config($meta . '/'));
+								$import_base = array_diff_key(lire_config($meta . '/'), $import_plus);
+								$import = (array_merge($import_plus, $import_base));
+							}
+							ksort($import);
+							ecrire_config($meta . '/', serialize($import));
+						}
+					}
+				}
+			}
+		}
+
+		if ($message_erreur != '') {
+			return array('message_erreur' => $message_erreur);
+		} else {
+			return array('message_ok' => _T('ieconfig:message_ok_import') . " ($option / $config_importee)");
+		}
+	}
+}
+
+// Renvoie la liste des fichiers de configurations présents dans un sous-répertoires ieconfig/
+function ieconfig_config_locales($prefixeaimporter = null) {
+	static $liste_config = null;
+
+	if (is_null($liste_config)) {
+		include_spip('inc/yaml');
+		$liste_config = array();
+		$match = '.+[.]yaml$';
+		foreach (array_merge(find_all_in_path('ieconfig/', $match), find_all_in_path(_DIR_TMP . 'ieconfig/', $match)) as $fichier => $chemin) {
+			$config = yaml_decode_file($chemin);
+			// On regarde s'il y a un necessite
+			$ok = true;
+			if (isset($config['necessite'])) {
+				if (!is_array($config['necessite'])) {
+					$config['necessite'] = array($config['necessite']);
+				}
+				foreach ($config['necessite'] as $plugin) {
+					if (!defined('_DIR_PLUGIN_' . strtoupper($plugin))) {
+						$ok = false;
+					}
+				}
+			}
+			if (isset($prefixeaimporter) AND !isset($config[$prefixeaimporter])) {
+				$ok = false;
+			}
+			//on vérifie s'il y a un champs nom
+			if ($ok) {
+				if (isset($config['nom'])) {
+					$liste_config[$chemin] = _T_ou_typo($config['nom']);
+				} else {
+					$liste_config[$chemin] = $fichier;
+				}
+			}
+		}
+	}
+
+	return $liste_config;
+}
diff --git a/paquet.xml b/paquet.xml
index 8361ea03..402f38cf 100644
--- a/paquet.xml
+++ b/paquet.xml
@@ -1,7 +1,7 @@
 <paquet
 	prefix="soyezcreateurs"
 	categorie="squelette"
-	version="5.0.5"
+	version="5.0.6"
 	etat="stable"
 	compatibilite="[3.1.8;3.2.*]"
 	logo="img_pack/soyezcreateurs_48.png"
diff --git a/prive/squelettes/navigation/configurer_soyezcreateurs.html b/prive/squelettes/navigation/configurer_soyezcreateurs.html
index b730cd8e..b97219c0 100644
--- a/prive/squelettes/navigation/configurer_soyezcreateurs.html
+++ b/prive/squelettes/navigation/configurer_soyezcreateurs.html
@@ -2,5 +2,5 @@
 #FORMULAIRE_IECONFIG_IMPORT{#ENV{cfg,soyezcreateurs}}
 </div>
 <div class="ajax">
-#FORMULAIRE_IECONFIG_EXPORT
+#FORMULAIRE_IECONFIG_EXPORT{#ENV{cfg,soyezcreateurs}}
 </div>
\ No newline at end of file
-- 
GitLab