Commit | Line | Data |
---|---|---|
c495c100 P |
1 | <?php |
2 | ||
3 | /***************************************************************************\ | |
4 | * SPIP, Systeme de publication pour l'internet * | |
5 | * * | |
6 | * Copyright (c) 2001-2007 * | |
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 | if (!defined("_ECRIRE_INC_VERSION")) return; // securiser | |
14 | if (!function_exists('generer_url_article')) { // si la place n'est pas prise | |
15 | ||
16 | /* | |
17 | ||
18 | - Comment utiliser ce jeu d'URLs ? | |
19 | ||
20 | Recopiez le fichier "htaccess.txt" du repertoire de base du site SPIP sous | |
21 | le sous le nom ".htaccess" (attention a ne pas ecraser d'autres reglages | |
22 | que vous pourriez avoir mis dans ce fichier) ; si votre site est en | |
23 | "sous-repertoire", vous devrez aussi editer la ligne "RewriteBase" ce fichier. | |
24 | Les URLs definies seront alors redirigees vers les fichiers de SPIP. | |
25 | ||
26 | Definissez ensuite dans ecrire/mes_options.php : | |
27 | < ?php $type_urls = 'propres'; ? > | |
28 | SPIP calculera alors ses liens sous la forme "Mon-titre-d-article". | |
29 | ||
30 | Variante 'propres2' : | |
31 | < ?php $type_urls = 'propres2'; ? > | |
32 | ajoutera '.html' aux adresses generees : "Mon-titre-d-article.html" | |
33 | ||
34 | Variante 'qs' (experimentale) : ce systeme fonctionne en "Query-String", | |
35 | c'est-a-dire sans utilisation de .htaccess ; les adresses sont de la forme | |
36 | "/?Mon-titre-d-article" | |
37 | < ?php $type_urls = 'qs'; ? > | |
38 | ||
39 | */ | |
40 | ||
41 | define ('_terminaison_urls_propres', ''); | |
42 | define ('_debut_urls_propres', ''); | |
43 | ||
44 | // http://doc.spip.org/@_generer_url_propre | |
45 | function _generer_url_propre($type, $id_objet) { | |
46 | $table = "spip_".table_objet($type); | |
47 | $col_id = id_table_objet($type); | |
48 | ||
49 | // Auteurs : on prend le nom | |
50 | if ($type == 'auteur') | |
51 | $champ_titre = 'nom AS titre'; | |
52 | else if ($type == 'site' OR $type=='syndic') | |
53 | $champ_titre = 'nom_site AS titre'; | |
54 | else | |
55 | $champ_titre = 'titre'; | |
56 | ||
57 | // Mots-cles : pas de champ statut | |
58 | if ($type == 'mot') | |
59 | $statut = "'publie' as statut"; | |
60 | else | |
61 | $statut = 'statut'; | |
62 | ||
63 | // D'abord, essayer de recuperer l'URL existante si possible | |
64 | $result = spip_query("SELECT url_propre, $statut, $champ_titre FROM $table WHERE $col_id=$id_objet"); | |
65 | if (!($row = spip_fetch_array($result))) return ""; # objet inexistant | |
66 | ||
67 | // Si l'on n'est pas dans spip_redirect.php3 sur un objet non publie | |
68 | // ou en preview (astuce pour corriger un url-propre) + admin connecte | |
69 | // Ne pas recalculer l'url-propre, | |
70 | // sauf si : | |
71 | // 1) il n'existe pas, ou | |
72 | // 2) l'objet n'est pas 'publie' et on est admin connecte, ou | |
73 | // 3) on le demande explicitement (preview) et on est admin connecte | |
74 | $modif_url_propre = false; | |
75 | if (function_exists('action_redirect_dist') AND | |
76 | ($GLOBALS['preview'] OR ($row['statut'] <> 'publie')) | |
77 | AND $GLOBALS['auteur_session']['statut'] == '0minirezo') | |
78 | $modif_url_propre = true; | |
79 | ||
80 | if ($row['url_propre'] AND !$modif_url_propre) | |
81 | return $row['url_propre']; | |
82 | ||
83 | // Sinon, creer l'URL | |
84 | include_spip('inc/filtres'); | |
85 | include_spip('inc/charsets'); | |
86 | $url = translitteration(corriger_caracteres( | |
87 | supprimer_tags(supprimer_numero(extraire_multi($row['titre']))) | |
88 | )); | |
89 | ||
90 | $url = @preg_replace(',[[:punct:][:space:]]+,u', ' ', $url); | |
91 | // S'il reste trop de caracteres non latins, ou trop peu | |
92 | // de caracteres latins, utiliser l'id a la place | |
93 | if (preg_match(",([^a-zA-Z0-9 ].*){5},", $url, $r) | |
94 | OR strlen($url)<3) { | |
95 | $url = $type.$id_objet; | |
96 | } | |
97 | else { | |
98 | $mots = preg_split(",[^a-zA-Z0-9]+,", $url); | |
99 | $url = ''; | |
100 | foreach ($mots as $mot) { | |
101 | if (!$mot) continue; | |
102 | $url2 = $url.'-'.$mot; | |
103 | if (strlen($url2) > 35) { | |
104 | break; | |
105 | } | |
106 | $url = $url2; | |
107 | } | |
108 | $url = substr($url, 1); | |
109 | //echo "$url<br>"; | |
110 | if (strlen($url) < 2) $url = $type.$id_objet; | |
111 | } | |
112 | ||
113 | // Verifier les eventuels doublons et mettre a jour | |
114 | $lock = "url $type $id_objet"; | |
115 | spip_get_lock($lock, 10); | |
116 | ||
117 | $n = spip_num_rows(spip_query("SELECT $col_id FROM $table WHERE url_propre=" . _q($url) . " AND $col_id != $id_objet LIMIT 1")); | |
118 | if ($n > 0) { | |
119 | $url = $url.','.$id_objet; | |
120 | } | |
121 | ||
122 | // Eviter de tamponner les URLs a l'ancienne (cas d'un article | |
123 | // intitule "auteur2") | |
124 | if ($type == 'article' | |
125 | AND preg_match(',^(article|breve|rubrique|mot|auteur)[0-9]+$,', $url)) | |
126 | $url = $url.','.$id_objet; | |
127 | ||
128 | // Mettre a jour dans la base | |
129 | spip_query("UPDATE $table SET url_propre=" . _q($url) . " WHERE $col_id=$id_objet"); | |
130 | ||
131 | spip_release_lock($lock); | |
132 | ||
133 | spip_log("Creation de l'url propre '$url' pour $col_id=$id_objet"); | |
134 | ||
135 | return $url; | |
136 | } | |
137 | ||
138 | // http://doc.spip.org/@generer_url_article | |
139 | function generer_url_article($id_article) { | |
140 | $url = _generer_url_propre('article', $id_article); | |
141 | if ($url) | |
142 | return _debut_urls_propres . $url . _terminaison_urls_propres; | |
143 | else | |
144 | return get_spip_script('./')."?page=article&id_article=$id_article"; | |
145 | } | |
146 | ||
147 | // http://doc.spip.org/@generer_url_rubrique | |
148 | function generer_url_rubrique($id_rubrique) { | |
149 | $url = _generer_url_propre('rubrique', $id_rubrique); | |
150 | if ($url) | |
151 | return _debut_urls_propres . '-'.$url.'-'._terminaison_urls_propres; | |
152 | else | |
153 | return get_spip_script('./')."?page=rubrique&id_rubrique=$id_rubrique"; | |
154 | } | |
155 | ||
156 | // http://doc.spip.org/@generer_url_breve | |
157 | function generer_url_breve($id_breve) { | |
158 | $url = _generer_url_propre('breve', $id_breve); | |
159 | if ($url) | |
160 | return _debut_urls_propres . '+'.$url.'+'._terminaison_urls_propres; | |
161 | else | |
162 | return get_spip_script('./')."?page=breve&id_breve=$id_breve"; | |
163 | } | |
164 | ||
165 | // http://doc.spip.org/@generer_url_forum | |
166 | function generer_url_forum($id_forum, $show_thread=false) { | |
167 | include_spip('inc/forum'); | |
168 | return generer_url_forum_dist($id_forum, $show_thread); | |
169 | } | |
170 | ||
171 | // http://doc.spip.org/@generer_url_mot | |
172 | function generer_url_mot($id_mot) { | |
173 | $url = _generer_url_propre('mot', $id_mot); | |
174 | if ($url) | |
175 | return _debut_urls_propres . '+-'.$url.'-+'._terminaison_urls_propres; | |
176 | else | |
177 | return get_spip_script('./')."?page=mot&id_mot=$id_mot"; | |
178 | } | |
179 | ||
180 | // http://doc.spip.org/@generer_url_auteur | |
181 | function generer_url_auteur($id_auteur) { | |
182 | $url = _generer_url_propre('auteur', $id_auteur); | |
183 | if ($url) | |
184 | return _debut_urls_propres . '_'.$url.'_'._terminaison_urls_propres; | |
185 | else | |
186 | return get_spip_script('./')."?page=auteur&id_auteur=$id_auteur"; | |
187 | } | |
188 | ||
189 | // http://doc.spip.org/@generer_url_site | |
190 | function generer_url_site($id_syndic) { | |
191 | $url = _generer_url_propre('site', $id_syndic); | |
192 | if ($url) | |
193 | return _debut_urls_propres . '@'.$url.'@'._terminaison_urls_propres; | |
194 | else | |
195 | return get_spip_script('./')."?page=site&id_syndic=$id_syndic"; | |
196 | } | |
197 | ||
198 | // http://doc.spip.org/@generer_url_document | |
199 | function generer_url_document($id_document) { | |
200 | if (($id_document = intval($id_document)) <= 0) | |
201 | return ''; | |
202 | if (($GLOBALS['meta']["creer_htaccess"]) == 'oui') | |
203 | return generer_url_action('autoriser',"arg=$id_document", true); | |
204 | $row = @spip_fetch_array(spip_query("SELECT fichier FROM spip_documents WHERE id_document = $id_document")); | |
205 | if ($row) return ($row['fichier']); | |
206 | return ''; | |
207 | } | |
208 | ||
209 | // http://doc.spip.org/@recuperer_parametres_url | |
210 | function recuperer_parametres_url(&$fond, $url) { | |
211 | global $contexte; | |
212 | $id_objet = 0; | |
213 | ||
214 | // Migration depuis anciennes URLs ? | |
215 | if ($_SERVER['REQUEST_METHOD'] != 'POST') { | |
216 | if (preg_match( | |
217 | ',(^|/)(article|breve|rubrique|mot|auteur|site)(\.php3?|[0-9]+\.html)' | |
218 | .'([?&].*)?$,', $url, $regs) | |
219 | ) { | |
220 | $type = $regs[3]; | |
221 | $id_table_objet = id_table_objet($type); | |
222 | $id_objet = intval($GLOBALS[$id_table_objet]); | |
223 | } | |
224 | ||
225 | /* Compatibilite urls-page */ | |
226 | else if (preg_match( | |
227 | ',[?/&](article|breve|rubrique|mot|auteur|site)[=]?([0-9]+),', | |
228 | $url, $regs)) { | |
229 | $type = $regs[1]; | |
230 | $id_objet = $regs[2]; | |
231 | } | |
232 | } | |
233 | if ($id_objet) { | |
234 | $func = "generer_url_$type"; | |
235 | $url_propre = $func($id_objet); | |
236 | if (strlen($url_propre) | |
237 | AND !strstr($url,$url_propre)) { | |
238 | include_spip('inc/headers'); | |
239 | http_status(301); | |
240 | // recuperer les arguments supplementaires (&debut_xxx=...) | |
241 | $reste = preg_replace('/^&/','?', | |
242 | preg_replace("/[?&]$id_table_objet=$id_objet/",'',$regs[5])); | |
243 | $reste .= preg_replace('/&/','?', | |
244 | preg_replace('/[?&]'.$type.'[=]?'.$id_objet.'/','', | |
245 | substr($url, strpos($url,'?')))); | |
246 | redirige_par_entete("$url_propre$reste"); | |
247 | } | |
248 | } | |
249 | /* Fin compatibilite anciennes urls */ | |
250 | ||
251 | ||
252 | // Chercher les valeurs d'environnement qui indiquent l'url-propre | |
253 | if (isset($_SERVER['REDIRECT_url_propre'])) | |
254 | $url_propre = $_SERVER['REDIRECT_url_propre']; | |
255 | elseif (isset($GLOBALS['HTTP_ENV_VARS']['url_propre'])) | |
256 | $url_propre = $GLOBALS['HTTP_ENV_VARS']['url_propre']; | |
257 | else { | |
258 | $url = substr($url, strrpos($url, '/') + 1); | |
259 | $url_propre = preg_replace(',[?].*,', '', $url); | |
260 | } | |
261 | // Mode Query-String ? | |
262 | $adapter_le_fond = false; | |
263 | if (!$url_propre | |
264 | AND preg_match(',([?])([^=/?&]+)(&.*)?$,', $GLOBALS['REQUEST_URI'], $r)) { | |
265 | $url_propre = $r[2]; | |
266 | $adapter_le_fond = true; | |
267 | } | |
268 | if (!$url_propre) return; | |
269 | ||
270 | // Compatilibite avec propres2 | |
271 | $url_propre = preg_replace(',\.html$,i', '', $url_propre); | |
272 | ||
273 | // Detecter les differents types d'objets demandes | |
274 | if (preg_match(',^\+-(.*?)-?\+?$,', $url_propre, $regs)) { | |
275 | $type = 'mot'; | |
276 | $url_propre = $regs[1]; | |
277 | } | |
278 | else if (preg_match(',^-(.*?)-?$,', $url_propre, $regs)) { | |
279 | $type = 'rubrique'; | |
280 | $url_propre = $regs[1]; | |
281 | } | |
282 | else if (preg_match(',^\+(.*?)\+?$,', $url_propre, $regs)) { | |
283 | $type = 'breve'; | |
284 | $url_propre = $regs[1]; | |
285 | } | |
286 | else if (preg_match(',^_(.*?)_?$,', $url_propre, $regs)) { | |
287 | $type = 'auteur'; | |
288 | $url_propre = $regs[1]; | |
289 | } | |
290 | else if (preg_match(',^@(.*?)@?$,', $url_propre, $regs)) { | |
291 | $type = 'syndic'; | |
292 | $url_propre = $regs[1]; | |
293 | } | |
294 | else { | |
295 | $type = 'article'; | |
296 | preg_match(',^(.*)$,', $url_propre, $regs); | |
297 | $url_propre = $regs[1]; | |
298 | } | |
299 | ||
300 | $table = "spip_".table_objet($type); | |
301 | $col_id = id_table_objet($type); | |
302 | $result = spip_query("SELECT $col_id FROM $table WHERE url_propre=" . _q($url_propre)); | |
303 | ||
304 | if ($row = spip_fetch_array($result)) { | |
305 | $contexte[$col_id] = $row[$col_id]; | |
306 | } | |
307 | ||
308 | // En mode Query-String, on fixe ici le $fond utilise | |
309 | if ($adapter_le_fond) { | |
310 | $fond = $type; | |
311 | if ($type == 'syndic') $fond = 'site'; | |
312 | } | |
313 | ||
314 | return; | |
315 | } | |
316 | } | |
317 | ||
318 | ?> |