[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / cfg / depots / php.php
1 <?php
2
3 /**
4 * Plugin générique de configuration pour SPIP
5 *
6 * @license GNU/GPL
7 * @package plugins
8 * @subpackage cfg
9 * @category outils
10 * @copyright (c) toggg, marcimat 2007-2008
11 * @link http://www.spip-contrib.net/
12 * @version $Id: php.php 43371 2011-01-07 10:44:03Z alexis.pellicier@nds.k12.tr $
13 */
14
15 if (!defined("_ECRIRE_INC_VERSION")) return;
16
17 /**
18 * Retrouve et met a jour les donnees d'un fichier php.
19 * @package plugins
20 * @subpackage cfg
21 */
22 class cfg_depot_php
23 {
24 /**
25 * Les champs manipulés
26 * @var Array
27 */
28 var $champs = array();
29
30 /**
31 * Si on passe par cfg_id, ça fait..
32 * Heu.. Quelque chose d'utile ?
33 * @var Array
34 */
35 var $champs_id = array();
36
37 /**
38 * Les valeurs en dépôt
39 * @var Array
40 */
41 var $val = array();
42
43 /**
44 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
45 * @var Array
46 */
47 var $param = array();
48
49 /**
50 * Pour gestion de l'affichage en succès ou échec
51 * @var Array
52 */
53 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
54
55 /**
56 * Arbre
57 * @var Array
58 */
59 var $_arbre = array();
60
61 /**
62 * version du depot
63 * @var int
64 */
65 var $version = 2;
66
67 /**
68 * Dépôt dans les attributs de la classe
69 *
70 * @param Array $params
71 */
72 function cfg_depot_php($params=array()) {
73 foreach ($params as $o=>$v) {
74 $this->$o = $v;
75 }
76 }
77
78
79 /**
80 * calcule l'emplacement du fichier
81 *
82 * @staticvar Array $fichier
83 * @return string # L'emplacement du fichier
84 */
85 function get_fichier(){
86 static $fichier = array();
87 $cle = $this->param['nom'] . ' - ' . $this->param['fichier'];
88 if (isset($fichier[$cle]))
89 return $fichier[$cle];
90
91 if (!$this->param['fichier'])
92 $f = _DIR_VAR . 'cfg/' . $this->param['nom'] . '.php';
93 else
94 $f = _DIR_RACINE . $this->param['fichier'];
95
96 include_spip('inc/flock');
97 return $fichier[$cle] = sous_repertoire(dirname($f)) . basename($f);
98 }
99
100
101 /**
102 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
103 *
104 * @param boolean $lire
105 * @return boolean
106 */
107 function charger($lire=false){
108 $fichier = $this->get_fichier();
109
110 // inclut une variable $cfg
111 if (!@include $fichier) {
112 if ($lire) return false;
113 $this->_base = array();
114 } elseif (!$cfg OR !is_array($cfg)) {
115 $this->_base = array();
116 } else {
117 $this->_base = $cfg;
118 }
119
120 $this->_ici = &$this->_base;
121 $this->_ici = &$this->monte_arbre($this->_ici, $this->param['nom']);
122 $this->_ici = &$this->monte_arbre($this->_ici, $this->param['casier']);
123 $this->_ici = &$this->monte_arbre($this->_ici, $this->param['cfg_id']);
124 return true;
125 }
126
127 /**
128 * recuperer les valeurs.
129 *
130 * @return Array
131 */
132 function lire() {
133 if (!$this->charger(true)){
134 return array(true, null); // pas de chargement = pas de valeur encore enregistrees
135 }
136
137 // utile ??
138 if ($this->param['cfg_id']) {
139 $cles = explode('/', $this->param['cfg_id']);
140 foreach ($this->champs_id as $i => $name) {
141 $this->_ici[$name] = $cles[$i];
142 }
143 }
144 return array(true, $this->_ici);
145 }
146
147
148 /**
149 * ecrit chaque enregistrement pour chaque champ.
150 *
151 * @return Array
152 */
153 function ecrire() {
154 if (!$this->charger()){
155 return array(false, $this->val);
156 }
157
158 foreach ($this->champs as $name => $def) {
159 if (isset($def['id'])) continue;
160 $this->_ici[$name] = $this->val[$name];
161 }
162
163 if (!$this->ecrire_fichier($this->_base)){
164 return array(false, $this->val);
165 }
166
167 return array(true, $this->_ici);
168 }
169
170
171 /**
172 * supprime chaque enregistrement pour chaque champ.
173 *
174 * @return Array
175 */
176 function effacer(){
177 if (!$this->charger()){
178 return array(false, $this->val);
179 }
180
181 // pas de champ, on supprime tout
182 if (!$this->champs)
183 return array($this->ecrire_fichier(), array());
184
185 // effacer les champs
186 foreach ($this->champs as $name => $def) {
187 if (isset($def['id'])) continue;
188 unset($this->_ici[$name]);
189 }
190
191 // supprimer les dossiers vides
192 for ($i = count($this->_arbre); $i--; ) {
193 if ($this->_arbre[$i][0][$this->_arbre[$i][1]]) {
194 break;
195 }
196 unset($this->_arbre[$i][0][$this->_arbre[$i][1]]);
197 }
198
199 return array($this->ecrire_fichier($this->_base), $this->_ici);
200 }
201
202
203 /**
204 * Ecrire un fichier
205 *
206 * @param Array $contenu
207 * @return boolean
208 */
209 function ecrire_fichier($contenu=array()){
210 $fichier = $this->get_fichier();
211
212 if (!$contenu) {
213 return supprimer_fichier($fichier);
214 }
215
216 $contenu = '<?php
217 /**************
218 * Config ecrite par cfg le ' . date('r') . '
219 *
220 * NE PAS EDITER MANUELLEMENT !
221 ***************/
222
223 $cfg = ' . var_export($contenu, true) . ';
224 ?>
225 ';
226 return ecrire_fichier($fichier, $contenu);
227 }
228
229 /**
230 * charger les arguments de
231 * - lire_config(php::nom/casier/champ)
232 * - lire_config(php::adresse/fichier.php:nom/casier/champ)
233 *
234 * @param string $args
235 * @return boolean
236 */
237 function charger_args($args){
238 list($fichier, $args) = explode(':',$args);
239 if (!$args) {
240 $args = $fichier;
241 $fichier = _DIR_VAR . 'cfg/' . $fichier . '.php';
242 }
243 $this->param['fichier'] = $fichier;
244 $arbre = explode('/',$args);
245 $this->param['nom'] = array_shift($arbre);
246 if ($champ = array_pop($arbre))
247 $this->champs = array($champ=>true);
248 $this->param['casier'] = implode('/',$arbre);
249 return true;
250 }
251
252
253 /**
254 * se positionner dans le tableau arborescent
255 *
256 * @param &Array $base
257 * @param Array $chemin
258 * @return &Array
259 */
260 function & monte_arbre(&$base, $chemin){
261 if (!$chemin) {
262 return $base;
263 }
264 if (!is_array($chemin)) {
265 $chemin = explode('/', $chemin);
266 }
267 if (!is_array($base)) {
268 $base = array();
269 }
270
271 foreach ($chemin as $dossier) {
272 if (!isset($base[$dossier])) {
273 $base[$dossier] = array();
274 }
275 $this->_arbre[] = array(&$base, $dossier);
276 $base = &$base[$dossier];
277 }
278
279 return $base;
280 }
281 }
282
283 ?>