1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | file : $Id: plugins.php 2216 2008-02-27 02:31:51Z rvelices $ |
---|
7 | // | last update : $Date: 2008-02-27 02:31:51 +0000 (Wed, 27 Feb 2008) $ |
---|
8 | // | last modifier : $Author: rvelices $ |
---|
9 | // | revision : $Revision: 2216 $ |
---|
10 | // +-----------------------------------------------------------------------+ |
---|
11 | // | This program is free software; you can redistribute it and/or modify | |
---|
12 | // | it under the terms of the GNU General Public License as published by | |
---|
13 | // | the Free Software Foundation | |
---|
14 | // | | |
---|
15 | // | This program is distributed in the hope that it will be useful, but | |
---|
16 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
17 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
18 | // | General Public License for more details. | |
---|
19 | // | | |
---|
20 | // | You should have received a copy of the GNU General Public License | |
---|
21 | // | along with this program; if not, write to the Free Software | |
---|
22 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
23 | // | USA. | |
---|
24 | // +-----------------------------------------------------------------------+ |
---|
25 | |
---|
26 | if( !defined("PHPWG_ROOT_PATH") ) |
---|
27 | { |
---|
28 | die ("Hacking attempt!"); |
---|
29 | } |
---|
30 | |
---|
31 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
32 | check_status(ACCESS_ADMINISTRATOR); |
---|
33 | |
---|
34 | $my_base_url = PHPWG_ROOT_PATH.'admin.php?page=plugins'; |
---|
35 | |
---|
36 | |
---|
37 | // +-----------------------------------------------------------------------+ |
---|
38 | // | perform requested actions | |
---|
39 | // +-----------------------------------------------------------------------+ |
---|
40 | if ( isset($_GET['action']) and isset($_GET['plugin']) and !is_adviser() ) |
---|
41 | { |
---|
42 | $plugin_id = $_GET['plugin']; |
---|
43 | $crt_db_plugin = get_db_plugins('', $plugin_id); |
---|
44 | if (!empty($crt_db_plugin)) |
---|
45 | { |
---|
46 | $crt_db_plugin=$crt_db_plugin[0]; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | unset($crt_db_plugin); |
---|
51 | } |
---|
52 | |
---|
53 | $errors = array(); |
---|
54 | $file_to_include = PHPWG_PLUGINS_PATH.$plugin_id.'/maintain.inc.php'; |
---|
55 | |
---|
56 | switch ( $_GET['action'] ) |
---|
57 | { |
---|
58 | case 'install': |
---|
59 | if ( !empty($crt_db_plugin)) |
---|
60 | { |
---|
61 | array_push($errors, 'CANNOT install - ALREADY INSTALLED'); |
---|
62 | break; |
---|
63 | } |
---|
64 | $fs_plugins = get_fs_plugins(); |
---|
65 | if ( !isset( $fs_plugins[$plugin_id] ) ) |
---|
66 | { |
---|
67 | array_push($errors, 'CANNOT install - NO SUCH PLUGIN'); |
---|
68 | break; |
---|
69 | } |
---|
70 | if ( file_exists($file_to_include) ) |
---|
71 | { |
---|
72 | include_once($file_to_include); |
---|
73 | if ( function_exists('plugin_install') ) |
---|
74 | { |
---|
75 | plugin_install($plugin_id, $fs_plugins[$plugin_id]['version'], $errors); |
---|
76 | } |
---|
77 | } |
---|
78 | if (empty($errors)) |
---|
79 | { |
---|
80 | $query = ' |
---|
81 | INSERT INTO '.PLUGINS_TABLE.' (id,version) VALUES ("' |
---|
82 | .$plugin_id.'","'.$fs_plugins[$plugin_id]['version'].'" |
---|
83 | )'; |
---|
84 | pwg_query($query); |
---|
85 | } |
---|
86 | break; |
---|
87 | |
---|
88 | case 'activate': |
---|
89 | if ( !isset($crt_db_plugin) ) |
---|
90 | { |
---|
91 | array_push($errors, 'CANNOT '. $_GET['action'] .' - NOT INSTALLED'); |
---|
92 | break; |
---|
93 | } |
---|
94 | if ($crt_db_plugin['state']!='inactive') |
---|
95 | { |
---|
96 | array_push($errors, 'invalid current state '.$crt_db_plugin['state']); |
---|
97 | break; |
---|
98 | } |
---|
99 | if ( file_exists($file_to_include) ) |
---|
100 | { |
---|
101 | include_once($file_to_include); |
---|
102 | if ( function_exists('plugin_activate') ) |
---|
103 | { |
---|
104 | plugin_activate($plugin_id, $crt_db_plugin['version'], $errors); |
---|
105 | } |
---|
106 | } |
---|
107 | if (empty($errors)) |
---|
108 | { |
---|
109 | $query = ' |
---|
110 | UPDATE '.PLUGINS_TABLE.' SET state="active" WHERE id="'.$plugin_id.'"'; |
---|
111 | pwg_query($query); |
---|
112 | } |
---|
113 | break; |
---|
114 | |
---|
115 | case 'deactivate': |
---|
116 | if ( !isset($crt_db_plugin) ) |
---|
117 | { |
---|
118 | die ('CANNOT '. $_GET['action'] .' - NOT INSTALLED'); |
---|
119 | } |
---|
120 | if ($crt_db_plugin['state']!='active') |
---|
121 | { |
---|
122 | die('invalid current state '.$crt_db_plugin['state']); |
---|
123 | } |
---|
124 | $query = ' |
---|
125 | UPDATE '.PLUGINS_TABLE.' SET state="inactive" WHERE id="'.$plugin_id.'"'; |
---|
126 | pwg_query($query); |
---|
127 | |
---|
128 | @include_once($file_to_include); |
---|
129 | if ( function_exists('plugin_deactivate') ) |
---|
130 | { |
---|
131 | plugin_deactivate($plugin_id); |
---|
132 | } |
---|
133 | break; |
---|
134 | |
---|
135 | case 'uninstall': |
---|
136 | if ( !isset($crt_db_plugin) ) |
---|
137 | { |
---|
138 | die ('CANNOT '. $_GET['action'] .' - NOT INSTALLED'); |
---|
139 | } |
---|
140 | $query = ' |
---|
141 | DELETE FROM '.PLUGINS_TABLE.' WHERE id="'.$plugin_id.'"'; |
---|
142 | pwg_query($query); |
---|
143 | |
---|
144 | @include_once($file_to_include); |
---|
145 | if ( function_exists('plugin_uninstall') ) |
---|
146 | { |
---|
147 | plugin_uninstall($plugin_id); |
---|
148 | } |
---|
149 | break; |
---|
150 | } |
---|
151 | if (empty($errors)) |
---|
152 | { |
---|
153 | // do the redirection so that we allow the plugins to load/unload |
---|
154 | redirect($my_base_url); |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | $page['errors'] = array_merge($page['errors'], $errors); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | // +-----------------------------------------------------------------------+ |
---|
164 | // | start template output | |
---|
165 | // +-----------------------------------------------------------------------+ |
---|
166 | $fs_plugins = get_fs_plugins(); |
---|
167 | uasort($fs_plugins, 'name_compare'); |
---|
168 | $db_plugins = get_db_plugins(); |
---|
169 | $db_plugins_by_id=array(); |
---|
170 | foreach ($db_plugins as $db_plugin) |
---|
171 | { |
---|
172 | $db_plugins_by_id[$db_plugin['id']] = $db_plugin; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | $template->set_filenames(array('plugins' => 'admin/plugins.tpl')); |
---|
177 | |
---|
178 | $num=0; |
---|
179 | foreach( $fs_plugins as $plugin_id => $fs_plugin ) |
---|
180 | { |
---|
181 | $display_name = $fs_plugin['name']; |
---|
182 | if ( !empty($fs_plugin['uri']) ) |
---|
183 | { |
---|
184 | $display_name='<a href="'.$fs_plugin['uri'].'">'.$display_name.'</a>'; |
---|
185 | } |
---|
186 | $desc = $fs_plugin['description']; |
---|
187 | if (!empty($fs_plugin['author'])) |
---|
188 | { |
---|
189 | $desc.= ' (<em>'; |
---|
190 | if (!empty($fs_plugin['author uri'])) |
---|
191 | { |
---|
192 | $desc.= '<a href="'.$fs_plugin['author uri'].'">'.$fs_plugin['author'].'</a>'; |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | $desc.= $fs_plugin['author']; |
---|
197 | } |
---|
198 | $desc.= '</em>)'; |
---|
199 | } |
---|
200 | |
---|
201 | $tpl_plugin = |
---|
202 | array( |
---|
203 | 'NAME' => $display_name, |
---|
204 | 'VERSION' => $fs_plugin['version'], |
---|
205 | 'DESCRIPTION' => $desc, |
---|
206 | 'actions' => array(), |
---|
207 | ); |
---|
208 | |
---|
209 | |
---|
210 | $action_url = $my_base_url.'&plugin='.$plugin_id; |
---|
211 | if ( isset($db_plugins_by_id[$plugin_id]) ) |
---|
212 | { // already in the database |
---|
213 | // MAYBE TODO HERE: check for the version and propose upgrade action |
---|
214 | switch ($db_plugins_by_id[$plugin_id]['state']) |
---|
215 | { |
---|
216 | case 'active': |
---|
217 | $tpl_plugin['actions'][] = |
---|
218 | array( |
---|
219 | 'U_ACTION' => $action_url . '&action=deactivate', |
---|
220 | 'L_ACTION' => l10n('Deactivate'), |
---|
221 | ); |
---|
222 | break; |
---|
223 | case 'inactive': |
---|
224 | $tpl_plugin['actions'][] = |
---|
225 | array( |
---|
226 | 'U_ACTION' => $action_url . '&action=activate', |
---|
227 | 'L_ACTION' => l10n('Activate'), |
---|
228 | ); |
---|
229 | $tpl_plugin['actions'][] = |
---|
230 | array( |
---|
231 | 'U_ACTION' => $action_url . '&action=uninstall', |
---|
232 | 'L_ACTION' => l10n('Uninstall'), |
---|
233 | 'CONFIRM' => true, |
---|
234 | ); |
---|
235 | break; |
---|
236 | } |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | $tpl_plugin['actions'][] = |
---|
241 | array( |
---|
242 | 'U_ACTION' => $action_url . '&action=install', |
---|
243 | 'L_ACTION' => l10n('Install'), |
---|
244 | 'CONFIRM' => true, |
---|
245 | ); |
---|
246 | } |
---|
247 | $template->append('plugins', $tpl_plugin); |
---|
248 | } |
---|
249 | |
---|
250 | $missing_plugin_ids = array_diff( |
---|
251 | array_keys($db_plugins_by_id), array_keys($fs_plugins) |
---|
252 | ); |
---|
253 | foreach( $missing_plugin_ids as $plugin_id ) |
---|
254 | { |
---|
255 | $action_url = $my_base_url.'&plugin='.$plugin_id; |
---|
256 | |
---|
257 | $template->append( 'plugins', |
---|
258 | array( |
---|
259 | 'NAME' => $plugin_id, |
---|
260 | 'VERSION' => $db_plugins_by_id[$plugin_id]['version'], |
---|
261 | 'DESCRIPTION' => "ERROR: THIS PLUGIN IS MISSING BUT IT IS INSTALLED! UNINSTALL IT NOW !", |
---|
262 | 'actions' => array ( array ( |
---|
263 | 'U_ACTION' => $action_url . '&action=uninstall', |
---|
264 | 'L_ACTION' => l10n('Uninstall'), |
---|
265 | ) ) |
---|
266 | ) |
---|
267 | ); |
---|
268 | } |
---|
269 | |
---|
270 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugins'); |
---|
271 | ?> |
---|