<?php

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

    filename: class_guestbookentry.php
*/
include "class_storagefile.php";

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

    class cGuestbookEntry
    {
        var $m_guestId;
        var $m_guestPageId;
        var $m_guestDate;
        var $m_guestEnterDate;
        var $m_guestTitle;
        var $m_guestName;
        var $m_guestEmail;
        var $m_guestText;
        var $m_guestLocked;
        var $m_guestFolder;
        var $m_guestFolderDeleted;

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

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

        function Init()
        {
            $this->m_guestId = "0";
            $this->m_guestPageId = "0";
            $this->m_guestDate = "";
            $this->m_guestEnterDate = "";
            $this->m_guestTitle = "";
            $this->m_guestName = "";
            $this->m_guestEmail = "";
            $this->m_guestText = "";
            $this->m_guestLocked = "0";

            $this->m_guestFolder = "";
            $this->m_guestFolderDeleted = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_guestId;
        }
        
        function GetPageId()
        {
            return $this->m_guestPageId;
        }
        function SetPageId($pageId)
        {
            $this->m_guestPageId = $pageId;
        }

        function GetDate()
        {
            return $this->m_guestDate;
        }
        
        function SetEnterDate($enterDate)
        {
            $this->m_guestEnterDate = $enterDate;
        }
        function GetEnterDate()
        {
            return $this->m_guestEnterDate;
        }

        function SetTitle($title)
        {
            $this->m_guestTitle = $this->RemoveHTMLTags($title);
        }
        function GetTitle()
        {
            return $this->m_guestTitle;
        }

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

        function SetEmail($email)
        {
            $this->m_guestEmail = str_replace("@", " <b>at</b> " ,$email);
        }
        function GetEmail()
        {
            return $this->m_guestEmail;
        }

        function SetText($text)
        {
            $this->m_guestText = $this->RemoveHTMLTags($text, true);
        }
        function GetText()
        {
            return $this->m_guestText;
        }

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

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

        function SetFolder($folder)
        {
            $this->m_guestFolder = $folder;
        }
        function SetFolderDeleted($userFolderDeleted)
        {
            $this->m_userFolderDeleted = $userFolderDeleted;
        }

        // Main methods
        function CreateGuestbookEntry()
        {
            $this->Init();
            $this->m_guestId = uniqid("gbe", FALSE);
            $this->m_guestDate = date("Y-m-d H:i:s");
        }

        function DeleteGuestbookEntryFinal()
        {
            return unlink($this->m_guestFolder . $this->m_guestId . ".gbe");
        }

        function DeleteGuestbookEntryFromId($guestId)
        {
            return unlink($this->m_guestFolder . $guestId . ".gbe");
        }

        function UnlockGuestbookEntry()
        {
            $m_guestLocked = "0";
            return $this->SaveGuestbookEntry();
        }

        function LockGuestbookEntry()
        {
            $m_guestLocked = "1";
            return $this->SaveGuestbookEntry();
        }

        function LoadGuestbookEntryFromId($guestId)
        {
            $this->m_guestId = $guestId;
            return $this->LoadGuestbookEntry();
        }

        function LoadGuestbookEntry()
        {
            $fileName = $this->m_guestFolder . $this->m_guestId  . ".gbe";
            //echo  $fileName;

            if ($this->m_guestId == "0" || $this->m_guestFolder == "")
                return false;

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

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

            $this->m_guestId = $cStorage->GetData("Id");
            $this->m_guestPageId = $cStorage->GetData("PageId");
            $this->m_guestDate = $cStorage->GetData("Date");
            $this->m_guestEnterDate = $cStorage->GetData("EnterDate");
            $this->m_guestTitle = $cStorage->GetData("Title");
            $this->m_guestName = $cStorage->GetData("Name");
            $this->m_guestEmail = $cStorage->GetData("Email");
            $this->m_guestText = $cStorage->GetData("Text");
            $this->m_guestLocked = $cStorage->GetData("Locked");

            return true;

        }

        function SaveGuestbookEntry()
        {
            $newRow = "\r\n";
            $fileName = $this->m_guestFolder . $this->m_guestId  . ".gbe";
            // echo $fileName;

            if ($this->m_guestId == "0" || $this->m_guestFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("guestbook_entry");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_guestId);
            $cStorage->AddData("PageId", $this->m_guestPageId);
            $cStorage->AddData("Date", $this->m_guestDate);
            $cStorage->AddData("EnterDate", $this->m_guestEnterDate);
            $cStorage->AddData("Title", $this->m_guestTitle);
            $cStorage->AddData("Name", $this->m_guestName);
            $cStorage->AddData("Email", $this->m_guestEmail);
            $cStorage->AddData("Text", $this->m_guestText);
            $cStorage->AddData("Locked", $this->m_guestLocked, false);

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

        function RemoveHTMLTags($string, $lineFeed = false)
        {
            $string = ereg_replace("<","&lt;", $string);
            $string = ereg_replace(">","&gt;", $string);
            $string = str_replace('\"', '"', $string);
            $string = str_replace("\'", "'", $string);
            
            if ($lineFeed == true)
            {
                $string = str_replace("\r\n", "<br>", $string);
                $string = str_replace("\r", "<br>", $string);
            }

            return ereg_replace("@", " <b>at</b> ",$string);
        }
        
        function NoHTMLLinefeed($string)
        {
            return str_replace("<br>", "\r\n", $string);
        }
        
        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}