Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Support for multi-domains
Browse files Browse the repository at this point in the history
We used to hardcode the the PID 0 to create a fake TSFE to build
frontend URIs from the backend. This used to work for single domain
install, but totally crash the BE module (and more) for multi-domains
install.

We now don't hardcode anything and instead use the respective object's
PID to generate link. The result is that Newsletter is able to generate
links across domains correctly.
  • Loading branch information
PowerKiKi committed Feb 3, 2016
1 parent ef1f618 commit 530094b
Show file tree
Hide file tree
Showing 20 changed files with 262 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function indexAction()
$configuration = array(
'pageId' => $this->pageId,
'pageType' => $pageType,
'emailShowUrl' => UriBuilder::buildFrontendUri('show', array(), 'Email'),
'emailShowUrl' => UriBuilder::buildFrontendUri($this->pageId, 'Email', 'show'),
);

$this->view->assign('configuration', $configuration);
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/Model/RecipientList.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ public function getExtract($limit = 30)
$out = '<table style="border: 1px grey solid; border-collapse: collapse;">' . $out . '</table>';

$authCode = \TYPO3\CMS\Core\Utility\GeneralUtility::stdAuthCode($this->_getCleanProperties());
$uriXml = UriBuilder::buildFrontendUri('export', array('uidRecipientList' => $this->getUid(), 'authCode' => $authCode, 'format' => 'xml'), 'RecipientList');
$uriCsv = UriBuilder::buildFrontendUri('export', array('uidRecipientList' => $this->getUid(), 'authCode' => $authCode, 'format' => 'csv'), 'RecipientList');
$uriXml = UriBuilder::buildFrontendUri($this->getPid(), 'RecipientList', 'export', array('uidRecipientList' => $this->getUid(), 'authCode' => $authCode, 'format' => 'xml'));
$uriCsv = UriBuilder::buildFrontendUri($this->getPid(), 'RecipientList', 'export', array('uidRecipientList' => $this->getUid(), 'authCode' => $authCode, 'format' => 'csv'));

