From ce1ab594828fbb8519db8e28b9248f418934dc6f Mon Sep 17 00:00:00 2001
From: Fil <fil@rezo.net>
Date: Sat, 19 May 2007 21:35:54 +0000
Subject: [PATCH] separation de la grosse fonction de creation automatique de
 site (qui sait meme creer des bases de donnees si on le lui demande
 gentiment, cf. http://www.spip-contrib.net/La-mutualisation-facile

---
 .gitattributes                  |   1 +
 ecrire/inc/mutualiser.php       |  82 ++++-----------
 ecrire/inc/mutualiser_creer.php | 172 ++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+), 60 deletions(-)
 create mode 100644 ecrire/inc/mutualiser_creer.php

diff --git a/.gitattributes b/.gitattributes
index 28e9401353..212a827eff 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -579,6 +579,7 @@ ecrire/inc/message_select.php -text
 ecrire/inc/minipres.php -text
 ecrire/inc/modifier.php -text
 ecrire/inc/mutualiser.php -text
+ecrire/inc/mutualiser_creer.php -text
 ecrire/inc/notifications.php -text
 ecrire/inc/petitionner.php -text
 ecrire/inc/plonger.php -text
diff --git a/ecrire/inc/mutualiser.php b/ecrire/inc/mutualiser.php
index 9dc7667f0b..5a58247c12 100644
--- a/ecrire/inc/mutualiser.php
+++ b/ecrire/inc/mutualiser.php
@@ -24,8 +24,10 @@ function demarrer_site($site = '', $options = array()) {
 	$options = array_merge(
 		array(
 			'creer_site' => false,
-			'cookie_prefix' => true,
-			'table_prefix' => true,
+			'creer_base' => false,
+			'mail' => '',
+			'table_prefix' => false,
+			'cookie_prefix' => false,
 			'repertoire' => 'sites'
 		),
 		$options
@@ -33,20 +35,15 @@ function demarrer_site($site = '', $options = array()) {
 
 	// Le prefixe = max 10 caracteres a-z0-9, qui ressemblent au domaine
 	// et ne commencent pas par un chiffre
-	if ($options['cookie_prefix'] OR $options['table_prefix']) {
-		$prefix = preg_replace(',^www\.|[^a-z0-9],', '', strtolower($site));
-		$prefix = substr($prefix, 0, 10);
-		if (!preg_match(',^[a-z],', $prefix))
-			$prefix = 'a'.$prefix;
-		if ($options['cookie_prefix'])
-			$GLOBALS['cookie_prefix'] = $prefix;
-		if ($options['table_prefix'])
-			$GLOBALS['table_prefix'] = $prefix;
-	}
+	if ($options['cookie_prefix'])
+		$GLOBALS['cookie_prefix'] = prefixe_mutualisation($site);
+	if ($options['table_prefix'])
+		$GLOBALS['table_prefix'] = prefixe_mutualisation($site);
 
 	if (!is_dir($e = _DIR_RACINE . $options['repertoire'].'/' . $site . '/')) {
 		spip_initialisation();
-		echec_init_mutualisation($e, $options);
+		include_spip('inc/mutualiser_creer');
+		mutualiser_creer($e, $options);
 		exit;
 	}
 
@@ -72,55 +69,20 @@ function demarrer_site($site = '', $options = array()) {
 
 }
 
+// Cette fonction cree un prefixe acceptable par MySQL a partir du nom
+// du site ; a utiliser comme prefixe des tables, comme suffixe du nom
+// de la base de donnees ou comme prefixe des cookies...
+function prefixe_mutualisation($site) {
+	static $prefix;
 
-// http://doc.spip.org/@echec_init_mutualisation
-function echec_init_mutualisation($e, $options) {
-	include_spip('inc/minipres');
-
-	if ($options['creer_site']) {
-		$ok_dir =
-		is_dir(_DIR_RACINE . $options['repertoire'])
-		AND is_writable(_DIR_RACINE . $options['repertoire']);
-
-		$ok = $ok_dir
-		AND mkdir($e, _SPIP_CHMOD)
-		AND chmod($e, _SPIP_CHMOD)
-		AND mkdir($e._NOM_PERMANENTS_INACCESSIBLES, _SPIP_CHMOD)
-		AND mkdir($e._NOM_PERMANENTS_ACCESSIBLES, _SPIP_CHMOD)
-		AND mkdir($e._NOM_TEMPORAIRES_INACCESSIBLES, _SPIP_CHMOD)
-		AND mkdir($e._NOM_TEMPORAIRES_ACCESSIBLES, _SPIP_CHMOD)
-		AND chmod($e._NOM_PERMANENTS_INACCESSIBLES, _SPIP_CHMOD)
-		AND chmod($e._NOM_PERMANENTS_ACCESSIBLES, _SPIP_CHMOD)
-		AND chmod($e._NOM_TEMPORAIRES_INACCESSIBLES, _SPIP_CHMOD)
-		AND chmod($e._NOM_TEMPORAIRES_ACCESSIBLES, _SPIP_CHMOD);
-
-		echo minipres(
-			_L('Creation du r&eacute;pertoire du site (<tt>'.$e.'</tt>)'),
-
-				"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n"
-				.'<h3>'.($ok
-					? _L('OK, vous pouvez <a href="'.generer_url_ecrire('install').'">installer votre site</a>.')
-					: _L('erreur')
-				).'</h3>'
-				. (!$ok_dir ? _L('Le r&#233;pertoire <tt>'.$options['repertoire'].'/</tt> n\'est pas accessible en &#233;criture') : '')
-		);
-	} else {
-		echo minipres(
-			_L('Le r&eacute;pertoire du site (<tt>'.$e.'</tt>) n\'existe pas'),
-			"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n".
-			'<h3>'
-			._L('Veuillez créer le répertoire '.$e.' et ses sous répertoires:')
-			.'</h3>'
-			.'<ul>'
-			.'<li>'.$e._NOM_PERMANENTS_INACCESSIBLES.'</li>'
-			.'<li>'.$e._NOM_PERMANENTS_ACCESSIBLES.'</li>'
-			.'<li>'.$e._NOM_TEMPORAIRES_INACCESSIBLES.'</li>'
-			.'<li>'.$e._NOM_TEMPORAIRES_ACCESSIBLES.'</li>'
-			.'</ul>'
-		);
+	if (!isset($prefix)) {
+		$prefix = preg_replace(',^www\.|[^a-z0-9],', '', strtolower($site));
+		$prefix = substr($prefix, 0, 10);
+		if (!preg_match(',^[a-z],', $prefix))
+			$prefix = 'a'.$prefix;
 	}
-}
-
+	return $prefix;
 
+}
 
 ?>
diff --git a/ecrire/inc/mutualiser_creer.php b/ecrire/inc/mutualiser_creer.php
new file mode 100644
index 0000000000..25427db1dd
--- /dev/null
+++ b/ecrire/inc/mutualiser_creer.php
@@ -0,0 +1,172 @@
+<?php
+
+/***************************************************************************\
+ *  SPIP, Systeme de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright (c) 2001-2007                                                *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
+ *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+if (!defined("_ECRIRE_INC_VERSION")) return;
+
+
+// http://doc.spip.org/@echec_init_mutualisation
+function mutualiser_creer($e, $options) {
+	include_spip('inc/minipres');
+	$GLOBALS['meta']["charset"] = 'utf-8'; // pour que le mail fonctionne
+
+
+	if ($options['creer_base']) {
+
+		if (defined('_INSTALL_HOST_DB')
+		AND defined('_INSTALL_USER_DB')
+		AND defined('_INSTALL_PASS_DB')
+		AND defined('_INSTALL_NAME_DB')) {
+			$link = mysql_connect(_INSTALL_HOST_DB, _INSTALL_USER_DB, _INSTALL_PASS_DB);
+
+			// si la base n'existe pas, on va travailler
+			if (!mysql_select_db(_INSTALL_NAME_DB)) {
+				if (_request('creerbase')) {
+					if (mysql_query('CREATE DATABASE '._INSTALL_NAME_DB)
+					AND mysql_select_db(_INSTALL_NAME_DB)) {
+						echo minipres(
+							_L('La base de donn&#233;es <tt>'._INSTALL_NAME_DB.'</tt> a &#233;t&#233; cr&#233;&#233;e'),
+							"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n".
+							'<h3>'
+							._L('<a href="'.parametre_url(self(), 'creerbase', null).'">Continuer...</a>')
+							.'</h3>'
+						);
+						if ($options['mail']) {
+							include_spip('inc/mail');
+							echo envoyer_mail($options['mail'],
+								_L('Creation de la base de donn&#233;es '._INSTALL_NAME_DB),
+								_L('La base de donn&#233;es '._INSTALL_NAME_DB.' a &#233;t&#233; cr&#233;&#233;e pour le site '.$e),
+								$options['mail']
+							);
+						}
+						exit;
+					} else {
+						echo minipres(
+							_L('Cr&#233;ation de la base de donn&#233;es <tt>'._INSTALL_NAME_DB.'</tt>'),
+							"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n".
+							'<h3>'
+							._L('erreur')
+							.'</h3>'
+						);
+						exit;
+					}
+
+				}
+				else {
+					echo minipres(
+						_L('Cr&#233;ation de la base de donn&#233;es <tt>'._INSTALL_NAME_DB.'</tt>'),
+						"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n".
+						'<h3>'
+						._L('Voulez-vous <a href="'.parametre_url(self(), 'creerbase', 'oui').'">cr&#233;er cette base ?</a>')
+						.'</h3>'
+					);
+					exit;
+				}
+			}
+
+			// ici la base existe, on passe aux repertoires
+		}
+		else {
+			echo minipres(
+				_L('Creation de la base de donn&#233;es du site (<tt>'.joli_repertoire($e).'</tt>)'),
+
+				"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n"
+				.'<h3>'. _L('erreur') .'</h3>'
+				. _L('Les donn&#233;es de connexion MySQL ne sont pas d&#233;finies, impossible de cr&#233;er automatiquement la base.')
+			);
+			exit;
+		}
+	}
+
+	if ($options['creer_site']) {
+		$ok_dir =
+		is_dir(_DIR_RACINE . $options['repertoire'])
+		AND is_writable(_DIR_RACINE . $options['repertoire']);
+
+		if (!$ok_dir) {
+			echo minipres(
+				_L('Creation du r&eacute;pertoire du site (<tt>'.joli_repertoire($e).'</tt>)'),
+
+				"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n"
+				.'<h3>'. _L('erreur') .'</h3>'
+				. _L('Le r&#233;pertoire <tt>'.$options['repertoire'].'/</tt> n\'est pas accessible en &#233;criture')
+			);
+			exit;
+		}
+
+		if (_request('creerrepertoire')) {
+			$ok =
+			mkdir($e, _SPIP_CHMOD)
+			AND chmod($e, _SPIP_CHMOD)
+			AND mkdir($e._NOM_PERMANENTS_INACCESSIBLES, _SPIP_CHMOD)
+			AND mkdir($e._NOM_PERMANENTS_ACCESSIBLES, _SPIP_CHMOD)
+			AND mkdir($e._NOM_TEMPORAIRES_INACCESSIBLES, _SPIP_CHMOD)
+			AND mkdir($e._NOM_TEMPORAIRES_ACCESSIBLES, _SPIP_CHMOD)
+			AND chmod($e._NOM_PERMANENTS_INACCESSIBLES, _SPIP_CHMOD)
+			AND chmod($e._NOM_PERMANENTS_ACCESSIBLES, _SPIP_CHMOD)
+			AND chmod($e._NOM_TEMPORAIRES_INACCESSIBLES, _SPIP_CHMOD)
+			AND chmod($e._NOM_TEMPORAIRES_ACCESSIBLES, _SPIP_CHMOD);
+
+			echo minipres(
+				_L('Creation du r&eacute;pertoire du site (<tt>'.joli_repertoire($e).'</tt>)'),
+
+				"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n"
+				.'<h3>'
+				. ($ok
+					? _L('Cr&#233;ation des r&#233;pertoires OK. Vous pouvez <a href="'.generer_url_ecrire('install').'">installer votre site</a>.')
+					: _L('erreur')
+				).'</h3>'
+			);
+
+			if ($options['mail']) {
+				include_spip('inc/mail');
+				envoyer_mail($options['mail'],
+					_L('Creation du site '.joli_repertoire($e)),
+					_L('Les répertoires du site '.$e.' ont &#233;t&#233; cr&#233;&#233;s.'),
+					$options['mail']
+				);
+			}
+			exit;
+
+		} else {
+			echo minipres(
+				_L('Creation du r&eacute;pertoire du site (<tt>'.joli_repertoire($e).'</tt>)'),
+
+				"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n"
+				.'<h3>'.
+					_L('Voulez-vous <a href="'.parametre_url(self(), 'creerrepertoire', 'oui').'">cr&#233;er les r&#233;pertoires de ce site ?</a>')
+				.'</h3>'
+				. (!$ok_dir ? _L('Le r&#233;pertoire <tt>'.$options['repertoire'].'/</tt> n\'est pas accessible en &#233;criture') : '')
+			);
+			exit;
+
+		}
+
+	} else {
+		echo minipres(
+			_L('Le r&eacute;pertoire du site (<tt>'.joli_repertoire($e).'</tt>) n\'existe pas'),
+			"<div><img alt='SPIP' src='" . _DIR_IMG_PACK . "logo-spip.gif' /></div>\n".
+			'<h3>'
+			._L('Veuillez créer le répertoire '.joli_repertoire($e).' et ses sous répertoires:')
+			.'</h3>'
+			.'<ul>'
+			.'<li>'.joli_repertoire($e)._NOM_PERMANENTS_INACCESSIBLES.'</li>'
+			.'<li>'.joli_repertoire($e)._NOM_PERMANENTS_ACCESSIBLES.'</li>'
+			.'<li>'.joli_repertoire($e)._NOM_TEMPORAIRES_INACCESSIBLES.'</li>'
+			.'<li>'.joli_repertoire($e)._NOM_TEMPORAIRES_ACCESSIBLES.'</li>'
+			.'</ul>'
+		);
+		exit;
+
+	}
+}
+
+?>
-- 
GitLab