# HG changeset patch
# User Administrator@buildbox.tempestintheaether.org
# Date 1330587702 28800
# Node ID 2cb95cfee2f75c42a3dd3677e740cd9ca8527b2e
# Parent  855a9dd6241e4ec9fdb97566e62aa5a52e490107
Added account creation php scripts. Client also now works.

diff -r 855a9dd6241e -r 2cb95cfee2f7 code/ryzom/tools/server/www/login/accountfunc.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/code/ryzom/tools/server/www/login/accountfunc.php	Wed Feb 29 23:41:42 2012 -0800
@@ -0,0 +1,300 @@
+<?PHP
+
+
+function validEmail($email)
+{
+   $isValid = true;
+   $atIndex = strrpos($email, "@");
+   if (is_bool($atIndex) && !$atIndex)
+   {
+      $isValid = false;
+   }
+   else
+   {
+      $domain = substr($email, $atIndex+1);
+      $local = substr($email, 0, $atIndex);
+      $localLen = strlen($local);
+      $domainLen = strlen($domain);
+      if ($localLen < 1 || $localLen > 64)
+      {
+         // local part length exceeded
+         $isValid = false;
+      }
+      else if ($domainLen < 1 || $domainLen > 255)
+      {
+         // domain part length exceeded
+         $isValid = false;
+      }
+      else if ($local[0] == '.' || $local[$localLen-1] == '.')
+      {
+         // local part starts or ends with '.'
+         $isValid = false;
+      }
+      else if (preg_match('/\\.\\./', $local))
+      {
+         // local part has two consecutive dots
+         $isValid = false;
+      }
+      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
+      {
+         // character not valid in domain part
+         $isValid = false;
+      }
+      else if (preg_match('/\\.\\./', $domain))
+      {
+         // domain part has two consecutive dots
+         $isValid = false;
+      }
+      else if
+(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
+                 str_replace("\\\\","",$local)))
+      {
+         // character not valid in local part unless 
+         // local part is quoted
+         if (!preg_match('/^"(\\\\"|[^"])+"$/',
+             str_replace("\\\\","",$local)))
+         {
+            $isValid = false;
+         }
+      }
+      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
+      {
+         // domain not found in DNS
+         $isValid = false;
+      }
+   }
+   return $isValid;
+}
+
+function generateSALT ($length = 2)
+  {
+
+    // start with a blank salt
+    $salt = "";
+
+    // define possible characters - any character in this string can be
+    // picked for use in the salt, so if you want to put vowels back in
+    // or add special characters such as exclamation marks, this is where
+    // you should do it
+    $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
+
+    // we refer to the length of $possible a few times, so let's grab it now
+    $maxlength = strlen($possible);
+  
+    // check for length overflow and truncate if necessary
+    if ($length > $maxlength) {
+      $length = $maxlength;
+    }
+	
+    // set up a counter for how many characters are in the salt so far
+    $i = 0; 
+    
+    // add random characters to $salt until $length is reached
+    while ($i < $length) { 
+
+      // pick a random character from the possible ones
+      $char = substr($possible, mt_rand(0, $maxlength-1), 1);
+        
+      // have we already used this character in $salt?
+      if (!strstr($salt, $char)) { 
+        // no, so it's OK to add it onto the end of whatever we've already got...
+        $salt .= $char;
+        // ... and increase the counter by one
+        $i++;
+      }
+
+    }
+
+    // done!
+    return $salt;
+
+  }
+function do_encrypt($pass,$user)
+{
+    $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
+    
+    $iv_size = mcrypt_enc_get_iv_size($cipher);
+    //printf("iv_size = %d\n", $iv_size);
+    
+    //must take 16 byte key
+	$key128 = 'fmaksdmf44Afwgnk';
+    
+    $iv = 'ag0ka93kAsfewA3';
+    
+    //printf("iv: %s\n", bin2hex($iv));
+    //printf("key128: %s\n", bin2hex($key128));
+    
+    // This is the plain-text to be encrypted:
+    $cleartext = $pass.$pass.$user.$user;
+    //printf("plainText: %s\n\n", $cleartext);
+    
+    // Now let's do 128-bit encryption:
+    if (mcrypt_generic_init($cipher, $key128, $iv) != -1) {
+        // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
+        $cipherText = mcrypt_generic($cipher, $cleartext);
+        mcrypt_generic_deinit($cipher);
+        
+        // Display the result in hex.
+        //printf("128-bit encrypted result:\n%s\n\n", bin2hex($cipherText));
+    }
+	return bin2hex($cipherText);
+}
+
+function checkUser () {
+	if (isset($_POST["Username"])) {
+		if (strlen($_POST["Username"])>12) {
+			return  "Username must be no more than 12 characters.";
+			} elseif (strlen($_POST["Username"])<5) {
+			return "Username must be 5 or more characters.";
+			} elseif (!preg_match('/^[a-z0-9\.]*$/', $_POST["Username"])) {
+			return "Username can only contain numbers and letters.";
+			} else {
+			return "success";
+		}
+	} else {
+		return "success";
+	}
+}
+
+function checkPassword () {
+	if (isset($_POST["Password"])) {
+		if (strlen($_POST["Password"])>8) {
+			return "Password must be no more than 8 characters.";
+			} elseif (strlen($_POST["Password"])<5) {
+			return "Password must be more than 5 characters.";
+			} else {
+			return "success";
+		}
+	}
+}
+
+function confirmPassword () {
+	if (($_POST["Password"]) != ($_POST["ConfirmPass"])) {
+			return "Passwords do not match.";
+		} else {
+			return "success";
+		}
+}
+
+
+function checkEmail () {
+	if (isset($_POST["Email"])) {
+		if (!validEmail($_POST["Email"])) {
+			return "Email address is not valid.";
+		} else {
+			return "success";
+		}
+	} else {
+		return "success";
+	}
+}
+
+	// see errorMsg
+	function errorMsgBlock($errNum=GENERIC_ERROR_NUM) // $mixedArgs
+	{
+		$args = func_get_args();
+		return '0:'.call_user_func_array('errorMsg', $args);
+	}
+
+function createUser () {
+    $salt = generateSALT();
+    $login = $_POST["Username"];
+    $password = crypt($_POST["Password"], $salt);
+    $email = $_POST["Email"];
+	global $DBHost;
+	global $DBHost;
+	global $DBUserName;
+	global $DBPassword;
+	global $DBName;
+
+	global $RingDBUserName;
+	global $RingDBName;
+	global $RingDBPassword;
+	
+    $link = mysql_connect($DBHost, $DBUserName, $DBPassword) or die (errorMsgBlock(3004, 'main', $DBHost, $DBUserName));
+	mysql_select_db ($DBName) or die (errorMsgBlock(3005, 'main', $DBName, $DBHost, $DBUserName));
+    
+    // login doesn't exist, create it
+    $query = "INSERT INTO user (Login, Password, Email) VALUES ('$login', '$password', '$email')";
+    $result = mysql_query ($query) or die (errorMsgBlock(2005, 'main', $DBHost, $DBUserName));
+    
+    // get the user to have his UId
+    $query = "SELECT * FROM user WHERE Login='$login'";
+    $result = mysql_query ($query) or die (errorMsgBlock(2004, $query, 'main', $DBName, $DBHost, $DBUserName, mysql_error()));
+    
+    
+    if (mysql_num_rows ($result) == 1)
+    {
+        $reason = errorMsg(3008, $login);
+        $row = mysql_fetch_array ($result);
+        $id = $row["UId"];
+        $priv = $row["Privilege"];
+        $extended = $row["ExtendedPrivilege"];
+        
+        // add the default permissions
+        $query = "INSERT INTO permission (UId, ClientApplication, AccessPrivilege) VALUES ('$id', 'r2', 'OPEN')";
+        $result = mysql_query ($query) or die (errorMsgBlock(3006, $query, 'main', $DBName, $DBHost, $DBUserName, mysql_error()));
+        $query = "INSERT INTO permission (UId, ClientApplication, AccessPrivilege) VALUES ('$id', 'ryzom_open', 'OPEN')";
+        $result = mysql_query ($query) or die (errorMsgBlock(3006, $query, 'main', $DBName, $DBHost, $DBUserName, mysql_error()));
+        
+        $res = false;
+        return true;
+    }
+} 
+
+function webpageSuccess () {
+	echo '
+	<html>
+		<head>
+						  <link rel="shortcut icon" href="http://www.tempestintheaether.org/templates/rhuk_milkyway_ta/favicon.ico">
+						  <link rel="stylesheet" href="style.css" type="text/css">
+
+						  <title>Tempest in the Aether</title>
+						  
+
+						  
+							
+						</head>
+
+		<body>
+						  <table width="100%" cellspacing="0" cellpadding="0">
+							<tbody>
+							  <tr>
+								<td align="center" valign="top">
+								  <div id="main">
+
+									<br>
+
+									<div style="text-align:right">
+									  <a href="http://www.tempestintheaether.org"><img border="0" src="http://www.tempestintheaether.org/templates/rhuk_milkyway_ta/images/ta_logo.png"></a><br>
+									  </div>
+
+									<div id="top"></div>
+
+									<div id="middle">
+									  <div class="title">
+										<br />
+										TEMPEST IN THE AETHER REGISTRATION<br />
+									  </div>
+
+									  <div>
+									  <br>
+										Congratulations!<br><br />
+										Your account is now registered! <a href="http://www.tempestintheaether.org/index.php/download">Click HERE to download the client.</a><br>
+									  </div>
+									  <br>
+									  <div style="clear:both;"></div>
+									</div>
+									<div id="bottom"></div>
+								  </div>
+								</td>
+							  </tr>
+							</tbody>
+						  </table>
+
+						
+						
+				</body>
+	</html>';
+	exit;
+}
diff -r 855a9dd6241e -r 2cb95cfee2f7 code/ryzom/tools/server/www/login/config.php
--- a/code/ryzom/tools/server/www/login/config.php	Sun Feb 19 03:15:05 2012 -0800
+++ b/code/ryzom/tools/server/www/login/config.php	Wed Feb 29 23:41:42 2012 -0800
@@ -20,8 +20,8 @@
 
 // If true, the server will add automatically unknown user in the database
 // (in nel.user, nel.permission, ring.ring_user and ring.characters
