source: trunk/spip/esqueleto-redcta/plugins/cfg/depots/php.php @ 152

Last change on this file since 152 was 152, checked in by guille, 15 years ago

se agregaron svn_update y cfg para el 2.0

File size: 4.6 KB
Line 
1<?php
2
3/*
4 * Plugin CFG pour SPIP
5 * (c) toggg 2007, distribue sous licence GNU/GPL
6 * Documentation et contact: http://www.spip-contrib.net/
7 *
8 * classe cfg_php: storage dans un fichier php
9 */
10
11if (!defined("_ECRIRE_INC_VERSION")) return;
12
13// cfg_php retrouve et met a jour les donnees d'un fichier php
14class cfg_depot_php
15{
16        var $champs = array();
17        var $champs_id = array();
18        var $val = array();
19        var $param = array();
20        var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
21       
22        var $_arbre = array();
23       
24        // version du depot
25        var $version = 2;
26       
27       
28        function cfg_depot_php($params=array()) {
29                foreach ($params as $o=>$v) {
30                        $this->$o = $v;
31                }
32        }
33       
34        // calcule l'emplacement du fichier
35        function get_fichier(){
36                static $fichier = array();
37                $cle = $this->param['nom'] . ' - ' . $this->param['fichier'];
38                if (isset($fichier[$cle])) 
39                        return $fichier[$cle];
40               
41                if (!$this->param['fichier']) 
42                        $f = _DIR_VAR . 'cfg/' . $this->param['nom'] . '.php'; 
43                else
44                        $f = _DIR_RACINE . $this->param['fichier'];
45
46                include_spip('inc/flock');
47                return $fichier[$cle] = sous_repertoire(dirname($f)) . basename($f);
48        }
49       
50       
51        // charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
52        function charger($lire=false){
53                $fichier = $this->get_fichier();
54
55                // inclut une variable $cfg
56        if (!@include $fichier) {
57                if ($lire) return false;
58                $this->_base = array();
59        } elseif (!$cfg OR !is_array($cfg)) {
60                $this->_base = array();
61        } else {
62                $this->_base = $cfg;   
63        }
64
65        $this->_ici = &$this->_base;
66        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['nom']);
67        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['casier']);
68        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['cfg_id']); 
69        return true;
70        }
71       
72        // recuperer les valeurs.
73        function lire() {
74                if (!$this->charger(true)){
75                        return array(true, null); // pas de chargement = pas de valeur encore enregistrees
76                }
77               
78        // utile ??
79        if ($this->param['cfg_id']) {
80                $cles = explode('/', $this->param['cfg_id']);
81                        foreach ($this->champs_id as $i => $name) {
82                                $this->_ici[$name] = $cles[$i];
83                    }
84        }
85            return array(true, $this->_ici);
86        }
87
88
89        // ecrit chaque enregistrement pour chaque champ
90        function ecrire() {
91                if (!$this->charger()){
92                        return array(false, $this->val);       
93                }
94
95                foreach ($this->champs as $name => $def) {
96                        if (isset($def['id'])) continue;
97                        $this->_ici[$name] = $this->val[$name];
98                }
99               
100                if (!$this->ecrire_fichier($this->_base)){
101                        return array(false, $this->val);
102                }
103               
104                return array(true, $this->_ici);       
105        }
106       
107       
108        // supprime chaque enregistrement pour chaque champ
109        function effacer(){
110                if (!$this->charger()){
111                        return array(false, $this->val);       
112                }
113
114                // pas de champ, on supprime tout
115                if (!$this->champs)
116                        return array($this->ecrire_fichier(), array());
117                       
118        // effacer les champs
119        foreach ($this->champs as $name => $def) {
120                        if (isset($def['id'])) continue;
121                        unset($this->_ici[$name]);
122                }
123               
124                // supprimer les dossiers vides
125                for ($i = count($this->_arbre); $i--; ) {
126                        if ($this->_arbre[$i][0][$this->_arbre[$i][1]]) {
127                                break;
128                        }
129                        unset($this->_arbre[$i][0][$this->_arbre[$i][1]]);
130                }
131
132                return array($this->ecrire_fichier($this->_base), $this->_ici);
133        }
134       
135       
136        function ecrire_fichier($contenu=array()){
137                $fichier = $this->get_fichier();
138
139                if (!$contenu) {
140                        return supprimer_fichier($fichier);
141                }
142
143$contenu = '<?php
144/**************
145* Config ecrite par cfg le ' . date('r') . '
146*
147* NE PAS EDITER MANUELLEMENT !
148***************/
149
150$cfg = ' . var_export($contenu, true) . ';
151?>
152';
153                return ecrire_fichier($fichier, $contenu);
154        }
155       
156        // charger les arguments de
157        // - lire_config(php::nom/casier/champ)
158        // - lire_config(php::adresse/fichier.php:nom/casier/champ)
159        function charger_args($args){
160                list($fichier, $args) = explode(':',$args);
161                if (!$args) {
162                        $args = $fichier;
163                        $fichier = _DIR_VAR . 'cfg/' . $fichier . '.php';       
164                }
165
166                $arbre = explode('/',$args);
167                $this->param['nom'] = array_shift($arbre);
168                if ($champ = array_pop($arbre))
169                        $this->champs = array($champ=>true);
170                $this->param['casier'] = implode('/',$arbre);
171                return true;   
172        }
173       
174       
175        // se positionner dans le tableau arborescent
176        function & monte_arbre(&$base, $chemin){
177                if (!$chemin) {
178                        return $base;
179                }
180                if (!is_array($chemin)) {
181                        $chemin = explode('/', $chemin);
182                }
183                if (!is_array($base)) {
184                        $base = array();
185                }
186               
187                foreach ($chemin as $dossier) {
188                        if (!isset($base[$dossier])) {
189                                $base[$dossier] = array();
190                        }
191                        $this->_arbre[] = array(&$base, $dossier);
192                        $base = &$base[$dossier];
193                }
194               
195                return $base;
196        }
197}
198
199?>
Note: See TracBrowser for help on using the repository browser.