2
0
Эх сурвалжийг харах

BETA: Fixing issue with GM names and logs.
* Reported by: DareStrike

Zoey76 12 жил өмнө
parent
commit
2f57492f8f

+ 23 - 11
L2J_Server_BETA/java/com/l2jserver/gameserver/util/GMAudit.java

@@ -25,28 +25,40 @@ import java.util.logging.Logger;
 import com.l2jserver.Config;
 import com.l2jserver.util.lib.Log;
 
+/**
+ * Audits Game Master's actions.
+ */
 public class GMAudit
 {
+	private static final Logger _log = Logger.getLogger(Log.class.getName());
+	
 	static
 	{
 		new File("log/GMAudit").mkdirs();
 	}
 	
-	private static final Logger _log = Logger.getLogger(Log.class.getName());
-	
 	/**
-	 * @param gmName
-	 * @param action
-	 * @param target
-	 * @param params
+	 * Logs a Game Master's action into a file.
+	 * @param gmName the Game Master's name
+	 * @param action the performed action
+	 * @param target the target's name
+	 * @param params the parameters
 	 */
 	public static void auditGMAction(String gmName, String action, String target, String params)
 	{
-		final File file = new File("log/GMAudit/" + gmName + ".txt");
 		final SimpleDateFormat _formatter = new SimpleDateFormat("dd/MM/yyyy H:mm:ss");
+		final String date = _formatter.format(new Date());
+		String path = com.l2jserver.util.Util.replaceIllegalCharacters("log/GMAudit/" + gmName + ".txt");
+		if (!com.l2jserver.util.Util.isValidFileName(path))
+		{
+			path = "log/GMAudit/INVALID_GM_NAME_" + date + ".txt";
+		}
+		
+		final File file = new File(path);
+		
 		try (FileWriter save = new FileWriter(file, true))
 		{
-			save.write(_formatter.format(new Date()) + ">" + gmName + ">" + action + ">" + target + ">" + params + Config.EOL);
+			save.write(date + ">" + gmName + ">" + action + ">" + target + ">" + params + Config.EOL);
 		}
 		catch (IOException e)
 		{
@@ -56,9 +68,9 @@ public class GMAudit
 	
 	/**
 	 * Wrapper method.
-	 * @param gmName
-	 * @param action
-	 * @param target
+	 * @param gmName the Game Master's name
+	 * @param action the performed action
+	 * @param target the target's name
 	 */
 	public static void auditGMAction(String gmName, String action, String target)
 	{

+ 56 - 2
L2J_Server_BETA/java/com/l2jserver/util/Util.java

@@ -14,6 +14,8 @@
  */
 package com.l2jserver.util;
 
+import java.io.File;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.net.InetAddress;
@@ -22,13 +24,31 @@ import java.nio.ByteBuffer;
 import java.util.logging.Logger;
 
 /**
- * This class ...
- * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
+ * Useful utilities common to L2J Server.
  */
 public class Util
 {
 	private static final Logger _log = Logger.getLogger(Util.class.getName());
 	
+	private static final char[] ILLEGAL_CHARACTERS =
+	{
+		'/',
+		'\n',
+		'\r',
+		'\t',
+		'\0',
+		'\f',
+		'`',
+		'?',
+		'*',
+		'\\',
+		'<',
+		'>',
+		'|',
+		'\"',
+		':'
+	};
+	
 	/**
 	 * Checks if a host name is internal
 	 * @param host the host name to check
@@ -109,4 +129,38 @@ public class Util
 		t.printStackTrace(new PrintWriter(sw));
 		return sw.toString();
 	}
+	
+	/**
+	 * Replaces most invalid characters for the given string with an underscore.
+	 * @param str the string that may contain invalid characters
+	 * @return the string with invalid character replaced by underscores
+	 */
+	public static String replaceIllegalCharacters(String str)
+	{
+		String valid = str;
+		for (char c : ILLEGAL_CHARACTERS)
+		{
+			valid = valid.replace(c, '_');
+		}
+		return valid;
+	}
+	
+	/**
+	 * Verify if a file name is valid.
+	 * @param name the name of the file
+	 * @return {@code true} if the file name is valid, {@code false} otherwise
+	 */
+	public static boolean isValidFileName(String name)
+	{
+		final File f = new File(name);
+		try
+		{
+			f.getCanonicalPath();
+			return true;
+		}
+		catch (IOException e)
+		{
+			return false;
+		}
+	}
 }