-$AcceptUnknownUser = true;
+$AcceptUnknownUser = false;
 // if true, the login service automaticaly create a ring user and a editor character if needed
-$AutoCreateRingInfo = true;
+$AutoCreateRingInfo = false;
 
 ?>
\ No newline at end of file
diff -r 855a9dd6241e -r 2cb95cfee2f7 code/ryzom/tools/server/www/login/register.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/code/ryzom/tools/server/www/login/register.php	Wed Feb 29 23:41:42 2012 -0800
@@ -0,0 +1,327 @@
+<?PHP
+
+	error_reporting(E_ERROR | E_PARSE);
+	set_error_handler('err_callback');
+
+	// For error handling, buffer all output
+	ob_start('ob_callback_r2login');
+
+	include_once('accountfunc.php');
+	include_once('config.php');
+	include_once('login_translations.php');
+	include_once('../tools/nel_message.php');
+	include_once('../tools/domain_info.php');
+	include_once('login_service_itf.php');
+	include_once('../ring/join_shard.php');
+		//check values
+		$user = checkUser();
+		$pass = checkPassword();
+		$cpass = confirmPassword();
+		$email = checkEmail();
+		
+		//if all are good then create user
+		if (($user == "success") and ($pass == "success") and ($cpass == "success") and ($email == "success") and (isset($_POST["TaC"]))) {
+			if (createUser()) {
+				//if created user suceeds then check if webpage or client
+				if (!isset($_SERVER[HTTP_USER_AGENT])) {
+					//header location is required to get contragulations screen
+					header('Location: email_sent.php');
+					exit;
+				} else {
+					//show congragulations page
+					webpageSuccess();
+					exit;
+				}
+			}	
+		} else {
+			//check if the client is the request
+			if (!isset($_SERVER[HTTP_USER_AGENT])) {
+				echo '
+				<div class="title">TEMPEST IN THE AETHER REGISTRATION</div>
+				
+				<div>Welcome! Please fill in the following fields to get your new Tempest in the Aether account:</div>
+				
+				<form name=\'Page1\' method=\'post\' action=\'register.php\'>
+				
+				<table>
+					<tr>
+						<td width="33%" ';
+												if ($user != "success") {
+													echo 'class="error" ';
+												}
+												echo 'id="caption-Username" >Desired Username: </td>
+						<td width="25%" >
+							<input type=\'text\' name=\'Username\' value="" maxlength="12" onfocus="javascript:showTooltip(\'5-12 lower-case characters and numbers. The login (username) you create here will be your login name. The name of your game characters will be chosen later on.\', this);" /></td>
+						';
+												if ($user != "success") {
+													echo '<td id="comment-Username" class="error" width="42%">'.$user.'</td>';
+												} else {
+													'<td width="42%" id="comment-Username" >';
+												}
+												echo '</td>
+					</tr>
+					
+					<tr>
+						<td width="33%" ';
+												if ($pass != "success") {
+													echo 'class="error" ';
+												}
+												echo 'id="caption-Password" >Desired Password: </td>
+						<td width="25%" ><input type=\'password\' name=\'Password\' value="" maxlength="8" onkeyup="testPassword(document.Page1.Password.value, \'comment-Password\')" onfocus="javascript:showTooltip(\' 5-8 alpha-numerical characters.\', this);" /></td>
+						';
+												if ($pass != "success") {
+													echo '<td id="comment-Password" class="error" width="42%">'.$pass.'</td>';
+												} else {
+													'<td width="42%" id="comment-Password" >';
+												}
+												echo '
+					</tr>
+					
+					<tr>
+						<td width="33%" ';
+												if ($cpass != "success") {
+													echo 'class="error" ';
+												}
+												echo 'id="caption-ConfirmPass" >Confirm Password: </td>
+						<td width="25%" >
+						<input type=\'password\' name=\'ConfirmPass\' value="" maxlength="8" onfocus="javascript:showTooltip(\'Retype your Password\', this);" />
+						</td>
+						';
+												if ($cpass != "success") {
+													echo '<td id="comment-ConfirmPass" class="error" width="42%">'.$cpass.'</td>';
+												} else {
+													'<td width="42%" id="comment-ConfirmPass" >';
+												}
+												echo '
+						</td>
+					</tr>
+					
+					<tr>
+						<td width="33%" ';
+												if ($email != "success") {
+													echo 'class="error" ';
+												}
+												echo 'id="caption-Email" >Email Address (to which a confirmation email will be sent): 
+						</td><td width="25%" ><input type=\'text\' name=\'Email\' value="" maxlength="255" onfocus="javascript:showTooltip(\'Please verify that the e-mail address you enter here is valid and will remain valid in the future. It will only be used to manage your Tempest in the Aether account.\', this);" /></td>';
+												if ($email != "success") {
+													echo '<td id="comment-Email" class="error" width="42%">'.$email.'</td>';
+												} else {
+													'<td width="42%" id="comment-Email" >';
+												}
+												echo '</td>
+					</tr>
+						
+					<tr><td width="33%" ';
+												if (!isset($_POST["TaC"])) {
+													echo 'class="error" ';
+												}
+												echo 'colspan=2 ><input type=\'checkbox\' name=\'TaC\' value="1"  onfocus="javascript:showTooltip(\'\', this);" /><span id="caption-TaC">YES, I agree to the terms of use</span></td>';
+												if (!isset($_POST["TaC"])) {
+													echo '<td id="comment-TaC" class="error" width="42%">You must accept the Terms of Service</td>';
+												} else {
+													'<td width="42%" id="comment-TaC" >';
+												}
+												echo '</td>
+					</tr>
+				
+				</table>
+				
+					<div style=\'text-align:left; padding-top:0.5em; padding-bottom:0.5em;\'>
+						<input type=\'submit\' name=\'Submit\' value=\'Continue\' />	
+					</div>
+				
+				</form>
+				
+				<div id=signupTooltip style=\'border: 1px inset white;\'></div>
+				<div id=tooltip-Username>5-12 lower-case characters and numbers. The login (username) you create here will be your login name. The name of your game characters will be chosen later on.</div>
+				<div id=tooltip-Password>5-8 alpha-numerical characters.</div>
+				<div id=tooltip-ConfirmPass>Retype your Password</div>
+				<div id=tooltip-Email>Please verify that the e-mail address you enter here is valid and will remain valid in the future. It will only be used to manage your Tempest in the Aether account.</div>
+				<div id=tooltip-TaC></div>';
+			} else {
+				//must be webpage so display webpage
+				echo '
+					<html xmlns="http://www.w3.org/1999/xhtml">
+						<head>
+						  <meta name="generator" content=
+						  "HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />
+						  <link href="http://www.tempestintheaether.org/templates/rhuk_milkyway_ta/favicon.ico"
+						  rel="shortcut icon" />
+						  <link type="text/css" href="style.css" rel=
+						  "stylesheet" />
+
+						  <title>Tempest in the Aether</title>
+						  <style type="text/css">
+						/*<![CDATA[*/
+								.error { color: red; }
+						  .title { padding: 0.5em; text-align: center; font-weight: bold; font-size: small; }
+						  /*]]>*/
+						  </style>
+
+						  <script language="JavaScript">
+							function showTooltip(text, ctrl) {
+								document.getElementById("signupTooltip").innerHTML = text;
+							}
+							function setLogin(name)	{
+								document.Page1.Username.value = name;
+							}
+							function focusCtrl(ctrlName) {
+								document.getElementsByName(ctrlName)[0].focus();
+							}
+							var VerdictStr = "<font color=#000000>Strength: </font>";
+							</script>
+							
+						</head>
+
+						<body>
+						  <table width="100%" cellspacing="0" cellpadding="0">
+							<tbody>
+							  <tr>
+								<td align="center" valign="top">
+								  <div id="main">
+
+									<br />
+
+									<div style="text-align:right">
+									  <a href="http://www.tempestintheaether.org"><img border="0" src=
+									  "http://www.tempestintheaether.org/templates/rhuk_milkyway_ta/images/ta_logo.png" /></a><br />
+									  </div>
+
+									<div id="top"></div>
+
+									<div id="middle">
+									  <div class="title">
+
+										TEMPEST IN THE AETHER REGISTRATION
+									  </div>
+
+									  <div>
+										Welcome!<br />
+										Please fill in the following fields to get your new Tempest in the Aether
+										account:<br />
+									  </div>
+
+									  <form action="register.php" method="post" name="Page1" id="Page1">
+
+										<table>
+										  <tbody>
+											<tr>
+											  <td width="33%" id="caption-Username">Desired Username:</td>
+
+											  <td width="25%"><input type="text" onfocus=
+											  "javascript:showTooltip(\'&lt;b&gt;Desired Username: &lt;/b&gt;&lt;br&gt;5-12 lower-case characters and numbers.&lt;br&gt; The login (username) you create here will be only used to login to the client. The name of your game characters will be chosen later on. \', this);"
+											  maxlength="12" value="'.$_POST["Username"].'" name="Username" /></td>
+											  ';
+												if ($user != "success") {
+													echo '<td width="42%" class="error" id="comment-Username">'.$user.'<br></td>';
+												}
+												echo '
+
+											  <td width="42%" id="comment-Username"></td>
+											</tr>
+
+											<tr>
+											  <td width="33%" id="caption-Password">Desired Password:</td>
+
+											  <td width="25%"><input type="password" onfocus=
+											  "javascript:showTooltip(\'&lt;b&gt;Desired Password: &lt;/b&gt;&lt;br&gt; 5-8 alpha-numerical characters.\', this);"
+											  onkeyup=
+											  "testPassword(document.Page1.Password.value, \'comment-Password\')"
+											  maxlength="8" value="" name="Password" /></td>';
+												if ($pass != "success") {
+													echo '<td width="42%" class="error" id="comment-Password">'.$pass.'<br></td>';
+												}
+												echo '
+
+											  <td width="42%" id="comment-Password"></td>
+											  
+											</tr>
+
+											<tr>
+
+											  <td width="33%" id="caption-ConfirmPass">Confirm Password:</td>
+
+											  <td width="25%"><input type="password" onfocus=
+											  "javascript:showTooltip(\'&lt;b&gt;Confirm Password: &lt;/b&gt;&lt;br&gt; Please type your password again.\', this);"
+											  maxlength="8" value="" name="ConfirmPass" /></td>
+
+											  <td width="42%" id="comment-ConfirmPass"></td>
+											</tr>
+
+											<tr>
+											  <td width="33%" id="caption-Email">Email Address (to which a
+											  confirmation email will be sent):</td>
+
+											  <td width="25%"><input type="text" onfocus=
+											  "javascript:showTooltip(\'&lt;b&gt;Email Address (to which a confirmation email will be sent): &lt;/b&gt;&lt;br&gt;Please verify that the e-mail address you enter here is valid and will remain valid in the future (caution with full mailboxes and with webmails that disable your address if you dont connect often enough). It will only be used to manage your Ryzom subscription.&lt;br&gt;&lt;a href=\'javascript:popUpExt(&quot;terms_of_use.php?privacy=1&quot;)\'&gt;More information about your privacy&lt;/a&gt;\', this);"
+											  maxlength="255" value="'.$_POST["Email"].'" name="Email" /></td>
+											  ';
+												if ($email != "success") {
+													echo '<td width="42%" class="error" id="comment-Email">'.$email.'<br></td>';
+												}
+												echo '
+
+											  <td width="42%" id="comment-Email"></td>
+											</tr>
+											<tr>
+											
+												<td width="33%" colspan=2 >
+													<input type="checkbox" name="TaC" value="1"  onfocus="javascript:showTooltip(\'\', this);" ';
+												if ((isset($_POST["TaC"])) and (isset($_POST["Submit"]))) {
+													echo 'checked="checked"';
+												}
+												echo '
+													
+													/>
+													<span id="caption-TaC">YES, I agree to the terms of use';
+												if ((!isset($_POST["TaC"])) and (isset($_POST["Submit"]))) {
+													echo '<br /><td class="error">													
+													You must agree to the Terms of Use</td>';
+												}
+												echo '
+													</span>
+													
+													</td><td width="25%" id="comment-TaC" >
+												</td>
+												
+											
+										</tr>
+
+										  </tbody>
+										</table>
+
+										<div style="text-align:left; padding-top:0.5em; padding-bottom:0.5em;">
+										  <input type="submit" value="Continue" name="Submit" />
+
+										</div>
+									  </form>
+
+									  <div style="border: 1px inset white;" id="signupTooltip">
+										<b>Desired Username:</b><br />
+										5-12 lower-case characters and numbers.<br />
+										The login (username) you create here will be your public name in the
+										official forum and chat ("Klients"). The name of your game characters
+										will be chosen later on.
+									  </div>
+									  
+									  <br />
+
+									  <div style="clear:both;"></div>
+									</div>
+
+									<div id="bottom"></div>
+								  </div>
+								</td>
+							  </tr>
+							</tbody>
+						  </table>
+
+						</body>
+						</html>
+				';			
+
+			}
+		}
+	
+	
+?>
\ No newline at end of file
