From 087b3a7fd4d81528ea23786bb2aaa707b3b4de17 Mon Sep 17 00:00:00 2001
From: chris <chris@jalakai.co.uk>
Date: Sat, 11 Mar 2006 20:26:55 +0100
Subject: [PATCH] plugin manager upate: protect default plugins, add
 enable/disable functionality

darcs-hash:20060311192655-9b6ab-c54d280d35b121730e2f8d50b15fe647d986574c.gz
---
 inc/pluginutils.php                 |  24 +++++-
 lib/plugins/plugin/admin.php        | 119 +++++++++++++++++++++-------
 lib/plugins/plugin/lang/en/lang.php |   1 +
 lib/plugins/plugin/style.css        |  15 ++--
 4 files changed, 122 insertions(+), 37 deletions(-)

diff --git a/inc/pluginutils.php b/inc/pluginutils.php
index 1a60d60be..d9cc848fe 100644
--- a/inc/pluginutils.php
+++ b/inc/pluginutils.php
@@ -5,21 +5,33 @@
  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
-
-
+ 
+// plugin related constants
+$plugin_types = array('admin','syntax');
+ 
 /**
  * Returns a list of available plugins of given type
  *
- * Returns all plugins if no type given
+ * @param $type  string, plugin_type name; 
+ *               the type of plugin to return, 
+ *               use empty string for all types
+ * @param $all   bool; 
+ *               false to only return enabled plugins,
+ *               true to return both enabled and disabled plugins
+ *
+ * @return       array of plugin names
  *
  * @author Andreas Gohr <andi@splitbrain.org>
  */
