public function IP2Location::__construct in Smart IP 6
Same name and namespace in other branches
- 7 includes/IP2Location.inc \IP2Location::__construct()
File
- includes/
IP2Location.inc, line 101
Class
Code
public function __construct($file = NULL, $mode = self::FILE_IO) {
if (!is_file($file)) {
throw new PEAR_Exception('Unable to open file "' . $file . '".');
}
// Define system unpack method
list($test) = array_values(unpack('L1L', pack('V', 1)));
// Use Big Endian Unpack if endian test failed
$this->unpack = $test != 1 ? self::BIG_ENDIAN : self::ENDIAN;
switch ($mode) {
case self::SHARED_MEMORY:
if (!function_exists('shmop_open')) {
throw new PEAR_Exception('Please make sure your PHP setup has "php_shmop" enabled.');
}
$this->mode = self::SHARED_MEMORY;
$this->shmId = @shmop_open(self::SHM_KEY, 'a', 0, 0);
/*shmop_delete($this->shmId);
shmop_close($this->shmId);
die;*/
if ($this->shmId === FALSE) {
// First execution, load database into memory
if (($fp = fopen($file, 'rb')) === FALSE) {
throw new PEAR_Exception('Unable to open file "' . $file . '".');
}
$stats = fstat($fp);
if ($shmId = @shmop_open(self::SHM_KEY, 'w', 0, 0)) {
shmop_delete($shmId);
shmop_close($shmId);
}
if ($shmId = @shmop_open(self::SHM_KEY, 'c', 0644, $stats['size'])) {
/*$buf = fread($fp, $stats['size']);
shmop_write($shmId, $buf, 0);*/
$offset = 0;
while ($offset < $stats['size']) {
$buf = fread($fp, 524288);
shmop_write($shmId, $buf, $offset);
$offset += 524288;
}
shmop_close($shmId);
}
fclose($fp);
$this->shmId = @shmop_open(self::SHM_KEY, 'a', 0, 0);
if ($this->shmId === FALSE) {
throw new PEAR_Exception('Unable to access shared memory block.');
}
}
break;
default:
$this->mode = self::FILE_IO;
$this->handle = fopen($file, 'rb');
if ($mode == self::MEMORY_CACHE) {
$this->mode = self::MEMORY_CACHE;
$stats = fstat($this->handle);
$this->buffer = fread($this->handle, $stats['size']);
}
}
$this->db['type'] = $this
->readByte(1, '8');
$this->db['column'] = $this
->readByte(2, '8');
$this->db['year'] = $this
->readByte(3, '8');
$this->db['month'] = $this
->readByte(4, '8');
$this->db['day'] = $this
->readByte(5, '8');
$this->db['count'] = $this
->readByte(6, '32');
$this->db['base_address'] = $this
->readByte(10, '32');
$this->db['ip_version'] = $this
->readByte(14, '32');
}