<?php

/*  Alternate WebKit
(C) 2013 by Alternate Tools
-----------------------------------------------------

File:         edit_logos.php
Description:  Administrative module to edit logos

*/

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

    $b_ErrorOccured = false;
    $errorString = "";

    $thmTheme = new cTheme;
    $thmTheme->SetFolder($THEME_FOLDER);
    $thmTheme->LoadThemeFromId($SITE_THEME);
    
    // Check privileges
    check_login();
    if (user_is_customer() == true)
    {
        log_out();
        return;
    }
    if (user_is_admin() == false)
    {
        header ("location:" . $ADMIN_MAIN_SITE);
        return;
    }

    // Pageimage edit cancelled?
    if (isset ($_POST['logos_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }

    // Reset logos?
    if (isset ($_POST['logos_Reset']))
    {
        if ($thmTheme->ResetLogos() == false)
        {
            $errorString = get_translation('admin_logoedt_errorimage') . '!';
            $b_ErrorOccured = true;
        }
        else
        {
            // Logos updated... install theme again
            $thmTheme->InstallTheme($THEME_MAIN_FOLDER, $THEME_IMAGES_FOLDER, $THEME_FILES_FOLDER, $FAVICO_IMAGE);
            log_message ('User ' . get_user_name() . ' reseted logos');
            header('location: ' . $ADMIN_MAIN_SITE);
            return;
        }
    }

    // Check for errors and if none, store the logo images
    if (isset ($_POST['logos_Save']))
    {
        // Main logo file
        if (!(empty ($_FILES['logos_MainLogo']['name'])))
        {
            // Check extension
            $fileExt = strtolower(retrieve_extension($_FILES['logos_MainLogo']['name']));
            if (check_image_extension($_FILES['logos_MainLogo']['name']) == false)
            {
                $errorString = get_translation('admin_logoedt_errortype') . '! : ' . $fileExt;
                $b_ErrorOccured = true;
            }
            else
            {
                $destName = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetLogo();
                if (resize_image($_FILES['logos_MainLogo']['name'], $_FILES['logos_MainLogo']['tmp_name'], $destName,
                    $thmTheme->GetLogoWidth(), $thmTheme->GetLogoHeight(), false, true) == false)
                {
                    $errorString = get_translation('admin_logoedt_errorimage') . ': ' . $_FILES['logos_MainLogo']['name'] . ', ' . $destName. '!';
                    $b_ErrorOccured = true;
                }
            }
        }
        // Index logo file
        if ($b_ErrorOccured == false && !(empty ($_FILES['logos_IndexLogo']['name'])))
        {
            // Check extension
            $fileExt = strtolower(retrieve_extension($_FILES['logos_IndexLogo']['name']));
            if (check_image_extension($_FILES['logos_IndexLogo']['name']) == false)
            {
                $errorString = get_translation('admin_logoedt_errortype') . '! : ' . $fileExt;
                $b_ErrorOccured = true;
            }
            else
            {
                $destName = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetIndexLogo();
                if (resize_image($_FILES['logos_IndexLogo']['name'], $_FILES['logos_IndexLogo']['tmp_name'], $destName,
                    $thmTheme->GetIndexLogoWidth(), $thmTheme->GetIndexLogoHeight(), false, true) == false)
                {
                    $errorString = get_translation('admin_logoedt_errorimage') . ': ' . $_FILES['logos_IndexLogo']['name'] . ', ' . $destName. '!';
                    $b_ErrorOccured = true;
                }
            }
        }

        // Fav icon file
        if ($b_ErrorOccured == false && !(empty ($_FILES['logos_FavIco']['name'])))
        {
            // Check extension
            $fileExt = strtolower(retrieve_extension($_FILES['logos_FavIco']['name']));
            if (check_ico_extension($_FILES['logos_FavIco']['name']) == false)
            {
                $errorString = get_translation('admin_logoedt_errorico') . '! : ' . $fileExt;
                $b_ErrorOccured = true;
            }
            else
            {
                $destName = $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetFavIco();
                if (@copy($_FILES['logos_FavIco']['tmp_name'], $destName) == false)
                {
                    $errorString = get_translation('admin_logoedt_errorimage') . ': ' . $_FILES['logos_FavIco']['name'] . ', ' . $destName. '!';
                    $b_ErrorOccured = true;
                }
            }
        }

        if ($b_ErrorOccured == false)
        {
            // Logos updated... install theme again
            $thmTheme->InstallTheme($THEME_MAIN_FOLDER, $THEME_IMAGES_FOLDER, $THEME_FILES_FOLDER, $FAVICO_IMAGE);
            log_message ('User ' . get_user_name() . ' modified logos');
            header ('Location: ' . $ADMIN_MAIN_SITE);
            return;
        }
    }

    // Display the dialog for changing logos
    print_admin_header(get_translation('admin_logoedt_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-13', true, get_translation('admin_backtomain'));
    if ($errorString !="")
        print_error_message($errorString);

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    // Main logo
    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></td>');
    echo_r('    <td style="text-align: right">');
    echo_r('    <input type="submit" class="action_btn" name="logos_Reset" value="' . get_translation('admin_logoedt_reset') . '">');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    echo_r('<table width="100%">');
    echo_r('  <tr>');
    echo_r('    <td><b>' . get_translation('admin_logoedt_mainlogo') . ':</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo('    <td>' . $thmTheme->GetLogo() . ' [' . $thmTheme->GetLogoWidth() . ' x ' . $thmTheme->GetLogoHeight() . ' Pixel]');
    echo_r('<br><img src="' . $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetLogo() . '" alt="">');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td><input type="file" name="logos_MainLogo" size="60" value=""></td>');
    echo_r('  </tr>');
    echo_r('</table>');
    // Index logo
    echo_r('<hr>');
    echo_r('<table width="100%">');
    echo_r('  <tr>');
    echo_r('    <td><b>' . get_translation('admin_logoedt_indexlogo') . ':</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo('    <td>' . $thmTheme->GetIndexLogo() . ' [' . $thmTheme->GetIndexLogoWidth() . ' x ' . $thmTheme->GetIndexLogoHeight() . ' Pixel]');
    echo_r('<br><img src="' . $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetIndexLogo() . '" alt="">');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td><input type="file" name="logos_IndexLogo" size="60" value=""></td>');
    echo_r('  </tr>');
    echo_r('</table>');
    // Fav icon
    echo_r('<hr>');
    echo_r('<table width="100%">');
    echo_r('  <tr>');
    echo_r('    <td><b>' . get_translation('admin_logoedt_facivo') . ':</b></td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo('    <td>' . $thmTheme->GetFavIco());
    echo_r('<br><img src="' . $thmTheme->GetCompleteFolder() . $thmTheme->GetImagesFolder() . $thmTheme->GetFavIco() . '" alt="">');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('  <tr>');
    echo_r('    <td><input type="file" name="logos_FavIco" size="60" value=""></td>');
    echo_r('  </tr>');
    echo_r('</table>');

    print_bottom_links("logos_Save", get_translation('admin_save'),
                       "logos_Cancel", get_translation('admin_cancel'), false);

    echo_r('</form>');

    print_admin_footer();
?>