*/
// todo
// - maintain a history of file modified
// - allow a plugin to contain extras to be copied to the current template (extra/tpl/)
// - to images (lib/images/) [ not needed, should go in lib/plugin/images/ ]
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'admin.php');
// language stuff here for now ... move to language files when complete
// global $lang;
//--------------------------[ GLOBALS ]------------------------------------------------
// note: probably should be dokuwiki wide globals, where they can be accessed by pluginutils.php
global $common_plugin_files, $common_plugin_types;
$common_plugin_types = array('syntax', 'admin');
$common_plugin_files = array("style.css", "screen.css", "print.css", "script.js");
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_plugin extends DokuWiki_Admin_Plugin {
var $disabled = 0;
var $plugin = '';
var $cmd = '';
var $handler = NULL;
var $functions = array('delete','update','settings','info'); // require a plugin name
var $commands = array('manage','refresh','download'); // don't require a plugin name
var $plugin_list = array();
var $msg = '';
var $error = '';
function admin_plugin_plugin() {
global $conf;
$this->disabled = (!isset($conf['pluginmanager']) || ($conf['pluginmanager'] == 0));
}
/**
* return some info
*/
function getInfo(){
$disabled = ($this->disabled) ? '(disabled)' : '';
return array(
'author' => 'Christopher Smith',
'email' => 'chris@jalakai.co.uk',
'date' => '2005-08-10',
'name' => 'Plugin Manager',
'desc' => "Manage Plugins, including automated plugin installer $disabled",
'url' => 'http://wiki.splitbrain.org/plugin:adminplugin',
);
}
/**
* return prompt for admin menu
*/
function getMenuText($language) {
if (!$this->disabled)
return parent::getMenuText($language);
return '';
}
/**
* return sort order for position in admin menu
*/
function getMenuSort() {
return 20;
}
/**
* handle user request
*/
function handle() {
global $ID, $lang;
if ($this->disabled) return;
$this->plugin = $_REQUEST['plugin'];
$this->cmd = $_REQUEST['fn'];
if (is_array($this->cmd)) $this->cmd = key($this->cmd);
sort($this->plugin_list = plugin_list());
// verify $_REQUEST vars
if (in_array($this->cmd, $this->commands)) {
$this->plugin = '';
} else if (!in_array($this->cmd, $this->functions) || !in_array($this->plugin, $this->plugin_list)) {
$this->cmd = 'manage';
$this->plugin = '';
}
// create object to handle the command
$class = "ap_".$this->cmd;
if (!class_exists($class)) $class = 'ap_manage';
$this->handler = & new $class($this, $plugin);
$this->msg = $this->handler->process();
}
/**
* output appropriate html
*/
function html() {
if ($this->disabled) return;
// enable direct access to language strings
$this->setupLocale();
if ($this->handler === NULL) $this->handler = & new ap_manage();
if (!$this->plugin_list) sort($this->plugin_list = plugin_list());
ptln('
');
$this->handler->html();
ptln('
');
}
}
class ap_manage {
var $manager = NULL;
var $lang = array();
var $plugin = '';
var $downloaded = array();
function ap_manage(&$manager, $plugin) {
$this->manager = & $manager;
$this->plugin = $plugin;
$this->lang = & $manager->lang;
}
function process() {
return '';
}
function html() {
print $this->manager->plugin_locale_xhtml('admin_plugin');
// FIXME, these probably shouldn't be here any more!
if (!$this->manager->msg) $this->manager->msg = ' ';
ptln("
{$manager->msg}
");
if ($this->manager->error) {
ptln("
".str_replace("\n"," ",$this->manager->error)."
");
}
$this->html_menu();
}
// build our standard menu
function html_menu($listPlugins = true) {
global $ID;
ptln('
');
ptln('
');
ptln(' ');
ptln('
');
if ($listPlugins) {
ptln('
'.$this->lang['manage'].'
');
ptln('
');
$this->html_pluginlist();
ptln('
');
}
ptln('
');
}
function html_pluginlist() {
foreach ($this->manager->plugin_list as $plugin) {
$new = (in_array($plugin, $this->downloaded)) ? ' class="new"' : '';
ptln(' ');
}
}
function html_button($btn, $disabled=false, $indent=0) {
$disabled = ($disabled) ? 'disabled="disabled"' : '';
ptln('',$indent);
}
/**
* Rebuild aggregated files & update latest plugin date
*/
function refresh() {
global $lang;
global $common_plugin_files;
sort($this->manager->plugin_list = plugin_list());
foreach ($common_plugin_files as $file) {
$aggregate = '';
// could replace with an class/object based aggregator,
// that way special files could have their own aggregator
foreach ($this->manager->plugin_list as $plugin) {
if (@file_exists(DOKU_PLUGIN."$plugin/$file")) {
$contents = @file_get_contents(DOKU_PLUGIN."$plugin/$file")."\n";
// url conversion for css files
if (is_css($file)) {
$contents = preg_replace('/(url\([\'\"]?)([^\/](?![a-zA-Z0-9]+:\/\/).*?)([\'\"]?\))/','$1'.$plugin.'/$2$3',$contents);
}
$aggregate .= $contents;
}
}
if (trim($aggregate)) {
if (!io_savefile(DOKU_PLUGIN."plugin_$file", $aggregate)) {
$this->manager->error .= sprintf($this->lang['error_write'],$file);
}
}
}
// update latest plugin date - FIXME
return (!$this->manager->error);
}
// log
function plugin_writelog($plugin, $cmd, $data) {
$file = DOKU_PLUGIN.$plugin.'/manager.dat';
switch ($cmd) {
case 'install' :
$url = $data[0];
$date = date('r');
if (!$fp = @fopen($file, 'w')) return;
fwrite($fp, "installed=$date\nurl=$url\n");
fclose($fp);
break;
case 'update' :
$date = date('r');
if (!$fp = @fopen($file, 'w+')) return;
fwrite($fp, "updated=$date\n");
fclose($fp);
break;
}
}
function plugin_readlog($plugin, $field) {
static $log = array();
$file = DOKU_PLUGIN.$plugin.'/manager.dat';
if (!isset($log[$plugin])) {
$tmp = @file_get_contents($file);
if (!$tmp) return '';
$log[$plugin] = & $tmp;
}
if ($field == 'ALL') {
return $log[$plugin];
}
if (preg_match_all('/'.$field.'=(.*)$/m',$log[$plugin], $match=array()))
return implode("\n", $match[1]);
return '';
}
}
class ap_refresh extends ap_manage {
function process() {
$this->refresh();
if (!$this->manager->error) return $this->lang['refreshed'];
}
function html() {
parent::html();
ptln('
');
ptln('
'.$this->lang['refreshing'].'
');
ptln('
'.$this->lang['refreshed'].'
');
ptln('
');
}
}
class ap_download extends ap_manage {
var $overwrite = false;
function process() {
global $lang, $conf;
$plugin_url = $_REQUEST['url'];
if (!preg_match("/[^\/]*$/", $plugin_url, $matches = array()) || !$matches[0]) {
$this->manager->error = $this->lang['error_badurl'].'\n';
return '';
}
$file = $matches[0];
$folder = "p".md5($file.date('r')); // tmp folder name - will be empty (should really make sure it doesn't already exist)
$tmp = DOKU_PLUGIN."tmp/$folder";
if (!$this->manager->error && !ap_mkdir($tmp)) {
$this->manager->error = $this->lang['error_dir_create'].'\n';
$folder = '';
}
if (!$this->manager->error && !io_download($plugin_url, "$tmp/$file")) {
$this->manager->error = sprintf($this->lang['error_download'],$url)."\n";
}
ap_decompress("$tmp/$file", $tmp);
// search tmp/$folder for the folder(s) that has been created
// move that folder(s) to lib/plugins/
if ($dh = @opendir("$tmp/")) {
while (false !== ($f = readdir($dh))) {
if ($f == '.' || $f == '..' || $f == 'tmp') continue;
if (!is_dir("$tmp/$f")) continue;
// check to make sure we aren't overwriting anything
if (file_exists(DOKU_PLUGIN."/$f")) {
// remember our settings, ask the user to confirm overwrite, FIXME
continue;
}
ap_copy("$tmp/$f", DOKU_PLUGIN.$f);
$this->downloaded[] = $f;
$this->plugin_writelog($f, 'install', array($plugin_url));
}
closedir($dh);
}
// cleanup
if ($folder && is_dir(DOKU_PLUGIN."tmp/$folder")) ap_delete(DOKU_PLUGIN."tmp/$folder");
if (!$this->manager->error) {
$this->refresh();
}
return '';
}
function html() {
parent::html();
ptln('
');
ptln('
'.$this->lang['downloading'].'
');
if ($this->manager->error) {
ptln('
'.$this->manager->error.'
');
} else if (count($this->downloaded) == 1) {
ptln('
");
}
// simple output filter, make html entities safe and convert new lines to
function out($text) {
return str_replace("\n",' ',htmlentities($text));
}
}
//--------------[ to do ]---------------------------------------
class ap_update extends ap_manage {
function html() {
parent::html();
ptln('
');
ptln('
'.$this->lang['updating'].'
');
if ($this->manager->error) {
ptln('
'.$this->manager->error.'
');
} else if (count($this->downloaded) == 1) {
ptln('