Is my 1st Ajax code. Is for suggest username to user for register new name in gallery.
1 - Is check for exist username.
2 - Is check for filter username.
3 - Is check for exist index name (all lang is use.
).
1 - If username exist - message is show (red) - say is no possible for use (exist in DB).
2 - If name find in filter (you add) - is no possible for use (reset form).
3 - If enter suggest name in lang array, is show to user for name of use (check with exist name in DB.
).
This MOD is work with page header - is no problem. Is use only for register form (test in:
4images v1.74).
// Step 1
In register.php file,
find:
if ($user_info['user_level'] != GUEST && $action != "activate") {
show_error_page($lang['already_registered']);
}
$content = "";
add after:
if ($action == "register_suggest") {
if (isset($HTTP_GET_VARS['q']) || isset($HTTP_POST_VARS['q'])) {
$q = (isset($HTTP_GET_VARS['q'])) ? un_htmlspecialchars(trim((string)$HTTP_GET_VARS['q'])) : un_htmlspecialchars(trim((string)$HTTP_POST_VARS['q']));
$q = preg_replace("/[^a-z0-9_-]+/i", "", $q);
} else {
$q = "";
}
if (isset($q) && !empty($q)) {
$q = (isset($q)) ? strip_tags(trim($q)) : "";
$sql = "
SELECT " . get_user_table_field("", "user_name") . "
FROM " . USERS_TABLE . "
WHERE " . get_user_table_field("", "user_name") . " LIKE '" . $q . "'
";
$same_user = $site_db->query_firstrow($sql);
$user_name = format_text(trim($same_user[$user_table_fields['user_name']]), 2);
if (strlen($q) > 0) {
$q = strtolower(trim($q));
$hint = "";
for ($i = 0; $i < count($index_name); $i++) {
if ($q == strtolower(substr($index_name[$i], 0, strlen($q)))) {
if ($hint == "") {
$hint = $index_name[$i];
} else {
$hint = $hint." , ".$index_name[$i];
}
}
}
}
if (isset($additional_index_name_filter_check) && is_array($additional_index_name_filter_check) && !empty($additional_index_name_filter_check)) {
$index_flag_for_no = false;
$index_flag_for_yes = false;
foreach ($additional_index_name_filter_check as $key => $val) {
if (!preg_match("/$val/i", $q)) {
$index_flag_for_no .= true;
} elseif (preg_match("/$val/i", $q)) {
$index_flag_for_yes .= true;
}
}
}
if ($hint == "" && !preg_match("/$user_name/i", $q) && isset($index_flag_for_no) && $index_flag_for_no == true) {
$response = $lang['register_suggest_available'];
} elseif (isset($index_flag_for_yes) && $index_flag_for_yes == true) {
$response = $lang['register_suggest_cannot_key_names'];
} elseif (preg_match("/[a-z0-9_-]/i", $user_name) && $user_name == $q) {
mt_srand((double) microtime() * 1000000);
$puddle = '0123456789';
$user_password = "";
$check_user_name = "";
for ($i = 0; $i < 5; $i++) {
$user_name = preg_replace("/[0-9]+/i", "", $user_name);
$user_password .= substr($puddle, (mt_rand()%(strlen($puddle))), 1);
}
$check_user_name .= $user_name. $user_password . "<br />";
$response = preg_replace("/" . $site_template->start . "check_user_name" . $site_template->end . "/siU", format_text(trim($check_user_name), 1), $lang['register_suggest_already_exists']);
} elseif ($hint != "" && $q != $user_name) {
$response = $hint;
}
echo $response;
exit;
}
}
Find:
if (isset($HTTP_POST_VARS['user_name'])) {
add after:
if (isset($additional_index_name_filter_check) && is_array($additional_index_name_filter_check) && !empty($additional_index_name_filter_check)) {
foreach ($additional_index_name_filter_check as $key => $val) {
if (preg_match("/$val/i", $user_name)) {
unset ($user_name);
$msg .= (($msg != "") ? "<br />" : "").$field_error = $lang['register_suggest_cannot_key_names'];
$error = 1;
break;
}
}
}
Find:
"lang_captcha_desc" => $lang['captcha_desc'],
add after:
"lang_register_suggest_text" => $lang['register_suggest_text'],
"register_suggest" => true,
// Step 2
In includes/db_field_definitions.php file,
add in top ?>:
$additional_index_name_filter_check = array("sex", "test", "webm", "adm", "await", "user", "guest", "password", "email");
This is filter for bad word if type username in register form.
// Step 3
In includes/page_header.php file,
find:
"url_captcha_image" => $site_sess->url(ROOT_PATH."captcha.php"),
add after:
"register_suggest" => false,
// Step 4
In lang/english/main.php file,
add in top ?>:
$lang['register_suggest_available'] = "<font color=\"green\">Available !</font>";
$lang['register_suggest_cannot_key_names'] = "<font color=\"red\">You cannot use this username.</font>";
$lang['register_suggest_already_exists'] = "<font color=\"red\">This user already exists in the database.</font><br /><br />Try with:<br /><b>{check_user_name}</b>";
$lang['register_suggest_text'] = "Suggestions: ";
$index_name = array("Anna", "Brittany", "Cinderella", "Diana", "Eva", "Fiona", "Gunda", "Hege", "Inga", "Johanna", "Kitty", "Linda", "Nina", "Ophelia", "Petunia", "Amanda", "Raquel", "Cindy", "Doris", "Eve", "Evita", "Sunniva", "Tove", "Unni", "Violet", "Liza", "Elisabeth", "Ellen", "Wenche", "Vicky");
// Step 5
In templates/your_template/register_form.html file,
find:
<input type="text" name="user_name" size="30" value="{user_name}" class="input" />
replace:
<input type="text" name="user_name" size="30" value="{user_name}" id="txt1" onkeyup="showHint(this.value)" class="input" />
<br />
{lang_register_suggest_text}<span id="txtHint"></span>
// Step 6
In templates/your_template, create file:
clienthint.js .
Add:
var xmlHttp
function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="register.php"
url=url+"?action=register_suggest&q="+str
//url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
// Step 7
In templates/your_template/header.html file (templates/your_template/layout/header.html for user use universal layout TWEAK) -
find:
{if has_rss}
<link rel="alternate" type="application/rss+xml" title="{rss_title}" href="{rss_url}" />
{endif has_rss}
add after:
{if register_suggest}
<script src="{template_url}/clienthint.js"></script>
{endif register_suggest}
I think is all ...
I no support AJAX JS file - I no edit other of URL .
If work, please say ...