Guest.php 4.07 KB
<?php
/** 
 * @brief Guest Manager
 * @version $Id: Guest.php 2517 2014-09-25 09:47:45Z elena $
 *   
 */
 
class Guest {

    private $guestXml, $guestXmlFile, $xp, $root;
    public  $Id, $Start, $Ip, $email;

    function __construct($Ip_, $email_){
      
        if($email_ != null) {
           $this->Start = getdate();
           $this->Ip = $Ip_;
           $this->email = $email_;
          }
         else {
             $this->Id = substr($Ip_,strlen("guest"));
          }

         $this->guestXmlFile = DATAPATH."guests.xml";
         $this->guestXml  =  new DomDocument("1.0");

         if (!file_exists($this->guestXmlFile)){
            $status = $this->generateXML();
         }
         else {
            $this->guestXml->load($this->guestXmlFile);
            $this->root = $this->guestXml->getElementsByTagName("guests")->item(0);
        }
        $this->xp = new domxpath($this->guestXml);                     
    }

    private function generateXML() {
        $this->root = $this->guestXml->createElement('guests'); 
        $this->guestXml->appendChild($this->root);

        $status = $this->guestXml->save($this->guestXmlFile);

        return $status;
    } 

    public function GetId(){
           
         $elements = $this->xp->query("//@xml:id"); 
// Now find New Valid ID        
            if ($elements->length > 0) {
                     $idList = array();
                     for ($i = 0; $i < $elements->length; $i++) 
                           $idList[$i] = $elements->item($i)->nodeValue; 
                      
                     sort($idList);
                     for ($i = 0; $i < $elements->length; $i++) {
                        if ($idList[$i] > $i) { 
                                 $newID = $i;
                                 break;
                           }  
                         $newID = $i+1;  
                      }                 
             } else { $newID = 0;}

       return $newID;
   }

   public function checkGuestTimes(){
   
         $Start_0 = time() - GuestSessionDuration*60; // in secs
         $startTimes = $this->xp->query("//guest[@start<".$Start_0."]/@xml:id"); 

         if ($startTimes->length > 0) {             
            for ($i = 0; $i < $startTimes->length; $i++) {                
                    $user = "guest".$startTimes->item($i)->value;
                    $this->deltree(USERPATH.$user);
                    $this->root->removeChild($startTimes->item($i)->parentNode);  
            }
            $this->xp = new domxpath($this->guestXml);    
         }

    }

   public function deleteGuest(){

            $user = "guest".$this->Id;
            $this->deltree(USERPATH.$user);
            $theGuest = $this->guestXml->getElementById($this->Id);
            $this->root->removeChild($theGuest);
            $this->guestXml->save($this->guestXmlFile);

 }

    public function addGuest(){
 
      if (($this->Id = $this->GetId()) < MaxGuests) {
          $guest = $this->guestXml->createElement("guest");        
          $guest->setAttribute('xml:id',$this->Id );         
          $guest->setAttribute('start',time());
          $guest->appendChild($this->guestXml->createElement("IP", $this->Ip));
          $guest->appendChild($this->guestXml->createElement("email", $this->email));
          $this->root->appendChild($guest);
          $this->guestXml->save($this->guestXmlFile);

          return "guest".$this->Id;
      }
       else  {  
           return "allGuestLoginsInUse";
      }
 }

     public function registerGuest(){

          $guest_file = fopen(DATAPATH.'guest.login','a'); 
          fwrite($guest_file, $this->email." ".$this->Ip." ".$this->Start['mday']."/".$this->Start['mon']."/".$this->Start['year']."\n"); 
          fclose($guest_file);     

    }

   public function deltree($f) {

        if (is_dir($f)) {
            foreach(glob($f.'/*') as $sf) {
                if (is_dir($sf) && !is_link($sf)) {
                    $this->deltree($sf);
                } else {
                    unlink($sf);
                } 
            } 
        }
        if (is_dir($f)) rmdir($f);
    }
    
} 
?>