Over the new year I got a PHP error from my Matomo crontask regarding GeoIP2 database from Maxmind. The error message is:
GeoIP2AutoUpdater: failed to download 'https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz'
curl_exec: Could not resolve host: geolite.maxmind.com. Hostname requested was: geolite.maxmind.com
After short time googling, I found out that Maxmind doesn’t allow anonymous download of their free GeoIP2 databases because of a California Consumer Privacy Act (CCPA). See source
To search a string part (needle) in string (haystack), you can easily use the PHP strpos function. But this function doesn’t exist in TypoScript.
The following TypoScript return true if the needle is found in the haystack. This can be used in an if condition.
cObject = COA
cObject {
1 = LOAD_REGISTER
1 {
# search for http (or https).
needle = http
# search in
haystack.field = header_link
# split haystack by the needle
# result is a haystack without a needle, if found
findNeedleInHaystack.cObject = TEXT
findNeedleInHaystack.cObject {
data = REGISTER:haystack
split.token.data = REGISTER:needle
}
}
# compare REGISTER:haystack and result of REGISTER:findNeedleInHaystack
# if both are the same, nothing was found
10 = TEXT
10 {
# true will be returned, if the needle was found inside haystack
value = true
if {
value.data = REGISTER:haystack
equals.data = REGISTER:findNeedleInHaystack
# if the same, then negate the return value
negate = 1
}
}
}
The above COA will return true if http is found in the header_link field. I used this to add ATagParams for external link, in assumption that link with http(s) is an external link.
ATagParams = class="external"
ATagParams.if {
cObject = COA
cObject {
// the strpos function
}
}
In version 2 of php_cs_fixer some parameters and rules are renamed. Upgrade guide can be see on their Github page. This how-to is accordingly updated.
Applying PSR-2 coding style manually is really no fun and wasting time. A short visit in Github and there’s a tool which detects and fixes the coding style. The tool is called php-cs-fixer.
I’ll show you how to get the tool and use it to easily reformat all PHP file in project.
How hard is it, to write a PHP script to allow user to download a file? really easy, one would say. Just send the appropriate HTTP header and echo the file content.
Basically that’s all I need to force download per PHP. But somehow Android stock browser (Chrome) only downloads and creates a 0 kB file. Other browser (tested with Firefox on Android) saves the file correctly.
Let’s imagine you have a huge array, with say couple ten thousands of elements and you want to remove any duplicates entries in this array. As PHP programmer you’ll be thinking of array_unique. For a “small” sized array and one dimensional array this one does the trick.
I have some nice snippets which cover multi dimensional and/or huge sized array.