source: tags/release-1_7_0/admin/plugins.php @ 8497

Last change on this file since 8497 was 1995, checked in by rvelices, 17 years ago

merge r1994 from trunk to branch-1_7
check for is_adviser in plugin admin page

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
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 1995 2007-05-02 03:11:16Z rvelices $
7// | last update   : $Date: 2007-05-02 03:11:16 +0000 (Wed, 02 May 2007) $
8// | last modifier : $Author: rvelices $
9// | revision      : $Revision: 1995 $
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
26if( !defined("PHPWG_ROOT_PATH") )
27{
28  die ("Hacking attempt!");
29}
30
31include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
32check_status(ACCESS_ADMINISTRATOR);
33
34$my_base_url = PHPWG_ROOT_PATH.'admin.php?page=plugins';
35
36
37// +-----------------------------------------------------------------------+
38// |                     perform requested actions                         |
39// +-----------------------------------------------------------------------+
40if ( 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 = '
81INSERT 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 = '
110UPDATE '.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 = '
125UPDATE '.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 = '
141DELETE 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$db_plugins = get_db_plugins();
168$db_plugins_by_id=array();
169foreach ($db_plugins as $db_plugin)
170{
171  $db_plugins_by_id[$db_plugin['id']] = $db_plugin;
172}
173
174
175$template->set_filenames(array('plugins' => 'admin/plugins.tpl'));
176
177$num=0;
178foreach( $fs_plugins as $plugin_id => $fs_plugin )
179{
180  $display_name = $fs_plugin['name'];
181  if ( !empty($fs_plugin['uri']) )
182  {
183    $display_name='<a href="'.$fs_plugin['uri'].'">'.$display_name.'</a>';
184  }
185  $desc = $fs_plugin['description'];
186  if (!empty($fs_plugin['author']))
187  {
188    $desc.= ' (<em>';
189    if (!empty($fs_plugin['author uri']))
190    {
191      $desc.= '<a href="'.$fs_plugin['author uri'].'">'.$fs_plugin['author'].'</a>';
192    }
193    else
194    {
195      $desc.= $fs_plugin['author'];
196    }
197    $desc.= '</em>)';
198  }
199  $template->assign_block_vars( 'plugins.plugin',
200      array(
201        'NAME' => $display_name,
202        'VERSION' => $fs_plugin['version'],
203        'DESCRIPTION' => $desc,
204        'CLASS' => ($num++ % 2 == 1) ? 'row2' : 'row1',
205        )
206     );
207
208
209  $action_url = $my_base_url.'&amp;plugin='.$plugin_id;
210  if ( isset($db_plugins_by_id[$plugin_id]) )
211  { // already in the database
212    // MAYBE TODO HERE: check for the version and propose upgrade action
213    switch ($db_plugins_by_id[$plugin_id]['state'])
214    {
215      case 'active':
216        $template->assign_block_vars( 'plugins.plugin.action',
217            array(
218              'U_ACTION' => $action_url . '&amp;action=deactivate',
219              'L_ACTION' => l10n('Deactivate'),
220            )
221          );
222        break;
223      case 'inactive':
224        $template->assign_block_vars( 'plugins.plugin.action',
225            array(
226              'U_ACTION' => $action_url . '&amp;action=activate',
227              'L_ACTION' => l10n('Activate'),
228            )
229          );
230        $template->assign_block_vars( 'plugins.plugin.action',
231            array(
232              'U_ACTION' => $action_url . '&amp;action=uninstall',
233              'L_ACTION' => l10n('Uninstall'),
234            )
235          );
236        $template->assign_block_vars( 'plugins.plugin.action.confirm', array());
237        break;
238    }
239  }
240  else
241  {
242    $template->assign_block_vars( 'plugins.plugin.action',
243        array(
244          'U_ACTION' => $action_url . '&amp;action=install',
245          'L_ACTION' => l10n('Install'),
246        )
247      );
248    $template->assign_block_vars( 'plugins.plugin.action.confirm', array());
249  }
250}
251
252$missing_plugin_ids = array_diff(
253    array_keys($db_plugins_by_id), array_keys($fs_plugins)
254  );
255foreach( $missing_plugin_ids as $plugin_id )
256{
257  $template->assign_block_vars( 'plugins.plugin',
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        'CLASS' => ($num++ % 2 == 1) ? 'row2' : 'row1',
263        )
264     );
265   $action_url = $my_base_url.'&amp;plugin='.$plugin_id;
266        $template->assign_block_vars( 'plugins.plugin.action',
267            array(
268              'U_ACTION' => $action_url . '&amp;action=uninstall',
269              'L_ACTION' => l10n('Uninstall'),
270            )
271          );
272
273}
274
275$template->assign_var_from_handle('ADMIN_CONTENT', 'plugins');
276?>
Note: See TracBrowser for help on using the repository browser.