<?php

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

File:         site_main.php
Description:  Main site

*/

    include "modules/mod_site.php";
    include "modules/class_textfile.php";
    include "modules/class_page.php";
    include "modules/class_gallery.php";
    include "modules/class_guestbookentry.php";
    include "modules/class_user.php";
    include "modules/class_userlist.php";

    // Construction?
    if ($SITE_SHOWCONSTRUCTION == true)
    {
        header ("location: " . $SITE_CONSTRUCTION_SITE);
        return;
    }

    $errorString = "";
    $cFile = new cTextFile;
    $pageList = new cPageList;
    $page = new cPage;
    $pageList->SetFolder(retrieve_absolute_path($PAGES_FOLDER));
    $page->SetFolder(retrieve_absolute_path($PAGES_FOLDER));

    $pageId = "";
    if (isset($_GET['page']) == true)
        $pageId = $_GET['page'];
    if ($pageId == "")
    {
        $pageList->LoadPageList(1); // Unlocked
        $pageList->SortPageList(4); // Nav pos
        $list = $pageList->GetPageList();
        $mainPage = "";
        foreach ($list as $value)
        {
            $page->LoadPageFromId($value);
            if ($pageId == "" || $page->IsMainPage() == true)
                $pageId = $page->GetId();
        }
    }

    // Failed to load, or page is locked?
    if ($page->LoadPageFromId($pageId) == false || $page->IsLocked() == true)
    {
        header ("location: " . $SITE_CONSTRUCTION_SITE);
        return;
    }
    // Override navigation levels?
    $naviShowLevelOverride = "";
    if (isset($_GET['nav_levels']) == true)
        $naviShowLevelOverride = $_GET['nav_levels'];
    if (isset($_POST['nav_levels']) == true)
        $naviShowLevelOverride = $_POST['nav_levels'];

    // Contact page?
    if ($page->GetType() == PAGETYPE_CONTACT)
    {
        if (isset($_POST['contact_name']) == false)
            $_POST['contact_name'] = "";
        if (isset($_POST['contact_email']) == false)
            $_POST['contact_email'] = "";
        if (isset($_POST['contact_message']) == false)
            $_POST['contact_message'] = "";

        // Send message?
        if (isset($_POST['contact_Send']) == true)
        {
            if ($_POST['contact_name'] == "" || $_POST['contact_email'] == "" || $_POST['contact_message'] == "")
            {
                $errorString = '<br>' . get_translation('site_contact_error') . ' <br>' . $LINE_FEED;
            }
            else
            {
            	if ($SITE_FORMSECURITY == "1" && ($_POST['formsec_check'] != $_POST['formsec_checkval']))
                	$errorString = '<br><b>' . get_translation('site_formsec_errorchecksum') . ' </b><br>' . $LINE_FEED;
				else
				{
                	send_mail($SITE_CONTACT_ADDRESS, get_translation('site_contact_messagefrom') . ' '  . $SITE_ADDRESS . ' - ' . $_POST['contact_name'], $_POST['contact_message'], "From:" . $_POST['contact_email']);
                	header ('location: ' . $SITE_MAIN_SITE);
				}
            }
        }
    }
    // Form page?
    if ($page->GetType() == PAGETYPE_FORM)
    {
        // Send form?
        if (isset($_POST['form_Send']) == true)
        {
            $cForm = new cForm;
            $cForm->SetFolder(retrieve_absolute_path($FORM_FOLDER));
            $cForm->LoadFormFromId($page->m_cForm->GetId());

            $errorFields = "";
            $strResponse = "";
            if ($SITE_FORMSECURITY == "1" && ($_POST['formsec_check'] != $_POST['formsec_checkval']))
                $errorString = '<br><b>' . get_translation('site_formsec_errorchecksum') . ' </b><br>' . $LINE_FEED;
			else
			{
                for ($i = 0; $i < $cForm->GetMaxFields(); $i ++)
            	{
                    if ($cForm->GetDescription($i) == "" || (
                        ($cForm->GetType($i) == FORMTYPE_RADIO || $cForm->GetType($i) == FORMTYPE_COMBOBOX) && $cForm->GetValue($i) == ""))
                   		continue;  // Invalid, skip...
            		if ($cForm->GetType($i) != FORMTYPE_CHECKBOX)
                	{
                    	if (isset($_POST['form_input' . $i]) == false || $_POST['form_input' .$i] == "")
                       	 	$errorFields = $errorFields . ' - ' . $cForm->GetDescription($i) . '<br>';
                	}
                	if ($errorFields == "")
                	{
                    	if (isset($_POST['form_input' . $i]) == false) // Checkboxes
                        	$strResponse = $strResponse . $cForm->GetDescription($i) . ': ' . $LINE_FEED;
                    	else
                        $strResponse = $strResponse . $cForm->GetDescription($i) . ': ' . $_POST['form_input' .$i] . $LINE_FEED;
                	}
            	}

            	if ($errorFields != "")
            	{
                	$errorString = '<br>' . get_translation('site_form_error') . ':<br>' . $errorFields . '<br>' . $LINE_FEED;
            	}
            	else
            	{
                	$receiver = $SITE_CONTACT_ADDRESS;
                	if ($cForm->GetAdditionalEMail() != "")  // Additional receivers?
                    	$receiver = $receiver . ', ' . $cForm->GetAdditionalEMail();
                	send_mail($receiver , get_translation('site_form_messagefrom') . ' '  . $SITE_ADDRESS, $strResponse, "From: <form_input>");
                	header ('location: ' . $SITE_MAIN_SITE);
            	}
			}
		}
    }
    // Guestbook page?
    if ($page->GetType() == PAGETYPE_GUESTBOOK)
    {
        $guestBookAdd = false;
        // Add entry?
        if (isset($_POST['guestbook_Add']) == true)
            $guestBookAdd = true;
        // Save entry?
        if (isset($_POST['guestbook_Save']) == true)
        {
            $guestBookAdd = true;
            if ((isset($_POST['guestbook_title']) == false || $_POST['guestbook_title'] == "") ||
                (isset($_POST['guestbook_name']) == false || $_POST['guestbook_name'] == "") ||
                (isset($_POST['guestbook_email']) == false || $_POST['guestbook_email'] == "") ||
                (isset($_POST['guestbook_text']) == false || $_POST['guestbook_text'] == ""))
                $errorString = '<br><b>' . get_translation('site_guestbook_error') . ' </b><br>' . $LINE_FEED;
            else
            {
                if ($SITE_FORMSECURITY == "1" && ($_POST['formsec_check'] != $_POST['formsec_checkval']))
                    $errorString = '<br><b>' . get_translation('site_formsec_errorchecksum') . ' </b><br>' . $LINE_FEED;
                else // Store entry
                {
                    $guestEntry = new cGuestbookEntry;
                    $guestEntry->CreateGuestbookEntry();
                    $guestEntry->SetFolder(retrieve_absolute_path($GUESTBOOK_FOLDER));
                    $guestEntry->SetPageId($page->GetId());
                    $guestEntry->SetEnterDate(date("d.m.Y H:i:s"));
                    $guestEntry->SetTitle($_POST['guestbook_title']);
                    $guestEntry->SetName($_POST['guestbook_name']);
                    $guestEntry->SetEmail($_POST['guestbook_email']);
                    $guestEntry->SetText($_POST['guestbook_text']);
                    if ($guestEntry->SaveGuestbookEntry() == false)
                        $errorString = '<br><b>' . get_translation('site_guestbook_errorsave') . ' </b><br>' . $LINE_FEED;
                    else
                        $guestBookAdd = false;
                }
            }
        }
    }
    // Newsletter page?
    if ($page->GetType() == PAGETYPE_NEWSLETTER)
    {
        // Subscribe/unsubcribe newsletter?
        if (isset($_POST['newsletter_Order']) == true || isset($_POST['newsletter_Cancel']) == true)
        {
            if (isset($_POST['newsletter_email']) == false || (!ereg("^.+@.+\\..+$", $_POST['newsletter_email'])))
            {
                $errorString = '<br><b>' . get_translation('site_newsletter_errormail') . ' </b><br>' . $LINE_FEED;
            }
            else
            {
            	if ($SITE_FORMSECURITY == "1" && ($_POST['formsec_check'] != $_POST['formsec_checkval']))
                	$errorString = '<br><b>' . get_translation('site_formsec_errorchecksum') . ' </b><br>' . $LINE_FEED;
				else
				{
                	$userList = new cUserList;
                	$userList->SetFolder(retrieve_absolute_path($USER_FOLDER));
                	if ($userList->SetNewsletterUserByEMail($_POST['newsletter_email'], isset($_POST['newsletter_Order'])) == false)
                    	$errorString = '<br><b>' . get_translation('site_newsletter_errorsave') . ' </b><br>' . $LINE_FEED;
                	else
                	{
                    	$subscription = "No";
                    	if (isset($_POST['newsletter_Order']) == true)
                        	$subscription = "Yes";

                    	log_message('User ' . $_POST['newsletter_email'] . ' newsletter subscription=' . $subscription);
                    	header ('location: ' . $SITE_MAIN_SITE);
                	}
				}
			}
        }
    }

    $contPage = build_page($SITE_THEME, $pageId, true, true, $naviShowLevelOverride);
    $contPage = fill_titles($contPage, $SITE_TITLE . ' - ' . $page->GetNavigationTitle(), $page->GetTitle());
    // Error occured?
    if ($errorString != "")
        $contPage = build_error_message($errorString, $contPage);
    
    // Main content
    $contMain = $cFile->LoadFromFile(retrieve_absolute_path($page->GetContentFile()));
    // Correct image paths for ouput
    $contMain = str_replace($PAGEIMAGES_FOLDER, retrieve_absolute_path($PAGEIMAGES_FOLDER), $contMain);
    // Counter on main page?
    if ($page->IsMainPage() == true)
        $contMain = $contMain . build_site_counter();

    // Additional content
    $contAdditional = "";
    // Conatct page?
    if ($page->GetType() == PAGETYPE_CONTACT)
        $contAdditional = $contAdditional . build_site_contact($naviShowLevelOverride, $page->GetAdditionalParameter2());
    // Form page?
    if ($page->GetType() == PAGETYPE_FORM)
        $contAdditional = $contAdditional . build_site_form($page->m_cForm->GetId(), $naviShowLevelOverride, $page->GetAdditionalParameter2());
    // Gallery page?
    if ($page->GetType() == PAGETYPE_GALLERY)
        $contAdditional = $contAdditional . build_site_gallery($page->m_cGallery->GetId(), $page->GetAdditionalColCount(),
                                                               $page->GetAdditionalParameter(), $page->GetAdditionalParameter2(),
                                                               $page->GetAdditionalThumbWidth(), $page->GetAdditionalThumbHeight(),
                                                               $page->GetAdditionalImgWidth(), $page->GetAdditionalImgHeight(),
                                                               $page->GetAdditionalParameter3());
    // YouTube gallery page?
    if ($page->GetType() == PAGETYPE_YOUTUBEGALLERY)
        $contAdditional = $contAdditional . build_site_youtubegallery($page->m_cYouTubeGallery->GetId(), $page->GetAdditionalColCount(),
                                                                      $page->GetAdditionalParameter(), $page->GetAdditionalParameter2(),
                                                                      $page->GetAdditionalImgWidth(), $page->GetAdditionalImgHeight());
    // Link list page?
    if ($page->GetType() == PAGETYPE_LINKS)
        $contAdditional = $contAdditional . build_site_linklist($page->m_cLinkList->GetId(), $page->GetAdditionalColCount(),
                                                                $page->GetAdditionalParameter(), $page->GetAdditionalParameter2());
    // Sitemap page?
    if ($page->GetType() == PAGETYPE_SITEMAP)
        $contAdditional = $contAdditional . build_site_sitemap();
    // Newsletter page?
    if ($page->GetType() == PAGETYPE_NEWSLETTER)
        $contAdditional = $contAdditional . build_site_newsletter($naviShowLevelOverride, $page->GetAdditionalParameter2());
    // Search page?
    if ($page->GetType() == PAGETYPE_SEARCH)
        $contAdditional = $contAdditional . build_site_search($naviShowLevelOverride, $page->GetAdditionalParameter2());
    // Guestbook page?
    if ($page->GetType() == PAGETYPE_GUESTBOOK)
    {
        if ($guestBookAdd == false)
            $contAdditional = $contAdditional . build_site_guestbook_content($pageId, $naviShowLevelOverride, $page->GetAdditionalParameter2());
        else
            $contAdditional = $contAdditional . build_site_guestbook_add($pageId, $naviShowLevelOverride, $page->GetAdditionalParameter2());
    }
    // Do some statistic...
    $page->IncreaseViewCount();

    // Put content together
    $contContent = build_site_content($page->GetContentPosition(), $contMain, $contAdditional);
    $contPage = fill_content($contPage, $contContent, $page->GetAdditionalContent(), $page->GetAdditionalContent2());
    $contPage = fill_emptyvars($contPage);

    // Final injections into body or head?
    if ($page->GetType() == PAGETYPE_GALLERY)
        $contPage = build_site_gallery_headincludes($page->m_cGallery->GetId(), $contPage);

    echo_r($contPage);
?>