Files

221 lines
8.7 KiB
PHP
Raw Permalink Normal View History

2015-10-08 14:08:26 -04:00
<?php
class Controller
{
2018-05-21 11:49:15 -04:00
const CACHE_CLEAR_PATH = '/clear-cache';
2017-10-31 15:35:13 -04:00
2018-05-21 11:49:15 -04:00
protected static $queuedFunctions = [];
2016-05-26 13:37:01 -04:00
2018-05-21 11:49:15 -04:00
public static function dispatch($uri)
2015-10-08 14:08:26 -04:00
{
2018-05-21 11:49:15 -04:00
try {
if (IS_PRODUCTION && function_exists('newrelic_name_transaction')) {
newrelic_name_transaction(Request::getMethod() . ' ' . strtolower($uri));
}
2016-07-25 13:56:45 -04:00
2018-05-21 11:49:15 -04:00
$viewAndParams = static::execute(Request::getMethod(), $uri);
$viewTemplate = $viewAndParams[0];
$viewParameters = $viewAndParams[1] ?? [];
if (!IS_PRODUCTION && isset($viewAndParams[2])) {
throw new Exception('use response::setheader instead of returning headers');
}
2015-10-08 14:08:26 -04:00
2018-05-21 11:49:15 -04:00
if (!$viewTemplate) {
if ($viewTemplate !== null) {
throw new LogicException('All execute methods must return a template or NULL.');
}
} else {
$layout = !(isset($viewParameters['_no_layout']) && $viewParameters['_no_layout']);
unset($viewParameters['_no_layout']);
$layoutParams = $viewParameters[View::LAYOUT_PARAMS] ?? [];
unset($viewParameters[View::LAYOUT_PARAMS]);
$content = View::render($viewTemplate, $viewParameters + ['fullPage' => true]);
if ($layout) {
$content = View::render('layout/basic', ['content' => $content] + $layoutParams);
2019-10-09 14:38:46 -04:00
$content = View::safeExternalLinks($content, Request::getHost());
}
Response::setContent($content);
2018-05-21 11:49:15 -04:00
}
Response::setDefaultSecurityHeaders();
if (Request::isGzipAccepted()) {
Response::gzipContentIfNotDisabled();
}
Response::send();
} catch (StopException $e) {
2016-11-25 16:06:58 -05:00
}
2016-03-10 21:25:58 -05:00
}
2018-05-21 11:49:15 -04:00
public static function execute($method, $uri)
2015-10-08 14:08:26 -04:00
{
2018-05-21 11:49:15 -04:00
$router = static::getRouterWithRoutes();
2019-02-05 16:56:59 -05:00
$domainResult = static::performDomainRouting($uri);
if ($domainResult) {
return $domainResult;
}
2019-03-21 14:28:22 -04:00
$dispatcher = new Routing\Dispatcher($router->getData());
2018-05-21 11:49:15 -04:00
try {
return $dispatcher->dispatch($method, $uri);
} catch (\Routing\HttpRouteNotFoundException $e) {
2019-03-21 14:28:22 -04:00
$lowerUri = strtolower($uri);
if ($lowerUri !== $uri && $dispatcher->hasMatchingRouteForUri($method, $lowerUri)) {
static::redirect($lowerUri, 301);
} else {
return NavActions::execute404();
}
2018-05-21 11:49:15 -04:00
} catch (\Routing\HttpMethodNotAllowedException $e) {
Response::setStatus(405);
Response::setHeader('Allow', implode(', ', $e->getAllowedMethods()));
return ['page/405'];
}
2015-10-08 14:08:26 -04:00
}
2016-03-10 21:25:58 -05:00
2019-02-05 16:56:59 -05:00
protected static function performDomainRouting($uri)
2015-10-08 14:08:26 -04:00
{
2018-05-21 11:49:15 -04:00
$subDomain = Request::getSubDomain();
2016-09-05 18:19:12 -04:00
2018-05-21 11:49:15 -04:00
switch ($subDomain) {
2019-02-05 16:56:59 -05:00
case 'chat':
case 'slack':
return static::redirect('https://discord.gg/Z3bERWA');
}
2019-02-05 17:02:28 -05:00
/*
* this is kind of a hack? unsure, so it probably is
*/
2019-02-05 16:56:59 -05:00
$hostName = $_SERVER['HTTP_HOST'];
if ($hostName && in_array($hostName, ['lbry.org', 'lbry.tv'])) {
2019-02-05 17:06:06 -05:00
if ($uri === '/') {
switch ($hostName) {
case 'lbry.org':
return ContentActions::executeOrg();
case 'lbry.tv':
return ContentActions::executeTv();
}
2019-02-05 17:12:00 -05:00
} else {
2019-02-05 16:56:59 -05:00
return static::redirect('/');
}
}
2016-07-04 14:07:52 -04:00
}
2018-05-21 11:49:15 -04:00
protected static function getRouterWithRoutes(): \Routing\RouteCollector
{
$router = new Routing\RouteCollector();
2018-05-21 11:49:15 -04:00
$router->get(['/', 'home'], 'ContentActions::executeHome');
$router->get(['/get', 'get'], 'DownloadActions::executeGet');
$router->get(['/getrubin', 'getrubin'], 'DownloadActions::executeGet');
foreach (array_keys(OS::getAll()) as $os) {
$router->get(['/' . $os, 'get-' . $os], 'DownloadActions::executeGet');
}
$router->get('/roadmap', 'ContentActions::executeRoadmap');
$router->post('/postcommit', 'OpsActions::executePostCommit');
$router->post('/log-upload', 'OpsActions::executeLogUpload');
$router->get(static::CACHE_CLEAR_PATH, 'OpsActions::executeClearCache');
$router->any('/list/subscribe', 'MailActions::executeSubscribe');
$router->any('/list/subscribed', 'MailActions::executeSubscribed');
$router->get('/list/unsubscribe/{email}', 'MailActions::executeUnsubscribe');
2018-10-15 09:16:53 -04:00
$router->any('/list/edit/{token}', 'MailActions::editEmailSettings');
2018-05-21 11:49:15 -04:00
$router->any('/dmca', 'ReportActions::executeDmca');
$router->any('/dmca/{claimid}', 'ReportActions::executeDmcaWithClaimId');
2018-05-21 11:49:15 -04:00
2019-12-07 11:30:39 -05:00
$router->get('/escapeyoutube', 'AcquisitionActions::executeYouTube');
$router->get('/escapeyoutubeiframe', 'AcquisitionActions::executeYouTubeIframe');
2018-10-15 09:16:53 -04:00
$router->post('/youtube/edit', 'AcquisitionActions::executeYoutubeEdit');
$router->post('/youtube/token', 'AcquisitionActions::executeYoutubeToken');
$router->any('/youtube/status/{token}', 'AcquisitionActions::executeYoutubeStatus');
$router->any('/youtube/status', 'AcquisitionActions::executeRedirectYoutube');
$router->any('/youtube', 'AcquisitionActions::executeYouTube');
$router->get('/youtube/{version}', 'AcquisitionActions::executeYouTube');
2018-05-21 11:49:15 -04:00
$router->get('/verify/{token}', 'AcquisitionActions::executeVerify');
2018-12-18 15:40:01 -05:00
$router->get('/verify', 'AcquisitionActions::executeAutoVerify');
2018-05-21 11:49:15 -04:00
2019-08-28 19:00:24 -04:00
$router->get('/follow/{claim}', 'AcquisitionActions::executeFollowCampaign');
2019-09-05 17:31:29 -04:00
$router->get('/i18n/get/{project}/{resource}/{language}.json', 'i18nActions::executeServeTranslationFile');
2018-05-21 11:49:15 -04:00
$router->get('/news/category/{category}', 'ContentActions::executePostCategoryFilter');
2019-09-05 17:31:29 -04:00
$router->post('/i18n/set-culture', 'i18nActions::setCulture');
2018-05-21 11:49:15 -04:00
2019-09-13 15:07:46 -04:00
$router->get('/snapshot/{type}', 'DownloadActions::executeDownloadSnapshot');
2018-05-21 11:49:15 -04:00
$permanentRedirectsPath = ROOT_DIR . '/data/redirect/permanent.yaml';
$tempRedirectsPath = ROOT_DIR . '/data/redirect/temporary.yaml';
$permanentRedirects = SpyC::YAMLLoadString(file_get_contents($permanentRedirectsPath));
$tempRedirects = SpyC::YAMLLoadString(file_get_contents($tempRedirectsPath));
foreach ([307 => $tempRedirects, 301 => $permanentRedirects] as $code => $redirects) {
foreach ($redirects as $src => $target) {
$router->any($src, function () use ($target, $code) {
return static::redirect($target, $code);
});
}
}
2018-12-03 17:31:33 -05:00
$router->get('/releases/{repo:c}.{ext:c}', 'DownloadActions::executeDownloadReleaseAsset');
$router->get('/releases/pre/{repo:c}.{ext:c}', 'DownloadActions::executeDownloadPrereleaseAsset');
2018-05-21 11:49:15 -04:00
$router->get([ContentActions::URL_NEWS . '/{slug:c}?', 'news'], 'ContentActions::executeNews');
$router->get([ContentActions::URL_FAQ . '/{slug:c}?', 'faq'], 'ContentActions::executeFaq');
2019-11-05 15:27:56 -05:00
$router->get(['/bounty/{slug:c}?', 'bounty'], 'ContentActions::executeBountyRedirect');
2018-05-21 11:49:15 -04:00
$router->get(ContentActions::URL_CREDIT_REPORTS, 'ContentActions::executeCreditReports');
$router->get([ContentActions::URL_CREDIT_REPORTS . '/{year:c}-q{quarter:c}', ContentActions::URL_CREDIT_REPORTS . '/{year:c}-Q{quarter:c}'], 'ContentActions::executeCreditReport');
2016-07-25 17:28:06 -04:00
2018-05-21 11:49:15 -04:00
$router->get('/{slug}', function (string $slug) {
2019-03-21 14:28:22 -04:00
if ($slug !== strtolower($slug)) {
return static::redirect('/' . strtolower($slug), 301);
}
2018-05-21 11:49:15 -04:00
if (View::exists('page/' . $slug)) {
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2018-05-21 11:49:15 -04:00
return ['page/' . $slug, []];
} else {
2019-03-21 14:28:22 -04:00
return NavActions::execute404();
2018-05-21 11:49:15 -04:00
}
});
2016-07-04 14:07:52 -04:00
2018-05-21 11:49:15 -04:00
return $router;
2015-10-08 14:08:26 -04:00
}
2018-05-21 11:49:15 -04:00
public static function redirect($url, $statusCode = 302)
2015-10-08 14:08:26 -04:00
{
2018-05-21 11:49:15 -04:00
if (!$url) {
throw new InvalidArgumentException('Cannot redirect to an empty URL.');
}
$url = str_replace('&amp;', '&', $url);
Response::setStatus($statusCode);
if ($statusCode == 201 || ($statusCode >= 300 && $statusCode < 400)) {
Response::setHeader(Response::HEADER_LOCATION, $url);
}
return ['internal/redirect', ['url' => $url]];
2015-10-08 14:08:26 -04:00
}
2018-05-21 11:49:15 -04:00
public static function queueToRunAfterResponse(callable $fn)
2016-09-05 18:32:06 -04:00
{
2018-05-21 11:49:15 -04:00
static::$queuedFunctions[] = $fn;
}
public static function shutdown()
{
while ($fn = array_shift(static::$queuedFunctions)) {
call_user_func($fn);
}
2016-09-05 18:32:06 -04:00
}
2016-03-10 21:25:58 -05:00
}