##############################################################
## MOD Title: AJAX username verifier
## MOD Author: adrien < webmaster@pc-facile.com > http://www.pc-facile.com
## MOD Description: Esta modificacion aade un campo AJAX para chequear si el nombre de usuario esta disponible o no.
## MOD Version: 1.0.0
## 
## MOD Traduction: Zuker - www.eddb.com.ar
##
## Installation Level: (Intermediate)
## Installation Time: 10 Minutes
## Files To Edit: profile.php,
##                includes/usercp_register.php,
##                lang_main.php
## Included Files: class.sajax.php, profile_add_body.tpl.
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## For security purposes, please check: http://www.phpbb.com/mods/
## for the latest version of this MOD. Although MODs are checked
## before being allowed in the MODs Database there is no guarantee
## that there are no security problems within the MOD. No support
## will be given for MODs not found within the MODs Database which
## can be found at http://www.phpbb.com/mods/
##############################################################
## Author Notes: This mod will allow you to have an AJAX check on the username a user is trying to register.
##               This script is based on www.cyberdummy.co.uk Gmail style check username.
##
##############################################################
## MOD History:
##
##   2006-10-09 - Version 1.0.0
##      - First public version
##
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#-----[ COPY ]------------------------------------------
#
copy class.sajax.php to includes/class.sajax.php
copy profile_add_body.tpl to templates/subSilver/profile_add_body.tpl

#
#-----[ OPEN ]------------------------------------------
#
profile.php

#
#-----[ FIND ]------------------------------------------
#
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

#
#-----[ AFTER, ADD ]------------------------------------------
#
include($phpbb_root_path . 'includes/class.sajax.'.$phpEx);

sajax_init(); // Intialize Sajax
sajax_export("check_user_exist"); // Register the function
sajax_handle_client_request(); // Serve client instances

#
#-----[ FIND ]------------------------------------------
#
redirect(append_sid("index.$phpEx", true));

#
#-----[ AFTER, ADD ]------------------------------------------
#
function check_user_exist ($username)
{
	global $db;

	// Make a list of words to postfix on username for suggest
	$suggest = array('007', '1', 'x', 'xxx', '2000', '2007');

	$username = mysql_escape_string ($username);
	$sql = "SELECT username FROM " . USERS_TABLE . " WHERE username = '" . $username . "'";
	if (!($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'Could not obtain confirmation code', __LINE__, __FILE__, $sql);
	}

	$list = array();
	while ( $row = $db->sql_fetchrow($result) )
	{
		$list[] = $row;
	}
	$db->sql_freeresult($result);

	if ( count ($list) > 0)
	{
		// Username not available
		$avail[0] = 'no';
		$i = 2;
		// Loop through suggested ones checking them
		foreach ($suggest AS $postfix)
		{
			$sql = "SELECT username FROM " . USERS_TABLE . " WHERE username = '" . $username . $postfix . "'";
			if (!($result_2 = $db->sql_query($sql)))
			{
				message_die(GENERAL_ERROR, 'Could not obtain confirmation code', __LINE__, __FILE__, $sql);
			}

			$user = $db->sql_fetchrow($result_2);

			if ( empty ($user) )
			{
				$avail[$i] = $username . $postfix;
				$i++;
			}
		}
		$avail[1] = $i - 1;
		return ($avail);
	}
	// Username is available
	return array('yes');
}

#
#-----[ OPEN ]------------------------------------------
#
includes/usercp_register.php

#
#-----[ FIND ]------------------------------------------
#
		'S_FORM_ENCTYPE' => $form_enctype,
		'S_PROFILE_ACTION' => append_sid("profile.$phpEx"))
	);
	
#
#-----[ AFTER, ADD ]------------------------------------------
#
	$template->assign_vars(array(
		'L_USERNAME_AVAILABLE' => $lang['Username_available'],
		'L_VERIFY_USERNAME' => $lang['Verify_username'],
		'TRY_ONE_OF_THESE' => $lang['Try_one_these'],
		'SAJAX_JAVASCRIPT' => sajax_get_javascript()
	));

#
#-----[ OPEN ]------------------------------------------
#
lang_main.php

#
#-----[ FIND ]------------------------------------------
#
$lang['Empty_message_email'] =

#
#-----[ AFTER, ADD ]------------------------------------------
#
$lang['Verify_username'] = "Chequear disponibilidad";
$lang['Username_available'] = "Disponible";
$lang['Try_one_these'] = "Ya utilizado, pruebe alguno de estos:";

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM