Skip to content

Instantly share code, notes, and snippets.

@htuscher
Last active March 26, 2018 20:00
Show Gist options
  • Save htuscher/600937d98cbb8eb6fb5b to your computer and use it in GitHub Desktop.
Save htuscher/600937d98cbb8eb6fb5b to your computer and use it in GitHub Desktop.
TYPO3 Multitree without domain records in development
<?php
use TYPO3\CMS\Core\Utility\GeneralUtility;
if (\TYPO3\CMS\Core\Core\Bootstrap::getInstance()->getApplicationContext()->isDevelopment()) {
/**
* This override adds the possibility to use multitree subdomains without domain records (for SHARED_DB, vagrant, etc.)
* You can override the mappings or undo the override by creating a config file inside conf.d
*
* The override PageRepository checks for $subdomain . exec('hostname -f')
* e.g. in vagrant flastname.4pweb.vm.1drop.de is FQDN hostname, so you will get de.flastname.4pweb.vm.1drop.de etc.
*/
$GLOBALS['TYPO3_CONF_VARS']['SYS']['domainMapping'] = [
'de' => 42,
'us' => 1337,
'uk' => 1010,
];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\\CMS\\Frontend\\Page\\PageRepository']['className'] = 'ODS\\Onedrop\\Overrides\\PageRepository';
}
/**
* !!! IMPORTANT !!!
*
* To enable your local configuration add
* one or more files in conf.d as they are
* loaded automatically.
*/
$configurationFiles = GeneralUtility::getFilesInDir(GeneralUtility::getFileAbsFileName('typo3conf/conf.d/'), 'php', TRUE);
foreach ($configurationFiles as $file) {
require_once($file);
}
<?php
namespace ODS\Onedrop\Overrides;
/**
* Copyright notice
*
* (c) Onedrop Solutions GmbH & Co. KG, www.1drop.de
*
* @author Hans Höchtl <hhoechtl@1drop.de>
*
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
*/
class PageRepository extends \TYPO3\CMS\Frontend\Page\PageRepository {
/**
* Will find the page carrying the domain record matching the input domain.
* Might exit after sending a redirect-header IF a found domain record
* instructs to do so.
*
* @param string $domain Domain name to search for. Eg. "www.typo3.com". Typical the HTTP_HOST value.
* @param string $path Path for the current script in domain. Eg. "/somedir/subdir". Typ. supplied by \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('SCRIPT_NAME')
* @param string $request_uri Request URI: Used to get parameters from if they should be appended. Typ. supplied by \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')
*
* @return mixed If found, returns integer with page UID where found. Otherwise blank. Might exit if location-header is sent, see description.
* @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::findDomainRecord()
*/
public function getDomainStartPage( $domain, $path = '', $request_uri = '' ) {
$domain = explode( ':', $domain );
$domain = strtolower( preg_replace( '/\\.$/', '', $domain[0] ) );
// Removing extra trailing slashes
$path = trim( preg_replace( '/\\/[^\\/]*$/', '', $path ) );
// Appending to domain string
$domain .= $path;
$domain = preg_replace( '/\\/*$/', '', $domain );
/**
* This is the additional condition to have domain mapping without domain records
*/
if ( isset( $GLOBALS['TYPO3_CONF_VARS']['SYS']['domainMapping'] ) && $domain !== exec('hostname -f') ) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['domainMapping'] as $subdomain => $pageUid) {
if ($subdomain . '.' . exec('hostname -f') === $domain) {
return $pageUid;
}
}
throw new \RuntimeException('No Mapping for domain ' . $domain . ' defined');
} else {
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery( 'pages.uid,sys_domain.redirectTo,sys_domain.redirectHttpStatusCode,sys_domain.prepend_params', 'pages,sys_domain', 'pages.uid=sys_domain.pid
AND sys_domain.hidden=0
AND (sys_domain.domainName=' . $GLOBALS['TYPO3_DB']->fullQuoteStr( $domain, 'sys_domain' ) . ' OR sys_domain.domainName=' . $GLOBALS['TYPO3_DB']->fullQuoteStr( ( $domain . '/' ), 'sys_domain' ) . ') ' . $this->where_hid_del . $this->where_groupAccess, '', '', 1 );
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res );
$GLOBALS['TYPO3_DB']->sql_free_result( $res );
if ( $row ) {
if ( $row['redirectTo'] ) {
$redirectUrl = $row['redirectTo'];
if ( $row['prepend_params'] ) {
$redirectUrl = rtrim( $redirectUrl, '/' );
$prependStr = ltrim( substr( $request_uri, strlen( $path ) ), '/' );
$redirectUrl .= '/' . $prependStr;
}
$statusCode = (int) $row['redirectHttpStatusCode'];
if ( $statusCode && defined( 'TYPO3\\CMS\\Core\\Utility\\HttpUtility::HTTP_STATUS_' . $statusCode ) ) {
\TYPO3\CMS\Core\Utility\HttpUtility::redirect( $redirectUrl, constant( 'TYPO3\\CMS\\Core\\Utility\\HttpUtility::HTTP_STATUS_' . $statusCode ) );
} else {
\TYPO3\CMS\Core\Utility\HttpUtility::redirect( $redirectUrl, \TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_301 );
}
die;
} else {
return $row['uid'];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment