diff --git a/CHANGELOG.md b/CHANGELOG.md index bfb0905..e92cae3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ Contao Extension "hofff/contao-robots-txt-editor" ------------------------------------------------- +### Version 1.0.0-beta2 (2016-08-31) ### +- Adds Multi-Site-Installation (see #1) +- Improves extending `sitemap` section in BE (see #2) + ### Version 1.0.0-beta1 (2016-06-02) ### - Initial release \ No newline at end of file diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditor.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditor.php index 4a8a279..887c730 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditor.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditor.php @@ -61,17 +61,33 @@ public function importRobotsTxt(\DataContainer $dc) */ public function createRobotsTxt(\DataContainer $dc) { - $filepath = TL_ROOT . "/" . FILE_ROBOTS_TXT; - - if (!is_writable($filepath)) - { - return false; - } + $filePath = TL_ROOT . "/" . FILE_ROBOTS_TXT; $objPage = $dc->activeRecord; - + if ($objPage != null) { + if (static::isDomainSpecicCreationAllowed($dc->activeRecord->useDomainSpecificRobotsTxt)) + { + $filePath = TL_ROOT . "/" . static::getDomainSpecificFilePath($dc->activeRecord->alias); + + // delete the old file, if the alias was changed + $objOldPage = \Contao\Database::getInstance()->prepare("SELECT * FROM tl_version WHERE fromTable=? AND pid=? ORDER BY version DESC") + ->limit(1) + ->execute('tl_page', $dc->id); + + if ($objOldPage != null && ($strAliasOld = deserialize($objOldPage->data)['alias']) && $strAliasOld!= $objPage->alias) + { + \Message::addInfo($GLOBALS['TL_LANG']['MSC']['DomainSpecificRobotsTxt_cleared']); + $filePathOld = TL_ROOT . "/" . static::getDomainSpecificFilePath($strAliasOld); + + if (file_exists($filePathOld)) + { + unlink($filePathOld); + } + } + } + $fileContent = $objPage->robotsTxtContent; if ($objPage->createSitemap && $objPage->sitemapName != '' && $objPage->robotsTxtAddAbsoluteSitemapPath) @@ -82,7 +98,7 @@ public function createRobotsTxt(\DataContainer $dc) $fileContent .= "Sitemap: " . $strDomain . "share/" . $objPage->sitemapName . ".xml"; } - if (file_put_contents($filepath, $fileContent) === FALSE) + if (file_put_contents($filePath, $fileContent) === FALSE) { return false; } @@ -94,4 +110,31 @@ public function createRobotsTxt(\DataContainer $dc) return false; } + + /** + * Checks whether the extension 'htaccess' is installed and active. + * @return True, if the extension 'htaccess' is installed and active. + */ + public static function isHtaccessEnabled () + { + return in_array('htaccess', \ModuleLoader::getActive()); + } + + /** + * Checks whether creation of a domain specific robots.txt is allowed. + * @param $blnUseDomainSpecificRobotsTxt The value from the DataContainer. + * @return True, if the extension 'htaccess' is installed and the parametrized value in the page is checked. + */ + public static function isDomainSpecicCreationAllowed ($blnUseDomainSpecificRobotsTxt) + { + return static::isHtaccessEnabled() && $blnUseDomainSpecificRobotsTxt; + } + + /** + * Returns the file path to the domain specific robots.txt file. + */ + public static function getDomainSpecificFilePath ($strAlias) + { + return FILE_ROBOTS_TXT_DOMAIN_SPECIFIC_Folder . "/" . FILE_ROBOTS_TXT_DOMAIN_SPECIFIC_PREFIX . $strAlias . FILE_ROBOTS_TXT_DOMAIN_SPECIFIC_SUFFIX;; + } } \ No newline at end of file diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditorHtaccessWriter.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditorHtaccessWriter.php new file mode 100644 index 0000000..5ecb922 --- /dev/null +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditorHtaccessWriter.php @@ -0,0 +1,64 @@ + + * @package Hofff_robots-txt-editor + */ +class RobotsTxtEditorHtaccessWriter implements \Symfony\Component\EventDispatcher\EventSubscriberInterface +{ + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return array( + \Bit3\Contao\Htaccess\HtaccessEvents::GENERATE_REWRITES => 'generateRewrites', + ); + } + + /** + * Generate this sub module code. + * + * @return string + */ + public function generateRewrites(\Bit3\Contao\Htaccess\Event\GenerateRewritesEvent $event) + { + $objPages = \Contao\Database::getInstance()->prepare("SELECT alias, dns FROM tl_page WHERE createRobotsTxt = 1 AND useDomainSpecificRobotsTxt = 1 AND published = 1")->execute(); + + while ($objPages->next()) + { + $strRewriteRule = sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteCond'], $this->prepareUrl($objPages->dns)) + . "\n" + . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteRule'], $this->prepareUrl(FILE_ROBOTS_TXT), $this->prepareUrl(RobotsTxtEditor::getDomainSpecificFilePath($objPages->alias))); + $pre = $event->getPre(); + $pre->append(PHP_EOL . $strRewriteRule); + } + } + + private function prepareUrl($strUrl) + { + $strUrl = str_replace('.', '\.', $strUrl); + return $strUrl; + } +} \ No newline at end of file diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/config/autoload.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/config/autoload.php index cd85e90..c00b6c0 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/config/autoload.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/config/autoload.php @@ -24,5 +24,6 @@ ClassLoader::addClasses(array ( // Classes - 'Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor' => 'system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditor.php', + 'Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor' => 'system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditor.php', + 'Hofff\Contao\RobotsTxtEditor\RobotsTxtEditorHtaccessWriter' => 'system/modules/hofff_robots-txt-editor/classes/RobotsTxtEditorHtaccessWriter.php', )); diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/config/config.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/config/config.php index 6128d49..246981e 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/config/config.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/config/config.php @@ -9,4 +9,21 @@ * File constants */ define('FILE_ROBOTS_TXT', 'robots.txt'); -define('FILE_ROBOTS_TXT_DEFAULT', 'robots.txt.default'); \ No newline at end of file +define('FILE_ROBOTS_TXT_DEFAULT', 'robots.txt.default'); +define('FILE_ROBOTS_TXT_DOMAIN_SPECIFIC_Folder', 'share'); +define('FILE_ROBOTS_TXT_DOMAIN_SPECIFIC_PREFIX', 'robots_'); +define('FILE_ROBOTS_TXT_DOMAIN_SPECIFIC_SUFFIX', '.txt'); + +/** + * Configuration + */ +// Rewrite condition and rule (escape % with %% !!!): +// RewriteCond %{HTTP_HOST} ^(www\.)?domain-a\.tld$ +// RewriteRule ^robots\.txt share/robots_alias\.txt +$GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteCond'] = "RewriteCond %%{HTTP_HOST} ^(www\.)?%s$"; +$GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteRule'] = "RewriteRule ^%s %s"; + +if (Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor::isHtaccessEnabled()) +{ + $GLOBALS['TL_EVENT_SUBSCRIBERS'][] = 'Hofff\Contao\RobotsTxtEditor\RobotsTxtEditorHtaccessWriter'; +} \ No newline at end of file diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/dca/tl_page.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/dca/tl_page.php index 35fb25c..83bbaed 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/dca/tl_page.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/dca/tl_page.php @@ -16,7 +16,7 @@ $GLOBALS['TL_DCA']['tl_page']['palettes']['root'] = implode(";", $arrLegends); $GLOBALS['TL_DCA']['tl_page']['palettes']['__selector__'][] = "createRobotsTxt"; -$GLOBALS['TL_DCA']['tl_page']['subpalettes']['createRobotsTxt'] = "robotsTxtContent"; +$GLOBALS['TL_DCA']['tl_page']['subpalettes']['createRobotsTxt'] = "robotsTxtContent,useDomainSpecificRobotsTxt"; $GLOBALS['TL_DCA']['tl_page']['fields']['createRobotsTxt'] = array ( @@ -38,6 +38,14 @@ ), 'sql' => "text NULL" ); +$GLOBALS['TL_DCA']['tl_page']['fields']['useDomainSpecificRobotsTxt'] = array +( + 'label' => Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor::isHtaccessEnabled() ? $GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'] : $GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessNotInstalled'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class'=>'w50', 'submitOnChange'=>true, 'disabled'=>!Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor::isHtaccessEnabled()), + 'sql' => "char(1) NOT NULL default ''" +); $GLOBALS['TL_DCA']['tl_page']['fields']['robotsTxtAddAbsoluteSitemapPath'] = array ( 'label' => &$GLOBALS['TL_LANG']['tl_page']['robotsTxtAddAbsoluteSitemapPath'], @@ -51,6 +59,7 @@ * Table tl_page */ $GLOBALS['TL_DCA']['tl_page']['config']['onsubmit_callback'][] = array('tl_page_hofff_robots_txt_editor', 'updateRobotsTxt'); +$GLOBALS['TL_DCA']['tl_page']['config']['onsubmit_callback'][] = array('tl_page_hofff_robots_txt_editor', 'updateHtaccess'); /** * Class tl_page_hofff_robots_txt_editor @@ -75,11 +84,15 @@ public function __construct() */ public function modifyPaletteAndFields($dc) { - $objPage = \PageModel::findById((int) $dc->id); - - if ($objPage != null && $objPage->createSitemap && $objPage->createRobotsTxt) + $objPage = \Database::getInstance()->prepare("SELECT * FROM tl_page WHERE id = ?")->execute($dc->id); + if ($objPage->next()) { - $GLOBALS['TL_DCA']['tl_page']['subpalettes']['createSitemap'] = $GLOBALS['TL_DCA']['tl_page']['subpalettes']['createSitemap'] . ',robotsTxtAddAbsoluteSitemapPath'; + if ($objPage->createRobotsTxt) + { + $GLOBALS['TL_DCA']['tl_page']['subpalettes']['createSitemap'] = $GLOBALS['TL_DCA']['tl_page']['subpalettes']['createSitemap'] . ',robotsTxtAddAbsoluteSitemapPath'; + } + + $GLOBALS['TL_DCA']['tl_page']['fields']['dns']['eval']['mandatory'] = $objPage->useDomainSpecificRobotsTxt; } } @@ -94,7 +107,7 @@ public function updateRobotsTxt(DataContainer $dc) $robotsTxtEditor = new Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor(); if ($robotsTxtEditor->createRobotsTxt($dc)) { - \Message::addInfo($GLOBALS['TL_LANG']['MSC']['robotstxt_updated']); + \Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['robotstxt_updated']); } else { @@ -103,6 +116,14 @@ public function updateRobotsTxt(DataContainer $dc) } } + public function updateHtaccess(DataContainer $dc) + { + if (Hofff\Contao\RobotsTxtEditor\RobotsTxtEditor::isDomainSpecicCreationAllowed($dc->activeRecord->useDomainSpecificRobotsTxt)) + { + $objHtaccess = Bit3\Contao\Htaccess\Htaccess::getInstance(); + $objHtaccess->update(); + } + } /** * Add a link to the robots.txt import wizard diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/default.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/default.php index 9e83ee6..e48a07f 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/default.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/default.php @@ -1,6 +1,8 @@ " . FILE_ROBOTS_TXT_DEFAULT . " in ihrem Contao Root.
Diese Datei hätte bei der Installation der Erweiterung initial erstellt werden sollen.

