<?php

/*  Alternate WebKit
(C) 2011-2014 by Alternate Tools
-----------------------------------------------------

File:         edit_file.php
Description:  Administrative module to edit plain text files

*/

    include "../modules/class_textfile.php";
    include "../modules/mod_admin.php";

    $cFile = new cTextFile;
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_reporter() == true)
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }

    // Check filename
    $fileName = "";
    if (isset ($_POST['file_name']) == true)
        $fileName = $_POST['file_name'];
    else if (isset ($_GET['filename']) == true)
        $fileName = $_GET['filename'];
    if (file_exists($fileName) == false)
        $errorString = get_translation('admin_filedt_errorload') . ': ' . $fileName;

    // Check back to
    $backTo = "";
    if (isset ($_POST['back_to']) == true)
        $backTo = $_POST['back_to'];
    else if (isset ($_GET['backto']) == true)
        $backTo = $_GET['backto'];

    // File edit cancelled?
    if (isset ($_POST['file_Cancel']))
    {
        if ($backTo == "")
            header ("Location: " . $ADMIN_MAIN_SITE);
        else
            header ("Location: " . $backTo);
        return;
    }

    // Save the file
    if (isset ($_POST['file_Save']))
    {
        if (isset($_POST['file_content']))
        {
            $_POST['file_content'] = str_replace("<textarea_begin", "<textarea", $_POST['file_content']);
            $_POST['file_content'] = str_replace("<textarea_end", "</textarea", $_POST['file_content']);
            $cFile->SaveToFile($fileName, transform_keep_html($_POST['file_content']));
            log_message ('User ' . get_user_name() . ' saved file ' . $fileName);
            if ($backTo == "")
                header ('Location: ' . $ADMIN_MAIN_SITE);
            else
                header ("Location: " . $backTo);
            return;
        }
        else
            $errorString = get_translation('admin_filedt_errorsave') . ': ' . $fileName;
    }

    // Display file content
    print_admin_header(get_translation('admin_filedt_edithead') . ':<br>' . $fileName, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'));
    if ($errorString != "")
    {
        print_error_message($errorString);
        return;
    }

    $fileContent = $cFile->LoadFromFile($fileName);
    $fileContent = str_replace("<textarea", "<textarea_begin", $fileContent);
    $fileContent = str_replace("</textarea", "<textarea_end", $fileContent);

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    echo_r('<table width="100%">');
    echo_r('<tr>');
    echo_r('  <td>');
    echo_r('    <textarea cols="115" rows="20" name="file_content" wrap="off">' . $fileContent . '</textarea>');
    echo_r('  </td>');
    echo_r('</tr>');
    echo_r('</table>');

    print_bottom_links("file_Save", get_translation('admin_save'),
                       "file_Cancel", get_translation('admin_cancel'), false);
    echo_r('<input type="hidden" name="file_name" value="' . $fileName . '">');
    echo_r('<input type="hidden" name="back_to" value="' . $backTo . '">');
    echo_r('</form>');

    print_admin_footer();

?>