[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / public / evaluer_page.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
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 /**
16 * Evaluer la page produite par un squelette
17 *
18 * Évalue une page pour la transformer en texte statique
19 * Elle peut contenir un < ?xml a securiser avant eval
20 * ou du php d'origine inconnue
21 *
22 * Attention cette partie eval() doit impérativement
23 * être déclenchée dans l'espace des globales (donc pas
24 * dans une fonction).
25 *
26 * @param array $page
27 * @return bool
28 */
29 $res = true;
30
31 // Cas d'une page contenant du PHP :
32 if ($page['process_ins'] != 'html') {
33
34 /**
35 * Teste si on a déjà évalué du PHP
36 *
37 * Inclure inc/lang la première fois pour définir spip_lang
38 * si ça n'a pas encore été fait.
39 */
40 if (!defined('_EVALUER_PAGE_PHP')) {
41 define('_EVALUER_PAGE_PHP', true);
42 include_spip('inc/lang');
43 }
44
45 // restaurer l'etat des notes avant calcul
46 if (isset($page['notes'])
47 AND $page['notes']
48 AND $notes = charger_fonction("notes","inc",true)){
49 $notes($page['notes'],'restaurer_etat');
50 }
51 ob_start();
52 if (strpos($page['texte'],'?xml')!==false)
53 $page['texte'] = str_replace('<'.'?xml', "<\1?xml", $page['texte']);
54
55 try {
56 $res = eval('?' . '>' . $page['texte']);
57 // error catching 5.2<=PHP<7
58 if ($res === false
59 and function_exists('error_get_last')
60 and ($erreur = error_get_last()) ) {
61 $code = $page['texte'];
62 $GLOBALS['numero_ligne_php'] = 1;
63 if (!function_exists('numerote_ligne_php')){
64 function numerote_ligne_php($match){
65 $GLOBALS['numero_ligne_php']++;
66 return "\n/*".str_pad($GLOBALS['numero_ligne_php'],3,"0",STR_PAD_LEFT)."*/";
67 }
68 }
69 $code = "/*001*/".preg_replace_callback(",\n,","numerote_ligne_php",$code);
70 $code = trim(highlight_string($code,true));
71 erreur_squelette("L".$erreur['line'].": ".$erreur['message']."<br />".$code,array($page['source'],'',$erreur['file'],'',$GLOBALS['spip_lang']));
72 $page['texte'] = "<!-- Erreur -->";
73 }
74 else {
75 $page['texte'] = ob_get_contents();
76 }
77 }
78 catch (Exception $e){
79 $code = $page['texte'];
80 $GLOBALS['numero_ligne_php'] = 1;
81 if (!function_exists('numerote_ligne_php')){
82 function numerote_ligne_php($match){
83 $GLOBALS['numero_ligne_php']++;
84 return "\n/*".str_pad($GLOBALS['numero_ligne_php'],3,"0",STR_PAD_LEFT)."*/";
85 }
86 }
87 $code = "/*001*/".preg_replace_callback(",\n,","numerote_ligne_php",$code);
88 $code = trim(highlight_string($code,true));
89 erreur_squelette("L".$e->getLine().": ".$e->getMessage()."<br />".$code,array($page['source'],'',$e->getFile(),'',$GLOBALS['spip_lang']));
90 $page['texte'] = "<!-- Erreur -->";
91 }
92 ob_end_clean();
93
94 $page['process_ins'] = 'html';
95
96 if (strpos($page['texte'],'?xml')!==false)
97 $page['texte'] = str_replace("<\1?xml", '<'.'?xml', $page['texte']);
98 }
99
100 page_base_href($page['texte']);