Guest.php
4.13 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
<?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_ = null){
if($email_) {
$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)) {
$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);
}
}
?>