b13/magnets

Magnets: TYPO3 Service Package revolving around having a good API to fetch the current Geo IP and its location of the user.

Maintainers

Details

github.com/b13/magnets

Source

Issues

Installs: 18 430

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 12

Forks: 2

Open Issues: 1

Type:typo3-cms-extension

2.2.0 2024-10-01 09:46 UTC

README

This extension acts as a thin wrapper for TYPO3 to access GeoIP relevant information.

A symfony CLI Command can be added to download the latest GeoIP2 data.

For download the latest GeoIP2 data you have to provide an licence-key from maxmind (as .env-Variable or $GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPLicenceKey'])

Maxmind API requests are limited (1000 requests/day). As an alternative the databases (GeoLite2-City, GeoLite2-Country) can be stored as a "Generic Package" in GitLab.

For Download during the CI run the CI_JOB_TOKEN can be used as follows:

if (!empty(getenv('CI_JOB_TOKEN'))) {
    $GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPSource']['url'] = 'https://<GITLAB_HOST>/api/v4/projects/<PROJECT_ID>/packages/generic/GeoLite2/1.0.0/###REMOTE_EDITION###.tar.gz';
    $GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPSource']['headers'] = [
        'JOB-TOKEN' => getenv('CI_JOB_TOKEN'),
    ];
}

Installation

Run composer req b13/magnets and install the extension via Extension Manager.

Usage

Ensure your cronjob / scheduler task is running and use the IpLocation PHP class to have a nice and quick API.

In addition, you have "countryCode" as TypoScript condition available.

[countryCode == 'FR']
  page.10 = TEXT
  page.10.value = You are from france
[global]

The condition is also available in site configurations.

Settings

Headers to send while running the request to download the database. Either use JOB-TOKEN or PRIVATE-TOKEN to authenticate on GitLab.

$GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPSource']['headers'] = [];

Change the URL to retrieve the databases from. The URL needs to include ###REMOTE_EDITION### which will be replaced by the "remote edition" (GeoLite2-Country and GeoLite2-City).

$GLOBALS['TYPO3_CONF_VARS']['SYS']['GeoIPSource']['url'] = 'https://download.maxmind.com/app/geoip_download?suffix=tar.gz&edition_id=###REMOTE_EDITION###';

Store and Update databases using GitLab

Create a project and add a .gitlab-ci.yml

stages:
  - package-update

geo-ip:
  stage: package-update
  image: alpine/curl:8.9.1
  variables:
    GIT_STRATEGY: none
    GITLAB_RELEASE_VERSION: "1.0.0"
    PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/GeoLite2"
  script:
    # Download Maxmind GeoIP database
    - mkdir -p downloads
    - curl -sSL "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=${GEOIP_LICENCE_KEY}&suffix=tar.gz" > downloads/GeoLite2-Country.tar.gz
    - curl -sSL "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${GEOIP_LICENCE_KEY}&suffix=tar.gz" > downloads/GeoLite2-City.tar.gz
    # Upload files to package
    - 'curl --fail-with-body --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ./downloads/GeoLite2-Country.tar.gz ${PACKAGE_REGISTRY_URL}/${GITLAB_RELEASE_VERSION}/GeoLite2-Country.tar.gz'
    - 'curl --fail-with-body --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ./downloads/GeoLite2-City.tar.gz ${PACKAGE_REGISTRY_URL}/${GITLAB_RELEASE_VERSION}/GeoLite2-City.tar.gz'

Under Settings -> CI/CD -> Variables add a variable named "GEOIP_LICENCE_KEY" containing the licence-key.

For regular updates add a "New schedule" und Build -> Pipeline schedules. e.g. 8 00 * * 1,3

License

Just as TYPO3 Core, this is an extension for TYPO3 and also licensed under GPL2+.

Made by b13 with ♥

Find more TYPO3 extensions we have developed that help us deliver value in client projects. As part of the way we work, we focus on testing and best practices to ensure long-term performance, reliability, and results in all our code.