<?php

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

File:         class_mail.php
Description:  Handling of mails
*/

include "class_storagefile.php";

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

    class cMail
    {
        var $m_mailId;
        var $m_mailDate;
        var $m_mailTitle;
        var $m_mailMessage;
        var $m_mailReceiver;
        var $m_mailSender;
        var $m_mailSent;
        var $m_mailLocked;
        var $m_mailFolder;
        var $m_mailFolderDeleted;

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

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

        function Init()
        {
            $this->m_mailId = "0";
            $this->m_mailDate = "";
            $this->m_mailTitle = "";
            $this->m_mailMessage = "";
            $this->m_mailReceiver = "";
            $this->m_mailSender = "";
            $this->m_mailSent = "0";

            $this->m_mailFolder = "";
            $this->m_mailFolderDeleted = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_mailId;
        }
        
        function GetDate()
        {
            return $this->m_mailDate;
        }
        
        function SetTitle($title)
        {
            $this->m_mailTitle = $title;
        }
        function GetTitle()
        {
            return $this->m_mailTitle;
        }

        function SetMessage($message)
        {
            $this->m_mailMessage = $message;
        }
        function GetMessage()
        {
            return $this->m_mailMessage;
        }
        
        function SetReceiver($receiver)
        {
            $this->m_mailReceiver = $receiver;
        }
        function GetReceiver()
        {
            return $this->m_mailReceiver;
        }

        function SetSender($sender)
        {
            $this->m_mailSender = $receiver;
        }
        function GetSender()
        {
            return $this->m_mailSender;
        }

        function SetFolder($folder)
        {
            $this->m_mailFolder = $folder;
        }
        function SetFolderDeleted($mailFolderDeleted)
        {
            $this->m_mailFolderDeleted = $mailFolderDeleted;
        }

        function IsSent()
        {
            return ($this->m_mailSent == "1");
        }

        // Main methods
        function CreateMail()
        {
            $this->Init();
            $this->m_mailId = uniqid("mal", FALSE);
            $this->m_mailDate = date("Y-m-d H:i:s");
        }

        function DeleteMailFinal()  // Move to trash
        {
            if (copy($this->m_mailFolder . $this->m_mailId . ".mal", $this->m_mailFolderDeleted . $this->m_mailId . ".mal") == false)
                return false;
            return unlink($this->m_mailFolder . $this->m_mailId . ".mal");
        }
        function DeleteMailFinalX() // unlink -> not useful
        {
            return unlink($this->m_mailFolder . $this->m_mailId . ".mal");
        }

        function DeleteMailFinalFromId($mailId)  // Move to trash
        {
            if (copy($this->m_mailFolder . $mailId . ".mal", $this->m_mailFolderDeleted . $mailId . ".mal") == false)
                return false;
            return unlink($this->m_mailFolder . $mailId . ".mal");
        }
        function DeleteMailFinalFromIdX($mailId) // unlink -> not useful
        {
            return unlink($this->m_mailFolder . $mailId . ".mal");
        }

        function LoadMailFromId($mailId)
        {
            $this->m_mailId = $mailId;
            return $this->LoadMail();
        }

        function LoadMail()
        {
            $fileName = $this->m_mailFolder . $this->m_mailId  . ".mal";
            //echo  $fileName;

            if ($this->m_mailId == "0" || $this->m_mailFolder == "")
                return false;

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

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

            $this->m_mailId = $cStorage->GetData("Id");
            $this->m_mailDate = $cStorage->GetData("Date");
            $this->m_mailTitle = $cStorage->GetData("Title");
            $this->m_mailMessage = $cStorage->GetData("Message");
            $this->m_mailReceiver = $cStorage->GetData("Receiver");
            $this->m_mailSender = $cStorage->GetData("Sender");
            $this->m_mailSent = $cStorage->GetData("Sent");

            return true;
        }

        function SaveMail()
        {
            $newRow = "\r\n";
            $fileName = $this->m_mailFolder . $this->m_mailId  . ".mal";
            // echo $fileName;

            if ($this->m_mailId == "0" || $this->m_mailFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("mail");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_mailId);
            $cStorage->AddData("Date", $this->m_mailDate);
            $cStorage->AddData("Title", $this->m_mailTitle);
            $cStorage->AddData("Message", $this->m_mailMessage);
            $cStorage->AddData("Receiver", $this->m_mailReceiver);
            $cStorage->AddData("Sender", $this->m_mailSender);
            $cStorage->AddData("Sent", $this->m_mailSent, false);

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

        function SendMail()
        {
            $this->m_mailSent = false;

            if ($this->m_mailReceiver == "" || $this->m_mailTitle == "" ||
                $this->m_mailMessage == "" || $this->m_mailSender == "" ||
                $this->m_mailId == "0")
                return false;
                
            $this->m_mailSent = mail($this->m_mailReceiver, $this->m_mailTitle, $this->m_mailMessage, "From: " . $this->m_mailSender);

            $this->SaveMail();
            return $this->m_mailSent;
        }

        function SendMailByParams($receiver, $title, $message, $sender, $folder)
        {
            $this->CreateMail();
            $this->m_mailReceiver = $receiver;
            $this->m_mailTitle = $title;
            $this->m_mailMessage = $message;
            $this->m_mailSender = $sender;
            $this->m_mailFolder = $folder;
            
            return $this->SendMail();
        }
    }
}