<?php

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

Filename:     class_pageimage.php
Description:  Class cPageImage definition,
              to load and save pageimage files

*/
include "class_storagefile.php";

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

    define ("PAGEIMAGEAUTHOR_NONE", "Anonymus");

    class cPageImage
    {
        var $m_pgiId;
        var $m_pgiDate;
        var $m_pgiTitle;
        var $m_pgiDescription;
        var $m_pgiImageFile;
        var $m_pgiOriginName;
        var $m_pgiWidth;
        var $m_pgiHeight;
        var $m_pgiCreatedBy;
        var $m_pgiModified;
        var $m_pgiAuthor;
        var $m_pgiLocked;

        var $m_pgiFolder;

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

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

        function Init()
        {
            $this->m_pgiId = "0";
            $this->m_pgiDate = "";
            $this->m_pgiTitle = "";
            $this->m_pgiDescription = "";
            $this->m_pgiImageFile = "";
            $this->m_pgiOriginName = "";
            $this->m_pgiWidth = "0";
            $this->m_pgiHeight = "0";
            $this->m_pgiCreatedBy = "";
            $this->m_pgiModified = "";
            $this->m_pgiAuthor = "";
            $this->m_pgiLocked = "0";

            $this->m_pgiFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_pgiId;
        }
        
        function GetDate()
        {
            return $this->m_pgiDate;
        }
        
        function SetTitle($title)
        {
            $this->m_pgiTitle = $title;
        }
        function GetTitle()
        {
            return $this->m_pgiTitle;
        }

        function SetDescription($description)
        {
            $this->m_pgiDescription = $description;
        }
        function GetDescription()
        {
            return $this->m_pgiDescription;
        }
        
        function SetImageFile($file)
        {
            $this->m_pgiImageFile = $file;
        }
        function GetImageFile()
        {
            return $this->m_pgiImageFile;
        }

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

        function SetWidth($width)
        {
            $this->m_pgiWidth = $width;
        }
        function GetWidth()
        {
            return $this->m_pgiWidth;
        }
        function SetHeight($height)
        {
            $this->m_pgiHeight = $height;
        }
        function GetHeight()
        {
            return $this->m_pgiHeight;
        }

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

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

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

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

        function SetLocked($bLock, $author = PAGEIMAGEAUTHOR_NONE)
        {
            $this->m_pgiLocked = "0";
            if ($bLock == true)
                $this->m_pgiLocked = "1";
            return $this->SavePageImage($author);
        }

        function SetFolder($folder)
        {
            $this->m_pgiFolder = $folder;
        }
        
        // Main methods
        function CreatePageImage($author = PAGEIMAGEAUTHOR_NONE)
        {
            $this->Init();
            $this->m_pgiId = uniqid("pgi", FALSE);
            $this->m_pgiDate = date("Y-m-d H:i:s");
            $this->m_pgiModified = $this->m_pgiDate;
            $this->m_pgiAuthor = $author;
            $this->m_pgiCreatedBy = $author;
        }

        function DeletePageImageFinal()
        {
            $bDone = true;
            if (file_exists($this->m_pgiImageFile))
            {
                if (unlink($this->m_pgiImageFile) == false)
                    $bDone = false;
            }
            if (unlink($this->m_pgiFolder . $this->m_pgiId . ".pgi") == false)
                $bDone = false;
                
            return $bDone;
        }

        function DeletePageImageFinalFromId($pgiId)
        {
            $this->m_pgiId = $pgiId;
            return $this->DeletePageImageFinal();
        }

        function LoadPageImageFromId($pgiId)
        {
            $this->m_pgiId = $pgiId;
            return $this->LoadPageImage();
        }

        function LoadPageImage()
        {
            $fileName = $this->m_pgiFolder . $this->m_pgiId  . ".pgi";
            //echo  $fileName;

            if ($this->m_pgiId == "0" || $this->m_pgiFolder == "")
                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_pgiId = $cStorage->GetData("Id");
            $this->m_pgiDate = $cStorage->GetData("Date");
            $this->m_pgiTitle = $cStorage->GetData("Title");
            $this->m_pgiDescription = $cStorage->GetData("Description");
            $this->m_pgiImageFile = $cStorage->GetData("ImageFile");
            $this->m_pgiOriginName = $cStorage->GetData("OriginName");
            $this->m_pgiWidth = $cStorage->GetData("Width");
            $this->m_pgiHeight = $cStorage->GetData("Height");
            $this->m_pgiCreatedBy = $cStorage->GetData("CreatedBy");
            $this->m_pgiModified = $cStorage->GetData("Modified");
            $this->m_pgiAuthor = $cStorage->GetData("Author");
            $this->m_pgiLocked = $cStorage->GetData("Locked");

            return true;
        }

        function SavePageImage($author = PAGEAUTHOR_NONE)
        {
            $newRow = "\r\n";
            $fileName = $this->m_pgiFolder . $this->m_pgiId  . ".pgi";
            // echo $fileName;

            if ($this->m_pgiId == "0" || $this->m_pgiFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("page_image");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_pgiId);
            $cStorage->AddData("Date", $this->m_pgiDate);
            $cStorage->AddData("Title", $this->m_pgiTitle);
            $cStorage->AddData("Description", $this->m_pgiDescription);
            $cStorage->AddData("ImageFile", $this->m_pgiImageFile);
            $cStorage->AddData("OriginName", $this->m_pgiOriginName);
            $cStorage->AddData("Width", $this->m_pgiWidth);
            $cStorage->AddData("Height", $this->m_pgiHeight);
            $cStorage->AddData("CreatedBy", $this->m_pgiCreatedBy);
            $cStorage->AddData("Modified", date("Y-m-d H:i:s"));
            $cStorage->AddData("Author", $author);
            $cStorage->AddData("Locked", $this->m_pgiLocked, false);

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


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

    }
    
}