<?php
# CMS Made Simple
# Plugin: {redirect_url_advanced}
#
# 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

function smarty_function_redirect_url_advanced($params, $smarty)
{
    if (isset($params['to'])) {
        $url = trim($params['to']);
        $code = isset($params['code']) ? (int)$params['code'] : 302;

        // Send appropriate HTTP headers based on the code parameter
        if ($code === 301) {
            header('HTTP/1.1 301 Moved Permanently');
        } else {
            header('HTTP/1.1 302 Found');
        }
        header('Location: ' . $url);
        exit; // Ensure no further code is executed
    } else {
        echo 'Error: The "to" parameter is required.';
    }
}

function smarty_cms_help_function_redirect_url_advanced()
{
    echo "
    <h3>What does this do?</h3>
    <p>The <strong>{redirect_url_advanced}</strong> plugin allows you to redirect the user from the current page to any specified URL. You can choose to perform either a temporary (302) or permanent (301) redirect by setting the appropriate HTTP status code.</p>

    <h3>How do I use it?</h3>
    <p>Insert the plugin tag into your template or page content where you want the redirect to occur.</p>
    <pre>{redirect_url_advanced to='URL' code='STATUS_CODE'}</pre>
    <p><strong>Note:</strong> Replace 'URL' with the destination URL and 'STATUS_CODE' with either 301 or 302.</p>

    <h3>Parameters</h3>
    <ul>
        <li><em>(required)</em> <tt>to</tt> - The URL to redirect to.</li>
        <li><em>(optional)</em> <tt>code</tt> - The HTTP status code for the redirection. Accepts 301 or 302. Defaults to 302 if not specified.</li>
    </ul>

    <h3>Examples</h3>
    <p><strong>Temporary Redirect (302 Found):</strong></p>
    <pre>{redirect_url_advanced to='http://www.example.com'}</pre>

    <p><strong>Permanent Redirect (301 Moved Permanently):</strong></p>
    <pre>{redirect_url_advanced to='http://www.example.com' code=301}</pre>
    ";
}

function smarty_cms_about_function_redirect_url_advanced()
{
    echo "
    <p>Author: John Scotcher (support@pearsontreehouse.co.uk) and ChaptGPT</p>
    <p>Version: 1.0</p>
    <p>Summary: A plugin to redirect to a specified URL with the ability to set a 301 or 302 HTTP status code. Created for use of redirecting old blog urls to new ones on the new site, where redirect page wouldn't do it.</p>
    ";
}
?>