Der Import wurde abgebrochen.

Bitte installieren Sie die Erweiterung erneut."; -$GLOBALS['TL_LANG']['ERR']['robotstxt_not_updated'] = "Die " . FILE_ROBOTS_TXT . " konnte nicht neu geschrieben werden (ggf. ist der Zugriff auf die Datei gesperrt)."; +$GLOBALS['TL_LANG']['ERR']['robotstxt_not_updated'] = "Die " . FILE_ROBOTS_TXT . " Datei konnte nicht neu erstellt werden (ggf. ist der Zugriff auf die Datei gesperrt)."; -$GLOBALS['TL_LANG']['MSC']['robotstxt_updated'] = "Die " . FILE_ROBOTS_TXT . " wurde neu geschrieben."; +$GLOBALS['TL_LANG']['MSC']['robotstxt_updated'] = "Die " . FILE_ROBOTS_TXT . " Datei wurde neu erstellt."; + +$GLOBALS['TL_LANG']['MSC']['DomainSpecificRobotsTxt_cleared'] = "Der Seitenalias wurde geändert, deshalb wurde die alte domainspezifische robots.txt gelöscht."; \ No newline at end of file diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/tl_page.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/tl_page.php index 4413612..6e0ec50 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/tl_page.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/de/tl_page.php @@ -1,9 +1,12 @@ share Verzeichnis angelegt und in die .htaccess Datei wird eine entsprechende Rewrite Regel eingetragen.

