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