<?php

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

Filename:     class_theme.php
Description:  Class cTheme definition,
              to load and save theme files
              
*/
include "class_storagefile.php";
include "class_systemfile.php";

if (!defined ("CLASS_THEME"))
{
    define ("CLASS_THEME", "defined");

    define ("THEMEAUTHOR_NONE", "Anonymus");
    
    define ("THEMEPATTERN_DEFAULT", "theme*.*");


    class cTheme
    {
        var $m_thmId;
        var $m_thmDate;
        var $m_thmAuthor;
        var $m_thmName;
        var $m_thmDescription;
        var $m_thmThemeFolder;
        var $m_thmLogo;
        var $m_thmLogoDefault;
        var $m_thmLogoWidth;
        var $m_thmLogoHeight;
        var $m_thmIndexLogo;
        var $m_thmIndexLogoDefault;
        var $m_thmIndexLogoWidth;
        var $m_thmIndexLogoHeight;
        var $m_thmFavIco;
        var $m_thmFavIcoDefault;

        // Hardcoded for first step
        var $m_thmFilesFolder;
        var $m_thmImagesFolder;
        var $m_thmMainFile;
        var $m_thmAdditionalMainFiles;
        var $m_thmFilePattern;
        var $m_thmNavigationFile;
        var $m_thmNavigationEntryFile;
        var $m_thmFooterFile;
        var $m_thmIndexFile;
        var $m_thmCSSFile;

        var $m_thmLocked;
        var $m_thmFolder;

        function cTheme()  // PHP 4
        {
            $this->Init();
        }

        function __construct()
        {
            $this->Init();
        }

        function Init()
        {
            $this->m_thmId = "0";
            $this->m_thmDate = "";
            $this->m_thmAuthor = "";
            $this->m_thmName = "";
            $this->m_thmDescription = "";
            $this->m_thmThemeFolder = "";
            $this->m_thmLogo = "";
            $this->m_thmLogoDefault = "";
            $this->m_thmLogoWidth = "";
            $this->m_thmLogoHeight = "";
            $this->m_thmIndexLogo = "";
            $this->m_thmIndexLogoDefault = "";
            $this->m_thmIndexLogoWidth = "";
            $this->m_thmIndexLogoHeight = "";
            $this->m_thmFavIco = "";
            $this->m_thmFavIcoDefault = "";

            // Hardcoded for first step
            $this->m_thmFilesFolder = "theme_files/";
            $this->m_thmImagesFolder = "theme_images/";
            $this->m_thmMainFile = "theme_main.htm";
            $this->m_thmAdditionalMainFiles = array();
            $this->m_thmFilePattern = THEMEPATTERN_DEFAULT;
            $this->m_thmNavigationFile = "theme_navigation.htm";
            $this->m_thmNavigationEntryFile = "theme_navigation_entry.htm";
            $this->m_thmFooterFile = "theme_footer.htm";
            $this->m_thmIndexFile = "theme_index.htm";
            $this->m_thmCSSFile = "theme.css";

            $this->m_thmLocked = "0";
            $this->m_thmFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_thmId;
        }
        
        function GetDate()
        {
            return $this->m_thmDate;
        }
        
        function GetAuthor()
        {
            return $this->m_thmAuthor;
        }

        function GetName()
        {
            return $this->m_thmName;
        }
        function SetName($name)
        {
            $this->m_thmName = $name;
        }

        function GetDescription()
        {
            return $this->m_thmName;
        }
        function SetDescription($description)
        {
            $this->m_thmName = $description;
        }

        function GetThemeFolder()
        {
            return $this->m_thmThemeFolder;
        }
        function SetThemeFolder($themeFolder)
        {
            $this->m_thmThemeFolder = $themeFolder;
        }

        function GetLogo()
        {
            return $this->m_thmLogo;
        }
        function SetLogo($logo)
        {
            $this->m_thmLogo = $logo;
        }
        function GetLogoDefault()
        {
            return $this->m_thmLogoDefault;
        }
        function SetLogoDefault($logo)
        {
            $this->m_thmLogoDefault = $logo;
        }
        function GetLogoWidth()
        {
            return $this->m_thmLogoWidth;
        }
        function SetLogoWidth($logoWidth)
        {
            $this->m_thmLogoWidth = $logoWidth;
        }
        function GetLogoHeight()
        {
            return $this->m_thmLogoHeight;
        }
        function SetLogoHeight($logoHeight)
        {
            $this->m_thmLogoHeight = $logoHeight;
        }

        function GetIndexLogo()
        {
            return $this->m_thmIndexLogo;
        }
        function SetIndexLogo($logo)
        {
            $this->m_thmIndexLogo = $logo;
        }
        function GetIndexLogoDefault()
        {
            return $this->m_thmIndexLogoDefault;
        }
        function SetIndexLogoDefault($logo)
        {
            $this->m_thmIndexLogoDefault = $logo;
        }
        function GetIndexLogoWidth()
        {
            return $this->m_thmIndexLogoWidth;
        }
        function SetIndexLogoWidth($logoWidth)
        {
            $this->m_thmIndexLogoWidth = $logoWidth;
        }
        function GetIndexLogoHeight()
        {
            return $this->m_thmIndexLogoHeight;
        }
        function SetIndexLogoHeight($logoHeight)
        {
            $this->m_thmIndexLogoHeight = $logoHeight;
        }

        function GetFavIco()
        {
            return $this->m_thmFavIco;
        }
        function SetFavIco($ico)
        {
            $this->m_thmFavIco = $ico;
        }
        function GetFavIcoDefault()
        {
            return $this->m_thmFavIcoDefault;
        }
        function SetFavIcoDefault($ico)
        {
            $this->m_thmFavIcoDefault = $ico;
        }

        function GetFilesFolder()
        {
            return $this->m_thmFilesFolder;
        }
        
        function GetImagesFolder()
        {
            return $this->m_thmImagesFolder;
        }

        function GetMainFile()
        {
            return $this->m_thmMainFile;
        }
        function GetAdditionalMainFiles()
        {
            return $this->m_thmAdditionalMainFiles;
        }

        function GetNavigationFile()
        {
            return $this->m_thmNavigationFile;
        }

        function GetNavigationEntryFile()
        {
            return $this->m_thmNavigationEntryFile;
        }

        function GetFooterFile()
        {
            return $this->m_thmFooterFile;
        }

        function GetIndexFile()
        {
            return $this->m_thmIndexFile;
        }

        function GetCSSFile()
        {
            return $this->m_thmCSSFile;
        }

        function IsLocked()
        {
            return ($this->m_thmLocked == "1");
        }

        function SetLocked($bLock)
        {
            $this->m_thmLocked = "0";
            if ($bLock == true)
                $this->m_thmLocked = "1";
            return $this->SavePage();
        }

        function SetFolder($folder)
        {
            $this->m_thmFolder = $folder;
        }
        function GetFolder()
        {
            return $this->m_thmFolder;
        }

        // Main methods
        function CreateTheme($author = PAGEAUTHOR_NONE)
        {
//            $this->Init();
            $cFile = new cSystemFile;
            
            $uId = uniqid("", FALSE);
            $this->m_thmId = "wkthm" . $uId;
            $this->m_thmDate = date("Y-m-d H:i:s");
            $this->m_thmAuthor = $author;
            // Create main file
            if ($this->CreateThemeFile($this->GetCompleteFolder() . $this->m_thmMainFile) == false)
                return false;
            // Create navigation file
            if ($this->CreateThemeFile($this->GetCompleteFolder() . $this->m_thmNavigationFile) == false)
                return false;
            // Create navigation entry file
            if ($this->CreateThemeFile($this->GetCompleteFolder() . $this->m_thmNavigationEntryFile) == false)
                return false;
            // Create footer file
            if ($this->CreateThemeFile($this->GetCompleteFolder() . $this->m_thmFooterFile) == false)
                return false;
            // Create index file
            if ($this->CreateThemeFile($this->GetCompleteFolder() . $this->m_thmIndexFile) == false)
                return false;
            // Create CSS file
            if ($this->CreateThemeFile($this->GetCompleteFolder() . $this->m_thmCSSFile) == false)
                return false;

            // Create images folder
            if ($cFile->CreateFolder($this->GetCompleteFolder() . $this->m_thmImagesFolder) == false)
                return false;
            // Create files folder
            if ($cFile->CreateFolder($this->GetCompleteFolder() . $this->m_thmFilesFolder) == false)
                return false;

            return true;
        }

        function DeleteThemeFinal()
        {
            $fileName = "";
            $cFile = new cSystemFile;
            // Delete main file
            $fileName = $this->GetCompleteFolder() . $this->m_thmMainFile;
            if (file_exists($fileName) == true && unlink($fileName) == false)
                return false;
            // Delete navigation file
            $fileName = $this->GetCompleteFolder() . $this->m_thmNavigationFile;
            if (file_exists($fileName) == true && unlink($fileName) == false)
                return false;
            // Delete navigation entry file
            $fileName = $this->GetCompleteFolder() . $this->m_thmNavigationEntryFile;
            if (file_exists($fileName) == true && unlink($fileName) == false)
                return false;
            // Delete footer file
            $fileName = $this->GetCompleteFolder() . $this->m_thmFooterFile;
            if (file_exists($fileName) == true && unlink($fileName) == false)
                return false;
            // Delete index file
            $fileName = $this->GetCompleteFolder() . $this->m_thmIndexFile;
            if (file_exists($fileName) == true && unlink($fileName) == false)
                return false;
            // Delete CSS file
            $fileName = $this->GetCompleteFolder() . $this->m_thmCSSFile;
            if (file_exists($fileName) == true && unlink($fileName) == false)
                return false;
                


            // Delete images folder
            if ($cFile->DeleteFolder($this->GetCompleteFolder() . $this->m_thmImagesFolder) == false)
                return false;
            // Delete files folder
            if ($cFile->DeleteFolder($this->GetCompleteFolder() . $this->m_thmFilesFolder) == false)
                return false;
            // Delete by pattern
            $pattern = $this->m_thmFilePattern;
            if ($pattern == "")
                $pattern = THEMEPATTERN_DEFAULT;
            $cFile->DeleteFilesByPattern($this->GetCompleteFolder() . $pattern);

            // Finally theme file
            return unlink($this->m_thmFolder . $this->m_thmId . ".wkthm");
        }

        function DeleteThemeFinalFromId($themeId)
        {
            $this->m_thmId = $themeId;
            return DeletePageFinal();
        }


        function LoadThemeFromId($themeId)
        {
            $this->m_thmId = $themeId;
            return $this->LoadTheme();
        }

        function LoadTheme()
        {
            $fileName = $this->m_thmFolder . $this->m_thmId  . ".wkthm";
            //echo  $fileName;

            if ($this->m_thmId == "0" || $this->m_thmFolder == "")
                return false;

            if (file_exists($fileName) == false)
                return false;

            $cStorage = new cStorageFile;
            if ($cStorage->LoadStorageFromFile($fileName) == false)
                return false;

            if ($cStorage->GetData("Id") == "")
                return false;

            $this->m_thmId = $cStorage->GetData("Id");
            $this->m_thmDate = $cStorage->GetData("Date");
            $this->m_thmAuthor = $cStorage->GetData("Author");
            $this->m_thmName = $cStorage->GetData("Name");
            $this->m_thmDescription = $cStorage->GetData("Description");
            $this->m_thmThemeFolder = $cStorage->GetData("ThemeFolder");
            $this->m_thmLogo = $cStorage->GetData("LogoFile");
            $this->m_thmLogoDefault = $cStorage->GetData("LogoFileDefault");
            $this->m_thmLogoWidth = $cStorage->GetData("LogoWidth");
            $this->m_thmLogoHeight = $cStorage->GetData("LogoHeight");
            $this->m_thmIndexLogo = $cStorage->GetData("IndexLogoFile");
            $this->m_thmIndexLogoDefault = $cStorage->GetData("IndexLogoFileDefault");
            $this->m_thmIndexLogoWidth = $cStorage->GetData("IndexLogoWidth");
            $this->m_thmIndexLogoHeight = $cStorage->GetData("IndexLogoHeight");
            $this->m_thmFavIco = $cStorage->GetData("FavIcoFile");
            $this->m_thmFavIcoDefault = $cStorage->GetData("FavIcoFileDefault");
            $this->m_thmFilesFolder = $cStorage->GetData("FilesFolder");
            $this->m_thmImagesFolder = $cStorage->GetData("ImagesFolder");
            $this->m_thmMainFile = $cStorage->GetData("MainFile");
            $this->m_thmAdditionalMainFiles = explode("-|-", $cStorage->GetData("AdditionalMainFiles"));
            $this->m_thmFilePattern = $cStorage->GetData("FilePattern");
            $this->m_thmNavigationFile = $cStorage->GetData("NavigationFile");
            $this->m_thmNavigationEntryFile = $cStorage->GetData("NavigationEntryFile");
            $this->m_thmFooterFile = $cStorage->GetData("FooterFile");
            $this->m_thmIndexFile = $cStorage->GetData("IndexFile");
            $this->m_thmCSSFile = $cStorage->GetData("CSSFile");
            $this->m_thmLocked = $cStorage->GetData("Locked");

            return true;
        }

        function SaveTheme()
        {
            $newRow = "\r\n";
            $fileName = $this->m_thmFolder . $this->m_thmId  . ".wkthm";
            // echo $fileName;

            if ($this->m_thmId == "0" || $this->m_thmFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("theme");
            $cStorage->SetVersion("1.000");
            
            $cStorage->AddData("Id", $this->m_thmId);
            $cStorage->AddData("Date", $this->m_thmDate);
            $cStorage->AddData("Author", $this->m_thmAuthor);
            $cStorage->AddData("Name", $this->m_thmName);
            $cStorage->AddData("Description", $this->m_thmDescription);
            $cStorage->AddData("ThemeFolder", $this->m_thmThemeFolder);
            $cStorage->AddData("LogoFile", $this->m_thmLogo);
            $cStorage->AddData("LogoFileDefault", $this->m_thmLogoDefault);
            $cStorage->AddData("LogoWidth", $this->m_thmLogoWidth);
            $cStorage->AddData("LogoHeight", $this->m_thmLogoHeight);
            $cStorage->AddData("IndexLogoFile", $this->m_thmIndexLogo);
            $cStorage->AddData("IndexLogoFileDefault", $this->m_thmIndexLogoDefault);
            $cStorage->AddData("IndexLogoWidth", $this->m_thmIndexLogoWidth);
            $cStorage->AddData("IndexLogoHeight", $this->m_thmIndexLogoHeight);
            $cStorage->AddData("FavIcoFile", $this->m_thmFavIco);
            $cStorage->AddData("FavIcoFileDefault", $this->m_thmFavIcoDefault);
            $cStorage->AddData("FilesFolder", $this->m_thmFilesFolder);
            $cStorage->AddData("ImagesFolder", $this->m_thmImagesFolder);
            $cStorage->AddData("MainFile", $this->m_thmMainFile);
            $cStorage->AddData("AdditionalMainFiles", implode('-|-', $this->m_thmAdditionalMainFiles));
            $cStorage->AddData("FilePattern", $this->m_thmFilePattern);
            $cStorage->AddData("NavigationFile", $this->m_thmNavigationFile);
            $cStorage->AddData("NavigationEntryFile", $this->m_thmNavigationEntryFile);
            $cStorage->AddData("FooterFile", $this->m_thmFooterFile);
            $cStorage->AddData("IndexFile", $this->m_thmIndexFile);
            $cStorage->AddData("CSSFile", $this->m_thmCSSFile);
            $cStorage->AddData("Locked", $this->m_thmLocked, false);

            return $cStorage->SaveStorageToFile($fileName);
        }

        function InstallTheme($folder, $imagesFolder, $filesFolder, $favicoFile)
        {
            $cFile = new cSystemFile;
            $bResult = true;
            // Clean folders first
            $cFile->CleanFolderContent($imagesFolder, "", "html");
            $cFile->CleanFolderContent($filesFolder, "", "html");

            // Copy images files
            $copyfolder = $this->GetCompleteFolder() . $this->m_thmImagesFolder;
            $cFile->CopyFolder($copyfolder, $imagesFolder);
            // Copy files files
            $copyfolder = $this->GetCompleteFolder() . $this->m_thmFilesFolder;
            $cFile->CopyFolder($copyfolder, $filesFolder);
            // Delete by pattern
            $pattern = $this->m_thmFilePattern;
            if ($pattern == "")
                $pattern = THEMEPATTERN_DEFAULT;
            $cFile->DeleteFilesByPattern($folder . $pattern);

            // Copy the main file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmMainFile, $folder . $this->m_thmMainFile) == false)
                $bResult = false;
            // Copy main additional files
            for ($i = 0; $i < count($this->m_thmAdditionalMainFiles); $i ++)
            {
                if ($this->m_thmAdditionalMainFiles[$i] != "")
                {
                    if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmAdditionalMainFiles[$i], $folder . $this->m_thmAdditionalMainFiles[$i]) == false)
                    {
                        $bResult = false;
                        break;
                    }
                }
            }
            // Copy the navigation file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmNavigationFile, $folder . $this->m_thmNavigationFile) == false)
                $bResult = false;
            // Copy the navigation entry file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmNavigationEntryFile, $folder . $this->m_thmNavigationEntryFile) == false)
                $bResult = false;
            // Copy the footer file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmFooterFile, $folder . $this->m_thmFooterFile) == false)
                $bResult = false;
            // Copy the footer file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmIndexFile, $folder . $this->m_thmIndexFile) == false)
                $bResult = false;
            // Copy the CSS file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmCSSFile, $folder . $this->m_thmCSSFile) == false)
                $bResult = false;
            // Copy the favico file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmFavIco, $favicoFile) == false)
                $bResult = false;
            return $bResult;
        }

        function ResetLogos()
        {
            $cFile = new cSystemFile;
            $bResult = true;
            // Copy the main logo file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmLogoDefault, $this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmLogo) == false)
                $bResult = false;
            // Copy the index logo file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmIndexLogoDefault, $this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmIndexLogo) == false)
                $bResult = false;
            // Copy the favico file
            if ($cFile->CopyFile($this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmFavIcoDefault, $this->GetCompleteFolder() . $this->m_thmImagesFolder . $this->m_thmFavIco) == false)
                $bResult = false;

            return $bResult;

        }

        // Helpers
        function GetCompleteFolder()
        {
            return $this->m_thmFolder . $this->m_thmThemeFolder;
        }

        function CreateThemeFile($fileName)
        {
            if (file_exists($fileName) == false)
            {
                $fp = fopen($fileName, "w");
                if ($fp == false)
                    return false;
                fclose($fp);
            }
            return true;
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}