Skip to content

Commit

Permalink
Adds support for editing the robots.txt on a website
Browse files Browse the repository at this point in the history
  • Loading branch information
WillStrohl committed Oct 9, 2020
1 parent 7c007a2 commit c424036
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
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)
{
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
.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

0 comments on commit c424036

Please sign in to comment.