| 4 | david | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | /***************************************************************************\
 | 
        
           |  |  | 4 |  *  SPIP, Systeme de publication pour l'internet                           *
 | 
        
           |  |  | 5 |  *                                                                         *
 | 
        
           |  |  | 6 |  *  Copyright (c) 2001-2005                                                *
 | 
        
           |  |  | 7 |  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
 | 
        
           |  |  | 8 |  *                                                                         *
 | 
        
           |  |  | 9 |  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
 | 
        
           |  |  | 10 |  *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
 | 
        
           |  |  | 11 | \***************************************************************************/
 | 
        
           |  |  | 12 |   | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 | // executer une seule fois
 | 
        
           |  |  | 15 | if (defined("_INC_URLS2")) return;
 | 
        
           |  |  | 16 | define("_INC_URLS2", "1");
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /*
 | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 | - Comment utiliser ce jeu d'URLs ?
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | Recopiez le fichier "htaccess.txt" du repertoire de base du site SPIP sous
 | 
        
           |  |  | 23 | le sous le nom ".htaccess" (attention a ne pas ecraser d'autres reglages
 | 
        
           |  |  | 24 | que vous pourriez avoir mis dans ce fichier) ; si votre site est en
 | 
        
           |  |  | 25 | "sous-repertoire", vous devrez aussi editer la ligne "RewriteBase" ce fichier.
 | 
        
           |  |  | 26 | Les URLs definies seront alors redirigees vers les fichiers de SPIP.
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | Definissez ensuite dans ecrire/mes_options.php3 :
 | 
        
           |  |  | 29 | 	type_urls = 'propres';
 | 
        
           |  |  | 30 | SPIP calculera alors ses liens sous la forme "Mon-titre-d-article".
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | Variante :
 | 
        
           |  |  | 33 | 	type_urls = 'propres2';
 | 
        
           |  |  | 34 | ajoutera '.html' aux adresses generees : "Mon-titre-d-article.html"
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | */
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | if (!defined('_terminaison_urls_propres'))
 | 
        
           |  |  | 39 | 	define ('_terminaison_urls_propres', '');
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 | function _generer_url_propre($type, $id_objet) {
 | 
        
           |  |  | 42 | 	$table = "spip_".table_objet($type);
 | 
        
           |  |  | 43 | 	$col_id = id_table_objet($type);
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | 	// Auteurs : on prend le nom
 | 
        
           |  |  | 46 | 	if ($type == 'auteur')
 | 
        
           |  |  | 47 | 		$champ_titre = 'nom AS titre';
 | 
        
           |  |  | 48 | 	else if ($type == 'site' OR $type=='syndic')
 | 
        
           |  |  | 49 | 		$champ_titre = 'nom_site AS titre';
 | 
        
           |  |  | 50 | 	else
 | 
        
           |  |  | 51 | 		$champ_titre = 'titre';
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 | 	// D'abord, essayer de recuperer l'URL existante si possible
 | 
        
           |  |  | 54 | 	$result = spip_query("SELECT url_propre, $champ_titre
 | 
        
           |  |  | 55 | 	FROM $table WHERE $col_id=$id_objet");
 | 
        
           |  |  | 56 | 	if (!($row = spip_fetch_array($result))) return ""; # objet inexistant
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | 	// Si l'on n'est pas dans spip_redirect.php3 sur un objet non publie
 | 
        
           |  |  | 59 | 	// ou en preview (astuce pour corriger un url-propre) + admin connecte
 | 
        
           |  |  | 60 | 	// Ne pas recalculer l'url-propre,
 | 
        
           |  |  | 61 | 	// sauf si :
 | 
        
           |  |  | 62 | 	// 1) il n'existe pas, ou
 | 
        
           |  |  | 63 | 	// 2) l'objet n'est pas 'publie' et on est admin connecte, ou
 | 
        
           |  |  | 64 | 	// 3) on le demande explicitement (preview) et on est admin connecte
 | 
        
           |  |  | 65 | 	if (defined('_SPIP_REDIRECT') AND
 | 
        
           |  |  | 66 | 	($GLOBALS['preview'] OR ($row['statut'] <> 'publie'))
 | 
        
           |  |  | 67 | 	AND $GLOBALS['auteur_session']['statut'] == '0minirezo')
 | 
        
           |  |  | 68 | 		$modif_url_propre = true;
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | 	if ($row['url_propre'] AND !$modif_url_propre)
 | 
        
           |  |  | 71 | 		return $row['url_propre'];
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 | 	// Sinon, creer l'URL
 | 
        
           |  |  | 74 | 	include_ecrire("inc_filtres.php3");
 | 
        
           |  |  | 75 | 	include_ecrire("inc_charsets.php3");
 | 
        
           |  |  | 76 | 	$url = translitteration(corriger_caracteres(
 | 
        
           |  |  | 77 | 		supprimer_tags(supprimer_numero(extraire_multi($row['titre'])))
 | 
        
           |  |  | 78 | 		));
 | 
        
           |  |  | 79 | 	$url = @preg_replace(',[[:punct:][:space:]]+,u', ' ', $url);
 | 
        
           |  |  | 80 | 	// S'il reste des caracteres non latins, utiliser l'id a la place
 | 
        
           |  |  | 81 | 	if (preg_match(",[^a-zA-Z0-9 ],", $url)) {
 | 
        
           |  |  | 82 | 		$url = $type.$id_objet;
 | 
        
           |  |  | 83 | 	}
 | 
        
           |  |  | 84 | 	else {
 | 
        
           |  |  | 85 | 		$mots = explode(' ', $url);
 | 
        
           |  |  | 86 | 		$url = '';
 | 
        
           |  |  | 87 | 		foreach ($mots as $mot) {
 | 
        
           |  |  | 88 | 			if (!$mot) continue;
 | 
        
           |  |  | 89 | 			$url2 = $url.'-'.$mot;
 | 
        
           |  |  | 90 | 			if (strlen($url2) > 35) {
 | 
        
           |  |  | 91 | 				break;
 | 
        
           |  |  | 92 | 			}
 | 
        
           |  |  | 93 | 			$url = $url2;
 | 
        
           |  |  | 94 | 		}
 | 
        
           |  |  | 95 | 		$url = substr($url, 1);
 | 
        
           |  |  | 96 | 		//echo "$url<br>";
 | 
        
           |  |  | 97 | 		if (strlen($url) < 2) $url = $type.$id_objet;
 | 
        
           |  |  | 98 | 	}
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | 	// Verifier les eventuels doublons et mettre a jour
 | 
        
           |  |  | 101 | 	$lock = "url $type $id_objet";
 | 
        
           |  |  | 102 | 	spip_get_lock($lock, 10);
 | 
        
           |  |  | 103 | 	$query = "SELECT $col_id FROM $table
 | 
        
           |  |  | 104 | 		WHERE url_propre='".addslashes($url)."' AND $col_id != $id_objet";
 | 
        
           |  |  | 105 | 	if (spip_num_rows(spip_query($query)) > 0) {
 | 
        
           |  |  | 106 | 		$url = $url.','.$id_objet;
 | 
        
           |  |  | 107 | 	}
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 | 	// Eviter de tamponner les URLs a l'ancienne (cas d'un article
 | 
        
           |  |  | 110 | 	// intitule "auteur2")
 | 
        
           |  |  | 111 | 	if ($type == 'article'
 | 
        
           |  |  | 112 | 	AND preg_match(',^(article|breve|rubrique|mot|auteur)[0-9]+$,', $url))
 | 
        
           |  |  | 113 | 		$url = $url.','.$id_objet;
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 | 	// Mettre a jour dans la base
 | 
        
           |  |  | 116 | 	$query = "UPDATE $table SET url_propre='".addslashes($url)."' WHERE $col_id=$id_objet";
 | 
        
           |  |  | 117 | 	spip_query($query);
 | 
        
           |  |  | 118 | 	spip_release_lock($lock);
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 | 	spip_log("Creation de l'url propre '$url' pour $col_id=$id_objet");
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | 	return $url;
 | 
        
           |  |  | 123 | }
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 | function generer_url_article($id_article) {
 | 
        
           |  |  | 126 | 	$url = _generer_url_propre('article', $id_article);
 | 
        
           |  |  | 127 | 	if ($url)
 | 
        
           |  |  | 128 | 		return $url . _terminaison_urls_propres;
 | 
        
           |  |  | 129 | 	else
 | 
        
           |  |  | 130 | 		return "article.php3?id_article=$id_article";
 | 
        
           |  |  | 131 | }
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 | function generer_url_rubrique($id_rubrique) {
 | 
        
           |  |  | 134 | 	$url = _generer_url_propre('rubrique', $id_rubrique);
 | 
        
           |  |  | 135 | 	if ($url)
 | 
        
           |  |  | 136 | 		return '-'.$url.'-'._terminaison_urls_propres;
 | 
        
           |  |  | 137 | 	else
 | 
        
           |  |  | 138 | 		return "rubrique.php3?id_rubrique=$id_rubrique";
 | 
        
           |  |  | 139 | }
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 | function generer_url_breve($id_breve) {
 | 
        
           |  |  | 142 | 	$url = _generer_url_propre('breve', $id_breve);
 | 
        
           |  |  | 143 | 	if ($url)
 | 
        
           |  |  | 144 | 		return '+'.$url.'+'._terminaison_urls_propres;
 | 
        
           |  |  | 145 | 	else
 | 
        
           |  |  | 146 | 		return "breve.php3?id_breve=$id_breve";
 | 
        
           |  |  | 147 | }
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 | function generer_url_forum($id_forum, $show_thread=false) {
 | 
        
           |  |  | 150 | 	include_ecrire('inc_forum.php3');
 | 
        
           |  |  | 151 | 	return generer_url_forum_dist($id_forum, $show_thread);
 | 
        
           |  |  | 152 | }
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 | function generer_url_mot($id_mot) {
 | 
        
           |  |  | 155 | 	$url = _generer_url_propre('mot', $id_mot);
 | 
        
           |  |  | 156 | 	if ($url)
 | 
        
           |  |  | 157 | 		return '+-'.$url.'-+'._terminaison_urls_propres;
 | 
        
           |  |  | 158 | 	else
 | 
        
           |  |  | 159 | 		return "mot.php3?id_mot=$id_mot";
 | 
        
           |  |  | 160 | }
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 | function generer_url_auteur($id_auteur) {
 | 
        
           |  |  | 163 | 	$url = _generer_url_propre('auteur', $id_auteur);
 | 
        
           |  |  | 164 | 	if ($url)
 | 
        
           |  |  | 165 | 		return '_'.$url.'_'._terminaison_urls_propres;
 | 
        
           |  |  | 166 | 	else
 | 
        
           |  |  | 167 | 		return "auteur.php3?id_auteur=$id_auteur";
 | 
        
           |  |  | 168 | }
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 | function generer_url_site($id_syndic) {
 | 
        
           |  |  | 171 | 	$url = _generer_url_propre('site', $id_syndic);
 | 
        
           |  |  | 172 | 	if ($url)
 | 
        
           |  |  | 173 | 		return '@'.$url.'@'._terminaison_urls_propres;
 | 
        
           |  |  | 174 | 	else
 | 
        
           |  |  | 175 | 		return "site.php3?id_syndic=$id_syndic";
 | 
        
           |  |  | 176 | }
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 | function generer_url_document($id_document) {
 | 
        
           |  |  | 179 | 	if (intval($id_document) <= 0)
 | 
        
           |  |  | 180 | 		return '';
 | 
        
           |  |  | 181 | 	if ((lire_meta("creer_htaccess")) == 'oui')
 | 
        
           |  |  | 182 | 		return "spip_acces_doc.php3?id_document=$id_document";
 | 
        
           |  |  | 183 | 	if ($row = @spip_fetch_array(spip_query("SELECT fichier FROM spip_documents WHERE id_document = $id_document")))
 | 
        
           |  |  | 184 | 		return ($row['fichier']);
 | 
        
           |  |  | 185 | 	return '';
 | 
        
           |  |  | 186 | }
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 | function recuperer_parametres_url($fond, $url) {
 | 
        
           |  |  | 189 | 	global $contexte;
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 | 	// Migration depuis anciennes URLs ?
 | 
        
           |  |  | 192 | 	if ($GLOBALS['_SERVER']['REQUEST_METHOD'] != 'POST' &&
 | 
        
           |  |  | 193 | preg_match(',(^|/)((article|breve|rubrique|mot|auteur|site)(\.php3?|[0-9]+\.html)([?&].*)?)$,', $url, $regs)) {
 | 
        
           |  |  | 194 | 		$type = $regs[3];
 | 
        
           |  |  | 195 | 		$id_objet = intval($GLOBALS[$id_table_objet = id_table_objet($type)]);
 | 
        
           |  |  | 196 | 		if ($id_objet) {
 | 
        
           |  |  | 197 | 			$func = "generer_url_$type";
 | 
        
           |  |  | 198 | 			$url_propre = $func($id_objet);
 | 
        
           |  |  | 199 | 			if ($url_propre
 | 
        
           |  |  | 200 | 			AND ($url_propre<>$regs[2])) {
 | 
        
           |  |  | 201 | 				http_status(301);
 | 
        
           |  |  | 202 | 				// recuperer les arguments supplementaires (&debut_xxx=...)
 | 
        
           |  |  | 203 | 				$reste = preg_replace('/^&/','?',
 | 
        
           |  |  | 204 | 					preg_replace("/[?&]$id_table_objet=$id_objet/",'',$regs[5]));
 | 
        
           |  |  | 205 | 				Header("Location: $url_propre$reste");
 | 
        
           |  |  | 206 | 				exit;
 | 
        
           |  |  | 207 | 			}
 | 
        
           |  |  | 208 | 		}
 | 
        
           |  |  | 209 | 		return;
 | 
        
           |  |  | 210 | 	}
 | 
        
           |  |  | 211 |   | 
        
           |  |  | 212 | 	$url_propre = $GLOBALS['_SERVER']['REDIRECT_url_propre'];
 | 
        
           |  |  | 213 | 	if (!$url_propre) $url_propre = $GLOBALS['HTTP_ENV_VARS']['url_propre'];
 | 
        
           |  |  | 214 | 	if (!$url_propre) {
 | 
        
           |  |  | 215 | 		$url = substr($url, strrpos($url, '/') + 1);
 | 
        
           |  |  | 216 | 		$url_propre = preg_replace(',[?].*,', '', $url);
 | 
        
           |  |  | 217 | 	}
 | 
        
           |  |  | 218 | 	if (!$url_propre) return;
 | 
        
           |  |  | 219 |   | 
        
           |  |  | 220 | 	// Compatilibite avec propres2
 | 
        
           |  |  | 221 | 	$url_propre = preg_replace(',\.html$,i', '', $url_propre);
 | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 | 	// Detecter les differents types d'objets demandes
 | 
        
           |  |  | 224 | 	if (preg_match(',^\+-(.*?)-?\+?$,', $url_propre, $regs)) {
 | 
        
           |  |  | 225 | 		$type = 'mot';
 | 
        
           |  |  | 226 | 		$url_propre = $regs[1];
 | 
        
           |  |  | 227 | 	}
 | 
        
           |  |  | 228 | 	else if (preg_match(',^-(.*?)-?$,', $url_propre, $regs)) {
 | 
        
           |  |  | 229 | 		$type = 'rubrique';
 | 
        
           |  |  | 230 | 		$url_propre = $regs[1];
 | 
        
           |  |  | 231 | 	}
 | 
        
           |  |  | 232 | 	else if (preg_match(',^\+(.*?)\+?$,', $url_propre, $regs)) {
 | 
        
           |  |  | 233 | 		$type = 'breve';
 | 
        
           |  |  | 234 | 		$url_propre = $regs[1];
 | 
        
           |  |  | 235 | 	}
 | 
        
           |  |  | 236 | 	else if (preg_match(',^_(.*?)_?$,', $url_propre, $regs)) {
 | 
        
           |  |  | 237 | 		$type = 'auteur';
 | 
        
           |  |  | 238 | 		$url_propre = $regs[1];
 | 
        
           |  |  | 239 | 	}
 | 
        
           |  |  | 240 | 	else if (preg_match(',^@(.*?)@?$,', $url_propre, $regs)) {
 | 
        
           |  |  | 241 | 		$type = 'syndic';
 | 
        
           |  |  | 242 | 		$url_propre = $regs[1];
 | 
        
           |  |  | 243 | 	}
 | 
        
           |  |  | 244 | 	else {
 | 
        
           |  |  | 245 | 		$type = 'article';
 | 
        
           |  |  | 246 | 		preg_match(',^(.*)$,', $url_propre, $regs);
 | 
        
           |  |  | 247 | 		$url_propre = $regs[1];
 | 
        
           |  |  | 248 | 	}
 | 
        
           |  |  | 249 |   | 
        
           |  |  | 250 | 	$table = "spip_".table_objet($type);
 | 
        
           |  |  | 251 | 	$col_id = id_table_objet($type);
 | 
        
           |  |  | 252 | 	$query = "SELECT $col_id FROM $table
 | 
        
           |  |  | 253 | 		WHERE url_propre='".addslashes($url_propre)."'";
 | 
        
           |  |  | 254 | 	$result = spip_query($query);
 | 
        
           |  |  | 255 | 	if ($row = spip_fetch_array($result)) {
 | 
        
           |  |  | 256 | 		$contexte[$col_id] = $row[$col_id];
 | 
        
           |  |  | 257 | 	}
 | 
        
           |  |  | 258 |   | 
        
           |  |  | 259 | 	return;
 | 
        
           |  |  | 260 | }
 | 
        
           |  |  | 261 |   | 
        
           |  |  | 262 | ?>
 |