-function plugin_list($type=''){
+function plugin_list($type='',$all=false){
   $plugins = array();
   if ($dh = opendir(DOKU_PLUGIN)) {
     while (false !== ($plugin = readdir($dh))) {
       if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
       if (is_file(DOKU_PLUGIN.$plugin)) continue;
+			
+			// if required, skip disabled plugins
+			if (!$all && plugin_isdisabled($plugin)) continue;
 
       if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
           $plugins[] = $plugin;
@@ -76,3 +88,7 @@ function &plugin_load($type,$name){
   $DOKU_PLUGINS[$type][$name] = new $class;
   return $DOKU_PLUGINS[$type][$name];
 }
+
+function plugin_isdisabled($name) { return @file_exists(DOKU_PLUGIN.$name.'/disabled'); }
+function plugin_enable($name) { return @unlink(DOKU_PLUGIN.$name.'/disabled'); }
+function plugin_disable($name) { return @touch(DOKU_PLUGIN.$name.'/disabled'); }
diff --git a/lib/plugins/plugin/admin.php b/lib/plugins/plugin/admin.php
index 69ff9da3d..8c332d4ed 100644
--- a/lib/plugins/plugin/admin.php
+++ b/lib/plugins/plugin/admin.php
@@ -17,9 +17,13 @@ require_once(DOKU_PLUGIN.'admin.php');
 
     //--------------------------[ GLOBALS ]------------------------------------------------
     // note: probably should be dokuwiki wide globals, where they can be accessed by pluginutils.php
-    global $common_plugin_types;
-    $common_plugin_types = array('syntax', 'admin');
-
+    // global $plugin_types;
+    // $plugin_types = array('syntax', 'admin');
+		
+		// plugins that are an integral part of dokuwiki, they shouldn't be disabled or deleted
+		global $plugin_protected;
+		$plugin_protected = array('acl','plugin','config','info','usermanager');
+		
 /**
  * All DokuWiki plugins to extend the admin function
  * need to inherit from this class
@@ -32,7 +36,7 @@ class admin_plugin_plugin extends DokuWiki_Admin_Plugin {
     var $handler = NULL;
 
     var $functions = array('delete','update',/*'settings',*/'info');  // require a plugin name
-    var $commands = array('manage','download');                       // don't require a plugin name
+    var $commands = array('manage','download','enable');              // don't require a plugin name
     var $plugin_list = array();
 
     var $msg = '';
@@ -87,11 +91,20 @@ class admin_plugin_plugin extends DokuWiki_Admin_Plugin {
       // enable direct access to language strings
       $this->setupLocale();
 
-      $this->plugin = $_REQUEST['plugin'];
-      $this->cmd = $_REQUEST['fn'];
-      if (is_array($this->cmd)) $this->cmd = key($this->cmd);
+//      $this->plugin = $_REQUEST['plugin'];
+//      $this->cmd = $_REQUEST['fn'];
+//      if (is_array($this->cmd)) $this->cmd = key($this->cmd);
+
+      $fn = $_REQUEST['fn'];
+      if (is_array($fn)) {
+          $this->cmd = key($fn);
+          $this->plugin = is_array($fn[$this->cmd]) ? key($fn[$this->cmd]) : null;
+      } else {
+          $this->cmd = $fn;
+          $this->plugin = null;
+      }
 
-      sort($this->plugin_list = plugin_list());
+      sort($this->plugin_list = plugin_list('', true));
 
       // verify $_REQUEST vars
       if (in_array($this->cmd, $this->commands)) {
@@ -120,7 +133,7 @@ class admin_plugin_plugin extends DokuWiki_Admin_Plugin {
       $this->setupLocale();
 
       if ($this->handler === NULL) $this->handler = & new ap_manage($this, $this->plugin);
-      if (!$this->plugin_list) sort($this->plugin_list = plugin_list());
+      if (!$this->plugin_list) sort($this->plugin_list = plugin_list('',true));
 
       ptln('<div id="plugin__manager">');
       $this->handler->html();
@@ -174,9 +187,23 @@ class ap_manage {
 
           if ($listPlugins) {
             ptln('<h2>'.$this->lang['manage'].'</h2>');
-            ptln('<div class="plugins">');
+						
+            ptln('<form action="'.wl($ID).'" method="post" class="plugins">');
+//            ptln('  <div class="plugins">');
+						
+						ptln('  <fieldset class="buttons">');
+            ptln('    <input type="hidden" name="do"     value="admin" />');
+            ptln('    <input type="hidden" name="page"   value="plugin" />');
+						ptln('  </fieldset>');
+						
             $this->html_pluginlist();
-            ptln('</div>');
+						
+						ptln('  <fieldset class="buttons">');
+						ptln('    <input type="submit" class="button" name="fn[enable]" value="'.$this->lang['btn_enable'].'" />');
+						ptln('  </fieldset>');
+						
+//            ptln('  </div>');
+						ptln('</form>');
           }
 
           ptln('</div>');
@@ -184,34 +211,43 @@ class ap_manage {
 
         function html_pluginlist() {
           global $ID;
+					global $plugin_protected;
 
           foreach ($this->manager->plugin_list as $plugin) {
 
-            $new = (in_array($plugin, $this->downloaded)) ? ' class="new"' : '';
-
-            ptln('  <form action="'.wl($ID).'" method="post" '.$new.'>');
-            ptln('    <fieldset>');
+						$disabled = plugin_isdisabled($plugin);
+						$protected = in_array($plugin,$plugin_protected);
+						
+						$checked = ($disabled) ? '' : ' checked="checked"';
+						$check_disabled = ($protected) ? ' disabled="disabled"' : '';
+						
+						// determine display class(es)
+						$class = array();
+						if (in_array($plugin, $this->downloaded)) $class[] = 'new';
+						if ($disabled) $class[] = 'disabled';
+						if ($protected) $class[] = 'protected';
+						
+						$class = count($class) ? ' class="'.join(' ', $class).'"' : '';
+
+            ptln('    <fieldset'.$class.'>');
             ptln('      <legend>'.$plugin.'</legend>');
+						ptln('      <input type="checkbox" class="enable" name="enabled[]" value="'.$plugin.'"'.$checked.$check_disabled.' />');
             ptln('      <h3 class="legend">'.$plugin.'</h3>');
-            ptln('      <input type="hidden" name="do"     value="admin" />');
-            ptln('      <input type="hidden" name="page"   value="plugin" />');
-            ptln('      <input type="hidden" name="plugin" value="'.$plugin.'" />');
 
-            $this->html_button('info', false, 6);
+            $this->html_button($plugin, 'info', false, 6);
             if (in_array('settings', $this->manager->functions)) {
-              $this->html_button('settings', !@file_exists(DOKU_PLUGIN.$plugin.'/settings.php'), 6);
+              $this->html_button($plugin, 'settings', !@file_exists(DOKU_PLUGIN.$plugin.'/settings.php'), 6);
             }
-            $this->html_button('update', !$this->plugin_readlog($plugin, 'url'), 6);
-            $this->html_button('delete', false, 6);
+            $this->html_button($plugin, 'update', !$this->plugin_readlog($plugin, 'url'), 6);						
+            $this->html_button($plugin, 'delete', $protected, 6);
 
             ptln('    </fieldset>');
-            ptln('  </form>');
             }
         }
 
-        function html_button($btn, $disabled=false, $indent=0) {
+        function html_button($plugin, $btn, $disabled=false, $indent=0) {
             $disabled = ($disabled) ? 'disabled="disabled"' : '';
-            ptln('<input type="submit" class="button" '.$disabled.' name="fn['.$btn.']" value="'.$this->lang['btn_'.$btn].'" />',$indent);
+            ptln('<input type="submit" class="button" '.$disabled.' name="fn['.$btn.']['.$plugin.']" value="'.$this->lang['btn_'.$btn].'" />',$indent);
         }
 
         /**
@@ -219,7 +255,7 @@ class ap_manage {
          */
         function refresh() {
 
-            sort($this->manager->plugin_list = plugin_list());
+            sort($this->manager->plugin_list = plugin_list('',true));
 
             // update latest plugin date - FIXME
             return (!$this->manager->error);
@@ -531,6 +567,33 @@ class ap_manage {
         }
     }
     class ap_settings extends ap_manage {}
+		
+		class ap_enable extends ap_manage {
+		
+		  var $enabled = array();
+		
+		  function process() {
+			  global $plugin_protected;
+				
+				$this->enabled = $_REQUEST['enabled'];
+				
+				foreach ($this->manager->plugin_list as $plugin) {
+				  if (in_array($plugin, $plugin_protected)) continue;
+					
+					$new = in_array($plugin, $this->enabled);
+					$old = !plugin_isdisabled($plugin);
+					
+					if ($new != $old) {
+						switch ($new) {
+						  // enable plugin
+							case true : plugin_enable($plugin); break;
+							case false: plugin_disable($plugin); break;
+						}
+					}		
+				}
+			}
+			
+		}
 
     //--------------[ utilities ]-----------------------------------
 
@@ -641,12 +704,12 @@ class ap_manage {
     // can this move to pluginutils?
     function ap_plugin_components($plugin) {
 
-      global $common_plugin_types;
+      global $plugin_types;
 
       $components = array();
       $path = DOKU_PLUGIN.$plugin.'/';
 
-      foreach ($common_plugin_types as $type) {
+      foreach ($plugin_types as $type) {
         if (file_exists($path.$type.'.php')) { $components[] = array('name'=>$plugin, 'type'=>$type); continue; }
 
         if ($dh = @opendir($path.$type.'/')) {
diff --git a/lib/plugins/plugin/lang/en/lang.php b/lib/plugins/plugin/lang/en/lang.php
index 11bd4479e..a19c10102 100644
--- a/lib/plugins/plugin/lang/en/lang.php
+++ b/lib/plugins/plugin/lang/en/lang.php
@@ -17,6 +17,7 @@ $lang['btn_update'] = 'update';
 $lang['btn_delete'] = 'delete';
 $lang['btn_settings'] = 'settings';
 $lang['btn_download'] = 'Download';
+$lang['btn_enable'] = 'Save';
 
 $lang['url']              = 'URL';
 
diff --git a/lib/plugins/plugin/style.css b/lib/plugins/plugin/style.css
index 9c2fde692..63f2408e3 100644
--- a/lib/plugins/plugin/style.css
+++ b/lib/plugins/plugin/style.css
@@ -30,16 +30,21 @@
 #plugin__manager .common input.edit { width: 24em; margin: 0.5em;}
 #plugin__manager .common .button { }
 
-#plugin__manager .plugins { }
-#plugin__manager .plugins form { }
-#plugin__manager .plugins fieldset { text-align: right; border-top:0; border-right:0; border-left:0;}
-#plugin__manager .plugins .legend { color: black; display: block; margin: 0; padding: 0; font-size: 1em; line-height: 1.4em; font-weight: normal; text-align: left; float: left; padding: 0;}
+#plugin__manager form.plugins { background: __dark__; }
+#plugin__manager .plugins fieldset { text-align: right; border-width: 0.3em 0.2em; border-color: __white__; margin: 1px 0; padding: 0.2em 0.3em; background: __white__;}
+#plugin__manager .plugins fieldset.protected { background: #fdd; color: #000; }
+#plugin__manager .plugins fieldset.disabled { background: #e0e0e0; color: #a8a8a8; }
+
+#plugin__manager .plugins .legend { color: black; display: block; margin: 0; padding: 0; font-size: 1em; line-height: 1.4em; font-weight: normal; text-align: left; float: left; padding: 0; clear: none;}
 #plugin__manager .plugins .button { width: 5em; font-size: 95%;}
 
+#plugin__manager .plugins fieldset.buttons .button { float: left; }
+
 #plugin__manager .pm_info h3 { margin-left: 0; }
 #plugin__manager .pm_info dl { margin: 1em 0; padding: 0;}
 #plugin__manager .pm_info dt { width: 6em; float: left; clear: left; margin:0; padding: 0;}
-#plugin__manager .pm_info dd { margin:0 0 0 7em; padding: 0;}
+#plugin__manager .pm_info dd { margin:0 0 0 7em; padding: 0; background: none;}
 
+#plugin__manager .plugins .enable { float: left; width: auto; margin-right: 0.5em;}
 
 /* end admin plugin styles */
-- 
GitLab