<?php
// This file is part of the Project JJ PHP Chat distribution.
// Created and maintained by Tino Didriksen <td@projectjj.dk>
// The contents of this file is subject to a license.
// Read license.txt and readme.txt for more information.
	include("mysql.php");
	include("setup.php");
	$base = getcwd()."/";

	//echo "$base<p>";

	$nchat		= strtolower(eregi_replace("([^-[:alnum:]_]+)", "", trim($_REQUEST['nchat'])));
	$username	= strtolower(eregi_replace($master_name_filter, "", trim($_REQUEST['username'])));
	$password	= strtolower(eregi_replace("([^[:alnum:]]+)", "", trim($_REQUEST['password'])));
	$email		= strtolower($_REQUEST['email']);

	if (($nchat != "") && ($nchat != "_new") && ($nchat != $master_chat)) {
		if ((strcasecmp($master_name, $_REQUEST['login']) != 0) || (strcmp($master_password, md5($_REQUEST['pass'])) != 0)) {
			die("Unauthorized usage. Please report this to the system admin.<p>");
		}
		ob_start();
		$oldumask = umask(0); 
		
		if (mkdir($base.$nchat, 0777)) {
			echo mkdir($base.$nchat."/jbb", 0777)."<br>\n";
			if (mkdir($base.$nchat."/register", 0777)) {
				echo mkdir($base.$nchat."/register/wizard_locked", 0777)."<br>\n";

				$files = array(
					"/sendmsg.php",
					"/index.php",
					"/login.php",
					"/reader.php",
					"/manual.php",
					"/gui_opt.php",
					"/gui_set.php",
					"/gui_icon.php",
					"/gui_lang.php",
					"/custom.php",
					"/userlist.php",
					"/register/biglist.php",
					"/register/adminlog.php",
					"/register/dblog.php",
					"/register/biglog.php",
					"/register/index.php",
					"/register/login.php",
					"/register/viewer.php",
					"/register/regapp.php",
					"/jbb/index.php"
				);
				
				for ($cc=0;$cc<count($files);$cc++) {
					echo copy($base."_new".$files[$cc], $base.$nchat.$files[$cc])." ".$base.$nchat.$files[$cc]."<br>";
					echo chmod($base.$nchat.$files[$cc], 0666).'<br>';
				}

				unset($files);
				$files = array(
					"/iconlist.php",
					"/settings.php",
					"/options.php"
				);

				for ($cc=0;$cc<count($files);$cc++) {
					echo copy($base."clean".$files[$cc], $base.$nchat.$files[$cc])." ".$base.$nchat.$files[$cc]."<br>";
					echo chmod($base.$nchat.$files[$cc], 0666)." ".$base.$nchat.$files[$cc]."<br>";
				}

				$fm = fopen($base."clean/settings.php", "r");
				$file = fread($fm, filesize($base."clean/settings.php"));
				fclose($fm);

				$fx = fopen($base.$nchat."/settings.php", "w");
				fwrite($fx, stripslashes($file)."\n\$cadmin = \"$email\";\n?>\n");
				fclose($fx);

				$fz = fopen($base.$nchat."/register/motd.dat", "w");
				fwrite($fz, "<center>- <a href=\"jbb/\">Board</a> -</center>");
				fclose($fz);
				chmod($base.$nchat."/register/motd.dat", 0666);

				$db_table = "uo_chat_database";
				$chatpath = "chat".$nchat;
				
				$username = str_replace("_", " ", $username);
				$username = strtolower(eregi_replace($master_name_filter, "", $username));
				$master_name = strtolower(eregi_replace($master_name_filter, "", $master_name));

				$regnotes = "Add comments here. You can for example specify what you want the applicant to write in the description field.";

				mysql_query("INSERT INTO $db_table (chat,username,password,flags,email) VALUES ('$chatpath','$master_name','$master_password','M','$master_email')", $handler);
				mysql_query("INSERT INTO $db_table (chat,username,password,flags,email) VALUES ('$chatpath','$username','".md5($password)."','m','$email')", $handler);
				mysql_query("DELETE FROM uo_chat_last WHERE chat='$chatpath'", $handler);
				mysql_query("INSERT INTO uo_chat_last (chat,utime,email,owner,ctime,regnotes) VALUES ('$chatpath','".time()."','$email','$username','".time()."','$regnotes')", $handler);

				$cpath = "http://pjj.cc";

				$username = ucwords($username);
				$master_name = ucwords($master_name);
				$subject = "pJJ: Chat /$nchat created, ".ucwords($username);
				$message = "Login: $username\n";
				$message .= "Password: $password\n";
				$message .= "\n";
				$message .= "Chat: $cpath/$nchat/\n";
				$message .= "Controlpanel: $cpath/$nchat/register/login.php\n";
				$message .= "Application Form: $cpath/$nchat/register/regapp.php\n";
				$message .= "Settings: $cpath/$nchat/gui_set.php\n";
				$message .= "Options: $cpath/$nchat/gui_opt.php\n";
				$message .= "Icons: $cpath/$nchat/gui_icon.php\n";
				$message .= "Language: $cpath/$nchat/gui_lang.php\n";
				$message .= "\n";
				$message .= "Common Icons: $cpath/common/icon/\n";
				$message .= "Image Service: http://image.projectjj.com/\n";
				$message .= "Preferences Help: $cpath/common/help.php?man=pref\n";
				$message .= "Flags Help: $cpath/common/help.php?man=flag\n";
				$message .= "Chat Pref Help: $cpath/common/help.php?man=chat\n";
				$message .= "Portal: $cpath/\n";
				$message .= "\n";
				$message .= "It is also a good idea to look at http://board.projectjj.com/ to get the latest developments.\n";
				$message .= "\n";
				$message .= "-- Tino Didriksen / Project JJ\n";
				mail("$username <$email>", $subject, $message, "From: $master_name <$master_email>\nReply-To: $master_name <$master_email>\nBcc: $master_name <$master_email>\n");
			}
		}

		umask($oldumask);
		ob_end_clean();
	} else {
		echo "<form method=post action=newchat.php><br>\n";
		echo "Chat: <input type=text name=nchat value=$nchat><br>\n";
		echo "Username: <input type=text name=username value=\"$username\"><br>\n";
		echo "Password: <input type=text name=password value=$password><br>\n";
		echo "Email: <input type=text name=email value=$email><p>\n";

		echo "Login: <input type=text name=login value='$login'><br>";
		echo "Pass: <input type=password name=pass value='$pass'><br>";
		echo "<input type=submit value=Create>\n";
		echo "</form>\n";
	}
?>