source: trunk/admin/plugins.php @ 1738

Last change on this file since 1738 was 1716, checked in by rvelices, 17 years ago

plugins improvements: allow plugins to fail the installation/activation
comments.php improvements:

  • no more double sql escaping on author & keyword (once in common.inc.php and

once in comments.php)

  • now can search comment content on all special char ( ', ", <, >, & )
  • author & keyword are correctly redisplayed in browser when they are MySql

escaped

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | branch        : BSF (Best So Far)
7// | file          : $Id: plugins.php 1716 2007-01-11 23:15:26Z rvelices $
8// | last update   : $Date: 2007-01-11 23:15:26 +0000 (Thu, 11 Jan 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1716 $
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
27if( !defined("PHPWG_ROOT_PATH") )
28{
29  die ("Hacking attempt!");
30}
31
32include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
33check_status(ACCESS_ADMINISTRATOR);
34
35$my_base_url = PHPWG_ROOT_PATH.'admin.php?page=plugins';
36
37
38// +-----------------------------------------------------------------------+
39// |                     perform requested actions                         |
40// +-----------------------------------------------------------------------+
41if ( isset($_REQUEST['action']) and isset($_REQUEST['plugin'])  )
42{
43  $plugin_id = $_REQUEST['plugin'];
44  $crt_db_plugin = get_db_plugins('', $plugin_id);
45  if (!empty($crt_db_plugin))
46  {
47    $crt_db_plugin=$crt_db_plugin[0];
48  }
49  else
50  {
51    unset($crt_db_plugin);
52  }
53
54  $errors = array();
55  $file_to_include = PHPWG_PLUGINS_PATH.$plugin_id.'/maintain.inc.php';
56
57  switch ( $_REQUEST['action'] )
58  {
59    case 'install':
60      if ( !empty($crt_db_plugin))
61      {
62        array_push($errors, 'CANNOT install - ALREADY INSTALLED');
63        break;
64      }
65      $fs_plugins = get_fs_plugins();
66      if ( !isset( $fs_plugins[$plugin_id] ) )
67      {
68        array_push($errors, 'CANNOT install - NO SUCH PLUGIN');
69        break;
70      }
71      if ( file_exists($file_to_include) )
72      {
73        include_once($file_to_include);
74        if ( function_exists('plugin_install') )
75        {
76          plugin_install($plugin_id, $fs_plugins[$plugin_id]['version'], $errors);
77        }
78      }
79      if (empty($errors))
80      {
81        $query = '
82INSERT INTO '.PLUGINS_TABLE.' (id,version) VALUES ("'
83.$plugin_id.'","'.$fs_plugins[$plugin_id]['version'].'"
84)';
85        pwg_query($query);
86      }
87      break;
88
89    case 'activate':
90      if ( !isset($crt_db_plugin) )
91      {
92        array_push($errors, 'CANNOT '. $_REQUEST['action'] .' - NOT INSTALLED');
93      }
94      if ($crt_db_plugin['state']!='inactive')
95      {
96        array_push($errors, 'invalid current state '.$crt_db_plugin['state']);
97      }
98      if ( file_exists($file_to_include) )
99      {
100        include_once($file_to_include);
101        if ( function_exists('plugin_activate') )
102        {
103          plugin_activate($plugin_id, $crt_db_plugin['version'], $errors);
104        }
105      }
106      if (empty($errors))
107      {
108        $query = '
109UPDATE '.PLUGINS_TABLE.' SET state="active" WHERE id="'.$plugin_id.'"';
110        pwg_query($query);
111      }
112      break;
113
114    case 'deactivate':
115      if ( !isset($crt_db_plugin) )
116      {
117        die ('CANNOT '. $_REQUEST['action'] .' - NOT INSTALLED');
118      }
119      if ($crt_db_plugin['state']!='active')
120      {
121        die('invalid current state '.$crt_db_plugin['state']);
122      }
123      $query = '
124UPDATE '.PLUGINS_TABLE.' SET state="inactive" WHERE id="'.$plugin_id.'"';
125      pwg_query($query);
126
127      @include_once($file_to_include);
128      if ( function_exists('plugin_deactivate') )
129      {
130        plugin_deactivate($plugin_id);
131      }
132      break;
133
134    case 'uninstall':
135      if ( !isset($crt_db_plugin) )
136      {
137        die ('CANNOT '. $_REQUEST['action'] .' - NOT INSTALLED');
138      }
139      $query = '
140DELETE FROM '.PLUGINS_TABLE.' WHERE id="'.$plugin_id.'"';
141      pwg_query($query);
142
143      @include_once($file_to_include);
144      if ( function_exists('plugin_uninstall') )
145      {
146        plugin_uninstall($plugin_id);
147      }
148      break;
149  }
150  if (empty($errors))
151  {
152    // do the redirection so that we allow the plugins to load/unload
153    redirect($my_base_url);
154  }
155  else
156  {
157    $page['errors'] = array_merge($page['errors'], $errors);
158  }
159}
160
161
162// +-----------------------------------------------------------------------+
163// |                     start template output                             |
164// +-----------------------------------------------------------------------+
165$fs_plugins = get_fs_plugins();
166$db_plugins = get_db_plugins();
167$db_plugins_by_id=array();
168foreach ($db_plugins as $db_plugin)
169{
170  $db_plugins_by_id[$db_plugin['id']] = $db_plugin;
171}
172
173
174$template->set_filenames(array('plugins' => 'admin/plugins.tpl'));
175
176$num=0;
177foreach( $fs_plugins as $plugin_id => $fs_plugin )
178{
179  $display_name = $fs_plugin['name'];
180  if ( !empty($fs_plugin['uri']) )
181  {
182    $display_name='<a href="'.$fs_plugin['uri'].'">'.$display_name.'</a>';
183  }
184  $template->assign_block_vars( 'plugins.plugin',
185      array(
186        'NAME' => $display_name,
187        'VERSION' => $fs_plugin['version'],
188        'DESCRIPTION' => $fs_plugin['description'],
189        'CLASS' => ($num++ % 2 == 1) ? 'row2' : 'row1',
190        )
191     );
192
193
194  $action_url = $my_base_url.'&amp;plugin='.$plugin_id;
195  if ( isset($db_plugins_by_id[$plugin_id]) )
196  { // already in the database
197    // MAYBE TODO HERE: check for the version and propose upgrade action
198    switch ($db_plugins_by_id[$plugin_id]['state'])
199    {
200      case 'active':
201        $template->assign_block_vars( 'plugins.plugin.action',
202            array(
203              'U_ACTION' => $action_url . '&amp;action=deactivate',
204              'L_ACTION' => l10n('Deactivate'),
205            )
206          );
207        break;
208      case 'inactive':
209        $template->assign_block_vars( 'plugins.plugin.action',
210            array(
211              'U_ACTION' => $action_url . '&amp;action=activate',
212              'L_ACTION' => l10n('Activate'),
213            )
214          );
215        $template->assign_block_vars( 'plugins.plugin.action',
216            array(
217              'U_ACTION' => $action_url . '&amp;action=uninstall',
218              'L_ACTION' => l10n('Uninstall'),
219            )
220          );
221        break;
222    }
223  }
224  else
225  {
226    $template->assign_block_vars( 'plugins.plugin.action',
227        array(
228          'U_ACTION' => $action_url . '&amp;action=install',
229          'L_ACTION' => l10n('Install'),
230        )
231      );
232  }
233}
234
235$missing_plugin_ids = array_diff(
236    array_keys($db_plugins_by_id), array_keys($fs_plugins)
237  );
238foreach( $missing_plugin_ids as $plugin_id )
239{
240  $template->assign_block_vars( 'plugins.plugin',
241      array(
242        'NAME' => $plugin_id,
243        'VERSION' => $db_plugins_by_id[$plugin_id]['version'],
244        'DESCRIPTION' => "ERROR: THIS PLUGIN IS MISSING BUT IT IS INSTALLED! UNINSTALL IT NOW !",
245        'CLASS' => ($num++ % 2 == 1) ? 'row2' : 'row1',
246        )
247     );
248   $action_url = $my_base_url.'&amp;plugin='.$plugin_id;
249        $template->assign_block_vars( 'plugins.plugin.action',
250            array(
251              'U_ACTION' => $action_url . '&amp;action=uninstall',
252              'L_ACTION' => l10n('Uninstall'),
253            )
254          );
255
256}
257
258$template->assign_var_from_handle('ADMIN_CONTENT', 'plugins');
259?>
Note: See TracBrowser for help on using the repository browser.