4 * Plugin générique de configuration pour SPIP
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 $
15 if (!defined("_ECRIRE_INC_VERSION")) return;
18 * Retrouve et met a jour les donnees d'un fichier php.
25 * Les champs manipulés
28 var $champs = array();
31 * Si on passe par cfg_id, ça fait..
32 * Heu.. Quelque chose d'utile ?
35 var $champs_id = array();
38 * Les valeurs en dépôt
44 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
50 * Pour gestion de l'affichage en succès ou échec
53 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
59 var $_arbre = array();
68 * Dépôt dans les attributs de la classe
70 * @param Array $params
72 function cfg_depot_php($params=array()) {
73 foreach ($params as $o=>$v) {
80 * calcule l'emplacement du fichier
82 * @staticvar Array $fichier
83 * @return string # L'emplacement du fichier
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];
91 if (!$this->param
['fichier'])
92 $f = _DIR_VAR
. 'cfg/' . $this->param
['nom'] . '.php';
94 $f = _DIR_RACINE
. $this->param
['fichier'];
96 include_spip('inc/flock');
97 return $fichier[$cle] = sous_repertoire(dirname($f)) . basename($f);
102 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
104 * @param boolean $lire
107 function charger($lire=false){
108 $fichier = $this->get_fichier();
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();
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']);
128 * recuperer les valeurs.
133 if (!$this->charger(true)){
134 return array(true, null); // pas de chargement = pas de valeur encore enregistrees
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];
144 return array(true, $this->_ici
);
149 * ecrit chaque enregistrement pour chaque champ.
154 if (!$this->charger()){
155 return array(false, $this->val
);
158 foreach ($this->champs
as $name => $def) {
159 if (isset($def['id'])) continue;
160 $this->_ici
[$name] = $this->val
[$name];
163 if (!$this->ecrire_fichier($this->_base
)){
164 return array(false, $this->val
);
167 return array(true, $this->_ici
);
172 * supprime chaque enregistrement pour chaque champ.
177 if (!$this->charger()){
178 return array(false, $this->val
);
181 // pas de champ, on supprime tout
183 return array($this->ecrire_fichier(), array());
185 // effacer les champs
186 foreach ($this->champs
as $name => $def) {
187 if (isset($def['id'])) continue;
188 unset($this->_ici
[$name]);
191 // supprimer les dossiers vides
192 for ($i = count($this->_arbre
); $i--; ) {
193 if ($this->_arbre
[$i][0][$this->_arbre
[$i][1]]) {
196 unset($this->_arbre
[$i][0][$this->_arbre
[$i][1]]);
199 return array($this->ecrire_fichier($this->_base
), $this->_ici
);
206 * @param Array $contenu
209 function ecrire_fichier($contenu=array()){
210 $fichier = $this->get_fichier();
213 return supprimer_fichier($fichier);
218 * Config ecrite par cfg le ' . date('r') . '
220 * NE PAS EDITER MANUELLEMENT !
223 $cfg = ' . var_export($contenu, true) . ';
226 return ecrire_fichier($fichier, $contenu);
230 * charger les arguments de
231 * - lire_config(php::nom/casier/champ)
232 * - lire_config(php::adresse/fichier.php:nom/casier/champ)
234 * @param string $args
237 function charger_args($args){
238 list($fichier, $args) = explode(':',$args);
241 $fichier = _DIR_VAR
. 'cfg/' . $fichier . '.php';
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);
254 * se positionner dans le tableau arborescent
256 * @param &Array $base
257 * @param Array $chemin
260 function & monte_arbre(&$base, $chemin){
264 if (!is_array($chemin)) {
265 $chemin = explode('/', $chemin);
267 if (!is_array($base)) {
271 foreach ($chemin as $dossier) {
272 if (!isset($base[$dossier])) {
273 $base[$dossier] = array();
275 $this->_arbre
[] = array(&$base, $dossier);
276 $base = &$base[$dossier];