Blame view

php/classes/Guest.php 5.07 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
<?php
/** 
 * @brief Guest Manager
 * @version $Id: Guest.php 2517 2014-09-25 09:47:45Z elena $
 *   
 */
 
class Guest {

275c15b1   Benjamin Renard   Concurrent access...
10
    private $guestXmlFile;
16035364   Benjamin Renard   First commit
11
12
    public  $Id, $Start, $Ip, $email;

9c30a3d3   Elena.Budnik   arg default in co...
13
    function __construct($Ip_, $email_ = null){
9c30a3d3   Elena.Budnik   arg default in co...
14
        if($email_) {
275c15b1   Benjamin Renard   Concurrent access...
15
16
17
            $this->Start = getdate();
            $this->Ip = $Ip_;
            $this->email = $email_;  
16035364   Benjamin Renard   First commit
18
        }
275c15b1   Benjamin Renard   Concurrent access...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        else {
            $this->Id = substr($Ip_,strlen("guest"));
        }

        $this->guestXmlFile = DATAPATH."guests.xml";
    }

    public function checkGuestTimes() {
        return $this->concurrentAccessGuestFile(array($this,'_checkGuestTimes'));
    }

    public function deleteGuest() {
        return $this->concurrentAccessGuestFile(array($this,'_deleteGuest'));
    }

    public function addGuest(){
        return $this->concurrentAccessGuestFile(array($this,'_addGuest'));
16035364   Benjamin Renard   First commit
36
37
    }

275c15b1   Benjamin Renard   Concurrent access...
38
39
40
41
42
43
44
45
46
47
48
49
    public function registerGuest(){
        return $this->concurrentAccessGuestFile(array($this,'_registerGuest'));
    }

    private function concurrentAccessGuestFile($callback) {
        $lockFile =  $this->guestXmlFile.".lockfile";

        $fp = fopen($lockFile, "w+");

        if ($fp === false) {
            return false;
        }
16035364   Benjamin Renard   First commit
50

275c15b1   Benjamin Renard   Concurrent access...
51
        $res = true;
16035364   Benjamin Renard   First commit
52

275c15b1   Benjamin Renard   Concurrent access...
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
        if (flock($fp, LOCK_EX))
        {
            if (!file_exists($this->guestXmlFile)) {
                $res = $this->_generateXML();
            }

            if ($res) {
                $dom = new DomDocument("1.0","UTF-8");
                $dom->preserveWhiteSpace = false;
                $dom->formatOutput = true;
                $res = $dom->load($this->guestXmlFile);
                if ($res) {
                    $func_res = call_user_func($callback,$dom);
                }
            }
        }
        else
            $res = false;

        fclose($fp);

        if ($res)
            return $func_res;

        return false;
    }

    private function _generateXML() {
        $dom = new DOMDocument("1.0","UTF-8");
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $rootNode = $dom->createElement('guests'); 
        $dom->appendChild($rootNode);
        return $dom->save($this->guestXmlFile);
16035364   Benjamin Renard   First commit
87
88
    } 

275c15b1   Benjamin Renard   Concurrent access...
89
90
91
    private function _getId($dom){
        $xp = new DOMXpath($dom); 
        $elements = $xp->query("//@xml:id"); 
16035364   Benjamin Renard   First commit
92
// Now find New Valid ID        
275c15b1   Benjamin Renard   Concurrent access...
93
94
95
96
        if ($elements->length > 0) {
            $idList = array();
            for ($i = 0; $i < $elements->length; $i++) 
                $idList[$i] = $elements->item($i)->nodeValue; 
16035364   Benjamin Renard   First commit
97
                      
275c15b1   Benjamin Renard   Concurrent access...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
            sort($idList);
            for ($i = 0; $i < $elements->length; $i++) {
                if ($idList[$i] > $i) { 
                    $newID = $i;
                    break;
                }  
                $newID = $i+1;  
            }                 
        } else { $newID = 0;}

        return $newID;
    }

    private function _checkGuestTimes($dom){
        $xp = new DOMXpath($dom);
        $Start_0 = time() - GuestSessionDuration*60; // in secs
        $startTimes = $xp->query("//guest[@start<".$Start_0."]/@xml:id"); 

        if ($startTimes->length > 0) {
16035364   Benjamin Renard   First commit
117
            for ($i = 0; $i < $startTimes->length; $i++) {                
275c15b1   Benjamin Renard   Concurrent access...
118
119
120
                $user = "guest".$startTimes->item($i)->value;
                $this->_deltree(USERPATH.$user);
                $dom->documentElement->removeChild($startTimes->item($i)->parentNode);
16035364   Benjamin Renard   First commit
121
            }
16035364   Benjamin Renard   First commit
122

275c15b1   Benjamin Renard   Concurrent access...
123
124
125
            return $dom->save($this->guestXmlFile);
        }
        return true;
16035364   Benjamin Renard   First commit
126
127
    }

275c15b1   Benjamin Renard   Concurrent access...
128
129
130
131
132
133
134
    private function _deleteGuest($dom){
        $user = "guest".$this->Id;
        $this->_deltree(USERPATH.$user);
        $theGuest = $dom->getElementById($this->Id);
        $dom->documentElement->removeChild($theGuest);
        return $dom->save($this->guestXmlFile);
    }
16035364   Benjamin Renard   First commit
135

275c15b1   Benjamin Renard   Concurrent access...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    private function _addGuest($dom){ 
        if (($this->Id = $this->_getId($dom)) < MaxGuests) {
            $guest = $dom->createElement("guest");        
            $guest->setAttribute('xml:id',$this->Id );         
            $guest->setAttribute('start',time());
            $guest->appendChild($dom->createElement("IP", $this->Ip));
            $guest->appendChild($dom->createElement("email", $this->email));
            $dom->documentElement->appendChild($guest);
            $dom->save($this->guestXmlFile);
            return "guest".$this->Id;
        }
        else  {  
            return "allGuestLoginsInUse";
        }
    }
16035364   Benjamin Renard   First commit
151

275c15b1   Benjamin Renard   Concurrent access...
152
153
154
155
156
    private function _registerGuest($dom){
        $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);
        return true; 
16035364   Benjamin Renard   First commit
157
158
    }

275c15b1   Benjamin Renard   Concurrent access...
159
    private function _deltree($f) {
32175950   Elena.Budnik   delete hidden fil...
160
 
16035364   Benjamin Renard   First commit
161
        if (is_dir($f)) {
32175950   Elena.Budnik   delete hidden fil...
162
163
164
165
166

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

            foreach($files as $sf) {
                if (is_dir("$f/$sf") && !is_link("$f/$sf")) {
275c15b1   Benjamin Renard   Concurrent access...
167
                    $this->_deltree("$f/$sf");
16035364   Benjamin Renard   First commit
168
                } else {
32175950   Elena.Budnik   delete hidden fil...
169
                    unlink("$f/$sf");
16035364   Benjamin Renard   First commit
170
171
172
                } 
            } 
        }
32175950   Elena.Budnik   delete hidden fil...
173

16035364   Benjamin Renard   First commit
174
175
176
177
178
        if (is_dir($f)) rmdir($f);
    }
    
} 
?>