Provide basePath support

To be able to serve the application via a reverse proxy in a subfolder
smarty needs to be aware of the basepath if any.
This commit is contained in:
bellington3 2020-09-27 13:59:44 +02:00
parent 9becaeaabe
commit 9d48ec19e7
7 changed files with 67 additions and 17 deletions

View File

@ -133,6 +133,22 @@ class Config
*/
public $debug = false;
/**
* Forward port.
*
* @var int
*/
public $forwardPort = 443;
/**
* Base Path.
* *Must* start with a '/' and *not* end with one.
*
* @var string
*/
public $basePath = '';
/**
* Default to audio.
*
@ -320,4 +336,14 @@ class Config
return $version->getPrettyVersion();
}
/**
* Return the configured base_path usable in smarty.
*
* @return string
*/
public function getBasePath($params, $smarty)
{
return $this->basePath;
}
}

View File

@ -33,8 +33,14 @@ class ViewFactory
}
$view = new Smarty(__DIR__ . '/../templates/');
/** @var Config $config */
$config = $container->get('config');
$scheme = 'http';
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
$request = $request->withUri($request->getUri()->withScheme('https')->withPort(443));
$scheme = 'https';
$forwardPort = $config->forwardPort;
$request = $request->withUri($request->getUri()->withScheme($scheme)->withPort($forwardPort));
}
/** @var LocaleManager $localeManager */
@ -43,6 +49,7 @@ class ViewFactory
$smartyPlugins = new SmartyPlugins($container->get('router'), $request->getUri()->withUserInfo(null));
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('function', 'base_path', [$config, 'getBasePath']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
return $view;

View File

@ -30,8 +30,11 @@ try {
// Locales.
$container['locale'] = LocaleManagerFactory::create();
$app->add(new LocaleMiddleware($container));
$container['config'] = $config;
// Smarty.
$container['view'] = ViewFactory::create($container);
@ -51,37 +54,37 @@ try {
// Routes.
$app->get(
'/',
$config->basePath . '/',
[$frontController, 'index']
)->setName('index');
$app->get(
'/extractors',
$config->basePath . '/extractors',
[$frontController, 'extractors']
)->setName('extractors');
$app->any(
'/info',
$config->basePath . '/info',
[$frontController, 'info']
)->setName('info');
$app->any(
'/watch',
$config->basePath . '/watch',
[$frontController, 'info']
);
$app->any(
'/download',
$config->basePath . '/download',
[$downloadController, 'download']
)->setName('download');
$app->get(
'/locale/{locale}',
$config->basePath . '/locale/{locale}',
[$frontController, 'locale']
)->setName('locale');
$app->get(
'/json',
$config->basePath . '/json',
[$jsonController, 'json']
)->setName('json');

View File

@ -8,19 +8,19 @@
<meta name="twitter:description" content="{$description|escape}"/>
<meta property="og:description" content="{$description|escape}"/>
{/if}
<link rel="stylesheet" href="{base_url}/vendor/webfontkit/open-sans/open-sans.css"/>
<link rel="stylesheet" href="{base_url}/css/style.css"/>
<link rel="stylesheet" href="{base_url}{base_path}/css/fonts.css"/>
<link rel="stylesheet" href="{base_url}{base_path}/css/style.css"/>
<title>{$config->appName}{if isset($title)} - {$title|escape}{/if}</title>
<link rel="canonical" href="{$canonical}"/>
<link rel="icon" href="{base_url}/img/favicon.png"/>
<link rel="icon" href="{base_url}{base_path}/img/favicon.png"/>
<meta property="og:title" content="{$config->appName}{if isset($title)} - {$title|escape}{/if}"/>
<meta property="og:image" content="{base_url}/img/logo.png"/>
<meta property="og:image" content="{base_url}{base_path}/img/logo.png"/>
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="{$config->appName}{if isset($title)} - {$title|escape}{/if}"/>
<meta name="twitter:image" content="{base_url}/img/logo.png"/>
<meta name="twitter:image" content="{base_url}{base_path}/img/logo.png"/>
<meta name="twitter:creator" content="@Tael67"/>
<meta name="theme-color" content="#4F4F4F"/>
<link rel="manifest" href="{base_url}/resources/manifest.json"/>
<link rel="manifest" href="{base_url}{base_path}/resources/manifest.json"/>
<meta name="generator" content="AllTube Download ({$config->getAppVersion()})"/>
</head>
<body class="{$class}">

View File

@ -1,5 +1,5 @@
<h1 class="logobis">
<a class="logocompatible" href="{base_url}">
<span class="logocompatiblemask"><img src="{base_url}/img/logocompatiblemask.png" width="447" height="107"
<a class="logocompatible" href="{base_url}{base_path}">
<span class="logocompatiblemask"><img src="{base_url}{base_path}/img/logocompatiblemask.png" width="447" height="107"
alt="{$config->appName}"/></span>
</a></h1>

View File

@ -1,7 +1,7 @@
{include file='inc/head.tpl'}
{include file='inc/header.tpl'}
<main class="main">
<div><img class="logo" src="{base_url}/img/logo.png"
<div><img class="logo" src="{base_url}{base_path}/img/logo.png"
alt="{$config->appName}" width="328" height="284"></div>
<form action="{path_for name="info"}">
<label class="labelurl" for="url">

View File

@ -40,6 +40,7 @@ class ConfigTest extends BaseTest
$this->assertIsString($config->youtubedl);
$this->assertIsString($config->python);
$this->assertIsString($config->ffmpeg);
$this->assertIsString($config->basePath);
$this->assertIsBool($config->convert);
$this->assertIsBool($config->uglyUrls);
$this->assertIsBool($config->stream);
@ -47,6 +48,7 @@ class ConfigTest extends BaseTest
$this->assertIsBool($config->defaultAudio);
$this->assertIsBool($config->convertSeek);
$this->assertIsInt($config->audioBitrate);
$this->assertIsInt($config->forwardPort);
}
/**
@ -122,4 +124,16 @@ class ConfigTest extends BaseTest
$this->assertEquals(true, $config->convert);
putenv('CONVERT');
}
/**
* Test the getBasePath function.
*
* @return void
*/
public function testGetBasePath()
{
Config::setOptions(['basePath' => '/foo']);
$config = Config::getInstance();
$this->assertEquals('/foo', $config->getBasePath(null, null));
}
}