<?php
/**
 * Backup Tool for DokuWiki
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Terence J. Grant<tjgrant@tatewake.com>
 */

$bt_loaded = 0;

function bt_load()
{
	global $bt_loaded, $bt_settings;

	$bt_file = dirname(__FILE__).'/local_pref.php';

	if ($bt_loaded == 0)
	{
		if(file_exists($bt_file))
		{
			include($bt_file);

			$bt_loaded = 1;
		}
	}
}

function bt_write($fp, $name, $val)
{
	fwrite($fp, '$bt_settings[\''.$name.'\'] = \''.$val.'\';'."\n");
}

function bt_save()
{
	global $bt_loaded, $bt_settings;

	$bt_file = dirname(__FILE__).'/local_pref.php';

	if (is_writable($bt_file) || is_writable(dirname(__FILE__)))
	{
		$fp = fopen($bt_file, "w");
		fwrite($fp, '<?php'."\n// This file is automatically generated\n");
		bt_write($fp, 'type', $bt_settings['type']);
		bt_write($fp, 'pages', $bt_settings['pages']);
		bt_write($fp, 'revisions', $bt_settings['revisions']);
		bt_write($fp, 'subscriptions', $bt_settings['subscriptions']);
		bt_write($fp, 'media', $bt_settings['media']);
		bt_write($fp, 'config', $bt_settings['config']);
		bt_write($fp, 'templates', $bt_settings['templates']);
		bt_write($fp, 'plugins', $bt_settings['plugins']);
		fclose($fp);

		ptln('<div class="success">'.'Backup Tool pref saved successfully.'.'</div>');
	}
	else
	{
		ptln('<div class="error">'.'Backup Tool pref is not writable by the server.'.'</div>');
	}
}

//Load settings
bt_load();