Erstellt folgenden .htaccess Eintrag (beispielhaft):

" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteCond'], "domain-a\.tld") . "
" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteRule'], "robots\.txt", "share/robots_alias\.txt") . "
"); +$GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessNotInstalled'] = array($GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'][0], "Für dieses Feature muss die Erweiterung hofff/contao-htaccess installiert und aktiv sein.

" . $GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'][1]); + +$GLOBALS['TL_LANG']['tl_page']['robotsTxtAddAbsoluteSitemapPath'] = array("Absoluten Sitemap Pfad zu robots.txt hinzufügen", "Den absoluten Pfad der Sitemap zur robots.txt hinzufügen."); $GLOBALS['TL_LANG']['tl_page']['robotsTxtContentImport'] = array("Default robots.txt importieren", "Die default robots.txt importieren (überschreibt vorhandene Daten)."); diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/default.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/default.php index a977403..eba4db8 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/default.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/default.php @@ -1,6 +1,8 @@ " . FILE_ROBOTS_TXT_DEFAULT . " in your Contao root.
This file should be created initially when you installed the extension.

The import was aborted.

Please install the extension again."; -$GLOBALS['TL_LANG']['ERR']['robotstxt_not_updated'] = "The " . FILE_ROBOTS_TXT . " has not been recreated (possibly, the access to the file is locked)."; +$GLOBALS['TL_LANG']['ERR']['robotstxt_not_updated'] = "The " . FILE_ROBOTS_TXT . " file has not been recreated (possibly, the access to the file is locked)."; -$GLOBALS['TL_LANG']['MSC']['robotstxt_updated'] = "The " . FILE_ROBOTS_TXT . " has been recreated."; +$GLOBALS['TL_LANG']['MSC']['robotstxt_updated'] = "The " . FILE_ROBOTS_TXT . " file has been recreated."; + +$GLOBALS['TL_LANG']['MSC']['DomainSpecificRobotsTxt_cleared'] = "The page alias has been changed, so the old domain specific robots.txt was deleted."; \ No newline at end of file diff --git a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/tl_page.php b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/tl_page.php index 356d9be..b161d43 100644 --- a/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/tl_page.php +++ b/CT_ROOT/system/modules/hofff_robots-txt-editor/languages/en/tl_page.php @@ -1,8 +1,12 @@ share folder and in the .htaccess file a corresponding rewrite rule is added.

