Initial commit

This commit is contained in:
Björn Fromme
2018-04-18 17:24:00 +02:00
commit da4db35be3
875 changed files with 80368 additions and 0 deletions
@@ -0,0 +1,55 @@
<?php
namespace EP\EpTrack\Hook;
/***************************************************************
*
* Copyright notice
*
* (c) 2016 Björn Fromme <[email protected]>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\DataHandling\DataHandler;
class Tcemain
{
/**
* @param array $fields
* @param string $table
* @param string $table
* @param int $id
* @param DataHandler $dataHandler
*/
public function processDatamap_preProcessFieldArray(array &$fields, $table, $id, DataHandler $dataHandler)
{
// Force trailing slash
if ($table === 'tx_eptrack_domain_model_redirect') {
$url = $fields['redirect_from'];
if (substr($url, 0, 1) !== '/') {
$url = '/' . $url;
}
if (substr($url, -1) !== '/') {
$url .= '/';
}
$fields['redirect_from'] = $url;
}
}
}
@@ -0,0 +1,177 @@
<?php
namespace EP\EpTrack\Service;
/***************************************************************
*
* Copyright notice
*
* (c) 2016 Björn Fromme <[email protected]>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Page\PageGenerator;
use TYPO3\CMS\Frontend\Utility\EidUtility;
class RequestService
{
const COOKIE_NAME = 'ep_trk';
const COOKIE_TTL = 30;
const CODE_PATTERN = '([a-z]{4}|PA)\d{4}.*';
public function handleRequest()
{
$currentUri = GeneralUtility::getIndpEnv('REQUEST_URI');
$this->matchRedirect($currentUri);
$this->matchTrackingCodeInUri($currentUri);
}
/**
* @param string $currentUri
*/
public function matchRedirect($currentUri)
{
$uri = strtolower(parse_url($currentUri, \PHP_URL_PATH));
/** @var DatabaseConnection $db */
$db = $GLOBALS['TYPO3_DB'];
$redirect = $db->exec_SELECTgetSingleRow(
'*',
'tx_eptrack_domain_model_redirect',
'tx_eptrack_domain_model_redirect.redirect_from=LOWER(' . $db->fullQuoteStr($uri, 'tx_eptrack_domain_model_redirect') . ')'
. ' AND tx_eptrack_domain_model_redirect.deleted=0 AND tx_eptrack_domain_model_redirect.hidden=0'
);
if ($redirect === false) {
return;
}
$trackingCode = $redirect['tracking_code'];
if (!empty($trackingCode)) {
$this->setCookie($trackingCode);
}
$targetPageUid = $redirect['target_page_uid'];
$responseCode = $redirect['response_code'];
$this->initTSFE($targetPageUid);
/** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$typolinkConfig = ['parameter' => $targetPageUid, 'forceAbsoluteUrl' => true ];
$query = $this->buildUtmParameters($redirect);
if ($query) {
$typolinkConfig['additionalParams'] = '&' . $query;
}
$uri = $cObj->typolink_URL($typolinkConfig);
$this->redirect($uri, $responseCode);
}
/**
* @param array $redirect
* @return string
*/
public function buildUtmParameters(array $redirect)
{
if ($redirect['utm_source'] && $redirect['utm_medium'] && $redirect['utm_campaign'] && $redirect['utm_content']) {
return http_build_query([
'utm_source' => $redirect['utm_source'],
'utm_medium' => $redirect['utm_medium'],
'utm_campaign' => $redirect['utm_campaign'],
'utm_content' => $redirect['utm_content'],
]);
}
return '';
}
/**
* @param string $currentUri
*/
public function matchTrackingCodeInUri($currentUri)
{
$query = parse_url($currentUri, \PHP_URL_QUERY);
$path = parse_url($currentUri, \PHP_URL_PATH);
if (preg_match('/(' . static::CODE_PATTERN . ')\/?$/i', $path)) {
$urlItems = GeneralUtility::trimExplode('/', $path, true);
$code = array_pop($urlItems);
$this->setCookie($code);
$uri = '/';
if (count($urlItems) > 0) {
$uri = '/' . implode('/', $urlItems) . '/';
}
if ($query !== null) {
$uri .= '?' . $query;
}
$this->redirect($uri);
}
}
/**
* @param string $code
*/
protected function setCookie($code)
{
$expiratonDate = new \DateTime('now');
$expiratonDate->add(new \DateInterval('P' . static::COOKIE_TTL. 'D'));
setcookie(static::COOKIE_NAME, $code, $expiratonDate->getTimestamp(), '/');
}
/**
* @param string $uri
* @param string $responseCode
*/
protected function redirect($uri, $responseCode = '301')
{
HttpUtility::redirect($uri, constant('\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_' . $responseCode));
exit;
}
/**
* @param int $id
* @param int $typeNum
*/
public function initTSFE($id = 1, $typeNum = 0)
{
EidUtility::initTCA();
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new NullTimeTracker;
$GLOBALS['TT']->start();
}
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $id, $typeNum);
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->rootLine = $GLOBALS['TSFE']->sys_page->getRootLine($id, '');
$GLOBALS['TSFE']->getConfigArray();
if (ExtensionManagementUtility::isLoaded('realurl')) {
$rootline = BackendUtility::BEgetRootLine($id);
$host = BackendUtility::firstDomainRecord($rootline);
$_SERVER['HTTP_HOST'] = $host;
}
PageGenerator::pagegenInit();
}
}