$out .= '<p><strong>' . $i . '/' . $this->getCount() . '</strong> recipients
(<a href="' . $uriXml . '">export XML</a>, '
Expand Down
6 changes: 2 additions & 4 deletions Classes/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ private function findAttachments($src)
*/
private function injectOpenSpy(Email $email)
{
$url = UriBuilder::buildFrontendUri('opened', array(
'c' => $email->getAuthCode(),
), 'Email');
$url = UriBuilder::buildFrontendUri($email->getPid(), 'Email', 'opened', array('c' => $email->getAuthCode()));

$this->html = str_ireplace('</body>', '<div><img src="' . $url . '" width="0" height="0" /></div></body>', $this->html);
}
Expand Down Expand Up @@ -302,7 +300,7 @@ private function getLinkAuthCode(Email $email, $url, $isPreview, $isPlainText =
if ($isPlainText) {
$arguments['p'] = 1;
}
$newUrl = UriBuilder::buildFrontendUri('clicked', $arguments, 'Link');
$newUrl = UriBuilder::buildFrontendUri($email->getPid(), 'Link', 'clicked', $arguments);

return $newUrl;
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Utility/MarkerSubstitutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ private function getMarkers(Email $email)

// Add predefined markers
$authCode = $email->getAuthCode();
$markers['newsletter_view_url'] = UriBuilder::buildFrontendUri('show', array('c' => $authCode), 'Email');
$markers['newsletter_unsubscribe_url'] = UriBuilder::buildFrontendUri('unsubscribe', array('c' => $authCode), 'Email');
$markers['newsletter_view_url'] = UriBuilder::buildFrontendUri($email->getPid(), 'Email', 'show', array('c' => $authCode));
$markers['newsletter_unsubscribe_url'] = UriBuilder::buildFrontendUri($email->getPid(), 'Email', 'unsubscribe', array('c' => $authCode));

return $markers;
}
Expand Down
74 changes: 40 additions & 34 deletions Classes/Utility/UriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,40 @@
*/
abstract class UriBuilder
{
const EXTENSION_NAME = 'newsletter';
const PLUGIN_NAME = 'p';

/**
* UriBuilder
* @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
* UriBuilders indexed by PID
* @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder[]
*/
private static $uriBuilder = null;
private static $uriBuilder = array();

private static function getUriBuilder($currentPid)
{
if (!isset(self::$uriBuilder[$currentPid])) {
$builder = self::createUriBuilder($currentPid);
self::$uriBuilder[$currentPid] = $builder;
}

return self::$uriBuilder[$currentPid];
}

/**
* Build an uriBuilder that can be used from any context (backend, frontend, TCA) to generate frontend URI
* @param string $extensionName
* @param string $pluginName
* @param int $currentPid
* @return \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
*/
private static function buildUriBuilder($extensionName, $pluginName)
private static function createUriBuilder($currentPid)
{

// If we are in Backend we need to simulate minimal TSFE
if (!isset($GLOBALS['TSFE']) || !($GLOBALS['TSFE'] instanceof \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController)) {
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\TimeTracker();
$GLOBALS['TT']->start();
}
$pid = 0;
$TSFE = @\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', $GLOBALS['TYPO3_CONF_VARS'], $pid, '0', 1);

$TSFE = @\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', $GLOBALS['TYPO3_CONF_VARS'], $currentPid, '0', 1);

$GLOBALS['TSFE'] = $TSFE;
$GLOBALS['TSFE']->initFEuser();
Expand All @@ -70,46 +81,41 @@ private static function buildUriBuilder($extensionName, $pluginName)
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
if (!(isset($GLOBALS['dispatcher']) && $GLOBALS['dispatcher'] instanceof \TYPO3\CMS\Extbase\Core\Bootstrap)) {
$extbaseBootstrap = $objectManager->get('TYPO3\\CMS\\Extbase\\Core\\Bootstrap');
$extbaseBootstrap->initialize(array('extensionName' => $extensionName, 'pluginName' => $pluginName));
$extbaseBootstrap->initialize(array('extensionName' => self::EXTENSION_NAME, 'pluginName' => self::PLUGIN_NAME));
}

return $objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder');
}

/**
* Returns a frontend URI independently of current context, with or without extbase, and with or without TSFE
* @param string $actionName
* @param array $controllerArguments
* @param int $currentPid
* @param string $controllerName
* @param string $extensionName
* @param string $pluginName
* @param array $otherArguments
* @param string $actionName
* @param array $arguments
* @return string absolute URI
*/
public static function buildFrontendUri($actionName, array $controllerArguments, $controllerName, $extensionName = 'newsletter', $pluginName = 'p', array $otherArguments = null)
public static function buildFrontendUri($currentPid, $controllerName, $actionName, array $arguments = array())
{
if (!self::$uriBuilder) {
self::$uriBuilder = self::buildUriBuilder($extensionName, $pluginName);
}
$controllerArguments['action'] = $actionName;
$controllerArguments['controller'] = $controllerName;

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$extensionService = $objectManager->get('TYPO3\\CMS\\Extbase\\Service\\ExtensionService');
$pluginNamespace = $extensionService->getPluginNamespace($extensionName, $pluginName);
$pluginNamespace = $extensionService->getPluginNamespace(self::EXTENSION_NAME, self::PLUGIN_NAME);

if (!isset($otherArguments) || is_null($otherArguments)) {
$otherArguments = array();
}
// Prepare arguments
$arguments['action'] = $actionName;
$arguments['controller'] = $controllerName;
$namespacedArguments = array($pluginNamespace => $arguments);

// Configure Uri
$uriBuilder = self::getUriBuilder($currentPid);
$uriBuilder->reset()
->setUseCacheHash(false)
->setCreateAbsoluteUri(true)
->setArguments($namespacedArguments)
->setTargetPageType(1342671779);

$arguments = $otherArguments;
$arguments[$pluginNamespace] = $controllerArguments;
self::$uriBuilder->reset()
->setUseCacheHash(false)
->setCreateAbsoluteUri(true)
->setArguments($arguments)
->setTargetPageType(1342671779);
$uri = $uriBuilder->buildFrontendUri();

return self::$uriBuilder->buildFrontendUri();
return $uri;
}
}
1 change: 0 additions & 1 deletion Tests/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function setUp()
parent::setUp();
$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');

$this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
$this->importDataSet(__DIR__ . '/Fixtures/fixtures.xml');
$this->authCode = md5(302 . '[email protected]');
}
Expand Down
68 changes: 68 additions & 0 deletions Tests/Functional/Fixtures/fixtures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,74 @@
</config>
</sys_template>

