Custom Router pri https generuje port v url

duskohu
Člen | 778
+
0
-

Ahojte mam custom router, ked som presiel na https na jednom hostingu, predpokladam ze maju nejako riesene proxy tak mi zrazu generuje rutu takto: https://test.example.com:80/some-page, neviete mi poradit ako to riesit? Predpokladam ze je problem v constructUrl
Na innom hostingu nemam ten problem.
Zatial som to vyriesil tak ze do index.php som pridal toto, ale vobec sa mi to nepaci, chcel by som nejako fixnut ten router.

// hack for https proxies
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
    if ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' && isset($_SERVER['SERVER_PORT']) && in_array($_SERVER['SERVER_PORT'],
            [80, 82])) { // https over proxy
        $_SERVER['HTTPS'] = 'On';
        $_SERVER['SERVER_PORT'] = 443;
    } elseif ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'http' && isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 80) { // http over proxy
        exit;
        $_SERVER['HTTPS'] = 'Off';
        $_SERVER['SERVER_PORT'] = 80;
    }
}
<?php

namespace Nas\CmsModule\Router;

use Kdyby\Doctrine\EntityManager;
use Nas\CmsModule\Model\Route;
use NasExt\Framework\Application\Router\BaseRouter;
use NasExt\Framework\Application\Router\RouterConfiguration;
use NasExt\Framework\Application\Translator\ILanguageStorage;
use NasExt\Framework\Application\Translator\Language;
use Nette\Application\IRouter;
use Nette\Application\Request;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nette\Http\IRequest;
use Nette\Http\Url;
use Nette\Utils\Strings;

class CmsRouter implements IRouter
{

    const CACHE_NAMESPACE = 'Nas.CmsModule.Router.CmsRouter';
    const URL_SEPARATOR = '/';
    const SLUG_SEPARATOR = '-';

    /** @var EntityManager */
    private $em;

    /** @var Cache */
    private $cache;

    /** @var  ILanguageStorage */
    private $languageStorage;

    /** @var RouterConfiguration */
    private $routerConfiguration;

    /**
     * @param EntityManager $em
     * @param IStorage $cacheStorage
     * @param ILanguageStorage $languageStorage
     * @param RouterConfiguration $routerConfiguration
     */
    public function __construct(
        EntityManager $em,
        IStorage $cacheStorage,
        ILanguageStorage $languageStorage,
        RouterConfiguration $routerConfiguration
    ) {
        $this->em = $em;
        $this->cache = new Cache($cacheStorage, self::CACHE_NAMESPACE);
        $this->languageStorage = $languageStorage;
        $this->routerConfiguration = $routerConfiguration;
    }

    /**
     * @param IRequest $httpRequest
     * @return Request|null
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function match(IRequest $httpRequest)
    {
        $url = $httpRequest->getUrl()->getPathInfo();
        $url = rtrim($url, self::URL_SEPARATOR);

        // process locale, root, and url
        /** @var Language $languageEntity */
        $languageEntity = $this->languageStorage->getDefaultFrontLanguage();
        $locales = $this->languageStorage->getFrontLanguages();

        // Prepare lang from url
        //'~^([A-Za-z]{2})(?:[\/|?|#]|$)~'
        $matches = Strings::match($url, '~^([A-Za-z_]+)(?:[\/|?|#]|$)~');
        if ($matches && isset($matches[1])) {

            foreach ($locales as $locale) {
                if ($matches[1] === $locale->getCode()) {
                    $languageEntity = $locale;
                    $url = substr($url, strlen($matches[1]) + 1);
                }
            }
        }

        /** @var Route $routeEntity */
        $routeEntity = $this->em->getRepository(Route::class)
            ->createQueryBuilder('route')
            ->select('route')
            ->andWhere('route.url = :url')
            ->andWhere('route.language = :language')
            ->setParameter('url', self::URL_SEPARATOR . $url)
            ->setParameter('language', $languageEntity ? $languageEntity->getCode() : $languageEntity)
            ->getQuery()
            ->getOneOrNullResult();

        if (!$routeEntity) {
            return null;
        }

