1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of dcAdvancedCleaner, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | if (!defined('DC_ADMIN_CONTEXT')){return;} |
---|
14 | |
---|
15 | /** |
---|
16 | @brief Modules uninstall features handler |
---|
17 | |
---|
18 | Provides an object to handle modules uninstall features |
---|
19 | (themes or plugins). |
---|
20 | This class used dcAdvancedCleaner. |
---|
21 | */ |
---|
22 | class dcUninstaller |
---|
23 | { |
---|
24 | protected $path; |
---|
25 | |
---|
26 | protected $modules = array(); ///< <b>array</b> Modules informations array |
---|
27 | protected $actions = array('user'=>array(),'callback'=>array()); |
---|
28 | protected $callbacks = array('user'=>array(),'callback'=>array()); |
---|
29 | |
---|
30 | protected $id = null; |
---|
31 | protected $mroot = null; |
---|
32 | |
---|
33 | /** |
---|
34 | Array of all allowed properties to uninstall parts of modules. |
---|
35 | 'settings' : settings set on dcSettings, |
---|
36 | 'tables' : if module creates table, |
---|
37 | 'plugins' : if module has files on plugin path, |
---|
38 | 'themes' : if module has files on theme path, (on current blog) |
---|
39 | 'caches' : if module has files on DC caches path, |
---|
40 | 'versions' : if module set a versions on DC table 'version' |
---|
41 | */ |
---|
42 | protected static $allowed_properties = array( |
---|
43 | 'settings' => array( |
---|
44 | 'delete_global' => 'delete global settings', |
---|
45 | 'delete_local' => 'delete local settings', |
---|
46 | 'delete_all' => 'delete all settings' |
---|
47 | ), |
---|
48 | 'tables' => array( |
---|
49 | 'empty' => 'empty table', |
---|
50 | 'delete' => 'delete table' |
---|
51 | ), |
---|
52 | 'plugins' => array( |
---|
53 | 'empty' => 'empty plugin folder', |
---|
54 | 'delete' => 'delete plugin folder' |
---|
55 | ), |
---|
56 | 'themes' => array( |
---|
57 | 'empty' => 'empty theme folder', |
---|
58 | 'delete' => 'delete theme folder' |
---|
59 | ), |
---|
60 | 'caches' => array( |
---|
61 | 'empty' => 'empty cache folder', |
---|
62 | 'delete' => 'delete cache folder' |
---|
63 | ), |
---|
64 | 'versions' => array( |
---|
65 | 'delete' => 'delete version in dc' |
---|
66 | ) |
---|
67 | ); |
---|
68 | |
---|
69 | public $core; ///< <b>dcCore</b> dcCore instance |
---|
70 | |
---|
71 | /** |
---|
72 | Object constructor. |
---|
73 | |
---|
74 | @param core <b>dcCore</b> dcCore instance |
---|
75 | */ |
---|
76 | public function __construct($core) |
---|
77 | { |
---|
78 | $this->core =& $core; |
---|
79 | } |
---|
80 | |
---|
81 | public static function getAllowedProperties() |
---|
82 | { |
---|
83 | return self::$allowed_properties; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | Loads modules. |
---|
88 | Files _defines.php and _uninstall.php must be present on module |
---|
89 | to be recognized. |
---|
90 | (path separator depends on your OS). |
---|
91 | |
---|
92 | @param path <b>string</b> Separated list of paths |
---|
93 | */ |
---|
94 | public function loadModules($path) |
---|
95 | { |
---|
96 | $this->path = explode(PATH_SEPARATOR,$path); |
---|
97 | |
---|
98 | foreach ($this->path as $root) { |
---|
99 | $this->loadModule($root); |
---|
100 | } |
---|
101 | |
---|
102 | # Sort modules by name |
---|
103 | uasort($this->modules,array($this,'sortModules')); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | Load one module. |
---|
108 | Files _defines.php and _uninstall.php must be present on module |
---|
109 | to be recognized. |
---|
110 | |
---|
111 | @param root <b>string</b> path of module |
---|
112 | */ |
---|
113 | public function loadModule($root) |
---|
114 | { |
---|
115 | if (!is_dir($root) || !is_readable($root)) return;; |
---|
116 | |
---|
117 | if (substr($root,-1) != '/') $root .= '/'; |
---|
118 | |
---|
119 | if (($d = @dir($root)) === false) return; |
---|
120 | |
---|
121 | while (($entry = $d->read()) !== false) |
---|
122 | { |
---|
123 | $full_entry = $root.'/'.$entry; |
---|
124 | |
---|
125 | if ($entry != '.' && $entry != '..' && is_dir($full_entry) |
---|
126 | && file_exists($full_entry.'/_define.php') |
---|
127 | && file_exists($full_entry.'/_uninstall.php')) { |
---|
128 | |
---|
129 | $this->id = $entry; |
---|
130 | $this->mroot = $full_entry; |
---|
131 | |
---|
132 | require $full_entry.'/_define.php'; |
---|
133 | require $full_entry.'/_uninstall.php'; |
---|
134 | |
---|
135 | $this->id = null; |
---|
136 | $this->mroot = null; |
---|
137 | } |
---|
138 | } |
---|
139 | $d->close(); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | This method registers a module in modules list. You should use |
---|
144 | this to register a new module. |
---|
145 | |
---|
146 | @param name <b>string</b> Module name |
---|
147 | @param desc <b>string</b> Module description |
---|
148 | @param author <b>string</b> Module author name |
---|
149 | @param version <b>string</b> Module version |
---|
150 | */ |
---|
151 | public function registerModule($name,$desc,$author,$version) |
---|
152 | { |
---|
153 | if ($this->id) { |
---|
154 | $this->modules[$this->id] = array( |
---|
155 | 'root' => $this->mroot, |
---|
156 | 'name' => $name, |
---|
157 | 'desc' => $desc, |
---|
158 | 'author' => $author, |
---|
159 | 'version' => $version, |
---|
160 | 'root_writable' => is_writable($this->mroot) |
---|
161 | ); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | Returns all modules associative array or only one module if <var>$id</var> |
---|
167 | is present. |
---|
168 | |
---|
169 | @param id <b>string</b> Optionnal module ID |
---|
170 | @return <b>array</b> |
---|
171 | */ |
---|
172 | public function getModules($id=null) |
---|
173 | { |
---|
174 | if ($id && isset($this->modules[$id])) { |
---|
175 | return $this->modules[$id]; |
---|
176 | } |
---|
177 | return $this->modules; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | Returns true if the module with ID <var>$id</var> exists. |
---|
182 | |
---|
183 | @param id <b>string</b> Module ID |
---|
184 | @return <b>boolean</b> |
---|
185 | */ |
---|
186 | public function moduleExists($id) |
---|
187 | { |
---|
188 | return isset($this->modules[$id]); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | Add a predefined action to unsintall features. |
---|
193 | This action is set in _uninstall.php. |
---|
194 | |
---|
195 | @param type <b>string</b> Type of action (from $allowed_properties) |
---|
196 | @param action <b>string</b> Action (from $allowed_properties) |
---|
197 | @param ns <b>string</b> Name of setting related to module. |
---|
198 | @param desc <b>string</b> Description of action |
---|
199 | */ |
---|
200 | protected function addUserAction($type,$action,$ns,$desc='') |
---|
201 | { |
---|
202 | $this->addAction('user',$type,$action,$ns,$desc); |
---|
203 | } |
---|
204 | |
---|
205 | protected function addDirectAction($type,$action,$ns,$desc='') |
---|
206 | { |
---|
207 | $this->addAction('direct',$type,$action,$ns,$desc); |
---|
208 | } |
---|
209 | |
---|
210 | private function addAction($group,$type,$action,$ns,$desc='') |
---|
211 | { |
---|
212 | $group = self::group($group); |
---|
213 | |
---|
214 | if (null === $this->id) return; |
---|
215 | |
---|
216 | if (empty($type) || empty($ns)) return; |
---|
217 | |
---|
218 | if (!isset(self::$allowed_properties[$type][$action])) return; |
---|
219 | |
---|
220 | if (empty($desc)) $desc = __($action); |
---|
221 | |
---|
222 | $this->actions[$group][$this->id][$type][] = array( |
---|
223 | 'ns' => $ns, |
---|
224 | 'action' => $action, |
---|
225 | 'desc' => $desc |
---|
226 | ); |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | Returns modules <var>$id</var> predefined actions associative array |
---|
231 | |
---|
232 | @param id <b>string</b> Optionnal module ID |
---|
233 | @return <b>array</b> |
---|
234 | */ |
---|
235 | public function getUserActions($id) |
---|
236 | { |
---|
237 | return $this->getActions('user',$id); |
---|
238 | } |
---|
239 | |
---|
240 | public function getDirectActions($id) |
---|
241 | { |
---|
242 | return $this->getActions('direct',$id); |
---|
243 | } |
---|
244 | |
---|
245 | protected function getActions($group,$id) |
---|
246 | { |
---|
247 | $group = self::group($group); |
---|
248 | |
---|
249 | if (!isset($this->actions[$group][$id])) return array(); |
---|
250 | |
---|
251 | return $this->actions[$group][$id]; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | Add a callable function for unsintall features. |
---|
256 | This action is set in _uninstall.php. |
---|
257 | |
---|
258 | @param func <b>string</b> Callable function |
---|
259 | @param desc <b>string</b> Description of action |
---|
260 | */ |
---|
261 | protected function addUserCallback($func,$desc='') |
---|
262 | { |
---|
263 | $this->addCallback('user',$func,$desc); |
---|
264 | } |
---|
265 | |
---|
266 | protected function addDirectCallback($func,$desc='') |
---|
267 | { |
---|
268 | $this->addCallback('direct',$func,$desc); |
---|
269 | } |
---|
270 | |
---|
271 | private function addCallback($group,$func,$desc) |
---|
272 | { |
---|
273 | $group = self::group($group); |
---|
274 | |
---|
275 | if (null === $this->id) return; |
---|
276 | |
---|
277 | if (empty($desc)) $desc = __('extra action'); |
---|
278 | |
---|
279 | if (!is_callable($func)) return; |
---|
280 | |
---|
281 | $this->callbacks[$group][$this->id][] = array( |
---|
282 | 'func' => $func, |
---|
283 | 'desc' => $desc |
---|
284 | ); |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | Returns modules <var>$id</var> callback actions associative array |
---|
289 | |
---|
290 | @param id <b>string</b> Optionnal module ID |
---|
291 | @return <b>array</b> |
---|
292 | */ |
---|
293 | public function getUserCallbacks($id) |
---|
294 | { |
---|
295 | return $this->getCallbacks('user',$id); |
---|
296 | } |
---|
297 | |
---|
298 | public function getDirectCallbacks($id) |
---|
299 | { |
---|
300 | return $this->getCallbacks('direct',$id); |
---|
301 | } |
---|
302 | |
---|
303 | protected function getCallbacks($group,$id) |
---|
304 | { |
---|
305 | $group = self::group($group); |
---|
306 | |
---|
307 | if (!isset($this->callbacks[$group][$id])) return array(); |
---|
308 | |
---|
309 | return $this->callbacks[$group][$id]; |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | Execute a predifined action. This function call dcAdvancedCleaner |
---|
314 | to do actions. |
---|
315 | |
---|
316 | @param type <b>string</b> Type of action (from $allowed_properties) |
---|
317 | @param action <b>string</b> Action (from $allowed_properties) |
---|
318 | @param ns <b>string</b> Name of setting related to module. |
---|
319 | @return <b>array</b> |
---|
320 | */ |
---|
321 | public function execute($type,$action,$ns) |
---|
322 | { |
---|
323 | $prop = $this->getAllowedProperties(); |
---|
324 | |
---|
325 | if (!isset($prop[$type][$action]) || empty($ns)) return; |
---|
326 | |
---|
327 | dcAdvancedCleaner::execute($this->core,$type,$action,$ns); |
---|
328 | } |
---|
329 | |
---|
330 | private function sortModules($a,$b) |
---|
331 | { |
---|
332 | return strcasecmp($a['name'],$b['name']); |
---|
333 | } |
---|
334 | |
---|
335 | private function group($group) |
---|
336 | { |
---|
337 | return in_array($group,array('user','direct')) ? $group : null; |
---|
338 | } |
---|
339 | } |
---|
340 | ?> |
---|