<sys_domain>
<pid>6</pid>
<domainName>alternative-domain.com</domainName>
</sys_domain>

<sys_template>
<pid>6</pid>
<root>1</root>
<config>
page = PAGE
page {
typeNum = 0
}
</config>
</sys_template>

<pages>
<uid>1</uid>
<pid>0</pid>
<title>Root 1</title>
<deleted>0</deleted>
<perms_everybody>15</perms_everybody>
<doktype>1</doktype>
<is_siteroot>1</is_siteroot>
</pages>
<pages>
<uid>2</uid>
<pid>1</pid>
<title>Dummy 1-2</title>
<deleted>0</deleted>
<perms_everybody>15</perms_everybody>
<doktype>1</doktype>
</pages>
<pages>
<uid>3</uid>
<pid>2</pid>
<title>Dummy 1-2-3</title>
<deleted>0</deleted>
<perms_everybody>15</perms_everybody>
<doktype>1</doktype>
</pages>

<pages>
<uid>4</uid>
<pid>0</pid>
<title>Root 2</title>
<deleted>0</deleted>
<perms_everybody>15</perms_everybody>
<doktype>1</doktype>
<is_siteroot>1</is_siteroot>
</pages>
<pages>
<uid>5</uid>
<pid>4</pid>
<title>Dummy 2-2</title>
<deleted>0</deleted>
<perms_everybody>15</perms_everybody>
<doktype>1</doktype>
</pages>
<pages>
<uid>6</uid>
<pid>5</pid>
<title>Dummy 2-2-3</title>
<deleted>0</deleted>
<perms_everybody>15</perms_everybody>
<doktype>1</doktype>
</pages>

<tx_newsletter_domain_model_newsletter>
<uid>10</uid>
<pid>2</pid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
<a href="&#x6D;&#x61;&#x69;&#x6C;&#x74;&#x6F;&#x3A;&#x6A;&#x6F;&#x68;&#x6E;&#x40;&#x65;&#x78;&#x61;&#x6D;&#x70;&#x6C;&#x65;&#x2E;&#x63;&#x6F;&#x6D;">HTML entities encoded mailto link should still work</a>

###unknown_field_will_be_kept###
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
[email protected]
my custom value

http://unknown_field_will_be_kept
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
[email protected]
my custom value

https://unknown_field_will_be_kept
https://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
https://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
https://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
https://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
[email protected]
my custom value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ should be injected [1] this custom link should be injected [2] this
link should be injected, and marker substituted [3] mailto link should
still work HTML entities encoded mailto link should still work [4]
###unknown_field_will_be_kept###
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
[email protected] my custom value
http://unknown_field_will_be_kept
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
http://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
[email protected] my custom value
https://unknown_field_will_be_kept
https://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
https://example.com/index.php?id=1&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
https://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=show&tx_newsletter_p%5Bcontroller%5D=Email
https://example.com/index.php?id=2&type=1342671779&tx_newsletter_p%5Bc%5D=d41d8cd98f00b204e9800998ecf8427e&tx_newsletter_p%5Baction%5D=unsubscribe&tx_newsletter_p%5Bcontroller%5D=Email
[email protected] my custom value boolean_false=
boolean_true=true-ish integer_false= integer_true=true-ish
string_false= string_true=true-ish boolean_false=false-ish
Expand Down
Loading

0 comments on commit 530094b

Please sign in to comment.