<?php

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

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

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

    class cDownload
    {
        var $m_dldId;
        var $m_dldDate;
        var $m_dldTitle;
        var $m_dldInternalTitle;
        var $m_dldNavTitle;
        var $m_dldVersion;
        var $m_dldDescriptionDE;
        var $m_dldDescriptionEN;
        var $m_dldImageUrl;
        var $m_dldFileUrl;
        var $m_dldSiteUrl;
        var $m_dldCategoryId;
        var $m_dldPosition;
        var $m_dldLocked;

        var $m_dldStatsFolder;
        var $m_dldFolder;

        function cDownload()  // PHP 4
        {
            $this->InitDownload();
        }

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

        function InitDownload()
        {
            $this->m_dldId = "0";
            $this->m_dldDate = "";
            $this->m_dldTitle = "";
            $this->m_dldInternalTitle = "";
            $this->m_dldNavTitle = "";
            $this->m_dldVersion = "";
            $this->m_dldDescriptionDE = "";
            $this->m_dldDescriptionEN = "";
            $this->m_dldImageUrl = "";
            $this->m_dldFileUrl = "";
            $this->m_dldSiteUrl = "";
            $this->m_dldCategoryId = "0";
            $this->m_dldPosition = "0";
            $this->m_dldLocked = "0";

            $this->m_dldStatsFolder = "";
            $this->m_dldFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_dldId;
        }
        
        function GetDate()
        {
            return $this->m_dldDate;
        }
        
        function SetTitle($title)
        {
            $this->m_dldTitle = $title;
        }
        function GetTitle()
        {
            return $this->m_dldTitle;
        }
        function SetInternalTitle($title)
        {
            $this->m_dldInternalTitle = $title;
        }
        function GetInternalTitle()
        {
            return $this->m_dldInternalTitle;
        }
        function SetNavTitle($title)
        {
            $this->m_dldNavTitle = $title;
        }
        function GetNavTitle()
        {
            return $this->m_dldNavTitle;
        }

        function SetVersion($version)
        {
            $this->m_dldVersion = $version;
        }
        function GetVersion()
        {
            return $this->m_dldVersion;
        }
        
        function SetDescriptionDE($descriptionDE)
        {
            $this->m_dldDescriptionDE = $descriptionDE;
        }
        function GetDescriptionDE()
        {
            return $this->m_dldDescriptionDE;
        }
        function SetDescriptionEN($descriptionEN)
        {
            $this->m_dldDescriptionEN = $descriptionEN;
        }
        function GetDescriptionEN()
        {
            return $this->m_dldDescriptionEN;
        }

        function SetImageUrl($url)
        {
            $this->m_dldImageUrl = $url;
        }
        function GetImageUrl()
        {
            return $this->m_dldImageUrl;
        }

        function SetFileUrl($url)
        {
            $this->m_dldFileUrl = $url;
        }
        function GetFileUrl()
        {
            return $this->m_dldFileUrl;
        }

        function SetSiteUrl($url)
        {
            $this->m_dldSiteUrl = $url;
        }
        function GetSiteUrl()
        {
            return $this->m_dldSiteUrl;
        }

        function SetCategoryId($catId)
        {
            $this->m_dldCategoryId = $catId;
        }
        function GetCategoryId()
        {
            return $this->m_dldCategoryId;
        }

        function SetPosition($position)
        {
            $this->m_dldPosition = $position;
        }
        function GetPosition()
        {
            return $this->m_dldPosition;
        }

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

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

        function SetStatsFolder($folder)
        {
            $this->m_dldStatsFolder = $folder;
        }
        function GetStatsFolder()
        {
            return $this->m_dldStatsFolder;
        }
        function SetFolder($folder)
        {
            $this->m_dldFolder = $folder;
        }
        
        // Main methods
        function CreateDownload()
        {
            $this->InitDownload();
            $this->m_dldId = uniqid("dld", FALSE);
            $this->m_dldDate = date("Y-m-d H:i:s");
        }

        function DeleteDownloadFinal()
        {
            $bDone = true;
            if (file_exists($this->m_dldSiteUrl))
            {
                if (unlink($this->m_dldSiteUrl) == false)
                    $bDone = false;
            }
            if (file_exists($this->m_dldImageUrl))
            {
                if (unlink($this->m_dldImageUrl) == false)
                    $bDone = false;
            }
            if (file_exists($this->m_dldFileUrl))
            {
                if (unlink($this->m_dldFileUrl) == false)
                    $bDone = false;
            }
            if (unlink($this->m_dldFolder . $this->m_dldId . ".dld") == false)
                $bDone = false;
                
            return $bDone;
        }

        function DeleteDownloadFinalFromId($dldId)
        {
            $this->LoadCategoryFromId($dldId);
            return $this->DeleteDownloadFinal();
        }

        function UnlockDownload()
        {
            $m_dldLocked = "0";
            return $this->SaveDownload();
        }

        function LockDownload()
        {
            $m_dldLocked = "1";
            return $this->SaveDownload();
        }

        function LoadDownloadFromId($dldId)
        {
            $this->m_dldId = $dldId;
            return $this->LoadDownload();
        }

        function LoadDownload()
        {
            $fileName = $this->m_dldFolder . $this->m_dldId  . ".dld";
            //echo  $fileName;

            if ($this->m_dldId == "0" || $this->m_dldFolder == "")
                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_dldId = $cStorage->GetData("Id");
            $this->m_dldDate = $cStorage->GetData("Date");
            $this->m_dldVersion = $cStorage->GetData("Version");
            $this->m_dldTitle = $cStorage->GetData("Title");
            $this->m_dldInternalTitle = $cStorage->GetData("InternalTitle");
            $this->m_dldNavTitle = $cStorage->GetData("NavTitle");
            $this->m_dldDescriptionDE = $cStorage->GetData("DescriptionDE");
            $this->m_dldDescriptionEN = $cStorage->GetData("DescriptionEN");
            $this->m_dldImageUrl = $cStorage->GetData("ImageUrl");
            $this->m_dldFileUrl = $cStorage->GetData("FileUrl");
            $this->m_dldSiteUrl = $cStorage->GetData("SiteUrl");
            $this->m_dldCategoryId = $cStorage->GetData("CategoryId");
            $this->m_dldPosition = $cStorage->GetData("Position");
            $this->m_dldLocked = $cStorage->GetData("Locked");

            return true;
        }

        function SaveDownload()
        {
            $newRow = "\r\n";
            $fileName = $this->m_dldFolder . $this->m_dldId  . ".dld";
            // echo $fileName;

            if ($this->m_dldId == "0" || $this->m_dldFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("download");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_dldId);
            $cStorage->AddData("Date", $this->m_dldDate);
            $cStorage->AddData("Version", $this->m_dldVersion);
            $cStorage->AddData("Title", $this->m_dldTitle);
            $cStorage->AddData("InternalTitle", $this->m_dldInternalTitle);
            $cStorage->AddData("NavTitle", $this->m_dldNavTitle);
            $cStorage->AddData("DescriptionDE", $this->m_dldDescriptionDE);
            $cStorage->AddData("DescriptionEN", $this->m_dldDescriptionEN);
            $cStorage->AddData("ImageUrl", $this->m_dldImageUrl);
            $cStorage->AddData("FileUrl", $this->m_dldFileUrl);
            $cStorage->AddData("SiteUrl", $this->m_dldSiteUrl);
            $cStorage->AddData("CategoryId", $this->m_dldCategoryId, false);
            $cStorage->AddData("Position", $this->m_dldPosition);
            $cStorage->AddData("Locked", $this->m_dldLocked, false);

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


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

    }
    
}