proxitok/index.php

159 lines
4.4 KiB
PHP
Raw Normal View History

2022-01-01 14:14:57 -05:00
<?php
require __DIR__ . "/vendor/autoload.php";
require __DIR__ . "/helpers/domains.php";
require __DIR__ . "/helpers/settings_elements.php";
2022-01-03 07:43:22 -05:00
use Steampixel\Route;
2022-01-01 14:14:57 -05:00
2022-01-03 07:43:22 -05:00
// LOAD DOTENV
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
2022-01-01 14:14:57 -05:00
2022-01-03 08:18:16 -05:00
function getSubdir(): string {
return $_ENV['APP_SUBDIR'] ? $_ENV['APP_SUBDIR'] : '/';
}
function getApi(array $proxy_elements): \Sovit\TikTok\Api {
$options = [];
2022-01-03 07:43:22 -05:00
// Proxy config
if (in_array($proxy_elements, $_COOKIE)) {
foreach ($proxy_elements as $proxy_element) {
$options[$proxy_element] = $_COOKIE[$proxy_element];
}
}
$api = new \Sovit\TikTok\Api($options);
return $api;
2022-01-01 18:42:03 -05:00
}
2022-01-03 07:43:22 -05:00
function getLatte(): \Latte\Engine {
$latte = new Latte\Engine;
$latte->setTempDirectory('./cache/views');
2022-01-03 08:18:16 -05:00
$latte->addFunction('assets', function (string $name, string $type) {
$subdir = getSubdir();
$path = "{$subdir}/{$type}/{$name}";
return $path;
});
2022-01-03 07:43:22 -05:00
return $latte;
}
function getView(string $template): string {
return "./views/{$template}.latte";
}
2022-01-03 07:43:22 -05:00
Route::add('/', function () {
http_response_code(302);
header('Location: ./home');
});
2022-01-03 07:43:22 -05:00
Route::add('/home', function () {
$latte = getLatte();
$latte->render('./views/home.latte');
});
2022-01-01 14:14:57 -05:00
2022-01-03 07:43:22 -05:00
Route::add('/images', function () use ($domains) {
if (!isset($_GET['url'])) {
die('You need to send a url!');
}
$url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST);
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array($host, $domains['image'])) {
die('Not a valid URL');
}
$img = file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]]));
if ($img) {
header('Content-Type: image/jpeg');
2022-01-03 07:43:22 -05:00
return $img;
} else {
2022-01-03 07:43:22 -05:00
return 'Error while getting image!';
}
});
2022-01-03 07:43:22 -05:00
Route::add('/audios', function () use ($domains) {
if (!isset($_GET['url'])) {
die('You need to send a url!');
}
$url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST);
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array($host, $domains['audio'])) {
die('Not a valid URL');
}
$audio = file_get_contents($url, false, stream_context_create(['http' => ['ignore_errors' => true]]));
if ($audio) {
header('Content-Type: audio/mp3');
2022-01-03 07:43:22 -05:00
return $audio;
} else {
2022-01-03 07:43:22 -05:00
return 'Error while getting audio!';
}
2022-01-01 14:14:57 -05:00
});
2022-01-03 07:43:22 -05:00
Route::add('/stream', function () use ($domains) {
2022-01-01 18:06:00 -05:00
if (!isset($_GET['url'])) {
die('You need to send a url!');
}
$url = $_GET['url'];
$host = parse_url($url, PHP_URL_HOST);
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array($host, $domains['video'])) {
die('Not a valid URL');
}
2022-01-03 07:43:22 -05:00
if (isset($_GET['download'])) {
header('Content-Disposition: attachment; filename="tiktok.mp4"');
}
$streamer = new \Sovit\TikTok\Stream();
$streamer->stream($url);
2022-01-01 18:42:03 -05:00
});
2022-01-03 07:43:22 -05:00
Route::add("/trending", function () use ($proxy_elements) {
2022-01-01 18:42:03 -05:00
$cursor = 0;
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
$cursor = (int) $_GET['cursor'];
}
2022-01-03 07:43:22 -05:00
$latte = getLatte();
$api = getApi($proxy_elements);
2022-01-01 18:42:03 -05:00
$feed = $api->getTrendingFeed($cursor);
if ($feed) {
2022-01-03 07:43:22 -05:00
$latte->render(getView('trending'), ['feed' => $feed]);
2022-01-01 18:42:03 -05:00
} else {
2022-01-03 07:43:22 -05:00
return 'ERROR!';
2022-01-01 18:42:03 -05:00
}
});
2022-01-03 07:43:22 -05:00
Route::add("/@([^/]+)", function (string $username) use ($proxy_elements) {
2022-01-01 14:48:42 -05:00
$cursor = 0;
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
$cursor = (int) $_GET['cursor'];
}
2022-01-03 07:43:22 -05:00
$latte = getLatte();
$api = getApi($proxy_elements);
2022-01-01 18:42:03 -05:00
$feed = $api->getUserFeed($username, $cursor);
if ($feed) {
2022-01-03 07:43:22 -05:00
if ($feed->info->detail->user->privateAccount) {
http_response_code(400);
return 'Private account detected! Not supported';
}
$latte->render(getView('user'), ['feed' => $feed]);
2022-01-01 14:14:57 -05:00
} else {
2022-01-03 07:43:22 -05:00
return 'ERROR!';
2022-01-01 14:14:57 -05:00
}
});
2022-01-03 07:43:22 -05:00
Route::add("/settings", function () use ($proxy_elements) {
$latte = getLatte();
$latte->render(getView('settings'), ["proxy_elements" => $proxy_elements]);
});
2022-01-03 07:43:22 -05:00
Route::add("/settings", function () use ($proxy_elements) {
if (in_array($proxy_elements, $_POST)) {
foreach ($proxy_elements as $proxy_element) {
setcookie($proxy_element, $_POST[$proxy_element], time()+60*60*24*30, '/', '', true, true);
}
}
http_response_code(302);
header('Location: ./home');
2022-01-03 07:43:22 -05:00
}, 'POST');
2022-01-03 07:43:22 -05:00
Route::run($subdir);