<?php

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

File:         edit_configuration.php
Description:  Administrative module to edit configuration

*/

    include "../modules/mod_admin.php";
    include "../modules/class_textfile.php";
    include "../modules/class_theme.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 config?
    if (isset($_POST['config_Cancel']))
    {
        header('location: ' . $ADMIN_MAIN_SITE);
        return;
    }
    
    // Save config?
    if (isset($_POST['config_Save']))
    {
        // Website title
        if (isset($_POST['config_Title']) && $_POST['config_Title'] != "")
            $cConfig->SetParameter("site_title", $_POST['config_Title']);
        else
            $errorString = get_translation('admin_config_errortitle') . "!";
        // Website charset
        if (isset($_POST['config_Charset']) && $_POST['config_Charset'] != "")
            $cConfig->SetParameter("site_charset", $_POST['config_Charset']);
        else
            $errorString = get_translation('admin_config_errorcharset') . "!";
        // Website description
        if (isset($_POST['config_Description']) && $_POST['config_Description'] != "")
            $cConfig->SetParameter("site_description", $_POST['config_Description']);
        else
            $errorString = get_translation('admin_config_errordescription') . "!";
        // Website tags
        if (isset($_POST['config_Tags']) && $_POST['config_Tags'] != "")
            $cConfig->SetParameter("site_tags", $_POST['config_Tags']);
        else
            $errorString = get_translation('admin_config_errormetatags') . "!";
        // Website address
        if (isset($_POST['config_Url']) && $_POST['config_Url'] != "")
            $cConfig->SetParameter("site_url", $_POST['config_Url']);
        else
            $errorString = get_translation('admin_config_errorurl') . "!";
        // Website revisit
        if (isset($_POST['config_Revisit']) && $_POST['config_Revisit'] != "")
            $cConfig->SetParameter("site_revisit", $_POST['config_Revisit']);
        else
            $errorString = get_translation('admin_config_errorrevisit') . "!";
        // Website contact email
        if (isset($_POST['config_ContactEmail']) && $_POST['config_ContactEmail'] != "")
            $cConfig->SetParameter("site_contact_email", $_POST['config_ContactEmail']);
        else
            $errorString = get_translation('admin_config_errorcontactemail') . "!";
        // Form security
        $cConfig->SetParameter("site_enable_formsecurity", isset($_POST['config_EnableFormSecurity']));
        // Website counter
        $cConfig->SetParameter("site_show_counter", isset($_POST['config_ShowCounter']));
        // WYSIWYG editor
        $cConfig->SetParameter("admin_usewysiwyg", isset($_POST['config_UseWYSIWYG']));
        // Wrap editor
        $cConfig->SetParameter("admin_wrapeditor", isset($_POST['config_WrapEditor']));
        // Language
        if (isset($_POST['config_Language']) == false)
            $_POST['config_Language'] = "en";
        $cConfig->SetParameter("admin_language", $_POST['config_Language']);
        // Image restrictions
        $cConfig->SetParameter("site_imagerestriction", isset($_POST['config_ImageRestriction']));
        if (isset($_POST['config_ImgBgColor']) == false || $_POST['config_ImgBgColor'] == "")
            $_POST['config_ImgBgColor'] = "0x00FFFFFF"; // default it
        $cConfig->SetParameter("site_imagebgcolor", $_POST['config_ImgBgColor']);
        if (isset($_POST['config_MaxImgWidth']) == false || $_POST['config_MaxImgWidth'] == "")
            $_POST['config_MaxImgWidth'] = 600; // default it
        $cConfig->SetParameter("site_maximgwidth", $_POST['config_MaxImgWidth']);
        if (isset($_POST['config_MaxImgHeight']) == false || $_POST['config_MaxImgHeight'] == "")
            $_POST['config_MaxImgHeight'] = 600; // default it
        $cConfig->SetParameter("site_maximgheight", $_POST['config_MaxImgHeight']);
        // Additional content usage
        $cConfig->SetParameter("admin_additionalcontent", isset($_POST['config_AdditionalContent']));
        // Special navigation
        $cConfig->SetParameter("admin_specialnavigation", isset($_POST['config_SpecialNavigation']));
        // HTML preview
        $cConfig->SetParameter("admin_htmlpreview", isset($_POST['config_HTMLPreview']));
        // External admin links
        $cConfig->SetParameter("admin_useexternallinks", isset($_POST['config_UseExternalLinks']));
        $cConfig->SetParameter("admin_externallink1", $_POST['config_ExternalLink1']);
        if ($_POST['config_ExternalLink1'] != "" && $_POST['config_ExternalLinkDescription1'] == "")
            $_POST['config_ExternalLinkDescription1'] = "Link 1";
        $cConfig->SetParameter("admin_externallinkdescription1", $_POST['config_ExternalLinkDescription1']);
        $cConfig->SetParameter("admin_externallink2", $_POST['config_ExternalLink2']);
        if ($_POST['config_ExternalLink2'] != "" && $_POST['config_ExternalLinkDescription2'] == "")
            $_POST['config_ExternalLinkDescription2'] = "Link 2";
        $cConfig->SetParameter("admin_externallinkdescription2", $_POST['config_ExternalLinkDescription2']);
        $cConfig->SetParameter("admin_externallink3", $_POST['config_ExternalLink3']);
        if ($_POST['config_ExternalLink3'] != "" && $_POST['config_ExternalLinkDescription3'] == "")
            $_POST['config_ExternalLinkDescription3'] = "Link 3";
        $cConfig->SetParameter("admin_externallinkdescription3", $_POST['config_ExternalLinkDescription3']);

        if ($errorString == "")
        {
            if ($cConfig->SaveConfigFile() == false)
                $errorString = get_translation('admin_config_errorsave') . "!";
            else
            {
                // Done, now update the index.htm forward page
                $thmTheme = new cTheme;
                $thmTheme->SetFolder($THEME_FOLDER);
                $thmTheme->LoadThemeFromId($SITE_THEME);
                update_index_file($thmTheme->GetCompleteFolder() . $thmTheme->GetIndexFile(), $SITE_INDEX_FILE);

                log_message ('User ' . get_user_name() . ' saved configuration');
                header('location: ' . $ADMIN_MAIN_SITE);
                return;
            }
        }
    }

    if ($cConfig->LoadConfigFile() == false)
        $errorString = get_translation('admin_config_errorload') . "!";
        
    // Display
    print_admin_header(get_translation('admin_config_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-9', true, get_translation('admin_backtomain'), "", "", false);
    echo_r('<script language="JavaScript" type="Text/JavaScript">');
    echo_r('function changeImageRestriction()');
    echo_r('{');
    echo_r('  document.getElementById(\'configImageMeasurements\').style.display = \'\';');
    echo_r('  if (document.getElementById(\'configImageRestriction\').checked == false)');
    echo_r('      document.getElementById(\'configImageMeasurements\').style.display = \'none\';');
    echo_r('}');
    echo_r('function changeUseExternalLinks()');
    echo_r('{');
    echo_r('  document.getElementById(\'configExternalLinks\').style.display = \'\';');
    echo_r('  if (document.getElementById(\'configUseExternalLinks\').checked == false)');
    echo_r('      document.getElementById(\'configExternalLinks\').style.display = \'none\';');
    echo_r('}');
    echo_r('</script>');
    finish_admin_header(get_translation('admin_config_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-9', true, get_translation('admin_backtomain'), "", "");
    if ($errorString != "")
        print_error_message ($errorString);

    echo_r ('<form method="post" action="">');
    // Website title
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_webtitle') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_Title" size="50" value="' . $cConfig->GetParameter("site_title") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website charset format
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_charset') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_Charset" size="50" value="' . $cConfig->GetParameter("site_charset") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website description
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_description') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_Description" size="80" value="' . $cConfig->GetParameter("site_description") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website tags
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_metatags') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_Tags" size="80" value="' . $cConfig->GetParameter("site_tags") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website address
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_weburl') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_Url" size="40" value="' . $cConfig->GetParameter("site_url") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website revisit
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_revisit') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_Revisit" size="10" value="' . $cConfig->GetParameter("site_revisit") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website contact email
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_contactemail') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_ContactEmail" size="50" value="' . $cConfig->GetParameter("site_contact_email") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
	// Form security
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_formsecurity') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_EnableFormSecurity" value="1"');
    if ($cConfig->GetParameter("site_enable_formsecurity") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Website counter
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_showcounter') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_ShowCounter" value="1"');
    if ($cConfig->GetParameter("site_show_counter") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Language
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_language') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <select name="config_Language">');
    echo   ('        <option value="de"');
    if ($cConfig->GetParameter("admin_language") == "de")
        echo(' selected');
    echo_r ('>' . get_translation('admin_config_languagegerman') . '</option>');
    echo   ('        <option value="en"');
    if ($cConfig->GetParameter("admin_language") == "en")
        echo(' selected');
    echo_r ('>' . get_translation('admin_config_languageenglish') . '</option>');
    echo_r ('      </select>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // WYSIWYG editor
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_usewysiwygeditor') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_UseWYSIWYG" value="1"');
    if ($cConfig->GetParameter("admin_usewysiwyg") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Word wrap
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_wrapeditor') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_WrapEditor" value="1"');
    if ($cConfig->GetParameter("admin_wrapeditor") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    echo_r ('<hr>');
    // Enable image restrictions
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_imagerestriction') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_ImageRestriction" value="1" id="configImageRestriction" onChange="changeImageRestriction();" onClick="changeImageRestriction();"');
    if ($cConfig->GetParameter("site_imagerestriction") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Image restrictions
    echo   ('<table border="0" width="100%" id="configImageMeasurements"');
    if ($cConfig->GetParameter("site_imagerestriction") != "1")
        echo (' style="display:none"');
    echo_r ('>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_maximagewidth') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_MaxImgWidth" size="5" value="' . $cConfig->GetParameter("site_maximgwidth") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_maximageheight') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_MaxImgHeight" size="5" value="' . $cConfig->GetParameter("site_maximgheight") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Image background color
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_imagebgcolor') . ':</td>');
    echo_r ('    <td>');
    echo_r ('      <input type="text" name="config_ImgBgColor" size="10" value="' . $cConfig->GetParameter("site_imagebgcolor") . '">');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Additional page content usage
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_useadditionalcontent') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_AdditionalContent" value="1"');
    if ($cConfig->GetParameter("admin_additionalcontent") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    // Sepcial navigation
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_usespecialnavigation') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_SpecialNavigation" value="1"');
    if ($cConfig->GetParameter("admin_specialnavigation") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // HTML preview
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_htmlpreview') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_HTMLPreview" value="1"');
    if ($cConfig->GetParameter("admin_htmlpreview") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // Use external admin links
    echo_r ('<hr>');
    echo_r ('<table border="0" width="100%">');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_useexternallinks') . ':</td>');
    echo_r ('    <td>');
    echo   ('      <input type="checkbox" name="config_UseExternalLinks" value="1" id="configUseExternalLinks" onChange="changeUseExternalLinks();" onClick="changeUseExternalLinks();"');
    if ($cConfig->GetParameter("admin_useexternallinks") == "1")
        echo (' checked');
    echo_r ('>');
    echo_r ('    </td>');
    echo_r ('  </tr>');
    echo_r ('</table>');
    // External admin linkss
    echo   ('<table border="0" width="100%" id="configExternalLinks"');
    if ($cConfig->GetParameter("admin_useexternallinks") != "1")
        echo (' style="display:none"');
    echo_r ('>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_externallink') . ' 1:</td>');
    echo_r ('    <td><input type="text" name="config_ExternalLink1" size="40" value="' . $cConfig->GetParameter("admin_externallink1") . '"> - ');
    echo_r (get_translation('admin_config_externallinkdescription') . ' 1: <input type="text" name="config_ExternalLinkDescription1" size="30" value="' . $cConfig->GetParameter("admin_externallinkdescription1") . '"></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_externallink') . ' 2:</td>');
    echo_r ('    <td><input type="text" name="config_ExternalLink2" size="40" value="' . $cConfig->GetParameter("admin_externallink2") . '"> - ');
    echo_r (get_translation('admin_config_externallinkdescription') . ' 2: <input type="text" name="config_ExternalLinkDescription2" size="30" value="' . $cConfig->GetParameter("admin_externallinkdescription2") . '"></td>');
    echo_r ('  </tr>');
    echo_r ('  <tr>');
    echo_r ('    <td width="30%">' . get_translation('admin_config_externallink') . ' 3:</td>');
    echo_r ('    <td><input type="text" name="config_ExternalLink3" size="40" value="' . $cConfig->GetParameter("admin_externallink3") . '"> - ');
    echo_r (get_translation('admin_config_externallinkdescription') . ' 3: <input type="text" name="config_ExternalLinkDescription3" size="30" value="' . $cConfig->GetParameter("admin_externallinkdescription3") . '"></td>');
    echo_r ('  </tr>');
    echo_r ('</table>');

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

    print_admin_footer();
?>