proxitok/app/Controllers/ProxyController.php

67 lines
2 KiB
PHP
Raw Permalink Normal View History

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 App\Helpers\Misc;
use TikScraper\Constants\UserAgents as TikScraperUserAgents;
2022-09-25 13:53:00 -04:00
2022-01-30 18:02:52 -05:00
class ProxyController {
const VALID_TIKTOK_DOMAINS = [
"tiktokcdn.com", "tiktokcdn-us.com", "tiktok.com"
];
2023-04-27 14:23:23 -04:00
public static function stream() {
2023-01-26 11:20:38 -05:00
self::checkUrl();
$url = $_GET['url'];
$config['user_agent'] = Misc::env("USER_AGENT", TikScraperUserAgents::DEFAULT);
$streamer = new \TikScraper\Stream($config);
2023-01-26 11:20:38 -05:00
$streamer->url($url);
}
2023-04-27 14:23:23 -04:00
public static function download() {
2023-01-26 11:20:38 -05:00
self::checkUrl();
$method = Cookies::downloader();
$downloader = new \TikScraper\Download($method);
// Params
$id = $_GET['id'] ?? '';
$watermark = isset($_GET['watermark']);
$url = $_GET['url'];
$user = $_GET['user'] ?? '';
// Filename
$filename = self::getFilename($id, $user);
// Running
$downloader->url($url, $filename, $watermark);
}
static private function isValidDomain(string $url): bool {
$valid = false;
2022-01-30 18:02:52 -05:00
$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
2023-01-26 11:20:38 -05:00
$valid = in_array($host_split[0] . '.' . $host_split[1], self::VALID_TIKTOK_DOMAINS);
2022-02-07 15:07:51 -05:00
} elseif ($host_count === 3) {
2023-01-26 11:20:38 -05:00
$valid = in_array($host_split[1] . '.' . $host_split[2], self::VALID_TIKTOK_DOMAINS);
2022-02-07 15:07:51 -05:00
}
2023-01-26 11:20:38 -05:00
return $valid;
2022-01-30 18:02:52 -05:00
}
2023-01-26 11:20:38 -05:00
static private function checkUrl(): void {
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;
}
2022-01-30 18:02:52 -05:00
}