Forked from
Ad Aures / Castopod
1078 commits behind the upstream repository.
-
Yassine Doghri authored
- refactor model / entity and controller logic for DRY code - update episodes and podcasts migrations - define callbacks for podcast and episode models for enclosure update and cache clearing
Yassine Doghri authored- refactor model / entity and controller logic for DRY code - update episodes and podcasts migrations - define callbacks for podcast and episode models for enclosure update and cache clearing
analytics_helper.php 5.90 KiB
<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
/**
* Set user country in session variable, for analytics purpose
*/
function set_user_session_country()
{
$session = \Config\Services::session();
$session->start();
$db = \Config\Database::connect();
$country = 'N/A';
// Finds country:
if (!$session->has('country')) {
try {
$reader = new \GeoIp2\Database\Reader(
WRITEPATH . 'uploads/GeoLite2-Country/GeoLite2-Country.mmdb'
);
$geoip = $reader->country($_SERVER['REMOTE_ADDR']);
$country = $geoip->country->isoCode;
} catch (\Exception $e) {
// If things go wrong the show must go on and the user must be able to download the file
}
$session->set('country', $country);
}
}
/**
* Set user player in session variable, for analytics purpose
*/
function set_user_session_player()
{
$session = \Config\Services::session();
$session->start();
if (!$session->has('player')) {
$session = \Config\Services::session();
$session->start();
$playerName = '- Unknown Player -';
$useragent = $_SERVER['HTTP_USER_AGENT'];
try {
$jsonUserAgents = json_decode(
file_get_contents(
WRITEPATH . 'uploads/user-agents/src/user-agents.json'
),
true
);
//Search for current HTTP_USER_AGENT in json file:
foreach ($jsonUserAgents as $player) {
foreach ($player['user_agents'] as $useragentsRegexp) {
//Does the HTTP_USER_AGENT match this regexp:
if (preg_match("#{$useragentsRegexp}#", $useragent)) {
if (isset($player['bot'])) {
//It’s a bot!
$playerName = '- Bot -';
} else {
//It isn’t a bot, we store device/os/app:
$playerName =
(isset($player['device'])