From 2c2f12ba79358e826cf658b6f9cf307ddc7531eb Mon Sep 17 00:00:00 2001
From: Antoine Pitrou <pitrou@free.fr>
Date: Sat, 20 Apr 2002 00:07:41 +0000
Subject: [PATCH] =?UTF-8?q?Boulot=20pr=C3=A9liminaire=20pour=20inclusion?=
 =?UTF-8?q?=20de=20squelettes=20rudimentaire.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 inc-cache.php3  |  72 +++++++++++++++++++++++++++++
 inc-calcul.php3 |  88 ++++++++++++++++++++++++++----------
 inc-public.php3 | 118 +++++++++++++++++-------------------------------
 3 files changed, 179 insertions(+), 99 deletions(-)

diff --git a/inc-cache.php3 b/inc-cache.php3
index 34929e51b4..06608a2246 100644
--- a/inc-cache.php3
+++ b/inc-cache.php3
@@ -6,6 +6,78 @@ if (defined("_INC_CACHE")) return;
 define("_INC_CACHE", "1");
 
 
+//
+// Inclure un fichier cache
+//
+
+function include_cache($chemin_cache) {
+	include_local($chemin_cache);
+	if ($GLOBALS['flag_apc']) {
+		apc_rm($chemin_cache);
+	}
+}
+
+//
+// Calcul du nom du fichier cache
+//
+
+function generer_nom_fichier_cache($fichier_requete) {
+	$md_cache = md5($fichier_requete);
+	
+	$fichier_cache = ereg_replace('^/+', '', $fichier_requete);
+	$fichier_cache = ereg_replace('\.[a-zA-Z0-9]*', '', $fichier_cache);
+	$fichier_cache = ereg_replace('&[^&]+=([^&]+)', '&\1', $fichier_cache);
+	$fichier_cache = rawurlencode(strtr($fichier_cache, '/&-', '--_'));
+	if (strlen($fichier_cache) > 24)
+		$fichier_cache = substr(ereg_replace('([a-zA-Z]{1,3})[^-]*-', '\1-', $fichier_cache), -24);
+	
+	if (!$fichier_cache)
+		$fichier_cache = 'INDEX-';
+	$fichier_cache .= '.'.substr($md_cache, 1, 6);
+	
+	$subdir_cache = substr($md_cache, 0, 1);
+	
+	if (creer_repertoire("CACHE", $subdir_cache))
+		$fichier_cache = "$subdir_cache/$fichier_cache";
+	
+	return $fichier_cache;
+}
+
+
+//
+// Doit-on recalculer le cache ?
+//
+
+function utiliser_cache($chemin_cache) {
+	global $HTTP_SERVER_VARS, $HTTP_POST_VARS;
+	global $lastmodified;
+
+	$use_cache = true;
+	if (file_exists($chemin_cache)) {
+		// Eviter de recalculer pour les moteurs de recherche, proxies...
+		if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == 'HEAD') {
+			$use_cache = true;
+		}
+		else {
+			$lastmodified = filemtime($chemin_cache);
+			$ledelais = time() - $lastmodified;
+			$use_cache &= ($ledelais < $delais AND $ledelais > 0);
+		}
+	}
+	else {
+		$use_cache = false;
+	}
+	$use_cache &= ($GLOBALS['recalcul'] != 'oui');
+	$use_cache &= empty($HTTP_POST_VARS);
+
+	if (!$use_cache) {
+		include_ecrire("inc_connect.php3");
+		if (!$GLOBALS['db_ok']) $use_cache = true;
+	}
+	return $use_cache;
+}
+
+
 //
 // Retourne true si le sous-repertoire peut etre cree, false sinon
 //
