<?php
/*  Page Integrator for DotClear
 *  Copyright (C) 2004  Sébastien LECACHEUR
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


# Plugin name
define('PI_PLUGIN_NAME','pageintegrator');

# Configuration file
$ini_file = dirname(__FILE__).'/'.PI_PLUGIN_NAME.'.ini';
$objIni = new iniFile($ini_file);
$objIni->read($ini_file);

# Tools URL
$url = 'tools.php?p='.PI_PLUGIN_NAME;

# Menu
$mySubMenu->addItem(__('Configuration'), $url, 'images/ico_edit.png', FALSE);
$mySubMenu->addItem(__('Use'), $url.'&use', 'images/ico_link.png', FALSE);
$mySubMenu->addItem(__('Information'), $url.'&info', 'images/ico_ok.png', FALSE);
$mySubMenu->addItem(__('FAQ'), $url.'&faq', 'images/ico_help.png', FALSE);


$err = '';
$is_writable = false;


if (!is_writable($ini_file)) {
	$err = '<p>'.sprintf(__('Config file %s, is not writable.'),'<strong>./'.PI_PLUGIN_NAME.'.ini</strong>').'</p>';
} else {
	$is_writable = true;

	# Modification du fichier
	if (!empty($_POST)) {
		$p_plugin_page_dir = trim($_POST['p_plugin_page_dir']);
		$p_plugin_page_ext_mask = trim($_POST['p_plugin_page_ext_mask']);
		$p_plugin_page_ext = trim($_POST['p_plugin_page_ext']);
		$p_plugin_mapping_active = trim($_POST['p_plugin_mapping_active']);
		$p_plugin_mapping_file = trim($_POST['p_plugin_mapping_file']);

		$objIni = new iniFile($ini_file);
		$objIni->editVar('plugin_page_dir', $p_plugin_page_dir);
		$objIni->editVar('plugin_page_ext_mask',(integer) $p_plugin_page_ext_mask);
		$objIni->editVar('plugin_page_ext', $p_plugin_page_ext);
		$objIni->editVar('plugin_mapping_active',(integer) $p_plugin_mapping_active);
		$objIni->editVar('plugin_mapping_file', $p_plugin_mapping_file);

		if ($objIni->saveFile() != false) {
			if (defined('DC_UPDATE_FILE_W') && DC_UPDATE_FILE_W) {
				files::touch(DC_UPDATE_FILE,time());
			}
			header('Location: '.$url.'&done=1');
			exit;
		} else {
			$err = __('An error occured while writing the file.');
		}
	} else {
		if ( strlen(plugin_page_dir) == 0) {
			$dc_ini_file = dirname(__FILE__).'/../../../conf/dotclear.ini';
			$objIni = new iniFile($dc_ini_file);
			$p_plugin_page_dir = dc_app_url.'/share';
			$objIni->editVar('plugin_page_dir', $p_plugin_page_dir);
			if ( $objIni->saveFile() != false ) {
				if (defined('DC_UPDATE_FILE_W') && DC_UPDATE_FILE_W) {
					files::touch(DC_UPDATE_FILE,time());
				}

				header('Location: '.$url.'&done=1');
				exit;
			} else {
				$err = __('An error occured while writing the file.');
			}
		}
	}
}


# Displaying
buffer::str('<h2>'.__('Page Integrator').'</h2>');


if ($err != '') {
	buffer::str('<div class="erreur"><p><strong>'.__('Error(s)').' :</strong></p>'.$err.'</div>');
}


if (isset($_GET['info'])) {
	#####################################
	# Information about some used files
	#####################################
	buffer::str('<fieldset class="clear"><legend>'.__('Files informations').'</legend>');
	$img_check = '<img src="images/check_%s.png" alt="" />';
	if (is_writable(dirname(__FILE__).'/../../../'.DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.PI_PLUGIN_NAME.'.ini')) {
		buffer::str('<p>'.sprintf($img_check,'on').' '.sprintf(__('File %s is writable.'),DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.PI_PLUGIN_NAME.'.ini').'</p>');
	} else {
		buffer::str('<p>'.sprintf($img_check,'off').' '.sprintf(__('File %s is not writable.'),DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.PI_PLUGIN_NAME.'.ini').'</p>');
	}

	if (is_writable(dirname(__FILE__).'/../../../'.DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.plugin_mapping_file)) {
		buffer::str('<p>'.sprintf($img_check,'on').' '.sprintf(__('File %s is writable.'),DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.plugin_mapping_file).'</p>');
	} else {
		buffer::str('<p>'.sprintf($img_check,'off').' '.sprintf(__('File %s is not writable.'),DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.plugin_mapping_file).'</p>');
	}
	buffer::str('</fieldset>');
} elseif (isset($_GET['use'])) {
	######################################
	# Explanations about the pluging use
	######################################
	buffer::str('<fieldset class="clear"><legend>'.__('Use').'</legend>');
	buffer::str('<h3>'.__('Plugin integration').'</h3>');
	buffer::str('<h4>'.__('Update').'</h4>');
	buffer::str(__('Search this PHP code in your main template file').' (template.php) :');
	buffer::str('<i><pre>'.
		htmlentities( '<?php if ($err_msg != \'\') : /* Si on a une quelconque erreur, on l\'affiche */?>')."\n".
		"\t".htmlentities('<div class="error"><strong>Erreur : </strong>')."\n".
		"\t".htmlentities('<?php echo $err_msg; ?></div>')."\n\n".
		htmlentities('<?php elseif ($preview) : /* Si on demande la prévisualisation d\'un commentaire */?>').
		'</pre></i>');
	buffer::str(__('And replace it by this following PHP code :'));
	buffer::str('<i><pre>'.
		htmlentities( '<?php if ($err_msg != \'\') : /* Si on a une quelconque erreur, on l\'affiche */?>')."\n".
		"\t".htmlentities('<div class="error"><strong>Erreur : </strong>')."\n".
		"\t".htmlentities('<?php echo $err_msg; ?></div>')."\n\n".
		htmlentities( '<?php elseif ($mode == \'show\') : dcIntegrator::includePage(); ?>')."\n\n".
		htmlentities('<?php elseif ($preview) : /* Si on demande la prévisualisation d\'un commentaire */?>').
		'</pre></i>');
	buffer::str(__('Now, you can insert link anywhere with this way :'));
	buffer::str('<i><pre>'.
		htmlentities( '<?php dcIntegrator::makeLink(\''.__('Go to my page').'\',\'mypage\');?>').
		'</pre></i>');

	# Show the step by step method if FileManager plugin is installed
	if (file_exists(dirname(__FILE__).'/../../../'.DC_ECRIRE.'/tools/filemanager/')) {
		# Get the used template file
		if (file_exists( dirname(__FILE__).'/../../../themes/'.dc_theme.'/template.php')) {
			$template_file = '/themes/'.dc_theme.'/template.php';
		} else {
			$template_file = '/themes/default/template.php';
		}

		buffer::str(
			'<h4>'.__('Step by step').'</h4>'.
			'<ol>'.
			'<li>'.__('Copy the PHP code above').'</li>'.
			'<li>'.'<a href="tools.php?p=filemanager&f='.$template_file.'">'.__('Edit your main template file').'</a></li>'.
			'<li>'.__('Paste the PHP code after the error displaying').'</li>'.
			'<li>'.__('Create some pages in the configured pages directory (with only body content)').'</li>'.
			'<li>'.__('You can use links to created pages like :'));
		if (dc_url_scan == 'path_info') {
			buffer::str('<pre>'.
				htmlentities('<a href="index.php/show/mypage">').
				__('View my page').htmlentities('</a>').
				'</pre>');
		} else if (dc_url_scan == 'query_string') {
			buffer::str('<pre>'.
				htmlentities('<a href="index.php?show/mypage">').
				__('View my page').htmlentities('</a>').
				'</pre>');
		}
		buffer::str(
			__('Or use this following PHP code :').
			'<pre>'.htmlentities('<?php dcIntegrator::makeLink(\'').__('View my page').htmlentities('\',\'mypage\');?>').'</pre>' .
			'</li>'.
			'<li>'.__("That's all :-)").'</li>'.
			'</ol>'
		    );

		# Display link to edit configuration files
		buffer::str('<h3>'.__('Advanced configuration').'</h3>');
		buffer::str('<p>'.'<a href="tools.php?p=filemanager&f=/'.DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.PI_PLUGIN_NAME.'.ini">'.__('Edit Integrator Page main config file').'</a></p>');
		buffer::str('<p>'.'<a href="tools.php?p=filemanager&f=/'.DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/'.plugin_mapping_file.'">'.__('Edit your mapping file').'</a></p>' );
	}

	buffer::str('</fieldset>');
} elseif (isset($_GET['faq'])) {
	#######
	# FAQ
	#######
	buffer::str('<fieldset class="clear"><legend>'.__('FAQ').'</legend>');
	$filename = dirname(__FILE__).'/../../../'.DC_ECRIRE.'/tools/'.PI_PLUGIN_NAME.'/doc/'.DC_LANG.'/faq4dotclear.txt';
	$contents = '';
	if (file_exists($filename)) {
		$handle = fopen ($filename,"r");
		while (!feof($handle)) {
			$contents .= fread($handle,8192);
		}
		fclose($handle);
		buffer::str('<pre>'.wordwrap(utf8_encode(htmlentities($contents)),90).'</pre>');
	} else {
		buffer::str(__('No FAQ available'));
	}
	buffer::str('</fieldset>');
} else {
	############################
	# The plugin configuration
	############################
	if ($is_writable) {
		if (!empty($_GET['done'])) {
			buffer::str('<p class="message">'.__('Configuration file successfully updated.').'</p>');
		} else {
		}

		buffer::str('<form action="'.$url.'" method="post">'.
			'<fieldset class="clear">'.
			'<legend>'.__('Base configuration').'</legend>'.
			'<p class="field"><label for="p_plugin_page_dir"><strong>'.
			__('Pages dir').'&nbsp;:</strong> '.
			'('.sprintf(__('From %s'),'http://'.$_SERVER['SERVER_NAME']).') '.
			helpLink('index&amp;plugin='.PI_PLUGIN_NAME,'p_plugin_page_dir').'</label>'.
			form::field('p_plugin_page_dir',40,'',htmlspecialchars(plugin_page_dir)).
			'</p>'.
			'<p class="field"><label for="p_plugin_page_ext_mask"><strong>'.
			__('Mask pages extensions').'&nbsp;:</strong> '.
			helpLink('index&amp;plugin='.PI_PLUGIN_NAME,'p_plugin_page_ext_mask').'</label>'.
			form::combo('p_plugin_page_ext_mask',array(__('yes')=>1,__('no')=>0),(integer) defined('plugin_page_ext_mask')?plugin_page_ext_mask:0).
			'</p>'.
			'<p class="field"><label for="p_plugin_page_ext"><strong>'.
			__('Pages extension').'&nbsp;:</strong> '.
			helpLink('index&amp;plugin='.PI_PLUGIN_NAME,'p_plugin_page_ext').'</label>'.
			form::field('p_plugin_page_ext',40,'',htmlspecialchars(defined('plugin_page_ext')?plugin_page_ext:'')).
			'</p>'.
			'<p class="field"><label for="p_plugin_mapping_active"><strong>'.
			__('Use mapping').'&nbsp;:</strong> '.
			helpLink('index&amp;plugin='.PI_PLUGIN_NAME,'p_plugin_mapping_active').'</label>'.
			form::combo('p_plugin_mapping_active',array(__('yes')=>1,__('no')=>0),(integer) defined('plugin_mapping_active')?plugin_mapping_active:0).
			'</p>'.
			'<p class="field"><label for="p_plugin_mapping_file"><strong>'.
			__('Mapping file').'&nbsp;:</strong> '.
			helpLink('index&amp;plugin='.PI_PLUGIN_NAME,'p_plugin_mapping_file').'</label>'.
			form::field('p_plugin_mapping_file',40,'',htmlspecialchars(defined('plugin_mapping_file')?plugin_mapping_file:'')).
			'</p>'.
			'</fieldset>'.
			'<p class="field"><input class="submit" type="submit" value="'.__('save').'" /></p>'.
			'</form>'
		);
	} else {
	}
}


?>
