<?php

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

File:         class_user.php
Description:  Class cUser definition,
              handling of the user entities

*/

include "class_storagefile.php";

if (!defined("CLASS_USER"))
{
    define ("CLASS_USER", "DEFINED");

    include "class_address.php";
    
    class cUser extends cAddress
    {
        var $m_userId;
        var $m_userDate;
        var $m_userAlias;
        var $m_userPassword;
        var $m_userLocked;
        var $m_userWantsNewsletter;
        var $m_userType;
        
        var $m_userFolder;
        var $m_userFolderDeleted;

        function cUser()  // PHP4
        {
            $this->Init();
        }

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

        function Init()
        {
            $this->m_userId = "";
            $this->m_userDate = "";
            $this->m_userAlias = "";
            $this->m_userPassword = "";
            $this->m_userLocked = "0";
            $this->m_userWantsNewsletter = "0";
            $this->m_userType = "0";

            $this->m_userFolder = "";
            $this->m_userFolderDeleted = "";

            $this->InitAddr();
        }

        function GetId()
        {
            return $this->m_userId;
        }
        
        function GetDate()
        {
            return $this->m_userDate;
        }

        function GetAlias()
        {
            return $this->m_userAlias;
        }
        function SetAlias($alias)
        {
            $this->m_userAlias = $alias;
        }

        function GetPassword()
        {
            if (md5($this->m_userPassword) == md5(""))
                return "";
            return $this->m_userPassword;
        }
        function SetPassword($passWord)
        {
            $this->m_userPassword = md5($passWord);
        }

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

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

        function SetWantsNewsletter($bWantsNewsletter)
        {
            $this->m_userWantsNewsletter = "0";
            if ($bWantsNewsletter == true)
                $this->m_userWantsNewsletter = "1";
        }
        
        function WantsNewsletter()
        {
            return ($this->m_userWantsNewsletter == "1");
        }

        function SetType($userType)
        {
            $this->m_userType = $userType;
        }
        function GetType()
        {
            return $this->m_userType;
        }

        function SetFolder($userFolder)
        {
            $this->m_userFolder = $userFolder;
        }
        function SetFolderDeleted($userFolderDeleted)
        {
            $this->m_userFolderDeleted = $userFolderDeleted;
        }

        // Main methods
        function CreateUser()
        {
            $this->Init();
            $this->m_userId = uniqid("usr", FALSE);
            $this->m_userDate = date("Y-m-d H:i:s");
        }

        function DeleteUserFinal()  // Move to trash
        {
            if (copy($this->m_userFolder . $this->m_userId . ".usr", $this->m_userFolderDeleted . $this->m_userId . ".usr") == false)
                return false;
            return unlink($this->m_userFolder . $this->m_userId . ".usr");
        }
        function DeleteUserFinalX()  // Unlink -> not useful!!
        {
            return unlink($this->m_userFolder . $this->m_userId . ".usr");
        }

        function DeleteArticelFinalFromId($userId)  // Move to trash
        {
            if (copy($this->m_userFolder . $userId . ".usr", $this->m_userFolderDeleted . $userId . ".usr") == false)
                return false;
            return unlink($this->m_userFolder . $userId . ".usr");
        }
        function DeleteArticelFinalFromIdX($userId)  // Unlink -> not useful!!
        {
            return unlink($this->m_userFolder . $userId . ".usr");
        }

        function LoadUserFromId($userId)
        {
            $this->m_userId = $userId;
            return $this->LoadUser();
        }

        function LoadUser()
        {
            $fileName = $this->m_userFolder . $this->m_userId  . ".usr";
            //echo  $fileName;

            if ($this->m_userId == "0" || $this->m_userFolder == "")
                return false;

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

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

            $this->m_userId = $cStorage->GetData("Id");
            $this->m_userDate = $cStorage->GetData("Date");
            $this->m_userAlias = $cStorage->GetData("Alias");
            $this->m_userPassword = $cStorage->GetData("Password");
            $this->m_userType = $cStorage->GetData("Type");
            $this->m_userLocked = $cStorage->GetData("Locked");
            $this->m_userWantsNewsletter = $cStorage->GetData("WantsNewsletter");
            $this->SetFirstName($cStorage->GetData("FirstName"));
            $this->SetLastName($cStorage->GetData("LastName"));
            $this->SetStreet($cStorage->GetData("Street"));
            $this->SetStreetNo($cStorage->GetData("StreetNo"));
            $this->SetCity($cStorage->GetData("City"));
            $this->SetZipCode($cStorage->GetData("ZipCode"));
            $this->SetLand($cStorage->GetData("Land"));
            $this->SetTelephoneNo($cStorage->GetData("TelephoneNo"));
            $this->SetEmail($cStorage->GetData("Email"));

            return true;
        }

        function SaveUser()
        {
            $newRow = "\r\n";
            $fileName = $this->m_userFolder . $this->m_userId . ".usr";
            // echo $fileName;

            if ($this->m_userId == "0" || $this->m_userFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("user");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_userId);
            $cStorage->AddData("Date", $this->m_userDate);
            $cStorage->AddData("Alias", $this->m_userAlias);
            $cStorage->AddData("Password", $this->m_userPassword);
            $cStorage->AddData("Type", $this->m_userType, false);
            $cStorage->AddData("Locked", $this->m_userLocked, false);
            $cStorage->AddData("WantsNewsletter", $this->m_userWantsNewsletter, false);
            $cStorage->AddData("FirstName", $this->GetFirstName());
            $cStorage->AddData("LastName", $this->GetLastName());
            $cStorage->AddData("Street", $this->GetStreet());
            $cStorage->AddData("StreetNo", $this->GetStreetNo());
            $cStorage->AddData("City", $this->GetCity());
            $cStorage->AddData("ZipCode", $this->GetZipCode());
            $cStorage->AddData("Land", $this->GetLand());
            $cStorage->AddData("TelephoneNo", $this->GetTelephoneNo());
            $cStorage->AddData("Email", $this->GetEmail());

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