c5298dd24b
* 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. * Provide basepath support via X-Forwarded headers * Fix warnings * Review adjustments * Provide support X-Forwarded-Host header * Use $uri in view factory directly * Use middleware to set basepath from X-Forwarded-Path header * Fix invalid type hint in RouterPathMiddleware * Add "X-Forwarded-Host" to README
68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* ViewFactory class.
|
|
*/
|
|
|
|
namespace Alltube;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
use Slim\Http\Request;
|
|
use Slim\Views\Smarty;
|
|
use Slim\Views\SmartyPlugins;
|
|
use SmartyException;
|
|
|
|
/**
|
|
* Create Smarty view object.
|
|
*/
|
|
class ViewFactory
|
|
{
|
|
/**
|
|
* Create Smarty view object.
|
|
*
|
|
* @param ContainerInterface $container Slim dependency container
|
|
* @param Request|null $request PSR-7 request
|
|
*
|
|
* @return Smarty
|
|
* @throws SmartyException
|
|
*/
|
|
public static function create(ContainerInterface $container, Request $request = null)
|
|
{
|
|
if (!isset($request)) {
|
|
$request = $container->get('request');
|
|
}
|
|
|
|
$view = new Smarty(__DIR__ . '/../templates/');
|
|
|
|
$uri = $request->getUri();
|
|
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
|
|
$uri = $uri->withScheme('https')->withPort(443);
|
|
}
|
|
|
|
// set values from X-Forwarded-* headers
|
|
$host = current($request->getHeader('X-Forwarded-Host'));
|
|
if ($host) {
|
|
$uri = $uri->withHost($host);
|
|
}
|
|
|
|
$port = current($request->getHeader('X-Forwarded-Port'));
|
|
if ($port) {
|
|
$uri = $uri->withPort(intVal($port));
|
|
}
|
|
|
|
$path = current($request->getHeader('X-Forwarded-Path'));
|
|
if ($path) {
|
|
$uri = $uri->withBasePath($path);
|
|
}
|
|
|
|
/** @var LocaleManager $localeManager */
|
|
$localeManager = $container->get('locale');
|
|
|
|
$smartyPlugins = new SmartyPlugins($container->get('router'), $uri->withUserInfo(null));
|
|
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
|
|
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
|
|
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
|
|
|
|
return $view;
|
|
}
|
|
}
|