1
0
mirror of https://github.com/typemill/typemill.git synced 2025-04-21 04:21:56 +02:00

moved assets to middleware for proxy detection

This commit is contained in:
trendschau 2020-10-18 08:50:58 +02:00
parent 1e6c42c98a
commit a3167a26ee
15 changed files with 183 additions and 83 deletions

View File

@ -2,8 +2,6 @@
Congratulations! If you see this page, then the setup of the system has worked successfully!! You can now login (/tm/login) and configure your system, your themes and your plugins. You will find all settings in the settings-area (/tm/settings).
## If it does not work
If you face any problems during the installation, then please make sure, that your system supports these features:
- PHP version 7+.

View File

@ -6,7 +6,7 @@ use Typemill\Models\ProcessImage;
class Assets
{
protected $baseUrl;
public $baseUrl;
public function __construct($baseUrl)
{
@ -22,6 +22,16 @@ class Assets
$this->imageFolder = 'original';
}
public function setUri($uri)
{
$this->uri = $uri;
}
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}
public function image($url)
{
$this->imageUrl = $url;
@ -151,6 +161,8 @@ class Assets
{
$this->JS[] = '<script src="' . $JSfile . '"></script>';
}
# print_r($this->JS);
}
public function addInlineJS($JS)

View File

@ -39,6 +39,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -72,7 +75,7 @@ class ArticleApiController extends ContentController
if(is_array($this->content))
{
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# turn markdown into an array of markdown-blocks
$this->content = $parsedown->arrayBlocksToMarkdown($this->content);
@ -125,6 +128,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -151,7 +157,7 @@ class ArticleApiController extends ContentController
if(!$this->setContent()){ return $response->withJson($this->errors, 404); }
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# turn markdown into an array of markdown-blocks
$contentArray = $parsedown->markdownToArrayBlocks($this->content);
@ -219,6 +225,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -237,7 +246,7 @@ class ArticleApiController extends ContentController
# set redirect url to edit page
$url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'];
if(isset($this->item->urlRelWoF))
if(isset($this->item->urlRelWoF) && $this->item->urlRelWoF != '/' )
{
$url = $url . $this->item->urlRelWoF;
}
@ -276,6 +285,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -351,6 +363,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -371,7 +386,7 @@ class ArticleApiController extends ContentController
$updatedContent = '# ' . $this->params['title'] . "\r\n\r\n" . $this->params['content'];
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# turn markdown into an array of markdown-blocks
$contentArray = $parsedown->markdownToArrayBlocks($updatedContent);
@ -829,7 +844,7 @@ class ArticleApiController extends ContentController
if(!$this->setStructure($draft = true, $cache = false)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
# set information for homepage
$this->setHomepage();
$this->setHomepage($args = false);
# get item for url and set it active again
if(isset($this->params['url']))
@ -854,6 +869,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
# set information for homepage
$this->setHomepage($args = false);
/* set item */
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -888,7 +906,7 @@ class ArticleApiController extends ContentController
if(!is_array($content))
{
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# turn markdown into an array of markdown-blocks
$content = $parsedown->markdownToArrayBlocks($content);
@ -916,6 +934,9 @@ class ArticleApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
# set information for homepage
$this->setHomepage($args = false);
/* set item */
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -947,7 +968,7 @@ class ArticleApiController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# fix footnotes in parsedown, might break with complicated footnotes
$parsedown->setVisualMode();

View File

@ -33,6 +33,8 @@ class BlockApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
$this->setHomepage($args = false);
/* set item */
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -72,7 +74,7 @@ class BlockApiController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# if content is not an array, then transform it
if(!is_array($pageMarkdown))
@ -170,7 +172,7 @@ class BlockApiController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# if content is not an array, then transform it
if(!is_array($content))
@ -229,6 +231,8 @@ class BlockApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
$this->setHomepage($args = false);
/* set item */
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -268,7 +272,7 @@ class BlockApiController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
$parsedown->setVisualMode();
# if content is not an array, then transform it
@ -384,6 +388,8 @@ class BlockApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -415,7 +421,7 @@ class BlockApiController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# if content is not an array, then transform it
if(!is_array($pageMarkdown))
@ -490,6 +496,8 @@ class BlockApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -521,7 +529,7 @@ class BlockApiController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# if content is not an array, then transform it
if(!is_array($this->content))

View File

@ -28,7 +28,7 @@ class ContentBackendController extends ContentController
if(!$this->setStructure($draft = true)){ return $this->renderIntern404($response, array( 'navigation' => true, 'content' => $this->errors )); }
# set information for homepage
$this->setHomepage();
$this->setHomepage($args);
# set item
if(!$this->setItem()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
@ -44,7 +44,7 @@ class ContentBackendController extends ContentController
# set path
$this->setItemPath($this->item->fileType);
# add the modified date for the file
$this->item->modified = ($this->item->published OR $this->item->drafted) ? filemtime($this->settings['contentFolder'] . $this->path) : false;
@ -58,7 +58,7 @@ class ContentBackendController extends ContentController
if(is_array($content))
{
# transform array to markdown
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
$content = $parsedown->arrayBlocksToMarkdown($content);
}
@ -108,7 +108,7 @@ class ContentBackendController extends ContentController
if(!$this->setStructure($draft = true)){ return $this->renderIntern404($response, array( 'navigation' => true, 'content' => $this->errors )); }
# set information for homepage
$this->setHomepage();
$this->setHomepage($args);
# set item
if(!$this->setItem()){ return $this->renderIntern404($response, array( 'navigation' => $this->structure, 'settings' => $this->settings, 'content' => $this->errors )); }
@ -136,7 +136,7 @@ class ContentBackendController extends ContentController
}
# initialize parsedown extension
$parsedown = new ParsedownExtension();
$parsedown = new ParsedownExtension($this->uri->getBaseUrl());
# to fix footnote-logic in parsedown, set visual mode to true
$parsedown->setVisualMode();

View File

@ -62,6 +62,8 @@ abstract class ContentController
$this->settings = $this->c->get('settings');
$this->structureLiveName = 'structure.txt';
$this->structureDraftName = 'structure-draft.txt';
$this->c->dispatcher->dispatch('onTwigLoaded');
}
# admin ui rendering
@ -275,7 +277,8 @@ abstract class ContentController
}
}
protected function setHomepage()
# this is only set by content backend controller
protected function setHomepage($args)
{
$contentFolder = Folder::scanFolderFlat($this->settings['rootPath'] . $this->settings['contentFolder']);
@ -295,7 +298,7 @@ abstract class ContentController
}
$active = false;
if($this->params['url'] == '/' || $this->params['url'] == $this->uri->getBasePath() )
if($this->params['url'] == '/' || (is_array($args) && empty($args)))
{
$active = 'active';
}
@ -305,8 +308,11 @@ abstract class ContentController
protected function setItem()
{
# home is only set by backend controller, not by api calls
$home = isset($this->homepage['active']) ? $this->homepage['active'] : false;
# search for the url in the structure
$item = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBasePath());
$item = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl(), NULL, $home);
if($item)
{

View File

@ -15,7 +15,8 @@ abstract class Controller
public function __construct(ContainerInterface $c)
{
$this->c = $c;
$this->c = $c;
$this->c->dispatcher->dispatch('onTwigLoaded');
}
# frontend rendering
@ -23,7 +24,7 @@ abstract class Controller
{
# why commented this out??
$data = $this->c->dispatcher->dispatch('onPageReady', new OnPageReady($data))->getData();
if(isset($_SESSION['old']))
{
unset($_SESSION['old']);

View File

@ -72,6 +72,9 @@ class MetaApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args = false);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
@ -153,6 +156,9 @@ class MetaApiController extends ContentController
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set information for homepage
$this->setHomepage($args);
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }

View File

@ -106,10 +106,11 @@ class PageController extends Controller
}
# if the user is on startpage
$home = false;
if(empty($args))
{
$home = true;
$item = Folder::getItemForUrl($navigation, $uri->getBasePath(), $uri->getBasePath());
$item = Folder::getItemForUrl($navigation, $uri->getBasePath(), $uri->getBaseUrl(), NULL, $home);
$urlRel = $uri->getBasePath();
}
else
@ -178,7 +179,7 @@ class PageController extends Controller
if(isset($item->hide) && !$item->hide)
{
# use the navigation instead of the structure so that hidden elements are erased
$item = Folder::getItemForUrl($navigation, $urlRel, $uri->getBasePath());
$item = Folder::getItemForUrl($navigation, $urlRel, $uri->getBaseUrl(), NULL, $home);
}
}
@ -200,7 +201,7 @@ class PageController extends Controller
$itemUrl = isset($item->urlRel) ? $item->urlRel : false;
/* initialize parsedown */
$parsedown = new ParsedownExtension($settings['headlineanchors']);
$parsedown = new ParsedownExtension($base_url, $settings['headlineanchors']);
/* set safe mode to escape javascript and html in markdown */
$parsedown->setSafeMode(true);

