Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffparnitzky committed Aug 31, 2016
2 parents 91703d4 + 2cb162f commit 3b9be7d
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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;;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @package Hofff_robots-txt-editor
* @link https://contao.org
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/


/**
* Run in a custom namespace, so the class can be replaced
*/
namespace Hofff\Contao\RobotsTxtEditor;


/**
* Class RobotsTxtEditorHtaccessWriter
*
* EventSubscriber to extend .htaccess file
* @copyright Hofff.com 2016-2016
* @author Cliff Parnitzky <[email protected]>
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
));
19 changes: 18 additions & 1 deletion CT_ROOT/system/modules/hofff_robots-txt-editor/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,21 @@
* File constants
*/
define('FILE_ROBOTS_TXT', 'robots.txt');
define('FILE_ROBOTS_TXT_DEFAULT', 'robots.txt.default');
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';
}
33 changes: 27 additions & 6 deletions CT_ROOT/system/modules/hofff_robots-txt-editor/dca/tl_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand All @@ -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'],
Expand All @@ -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
Expand All @@ -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;
}
}

Expand All @@ -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
{
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

$GLOBALS['TL_LANG']['ERR']['no_robotstxt_default'] = "Es befindet sich keine <b>" . FILE_ROBOTS_TXT_DEFAULT . "</b> in ihrem Contao Root.<br/>Diese Datei hätte bei der Installation der Erweiterung initial erstellt werden sollen.<br/><br/>Der Import wurde abgebrochen.<br/><br/>Bitte installieren Sie die Erweiterung erneut.";
$GLOBALS['TL_LANG']['ERR']['robotstxt_not_updated'] = "Die <b>" . FILE_ROBOTS_TXT . "</b> 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 <b>" . FILE_ROBOTS_TXT . "</b> 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.";
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

$GLOBALS['TL_LANG']['tl_page']['robotstxt_legend'] = "robots.txt Editor";
$GLOBALS['TL_LANG']['tl_page']['createRobotsTxt'] = array("Eine robots.txt erstellen", "Eine individuelle robots.txt erstellen.");
$GLOBALS['TL_LANG']['tl_page']['robotsTxtContent'] = array("Inhalt der robots.txt", "Geben Sie den Inhalt der robots.txt an.");

$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");
$GLOBALS['TL_LANG']['tl_page']['createRobotsTxt'] = array("Eine robots.txt erstellen", "Eine individuelle robots.txt erstellen.");
$GLOBALS['TL_LANG']['tl_page']['robotsTxtContent'] = array("Inhalt der robots.txt", "Geben Sie den Inhalt der robots.txt an.");
$GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'] = array("Domainspezifische robots.txt verwenden", "Wählen Sie, ob die robots.txt domainspezifisch sein soll, d.h. für die Domain dieses Seitenbaums wird eine individuelle robots.txt im <i>share</i> Verzeichnis angelegt und in die .htaccess Datei wird eine entsprechende Rewrite Regel eingetragen.<br/><br/>Erstellt folgenden .htaccess Eintrag (beispielhaft):<br/><br/><i>" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteCond'], "domain-a\.tld") . "<br/>" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteRule'], "robots\.txt", "share/robots_alias\.txt") . "</i>");
$GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessNotInstalled'] = array($GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'][0], "Für dieses Feature muss die Erweiterung <i>hofff/contao-htaccess</i> installiert und aktiv sein.<br/><br/>" . $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).");
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

$GLOBALS['TL_LANG']['ERR']['no_robotstxt_default'] = "There is no <b>" . FILE_ROBOTS_TXT_DEFAULT . "</b> in your Contao root.<br/>This file should be created initially when you installed the extension.<br/><br/>The import was aborted.<br/><br/>Please install the extension again.";
$GLOBALS['TL_LANG']['ERR']['robotstxt_not_updated'] = "The <b>" . FILE_ROBOTS_TXT . "</b> 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 <b>" . FILE_ROBOTS_TXT . "</b> 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.";
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

$GLOBALS['TL_LANG']['tl_page']['robotstxt_legend'] = "robots.txt Editor";
$GLOBALS['TL_LANG']['tl_page']['createRobotsTxt'] = array("Create a robots.txt", "Create a custom robots.txt file.");
$GLOBALS['TL_LANG']['tl_page']['robotsTxtContent'] = array("Content of the robots.txt", "Please enter the content of the robots.txt file.");

$GLOBALS['TL_LANG']['tl_page']['createRobotsTxt'] = array("Create a robots.txt", "Create a custom robots.txt file.");
$GLOBALS['TL_LANG']['tl_page']['robotsTxtContent'] = array("Content of the robots.txt", "Please enter the content of the robots.txt file.");
$GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'] = array("Use domain specific robots.txt", "Please select, if the robots.txt should be domain specific, i.e. for the domain of this page tree a custom robots.txt will be created in the <i>share</i> folder and in the .htaccess file a corresponding rewrite rule is added.<br/><br/>Creates the following .htaccess entry (exemplary):<br/><br/><i>" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteCond'], "domain-a\.tld") . "<br/>" . sprintf($GLOBALS['TL_CONFIG']['RobotsTxtEditorRewriteRule'], "robots\.txt", "share/robots_alias\.txt") . "</i>");
$GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessNotInstalled'] = array($GLOBALS['TL_LANG']['tl_page']['useDomainSpecificRobotsTxt_htaccessInstalled'][0], "To use this feature the extension <i>hofff/contao-htaccess</i> has to be installed and must be active.<br/><br/>" . $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");

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down
Binary file modified screenshot-backend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3b9be7d

Please sign in to comment.