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