From 22a176db1502f827d88f1918a1e05ffc18fdbf3b Mon Sep 17 00:00:00 2001
From: Maurice Renck <work@maurice-renck.de>
Date: Sun, 31 Mar 2019 18:45:04 +0200
Subject: [PATCH] =?UTF-8?q?trying=20to=20get=20along=20with=20the=203=20in?=
 =?UTF-8?q?stall=20methods=E2=80=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 composer.json                       |   3 +-
 composer.lock                       |   2 +-
 index.php                           | 161 +++++++++++++++++++++++++++-
 vendor/composer/autoload_files.php  |   1 -
 vendor/composer/autoload_static.php |   1 -
 5 files changed, 162 insertions(+), 6 deletions(-)

diff --git a/composer.json b/composer.json
index ca57f60..3fe3ae8 100644
--- a/composer.json
+++ b/composer.json
@@ -9,10 +9,9 @@
             "email": "hello@maurice-renck.de"
         }
     ],
-    "version": "1.0.0",
+    "version": "1.0.1",
     "autoload": {
         "files": [
-            "config.php",
             "utils/PodcasterUtils.php",
             "utils/PodcasterAudioUtils.php",
             "utils/PodcasterStats.php",
diff --git a/composer.lock b/composer.lock
index d1ea95e..fe2d1d7 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "3f2a5848de350bd9bd998b8dea73cd95",
+    "content-hash": "a17bb12c37678af3786e994da867788f",
     "packages": [
         {
             "name": "getkirby/composer-installer",
diff --git a/index.php b/index.php
index 539f2df..482820c 100644
--- a/index.php
+++ b/index.php
@@ -1,2 +1,161 @@
 <?php
-require_once __DIR__ . '/vendor/autoload.php';
+namespace Plugin\Podcaster;
+use Kirby;
+use Kirby\Exception\Exception;
+use \PiwikTracker;
+use Kirby\Http\Response;
+
+// load([
+//     'distantnative\\Retour\\Log'       => 'models/Log.php',
+//     'distantnative\\Retour\\Logs'      => 'models/Logs.php',
+//     'distantnative\\Retour\\Redirects' => 'models/Redirects.php',
+//     'distantnative\\Retour\\Stats'     => 'models/Stats.php',
+//     'distantnative\\Retour\\System'    => 'models/System.php'
+// ], __DIR__);
+
+Kirby::plugin('mauricerenck/podcaster', [
+    'options' => [
+        'statsInternal' => false,
+        'statsType' => 'mysql',
+        'statsHost' => null,
+        'statsDatabase' => null,
+        'statsUser' => null,
+        'statsPassword' => null,
+        'matomoToken' => null,
+        'matomoBaseUrl' => null
+    ],
+    'templates' => [
+        'podcasterfeed' => __DIR__ . '/templates/podcasterfeed.php'
+    ],
+    'blueprints' => [
+        'pages/podcasterfeed' => __DIR__ . '/blueprints/pages/podcasterfeed.yml',
+        'tabs/podcasterepisode' => __DIR__ . '/blueprints/tabs/episode.yml',
+        'files/podcaster-episode' => __DIR__ . '/blueprints/files/podcaster-episode.yml'
+    ],
+    'sections' => [
+        'podcaster-stats' => [
+            'props' => [
+                'headline' => function ($headline = 'Last modified') {
+                    return $headline;
+                }
+            ]
+        ]
+    ],
+    'snippets' => [
+        'podcaster-player' => __DIR__ . '/snippets/podcaster-player.php',
+        'podcaster-podlove-player' => __DIR__ . '/snippets/podlove-player.php',
+        'podcaster-html5-player' => __DIR__ . '/snippets/html5-player.php',
+        'podcaster-ogaudio' => __DIR__ . '/snippets/og-audio.php'
+    ],
+    'routes' => [
+        [
+            'pattern' => '(:all)/podcaster-feed-style',
+            'action' => function () {
+                $string = file_get_contents(__DIR__ . '/res/feed-style.xsl');
+                return new Response($string, 'text/xml');
+            }
+        ],
+        [
+            'pattern' => '(:all)/' . option('mauricerenck.podcaster.defaultFeed', 'feed'),
+            'action' => function ($slug) {
+                $podcasterUtils = new PodcasterUtils();
+                $page = $podcasterUtils->getPageFromSlug($slug. '/' . option('mauricerenck.podcaster.defaultFeed', 'feed'));
+
+                if(option('mauricerenck.podcaster.statsInternal') === true) {
+                    $stats = new PodcasterStats();
+                    $trackingDate = time();
+                    $stats->increaseFeedVisits($page, $trackingDate);
+                }
+
+                if($page->podcasterMatomoFeedEnabled()->isTrue()) {
+                    $matomo = new PiwikTracker($page->podcasterMatomoFeedSiteId(), option('mauricerenck.podcaster.matomoBaseUrl'));
+
+                    $matomo->setTokenAuth(option('mauricerenck.podcaster.matomoToken'));
+                    $matomo->disableSendImageResponse();
+                    $matomo->disableCookieSupport();
+                    $matomo->setUrl($page->url());
+                    $matomo->setIp($_SERVER['REMOTE_ADDR']);
+
+                    if($page->podcasterMatomoFeedGoalId()->isNotEmpty()) {
+                        $matomo->doTrackGoal($page->podcasterMatomoFeedGoalId(), 1);
+                    }
+
+                    if($page->podcasterMatomoFeedEventName()->isNotEmpty()) {
+                        $matomo->doTrackEvent($page->podcasterTitle(), $page->podcasterMatomoFeedEventName(), 1);
+                    }
+
+                    if($page->podcasterMatomoFeedAction()->isTrue()) {
+                        $matomo->doTrackAction($page->url(), 'download');
+                    }
+                }
+
+                return new Response($page->render(), 'text/xml');
+            }
+        ],
+        [
+            'pattern' => '(:all)/' . option('mauricerenck.podcaster.downloadTriggerPath', 'download') . '/(:any)',
+            'action' => function ($slug, $filename) {
+
+                $podcasterUtils = new PodcasterUtils();
+                $episode = $podcasterUtils->getPageFromSlug($slug);
+                $podcasterUtils->setCurrentEpisode($episode);
+
+                $podcast = $episode->siblings()->find('feed');
+
+                if(option('mauricerenck.podcaster.statsInternal') === true) {
+                    $stats = new PodcasterStats();
+                    $trackingDate = time();
+                    $stats->increaseDownloads($episode, $trackingDate);
+                }
+
+                if($podcast->podcasterMatomoEnabled()->isTrue()) {
+                    $matomo = new PiwikTracker($podcast->podcasterMatomoSiteId(), option('mauricerenck.podcaster.matomoBaseUrl'));
+
+                    // setup
+                    $matomo->setTokenAuth(option('mauricerenck.podcaster.matomoToken'));
+                    $matomo->disableSendImageResponse();
+                    $matomo->disableCookieSupport();
+                    $matomo->setUrl($episode->url());
+                    $matomo->setIp($_SERVER['REMOTE_ADDR']);
+        
+                    if($podcast->podcasterMatomoGoalId()->isNotEmpty()) {
+                        $matomo->doTrackGoal($podcast->podcasterMatomoGoalId(), 1);
+                    }
+        
+                    if($podcast->podcasterMatomoEventName()->isNotEmpty()) {
+                        $matomo->doTrackEvent($podcast->podcasterTitle(), $episode->title(), $podcast->podcasterMatomoEventName());
+                    }
+        
+                    if($podcast->podcasterMatomoAction()->isTrue()) {
+                        $matomo->doTrackAction($episode->url(), 'download');
+                    }
+                }
+
+                $filename = str_replace('.mp3', '', $filename);
+                return $podcasterUtils->getPodcastFile();
+            }
+        ]
+    ],
+    'hooks' => [
+        'file.create:after' => function ($file) {
+            if($file->extension() == 'mp3') {
+                try {
+                    $audioUtils = new PodcasterAudioUtils();
+                    $audioUtils->setAudioFileMeta($file);
+                } catch(Exception $e) {
+                    throw new Exception(array('details' => 'the audio id3 data could not be read'));
+                }
+            }
+        },
+        'file.replace:after' => function ($file) {
+            if($file->extension() == 'mp3') {
+                try {
+                    $audioUtils = new PodcasterAudioUtils();
+                    $audioUtils->setAudioFileMeta($file);
+                } catch (Exception $e) {
+                    throw new Exception(array('details' => 'the audio id3 data could not be read'));
+                }
+            }
+        }
+    ]
+]);
\ No newline at end of file
diff --git a/vendor/composer/autoload_files.php b/vendor/composer/autoload_files.php
index 7741194..6fd4964 100644
--- a/vendor/composer/autoload_files.php
+++ b/vendor/composer/autoload_files.php
@@ -8,7 +8,6 @@
 return array(
     '320cde22f66dd4f5d3fd621d3e88b98f' => $vendorDir . '/symfony/polyfill-ctype/bootstrap.php',
     '6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php',
-    'ed5eafd960033377637fa9e405d3c2a7' => $baseDir . '/config.php',
     '8df3dfd1f38b5f54e639d8aee9e2bd5b' => $baseDir . '/utils/PodcasterUtils.php',
     'aa1a9ddc5c71d010b159c1768f7031e2' => $baseDir . '/utils/PodcasterAudioUtils.php',
     '1a1828adf062a2860974d695d05734cf' => $baseDir . '/utils/PodcasterStats.php',
diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php
index 2189fe2..9d00859 100644
--- a/vendor/composer/autoload_static.php
+++ b/vendor/composer/autoload_static.php
@@ -9,7 +9,6 @@ class ComposerStaticInitf221e7d41fb5f9d943f848bde0ca9936
     public static $files = array (
         '320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
         '6124b4c8570aa390c21fafd04a26c69f' => __DIR__ . '/..' . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php',
-        'ed5eafd960033377637fa9e405d3c2a7' => __DIR__ . '/../..' . '/config.php',
         '8df3dfd1f38b5f54e639d8aee9e2bd5b' => __DIR__ . '/../..' . '/utils/PodcasterUtils.php',
         'aa1a9ddc5c71d010b159c1768f7031e2' => __DIR__ . '/../..' . '/utils/PodcasterAudioUtils.php',
         '1a1828adf062a2860974d695d05734cf' => __DIR__ . '/../..' . '/utils/PodcasterStats.php',