<?php

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

File:         class_userlist.php
Description:  Class cUserList definition,
              handling of the user lists

*/

include "class_user.php";

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

    class cUserList
    {

        var $m_userlFolder;
        var $m_userlIds = array();

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

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

        function Init()
        {

            $this->m_userlFolder = "";
        }

        function SetFolder($userlFolder)
        {
            $this->m_userlFolder = $userlFolder;
        }

        function SetUserList($userlArray)
        {
            $this->m_userlIds = $userlArray;
        }

        function GetUserList()
        {
            return $this->m_userlIds;
        }
        
        function LoadAllUsers()
        {
            $rDir = dir($this->m_userlFolder);
            $strEntry = "";
            $fileName = "";
            $userList = array();

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.usr')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    $userList[] = $fileName;
                }
            }
            return $userList;
        }
        
        function LoadUserList($filter = 0)
        {
            $usr_User = new cUser;
            $usr_User->SetFolder($this->m_userlFolder);
            $usr_List = $this->LoadAllUsers();

            for ($i = 0; $i < count($usr_List); $i ++)
            {
                if ($filter == 0)
                    $this->m_userlIds[] = $usr_List[$i];
                else
                {
                    if ($usr_User->LoadUserFromId($usr_List[$i]) == true)
                    {
                        $addMe = false;
                        switch($filter)
                        {
                            case 1:   // unlocked
                                if ($usr_User->IsLocked() == false)
                                    $addMe = true;
                            break;
                            case 2:   // locked
                                if ($usr_User->IsLocked() == true)
                                    $addMe = true;
                            break;
                        }
                        if ($addMe == true)
                            $this->m_userlIds[] = $usr_List[$i];
                    }
                }
            }
        }

        function CheckAlias($newAlias, $usrId = "")
        {
            $usr_User = new cUser;
            $usr_User->SetFolder($this->m_userlFolder);
            $usr_List = $this->LoadAllUsers();

            for ($i = 0; $i < count($usr_List); $i ++)
            {
                if ($usr_User->LoadUserFromId($usr_List[$i]) == true)
                {
                    if ($usr_User->GetAlias() == $newAlias && $usr_User->GetId() != $usrId)
                        return false;
                }
            }
            return true;
        }

        function GetUserIdByEmail($email)
        {
            $usr_User = new cUser;
            $usr_User->SetFolder($this->m_userlFolder);
            $usr_List = $this->LoadAllUsers();

            for ($i = 0; $i < count($usr_List); $i ++)
            {
                if ($usr_User->LoadUserFromId($usr_List[$i]) == true)
                {
                    if ($usr_User->GetEmail() == $email)
                        return $usr_User->GetId();
                }
            }
            return false;
        }
        
        function SetNewsletterUserByEMail($email, $wantsNewsletter = false)
        {
            $usr_User = new cUser;
            $usrId = $this->GetUserIdByEmail($email);
            $usr_User->SetFolder($this->m_userlFolder);
            if ($usrId == false)
            {
                if ($wantsNewsletter == false)
                {
                    return true;  // User not found, but unsubscribed...
                }
                else
                {
                    $usr_User = new cUser;
                    $usr_User->CreateUser();
                    $usr_User->SetFolder($this->m_userlFolder);
                    $usr_User->SetFirstName("Newsletter name");
                    $usr_User->SetLastName("Newsletter lastname");
                    $usr_User->SetStreet("Newsletter street");
                    $usr_User->SetStreetNo("Newsletter no 1");
                    $usr_User->SetCity("Newsletter city");
                    $usr_User->SetZipCode("Newsletter zipcode");
                    $usr_User->SetLand("Newsletter country");
                    $usr_User->SetTelephoneNo("1234567890");
                    $usr_User->SetEmail($_POST['newsletter_email']);
                }
            }
            else
            {
                if ($usr_User->LoadUserFromId($usrId) == false)
                {
                    return ($wantsNewsletter == false);  // if unsubscribed ok, otherwise error
                }
            }
            $usr_User->SetWantsNewsletter($wantsNewsletter);
            return ($usr_User->SaveUser());
        }
        

        function CheckLogin($strUser, $strPassword)
        {
            $usr_User = new cUser;
            $usr_User->SetFolder($this->m_userlFolder);
            $usr_List = $this->LoadAllUsers();

            for ($i = 0; $i < count($usr_List); $i ++)
            {
                if ($usr_User->LoadUserFromId($usr_List[$i]) == true)
                {
                    if ($usr_User->GetAlias() == $strUser && $usr_User->GetPassword() == md5($strPassword) && $usr_User->IsLocked() == false)
                    {
                        return $usr_User->GetType();
                    }
                }
            }
            return false;
        }

        function LoadNewsletterUserList()
        {
            $usr_User = new cUser;
            $usr_User->SetFolder($this->m_userlFolder);
            $usr_List = $this->LoadAllUsers();

            for ($i = 0; $i < count($usr_List); $i ++)
            {
                if ($usr_User->LoadUserFromId($usr_List[$i]) == true)
                {
                    if ($usr_User->WantsNewsletter() == true && $usr_User->IsLocked() == false)
                        $this->m_userlIds[] = $usr_List[$i];
                }
            }
        }
        
    }
}
?>