2022-02-05 18:58:30 -05:00
|
|
|
<?php
|
|
|
|
namespace App\Controllers;
|
2022-07-26 19:49:14 -04:00
|
|
|
use App\Helpers\ErrorHandler;
|
2022-02-05 18:58:30 -05:00
|
|
|
use App\Helpers\Misc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to be compatible with HTML forms
|
|
|
|
*/
|
|
|
|
class RedirectController {
|
|
|
|
static public function redirect() {
|
2022-03-29 13:30:31 -04:00
|
|
|
$endpoint = '/';
|
|
|
|
if (isset($_GET['type'], $_GET['term'])) {
|
2022-10-29 14:35:07 -04:00
|
|
|
$term = trim($_GET['term']);
|
2022-03-29 13:30:31 -04:00
|
|
|
switch ($_GET['type']) {
|
2022-07-26 19:49:14 -04:00
|
|
|
case 'url':
|
2022-07-31 12:20:38 -04:00
|
|
|
$endpoint = self::to_endpoint($term);
|
2022-07-26 19:49:14 -04:00
|
|
|
if (!$endpoint) {
|
2022-09-03 08:01:03 -04:00
|
|
|
ErrorHandler::showText(400, 'Invalid TikTok URL');
|
2022-07-26 19:49:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2022-03-29 13:30:31 -04:00
|
|
|
case 'user':
|
2022-04-21 13:59:55 -04:00
|
|
|
// Remove @ if sent
|
|
|
|
if ($term[0] === '@') {
|
|
|
|
$term = substr($term, 1);
|
|
|
|
}
|
2022-10-29 14:35:07 -04:00
|
|
|
$endpoint = '/@' . urlencode($term);
|
2022-03-29 13:30:31 -04:00
|
|
|
break;
|
|
|
|
case 'tag':
|
2022-04-21 13:59:55 -04:00
|
|
|
// Remove # if sent
|
|
|
|
if ($term[0] === '#') {
|
|
|
|
$term = substr($term, 1);
|
|
|
|
}
|
2022-10-29 14:35:07 -04:00
|
|
|
$endpoint = '/tag/' . urlencode($term);
|
2022-03-29 13:30:31 -04:00
|
|
|
break;
|
|
|
|
case 'music':
|
2022-10-29 14:35:07 -04:00
|
|
|
$endpoint = '/music/' . urlencode($term);
|
2022-03-29 13:30:31 -04:00
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
// The @username part is not used, but
|
|
|
|
// it is the schema that TikTok follows
|
2022-10-29 14:35:07 -04:00
|
|
|
$endpoint = '/@placeholder/video/' . urlencode($term);
|
2022-03-29 13:30:31 -04:00
|
|
|
break;
|
|
|
|
}
|
2022-02-05 18:58:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$url = Misc::url($endpoint);
|
|
|
|
header("Location: {$url}");
|
|
|
|
}
|
2022-07-26 19:49:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* to_endpoint maps a TikTok URL into a ProxiTok-compatible endpoint URL.
|
|
|
|
*/
|
2022-07-31 12:20:38 -04:00
|
|
|
static private function to_endpoint(string $url): string {
|
2022-08-15 18:03:48 -04:00
|
|
|
if (preg_match('%^(?:https?://|www\.)?(?:vm\.|vt\.)?tiktok\.com/(?:t/)?([A-Za-z0-9]+)%', $url, $m)) {
|
2022-07-26 19:49:14 -04:00
|
|
|
// Short video URL
|
|
|
|
return '/@placeholder/video/' . $m[1];
|
|
|
|
} elseif (preg_match('%^https://www\.tiktok\.com/(.+)%', $url, $m)) {
|
|
|
|
// Username component (which may indicate a user profile URL or a video URL)
|
|
|
|
if (preg_match('%^(@[A-Za-z0-9_.]+)(?:/|$)(.*)%', $m[1], $u)) {
|
|
|
|
if ($u[2] == '') {
|
|
|
|
// User profile URL
|
2022-10-29 14:35:07 -04:00
|
|
|
return '/' . urlencode($u[1]);
|
2022-07-26 19:49:14 -04:00
|
|
|
} elseif (preg_match('%^video/(\d+)%', $u[2], $v)) {
|
|
|
|
// Video URL
|
2022-10-29 14:35:07 -04:00
|
|
|
return '/' . urlencode($u[1]) . '/video/' . urlencode($v[1]);
|
2022-07-26 19:49:14 -04:00
|
|
|
}
|
|
|
|
} elseif (preg_match('%^tag/([^ ]+?)(?:/|$)%', $m[1], $t)) {
|
|
|
|
// Tag URL
|
2022-10-29 14:35:07 -04:00
|
|
|
return '/tag/' . urlencode($t[1]);
|
2022-07-26 19:49:14 -04:00
|
|
|
} elseif (preg_match('%^music/([^ ]+?)(?:/|$)%', $m[1], $m)) {
|
|
|
|
// Music URL
|
2022-10-29 14:35:07 -04:00
|
|
|
return '/music/' . urlencode($m[1]);
|
2022-07-26 19:49:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2022-02-05 18:58:30 -05:00
|
|
|
}
|