<?php

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

File:         edit_splashpage.php
Description:  Administrative module to splashpage

*/

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

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

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

    // Cancel titles?
    if (isset($_POST['splashpage_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }
    
    // Save splashpage?
    if (isset($_POST['splashpage_Save']))
    {
        // Activate
        $cConfig->SetParameter("site_show_splashpage", isset($_POST['splashpage_Activate']) == true);
        // Splashpage title
        $cFile->SaveToFile('../' . $SPLASHPAGETITLE_CONFIG_FILE, $_POST['splashpage_Title']);
        // Splashpage image
        if (isset($_POST['splashpage_NoImage']) == true)
        {
            if (file_exists($cConfig->GetParameter("site_image_splashpage")) == true)
                unlink($cConfig->GetParameter("site_image_splashpage"));
        }
        if (!(empty ($_FILES['splashpage_Image']['name'])))
        {
            if (check_image_extension($_FILES['splashpage_Image']['name']) == false)  // Check extension
            {
                $errorString = get_translation('admin_splash_errortype') . '! : ' . $_FILES['splashpage_Image']['name'];
            }
            else
            {
                // Delete old one first!
                if (file_exists($cConfig->GetParameter("site_image_splashpage")) == true)
                    unlink($cConfig->GetParameter("site_image_splashpage"));
                $destName = $SPLASHPAGE_IMAGE . retrieve_extension($_FILES['splashpage_Image']['name']);
                // Is resize necessary? ...otherwise copy
                if(resize_image($_FILES['splashpage_Image']['name'], $_FILES['splashpage_Image']['tmp_name'], $destName, $SITE_MAXIMGWIDTH, $SITE_MAXIMGHEIGHT, false) == false)
                {
                    $errorString = get_translation('admin_splash_errorimage') . ': ' . $_FILES['splashpage_Image']['tmp_name'] . '!';
                }
                else
                {
                    $cConfig->SetParameter("site_image_splashpage", $destName);
                }
            }
        }
        if ($cConfig->SaveConfigFile() == false)
            $errorString = get_translation('admin_splash_errorconfig') . "!";
        if ($errorString == "")
        {
            log_message ('User ' . get_user_name() . ' saved splashpage');
            header('location: ' . $ADMIN_MAIN_SITE);
            return;
        }
    }

    // Display
    print_admin_header(get_translation('admin_splash_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-4', true, get_translation('admin_backtomain'), "", "", false);
    // Enable editor?
    if ($ADMIN_USEWYSIGWYG == true)
        print_wysiwyg_header();
    finish_admin_header(get_translation('admin_splash_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-4', true, get_translation('admin_backtomain'), "", "");

    if ($errorString != "")
        print_error_message ($errorString);
        
    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    // Activate splash page
    echo_r('<hr>');
    echo_r('<table border="0" width="100%">');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_splash_activate') . ':</td>');
    echo_r('    <td>');
    echo  ('      <input type="checkbox" name="splashpage_Activate" value="1"');
    if ($cConfig->GetParameter("site_show_splashpage") == "1")
        echo (' checked');
    echo_r('>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    // Splash page image
    echo_r('<hr>');
    echo_r('<table border="0" width="100%">');
    echo_r('  <tr>');
    echo_r('    <td width="30%">' . get_translation('admin_maxuploadsize') . ':</td>');
    echo_r('    <td><b>' . ini_get("upload_max_filesize") . '</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_sizerestriction') . ':</td>');
    if ($cConfig->GetParameter("site_imagerestriction") == "1")
        echo_r('    <td><b>' . $cConfig->GetParameter("site_maximgwidth") . ' x ' . $cConfig->GetParameter("site_maximgheight") . ' ' . get_translation('admin_sizerestrictionnpixels') . '</b></td>');
    else
        echo_r('    <td><b>' . get_translation('admin_sizerestrictionnone') . '</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td>' . get_translation('admin_splash_image') . ':</td>');
    echo_r('    <td>');
    if (file_exists($cConfig->GetParameter("site_image_splashpage")) == true)
    {
        echo_r('     <br><img src="' . $cConfig->GetParameter("site_image_splashpage") . '" alt="">');
    }
    echo_r('    <br><input type="file" name="splashpage_Image" size="50">');
    echo_r('    <br><input type="checkbox" name="splashpage_NoImage" value="1">&nbsp;' . get_translation('admin_splash_noimageordelete') . '');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    // Splash page text
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_splash_text') . ':</td>');
    echo_r ('    <td>');
    echo ('      <textarea cols="80" rows="20" id="page_Content" name="splashpage_Title"');
    if ($ADMIN_WRAPEDITOR == true)
        echo (' wrap="off"');
    echo_r ('>' . $cFile->LoadFromFile('../' . $SPLASHPAGETITLE_CONFIG_FILE) . '</textarea>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

    print_bottom_links("splashpage_Save", get_translation('admin_save'),
                       "splashpage_Cancel", get_translation('admin_cancel'), false);
    echo_r ('</form>');

    print_admin_footer();
?>