public attr / function, constructeurs __construct
[mtweb] / mw / env / modules / mw_env_plugins.php
1 <?php
2
3   class mw_env_plugins extends mw_env{
4
5     public $plugins_asc;
6     public $plugins_desc;
7
8     public function plugins($PRIORITE = "ASC"){
9       $this->init_plugins($PRIORITE);
10       if($PRIORITE == "ASC") return $this->plugins_asc;
11       if($PRIORITE == "DESC") return $this->plugins_desc;
12       return false;
13     }
14
15     # ---------------------------------------------------------------------------------
16     #                                                                              init
17     #
18
19     public function init_plugins($PRIORITE = "ASC", $RELOAD = false){
20       if(!$RELOAD && (isset($this->plugins_asc) || isset($this->plugins_desc))){
21         if($PRIORITE == "ASC"){
22           if(!isset($this->plugins_asc)) $this->plugins_asc = $this->ordonne_plugins($this->plugins_desc, $PRIORITE);
23         }
24         elseif($PRIORITE == "DESC"){
25           if(!isset($this->plugins_desc)) $this->plugins_desc = $this->ordonne_plugins($this->plugins_asc, $PRIORITE);
26         }
27         return;
28       }
29       $plugins = array();
30       if($dh = opendir($this->path("mw_dir")."plugins/")){
31         $OK = true;
32         while($OK && ($plugin_name = readdir($dh)) !== false){
33           if(substr($plugin_name, 0 ,1) !== "." && is_dir($this->path("mw_dir")."plugins/".$plugin_name)){
34             if(!isset($plugins[$plugin_name])){
35               if(($plugin = $this->plugin_data($plugin_name)) !== false){
36                 $MAJ = false;
37                 if(!isset($plugin["installed"]) || !isset($plugin["enabled"])){
38                   $plugin["installed"] = false;
39                   $plugin["enabled"] = false;
40                   $plugin["priorite"] = 0;
41                   $MAJ = true;
42                 }
43                 if(!$plugin["installed"] && $plugin["enabled"]){
44                   $plugin["enabled"] = false;
45                   $MAJ = true;
46                 }
47                 if($MAJ) $OK = $this->set_plugin_data($plugin_name, $plugin);
48                 if($OK){
49                   if(($plugin["impl"] = $this->plugin_impl($plugin_name)) !== false){
50                     $plugin["title"] =  ($plugin_title = $this->plugin_call($plugin["impl"], "title")) ? $plugin_title : "";
51                     $plugin["description"] = ($plugin_description = $this->plugin_call($plugin["impl"], "description")) ? $plugin_description : "";
52                     $plugin["name"] = $plugin_name;
53                     $plugins[$plugin_name] = $plugin;
54                   }
55                 }
56               }
57               else $OK = false;
58             }
59           }
60           if(!$OK) $plugins = false;
61         }
62         closedir($dh);
63         if($plugins !== false){
64           if(file_exists($this->plugins_data_dir()) && is_dir($this->plugins_data_dir())){
65             if($dh = opendir($this->plugins_data_dir())){
66               $plugins_data_files = array();
67               $OK = true;
68               while($OK && ($plugin_name = readdir($dh)) !== false){
69                 if(substr($plugin_name, 0 ,1) != "." && !is_dir($this->plugin_data_file($plugin_name))){
70                   if(!isset($plugins[$plugin_name])) $this->del_plugin_data($plugin_name);
71                 }
72                 if(!$OK) $plugins = false;
73               }
74               closedir($dh);
75             }
76           }
77         }
78       }
79       else $plugins = false;
80       if($plugins !== false){
81         if($PRIORITE == "ASC") $this->plugins_asc = $this->ordonne_plugins($plugins, $PRIORITE);
82         elseif($PRIORITE == "DESC") $this->plugins_desc = $this->ordonne_plugins($plugins, $PRIORITE);
83       }
84       else{
85         $this->plugins_asc = false;
86         $this->plugins_desc = false;
87       }
88     }
89
90     public function ordonne_plugins($plugins, $PRIORITE = "ASC"){
91       $values = array_values($plugins);
92       $maximum = count($values);
93       while($maximum > 0){
94         $maximumTemporaire = 0;
95         for($i = 0; $i < $maximum - 1; $i++){
96           if(
97                ($PRIORITE == "ASC" && $values[$i]["priorite"] > $values[$i + 1]["priorite"])
98             || ($PRIORITE == "DESC" && $values[$i]["priorite"] < $values[$i + 1]["priorite"])
99           ){
100             $tmp = $values[$i];
101             $values[$i] = $values[$i + 1];
102             $values[$i + 1] = $tmp;
103             $maximumTemporaire = $i + 1;
104           }
105         }
106         $maximum = $maximumTemporaire;
107       }
108       $res = array();
109       foreach($values as $value) if($value["name"]) $res[$value["name"]] = $value;
110       return $res;
111     }
112
113     public function plugin_call($impl, $method){
114       if(method_exists($impl, $method)) return $impl->$method($this);
115     }
116
117     public function loaded_plugins(){
118       if($plugins = $this->plugins()){
119         foreach($plugins as $plugin_name => $plugin){
120           $plugin["impl"]->loaded($this);
121         }
122       }
123     }
124
125     # ---------------------------------------------------------------------------------
126     #                                                                              impl
127     #
128
129     public function plugin_impl($plugin_name){
130       $plugin = false;
131       if(file_exists($this->path("mw_dir")."plugins")){
132         if(substr($plugin_name, 0 ,1) !== "." && is_dir($this->path("mw_dir")."plugins/".$plugin_name)){
133           if(file_exists($this->path("mw_dir")."plugins/".$plugin_name."/".$plugin_name.".php")){
134             if(!class_exists($plugin_name)){
135               require_once $this->path("mw_dir")."plugins/".$plugin_name."/".$plugin_name.".php";
136             }
137             if(class_exists($plugin_name)){
138               $plugin = new $plugin_name();
139             }
140           }
141         }
142       }
143       return $plugin;
144     }
145
146     # ---------------------------------------------------------------------------------
147     #                                                                              data
148     #
149
150     public function plugins_data_dir(){
151       return $this->path("content")."data/plugins/";
152     }
153
154     public function plugin_data_file($plugin_name){
155       return $this->plugins_data_dir().$plugin_name;
156     }
157
158     public function plugin_data($plugin_name){
159       $data_file = $this->plugin_data_file($plugin_name);
160       $data = array();
161       if(file_exists($data_file)){
162         if($content = file_get_contents($data_file)){
163           $data = unserialize($content);
164         }
165       }
166       return $data;
167     }
168
169     public function set_plugin_data($plugin_name, $data){
170       if(!is_dir($this->plugins_data_dir())) @mkdir($this->plugins_data_dir());
171       if(!is_dir($this->plugins_data_dir())) return false;
172       $data_file = $this->plugin_data_file($plugin_name);
173       $content = serialize($data);
174       $OK = false;
175       if($fh = fopen($data_file, "w")){
176         if(fwrite($fh, $content) !== false){
177           $OK = true;
178         }
179         fclose($fh);
180       }
181       return $OK;
182     }
183
184     public function del_plugin_data($plugin_name){
185       $data_file = $this->plugin_data_file($plugin_name);
186       if(file_exists($data_file)) return @unlink($data_file);
187       return true;
188     }
189
190   }
191
192   // -------------------------------------------------------------------------------------------
193   //                                                                             class mw_plugin
194   //
195
196   abstract class mw_plugin{
197
198     public function title(){
199       return "(sans titre)";
200     }
201
202     public function description(){
203       return "";
204     }
205
206     public function loaded($env){
207     }
208
209     public function install($env){
210       return true;
211     }
212
213     public function uninstall($env){
214       return true;
215     }
216
217     public function enable($env){
218       return true;
219     }
220
221     public function disable($env){
222       return true;
223     }
224
225     public function init($env){
226       return true;
227     }
228
229   }