*/
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');
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_tplselector extends DokuWiki_Admin_Plugin {
var $templates = array();
var $_old = '';
var $_new = '';
var $_local = array();
var $_backup = array();
var $_ok = null;
/**
* return some info
*/
function getInfo(){
return array(
'author' => 'Christopher Smith',
'email' => 'chris@jalakai.co.uk',
'date' => '2005-09-22',
'name' => 'Template Selector',
'desc' => "Interactive selection of installed dokuwiki templates",
'url' => 'http://www.dokuwiki.org/plugin:template selector',
);
}
function getMenuSort() { return 100; }
/**
* handle user request
*/
function handle() {
global $conf;
if (!isset($_REQUEST['tpl'])) return; // nothing to do
// retrieve and validate form variables
$tpl = trim($_REQUEST['tpl']);
if ($tpl == $conf['template']) return; // same as current
if (!count($this->templates)) $this->_build_templatelist();
if (!in_array($tpl, $this->templates)) return; // unknown template
$this->_new = $tpl;
$this->_old = $conf['template'];
$this->_read_conflocal();
$this->_local['template'] = $tpl;
$this->_ok = $this->_write_conflocal();
}
/**
* output appropriate html
*/
function html() {
if (!count($this->templates)) $this->_build_templatelist();
ptln('');
if ($this->_new) {
if ($this->_ok === true) {
print($this->locale_xhtml('tplnew'));
ptln(' ');
} else {
print($this->locale_xhtml('tplfail'));
}
} else {
print($this->locale_xhtml('tplselector'));
$this->_html_templatelist();
}
ptln('
');
}
function _build_templatelist() {
$list = array();
$dir = DOKU_INC.'lib/tpl/';
if ($dh = @opendir($dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
$file = (is_link($dir.$entry)) ? readlink($dir.$entry) : $entry;
if (is_dir($dir.$file)) $list[] = $entry;
}
closedir($dh);
}
sort($list);
$this->templates = $list;
}
function _html_templatelist() {
global $conf;
ptln(' ');
}
/*
* read dokuwiki's local configuration file into $this->local
*/
function _read_conflocal() {
$config = DOKU_CONF.'local.php';
if (!@file_exists($config)) return;
$contents = join('',@file($config));
$php_open = ini_get('short_open_tag') ? '<\?' : '<\?php';
preg_match('/(?<='.$php_open.').*?(?=$|\?>)/s', $contents, $match=array());
$contents = join('\n',$match);
// play safe
$this->_saveconfig();
eval($contents);
$this->_local = $conf;
$this->_restoreconfig();
}
/*
* write $this->local to dokuwiki's local configuration file
*/
function _write_conflocal() {
$config = DOKU_CONF.'local.php';
if (@file_exists($config)) {
if (@file_exists($config.'.bak')) @unlink($config.'.bak');
if (!@rename($config, $config.'.bak')) return false;
}
if (!$fh = @fopen($config, 'wb')) {
@rename($config.'.bak', $config);
return false;
}
$out = 'getPluginName()." plugin \n".
" * Date: ".date('r')."\n".
" */\n\n";
$out .= $this->_out_local($this->_local, '$conf');
$out .= "\n@include(DOKU_CONF.'local.protected.php');\n";
$out .= "\n// end auto-generated content\n";
@fwrite($fh, $out);
fclose($fh);
return true;
}
function _out_local($array, $prefix) {
// translation string needs to be improved FIXME
$tr = array("\n"=>'\n', "\r"=>'\r', "\t"=>'\t', "\\" => '\\\\', "'" => '\\\'');
$out = '';
foreach ($array as $name => $val) {
if (is_array($val)) {
$out .= $this->_out_local($val, $prefix."['$name']");
} else {
$out .= $prefix."['$name'] = '".strtr($val, $tr)."';\n";
}
}
return $out;
}
function _saveconfig() {
global $conf;
$this->_backup = $conf;
}
function _restoreconfig() {
global $conf;
$conf = $this->_backup;
$this->_backup = array();
}
}
==== style.css ====
These may be modified to suit your own requirements.