fix: move analytics to helper
This commit is contained in:
parent
13be386842
commit
d31191732e
|
@ -11,103 +11,40 @@ use CodeIgniter\Controller;
|
||||||
|
|
||||||
class Analytics extends Controller
|
class Analytics extends Controller
|
||||||
{
|
{
|
||||||
function __construct()
|
/**
|
||||||
{
|
* An array of helpers to be loaded automatically upon
|
||||||
$session = \Config\Services::session();
|
* class instantiation. These helpers will be available
|
||||||
$session->start();
|
* to all other controllers that extend Analytics.
|
||||||
$db = \Config\Database::connect();
|
*
|
||||||
$country = 'N/A';
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $helpers = ['analytics'];
|
||||||
|
|
||||||
// Finds country:
|
/**
|
||||||
if (!$session->has('country')) {
|
* Constructor.
|
||||||
try {
|
*/
|
||||||
$reader = new \GeoIp2\Database\Reader(
|
public function initController(
|
||||||
WRITEPATH . 'uploads/GeoLite2-Country/GeoLite2-Country.mmdb'
|
\CodeIgniter\HTTP\RequestInterface $request,
|
||||||
);
|
\CodeIgniter\HTTP\ResponseInterface $response,
|
||||||
$geoip = $reader->country($_SERVER['REMOTE_ADDR']);
|
\Psr\Log\LoggerInterface $logger
|
||||||
$country = $geoip->country->isoCode;
|
) {
|
||||||
} catch (\Exception $e) {
|
// Do Not Edit This Line
|
||||||
// If things go wrong the show must go on and the user must be able to download the file
|
parent::initController($request, $response, $logger);
|
||||||
}
|
|
||||||
$session->set('country', $country);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finds player:
|
//--------------------------------------------------------------------
|
||||||
if (!$session->has('player')) {
|
// Preload any models, libraries, etc, here.
|
||||||
$playerName = '-unknown-';
|
//--------------------------------------------------------------------
|
||||||
$error = '';
|
// E.g.:
|
||||||
|
// $this->session = \Config\Services::session();
|
||||||
|
|
||||||
try {
|
set_user_session_country();
|
||||||
$useragent = $_SERVER['HTTP_USER_AGENT'];
|
set_user_session_player();
|
||||||
$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'])
|
|
||||||
? $player['device'] . '/'
|
|
||||||
: '') .
|
|
||||||
(isset($player['os'])
|
|
||||||
? $player['os'] . '/'
|
|
||||||
: '') .
|
|
||||||
(isset($player['app'])
|
|
||||||
? $player['app']
|
|
||||||
: '?');
|
|
||||||
}
|
|
||||||
//We found it!
|
|
||||||
break 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
// If things go wrong the show must go on and the user must be able to download the file
|
|
||||||
}
|
|
||||||
if ($playerName == '-unknown-') {
|
|
||||||
// Add to unknown list
|
|
||||||
try {
|
|
||||||
$procedureNameAUU = $db->prefixTable(
|
|
||||||
'analytics_unknown_useragents'
|
|
||||||
);
|
|
||||||
$db->query("CALL $procedureNameAUU(?)", [$useragent]);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
// If things go wrong the show must go on and the user must be able to download the file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$session->set('player', $playerName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add one hit to this episode:
|
// Add one hit to this episode:
|
||||||
public function hit($p_podcast_id, $p_episode_id, ...$filename)
|
public function hit($p_podcast_id, $p_episode_id, ...$filename)
|
||||||
{
|
{
|
||||||
$session = \Config\Services::session();
|
podcast_hit($p_podcast_id, $p_episode_id);
|
||||||
$db = \Config\Database::connect();
|
|
||||||
$procedureName = $db->prefixTable('analytics_podcasts');
|
|
||||||
$p_country_code = $session->get('country');
|
|
||||||
$p_player = $session->get('player');
|
|
||||||
try {
|
|
||||||
$db->query("CALL $procedureName(?,?,?,?);", [
|
|
||||||
$p_podcast_id,
|
|
||||||
$p_episode_id,
|
|
||||||
$p_country_code,
|
|
||||||
$p_player,
|
|
||||||
]);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
// If things go wrong the show must go on and the user must be able to download the file
|
|
||||||
}
|
|
||||||
return redirect()->to(media_url(implode('/', $filename)));
|
return redirect()->to(media_url(implode('/', $filename)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ class BaseController extends Controller
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $helpers = [];
|
protected $helpers = ['analytics'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
@ -45,55 +45,13 @@ class BaseController extends Controller
|
||||||
// E.g.:
|
// E.g.:
|
||||||
// $this->session = \Config\Services::session();
|
// $this->session = \Config\Services::session();
|
||||||
|
|
||||||
$session = \Config\Services::session();
|
set_user_session_country();
|
||||||
$session->start();
|
set_user_session_browser();
|
||||||
|
set_user_session_referer();
|
||||||
// Defines country
|
|
||||||
if (!$session->has('country')) {
|
|
||||||
try {
|
|
||||||
$reader = new \GeoIp2\Database\Reader(
|
|
||||||
WRITEPATH . 'uploads/GeoLite2-Country/GeoLite2-Country.mmdb'
|
|
||||||
);
|
|
||||||
$geoip = $reader->country($_SERVER['REMOTE_ADDR']);
|
|
||||||
$session->set('country', $geoip->country->isoCode);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$session->set('country', 'N/A');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Defines browser
|
|
||||||
if (!$session->has('browser')) {
|
|
||||||
try {
|
|
||||||
$whichbrowser = new \WhichBrowser\Parser(getallheaders());
|
|
||||||
$session->set('browser', $whichbrowser->browser->name);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$session->set('browser', 'Other');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Defines referrer
|
|
||||||
$newreferer = isset($_SERVER['HTTP_REFERER'])
|
|
||||||
? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)
|
|
||||||
: '- Direct -';
|
|
||||||
$newreferer =
|
|
||||||
$newreferer == parse_url(current_url(false), PHP_URL_HOST)
|
|
||||||
? '- Direct -'
|
|
||||||
: $newreferer;
|
|
||||||
if (!$session->has('referer') or $newreferer != '- Direct -') {
|
|
||||||
$session->set('referer', $newreferer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function stats($postcast_id)
|
protected function stats($postcast_id)
|
||||||
{
|
{
|
||||||
$session = \Config\Services::session();
|
webpage_hit($postcast_id);
|
||||||
$session->start();
|
|
||||||
$db = \Config\Database::connect();
|
|
||||||
$procedureName = $db->prefixTable('analytics_website');
|
|
||||||
$db->query("call $procedureName(?,?,?,?)", [
|
|
||||||
$postcast_id,
|
|
||||||
$session->get('country'),
|
|
||||||
$session->get('browser'),
|
|
||||||
$session->get('referer'),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,191 @@
|
||||||
|
|
||||||
|
<?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'])
|
||||||
|
? $player['device'] . '/'
|
||||||
|
: '') .
|
||||||
|
(isset($player['os'])
|
||||||
|
? $player['os'] . '/'
|
||||||
|
: '') .
|
||||||
|
(isset($player['app']) ? $player['app'] : '?');
|
||||||
|
}
|
||||||
|
//We found it!
|
||||||
|
break 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// If things go wrong the show must go on and the user must be able to download the file
|
||||||
|
}
|
||||||
|
if ($playerName == '- Unknown Player -') {
|
||||||
|
// Add to unknown list
|
||||||
|
try {
|
||||||
|
$db = \Config\Database::connect();
|
||||||
|
$procedureNameAUU = $db->prefixTable(
|
||||||
|
'analytics_unknown_useragents'
|
||||||
|
);
|
||||||
|
$db->query("CALL $procedureNameAUU(?)", [$useragent]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// If things go wrong the show must go on and the user must be able to download the file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$session->set('player', $playerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set user browser in session variable, for analytics purpose
|
||||||
|
*/
|
||||||
|
function set_user_session_browser()
|
||||||
|
{
|
||||||
|
$session = \Config\Services::session();
|
||||||
|
$session->start();
|
||||||
|
|
||||||
|
if (!$session->has('browser')) {
|
||||||
|
$browserName = '- Other -';
|
||||||
|
try {
|
||||||
|
$whichbrowser = new \WhichBrowser\Parser(getallheaders());
|
||||||
|
$browserName = $whichbrowser->browser->name;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$browserName = '- Could not get browser name -';
|
||||||
|
}
|
||||||
|
if ($browserName == null) {
|
||||||
|
$browserName = '- Could not get browser name -';
|
||||||
|
}
|
||||||
|
$session->set('browser', $browserName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set user referer in session variable, for analytics purpose
|
||||||
|
*/
|
||||||
|
function set_user_session_referer()
|
||||||
|
{
|
||||||
|
$session = \Config\Services::session();
|
||||||
|
$session->start();
|
||||||
|
|
||||||
|
$newreferer = isset($_SERVER['HTTP_REFERER'])
|
||||||
|
? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)
|
||||||
|
: '- Direct -';
|
||||||
|
$newreferer =
|
||||||
|
$newreferer == parse_url(current_url(false), PHP_URL_HOST)
|
||||||
|
? '- Direct -'
|
||||||
|
: $newreferer;
|
||||||
|
if (!$session->has('referer') or $newreferer != '- Direct -') {
|
||||||
|
$session->set('referer', $newreferer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function webpage_hit($postcast_id)
|
||||||
|
{
|
||||||
|
$session = \Config\Services::session();
|
||||||
|
$session->start();
|
||||||
|
$db = \Config\Database::connect();
|
||||||
|
|
||||||
|
$procedureName = $db->prefixTable('analytics_website');
|
||||||
|
$db->query("call $procedureName(?,?,?,?)", [
|
||||||
|
$postcast_id,
|
||||||
|
$session->get('country'),
|
||||||
|
$session->get('browser'),
|
||||||
|
$session->get('referer'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function podcast_hit($p_podcast_id, $p_episode_id)
|
||||||
|
{
|
||||||
|
$session = \Config\Services::session();
|
||||||
|
$session->start();
|
||||||
|
$first_time_for_this_episode = true;
|
||||||
|
|
||||||
|
if ($session->has('episodes')) {
|
||||||
|
if (in_array($p_episode_id, $session->get('episodes'))) {
|
||||||
|
$first_time_for_this_episode = false;
|
||||||
|
} else {
|
||||||
|
$session->push('episodes', [$p_episode_id]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$session->set('episodes', [$p_episode_id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($first_time_for_this_episode) {
|
||||||
|
$db = \Config\Database::connect();
|
||||||
|
$procedureName = $db->prefixTable('analytics_podcasts');
|
||||||
|
try {
|
||||||
|
$db->query("CALL $procedureName(?,?,?,?);", [
|
||||||
|
$p_podcast_id,
|
||||||
|
$p_episode_id,
|
||||||
|
$session->get('country'),
|
||||||
|
$session->get('player'),
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// If things go wrong the show must go on and the user must be able to download the file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,3 +16,23 @@ function media_url($uri = '', string $protocol = null): string
|
||||||
{
|
{
|
||||||
return base_url(config('App')->mediaRoot . '/' . $uri, $protocol);
|
return base_url(config('App')->mediaRoot . '/' . $uri, $protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the podcast URL to use in views
|
||||||
|
*
|
||||||
|
* @param mixed $uri URI string or array of URI segments
|
||||||
|
* @param string $protocol
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function podcast_url(
|
||||||
|
$podcast_id = 1,
|
||||||
|
$episode_id = 1,
|
||||||
|
$podcast_name = '',
|
||||||
|
$uri = '',
|
||||||
|
string $protocol = null
|
||||||
|
): string {
|
||||||
|
return base_url(
|
||||||
|
"/stats/$podcast_id/$episode_id/$podcast_name/$uri",
|
||||||
|
$protocol
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -6,11 +6,15 @@
|
||||||
<img src="<?= media_url(
|
<img src="<?= media_url(
|
||||||
$episode->image ? $episode->image : $podcast->image
|
$episode->image ? $episode->image : $podcast->image
|
||||||
) ?>" alt="Episode cover" class="object-cover w-40 h-40 mb-6" />
|
) ?>" alt="Episode cover" class="object-cover w-40 h-40 mb-6" />
|
||||||
<audio controls>
|
<audio controls preload="none">
|
||||||
<source src="<?= media_url(
|
<source src="<?= podcast_url(
|
||||||
|
$episode->podcast_id,
|
||||||
|
$episode->id,
|
||||||
|
$podcast->name,
|
||||||
$episode->enclosure_url
|
$episode->enclosure_url
|
||||||
) ?>" type="<?= $episode->enclosure_type ?>">
|
) ?>" type="<?= $episode->enclosure_type ?>">
|
||||||
Your browser does not support the audio tag.
|
Your browser does not support the audio tag.
|
||||||
</audio>
|
</audio>
|
||||||
|
|
||||||
<?= $this->endSection() ?>
|
<?= $this->endSection()
|
||||||
|
?>
|
||||||
|
|
|
@ -34,8 +34,11 @@
|
||||||
</h3>
|
</h3>
|
||||||
<p><?= $episode->description ?></p>
|
<p><?= $episode->description ?></p>
|
||||||
</a>
|
</a>
|
||||||
<audio controls class="mt-auto">
|
<audio controls class="mt-auto" preload="none">
|
||||||
<source src="<?= media_url(
|
<source src="<?= podcast_url(
|
||||||
|
$episode->podcast_id,
|
||||||
|
$episode->id,
|
||||||
|
$podcast->name,
|
||||||
$episode->enclosure_url
|
$episode->enclosure_url
|
||||||
) ?>" type="<?= $episode->enclosure_type ?>">
|
) ?>" type="<?= $episode->enclosure_type ?>">
|
||||||
Your browser does not support the audio tag.
|
Your browser does not support the audio tag.
|
||||||
|
@ -49,4 +52,5 @@
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
||||||
<?= $this->endSection() ?>
|
<?= $this->endSection()
|
||||||
|
?>
|
||||||
|
|
Loading…
Reference in New Issue