diff --git a/inc-calcul.php3 b/inc-calcul.php3
index e1bb42fda8..ebf72133ce 100644
--- a/inc-calcul.php3
+++ b/inc-calcul.php3
@@ -266,35 +266,46 @@ class InstanceBoucle {
 function executer_squelette($squelette, $contexte) {
 	global $pile_boucles;
 	global $ptr_pile_boucles;
+	static $fonctions_squelettes = '';
 
 	$pile_boucles = '';
 	$ptr_pile_boucles = 0;
 
-	$squelette_cache = 'CACHE/skel_'.rawurlencode($squelette).'.php3';
-	$use_cache = false;
-	if (file_exists($squelette_cache)) {
-		$t = filemtime($squelette_cache);
-		if ((filemtime("$squelette.html") < $t)
-		AND (filemtime("inc-calcul-squel.php3") < $t)
-		AND (!file_exists("mes_fonctions.php3") OR (filemtime("mes_fonctions.php3") < $t))) {
-			$use_cache = true;
-		}
-	}
-	if ($GLOBALS['recalcul_squelettes'] == 'oui') {
+	// Si squelette pas deja inclus, l'inclure
+	if (!$fonctions_squelettes[$squelette]) {
+		$squelette_cache = 'CACHE/skel_'.rawurlencode($squelette).'.php3';
 		$use_cache = false;
-	}
-	
-	if (!$use_cache) {
-		include_local ("inc-calcul-squel.php3");
-		calculer_squelette($squelette, $squelette_cache);
-	}
+		if (file_exists($squelette_cache)) {
+			$t = filemtime($squelette_cache);
+			if ((filemtime("$squelette.html") < $t)
+			AND (filemtime("inc-calcul-squel.php3") < $t)
+			AND (!file_exists("mes_fonctions.php3") OR (filemtime("mes_fonctions.php3") < $t))) {
+				$use_cache = true;
+			}
+		}
+		if ($GLOBALS['recalcul_squelettes'] == 'oui') {
+			$use_cache = false;
+		}
 
-	include($squelette_cache);
-	if ($GLOBALS['flag_apc']) {
-		apc_rm($squelette_cache);
+		// Au besoin, recompiler le squelette
+		if (!$use_cache) {
+			include_local ("inc-calcul-squel.php3");
+			calculer_squelette($squelette, $squelette_cache);
+		}
+
+		// L'inclusion du squelette permet de definir les fonctions associees
+		// aux boucles, et de recuperer le nom de la fonction principale	
+		include($squelette_cache);
+		$fonctions_squelettes[$squelette] = $func_squelette_executer;
+		if ($GLOBALS['flag_apc']) {
+			apc_rm($squelette_cache);
+		}
 	}
 
-	return $func_squelette_executer($contexte);
+	// Executer la fonction principale du squelette
+	// (i.e. racine de l'arbre d'execution)
+	$f = $fonctions_squelettes[$squelette];
+	return $f($contexte);
 }
 
 
@@ -364,8 +375,6 @@ function calculer_page($fond) {
 	$id_doublons['syndication'] = '0';
 	$id_doublons['documents'] = '0';
 
-	$fond = chercher_squelette($fond, $id_rubrique_fond);
-	
 	$contexte_defaut = array('id_parent', 'id_rubrique', 'id_article', 'id_auteur',
 		'id_breve', 'id_forum', 'id_secteur', 'id_syndic', 'id_mot', 'id_document');
 	reset($contexte_defaut);
@@ -378,6 +387,39 @@ function calculer_page($fond) {
 		$contexte["date"] = $GLOBALS["date"];
 	}
 
+	// Calcul de la rubrique associee a la requete
+	// (selection de squelette specifique)
+
+	if ($id_rubrique = $contexte['id_rubrique']) {
+		$id_rubrique_fond = $id_rubrique;
+	}
+	else if ($id_breve  = $contexte['id_breve']) {
+		$query = "SELECT id_rubrique FROM spip_breves WHERE id_breve='$id_breve'";
+		$result = spip_query($query);
+		while($row = mysql_fetch_array($result)) {
+			$id_rubrique_fond = $row[0];
+		}
+	}
+	else if ($id_syndic = $contexte['id_syndic']) {
+		$query = "SELECT id_rubrique FROM spip_syndic WHERE id_syndic='$id_syndic'";
+		$result = spip_query($query);
+		while($row = mysql_fetch_array($result)) {
+			$id_rubrique_fond = $row[0];
+		}
+	}
+	else if ($id_article = $contexte['id_article']) {
+		$query = "SELECT id_rubrique FROM spip_articles WHERE id_article='$id_article'";
+		$result = spip_query($query);
+		while($row = mysql_fetch_array($result)) {
+			$id_rubrique_fond = $row[0];
+		}
+	}
+	else {
+		$id_rubrique_fond = 0;
+	}
+
+	$fond = chercher_squelette($fond, $id_rubrique_fond);
+
 	recuperer_parametres_url($fond, $fichier_requete);
 
 	// Special stats et boutons admin
diff --git a/inc-public.php3 b/inc-public.php3
index a911d160c6..f75266a14e 100644
--- a/inc-public.php3
+++ b/inc-public.php3
@@ -1,5 +1,37 @@
 <?php
 
+if (defined("_INC_PUBLIC")) {
+	global $contexte;
+	$fichier_requete = $fond;
+	if (is_array($contexte)) {
+		reset($contexte);
+		while(list($key, $val) = each($contexte)) $fichier_requete .= '&'.$key.'='.$val;
+	}
+	echo $fichier_requete."<p>";
+	$fichier_cache = generer_nom_fichier_cache($fichier_requete);
+	$chemin_cache = "CACHE/".$fichier_cache;
+	$use_cache = utiliser_cache($chemin_cache);
+
+	if (!$use_cache) {
+		include_ecrire("inc_connect.php3");
+		include_local("inc-calcul.php3");
+		$page = executer_squelette($fond, $contexte);
+		if ($page) {
+			$f = fopen($chemin_cache, "wb");
+			fwrite($f, $page);
+			fclose($f);
+		}
+	}
+
+	include_cache($chemin_cache);
+
+	// ATTENTION : ne marchera pas sous PHP3
+	return;
+}
+
+
+define("_INC_PUBLIC", "1");
+
 $dir_ecrire = 'ecrire/';
 include ("ecrire/inc_version.php3");
 include_local ("inc-cache.php3");
@@ -15,62 +47,19 @@ if ($ajout_forum) {
 
 
 //
-// Calcul du nom du fichier cache
+// Gestion du cache et calcul de la page
 //
 
 $fichier_requete = $REQUEST_URI;
 $fichier_requete = strtr($fichier_requete, '?', '&');
 $fichier_requete = eregi_replace('&(submit|valider|(var_[^=&]*)|recalcul)=[^&]*', '', $fichier_requete);
 
-$md_cache = md5($fichier_requete);
-
-$fichier_cache = ereg_replace('^/+', '', $fichier_requete);
-$fichier_cache = ereg_replace('\.[a-zA-Z0-9]*', '', $fichier_cache);
-$fichier_cache = ereg_replace('&[^&]+=([^&]+)', '&\1', $fichier_cache);
-$fichier_cache = rawurlencode(strtr($fichier_cache, '/&-', '--_'));
-if (strlen($fichier_cache) > 24)
-	$fichier_cache = substr(ereg_replace('([a-zA-Z]{1,3})[^-]*-', '\1-', $fichier_cache), -24);
-
-if (!$fichier_cache)
-	$fichier_cache = 'INDEX-';
-$fichier_cache .= '.'.substr($md_cache, 1, 6);
-
-$subdir_cache = substr($md_cache, 0, 1);
-
-if (creer_repertoire("CACHE", $subdir_cache))
-	$fichier_cache = "$subdir_cache/$fichier_cache";
-
-$chemin_cache = "CACHE/$fichier_cache";
+$fichier_cache = generer_nom_fichier_cache($fichier_requete);
+$chemin_cache = "CACHE/".$fichier_cache;
 
 
-//
-// Doit-on recalculer le cache ?
-//
+$use_cache = utiliser_cache($chemin_cache);
 
-$use_cache = true;
-
-if (file_exists($chemin_cache)) {
-	// Eviter de recalculer pour les moteurs de recherche, proxies...
-	if ($REQUEST_METHOD == 'HEAD') {
-		$use_cache = true;
-	}
-	else {
-		$lastmodified = filemtime($chemin_cache);
-		$ledelais = time() - $lastmodified;
-		$use_cache &= ($ledelais < $delais AND $ledelais > 0);
-	}
-}
-else {
-	$use_cache = false;
-}
-
-$use_cache &= ($recalcul != 'oui');
-$use_cache &= empty($HTTP_POST_VARS);
-
-if (!$use_cache) {
-	include_ecrire("inc_connect.php3");
-	if (!$db_ok) $use_cache = true;
-}
 
 if ($use_cache) {
 	if (file_exists("ecrire/inc_meta_cache.php3")) {
@@ -96,29 +85,11 @@ else {
 
 	$calculer_cache = true;
 
-	if ($id_rubrique) {
-		$id_rubrique_fond = $id_rubrique;
-	}
-	else if ($id_breve) {
-		$query = "SELECT id_rubrique FROM spip_breves WHERE id_breve='$id_breve'";
-		$result = spip_query($query);
-		while($row = mysql_fetch_array($result)) {
-			$id_rubrique_fond = $row[0];
-		}
-	}
-	else if ($id_syndic) {
-		$query = "SELECT id_rubrique FROM spip_syndic WHERE id_syndic='$id_syndic'";
-		$result = spip_query($query);
-		while($row = mysql_fetch_array($result)) {
-			$id_rubrique_fond = $row[0];
-		}
-	}
-	else if ($id_article) {
-		$query = "SELECT id_rubrique, chapo FROM spip_articles WHERE id_article='$id_article'";
+	if ($id_article) {
+		$query = "SELECT chapo FROM spip_articles WHERE id_article='$id_article'";
 		$result = spip_query($query);
 		while($row = mysql_fetch_array($result)) {
-			$id_rubrique_fond = $row[0];
-			$chapo = $row[1];
+			$chapo = $row[0];
 		}
 		if (substr($chapo, 0, 1) == '=') {
 			$url = substr($chapo, 1);
@@ -129,9 +100,7 @@ else {
 			fclose($file);
 		}
 	}
-	else {
-		$id_rubrique_fond = 0;
-	}
+
 	if ($calculer_cache) {
 		include_local ("inc-calcul.php3");
 		$page = calculer_page($fond);
@@ -162,11 +131,8 @@ if ($var_recherche AND $flag_ob AND $flag_preg_replace AND !$flag_preserver AND
 //
 
 if (file_exists($chemin_cache)) {
-	@Header ("Last-Modified: ".gmdate("D, d M Y H:i:s T", $lastmodified));
-	include ($chemin_cache);
-	if ($flag_apc) {
-		apc_rm($chemin_cache);
-	}
+	if ($lastmodified) @Header ("Last-Modified: ".gmdate("D, d M Y H:i:s T", $lastmodified));
+	include_cache($chemin_cache);
 }
 
 
-- 
GitLab