<?php

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

Filename:     class_pagefile.php
Description:  Class cPageFile definition,
              to load and save pagefiles

*/
include "class_storagefile.php";

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

    define ("PAGEFILEAUTHOR_NONE", "Anonymus");

    class cPageFile
    {
        var $m_pgfId;
        var $m_pgfDate;
        var $m_pgfTitle;
        var $m_pgfDescription;
        var $m_pgfFile;
        var $m_pgfOriginName;
        var $m_pgfCreatedBy;
        var $m_pgfModified;
        var $m_pgfAuthor;
        var $m_pgfLocked;

        var $m_pgfFolder;

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

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

        function Init()
        {
            $this->m_pgfId = "0";
            $this->m_pgfDate = "";
            $this->m_pgfTitle = "";
            $this->m_pgfDescription = "";
            $this->m_pgfFile = "";
            $this->m_pgfOriginName = "";
            $this->m_pgfCreatedBy = "";
            $this->m_pgfModified = "";
            $this->m_pgfAuthor = "";
            $this->m_pgfLocked = "0";

            $this->m_pgfFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_pgfId;
        }
        
        function GetDate()
        {
            return $this->m_pgfDate;
        }
        
        function SetTitle($title)
        {
            $this->m_pgfTitle = $title;
        }
        function GetTitle()
        {
            return $this->m_pgfTitle;
        }

        function SetDescription($description)
        {
            $this->m_pgfDescription = $description;
        }
        function GetDescription()
        {
            return $this->m_pgfDescription;
        }
        
        function SetFile($file)
        {
            $this->m_pgfFile = $file;
        }
        function GetFile()
        {
            return $this->m_pgfFile;
        }

        function SetOriginName($name)
        {
            $this->m_pgfOriginName = $name;
        }
        function GetOriginName()
        {
            return $this->m_pgfOriginName;
        }

        function GetAuthor()
        {
            return $this->m_pgfAuthor;
        }

        function GetModified()
        {
            return $this->m_pgfModified;
        }

        function GetCreatedBy()
        {
            return $this->m_pgfCreatedBy;
        }

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

        function SetLocked($bLock, $author = PAGEFILEAUTHOR_NONE)
        {
            $this->m_pgfLocked = "0";
            if ($bLock == true)
                $this->m_pgfLocked = "1";
            return $this->SavePageFile($author);
        }

        function SetFolder($folder)
        {
            $this->m_pgfFolder = $folder;
        }
        
        // Main methods
        function CreatePageFile($author = PAGEFILEAUTHOR_NONE)
        {
            $this->Init();
            $this->m_pgfId = uniqid("pgf", FALSE);
            $this->m_pgfDate = date("Y-m-d H:i:s");
            $this->m_pgfModified = $this->m_pgfDate;
            $this->m_pgfAuthor = $author;
            $this->m_pgfCreatedBy = $author;
        }

        function DeletePageFileFinal()
        {
            $bDone = true;
            if (file_exists($this->m_pgfFile))
            {
                if (unlink($this->m_pgfFile) == false)
                    $bDone = false;
            }
            if (unlink($this->m_pgfFolder . $this->m_pgfId . ".pgf") == false)
                $bDone = false;
                
            return $bDone;
        }

        function DeletePageFileFinalFromId($pgfId)
        {
            $this->m_pgfId = $pgfId;
            return $this->DeletePageFileFinal();
        }

        function LoadPageFileFromId($pgfId)
        {
            $this->m_pgfId = $pgfId;
            return $this->LoadPageFile();
        }

        function LoadPageFile()
        {
            $fileName = $this->m_pgfFolder . $this->m_pgfId  . ".pgf";
            //echo  $fileName;

            if ($this->m_pgfId == "0" || $this->m_pgfFolder == "")
                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_pgfId = $cStorage->GetData("Id");
            $this->m_pgfDate = $cStorage->GetData("Date");
            $this->m_pgfTitle = $cStorage->GetData("Title");
            $this->m_pgfDescription = $cStorage->GetData("Description");
            $this->m_pgfFile = $cStorage->GetData("File");
            $this->m_pgfOriginName = $cStorage->GetData("OriginName");
            $this->m_pgfCreatedBy = $cStorage->GetData("CreatedBy");
            $this->m_pgfModified = $cStorage->GetData("Modified");
            $this->m_pgfAuthor = $cStorage->GetData("Author");
            $this->m_pgfLocked = $cStorage->GetData("Locked");

            return true;
        }

        function SavePageFile($author = PAGEAUTHOR_NONE)
        {
            $newRow = "\r\n";
            $fileName = $this->m_pgfFolder . $this->m_pgfId  . ".pgf";
            // echo $fileName;

            if ($this->m_pgfId == "0" || $this->m_pgfFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("page_file");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_pgfId);
            $cStorage->AddData("Date", $this->m_pgfDate);
            $cStorage->AddData("Title", $this->m_pgfTitle);
            $cStorage->AddData("Description", $this->m_pgfDescription);
            $cStorage->AddData("File", $this->m_pgfFile);
            $cStorage->AddData("OriginName", $this->m_pgfOriginName);
            $cStorage->AddData("CreatedBy", $this->m_pgfCreatedBy);
            $cStorage->AddData("Modified", date("Y-m-d H:i:s"));
            $cStorage->AddData("Author", $author);
            $cStorage->AddData("Locked", $this->m_pgfLocked, false);

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


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

    }
    
}