Loading app/Config/Routes.php +7 −0 Original line number Diff line number Diff line Loading @@ -46,6 +46,13 @@ $routes->group('(:podcastSlug)', function ($routes) { ]); }); // Route for podcast audio file analytics (/stats/podcast_id/episode_id/podcast_folder/filename.mp3) $routes->add('/stats/(:num)/(:num)/(:any)', 'Analytics::hit/$1/$2/$3'); // Show the Unknown UserAgents $routes->add('/.well-known/unknown-useragents', 'UnknownUserAgents'); $routes->add('/.well-known/unknown-useragents/(:num)', 'UnknownUserAgents/$1'); /** * -------------------------------------------------------------------- * Additional Routing Loading app/Controllers/Analytics.php 0 → 100644 +113 −0 Original line number Diff line number Diff line <?php namespace App\Controllers; /** * Class Analytics * Creates Analytics controller * @copyright 2020 Podlibre * @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3 * @link https://castopod.org/ */ use CodeIgniter\Controller; class Analytics extends Controller { function __construct() { $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); } // Finds player: if (!$session->has('player')) { $playerName = '-unknown-'; $error = ''; try { $useragent = $_SERVER['HTTP_USER_AGENT']; $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: public function hit($p_podcast_id, $p_episode_id, ...$filename) { $session = \Config\Services::session(); $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))); } } app/Controllers/BaseController.php +51 −0 Original line number Diff line number Diff line Loading @@ -44,5 +44,56 @@ class BaseController extends Controller //-------------------------------------------------------------------- // E.g.: // $this->session = \Config\Services::session(); $session = \Config\Services::session(); $session->start(); // 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) { $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'), ]); } } app/Controllers/Episodes.php +1 −0 Original line number Diff line number Diff line Loading @@ -125,6 +125,7 @@ class Episodes extends BaseController ->first(), 'episode' => $episode_model->where('slug', $episode_slug)->first(), ]; self::stats($data['podcast']->id); return view('episodes/view.php', $data); } Loading app/Controllers/Podcasts.php +1 −0 Original line number Diff line number Diff line Loading @@ -96,6 +96,7 @@ class Podcasts extends BaseController ->where('podcast_id', $podcast->id) ->findAll(), ]; self::stats($podcast->id); return view('podcasts/view', $data); } Loading Loading
app/Config/Routes.php +7 −0 Original line number Diff line number Diff line Loading @@ -46,6 +46,13 @@ $routes->group('(:podcastSlug)', function ($routes) { ]); }); // Route for podcast audio file analytics (/stats/podcast_id/episode_id/podcast_folder/filename.mp3) $routes->add('/stats/(:num)/(:num)/(:any)', 'Analytics::hit/$1/$2/$3'); // Show the Unknown UserAgents $routes->add('/.well-known/unknown-useragents', 'UnknownUserAgents'); $routes->add('/.well-known/unknown-useragents/(:num)', 'UnknownUserAgents/$1'); /** * -------------------------------------------------------------------- * Additional Routing Loading
app/Controllers/Analytics.php 0 → 100644 +113 −0 Original line number Diff line number Diff line <?php namespace App\Controllers; /** * Class Analytics * Creates Analytics controller * @copyright 2020 Podlibre * @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3 * @link https://castopod.org/ */ use CodeIgniter\Controller; class Analytics extends Controller { function __construct() { $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); } // Finds player: if (!$session->has('player')) { $playerName = '-unknown-'; $error = ''; try { $useragent = $_SERVER['HTTP_USER_AGENT']; $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: public function hit($p_podcast_id, $p_episode_id, ...$filename) { $session = \Config\Services::session(); $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))); } }
app/Controllers/BaseController.php +51 −0 Original line number Diff line number Diff line Loading @@ -44,5 +44,56 @@ class BaseController extends Controller //-------------------------------------------------------------------- // E.g.: // $this->session = \Config\Services::session(); $session = \Config\Services::session(); $session->start(); // 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) { $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'), ]); } }
app/Controllers/Episodes.php +1 −0 Original line number Diff line number Diff line Loading @@ -125,6 +125,7 @@ class Episodes extends BaseController ->first(), 'episode' => $episode_model->where('slug', $episode_slug)->first(), ]; self::stats($data['podcast']->id); return view('episodes/view.php', $data); } Loading
app/Controllers/Podcasts.php +1 −0 Original line number Diff line number Diff line Loading @@ -96,6 +96,7 @@ class Podcasts extends BaseController ->where('podcast_id', $podcast->id) ->findAll(), ]; self::stats($podcast->id); return view('podcasts/view', $data); } Loading