Creates the following .htaccess entry (exemplary):

" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteCond'], "domain-a\.tld") . "
" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteRule'], "robots\.txt", "share/robots_alias\.txt") . "
"); +$GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessNotInstalled'] = array($GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'][0], "To use this feature the extension hofff/contao-htaccess has to be installed and must be active.

" . $GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'][1]); + $GLOBALS['TL_LANG']['tl_page']['robotsTxtAddAbsoluteSitemapPath'] = array("Add absolute sitemap path to robots.txt", "Add the absolute path of the sitemap to the robots.txt"); diff --git a/README.md b/README.md index b0d0102..101430f 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Provides an editor for auto creation and modification of robots.txt inside a Con - Define content of robots.txt in root page - Add absolute path to sitemap +- If the extension [hofff/contao-htaccess](https://packagist.org/packages/hofff/contao-htaccess) is installed it is possible to create different custom robots.txt files in a multi site installation. ## Compatibility @@ -30,6 +31,8 @@ If you prefer to install it manually, download the latest release here: https:// There are no dependencies to other extensions, that have to be installed. +The extension [hofff/contao-htaccess](https://packagist.org/packages/hofff/contao-htaccess) is suggested to use different custom robots.txt files in a multi site installation. + ## Screenshots diff --git a/composer.json b/composer.json index 25ef4c0..1675a2d 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,9 @@ "conflict": { "contao/core":"3.4.0,3.4.1,3.4.2" }, + "suggest" : { + "hofff/contao-htaccess" : "To use different custom robots.txt files in a multi site installation." + }, "extra" : { "contao" : { "sources" : { diff --git a/screenshot-backend.png b/screenshot-backend.png index 267a33a..8c2df57 100644 Binary files a/screenshot-backend.png and b/screenshot-backend.png differ