diff --git a/MCGalaxy/Commands/Information/CmdHelp.cs b/MCGalaxy/Commands/Information/CmdHelp.cs index d15e13839..6d41d465e 100644 --- a/MCGalaxy/Commands/Information/CmdHelp.cs +++ b/MCGalaxy/Commands/Information/CmdHelp.cs @@ -64,7 +64,7 @@ namespace MCGalaxy.Commands.Info { string undoTime = grp.MaxUndo.Shorten(true, false); p.Message("{0} %S- Draw: {1}, Undo: {2}, Perm: {3}", - grp.ColoredName, grp.DrawLimit, undoTime, (int)grp.Permission); + grp.ColoredName, grp.DrawLimit, undoTime, (int)grp.Permission); } } diff --git a/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs b/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs index 63c7d86ab..21187e4f7 100644 --- a/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs +++ b/MCGalaxy/Commands/Moderation/CmdRestoreSelection.cs @@ -34,7 +34,7 @@ namespace MCGalaxy.Commands.Moderation { public override void Use(Player p, string message, CommandData data) { if (message.Length == 0) { Help(p); return; } - if (!Formatter.ValidName(p, message, "level")) return; + if (!Formatter.ValidMapName(p, message)) return; string path = LevelInfo.BackupFilePath(p.level.name, message); if (File.Exists(path)) { diff --git a/MCGalaxy/Commands/World/CmdCopyLVL.cs b/MCGalaxy/Commands/World/CmdCopyLVL.cs index 096485323..6678a0b0b 100644 --- a/MCGalaxy/Commands/World/CmdCopyLVL.cs +++ b/MCGalaxy/Commands/World/CmdCopyLVL.cs @@ -43,7 +43,7 @@ namespace MCGalaxy.Commands.World { if (!LevelInfo.Check(p, data.Rank, src, "copy this map")) return; string dst = args[1]; - if (!Formatter.ValidName(p, dst, "level")) return; + if (!Formatter.ValidMapName(p, dst)) return; if (LevelInfo.MapExists(dst)) { p.Message("Level \"" + dst + "\" already exists."); return; } try { diff --git a/MCGalaxy/Commands/World/CmdDeleteLvl.cs b/MCGalaxy/Commands/World/CmdDeleteLvl.cs index b3f50c1bc..9a29cda30 100644 --- a/MCGalaxy/Commands/World/CmdDeleteLvl.cs +++ b/MCGalaxy/Commands/World/CmdDeleteLvl.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.World { public override void Use(Player p, string message, CommandData data) { if (message.Length == 0 || message.SplitSpaces().Length > 1) { Help(p); return; } - if (!Formatter.ValidName(p, message, "level")) return; + if (!Formatter.ValidMapName(p, message)) return; string map = Matcher.FindMaps(p, message); if (map == null) return; diff --git a/MCGalaxy/Commands/World/CmdGoto.cs b/MCGalaxy/Commands/World/CmdGoto.cs index 572ac173b..c4a605a1b 100644 --- a/MCGalaxy/Commands/World/CmdGoto.cs +++ b/MCGalaxy/Commands/World/CmdGoto.cs @@ -54,7 +54,7 @@ namespace MCGalaxy.Commands.World { } PlayerActions.ChangeMap(p, map); - } else if (Formatter.ValidName(p, message, "level")) { + } else if (Formatter.ValidMapName(p, message)) { PlayerActions.ChangeMap(p, message); } } diff --git a/MCGalaxy/Commands/World/CmdImport.cs b/MCGalaxy/Commands/World/CmdImport.cs index e93a2190b..0a0a797f9 100644 --- a/MCGalaxy/Commands/World/CmdImport.cs +++ b/MCGalaxy/Commands/World/CmdImport.cs @@ -27,7 +27,7 @@ namespace MCGalaxy.Commands.World { public override void Use(Player p, string message, CommandData data) { if (message.Length == 0) { Help(p); return; } - if (!Formatter.ValidName(p, message, "level")) return; + if (!Formatter.ValidMapName(p, message)) return; string path = Paths.ImportsDir + message; if (!Directory.Exists(Paths.ImportsDir)) { diff --git a/MCGalaxy/Commands/World/CmdLockdown.cs b/MCGalaxy/Commands/World/CmdLockdown.cs index 3180c4122..c5822cb5a 100644 --- a/MCGalaxy/Commands/World/CmdLockdown.cs +++ b/MCGalaxy/Commands/World/CmdLockdown.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.World { public override void Use(Player p, string map, CommandData data) { if (map.Length == 0) { Help(p); return; } - if (!Formatter.ValidName(p, map, "level")) return; + if (!Formatter.ValidMapName(p, map)) return; map = Matcher.FindMaps(p, map); if (map == null) return; diff --git a/MCGalaxy/Commands/World/CmdMain.cs b/MCGalaxy/Commands/World/CmdMain.cs index f40863454..e549a1759 100644 --- a/MCGalaxy/Commands/World/CmdMain.cs +++ b/MCGalaxy/Commands/World/CmdMain.cs @@ -38,7 +38,7 @@ namespace MCGalaxy.Commands.World { } } else { if (!CheckExtraPerm(p, data, 1)) return; - if (!Formatter.ValidName(p, message, "level")) return; + if (!Formatter.ValidMapName(p, message)) return; if (!LevelInfo.Check(p, data.Rank, Server.mainLevel, "set main to another map")) return; string map = Matcher.FindMaps(p, message); diff --git a/MCGalaxy/Commands/World/CmdNewLvl.cs b/MCGalaxy/Commands/World/CmdNewLvl.cs index f9ce7651a..7015d75f1 100644 --- a/MCGalaxy/Commands/World/CmdNewLvl.cs +++ b/MCGalaxy/Commands/World/CmdNewLvl.cs @@ -55,7 +55,7 @@ namespace MCGalaxy.Commands.World { if (!GetDimensions(p, args, 1, ref x, ref y, ref z)) return null; string seed = args.Length == 6 ? args[5] : ""; - if (!Formatter.ValidName(p, name, "level")) return null; + if (!Formatter.ValidMapName(p, name)) return null; if (LevelInfo.MapExists(name)) { p.Message("Level \"{0}\" already exists", name); return null; } diff --git a/MCGalaxy/Commands/World/CmdRenameLvl.cs b/MCGalaxy/Commands/World/CmdRenameLvl.cs index efca89ffd..b7fdee4b5 100644 --- a/MCGalaxy/Commands/World/CmdRenameLvl.cs +++ b/MCGalaxy/Commands/World/CmdRenameLvl.cs @@ -35,7 +35,7 @@ namespace MCGalaxy.Commands.World { Level lvl = Matcher.FindLevels(p, args[0]); if (lvl == null) return; string newMap = args[1].ToLower(); - if (!Formatter.ValidName(p, newMap, "level")) return; + if (!Formatter.ValidMapName(p, newMap)) return; if (LevelInfo.MapExists(newMap)) { p.Message("Level already exists."); return; } if (lvl == Server.mainLevel) { p.Message("Cannot rename the main level."); return; } diff --git a/MCGalaxy/Database/IDatabaseBackend.cs b/MCGalaxy/Database/IDatabaseBackend.cs index 53d246ac6..767fc4ce8 100644 --- a/MCGalaxy/Database/IDatabaseBackend.cs +++ b/MCGalaxy/Database/IDatabaseBackend.cs @@ -160,13 +160,17 @@ namespace MCGalaxy.SQL { Database.Execute(sql.ToString(), args); } + internal static bool ValidNameChar(char c) { + return + c > ' ' && c != '"' && c != '%' && c != '&' && + c != '\'' && c != '*' && c != '/' && c != ':' && + c != '<' && c != '>' && c != '?' && c != '\\' && + c != '`' && c != '|' && c <= '~'; + } + protected static void ValidateTable(string name) { foreach (char c in name) { - if (c >= '0' && c <= '9') continue; - if (c >= 'a' && c <= 'z') continue; - if (c >= 'A' && c <= 'Z') continue; - if (c == '+' || c == '_' || c == '@' || c == '-' || c == '.') continue; - + if (ValidNameChar(c)) continue; throw new ArgumentException("Invalid character in table name: " + c); } } diff --git a/MCGalaxy/Levels/LevelInfo.cs b/MCGalaxy/Levels/LevelInfo.cs index ed091cd49..a34853b3f 100644 --- a/MCGalaxy/Levels/LevelInfo.cs +++ b/MCGalaxy/Levels/LevelInfo.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.IO; using MCGalaxy.Events.LevelEvents; +using MCGalaxy.SQL; namespace MCGalaxy { @@ -152,6 +153,13 @@ namespace MCGalaxy { return true; } + public static bool ValidName(string map) { + foreach (char c in map) { + if (!IDatabaseBackend.ValidNameChar(c)) return false; + } + return true; + } + public static bool IsRealmOwner(string name, string map) { Level lvl = null; LevelConfig cfg = GetConfig(map, out lvl); diff --git a/MCGalaxy/util/Formatting/Formatter.cs b/MCGalaxy/util/Formatting/Formatter.cs index 594f39d9e..16ba94d9f 100644 --- a/MCGalaxy/util/Formatting/Formatter.cs +++ b/MCGalaxy/util/Formatting/Formatter.cs @@ -78,5 +78,11 @@ namespace MCGalaxy { p.Message("\"{0}\" is not a valid {1} name.", name, type); return false; } + + public static bool ValidMapName(Player p, string name) { + if (LevelInfo.ValidName(name)) return true; + p.Message("\"{0}\" is not a valid level name.", name); + return false; + } } } diff --git a/MCGalaxy/util/Formatting/Matcher.cs b/MCGalaxy/util/Formatting/Matcher.cs index 3f1969814..2d1de0ecc 100644 --- a/MCGalaxy/util/Formatting/Matcher.cs +++ b/MCGalaxy/util/Formatting/Matcher.cs @@ -60,7 +60,7 @@ namespace MCGalaxy { /// Find partial matches of 'name' against the list of all map files. public static string FindMaps(Player pl, string name) { - if (!Formatter.ValidName(pl, name, "level")) return null; + if (!Formatter.ValidMapName(pl, name)) return null; int matches; return Find(pl, name, out matches, LevelInfo.AllMapNames(), null, l => l, "levels", 10);