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