2022-01-30 18:02:52 -05:00
|
|
|
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2022-09-25 13:53:00 -04:00
|
|
|
use App\Helpers\Cookies;
|
|
|
|
use TikScraper\Helpers\Converter;
|
|
|
|
|
2022-01-30 18:02:52 -05:00
|
|
|
class ProxyController {
|
|
|
|
const VALID_TIKTOK_DOMAINS = [
|
|
|
|
"tiktokcdn.com", "tiktokcdn-us.com", "tiktok.com"
|
|
|
|
];
|
|
|
|
|
|
|
|
static private function isValidDomain(string $url) {
|
|
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
|
|
$host_split = explode('.', $host);
|
2022-02-07 15:07:51 -05:00
|
|
|
$host_count = count($host_split);
|
|
|
|
if ($host_count === 2) {
|
|
|
|
// Using no watermark
|
|
|
|
return in_array($host_split[0] . '.' . $host_split[1], self::VALID_TIKTOK_DOMAINS);
|
|
|
|
} elseif ($host_count === 3) {
|
|
|
|
return in_array($host_split[1] . '.' . $host_split[2], self::VALID_TIKTOK_DOMAINS);
|
|
|
|
}
|
|
|
|
return false;
|
2022-01-30 18:02:52 -05:00
|
|
|
}
|
|
|
|
|
2022-02-07 17:45:07 -05:00
|
|
|
static private function checkUrl() {
|
2022-01-30 18:02:52 -05:00
|
|
|
if (!isset($_GET['url'])) {
|
2022-02-07 17:45:07 -05:00
|
|
|
die('You need to send a URL');
|
2022-01-30 18:02:52 -05:00
|
|
|
}
|
|
|
|
|
2022-02-07 17:45:07 -05:00
|
|
|
if (!filter_var($_GET['url'], FILTER_VALIDATE_URL) || !self::isValidDomain($_GET['url'])) {
|
2022-01-30 18:02:52 -05:00
|
|
|
die('Not a valid URL');
|
|
|
|
}
|
|
|
|
|
2022-02-07 17:45:07 -05:00
|
|
|
}
|
|
|
|
|
2022-10-24 14:53:59 -04:00
|
|
|
static private function getFilename(string $id, string $user): string {
|
2022-09-25 13:53:00 -04:00
|
|
|
$filename = 'tiktok-video-' . $id . '-' . $user;
|
2022-02-07 17:45:07 -05:00
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function stream() {
|
2022-03-11 12:56:19 -05:00
|
|
|
self::checkUrl();
|
|
|
|
$url = $_GET['url'];
|
|
|
|
$streamer = new \TikScraper\Stream();
|
|
|
|
$streamer->url($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function download() {
|
2022-09-25 13:53:00 -04:00
|
|
|
self::checkUrl();
|
|
|
|
$method = Cookies::downloader();
|
|
|
|
$downloader = new \TikScraper\Download($method);
|
|
|
|
|
|
|
|
// Params
|
2022-10-24 14:53:59 -04:00
|
|
|
$id = $_GET['id'] ?? '';
|
2022-03-11 12:56:19 -05:00
|
|
|
$watermark = isset($_GET['watermark']);
|
2022-09-25 13:53:00 -04:00
|
|
|
$url = $_GET['url'];
|
|
|
|
$user = $_GET['user'] ?? '';
|
|
|
|
// Filename
|
2022-10-24 14:53:59 -04:00
|
|
|
$filename = self::getFilename($id, $user);
|
2022-09-25 13:53:00 -04:00
|
|
|
// Running
|
2022-10-24 14:53:59 -04:00
|
|
|
$downloader->url($url, $filename, $watermark);
|
2022-01-30 18:02:52 -05:00
|
|
|
}
|
|
|
|
}
|