<?php
#CMS - CMS Made Simple
#(c)2009 by Jean-Christophe Cuvelier (jcc@morris-chapman.com)
#include 'function.redirect_main_domain.php';

#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

# VERSION 1.0

function smarty_cms_function_redirect_main_domain($params, &$smarty) {
	
	$protocol = $_SERVER['HTTPS'] ? 'https' : 'http';

	if (isset($params['domain']) && $_SERVER['HTTP_HOST'] != $params['domain'])
	{
		$url = $_SERVER['HTTPS'] ? 'https' : 'http' . '://' . $params['domain'] . $_SERVER['REQUEST_URI'];
		
		// return $url;
    header( 'HTTP/1.1 301 Moved Permanently' ); 
    header( 'Location: ' . $url); 
		
	}
	else
	{
		return null;
	}
		
}

function smarty_cms_help_function_redirect_main_domain() {
  ?>
	<p>To use this plugin, just include it in your template, in the top, like {redirect_main_domain domain="http://www.mymaindomain.com/"} and all the requests to cibling domains will be 301 redirected to the same page with the main domain.</p>
	<p>Example: http://www.mydomain.org/about-this-website will be redirected to http://www.mymaindomain.com/about-this-website</p>
	<?php
}


function smarty_cms_about_function_redirect_main_domain() {
	?>
	<p>Author: Jean-Christophe Cuvelier &lt;cybertotophe@gmail.com&gt;</p>
	<p>Version: 1.1</p>
	<p>
	Change History:<br/>
		20110419 Help improved<br/>
    20100913 1st release.
	</p>
	<?php
}

?>
