From 837f126021ebf9c6f6d926aff2f367230f1b23e5 Mon Sep 17 00:00:00 2001 From: Pablo Ferreiro Date: Mon, 31 Jan 2022 00:02:52 +0100 Subject: [PATCH] Project structure change --- README.md | 12 +-- .../CacheEngines => app/Cache}/JSONCache.php | 2 +- .../CacheEngines => app/Cache}/RedisCache.php | 2 +- app/Controllers/FollowingController.php | 15 +++ app/Controllers/MusicController.php | 21 ++++ app/Controllers/ProxyController.php | 35 +++++++ .../Controllers/SettingsController.php | 38 +++---- app/Controllers/TagController.php | 32 ++++++ app/Controllers/TrendingController.php | 32 ++++++ app/Controllers/UserController.php | 36 +++++++ app/Controllers/VideoController.php | 19 ++++ .../Settings.php => app/Helpers/Cookies.php | 4 +- {helpers => app/Helpers}/ErrorHandler.php | 2 +- {helpers => app/Helpers}/Following.php | 4 +- {helpers => app/Helpers}/Misc.php | 25 +++-- {helpers => app/Helpers}/RSS.php | 4 +- {views/models => app/Models}/BaseTemplate.php | 2 +- {views/models => app/Models}/FeedTemplate.php | 2 +- .../Models}/FollowingTemplate.php | 2 +- {views/models => app/Models}/ItemTemplate.php | 2 +- .../Models}/SettingsTemplate.php | 8 +- composer.json | 5 +- composer.lock | 2 +- index.php | 5 +- routes.php | 43 ++++++++ routes/assets.php | 38 ------- routes/following.php | 15 --- routes/index.php | 99 ------------------- routes/rss.php | 46 --------- 29 files changed, 298 insertions(+), 254 deletions(-) rename {helpers/CacheEngines => app/Cache}/JSONCache.php (96%) rename {helpers/CacheEngines => app/Cache}/RedisCache.php (96%) create mode 100644 app/Controllers/FollowingController.php create mode 100644 app/Controllers/MusicController.php create mode 100644 app/Controllers/ProxyController.php rename routes/settings.php => app/Controllers/SettingsController.php (61%) create mode 100644 app/Controllers/TagController.php create mode 100644 app/Controllers/TrendingController.php create mode 100644 app/Controllers/UserController.php create mode 100644 app/Controllers/VideoController.php rename helpers/Settings.php => app/Helpers/Cookies.php (93%) rename {helpers => app/Helpers}/ErrorHandler.php (90%) rename {helpers => app/Helpers}/Following.php (93%) rename {helpers => app/Helpers}/Misc.php (84%) rename {helpers => app/Helpers}/RSS.php (94%) rename {views/models => app/Models}/BaseTemplate.php (90%) rename {views/models => app/Models}/FeedTemplate.php (91%) rename {views/models => app/Models}/FollowingTemplate.php (91%) rename {views/models => app/Models}/ItemTemplate.php (91%) rename {views/models => app/Models}/SettingsTemplate.php (70%) create mode 100644 routes.php delete mode 100644 routes/assets.php delete mode 100644 routes/following.php delete mode 100644 routes/index.php delete mode 100644 routes/rss.php diff --git a/README.md b/README.md index ca32da6..38c91d0 100644 --- a/README.md +++ b/README.md @@ -36,17 +36,17 @@ Available cache engines: You don't have to do anything more ### Nginx -Add the following to your config (you can modify the tiktok-viewer part if you have or not a subdir): +Add the following to your config (you can modify the proxitok part if you have or not a subdir): ``` -location /tiktok-viewer { - return 302 $scheme://$host/tiktok-viewer/; +location /proxitok { + return 302 $scheme://$host/proxitok/; } -location /tiktok-viewer/ { - try_files $uri $uri/ /tiktok-viewer/index.php?$query_string; +location /proxitok/ { + try_files $uri $uri/ /proxitok/index.php?$query_string; } -location /tiktok-viewer/.env { +location /proxitok/.env { deny all; return 404; } diff --git a/helpers/CacheEngines/JSONCache.php b/app/Cache/JSONCache.php similarity index 96% rename from helpers/CacheEngines/JSONCache.php rename to app/Cache/JSONCache.php index 982c8e3..7b16c60 100644 --- a/helpers/CacheEngines/JSONCache.php +++ b/app/Cache/JSONCache.php @@ -1,5 +1,5 @@ render(Misc::getView('following'), new FollowingTemplate($users, $feed)); + } +} diff --git a/app/Controllers/MusicController.php b/app/Controllers/MusicController.php new file mode 100644 index 0000000..c5edec6 --- /dev/null +++ b/app/Controllers/MusicController.php @@ -0,0 +1,21 @@ +getMusicFeed($music_id, $cursor); + if ($feed->meta->success) { + $latte = Misc::latte(); + $latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed)); + } else { + ErrorHandler::show($feed->meta); + } + } +} diff --git a/app/Controllers/ProxyController.php b/app/Controllers/ProxyController.php new file mode 100644 index 0000000..494c29e --- /dev/null +++ b/app/Controllers/ProxyController.php @@ -0,0 +1,35 @@ +url($url, "tiktok-video", 'mp4'); + } else { + // Stream + $streamer = new \Sovit\TikTok\Stream(); + $streamer->stream($url); + } + } +} diff --git a/routes/settings.php b/app/Controllers/SettingsController.php similarity index 61% rename from routes/settings.php rename to app/Controllers/SettingsController.php index 4f9cb60..daf7a2c 100644 --- a/routes/settings.php +++ b/app/Controllers/SettingsController.php @@ -1,29 +1,29 @@ mount('/settings', function () use ($router) { - $router->get('/', function () { +class SettingsController { + static public function index() { $latte = Misc::latte(); $latte->render(Misc::getView('settings'), new SettingsTemplate()); - }); + } - $router->post('/proxy', function () { - if (in_array(Settings::PROXY, $_POST)) { - foreach (Settings::PROXY as $proxy_element) { - Settings::set($proxy_element, $_POST[$proxy_element]); + static public function proxy() { + if (in_array(Cookies::PROXY, $_POST)) { + foreach (Cookies::PROXY as $proxy_element) { + Cookies::set($proxy_element, $_POST[$proxy_element]); } } http_response_code(302); - header('Location: ./home'); - }); + $url = Misc::env('APP_URL', ''); + header("Location: {$url}"); + } - $router->post('/following', function () { + static public function following() { $following = Following::getUsers(); if (!isset($_POST['mode']) || empty($_POST['mode'])) { die('You need to send a mode'); @@ -48,7 +48,7 @@ $router->mount('/settings', function () use ($router) { // Build string $following_string = implode(',', $following); - Settings::set('following', $following_string); + Cookies::set('following', $following_string); header('Location: ../settings'); - }); -}); + } +} diff --git a/app/Controllers/TagController.php b/app/Controllers/TagController.php new file mode 100644 index 0000000..5ed8c26 --- /dev/null +++ b/app/Controllers/TagController.php @@ -0,0 +1,32 @@ +getChallengeFeed($name, $cursor); + if ($feed->meta->success) { + $latte = Misc::latte(); + $latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed)); + } else { + ErrorHandler::show($feed->meta); + } + } + + static public function rss(string $name) { + $api = Misc::api(); + $feed = $api->getChallengeFeed($name); + if ($feed->meta->success) { + $feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->challenge->desc, $feed->items); + // Setup headers + RSS::setHeaders('tag.rss'); + echo $feed; + } + } +} diff --git a/app/Controllers/TrendingController.php b/app/Controllers/TrendingController.php new file mode 100644 index 0000000..2d1cffe --- /dev/null +++ b/app/Controllers/TrendingController.php @@ -0,0 +1,32 @@ +getTrendingFeed($cursor); + if ($feed->meta->success) { + $latte = Misc::latte(); + $latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed)); + } else { + ErrorHandler::show($feed->meta); + } + } + + static public function rss() { + $api = Misc::api(); + $feed = $api->getTrendingFeed(); + if ($feed->meta->success) { + $feed = RSS::build('/trending', 'Trending', 'Tiktok trending', $feed->items); + // Setup headers + RSS::setHeaders('trending.rss'); + echo $feed; + } + } +} diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php new file mode 100644 index 0000000..91398a9 --- /dev/null +++ b/app/Controllers/UserController.php @@ -0,0 +1,36 @@ +getUserFeed($username, $cursor); + if ($feed->meta->success) { + if ($feed->info->detail->user->privateAccount) { + http_response_code(400); + echo 'Private account detected! Not supported'; + } + $latte = Misc::latte(); + $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed)); + } else { + ErrorHandler::show($feed->meta); + } + } + + static public function rss(string $username) { + $api = Misc::api(); + $feed = $api->getUserFeed($username); + if ($feed->meta->success) { + $feed = RSS::build('/@'.$username, $feed->info->detail->user->nickname, $feed->info->detail->user->signature, $feed->items); + // Setup headers + RSS::setHeaders('user.rss'); + echo $feed; + } + } +} diff --git a/app/Controllers/VideoController.php b/app/Controllers/VideoController.php new file mode 100644 index 0000000..ae1a6a6 --- /dev/null +++ b/app/Controllers/VideoController.php @@ -0,0 +1,19 @@ +getVideoByID($video_id); + if ($item->meta->success) { + $latte = Misc::latte(); + $latte->render(Misc::getView('video'), new ItemTemplate($item->info->detail->user->nickname, $item)); + } else { + ErrorHandler::show($item->meta); + } + } +} diff --git a/helpers/Settings.php b/app/Helpers/Cookies.php similarity index 93% rename from helpers/Settings.php rename to app/Helpers/Cookies.php index 4279c24..97e277b 100644 --- a/helpers/Settings.php +++ b/app/Helpers/Cookies.php @@ -1,7 +1,7 @@ setTempDirectory($cache_path); // -- CUSTOM FUNCTIONS -- // // Import assets - $latte->addFunction('assets', function (string $name, string $type) use ($url) { + $latte->addFunction('assets', function (string $name, string $type) use ($url) { $path = "{$url}/{$type}/{$name}"; return $path; }); diff --git a/helpers/RSS.php b/app/Helpers/RSS.php similarity index 94% rename from helpers/RSS.php rename to app/Helpers/RSS.php index 44644c3..0c1debf 100644 --- a/helpers/RSS.php +++ b/app/Helpers/RSS.php @@ -1,5 +1,5 @@ proxy_elements = Settings::PROXY; + $this->proxy_elements = Cookies::PROXY; $this->following = Following::getUsers(); } } diff --git a/composer.json b/composer.json index 331eba1..88d3630 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "pablouser1/proxitok", "description": "An alternative frontend for TikTok", - "version": "1.3.0", + "version": "1.3.1", "license": "AGPL-3.0-or-later", "repositories": [ { @@ -19,8 +19,7 @@ }, "autoload": { "psr-4": { - "Helpers\\": "helpers/", - "Views\\Models\\": "views/models/" + "App\\": "app/" } } } diff --git a/composer.lock b/composer.lock index 57d09c0..d682b8b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c86d4aba41374f807418dde1044dba45", + "content-hash": "1ecb565500ab6893126d60eeebcd7379", "packages": [ { "name": "bhaktaraz/php-rss-generator", diff --git a/index.php b/index.php index 0568d73..0da71fc 100644 --- a/index.php +++ b/index.php @@ -7,5 +7,8 @@ $dotenv->safeLoad(); // ROUTER $router = new Bramus\Router\Router(); -require __DIR__ . '/routes/index.php'; +$router->setNamespace('\App\Controllers'); + +require __DIR__ . '/routes.php'; + $router->run(); diff --git a/routes.php b/routes.php new file mode 100644 index 0000000..f0d79d0 --- /dev/null +++ b/routes.php @@ -0,0 +1,43 @@ +get('/', function () { + $latte = Misc::latte(); + $latte->render(Misc::getView('home'), new BaseTemplate('Home')); +}); + +$router->get('/about', function () { + $latte = Misc::latte(); + $latte->render(Misc::getView('about'), new BaseTemplate('About')); +}); + +$router->get('/stream', 'ProxyController@stream'); + +$router->mount('/trending', function () use ($router) { + $router->get('/', 'TrendingController@get'); + $router->get('/rss', 'TrendingController@rss'); +}); + +$router->mount('/@([^/]+)', function () use ($router) { + $router->get('/', 'UserController@get'); + $router->get('/rss', 'UserController@rss'); +}); + +$router->get('/video/(\w+)', 'VideoController@get'); + +$router->mount('/tag', function () use ($router) { + $router->get('/([^/]+)', 'TagController@get'); + $router->get('/rss', 'TagController@rss'); +}); + +$router->get('/music/([^/]+)', 'MusicController@get'); + +// -- Settings -- // +$router->mount('/settings', function () use ($router) { + $router->get('/', 'SettingsController@index'); + $router->post('/proxy', 'SettingsController@proxy'); + $router->post('/following', 'SettingsController@following'); +}); + +$router->get('/following', 'FollowingController@get'); diff --git a/routes/assets.php b/routes/assets.php deleted file mode 100644 index 51b8137..0000000 --- a/routes/assets.php +++ /dev/null @@ -1,38 +0,0 @@ -get('/stream', function () { - if (!isset($_GET['url'])) { - die('You need to send a url!'); - } - - $url = $_GET['url']; - if (!filter_var($url, FILTER_VALIDATE_URL) || !isValidDomain($url)) { - die('Not a valid URL'); - } - - if (isset($_GET['download'])) { - // Download (video only) - $downloader = new \Sovit\TikTok\Download(); - $downloader->url($url, "tiktok-video", 'mp4'); - } else { - // Stream - $streamer = new \Sovit\TikTok\Stream(); - $streamer->stream($url); - } -}); diff --git a/routes/following.php b/routes/following.php deleted file mode 100644 index 3b0eb6f..0000000 --- a/routes/following.php +++ /dev/null @@ -1,15 +0,0 @@ -get('/following', function () { - $users = Following::getUsers(); - $feed = Following::getAll($users); - $latte = Misc::latte(); - $latte->render(Misc::getView('following'), new FollowingTemplate($users, $feed)); -}); diff --git a/routes/index.php b/routes/index.php deleted file mode 100644 index 3fa0f65..0000000 --- a/routes/index.php +++ /dev/null @@ -1,99 +0,0 @@ -get('/', function () { - $latte = Misc::latte(); - $latte->render(Misc::getView('home'), new BaseTemplate('Home')); -}); - -$router->get('/about', function () { - $latte = Misc::latte(); - $latte->render(Misc::getView('about'), new BaseTemplate('About')); -}); - -$router->get("/trending", function () { - $cursor = 0; - if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { - $cursor = (int) $_GET['cursor']; - } - $api = Misc::api(); - $feed = $api->getTrendingFeed($cursor); - if ($feed->meta->success) { - $latte = Misc::latte(); - $latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed)); - } else { - ErrorHandler::show($feed->meta); - } -}); - -$router->get("/@([^/]+)", function (string $username) { - $cursor = 0; - if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { - $cursor = (int) $_GET['cursor']; - } - $api = Misc::api(); - $feed = $api->getUserFeed($username, $cursor); - if ($feed->meta->success) { - if ($feed->info->detail->user->privateAccount) { - http_response_code(400); - return 'Private account detected! Not supported'; - } - $latte = Misc::latte(); - $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed)); - } else { - ErrorHandler::show($feed->meta); - } -}); - -$router->get('/video/([^/]+)', function (string $video_id) { - $api = Misc::api(); - $item = $api->getVideoByID($video_id); - if ($item->meta->success) { - $latte = Misc::latte(); - $latte->render(Misc::getView('video'), new ItemTemplate($item->info->detail->user->nickname, $item)); - } else { - ErrorHandler::show($item->meta); - } -}); - -$router->get('/music/([^/]+)', function (string $music_id) { - $cursor = 0; - if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { - $cursor = (int) $_GET['cursor']; - } - - $api = Misc::api(); - $feed = $api->getMusicFeed($music_id, $cursor); - if ($feed->meta->success) { - $latte = Misc::latte(); - $latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed)); - } else { - ErrorHandler::show($feed->meta); - } -}); - -$router->get('/tag/(\w+)', function (string $name) { - $cursor = 0; - if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { - $cursor = (int) $_GET['cursor']; - } - $api = Misc::api(); - $feed = $api->getChallengeFeed($name, $cursor); - if ($feed->meta->success) { - $latte = Misc::latte(); - $latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed)); - } else { - ErrorHandler::show($feed->meta); - } -}); diff --git a/routes/rss.php b/routes/rss.php deleted file mode 100644 index f33198e..0000000 --- a/routes/rss.php +++ /dev/null @@ -1,46 +0,0 @@ -all("/@([^/]+)/rss", function (string $username) { - $api = Misc::api(); - $feed = $api->getUserFeed($username); - if ($feed->meta->success) { - $feed = RSS::build('/@'.$username, $feed->info->detail->user->nickname, $feed->info->detail->user->signature, $feed->items); - // Setup headers - RSS::setHeaders('user.rss'); - echo $feed; - } else { - ErrorHandler::show($feed->meta); - } -}); - -$router->all("/trending/rss", function () { - $api = Misc::api(); - $feed = $api->getTrendingFeed(); - if ($feed->meta->success) { - $feed = RSS::build('/trending', 'Trending', 'Tiktok trending', $feed->items); - // Setup headers - RSS::setHeaders('trending.rss'); - echo $feed; - } else { - ErrorHandler::show($feed->meta); - } -}); - -$router->all("/tag/(\w+)/rss", function (string $name) { - $api = Misc::api(); - $feed = $api->getChallengeFeed($name); - if ($feed->meta->success) { - $feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->challenge->desc, $feed->items); - // Setup headers - RSS::setHeaders('tag.rss'); - echo $feed; - } else { - ErrorHandler::show($feed->meta); - } -});