source: tags/release-1_7_2/admin/plugins.php @ 26010

Last change on this file since 26010 was 2143, checked in by rvelices, 17 years ago

feature 765: Sort alphabetically the admin plugin menus and plugin list

  • 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 2143 2007-10-19 02:04:24Z rvelices $
7// | last update   : $Date: 2007-10-19 02:04:24 +0000 (Fri, 19 Oct 2007) $
8// | last modifier : $Author: rvelices $
9// | revision      : $Revision: 2143 $
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();
167uasort($fs_plugins, 'name_compare');
168$db_plugins = get_db_plugins();
169$db_plugins_by_id=array();
170foreach ($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;
179foreach( $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  $template->assign_block_vars( 'plugins.plugin',
201      array(
202        'NAME' => $display_name,
203        'VERSION' => $fs_plugin['version'],
204        'DESCRIPTION' => $desc,
205        'CLASS' => ($num++ % 2 == 1) ? 'row2' : 'row1',
206        )
207     );
208
209
210  $action_url = $my_base_url.'&amp;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        $template->assign_block_vars( 'plugins.plugin.action',
218            array(
219              'U_ACTION' => $action_url . '&amp;action=deactivate',
220              'L_ACTION' => l10n('Deactivate'),
221            )
222          );
223        break;
224      case 'inactive':
225        $template->assign_block_vars( 'plugins.plugin.action',
226            array(
227              'U_ACTION' => $action_url . '&amp;action=activate',
228              'L_ACTION' => l10n('Activate'),
229            )
230          );
231        $template->assign_block_vars( 'plugins.plugin.action',
232            array(
233              'U_ACTION' => $action_url . '&amp;action=uninstall',
234              'L_ACTION' => l10n('Uninstall'),
235            )
236          );
237        $template->assign_block_vars( 'plugins.plugin.action.confirm', array());
238        break;
239    }
240  }
241  else
242  {
243    $template->assign_block_vars( 'plugins.plugin.action',
244        array(
245          'U_ACTION' => $action_url . '&amp;action=install',
246          'L_ACTION' => l10n('Install'),
247        )
248      );
249    $template->assign_block_vars( 'plugins.plugin.action.confirm', array());
250  }
251}
252
253$missing_plugin_ids = array_diff(
254    array_keys($db_plugins_by_id), array_keys($fs_plugins)
255  );
256foreach( $missing_plugin_ids as $plugin_id )
257{
258  $template->assign_block_vars( 'plugins.plugin',
259      array(
260        'NAME' => $plugin_id,
261        'VERSION' => $db_plugins_by_id[$plugin_id]['version'],
262        'DESCRIPTION' => "ERROR: THIS PLUGIN IS MISSING BUT IT IS INSTALLED! UNINSTALL IT NOW !",
263        'CLASS' => ($num++ % 2 == 1) ? 'row2' : 'row1',
264        )
265     );
266   $action_url = $my_base_url.'&amp;plugin='.$plugin_id;
267        $template->assign_block_vars( 'plugins.plugin.action',
268            array(
269              'U_ACTION' => $action_url . '&amp;action=uninstall',
270              'L_ACTION' => l10n('Uninstall'),
271            )
272          );
273
274}
275
276$template->assign_var_from_handle('ADMIN_CONTENT', 'plugins');
277?>
Note: See TracBrowser for help on using the repository browser.