6f2b900629f610b6ba3d7cc31b2b5b800cc12cb0
[lhc/web/www.git] / www / ecrire / plugins / verifie_conformite.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
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 include_spip('inc/xml');
18 include_spip('inc/plugin');
19
20 // http://code.spip.net/@plugin_verifie_conformite
21 function plugins_verifie_conformite_dist($plug, &$arbre, $dir_plugins = _DIR_PLUGINS) {
22 static $etats = array('dev', 'experimental', 'test', 'stable');
23
24 $matches = array();
25 $silence = false;
26 $p = null;
27 // chercher la declaration <plugin spip='...'> a prendre pour cette version de SPIP
28 if ($n = spip_xml_match_nodes(",^plugin(\s|$),", $arbre, $matches)) {
29 // version de SPIP
30 $vspip = $GLOBALS['spip_version_branche'];
31 foreach ($matches as $tag => $sous) {
32 list($tagname, $atts) = spip_xml_decompose_tag($tag);
33 if ($tagname == 'plugin' and is_array($sous)) {
34 // On rajoute la condition sur $n :
35 // -- en effet si $n==1 on a pas plus a choisir la balise que l'on ait
36 // un attribut spip ou pas. Cela permet de traiter tous les cas mono-balise
37 // de la meme facon.
38 if (!isset($atts['spip'])
39 or $n == 1
40 or plugin_version_compatible($atts['spip'], $vspip, 'spip')
41 ) {
42 // on prend la derniere declaration avec ce nom
43 $p = end($sous);
44 $compat_spip = isset($atts['spip']) ? $atts['spip'] : '';
45 }
46 }
47 }
48 }
49 if (is_null($p)) {
50 $arbre = array('erreur' => array(_T('erreur_plugin_tag_plugin_absent') . " : $plug"));
51 $silence = true;
52 } else {
53 $arbre = $p;
54 }
55 if (!is_array($arbre)) {
56 $arbre = array();
57 }
58 // verification de la conformite du plugin avec quelques
59 // precautions elementaires
60 if (!isset($arbre['nom'])) {
61 if (!$silence) {
62 $arbre['erreur'][] = _T('erreur_plugin_nom_manquant');
63 }
64 $arbre['nom'] = array("");
65 }
66 if (!isset($arbre['version'])) {
67 if (!$silence) {
68 $arbre['erreur'][] = _T('erreur_plugin_version_manquant');
69 }
70 $arbre['version'] = array("");
71 }
72 if (!isset($arbre['prefix'])) {
73 if (!$silence) {
74 $arbre['erreur'][] = _T('erreur_plugin_prefix_manquant');
75 }
76 $arbre['prefix'] = array("");
77 } else {
78 $prefix = trim(end($arbre['prefix']));
79 if (strtoupper($prefix) == 'SPIP' and $plug != "./") {
80 $arbre['erreur'][] = _T('erreur_plugin_prefix_interdit');
81 }
82 if (isset($arbre['etat'])) {
83 $etat = trim(end($arbre['etat']));
84 if (!in_array($etat, $etats)) {
85 $arbre['erreur'][] = _T('erreur_plugin_etat_inconnu') . " : '$etat'";
86 }
87 }
88 if (isset($arbre['options'])) {
89 foreach ($arbre['options'] as $optfile) {
90 $optfile = trim($optfile);
91 if (!@is_readable($dir_plugins . "$plug/$optfile")) {
92 if (!$silence) {
93 $arbre['erreur'][] = _T('erreur_plugin_fichier_absent') . " : $optfile";
94 }
95 }
96 }
97 }
98 if (isset($arbre['fonctions'])) {
99 foreach ($arbre['fonctions'] as $optfile) {
100 $optfile = trim($optfile);
101 if (!@is_readable($dir_plugins . "$plug/$optfile")) {
102 if (!$silence) {
103 $arbre['erreur'][] = _T('erreur_plugin_fichier_absent') . " : $optfile";
104 }
105 }
106 }
107 }
108 $fonctions = array();
109 if (isset($arbre['fonctions'])) {
110 $fonctions = $arbre['fonctions'];
111 }
112 $liste_methodes_reservees = array(
113 '__construct',
114 '__destruct',
115 'plugin',
116 'install',
117 'uninstall',
118 strtolower($prefix)
119 );
120
121 $extraire_pipelines = charger_fonction("extraire_pipelines", "plugins");
122 $arbre['pipeline'] = $extraire_pipelines($arbre);
123 foreach ($arbre['pipeline'] as $pipe) {
124 if (!isset($pipe['nom'])) {
125 if (!$silence) {
126 $arbre['erreur'][] = _T("erreur_plugin_nom_pipeline_non_defini");
127 }
128 }
129 if (isset($pipe['action'])) {
130 $action = $pipe['action'];
131 } else {
132 $action = $pipe['nom'];
133 }
134 // verif que la methode a un nom autorise
135 if (in_array(strtolower($action), $liste_methodes_reservees)) {
136 if (!$silence) {
137 $arbre['erreur'][] = _T("erreur_plugin_nom_fonction_interdit") . " : $action";
138 }
139 }
140 if (isset($pipe['inclure'])) {
141 $inclure = $dir_plugins . "$plug/" . $pipe['inclure'];
142 if (!@is_readable($inclure)) {
143 if (!$silence) {
144 $arbre['erreur'][] = _T('erreur_plugin_fichier_absent') . " : $inclure";
145 }
146 }
147 }
148 }
149 $necessite = array();
150 $spip_trouve = false;
151 if (spip_xml_match_nodes(',^necessite,', $arbre, $needs)) {
152 foreach (array_keys($needs) as $tag) {
153 list($tag, $att) = spip_xml_decompose_tag($tag);
154 if (!isset($att['id'])) {
155 if (!$silence) {
156 $arbre['erreur'][] = _T('erreur_plugin_attribut_balise_manquant',
157 array('attribut' => 'id', 'balise' => $att));
158 }
159 } else {
160 $necessite[] = $att;
161 }
162 if (strtolower($att['id']) == 'spip') {
163 $spip_trouve = true;
164 }
165 }
166 }
167 if ($compat_spip and !$spip_trouve) {
168 $necessite[] = array('id' => 'spip', 'version' => $compat_spip);
169 }
170 $arbre['necessite'] = $necessite;
171 $utilise = array();
172 if (spip_xml_match_nodes(',^utilise,', $arbre, $uses)) {
173 foreach (array_keys($uses) as $tag) {
174 list($tag, $att) = spip_xml_decompose_tag($tag);
175 if (!isset($att['id'])) {
176 if (!$silence) {
177 $arbre['erreur'][] = _T('erreur_plugin_attribut_balise_manquant',
178 array('attribut' => 'id', 'balise' => $att));
179 }
180 } else {
181 $utilise[] = $att;
182 }
183 }
184 }
185 $arbre['utilise'] = $utilise;
186 $procure = array();
187 if (spip_xml_match_nodes(',^procure,', $arbre, $uses)) {
188 foreach (array_keys($uses) as $tag) {
189 list($tag, $att) = spip_xml_decompose_tag($tag);
190 $procure[] = $att;
191 }
192 }
193 $arbre['procure'] = $procure;
194 $path = array();
195 if (spip_xml_match_nodes(',^chemin,', $arbre, $paths)) {
196 foreach (array_keys($paths) as $tag) {
197 list($tag, $att) = spip_xml_decompose_tag($tag);
198 $att['path'] = $att['dir']; // ancienne syntaxe
199 $path[] = $att;
200 }
201 } else {
202 $path = array(array('dir' => ''));
203 } // initialiser par defaut
204 $arbre['path'] = $path;
205 // exposer les noisettes
206 if (isset($arbre['noisette'])) {
207 foreach ($arbre['noisette'] as $k => $nut) {
208 $nut = preg_replace(',[.]html$,uims', '', trim($nut));
209 $arbre['noisette'][$k] = $nut;
210 if (!@is_readable($dir_plugins . "$plug/$nut.html")) {
211 if (!$silence) {
212 $arbre['erreur'][] = _T('erreur_plugin_fichier_absent') . " : $nut";
213 }
214 }
215 }
216 }
217 $traduire = array();
218 if (spip_xml_match_nodes(',^traduire,', $arbre, $trads)) {
219 foreach (array_keys($trads) as $tag) {
220 list($tag, $att) = spip_xml_decompose_tag($tag);
221 $traduire[] = $att;
222 }
223 }
224 $arbre['traduire'] = $traduire;
225 }
226 }