package de.greifintegra.kontor.einrichten.zentrale.logik;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class ZentralePhp 
{
	static void erzeugeDatei(String inhalt, String pfadDatei) throws IOException
	{
		BufferedWriter schreiber = new BufferedWriter(new FileWriter(pfadDatei));
		schreiber.write(inhalt.trim());
		schreiber.flush();
		schreiber.close();
	}
	
	static String leseRoot()
	{
		return "<?php\n"+
				"echo $_SERVER['DOCUMENT_ROOT'];\n"+
				"?>";
	}
	
	// mikey_nich (at) hotmáil . com
	static String cyrptPasswort()
	{
		return "<?php\n" +
			   "foreach($_POST as $key => $value)\n" +
			   "{\n" +
			   "$liste[\"$key\"] = $value;\n" +
			   "}\n" +
			   "$plainpasswd=$liste[0];\n" +
			   "$tmp=\"\";\n" +
			   "$salt = substr(str_shuffle(\"abcdefghijklmnopqrstuvwxyz0123456789\"), 0, 8);\n" +
			   "$len = strlen($plainpasswd);\n" +
			   "$text = $plainpasswd.'$apr1$'.$salt;\n" +
			   "$bin = pack(\"H32\", md5($plainpasswd.$salt.$plainpasswd));\n" +
			   "for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); }\n" +
			   "for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $plainpasswd{0}; }\n" +
			   "$bin = pack(\"H32\", md5($text));\n" +
			   "for($i = 0; $i < 1000; $i++)\n" +
			   "{\n" +
			   "$new = ($i & 1) ? $plainpasswd : $bin;\n" +
			   "if ($i % 3) $new .= $salt;\n" +
			   "if ($i % 7) $new .= $plainpasswd;\n" +
			   "$new .= ($i & 1) ? $bin : $plainpasswd;\n" +
			   "$bin = pack(\"H32\", md5($new));\n" +
			   "}\n" +
			   "for ($i = 0; $i < 5; $i++)\n" +
			   "{\n" +
			   "$k = $i + 6;\n" +
			   "$j = $i + 12;\n" +
			   "if ($j == 16) $j = 5;\n" +
			   "$tmp = $bin[$i].$bin[$k].$bin[$j].$tmp;\n" +
			   "}\n" +
			   "$tmp = chr(0).chr(0).$bin[11].$tmp;\n" +
			   "$tmp = strtr(strrev(substr(base64_encode($tmp), 2)),\n" +
			   "\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\",\n" +
			   "\"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\");\n" +
			   "die( \"$\".\"apr1\".\"$\".$salt.\"$\".$tmp);\n" +
			   "?>";
	}
	
	static String userDatei(String nutzerName, String nutzerPasswort, String adminName, String adminPasswort, String zentraleName, String zentralePasswort)
	{
		return nutzerName + ":" + nutzerPasswort + "\n"+
			   adminName + ":" + adminPasswort + "\n" +
			   zentraleName + ":" + zentralePasswort;
	}
	
	static String erzeugeHtaccess(String root, String ordner, String nutzer)
	{
		return "AuthUserFile " + root + "/" + ordner + "/.htuser\n" +
			   "AuthName \"Gesicherter Bereich " + ordner +"\"\n"+
			   "AuthType Basic\n" +
			   "require user " + nutzer;
	}
}
