fork download
  1. <?php
  2. namespace Vendor\RmedRealurlConfig\Hooks;
  3.  
  4.  
  5. use DmitryDulepov\Realurl\Encoder\UrlEncoder;
  6. use TYPO3\CMS\Core\Error\Exception;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Core\Utility\HttpUtility;
  9. use TYPO3\CMS\Core\Utility\MathUtility;
  10. use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
  11. use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
  12. use TYPO3\CMS\Frontend\Page\PageRepository;
  13.  
  14.  
  15. /**
  16.  * @Notice This is an individual redirect for clubwien.at.
  17.  * You can find the method buildUrl where you can change the output individual
  18.  *
  19.  */
  20. class UrlRedirect
  21. {
  22. /**
  23.   * @var $pageId
  24.   */
  25. protected $pageId;
  26.  
  27. /**
  28.   * cObject to generate links
  29.   *
  30.   * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
  31.   * @inject
  32.   */
  33. protected $cObj;
  34.  
  35. /**
  36.   * @var $pageUid
  37.   */
  38. protected $pageUid;
  39.  
  40. /**
  41.   * @var $rootLine
  42.   */
  43. protected $rootLine;
  44.  
  45. /**
  46.   * @var $params
  47.   */
  48. protected $params;
  49.  
  50. /**
  51.   * @var array $sessionParams
  52.   */
  53. private $sessionParams = [
  54. 'type' => 'ses',
  55. 'key' => 'lastVisit'
  56. ];
  57.  
  58.  
  59. public function __construct()
  60. {
  61. $this->cObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
  62. $this->cObj->start(array());
  63. }
  64.  
  65.  
  66. /**
  67.   * @param array $params
  68.   */
  69. public function redirect(array $params)
  70. {
  71. /** @var UrlEncoder $encoder */
  72. try {
  73. $headerCode = HttpUtility::HTTP_STATUS_301;
  74. $this->params = $params;
  75. $pObj = $params['params']['pObj'];
  76. $uri = $pObj->siteScript;
  77.  
  78. if(strpos($uri, 'html') !== false) {
  79.  
  80. // @todo add a hook for change the expressionx
  81. preg_match("/[-\w]+\.([0-9]+)\.0\.html$/", $uri, $match);
  82.  
  83. if (isset($match[1])) {
  84. $uid = intval($match[1]);
  85. $this->setPageUid($uid);
  86.  
  87. // sets rootLine if empty it throws page not found error
  88. if(!$this->rootLine = $this->getRootLine())
  89. {
  90. $errorMessage = '404 Page Not Found!';
  91. return $this->throw404($errorMessage);
  92. }
  93.  
  94. $this->initTSFE();
  95.  
  96. // 1: page, 2: category, 3: category page, 4: page
  97. if($this->hasParentPage()) {
  98. $uri = $this->buildUri();
  99. HttpUtility::redirect($uri, $headerCode);
  100. }
  101. }
  102. }
  103. } catch (Exception $e) {
  104. $errorMessage = $e->getMessage();
  105. $this->TypoScriptFrontendController()->pageNotFoundAndExit($errorMessage);
  106. }
  107. }
  108.  
  109.  
  110. /**
  111.   * @param int $nodeLevel
  112.   * @return mixed|string
  113.   */
  114. protected function buildUri($nodeLevel = 3)
  115. {
  116. if($this->countRootLine() >= $nodeLevel) {
  117. $this->params['URL'] = $this->getPageLink();
  118. return $this->params['URL'];
  119. }
  120. }
  121.  
  122.  
  123. /**
  124.   * Gets the Root
  125.   *
  126.   * @return array
  127.   */
  128. protected function getRootLine()
  129. {
  130. $uid = $this->getPageUid();
  131. if($this->getPageByUID()) {
  132. return $this->PageRepository()->getRootLine($uid);
  133. }
  134.  
  135. return null;
  136. }
  137.  
  138.  
  139. /**
  140.   * @return array
  141.   */
  142. protected function getPageByUID()
  143. {
  144. $uid = $this->getPageUid();
  145. return $this->PageRepository()->getPage($uid);
  146. }
  147.  
  148.  
  149. /**
  150.   * @return bool
  151.   */
  152. protected function hasParentPage()
  153. {
  154. $hasParent = false;
  155. if(!empty($this->getCurrentPage('pid'))) {
  156. $hasParent = true;
  157. }
  158. return $hasParent;
  159. }
  160.  
  161.  
  162. /**
  163.   * This is just a workaround to build a Speaking URL
  164.   *
  165.   * @param null $key
  166.   * @return mixed
  167.   */
  168. protected function getCurrentPage($key = null)
  169. {
  170. $pObj = null;
  171. $currentKey = ($this->countRootLine()-1);
  172. if(isset($this->rootLine[$currentKey])) {
  173. $pObj = $this->rootLine[$currentKey];
  174. if($key) {
  175. $pObj = $pObj[$key];
  176. }
  177. }
  178. return $pObj;
  179. }
  180.  
  181. /**
  182.   * Generates the link to a single page
  183.   *
  184.   * @return string Full URL of the page including host name (escaped)
  185.   */
  186. protected function getPageLink()
  187. {
  188. $pageId = $this->getPageUid();
  189. # old conf for typoLink method from ContentObjectRenderer
  190. $conf = array(
  191. 'useCacheHash' => true,
  192. 'parameter' => $pageId,
  193. 'returnLast' => 'url',
  194. 'additionalParams' => '&tx_rmedpage_showcase[controller]=News'.
  195. '&tx_rmedpage_showcase[action]=detail'
  196. );
  197.  
  198. /** @var ContentObjectRenderer cObj */
  199. $this->cObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
  200. $this->cObj->start([]);
  201.  
  202. $uri = htmlspecialchars($this->cObj->typoLink('', $conf));
  203. return GeneralUtility::locationHeaderUrl($uri);
  204. }
  205.  
  206.  
  207. // unused
  208. protected function initTSFE($typeNum = 0)
  209. {
  210. \TYPO3\CMS\Frontend\Utility\EidUtility::initTCA();
  211. if (!is_object($GLOBALS['TT'])) {
  212. $GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
  213. $GLOBALS['TT']->start();
  214. }
  215.  
  216. $uid = $this->getPageUid();
  217. $GLOBALS['TSFE']->id = $uid;
  218.  
  219. /** @var TypoScriptFrontendController $GLOBALS['TSFE'] */
  220. $GLOBALS['TSFE'] = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', $GLOBALS['TYPO3_CONF_VARS'], $uid, $typeNum);
  221.  
  222. /** @var GeneralUtility sys_page */
  223. $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
  224. $GLOBALS['TSFE']->sys_page->init(true);
  225.  
  226. $GLOBALS['TSFE']->connectToDB();
  227. $GLOBALS['TSFE']->initFEuser();
  228. $GLOBALS['TSFE']->determineId();
  229. $GLOBALS['TSFE']->initTemplate();
  230.  
  231. $GLOBALS['TSFE']->rootLine = $this->rootLine;
  232. $GLOBALS['TSFE']->getConfigArray();
  233.  
  234. if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('realurl')) {
  235. $rootline = \TYPO3\CMS\Backend\Utility\BackendUtility::BEgetRootLine($uid);
  236. $host = \TYPO3\CMS\Backend\Utility\BackendUtility::firstDomainRecord($rootline);
  237. $_SERVER['HTTP_HOST'] = $host;
  238. }
  239. }
  240.  
  241.  
  242. /**
  243.   * Throws a 404 error with the corresponding message.
  244.   *
  245.   * @param string $errorMessage
  246.   * @return void
  247.   */
  248. protected function throw404($errorMessage)
  249. {
  250. // Set language to allow localized error pages
  251. if (MathUtility::canBeInterpretedAsInteger($this->detectedLanguageId)) {
  252. $_GET['L'] = $this->detectedLanguageId;
  253. }
  254. $this->TypoScriptFrontendController()->pageNotFoundAndExit($errorMessage);
  255. }
  256.  
  257.  
  258. /**
  259.   * @return int
  260.   */
  261. protected function countRootLine()
  262. {
  263. $count = count($this->rootLine);
  264. return $count;
  265. }
  266.  
  267.  
  268. /**
  269.   * @return ContentObjectRenderer object
  270.   */
  271. protected function getCObj()
  272. {
  273. return GeneralUtility::makeInstance(ContentObjectRenderer::class);
  274. }
  275.  
  276.  
  277. /**
  278.   * @return PageRepository object
  279.   */
  280. protected function PageRepository()
  281. {
  282. return GeneralUtility::makeInstance(PageRepository::class);
  283. }
  284.  
  285.  
  286. /**
  287.   * @return TypoScriptFrontendController object
  288.   */
  289. protected function TypoScriptFrontendController()
  290. {
  291. return $GLOBALS['TSFE'];
  292. }
  293.  
  294.  
  295. /**
  296.   * @return mixed
  297.   */
  298. public function getPageUid()
  299. {
  300. return $this->pageUid;
  301. }
  302.  
  303.  
  304. /**
  305.   * @param mixed $pageUid
  306.   * @throws Exception
  307.   */
  308. public function setPageUid($pageUid)
  309. {
  310. if(MathUtility::canBeInterpretedAsInteger($pageUid)) {
  311. $this->pageUid = intval($pageUid);
  312. return;
  313. }
  314.  
  315. throw new Exception('The input can not be interpreted as integer');
  316. }
  317. }
Success #stdin #stdout 0.01s 82560KB
stdin
Standard input is empty
stdout
Standard output is empty