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