[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / exec / aide_index.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2010 *
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;
14
15 include_spip('inc/headers');
16 include_spip('inc/texte');
17 include_spip('inc/layer');
18
19 // Les sections d'un fichier aide sont reperees ainsi:
20
21 define('_SECTIONS_AIDE', ',<h([12])(?:\s+class="spip")?'. '>([^/]+?)(?:/(.+?))?</h\1>,ism');
22
23 // Les appels a soi-meme (notamment les images)
24 // doivent etre en relatif pour pouvoir creer un cache local
25
26 function generer_url_aide($args)
27 {
28 return generer_url_ecrire('aide_index', $args, false, true);
29 }
30
31 function help_fichier_contenu ($lang_aide, $path, $help_server) {
32 $contenu = array();
33 include_spip('inc/distant');
34 foreach ($help_server as $k => $server) {
35 // Remplacer les liens aux images par leur gestionnaire de cache
36 $url = "$server/$path";
37 $page = help_replace_img(recuperer_page($url),$k);
38 // les liens internes ne doivent pas etre deguises en externes
39 $url = parse_url($url);
40 $re = '@(<a\b[^>]*\s+href=["\'])' .
41 '(?:' . $url['scheme'] . '://' . $url['host'] . ')?' .
42 $url['path'] . '([^"\']*)@ims';
43 $page = preg_replace($re,'\\1\\2', $page);
44
45 preg_match_all(_SECTIONS_AIDE, $page, $sections, PREG_SET_ORDER);
46 // Fusionner les aides ayant meme nom de section
47 $vus = array();
48 foreach ($sections as $section) {
49 list($tout,$prof, $sujet,) = $section;
50 if (in_array($sujet, $vus)) continue;
51 $corps = help_section($sujet, $page, $prof);
52 foreach ($contenu as $k => $s) {
53 if ($sujet == $k) {
54 // Section deja vue qu'il faut completer
55 // Si le complement a des sous-sections,
56 // ne pas en tenir compte quand on les rencontrera
57 // lors des prochains passages dans la boucle
58 if (preg_match_all(_SECTIONS_AIDE, $corps, $m, PREG_PATTERN_ORDER)) {
59 if ($m) {$vus = array_merge($vus, $m[2]);}
60 $contenu[$k] .= $corps;
61 $corps = '';
62 break;
63 } else spip_log("aide $server $section incorrecte");
64 }
65 }
66 // Si totalement nouveau, inserer le titre
67 // mais pas le corps s'il contient des sous-sections:
68 // elles vont venir dans les passages suivants
69 if ($corps) {
70 $corps = help_section($sujet, $page);
71 $contenu[$sujet] = $tout . "\n" . $corps;
72 }
73 }
74 }
75
76 // Renvoyer les liens vraiment externes dans une autre fenetre
77 $contenu = preg_replace('@<a href="(http://[^"]+)"([^>]*)>@',
78 '<a href="\\1"\\2 target="_blank">',
79 join('',$contenu));
80
81
82 if (strlen($contenu) <= 75) return array(false, false);
83
84 // Correction typo dans la langue demandee
85 changer_typo($lang_aide);
86 return '<body>' . justifier("<div>$contenu</div>") . '</body>';
87 }
88
89 // http://doc.spip.org/@help_lastmodified
90 function help_lastmodified($lastmodified)
91 {
92 $gmoddate = gmdate("D, d M Y H:i:s", $lastmodified);
93 header("Last-Modified: ".$gmoddate." GMT");
94 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
95 # MSoft IIS is dumb
96 AND !preg_match(',IIS/,', $_SERVER['SERVER_SOFTWARE'])) {
97
98 $ims = preg_replace('/;.*/', '',
99 $_SERVER['HTTP_IF_MODIFIED_SINCE']);
100 $ims = trim(str_replace('GMT', '', $ims));
101 if ($ims == $gmoddate) {
102 http_status(304);
103 return true;
104 }
105 }
106 return false;
107 }
108
109 // Les aides non mises a jour ont un vieux Path a remplacer
110 // (mais ce serait bien de le faire en SQL une bonne fois)
111 define('_REPLACE_IMG_PACK', "@(<img([^<>]* +)?\s*src=['\"])img_pack\/@ims");
112
113 // Remplacer les URL des images par l'URL du gestionnaire de cache local
114
115 function help_replace_img($contenu, $server=0)
116 {
117 $html = "";
118 $re = "@(<img([^<>]* +)?\s*src=['\"])((AIDE|IMG|local)/([-_a-zA-Z0-9]*/?)([^'\"<>]*))@imsS";
119 while (preg_match($re, $contenu, $r)) {
120 $p = strpos($contenu, $r[0]);
121 $i = $server . ':' . str_replace('/', '-', $r[3]);
122 $h = generer_url_aide("img=" . $i);
123 $html .= substr($contenu, 0, $p) . $r[1] . $h;
124 $contenu = substr($contenu, $p + strlen($r[0]));
125 }
126 $html .= $contenu;
127
128 // traiter les vieilles doc
129 return preg_replace(_REPLACE_IMG_PACK,"\\1"._DIR_IMG_PACK, $html);
130 }
131
132 // un bout de squelette qu'il serait bon d'evacuer un jour.
133
134 define('_HELP_PANNEAU', "<img src='" .
135 chemin_image('logo-spip.gif') .
136 "' alt='SPIP' style='width: 267px; height: 170px; border: 0px' />
137 <br />
138 <div align='center' style='font-variant: small-caps;'>
139 Syst&egrave;me de publication pour l'Internet
140 </div></div>
141 <div style='position:absolute; bottom: 10px; right:20px; font-size: 12px; '>");
142
143 // Autre squelette qui ne s'avoue pas comme tel
144
145 // http://doc.spip.org/@help_body
146 function help_body($aide) {
147
148 if (!$aide) {
149 $c = _T('info_copyright_doc',
150 array('spipnet' => $GLOBALS['home_server']
151 . '/' . $GLOBALS['spip_lang']
152 . '_'));
153 return "<div align='center'>" .
154 _HELP_PANNEAU .
155 preg_replace(",<a ,i", "<a class='target_blank' ", $c) .
156 '</div>';
157 } elseif ($aide == 'spip') {
158 return "<table border='0' width='100%' height='60%'>
159 <tr style='width: 100%' height='60%'>
160 <td style='width: 100%' height='60%' align='center' valign='middle'>
161 <img src='" . generer_url_aide('img=AIDE--logo-spip.gif').
162 "' alt='SPIP' style='width: 300px; height: 170px; border: 0px;' />
163 </td></tr></table>";
164 } return '';
165 }
166
167
168 // Extraire la seule section demandee,
169 // qui commence par son nom entouree d'une balise h2
170 // et se termine par la prochaine balise h2 ou h1 ou le /body final.
171
172 function help_section($aide, $contenu, $prof=2)
173 {
174 $maxprof = ($prof >=2) ? "12" : "1";
175 $r = "@<h$prof" . '(?: class="spip")?' . '>\s*' . $aide
176 ."\s*(?:/.+?)?</h$prof>(.*?)<(?:(?:h[$maxprof])|/body)@ism";
177
178 if (preg_match($r, $contenu, $m))
179 return $m[1];
180 return '';
181 }
182
183
184
185 // Affichage du menu de gauche avec analyse de la section demandee
186 // afin d'ouvrir le sous-menu correspondant a l'affichage a droite
187 // http://doc.spip.org/@help_menu_rubrique
188 function help_menu_rubrique($aide, $contenu)
189 {
190 global $spip_lang;
191
192 $afficher = false;
193 $ligne = $numrub = 0;
194 $texte = $res = '';
195 preg_match_all(_SECTIONS_AIDE, $contenu, $sections, PREG_SET_ORDER);
196 foreach ($sections as $section) {
197 list(,$prof, $sujet, $bloc) = $section;
198 if ($prof == '1') {
199 if ($afficher && $texte)
200 $res .= block_parfois_visible("block$numrub", "<div class='rubrique'>$titre</div>", "\n$texte",'', $ouvrir);
201 $afficher = $bloc ? ($bloc == 'redac') : true;
202 $texte = '';
203 if ($afficher) {
204 $numrub++;
205 $ouvrir = 0;
206 $titre = $sujet;
207 }
208 } else {
209 ++$ligne;
210 $id = "ligne$ligne";
211
212 if ($aide == $sujet) {
213 $ouvrir = 1;
214 $class = "article-actif";
215 $texte .= http_script("curr_article = '$id';");
216 } else $class = "article-inactif";
217
218 $h = generer_url_aide("aide=$sujet&frame=body&var_lang=$spip_lang");
219 $texte .= "<a class='$class' target='droite' id='$id' href='$h' onclick=\"activer_article('$id');return true;\">"
220 . $bloc
221 . "</a><br style='clear:both;' />\n";
222 }
223 }
224 if ($afficher && $texte)
225 $res .= block_parfois_visible("block$numrub", "<div class='rubrique'>$titre</div>", "\n$texte",'', $ouvrir);
226 return $res;
227 }
228
229 function help_frame_menu($titre, $contenu, $lang)
230 {
231 global $spip_lang_rtl;
232
233 return "<head>\n<title>" .$titre ."</title>\n" .
234 '<link rel="stylesheet" type="text/css" href="' .
235 generer_url_public('aide_menu', "ltr=". $GLOBALS['spip_lang_left']) .
236 "\"/>\n" .
237 http_script('', 'jquery.js') .
238 "\n" .
239 $GLOBALS['browser_layer'] .
240 http_script('var curr_article;
241 function activer_article(id) {
242 if (curr_article)
243 jQuery("#"+curr_article).removeClass("article-actif").addClass("article-inactif");
244 if (id) {
245 jQuery("#"+id).removeClass("article-inactif").addClass("article-actif");
246 curr_article = id;
247 }
248 }
249 ') . '
250 </head>
251 <body bgcolor="#FFFFFF" text="#000000" link="#E86519" vlink="#6E003A" alink="#FF9900" topmargin="5" leftmargin="5" marginwidth="5" marginheight="5"' .
252 ($spip_lang_rtl ? " dir='rtl'" : '') .
253 " lang='$lang'" . '>' .
254 $contenu .
255 '</body>';
256 }
257
258 function help_frame_body($titre, $aide, $html, $lang_aide='')
259 {
260 global $spip_lang_rtl;
261 $dir = $spip_lang_rtl ? " dir='rtl'" : '';
262
263 return "<head>\n<title>$titre</title>\n".
264 '<link rel="stylesheet" type="text/css" href="'.
265 url_absolue(find_in_path('aide_body.css')).
266 "\"/>\n".
267 "</head>\n".
268 '<body bgcolor="#FFFFFF" text="#000000" topmargin="24" leftmargin="24" marginwidth="24" marginheight="24"' .
269 $dir .
270 " lang='$lang'>".
271 help_body($aide) .
272 ($aide ? $html : '').
273 '</body>';
274 }
275
276 function help_frame_frame($titre, $aide, $lang)
277 {
278 global $spip_lang_rtl;
279 $menu = "<frame src='" . generer_url_aide("aide=$aide&var_lang=$lang&frame=menu") . "' name=\"gauche\" id=\"gauche\" scrolling=\"auto\" />\n";
280 $body = "<frame src='" . generer_url_aide("aide=$aide&var_lang=$lang&frame=body") . "' name=\"droite\" id=\"droite\" scrolling=\"auto\" />\n";
281
282 $seq = $spip_lang_rtl ? "$body$menu" : "$menu$body";
283 $dim = $spip_lang_rtl ? '*,160' : '160,*';
284 return "<head>\n<title>$titre</title>\n</head>\n<frameset cols='$dim' border='0' frameborder='0' framespacing='0'>$seq</frameset>";
285 }
286
287 // http://doc.spip.org/@help_img_cache
288 function help_img_cache($img, $ext)
289 {
290 header("Content-Type: image/$ext");
291 header("Expires: ".gmdate("D, d M Y H:i:s", time()+24*3600) .' GMT');
292 readfile($img);
293 }
294
295 // Regexp reperant le travail fait par help_replace_img
296 define('_HELP_PLACE_IMG',',^(\d+:)?(([^-.]*)-([^-.]*)-([^\.]*\.(gif|jpg|png)))$,');
297
298 // Distinguer la demande d'une image et la demande d'un texte.
299 // Si c'est une URL d'image deguisee, on la cherche dans le cache ou on l'y met.
300 // Voir les differentes localisations possibles dans help_replace_img
301 //
302 // http://doc.spip.org/@exec_aide_index_dist
303 function exec_aide_index_dist()
304 {
305 global $help_server;
306 if (!is_array($help_server)) $help_server = array($help_server);
307 if (!preg_match(_HELP_PLACE_IMG, _request('img'), $r)) {
308 aide_index_frame(_request('var_lang_r'),
309 _request('lang_r'),
310 _request('frame'),
311 strtr(_request('aide'),'<>"\'', '____'),
312 $help_server);
313 } else {
314 list (,$server, $cache, $rep, $lang, $file, $ext) = $r;
315 if ($rep=="IMG" AND $lang=="cache"
316 AND @file_exists($img = _DIR_VAR.'cache-TeX/'.preg_replace(',^TeX-,', '', $file))) {
317 help_img_cache($img, $ext);
318 } else if (@file_exists($img = _DIR_AIDE . $cache)) {
319 help_img_cache($img, $ext);
320 } else if (@file_exists($img = _DIR_RACINE . 'AIDE/aide-'.$cache)) {
321 help_img_cache($img, $ext);
322 } else {
323 $server = intval(substr($server, 0, -1));
324 if ($server = $help_server[$server]) {
325 include_spip('inc/distant');
326 sous_repertoire(_DIR_AIDE,'','',true);
327 $img = "$server/$rep/$lang/$file";
328 $contenu = recuperer_page($img);
329 if ($contenu) {
330 ecrire_fichier (_DIR_AIDE . $cache, $contenu);
331 // Bug de certains OS:
332 // le contenu est incompris au premier envoi
333 // Donc ne pas mettre d'Expire
334 header("Content-Type: image/$ext");
335 echo $contenu;
336 } else redirige_par_entete($img);
337 } else redirige_par_entete(generer_url_public('404'));
338 }
339 }
340 }
341
342 // Determiner la langue L, et en deduire le Path du fichier d'aide.
343 // Sur le site www.spip.net/, ca donne l'URL www.spip.net/L-aide.html
344 // reecrit par le htacces suivant:
345 // http://zone.spip.org/trac/spip-zone/browser/_galaxie_/www.spip.net/squelettes/htaccess.txt
346
347 function aide_index_frame($var_lang_r, $lang_r, $frame, $aide, $help_server)
348 {
349 global $spip_lang;
350
351 if ($var_lang_r)
352 changer_langue($lang = $var_lang_r);
353 if ($lang_r)
354 # pour le cas ou on a fait appel au menu de changement de langue
355 # (aide absente dans la langue x)
356 changer_langue($lang = $lang_r);
357 else $lang = $spip_lang;
358
359 // L'aide correspondant a la langue demandee est dans un cache
360 // reposant sur la date du fichier indiquant la version de SPIP
361 // (approximatif, mais c'est deja qqch)
362
363 $path = $spip_lang . "-aide.html";
364 $md5 = md5(serialize($help_server));
365 $fichier = _DIR_AIDE . substr($md5,0,16) . "-" . $path;
366 $lastm = is_readable($fichier) ? filemtime($fichier) : 0;
367 $lastversion = @filemtime(_DIR_RESTREINT . 'inc_version.php');
368 if (!($lastm AND ($lastm >= $lastversion))) {
369 $contenu = help_fichier_contenu($spip_lang, $path, $help_server);
370 // mettre en cache (tant pis si echec)
371 sous_repertoire(_DIR_AIDE,'','',true);
372 if ($contenu) ecrire_fichier ($fichier, $contenu);
373 $lastm = time();
374 }
375 $titre = _T('info_aide_en_ligne');
376 if (!$frame) {
377 echo _DOCTYPE_AIDE, html_lang_attributes();
378 echo help_frame_frame($titre, $aide, $lang);
379 echo "\n</html>";
380 } else {
381 header("Content-Type: text/html; charset=utf-8");
382 if (!isset($contenu)) {
383 lire_fichier($fichier, $contenu);
384 }
385 if (!$contenu) {
386 include_spip('inc/minipres');
387 echo minipres(_T('forum_titre_erreur'),
388 "<div><a href='" .
389 $GLOBALS['home_server'] .
390 "'>" .
391 $help_server[0] .
392 "</a> $aide&nbsp;: ".
393 _T('aide_non_disponible').
394 "</div><br /><div align='right'>".
395 menu_langues('var_lang_ecrire').
396 "</div>");
397 // Envoie le not-modified-since si possible, sinon envoie tout
398 } elseif (!help_lastmodified($lastm)) {
399 echo _DOCTYPE_AIDE, html_lang_attributes();
400 if ($frame === 'menu') {
401 $contenu = help_menu_rubrique($aide, $contenu);
402 echo help_frame_menu($titre, $contenu, $lang);
403 } else {
404 if ($aide) {
405 $contenu = help_section($aide, $contenu);
406 if (!$contenu) spip_log("aide inconnue $aide dans " . substr($contenu, 0, 150));
407 }
408 echo help_frame_body($titre, $aide, $contenu, $lang);
409 }
410 echo "\n</html>";
411 }
412 }
413 }
414
415 ?>