You are here

function _setup_segments in GeoIP API 7

Same name and namespace in other branches
  1. 6 lib/geoip.inc \_setup_segments()
1 call to _setup_segments()
geoip_open in lib/geoip.inc

File

lib/geoip.inc, line 254

Code

function _setup_segments($gi) {
  $gi->databaseType = GEOIP_COUNTRY_EDITION;
  $gi->record_length = STANDARD_RECORD_LENGTH;
  if ($gi->flags & GEOIP_SHARED_MEMORY) {
    $offset = @shmop_size($gi->shmid) - 3;
    for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
      $delim = @shmop_read($gi->shmid, $offset, 3);
      $offset += 3;
      if ($delim == chr(255) . chr(255) . chr(255)) {
        $gi->databaseType = ord(@shmop_read($gi->shmid, $offset, 1));
        $offset++;
        if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) {
          $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
        }
        else {
          if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
            $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
          }
          else {
            if ($gi->databaseType == GEOIP_CITY_EDITION_REV0 || $gi->databaseType == GEOIP_CITY_EDITION_REV1 || $gi->databaseType == GEOIP_ORG_EDITION || $gi->databaseType == GEOIP_ISP_EDITION || $gi->databaseType == GEOIP_ASNUM_EDITION) {
              $gi->databaseSegments = 0;
              $buf = @shmop_read($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
              for ($j = 0; $j < SEGMENT_RECORD_LENGTH; $j++) {
                $gi->databaseSegments += ord($buf[$j]) << $j * 8;
              }
              if ($gi->databaseType == GEOIP_ORG_EDITION || $gi->databaseType == GEOIP_ISP_EDITION) {
                $gi->record_length = ORG_RECORD_LENGTH;
              }
            }
          }
        }
        break;
      }
      else {
        $offset -= 4;
      }
    }
    if ($gi->databaseType == GEOIP_COUNTRY_EDITION || $gi->databaseType == GEOIP_PROXY_EDITION || $gi->databaseType == GEOIP_NETSPEED_EDITION) {
      $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
    }
  }
  else {
    $filepos = ftell($gi->filehandle);
    fseek($gi->filehandle, -3, SEEK_END);
    for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
      $delim = fread($gi->filehandle, 3);
      if ($delim == chr(255) . chr(255) . chr(255)) {
        $gi->databaseType = ord(fread($gi->filehandle, 1));
        if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) {
          $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
        }
        else {
          if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
            $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
          }
          else {
            if ($gi->databaseType == GEOIP_CITY_EDITION_REV0 || $gi->databaseType == GEOIP_CITY_EDITION_REV1 || $gi->databaseType == GEOIP_ORG_EDITION || $gi->databaseType == GEOIP_ISP_EDITION || $gi->databaseType == GEOIP_ASNUM_EDITION) {
              $gi->databaseSegments = 0;
              $buf = fread($gi->filehandle, SEGMENT_RECORD_LENGTH);
              for ($j = 0; $j < SEGMENT_RECORD_LENGTH; $j++) {
                $gi->databaseSegments += ord($buf[$j]) << $j * 8;
              }
              if ($gi->databaseType == GEOIP_ORG_EDITION || $gi->databaseType == GEOIP_ISP_EDITION) {
                $gi->record_length = ORG_RECORD_LENGTH;
              }
            }
          }
        }
        break;
      }
      else {
        fseek($gi->filehandle, -4, SEEK_CUR);
      }
    }
    if ($gi->databaseType == GEOIP_COUNTRY_EDITION || $gi->databaseType == GEOIP_PROXY_EDITION || $gi->databaseType == GEOIP_NETSPEED_EDITION) {
      $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
    }
    fseek($gi->filehandle, $filepos, SEEK_SET);
  }
  return $gi;
}