1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | file : $Id: plugins.class.php 2264 2008-03-07 17:15:46Z patdenice $ |
---|
8 | // | last update : $Date: 2008-03-07 17:15:46 +0000 (Fri, 07 Mar 2008) $ |
---|
9 | // | last modifier : $Author: patdenice $ |
---|
10 | // | revision : $Revision: 2264 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | class plugins |
---|
28 | { |
---|
29 | var $fs_plugins = array(); |
---|
30 | var $db_plugins_by_id = array(); |
---|
31 | var $server_plugins = array(); |
---|
32 | |
---|
33 | /** |
---|
34 | * Initialize $fs_plugins and $db_plugins_by_id |
---|
35 | */ |
---|
36 | function plugins() |
---|
37 | { |
---|
38 | $this->get_fs_plugins(); |
---|
39 | |
---|
40 | foreach (get_db_plugins() as $db_plugin) |
---|
41 | { |
---|
42 | $this->db_plugins_by_id[$db_plugin['id']] = $db_plugin; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Perform requested actions |
---|
48 | * @param string - action |
---|
49 | * @param string - plugin id |
---|
50 | * @param array - errors |
---|
51 | */ |
---|
52 | function perform_action($action, $plugin_id, $errors=array()) |
---|
53 | { |
---|
54 | if (isset($this->db_plugins_by_id[$plugin_id])) |
---|
55 | { |
---|
56 | $crt_db_plugin = $this->db_plugins_by_id[$plugin_id]; |
---|
57 | } |
---|
58 | $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php'; |
---|
59 | |
---|
60 | switch ($action) |
---|
61 | { |
---|
62 | case 'install': |
---|
63 | if (!empty($crt_db_plugin)) |
---|
64 | { |
---|
65 | array_push($errors, 'CANNOT INSTALL - ALREADY INSTALLED'); |
---|
66 | break; |
---|
67 | } |
---|
68 | if (!isset($this->fs_plugins[$plugin_id])) |
---|
69 | { |
---|
70 | array_push($errors, 'CANNOT INSTALL - NO SUCH PLUGIN'); |
---|
71 | break; |
---|
72 | } |
---|
73 | if (file_exists($file_to_include)) |
---|
74 | { |
---|
75 | include_once($file_to_include); |
---|
76 | if (function_exists('plugin_install')) |
---|
77 | { |
---|
78 | plugin_install($plugin_id, $this->fs_plugins[$plugin_id]['version'], $errors); |
---|
79 | } |
---|
80 | } |
---|
81 | if (empty($errors)) |
---|
82 | { |
---|
83 | $query = ' |
---|
84 | INSERT INTO ' . PLUGINS_TABLE . ' (id,version) VALUES ("' |
---|
85 | . $plugin_id . '","' . $this->fs_plugins[$plugin_id]['version'] . '" |
---|
86 | )'; |
---|
87 | pwg_query($query); |
---|
88 | } |
---|
89 | break; |
---|
90 | |
---|
91 | case 'activate': |
---|
92 | if (!isset($crt_db_plugin)) |
---|
93 | { |
---|
94 | array_push($errors, 'CANNOT ACTIVATE - NOT INSTALLED'); |
---|
95 | break; |
---|
96 | } |
---|
97 | if ($crt_db_plugin['state'] != 'inactive') |
---|
98 | { |
---|
99 | array_push($errors, 'invalid current state ' . $crt_db_plugin['state']); |
---|
100 | break; |
---|
101 | } |
---|
102 | if (file_exists($file_to_include)) |
---|
103 | { |
---|
104 | include_once($file_to_include); |
---|
105 | if (function_exists('plugin_activate')) |
---|
106 | { |
---|
107 | plugin_activate($plugin_id, $crt_db_plugin['version'], $errors); |
---|
108 | } |
---|
109 | } |
---|
110 | if (empty($errors)) |
---|
111 | { |
---|
112 | $query = ' |
---|
113 | UPDATE ' . PLUGINS_TABLE . ' SET state="active" WHERE id="' . $plugin_id . '"'; |
---|
114 | pwg_query($query); |
---|
115 | } |
---|
116 | break; |
---|
117 | |
---|
118 | case 'deactivate': |
---|
119 | if (!isset($crt_db_plugin)) |
---|
120 | { |
---|
121 | die ('CANNOT DEACTIVATE - NOT INSTALLED'); |
---|
122 | } |
---|
123 | if ($crt_db_plugin['state'] != 'active') |
---|
124 | { |
---|
125 | die('invalid current state ' . $crt_db_plugin['state']); |
---|
126 | } |
---|
127 | $query = ' |
---|
128 | UPDATE ' . PLUGINS_TABLE . ' SET state="inactive" WHERE id="' . $plugin_id . '"'; |
---|
129 | pwg_query($query); |
---|
130 | if (file_exists($file_to_include)) |
---|
131 | { |
---|
132 | include_once($file_to_include); |
---|
133 | if (function_exists('plugin_deactivate')) |
---|
134 | { |
---|
135 | plugin_deactivate($plugin_id); |
---|
136 | } |
---|
137 | } |
---|
138 | break; |
---|
139 | |
---|
140 | case 'uninstall': |
---|
141 | if (!isset($crt_db_plugin)) |
---|
142 | { |
---|
143 | die ('CANNOT UNINSTALL - NOT INSTALLED'); |
---|
144 | } |
---|
145 | $query = ' |
---|
146 | DELETE FROM ' . PLUGINS_TABLE . ' WHERE id="' . $plugin_id . '"'; |
---|
147 | pwg_query($query); |
---|
148 | if (file_exists($file_to_include)) |
---|
149 | { |
---|
150 | include_once($file_to_include); |
---|
151 | if (function_exists('plugin_uninstall')) |
---|
152 | { |
---|
153 | plugin_uninstall($plugin_id); |
---|
154 | } |
---|
155 | } |
---|
156 | break; |
---|
157 | |
---|
158 | case 'delete': |
---|
159 | if (!empty($crt_db_plugin)) |
---|
160 | { |
---|
161 | array_push($errors, 'CANNOT DELETE - PLUGIN IS INSTALLED'); |
---|
162 | break; |
---|
163 | } |
---|
164 | if (!isset($this->fs_plugins[$plugin_id])) |
---|
165 | { |
---|
166 | array_push($errors, 'CANNOT DELETE - NO SUCH PLUGIN'); |
---|
167 | break; |
---|
168 | } |
---|
169 | if (!$this->deltree(PHPWG_PLUGINS_PATH . $plugin_id)) |
---|
170 | { |
---|
171 | $this->send_to_trash(PHPWG_PLUGINS_PATH . $plugin_id); |
---|
172 | } |
---|
173 | break; |
---|
174 | } |
---|
175 | return $errors; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Get plugins defined in the plugin directory |
---|
180 | */ |
---|
181 | function get_fs_plugins() |
---|
182 | { |
---|
183 | $dir = opendir(PHPWG_PLUGINS_PATH); |
---|
184 | while ($file = readdir($dir)) |
---|
185 | { |
---|
186 | if ($file!='.' and $file!='..') |
---|
187 | { |
---|
188 | $path = PHPWG_PLUGINS_PATH.$file; |
---|
189 | if (is_dir($path) and !is_link($path) |
---|
190 | and preg_match('/^[a-zA-Z0-9-_]+$/', $file ) |
---|
191 | and file_exists($path.'/main.inc.php') |
---|
192 | ) |
---|
193 | { |
---|
194 | $plugin = array( |
---|
195 | 'name'=>$file, |
---|
196 | 'version'=>'0', |
---|
197 | 'uri'=>'', |
---|
198 | 'description'=>'', |
---|
199 | 'author'=>'', |
---|
200 | ); |
---|
201 | $plg_data = implode( '', file($path.'/main.inc.php') ); |
---|
202 | |
---|
203 | if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) ) |
---|
204 | { |
---|
205 | $plugin['name'] = trim( $val[1] ); |
---|
206 | } |
---|
207 | if (preg_match("|Version: (.*)|", $plg_data, $val)) |
---|
208 | { |
---|
209 | $plugin['version'] = trim($val[1]); |
---|
210 | } |
---|
211 | if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) ) |
---|
212 | { |
---|
213 | $plugin['uri'] = trim($val[1]); |
---|
214 | } |
---|
215 | if ( preg_match("|Description: (.*)|", $plg_data, $val) ) |
---|
216 | { |
---|
217 | $plugin['description'] = trim($val[1]); |
---|
218 | } |
---|
219 | if ( preg_match("|Author: (.*)|", $plg_data, $val) ) |
---|
220 | { |
---|
221 | $plugin['author'] = trim($val[1]); |
---|
222 | } |
---|
223 | if ( preg_match("|Author URI: (.*)|", $plg_data, $val) ) |
---|
224 | { |
---|
225 | $plugin['author uri'] = trim($val[1]); |
---|
226 | } |
---|
227 | if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid=')) |
---|
228 | { |
---|
229 | list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']); |
---|
230 | if (is_numeric($extension)) $plugin['extension'] = $extension; |
---|
231 | } |
---|
232 | // IMPORTANT SECURITY ! |
---|
233 | $plugin = array_map('htmlspecialchars', $plugin); |
---|
234 | $this->fs_plugins[$file] = $plugin; |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | closedir($dir); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Sort fs_plugins |
---|
243 | */ |
---|
244 | function sort_fs_plugins($order='name') |
---|
245 | { |
---|
246 | switch ($order) |
---|
247 | { |
---|
248 | case 'name': |
---|
249 | uasort($this->fs_plugins, 'name_compare'); |
---|
250 | break; |
---|
251 | case 'status': |
---|
252 | $this->sort_plugins_by_state(); |
---|
253 | break; |
---|
254 | case 'author': |
---|
255 | uasort($this->fs_plugins, array($this, 'plugin_author_compare')); |
---|
256 | break; |
---|
257 | case 'id': |
---|
258 | uksort($this->fs_plugins, 'strcasecmp'); |
---|
259 | break; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Retrieve PEM server datas to $server_plugins |
---|
265 | */ |
---|
266 | function get_server_plugins($new=false) |
---|
267 | { |
---|
268 | foreach($this->fs_plugins as $fs_plugin) |
---|
269 | { |
---|
270 | if (isset($fs_plugin['extension'])) |
---|
271 | { |
---|
272 | $plugins_to_check[] = $fs_plugin['extension']; |
---|
273 | } |
---|
274 | } |
---|
275 | $url = PEM_URL . '/uptodate.php?version=' . rawurlencode(PHPWG_VERSION) . '&extensions=' . implode(',', $plugins_to_check); |
---|
276 | $url .= $new ? '&newext=Plugin' : ''; |
---|
277 | |
---|
278 | if (!empty($plugins_to_check) and $source = @file_get_contents($url)) |
---|
279 | { |
---|
280 | $this->server_plugins = @unserialize($source); |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | $this->server_plugins = false; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * Sort $server_plugins |
---|
290 | */ |
---|
291 | function sort_server_plugins($order='date') |
---|
292 | { |
---|
293 | switch ($order) |
---|
294 | { |
---|
295 | case 'date': |
---|
296 | krsort($this->server_plugins); |
---|
297 | break; |
---|
298 | case 'name': |
---|
299 | uasort($this->server_plugins, array($this, 'extension_name_compare')); |
---|
300 | break; |
---|
301 | case 'author': |
---|
302 | uasort($this->server_plugins, array($this, 'extension_author_compare')); |
---|
303 | break; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * Extract plugin files from archive |
---|
309 | * @param string - install or upgrade |
---|
310 | * @param string - archive URL |
---|
311 | * @param string - destination path |
---|
312 | */ |
---|
313 | function extract_plugin_files($action, $source, $dest) |
---|
314 | { |
---|
315 | if ($archive = tempnam( PHPWG_PLUGINS_PATH, 'zip')) |
---|
316 | { |
---|
317 | if (@copy(PEM_URL . str_replace(' ', '%20', $source), $archive)) |
---|
318 | { |
---|
319 | include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); |
---|
320 | $zip = new PclZip($archive); |
---|
321 | if ($list = $zip->listContent()) |
---|
322 | { |
---|
323 | foreach ($list as $file) |
---|
324 | { |
---|
325 | // we search main.inc.php in archive |
---|
326 | if (basename($file['filename']) == 'main.inc.php' |
---|
327 | and (!isset($main_filepath) |
---|
328 | or strlen($file['filename']) < strlen($main_filepath))) |
---|
329 | { |
---|
330 | $main_filepath = $file['filename']; |
---|
331 | } |
---|
332 | } |
---|
333 | if (isset($main_filepath)) |
---|
334 | { |
---|
335 | $root = dirname($main_filepath); // main.inc.php path in archive |
---|
336 | if ($action == 'upgrade') |
---|
337 | { |
---|
338 | $extract_path = PHPWG_PLUGINS_PATH.$dest; |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | $extract_path = PHPWG_PLUGINS_PATH |
---|
343 | . ($root == '.' ? 'extension_' . $dest : basename($root)); |
---|
344 | } |
---|
345 | if($result = $zip->extract(PCLZIP_OPT_PATH, $extract_path, |
---|
346 | PCLZIP_OPT_REMOVE_PATH, $root, |
---|
347 | PCLZIP_OPT_REPLACE_NEWER)) |
---|
348 | { |
---|
349 | foreach ($result as $file) |
---|
350 | { |
---|
351 | if ($file['stored_filename'] == $main_filepath) |
---|
352 | { |
---|
353 | $status = $file['status']; |
---|
354 | break; |
---|
355 | } |
---|
356 | } |
---|
357 | } |
---|
358 | else $status = 'extract_error'; |
---|
359 | } |
---|
360 | else $status = 'archive_error'; |
---|
361 | } |
---|
362 | else $status = 'archive_error'; |
---|
363 | } |
---|
364 | else $status = 'dl_archive_error'; |
---|
365 | } |
---|
366 | else $status = 'temp_path_error'; |
---|
367 | |
---|
368 | @unlink($archive); |
---|
369 | return $status; |
---|
370 | } |
---|
371 | |
---|
372 | /** |
---|
373 | * delete $path directory |
---|
374 | * @param string - path |
---|
375 | */ |
---|
376 | function deltree($path) |
---|
377 | { |
---|
378 | if (is_dir($path)) |
---|
379 | { |
---|
380 | $fh = opendir($path); |
---|
381 | while ($file = readdir($fh)) |
---|
382 | { |
---|
383 | if ($file != '.' and $file != '..') |
---|
384 | { |
---|
385 | $pathfile = $path . '/' . $file; |
---|
386 | if (is_dir($pathfile)) |
---|
387 | { |
---|
388 | $this->deltree($pathfile); |
---|
389 | } |
---|
390 | else |
---|
391 | { |
---|
392 | @unlink($pathfile); |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | closedir($fh); |
---|
397 | return @rmdir($path); |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | /** |
---|
402 | * send $path to trash directory |
---|
403 | * @param string - path |
---|
404 | */ |
---|
405 | function send_to_trash($path) |
---|
406 | { |
---|
407 | $trash_path = PHPWG_PLUGINS_PATH . 'trash'; |
---|
408 | if (!is_dir($trash_path)) |
---|
409 | { |
---|
410 | @mkdir($trash_path); |
---|
411 | $file = @fopen($trash_path . '/.htaccess', 'w'); |
---|
412 | @fwrite($file, 'deny from all'); |
---|
413 | @fclose($file); |
---|
414 | } |
---|
415 | while ($r = $trash_path . '/' . md5(uniqid(rand(), true))) |
---|
416 | { |
---|
417 | if (!is_dir($r)) |
---|
418 | { |
---|
419 | @rename($path, $r); |
---|
420 | break; |
---|
421 | } |
---|
422 | } |
---|
423 | } |
---|
424 | |
---|
425 | /** |
---|
426 | * Sort functions |
---|
427 | */ |
---|
428 | function plugin_version_compare($a, $b) |
---|
429 | { |
---|
430 | $r = version_compare($a['version'], $b['version']); |
---|
431 | if ($r == 0) return strcasecmp($a['version'], $b['version']); |
---|
432 | else return $r; |
---|
433 | } |
---|
434 | |
---|
435 | function extension_name_compare($a, $b) |
---|
436 | { |
---|
437 | return strcmp(strtolower($a['ext_name']), strtolower($b['ext_name'])); |
---|
438 | } |
---|
439 | |
---|
440 | function extension_author_compare($a, $b) |
---|
441 | { |
---|
442 | $r = strcasecmp($a['author'], $b['author']); |
---|
443 | if ($r == 0) return $this->extension_name_compare($a, $b); |
---|
444 | else return $r; |
---|
445 | } |
---|
446 | |
---|
447 | function plugin_author_compare($a, $b) |
---|
448 | { |
---|
449 | $r = strcasecmp($a['author'], $b['author']); |
---|
450 | if ($r == 0) return name_compare($a, $b); |
---|
451 | else return $r; |
---|
452 | } |
---|
453 | |
---|
454 | function sort_plugins_by_state() |
---|
455 | { |
---|
456 | uasort($this->fs_plugins, 'name_compare'); |
---|
457 | |
---|
458 | $active_plugins = array(); |
---|
459 | $inactive_plugins = array(); |
---|
460 | $not_installed = array(); |
---|
461 | |
---|
462 | foreach($this->fs_plugins as $plugin_id => $plugin) |
---|
463 | { |
---|
464 | if (isset($this->db_plugins_by_id[$plugin_id])) |
---|
465 | { |
---|
466 | $this->db_plugins_by_id[$plugin_id]['state'] == 'active' ? |
---|
467 | $active_plugins[$plugin_id] = $plugin : $inactive_plugins[$plugin_id] = $plugin; |
---|
468 | } |
---|
469 | else |
---|
470 | { |
---|
471 | $not_installed[$plugin_id] = $plugin; |
---|
472 | } |
---|
473 | } |
---|
474 | $this->fs_plugins = $active_plugins + $inactive_plugins + $not_installed; |
---|
475 | } |
---|
476 | } |
---|
477 | ?> |
---|