Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for editing the robots.txt on a website #4165

Merged
merged 3 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions DNN Platform/Library/Common/Utilities/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@ public static XmlDocument Load(string filename)
return xmlDoc;
}

public static string LoadNonConfig(string filename)
{
// open the config file
var doc = File.ReadAllText(Globals.ApplicationMapPath + "\\" + filename);

return doc;
}

public static void RemoveCodeSubDirectory(string name)
{
XmlDocument xmlConfig = Load();
Expand Down Expand Up @@ -632,6 +640,61 @@ public static string Save(XmlDocument xmlDoc, string filename)
return retMsg;
}

public static string SaveNonConfig(string document, string filename)
WillStrohl marked this conversation as resolved.
Show resolved Hide resolved
{
var retMsg = string.Empty;
try
{
var strFilePath = Globals.ApplicationMapPath + "\\" + filename;
var objFileAttributes = FileAttributes.Normal;
if (File.Exists(strFilePath))
{
// save current file attributes
objFileAttributes = File.GetAttributes(strFilePath);

// change to normal ( in case it is flagged as read-only )
File.SetAttributes(strFilePath, FileAttributes.Normal);
}

// Attempt a few times in case the file was locked; occurs during modules' installation due
// to application restarts where IIS can overlap old application shutdown and new one start.
const int maxRetires = 4;
const double miltiplier = 2.5;
for (var retry = maxRetires; retry >= 0; retry--)
{
try
{
// save the config file
File.WriteAllText(strFilePath, document);

break;
}
catch (IOException exc)
{
if (retry == 0)
{
Logger.Error(exc);
retMsg = exc.Message;
}

// try incremental delay; maybe the file lock is released by then
Thread.Sleep((int)(miltiplier * (maxRetires - retry + 1)) * 1000);
}
}

// reset file attributes
File.SetAttributes(strFilePath, objFileAttributes);
}
catch (Exception exc)
{
// the file permissions may not be set properly
Logger.Error(exc);
retMsg = exc.Message;
}

return retMsg;
}

public static bool Touch()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ namespace Dnn.PersonaBar.ConfigConsole.Components

public class ConfigConsoleController
{
private const string CONFIG_EXT = ".config";
private const string ROBOTS_EXT = "robots.txt"; // in multi-portal instances, there may be multiple robots.txt files (e.g., site1.com.robots.txt, site2.com.robots.txt, etc.)

public IEnumerable<string> GetConfigFilesList()
{
var files = Directory.GetFiles(Globals.ApplicationMapPath, "*.config");
var files = Directory
mitchelsellers marked this conversation as resolved.
Show resolved Hide resolved
.EnumerateFiles(Globals.ApplicationMapPath)
.Where(file => file.ToLower().EndsWith(CONFIG_EXT, StringComparison.InvariantCultureIgnoreCase) || file.ToLower().EndsWith(ROBOTS_EXT, StringComparison.InvariantCultureIgnoreCase))
.ToList();
IEnumerable<string> fileList = (from file in files select Path.GetFileName(file));
return fileList;
}
Expand All @@ -26,25 +32,41 @@ public string GetConfigFile(string configFile)
{
this.ValidateFilePath(configFile);

var configDoc = Config.Load(configFile);
using (var txtWriter = new StringWriter())
if (configFile.EndsWith(CONFIG_EXT, StringComparison.InvariantCultureIgnoreCase))
{
using (var writer = new XmlTextWriter(txtWriter))
var configDoc = Config.Load(configFile);
using (var txtWriter = new StringWriter())
{
writer.Formatting = Formatting.Indented;
configDoc.WriteTo(writer);
using (var writer = new XmlTextWriter(txtWriter))
{
writer.Formatting = Formatting.Indented;
configDoc.WriteTo(writer);
}

return txtWriter.ToString();
}
return txtWriter.ToString();
}
else
{
var doc = Config.LoadNonConfig(configFile);
return doc;
}
}

public void UpdateConfigFile(string fileName, string fileContent)
{
this.ValidateFilePath(fileName);

var configDoc = new XmlDocument { XmlResolver = null };
configDoc.LoadXml(fileContent);
Config.Save(configDoc, fileName);
if (fileName.EndsWith(CONFIG_EXT, StringComparison.InvariantCultureIgnoreCase))
{
var configDoc = new XmlDocument {XmlResolver = null};
configDoc.LoadXml(fileContent);
Config.Save(configDoc, fileName);
}
else
{
Config.SaveNonConfig(fileContent, fileName);
}
}

public void MergeConfigFile(string fileContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ define(['jquery',
configEditor.setValue('');
} else {
requestService('get', 'GetConfigFile', { 'fileName': curConfigName }, function (data) {
if (curConfigName.endsWith("config")) {
configEditor.setOption("mode", "xml");
}
else if (curConfigName.endsWith("txt")) {
configEditor.setOption("mode", "text");
}
configEditor.setValue(data.FileContent);
}, function () {
// failed
Expand Down