Hi all,
I have worked over the backup tool plugin to get rid of the calls to shell_exec. Along the way, I have updated my dokuwiki install and made sure my backuptool would work with the current version as well.
(I have made some adjustments in the lang folder as well, but I suppose you can figure these out yourselves.
<?php
/**
* Backup Tool for DokuWiki
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Terence J. Grant<tjgrant@tatewake.com>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
if(!defined('DOKU_INCLUDE')) define('DOKU_INCLUDE',DOKU_INC.'inc/');
require_once(DOKU_PLUGIN . 'admin.php');
require_once(DOKU_INCLUDE . 'TarLib.class.php');
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_backup extends DokuWiki_Admin_Plugin
{
var $state = 0;
var $backup = '';
/**
* Constructor
*/
function admin_plugin_backup()
{
$this->setupLocale();
}
/**
* return some info
*/
function getInfo()
{
return array(
'author' => 'Terence J. Grant, Andreas Wagner',
'email' => 'tjgrant@tatewake.com, andreas.wagner@em.uni-frankfurt.de',
'date' => '2008-07-04',
'name' => 'Backup Tool Plugin',
'desc' => 'A tool to backup your data and configuration.',
'url' => 'http://tatewake.com/wiki/projects:backuptool_for_dokuwiki',
);
}
/**
* return sort order for position in admin menu
*/
function getMenuSort()
{
return 999;
}
/**
* return a menu prompt for the admin menu
* NOT REQUIRED - its better to place $lang['menu'] string in localised string file
* only use this function when you need to vary the string returned
*/
function getMenuText()
{
return 'Backup Tool';
}
/**
* handle user request
*/
function handle()
{
$this->state = 0;
if (!isset($_REQUEST['cmd'])) return; // first time - nothing to do
if (!is_array($_REQUEST['cmd'])) return;
$this->backup = $_REQUEST['backup'];
if (is_array($this->backup))
{
$this->state = 1;
}
}
/**
* output appropriate html
*/
function html()
{
global $conf;
if ($this->state == 0)
{
print $this->plugin_locale_xhtml('intro');
ptln("<form action=\"".wl($ID)."\" method=\"post\">");
ptln(' <input type="hidden" name="do" value="admin" />');
ptln(' <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
ptln(' <input type="hidden" name="cmd[backup]" value="true" />');
print '<center><table class="inline">';
print ' <tr><th> '.$this->getLang('bt_item_type').' </th><th> '.$this->getLang('bt_add_to_archive').' </th></tr>';
print ' <tr><td> '.$this->getLang('bt_pages').' </td><td><input type="checkbox" name="backup[pages]" checked/></td></tr>';
print ' <tr><td> '.$this->getLang('bt_old_revisions_of_pages').' </td><td><input type="checkbox" name="backup[revisions]" checked/></td></tr>';
print ' <tr><td> '.$this->getLang('bt_meta_data_subscriptions').'</td><td><input type="checkbox" name="backup[subscriptions]" checked/></td></tr>';
print ' <tr><td> '.$this->getLang('bt_media_files').' </td><td><input type="checkbox" name="backup[media]" checked/></td></tr>';
print ' <tr><td> '.$this->getLang('bt_wiki_acl_user_config').' </td><td><input type="checkbox" name="backup[config]" checked/></td></tr>';
print ' <tr><td> '.$this->getLang('bt_templates').'</td><td><input type="checkbox" name="backup[templates]" /></td></tr>';
print ' <tr><td> '.$this->getLang('bt_plugins').'</td><td><input type="checkbox" name="backup[plugins]" /></td></tr>';
print '</table>';
print '<br />';
print '<p><input type="submit" value="'.$this->getLang('bt_create_backup').'"></p></center>';
print '</form>';
}
else
{
print $this->plugin_locale_xhtml('outro');
//remove previous backups
// shell_exec("rm -rf backup*.tar");
// shell_exec("rm -rf backup*.tar.bz2");
foreach(glob("backup*.tar") as $fn) unlink($fn);
foreach(glob("backup*.tar.gz") as $fn) unlink($fn);
foreach(glob("backup*.tar.bz2") as $fn) unlink($fn);
$finaldate = date('Ymd-His');
if(extension_loaded('bz2')) $finalfilename = 'backup-'.$finaldate . ".tar.bz2";
elseif(extension_loaded('zlib')) $finalfilename = 'backup-'.$finaldate . ".tar.gz";
else $finalfilename = 'backup-'.$finaldate . ".tar";
$finalfile = DOKU_INC.'/data/tmp/'.$finalfilename;
$Archive = new tarlib($finalfile, COMPRESS_DETECT);
print "<ul>";
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." smileys.conf...</div></li>"; ob_flush(); flush();
// shell_exec("tar -cf ".$finalfile.".tar conf/smileys.conf");
$files = array(DOKU_INC . "/conf/smileys.conf");
if ($this->backup['pages'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_pages')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar ".$conf['datadir']);
$files = array_merge($files, directoryToArray($conf['datadir']));
}
if ($this->backup['revisions'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_old_revisions_of_pages')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar ".$conf['olddir']);
$files = array_merge($files, directoryToArray($conf['olddir']));
}
if ($this->backup['subscriptions'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_meta_data_subscriptions')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar ".$conf['metadir']);
$files = array_merge($files, directoryToArray($conf['metadir']));
}
if ($this->backup['media'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_media_files')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar ".$conf['mediadir']);
$files = array_merge($files, directoryToArray($conf['mediadir']));
}
if ($this->backup['config'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_wiki_acl_user_config')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar conf/acl.auth.php");
// shell_exec("tar -rf ".$finalfile.".tar conf/users.auth.php");
// shell_exec("tar -rf ".$finalfile.".tar conf/local.php");
// shell_exec("tar -rf ".$finalfile.".tar conf/local.protected.php");
$files = array_merge($files, directoryToArray(DOKU_INC . "/conf"));
}
if ($this->backup['templates'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_templates')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar lib/tpl");
$files = array_merge($files, directoryToArray(DOKU_INC . "/lib/tpl"));
}
if ($this->backup['plugins'])
{
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_archiving')." ".$this->getLang('bt_plugins')."...</div></li>"; ob_flush(); flush();
// shell_exec("tar -rf ".$finalfile.".tar lib/plugins");
$files = array_merge($files, directoryToArray(DOKU_INC . "/lib/plugins"));
}
print "<li class=\"level1\"><div class=\"li\">".$this->getLang('bt_compressing_archive')."...</div></li>"; ob_flush(); flush();
// shell_exec("bzip2 -z -9 ".$finalfile.".tar");
// print '<pre>$files:'; print_r($files); print '</pre>'; ob_flush(); flush();
$success = $Archive->Add($files, "wiki_root", DOKU_INC);
if ($success<=0) {
print "<div class=\"error\">Error on creating archive: " . $Archive->TarErrorStr($success) . "</div>";
print '<pre>$files:'; print_r($files); print '</pre>'; ob_flush(); flush();
exit;
}
print "</ul>";
// shell_exec("mv ".$finalfile.".tar.bz2 ".$conf['mediadir']."/".$finalfile.".tar.bz2");
rename($finalfile, $conf['mediadir'] . "/" . $finalfilename);
print $this->plugin_locale_xhtml('download');
print $this->plugin_render('{{:'.$finalfilename.'}}');
ob_flush(); flush();
}
}
}
// from http://snippets.dzone.com/posts/show/155 :
function directoryToArray($directory) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "_dummy" && $file != "disabled") {
$file = $directory . "/" . $file;
if (is_dir($file)) {
$array_items = array_merge($array_items, directoryToArray($file));
} else {
if(filesize($file) !== 0) $array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
You can also find it here:
http://dokuwiki.1t2.us/10207
I hope this helps. Cheers,
Andreas