alltube/index.php

120 lines
2.8 KiB
PHP
Raw Normal View History

2016-08-01 16:04:44 +00:00
<?php
2016-09-07 22:28:28 +00:00
require_once __DIR__ . '/vendor/autoload.php';
use Alltube\Config;
2019-04-22 15:04:59 +00:00
use Alltube\Controller\DownloadController;
2017-01-10 22:39:58 +00:00
use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
2017-05-30 20:20:16 +00:00
use Alltube\LocaleManager;
2017-05-29 19:11:59 +00:00
use Alltube\LocaleMiddleware;
use Alltube\UglyRouter;
use Alltube\ViewFactory;
use Slim\App;
2020-05-13 20:28:05 +00:00
use Slim\Container;
2019-11-27 20:22:23 +00:00
use Symfony\Component\Debug\Debug;
2015-10-29 20:19:40 +00:00
2016-10-26 15:16:17 +00:00
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) {
header('Location: ' . str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI']));
2016-07-27 00:19:29 +00:00
die;
}
if (is_file(__DIR__ . '/config/config.yml')) {
2020-05-13 20:33:02 +00:00
try {
Config::setFile(__DIR__ . '/config/config.yml');
} catch (Exception $e) {
die('Could not load config file: ' . $e->getMessage());
}
}
// Create app.
$app = new App();
2020-05-13 20:28:05 +00:00
/** @var Container $container */
2016-03-29 23:39:47 +00:00
$container = $app->getContainer();
// Load config.
$config = Config::getInstance();
if ($config->uglyUrls) {
$container['router'] = new UglyRouter();
}
2019-11-27 20:22:23 +00:00
if ($config->debug) {
/*
We want to enable this as soon as possible,
in order to catch errors that are thrown
before the Slim error handler is ready.
*/
2019-11-27 20:22:23 +00:00
Debug::enable();
}
// Locales.
if (!class_exists('Locale')) {
die('You need to install the intl extension for PHP.');
}
$container['locale'] = LocaleManager::getInstance();
2017-05-30 20:20:16 +00:00
$app->add(new LocaleMiddleware($container));
2016-03-29 23:39:47 +00:00
// Smarty.
2020-05-13 20:33:02 +00:00
try {
$container['view'] = ViewFactory::create($container);
} catch (SmartyException $e) {
die('Could not load Smarty: ' . $e->getMessage());
}
// Controllers.
$frontController = new FrontController($container);
$jsonController = new JsonController($container);
$downloadController = new DownloadController($container);
2016-04-08 17:06:41 +00:00
// Error handling.
2019-04-26 18:34:34 +00:00
$container['errorHandler'] = [$frontController, 'error'];
2019-12-02 21:04:14 +00:00
$container['phpErrorHandler'] = [$frontController, 'error'];
2016-04-30 23:25:08 +00:00
// Routes.
2015-10-29 20:23:02 +00:00
$app->get(
'/',
[$frontController, 'index']
2016-06-09 19:15:44 +00:00
)->setName('index');
2015-10-29 20:23:02 +00:00
$app->get(
'/extractors',
[$frontController, 'extractors']
2016-03-29 23:39:47 +00:00
)->setName('extractors');
$app->any(
'/info',
[$frontController, 'info']
)->setName('info');
// Legacy route.
$app->any('/video', [$frontController, 'info']);
$app->any(
'/watch',
2019-10-19 20:06:30 +00:00
[$frontController, 'info']
);
$app->any(
'/download',
[$downloadController, 'download']
)->setName('download');
// Legacy route.
$app->get('/redirect', [$downloadController, 'download']);
2017-05-30 20:20:16 +00:00
$app->get(
'/locale/{locale}',
[$frontController, 'locale']
2017-05-30 20:20:16 +00:00
)->setName('locale');
$app->get(
'/json',
[$jsonController, 'json']
)->setName('json');
try {
$app->run();
} catch (SmartyException $e) {
die('Smarty could not compile the template file: ' . $e->getMessage());
2020-05-13 20:33:02 +00:00
} catch (Throwable $e) {
// Last resort if the error has not been caught by the error handler for some reason.
die('Error when starting the app: ' . $e->getMessage());
}