Blame view

php/classes/Guest.php 4.13 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
<?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;

9c30a3d3   Elena.Budnik   arg default in co...
13
    function __construct($Ip_, $email_ = null){
16035364   Benjamin Renard   First commit
14
      
9c30a3d3   Elena.Budnik   arg default in co...
15
        if($email_) {
16035364   Benjamin Renard   First commit
16
17
           $this->Start = getdate();
           $this->Ip = $Ip_;
9c30a3d3   Elena.Budnik   arg default in co...
18
           $this->email = $email_;  
16035364   Benjamin Renard   First commit
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
          }
         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);
        }
9c30a3d3   Elena.Budnik   arg default in co...
34
        $this->xp = new domxpath($this->guestXml);  
16035364   Benjamin Renard   First commit
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    }

    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);     

    }

32175950   Elena.Budnik   delete hidden fil...
120
121
    public function deltree($f) {
 
16035364   Benjamin Renard   First commit
122
        if (is_dir($f)) {
32175950   Elena.Budnik   delete hidden fil...
123
124
125
126
127
128

            $files = array_diff(scandir($f), array('.','..'));

            foreach($files as $sf) {
                if (is_dir("$f/$sf") && !is_link("$f/$sf")) {
                    $this->deltree("$f/$sf");
16035364   Benjamin Renard   First commit
129
                } else {
32175950   Elena.Budnik   delete hidden fil...
130
                    unlink("$f/$sf");
16035364   Benjamin Renard   First commit
131
132
133
                } 
            } 
        }
32175950   Elena.Budnik   delete hidden fil...
134

16035364   Benjamin Renard   First commit
135
136
137
138
139
        if (is_dir($f)) rmdir($f);
    }
    
} 
?>