Guest.php
5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* @brief Guest Manager
* @version $Id: Guest.php 2517 2014-09-25 09:47:45Z elena $
*
*/
class Guest {
private $guestXmlFile;
public $Id, $Start, $Ip, $email;
function __construct($Ip_, $email_ = null){
if($email_) {
$this->Start = getdate();
$this->Ip = $Ip_;
$this->email = $email_;
}
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'));
}
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;
}
$res = true;
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);
}
private function _getId($dom){
$xp = new DOMXpath($dom);
$elements = $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;
}
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) {
for ($i = 0; $i < $startTimes->length; $i++) {
$user = "guest".$startTimes->item($i)->value;
$this->_deltree(USERPATH.$user);
$dom->documentElement->removeChild($startTimes->item($i)->parentNode);
}
return $dom->save($this->guestXmlFile);
}
return true;
}
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);
}
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";
}
}
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;
}
private function _deltree($f) {
if (is_dir($f)) {
$files = array_diff(scandir($f), array('.','..'));
foreach($files as $sf) {
if (is_dir("$f/$sf") && !is_link("$f/$sf")) {
$this->_deltree("$f/$sf");
} else {
unlink("$f/$sf");
}
}
}
if (is_dir($f)) rmdir($f);
}
}
?>