        $pos = strrpos($routeEntity->getDestination(), ':');
        $parameters = $httpRequest->getQuery();
        $parameters['action'] = substr($routeEntity->getDestination(), $pos + 1);
        $destination = substr($routeEntity->getDestination(), 0, $pos);
        $parameters['id'] = $routeEntity->getId();
        $parameters[BaseRouter::LOCALE_PARAM] = $languageEntity ? $languageEntity->getCode() : $languageEntity;

        return new Request(
            $destination,
            $httpRequest->getMethod(),
            $parameters,
            $httpRequest->getPost(),
            $httpRequest->getFiles(),
            array(Request::SECURED => $httpRequest->isSecured())
        );
    }

    /**
     * @param Request $appRequest
     * @param Url string $refUrl
     * @return null|string
     */
    function constructUrl(Request $appRequest, Url $refUrl)
    {
        $parameters = $appRequest->getParameters();
        if (!isset($parameters['action'])) {
            return null;
        }


        if (isset($parameters['routeEntity']) && $parameters['routeEntity'] instanceof Route) {
            /** @var Route $routeEntity */
            $routeEntity = $parameters['routeEntity'];

            $parameters['id'] = $routeEntity->getId();
            $data['url'] = ltrim($routeEntity->getUrl(), self::URL_SEPARATOR);
            $data[BaseRouter::LOCALE_PARAM] = $routeEntity->getLanguage();
        } else {
            $key = $appRequest->getPresenterName() . ':' . $parameters['action'] . (isset($parameters['id']) ? '-' . $parameters['id'] : '');

            $data = $this->cache->load(
                $key, function () use ($appRequest, $parameters) {

                /** @var Route $routeEntity */
                $routeEntity = $this->em->getRepository(Route::class)
                    ->createQueryBuilder('route')
                    ->select('route')
                    ->andWhere('route.destination = :destination')
                    ->setParameter('destination', $appRequest->getPresenterName() . ':' . $parameters['action'])
                    ->andWhere('route.id = :routeId')
                    ->setParameter('routeId', isset($parameters['id']) ? $parameters['id'] : null)
                    ->setMaxResults(1)
                    ->getQuery()
                    ->getOneOrNullResult();

                if ($routeEntity) {
                    return array(
                        'url' => ltrim($routeEntity->getUrl(), self::URL_SEPARATOR),
                        BaseRouter::LOCALE_PARAM => $routeEntity->getLanguage(),
                    );
                }
                return false;
            }
            );
        }

        if ($data === false) {
            return null;
        }

        $url = $data['url'];

        if ($this->routerConfiguration->isUseFrontLanguageRoutePrefix() == true || $data[BaseRouter::LOCALE_PARAM] !== $this->languageStorage->getDefaultFrontLanguage()->getCode()) {
            $url = !empty($url) ? $data[BaseRouter::LOCALE_PARAM] . self::URL_SEPARATOR . $url : $data[BaseRouter::LOCALE_PARAM];
        }

        unset($parameters['action'], $parameters['id'], $parameters[BaseRouter::LOCALE_PARAM], $parameters['routeEntity']);

        $url = new Url($refUrl->getBaseUrl() . $url);
        $url->setQuery($parameters);
        return $url->getAbsoluteUrl();
    }
}

Editoval duskohu (16. 4. 2020 14:27)

CZechBoY
Člen | 3608
+
0
-

Sice dokumentace není moc ukecaná, ale mělo by to stačit: https://doc.nette.org/cs/configuring#…
Pro zjištění hodnoty kterou bys měl nastavit si asi vydumpuj co ti chodí za IP teď.

duskohu
Člen | 778
+
0
-

CZechBoY napsal(a):

Sice dokumentace není moc ukecaná, ale mělo by to stačit: https://doc.nette.org/cs/configuring#…
Pro zjištění hodnoty kterou bys měl nastavit si asi vydumpuj co ti chodí za IP teď.

@CZechBoY dakujem velmi pekne,skusim, len divne je ze default router ktory ma nette, ten mi generuje url bez problemov. Stale mam pocit ze ten moj ruter nieco zanedbava.