<?php

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

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

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

    class cVideo
    {
        var $m_vidId;
        var $m_vidDate;
        var $m_vidNameDE;
        var $m_vidNameEN;
        var $m_vidUrl;
        var $m_vidPosition;
        var $m_vidLocked;
        var $m_vidFolder;

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

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

        function Init()
        {
            $this->m_vidId = "0";
            $this->m_vidDate = "";
            $this->m_vidNameDE = "";
            $this->m_vidNameEN = "";
            $this->m_vidUrl = "";
            $this->m_vidPosition = "0";
            $this->m_vidLocked = "0";

            $this->m_vidFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_vidId;
        }
        
        function GetDate()
        {
            return $this->m_vidDate;
        }
        
        function SetNameDE($nameDE)
        {
            $this->m_vidNameDE = $nameDE;
        }
        function GetNameDE()
        {
            return $this->m_vidNameDE;
        }

        function SetNameEN($nameEN)
        {
            $this->m_vidNameEN = $nameEN;
        }
        function GetNameEN()
        {
            return $this->m_vidNameEN;
        }

        function SetUrl($url)
        {
            $this->m_vidUrl = $url;
        }
        function GetUrl()
        {
            return $this->m_vidUrl;
        }

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

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

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

        function SetFolder($folder)
        {
            $this->m_vidFolder = $folder;
        }
        
        // Main methods
        function CreateVideo()
        {
            $this->Init();
            $this->m_vidId = uniqid("vid", FALSE);
            $this->m_vidDate = date("Y-m-d H:i:s");
        }

        function DeleteVideoFinal()
        {
            return unlink($this->m_vidFolder . $this->m_vidId . ".vid");
        }

        function DeleteVideoFinalFromId($vidId)
        {
            return unlink($this->m_vidFolder . $vidId . ".vid");
        }

        function UnlockVideo()
        {
            $m_vidLocked = "0";
            return $this->SaveVideo();
        }

        function LockVideo()
        {
            $m_vidLocked = "1";
            return $this->SaveVideo();
        }

        function LoadVideoFromId($vidId)
        {
            $this->m_vidId = $vidId;
            return $this->LoadVideo();
        }

        function LoadVideo()
        {
            $fileName = $this->m_vidFolder . $this->m_vidId  . ".vid";
            //echo  $fileName;

            if ($this->m_vidId == "0" || $this->m_vidFolder == "")
                return false;

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

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

            $this->m_vidId = $cStorage->GetData("Id");
            $this->m_vidDate = $cStorage->GetData("Date");
            $this->m_vidNameDE = $cStorage->GetData("NameDE");
            $this->m_vidNameEN = $cStorage->GetData("NameEN");
            $this->m_vidUrl = $cStorage->GetData("Url");
            $this->m_vidPosition = $cStorage->GetData("Position");
            $this->m_vidLocked = $cStorage->GetData("Locked");

            return true;
        }

        function SaveVideo()
        {
            $newRow = "\r\n";
            $fileName = $this->m_vidFolder . $this->m_vidId  . ".vid";
            // echo $fileName;

            if ($this->m_vidId == "0" || $this->m_vidFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("video");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_vidId);
            $cStorage->AddData("Date", $this->m_vidDate);
            $cStorage->AddData("Version", $this->m_dldVersion);
            $cStorage->AddData("NameDE", $this->m_vidNameDE);
            $cStorage->AddData("NameEN", $this->m_vidNameEN);
            $cStorage->AddData("Url", $this->m_vidUrl);
            $cStorage->AddData("Position", $this->m_vidPosition);
            $cStorage->AddData("Locked", $this->m_vidLocked, false);

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

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