View File

@ -6,7 +6,7 @@ use \URLify;
class ParsedownExtension extends \ParsedownExtra
{
function __construct($showAnchor = NULL, $skipAbsoluteUrls = NULL)
function __construct($baseUrl = '', $showAnchor = NULL)
{
parent::__construct();
@ -14,7 +14,7 @@ class ParsedownExtension extends \ParsedownExtra
$this->showAnchor = $showAnchor;
# base url is needed for media/images and relative links (e.g. if www.mydomain.com/mywebsite)
$this->baseUrl = $skipAbsoluteUrls ? '' : TM_BASE_URL;
$this->baseUrl = $baseUrl;
# math support
$this->BlockTypes['\\'][] = 'Math';

View File

@ -0,0 +1,40 @@
<?php
namespace Typemill\Middleware;
use Slim\Views\Twig;
use Slim\Http\Request;
use Slim\Http\Response;
class assetMiddleware
{
protected $view;
protected $c;
public function __construct($container)
{
# $this->view = $view;
$this->container = $container;
}
public function __invoke(Request $request, Response $response, $next)
{
# get the uri after proxy detection
$uri = $request->getUri()->withUserInfo('');
# update the asset object in the container (for plugins) with the new url
$this->container->assets->setBaseUrl($uri->getBaseUrl());
# add the asset object to twig-frontend for themes
$this->container['view']->getEnvironment()->addGlobal('assets', $this->container['assets']);
# use {{ base_url() }} in twig templates
$this->container['view']['base_url'] = $uri->getBaseUrl();
$this->container['view']['current_url'] = $uri->getPath();
$response = $next($request, $response);
return $response;
}
}

View File

@ -267,11 +267,11 @@ class Folder
}
}
public static function getItemForUrl($folderContentDetails, $url, $baseUrl, $result = NULL)
public static function getItemForUrl($folderContentDetails, $url, $baseUrl, $result = NULL, $home = NULL )
{
# if we are on the homepage
if($url == '/' OR $url == $baseUrl)
if($home)
{
# return a standard item-object
$item = new \stdClass;

View File

@ -49,7 +49,7 @@ abstract class Plugin implements EventSubscriberInterface
->withHeader("Content-Type", "application/json")
->withStatus(400)
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
}
protected function getSettings()
{

View File

@ -42,23 +42,19 @@ if($settings['settings']['displayErrorDetails'])
$app = new \Slim\App($settings);
/************************
* ADD PROXY DETECTION *
************************/
if(isset($settings['settings']['proxy']) && $settings['settings']['proxy'])
{
$trustedProxies = ( isset($settings['settings']['trustedproxies']) && !empty($settings['settings']['trustedproxies']) ) ? explode(",", $settings['settings']['trustedproxies']) : [];
$app->add(new RKA\Middleware\ProxyDetection($trustedProxies));
}
/************************
* GET SLIM CONTAINER *
************************/
$container = $app->getContainer();
/************************
* Create URI *
************************/
# get uri and delete username and password from uri
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER))->withUserInfo('');
/************************
* LOAD & UPDATE PLUGINS *
************************/
@ -157,20 +153,6 @@ $container['dispatcher'] = function($container) use ($dispatcher)
return $dispatcher;
};
# delete username and password from uri
$uri = $container['request']->getUri()->withUserInfo('');
define("TM_BASE_URL", $uri->getBaseUrl());
/********************************
* ADD ASSET-FUNCTION FOR TWIG *
********************************/
$container['assets'] = function($c) use ($uri)
{
return new \Typemill\Assets($uri->getBaseUrl());
};
/************************
* DECIDE FOR SESSION *
************************/
@ -181,20 +163,32 @@ $session_segments = array('setup', 'tm/', 'api/', '/setup', '/tm/', '/api/');
$client_segments = $dispatcher->dispatch('onSessionSegmentsLoaded', new OnSessionSegmentsLoaded([]))->getData();
$session_segments = array_merge($session_segments, $client_segments);
$path = $uri->getPath();
$container['flash'] = false;
$container['csrf'] = false;
/************************************
* ADD ASSET-FUNCTION FOR PLUGINS *
************************************/
$container['assets'] = function($c) use ($uri)
{
return new \Typemill\Assets($uri->getBaseUrl());
};
/********************************
* MOVE TO MIDDLEWARE NEXT TIME *
********************************/
# if website is restricted to registered user
if(isset($settings['settings']['access']) && $settings['settings']['access'] == 'registered')
{
# activate session for all routes
$session_segments = [$path];
$session_segments = [$uri->getPath()];
}
foreach($session_segments as $segment)
{
if(substr( $path, 0, strlen($segment) ) === $segment)
if(substr( $uri->getPath(), 0, strlen($segment) ) === $segment)
{
// configure session
ini_set('session.cookie_httponly', 1 );
@ -245,9 +239,13 @@ $container['view'] = function ($container) use ($uri)
'debug' => true
]);
// Instantiate and add Slim specific extension
$basePath = rtrim(str_ireplace('index.php', '', $uri->getBasePath()), '/');
$view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
# Instantiate and add Slim specific extension
$router = $container->get('router');
# $basePath = rtrim(str_ireplace('index.php', '', $uri->getBasePath()), '/');
# $view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
$view->addExtension(new Twig_Extension_Debug());
$view->addExtension(new Typemill\Extensions\TwigUserExtension());
$view->addExtension(new Typemill\Extensions\TwigMarkdownExtension());
@ -255,34 +253,27 @@ $container['view'] = function ($container) use ($uri)
$view->addExtension(new Typemill\Extensions\TwigPagelistExtension());
# use {{ base_url() }} in twig templates
$view['base_url'] = $uri->getBaseUrl();
$view['current_url'] = $uri->getPath();
# $view['base_url'] = $uri->getBaseUrl();
# $view['current_url'] = $uri->getPath();
/* if session route, add flash messages and csrf-protection */
# if session route, add flash messages and csrf-protection
if($container['flash'])
{
$view->getEnvironment()->addGlobal('flash', $container->flash);
$view->addExtension(new Typemill\Extensions\TwigCsrfExtension($container['csrf']));
}
/* add asset-function to all views */
$view->getEnvironment()->addGlobal('assets', $container->assets);
/******************************
* LOAD TRANSLATIONS *
******************************/
$uri = $_SERVER['REQUEST_URI'];
$base_path = $container['request']->getUri()->getBasePath();
$uri = str_replace($base_path,'',$uri);
$pieces = explode('/',$uri);
if(isset($uri) && ($pieces[1] === 'tm' OR $pieces[1] === 'setup') )
$pieces = explode('/',$uri->getPath());
if( ($pieces[0] === 'tm' OR $pieces[0] === 'setup') )
{
// Admin environment labels
# Admin environment labels
$labels = Typemill\Translations::loadTranslations('admin');
} else {
// User environment labels
// For now it is useless, but it will prove useful in the future
# User environment labels
# For now it is useless, but it will prove useful in the future
$labels = Typemill\Translations::loadTranslations('user');
}
$view['translations'] = $labels;
@ -291,7 +282,7 @@ $container['view'] = function ($container) use ($uri)
return $view;
};
$container->dispatcher->dispatch('onTwigLoaded');
# $container->dispatcher->dispatch('onTwigLoaded');
/***************************
* ADD NOT FOUND HANDLER *
@ -323,6 +314,22 @@ if($container['flash'])
$app->add($container->get('csrf'));
}
/********************************
* ASSET MIDDLEWARE FOR TWIG *
********************************/
$app->add(new \Typemill\Middleware\assetMiddleware($container));
/********************************
* PROXY DETECTION FOR REQUEST *
********************************/
if(isset($settings['settings']['proxy']) && $settings['settings']['proxy'])
{
$trustedProxies = ( isset($settings['settings']['trustedproxies']) && !empty($settings['settings']['trustedproxies']) ) ? explode(",", $settings['settings']['trustedproxies']) : [];
$app->add(new RKA\Middleware\ProxyDetection($trustedProxies));
}
/************************
* ADD ROUTES *
************************/

View File

@ -17,7 +17,7 @@
<li class="db list tl pa0 w-100 mh0 mv2 mh2-l shadow-2-hover">
<a class="link mv4 dim" href="{{ element.urlAbs }}">
<div class="db h4-5 overflow-hidden">
<img class="db w-100" src="{{ page.meta.heroimage }}" alt="{{ page.meta.heroimagealt }}">
<img class="db w-100" src="{{base_url}}/{{ page.meta.heroimage }}" alt="{{ page.meta.heroimagealt }}">
</div>
<div class="pa2 ph3-ns pb3-ns">
<h3>{{ page.meta.title }}</h3>
@ -43,7 +43,7 @@
<a class="link mv4 dim" href="{{ element.urlAbs }}">
<div class="db h4-5 overflow-hidden">
<img class="db w-100" src="{{ post.meta.heroimage }}" alt="{{ post.meta.heroimagealt }}">
<img class="db w-100" src="{{base_url}}/{{ post.meta.heroimage }}" alt="{{ post.meta.heroimagealt }}">
</div>
<div class="pa2 ph3-ns pb3-ns">
<h3>{{ post.meta.title }}</h3>