Entries is array with keys as indexes * and $obj->Entries['MyNVaultKey']->iPointer contain position data in file. * * Warning: * $obj->Entries['MyKey']->Value is undefined in this step! * * $numberOfEntries = count($obj->Entries); * * @return true on success */ public function LoadEntries(); /** * Fill Value and Timestamp in one element of Entries array. * Use when want value of specific field without loading all data. * * @param entry nVault`s key, index */ public function getEntry($entry); /** * Fill Value and Timestamp in whole Entries array */ public function getAllEntries(); /** * Set keys filter for load only some group of entries * * @param callback * function cb($key){} * return 1 when want to load */ public function setKeyFilter($callback); /** * Print debug listing of entries */ public function print_r(); }; /** * BinaryReader */ class BinaryReader{ /** * File Pointer */ private $fp; public function BinaryReader($fp){ $this->fp = $fp; } public function ReadAddr(&$buffer, $size){ if($size <= 0) return false; if(($buffer = fread($this->fp, $size)) !== false) return true; return false; } public function ReadUInt32(){ $num; if(!$this->ReadAddr($num, 4)) throw new Exception("Read failed"); $res = unpack("i", $num); return $res[1]; } public function ReadInt32(){ $num; if(!$this->ReadAddr($num, 4)) throw new Exception("Read failed"); $res = unpack("I", $num); return $res[1]; } public function ReadUInt16(){ $num; if(!$this->ReadAddr($num, 2)) throw new Exception("Read failed"); $res = unpack("v", $num); return $res[1]; } public function ReadInt16(){ $num; if(!$this->ReadAddr($num, 2)) throw new Exception("Read failed"); $res = unpack("s", $num); return $res[1]; } public function ReadUInt8(){ $num; if(!$this->ReadAddr($num, 1)) throw new Exception("Read failed"); $res = unpack("C", $num); return $res[1]; } public function ReadInt8(){ $num; if(!$this->ReadAddr($num, 1)) throw new Exception("Read failed"); $res = unpack("c", $num); return $res[1]; } public function ReadChars(&$buffer, $len){ return $this->ReadAddr($buffer, $len); } }; class Vault{ public $iPointer; public $iKeyLen, $iValLen; public $Value; public $iTimestamp; public function Vault($pos, $value, $tstamp){ $this->iPointer = $pos; $this->Value = $value; $this->iTimestamp = $tstamp; } }; class nVaultRead implements inVaultRead{ /** * Name of vault file */ private $szFileName; /** * Binary reader object */ private $binread; /** * Vault version $iMajorVer.$iMinorVer */ public $iMinorVer, $iMajorVer; /** * Array of vault entries */ public $Entries; /** * Filter function */ public $callback; public function Open($file){ $this->szFileName = $file; $fp = fopen($file, "rb"); if(!$fp) return false; $br = new BinaryReader($fp); try{ if($br->ReadUInt32() != VAULT_MAGIC) return false; }catch(Exception $ex){ return false; } fclose($fp); $callback = NULL; return true; } public function fullLoadEntries(){ $fp = fopen($this->szFileName, "rb"); if(!$fp) return false; $br = new BinaryReader($fp); try{ if($br->ReadUInt32() != VAULT_MAGIC) return false; $this->iMinorVer = $br->ReadUInt8(); $this->iMajorVer = $br->ReadUInt8(); $iEntries = $br->ReadUInt32(); } catch(Exception $ex){ die("

".$ex->getMessage()."

"); } $pos = 10; fseek($fp, $pos); $this->Entries = array(); for ($i = 0; $i < $iEntries; $i++) { $szKey = ""; $vault = new Vault($pos, "", 0); try{ $vault->iTimestamp = $br->ReadInt32(); $vault->iKeyLen = $br->ReadUInt8(); $vault->iValLen = $br->ReadUInt16(); $br->ReadChars($szKey, $vault->iKeyLen); $br->ReadChars($vault->Value, $vault->iValLen); } catch(Exception $ex){ die("

".$ex->getMessage()."

"); } if(!is_callable($this->callback) || call_user_func($this->callback, $szKey)) $this->Entries[$szKey] = $vault; $pos = $pos + 11 + $vault->iKeyLen + $vault->iValLen; } fclose($fp); } public function LoadEntriesNum(){ static $num = NULL; if($num === NULL){ $fp = fopen($this->szFileName, "rb"); if(!$fp) return false; $br = new BinaryReader($fp); try{ if($br->ReadUInt32() != VAULT_MAGIC) return false; $br->ReadUInt8(); $br->ReadUInt8(); $num = $br->ReadUInt32(); } catch(Exception $ex){ die("

".$ex->getMessage()."

"); } fclose($fp); } return $num; } public function LoadEntries(){ $fp = fopen($this->szFileName, "rb"); if(!$fp) return false; $br = new BinaryReader($fp); try{ if($br->ReadUInt32() != VAULT_MAGIC) return false; $this->iMinorVer = $br->ReadUInt8(); $this->iMajorVer = $br->ReadUInt8(); $iEntries = $br->ReadUInt32(); } catch(Exception $ex){ die("

".$ex->getMessage()."

"); } $szKey = ""; $pos = 14; fseek($fp, $pos); $iPointers = array(); $this->Entries = array(); for ($i = 0; $i < $iEntries; $i++) { $szKey = ""; $iPointers[$i] = $pos-4; try{ $iKeyLen = $br->ReadUInt8(); $iValLen = $br->ReadUInt16(); $br->ReadChars($szKey, $iKeyLen); } catch(Exception $ex){ die("

".$ex->getMessage()."

"); } $this->Entries[$szKey] = new Vault($iPointers[$i], "", 0); $pos = $iPointers[$i] + 11 + $iKeyLen + $iValLen; fseek($fp, $pos); } fclose($fp); return true; } public function getEntry($entry){ $fp = fopen($this->szFileName, "rb"); if (!$fp) return 0; $vault = $this->Entries[$entry]; $iPos = $vault->iPointer; if($iPos < 0) return 0; $br = new BinaryReader($fp); fseek($fp, $iPos); try{ $vault->iTimestamp = $br->ReadInt32(); $vault->iKeyLen = $br->ReadUInt8(); $vault->iValLen = $br->ReadUInt16(); $br->ReadChars($szKey, $vault->iKeyLen); $br->ReadChars($vault->Value, $vault->iValLen); } catch(Exception $ex){ die("

".$ex->getMessage()."

"); } fclose($fp); return 1; } public function getAllEntries(){ foreach($this->Entries as $key=>$value){ $this->getEntry($key); } } public function print_r(){ echo "
";
		print_r($this->Entries);
		echo "
"; } public function setKeyFilter($callback){ $this->callback = $callback; } public function sort(){ uasort($this->Entries, 'ByXp'); } }; function ByXP($a, $b){ $temp = $a->Value; $temp = explode("#",$temp); $xpA = intVal($temp[1]); $temp = $b->Value; $temp = explode("#",$temp); $xpB = intVal($temp[1]); if($xpA == $xpB) return 0; return ($xpA > $xpB)?-1:1; } ?>