4images Forum & Community

4images Modifications / Modifikationen => Mods & Plugins (Releases & Support) => Topic started by: thunderstrike on November 16, 2007, 12:36:11 AM

Title: [MOD] - AJAX - register with username suggestion
Post by: thunderstrike on November 16, 2007, 12:36:11 AM
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:

Code: [Select]
if ($user_info['user_level'] != GUEST && $action != "activate") {
  show_error_page($lang['already_registered']);
}
$content = "";

add after:

Code: [Select]
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:

Code: [Select]
if (isset($HTTP_POST_VARS['user_name'])) {

add after:

Code: [Select]
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:

Code: [Select]
"lang_captcha_desc" => $lang['captcha_desc'],

add after:

Code: [Select]
"lang_register_suggest_text" => $lang['register_suggest_text'],      
"register_suggest" => true,      

// Step 2

In includes/db_field_definitions.php file,

add in top ?>:

Code: [Select]
$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:

Code: [Select]
"url_captcha_image" => $site_sess->url(ROOT_PATH."captcha.php"),

add after:

Code: [Select]
"register_suggest" => false,

// Step 4

In lang/english/main.php file,

add in top ?>:

Code: [Select]
$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:

Code: [Select]
<input type="text" name="user_name" size="30" value="{user_name}" class="input" />

replace:

Code: [Select]
<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:

Code: [Select]
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:

Code: [Select]
{if has_rss}
<link rel="alternate" type="application/rss+xml" title="{rss_title}" href="{rss_url}" />
{endif has_rss}

add after:

Code: [Select]
{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 ... ;)
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Nicky on November 16, 2007, 09:20:47 AM
Hi Thunderstrike,

i just tested this mod with fresh 1.7.4 on xampp windows machine (going to test it on linux now)
below statement is from windows

1.)
bad user name works fine.

2.)
Code: [Select]
$lang['register_suggest_available'] = "<font color=\"green\">Available !</font>";if i put example "Bingo"
is not showing that this name is available.

3.)
i'm admin in my gallery as "Nicky"
it do not shows that this user can not be used.
and suggest username will not be showed example "Nicky_xxxxx"

4.) is there a demo on your site? :)

Title: Re: [MOD] - AJAX - register with username suggestion
Post by: thunderstrike on November 16, 2007, 12:45:23 PM
Correct. Small bug for Available ! word - I send fix if find.


Quote
3.)
i'm admin in my gallery as "Nicky"
it do not shows that this user can not be used.
and suggest username will not be showed example "Nicky_xxxxx"

I edit post with screenshot. I try nicky on fresh gallery with MOD - is work.
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Loda on November 24, 2007, 06:12:35 PM
hi,
works in version 1.7 too, except the "Available !"
thank you very much.
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: thunderstrike on November 24, 2007, 06:21:38 PM
Ok, I check for post first comment reply and I get email message ...
only first comment post from first poster is no send ... code is right ...
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: masterred on February 08, 2008, 05:03:53 AM
hi
this MOD working fine in  4images 1.7.6

i see
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: ccsakuweb on May 16, 2008, 12:43:57 PM
I found a bug in Nicky's demo. when i write #hola it show in Sugestions register.html
i think that characters like # or others they haven't to be valid in usernames  :roll: i use the next to check usernames
Code: [Select]
function validchars($name){ //If it have invalid chars will return 0
  $regex_valid_variable_name = '^[A-Za-z_-][A-Za-z0-9_-]*$';
  return ereg($regex_valid_variable_name, $name);
}
this function only allow A-Z , a-z, - and _
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Nicky on May 16, 2008, 12:45:52 PM
i will remove my demo, due it's not my MOD :)

it was not worked for me...
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: rockboy_62 on June 06, 2008, 08:27:34 AM
hi.thanks for this mode.i want use check username and disable username suggestion.possible?
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Tobi.L on June 06, 2008, 06:26:27 PM
Aber eines geht nicht!

Wenn ein Admin paul heisst, dann und ein User sich reggen möchte, wird das nicht angezeigt. Aber heisst ein User "Paul" und ein User will sich reggen mit Paul wird es angezeigt.
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: nobby on June 06, 2008, 06:35:11 PM
Hallo,

was heißt reggen

nobby
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Nicky on June 06, 2008, 06:39:17 PM
registrieren
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: nobby on June 06, 2008, 07:01:13 PM
registrieren

und was für eine sprache?
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Nicky on June 06, 2008, 07:12:52 PM
kommt aus den irc chats und ist auf deutsch ;)
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Jan-Lukas on June 06, 2008, 08:16:15 PM
Lass  mal Nobby, das verstehen wir beide sowieso nicht mehr.
In vielen Foren, gibt es eine Sprache, die nichts mit der zu tun hat, die wir noch unter Muttersprache verstehen  :wink:
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: nobby on June 06, 2008, 08:22:54 PM
Du hast recht wir werden alt  :mrgreen:

Und trotzdem sind wir Gut, nicht  :P :P  :wink:

nobby
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Jan-Lukas on June 06, 2008, 10:50:33 PM
auf alle Fälle, wenn nicht wir, wer sonst *grübel*
Title: Re: [MOD] - AJAX - Registration mit Überprüfung auf Verfügbarkeit
Post by: Sunny C. on October 15, 2008, 04:07:08 PM
Thanks to Thunderstrike for this Mod: http://www.4homepages.de/forum/index.php?topic=19462.msg104998#msg104998

Übersetzung:
Translate:


Diese Modifikation überprüft ob bestimmte Usernamen schon vorhanden sind.
Ist ein Name vorhanden so bekommt man eine Message das der Name vorhanden sein.
Ebenso wird man Vorschläge gezeigt bekommen.

// Step 1

Öffne: register.php
Finde:

Code: [Select]
if ($user_info['user_level'] != GUEST && $action != "activate") {
  show_error_page($lang['already_registered']);
}
$content = "";

Füge darunterr:

Code: [Select]
if ($action == "register_suggest") {
   
    if (isset($HTTP_GET_VARS['q']) || isset($HTTP_POST_VARS['q'])) {
        $q = (isset($HTTP_GET_VARS['q'])) ? un_htmlspecialchars(trim($HTTP_GET_VARS['q'])) : un_htmlspecialchars(trim($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;
    }
}

Finde:

Code: [Select]
if (isset($HTTP_POST_VARS['user_name'])) {

Füge darunter:

Code: [Select]
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;
              }
          }
      }

Finde:

Code: [Select]
"lang_captcha_desc" => $lang['captcha_desc'],

Füge darunter:

Code: [Select]
"lang_register_suggest_text" => $lang['register_suggest_text'],     
"register_suggest" => true,     

// Step 2

Öffne: includes/db_field_definitions.php
Füge vor
Code: [Select]
?>Das ein:

Code: [Select]
$additional_index_name_filter_check = array("sex", "test", "webm", "adm", "await", "user", "guest", "password", "email");

Das sind Wörter die verboten sind! Diese kannst du bearbeiten oder weitere hinzufügen!

// Step 3

Öffne: includes/page_header.php
finde:

Code: [Select]
"url_captcha_image" => $site_sess->url(ROOT_PATH."captcha.php"),

Füge darunter:

Code: [Select]
"register_suggest" => false,

// Step 4

Öffne: lang/deutsch/main.php
Füge vor:
Code: [Select]
?>Das ein:

Code: [Select]
$lang['register_suggest_available'] = "<font color=\"green\">Verfügbar !</font>";
$lang['register_suggest_cannot_key_names'] = "<font color=\"red\">Diesen Benutzernamen kannst du nicht verwenden!</font>";
$lang['register_suggest_already_exists'] = "<font color=\"red\">Dieser benutzername ist schon vorhanden!</font><br /><br />Try with:<br /><b>{check_user_name}</b>";
$lang['register_suggest_text'] = "Vorschläge: ";
$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

Öffne: templates/your_template/register_form.html

finde:

Code: [Select]
<input type="text" name="user_name" size="30" value="{user_name}" class="input" />

Ersetze mit:

Code: [Select]
<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

Öffne: templates/your_template, Erstelle diese Datei: clienthint.js .

Mit diesem Inhalt:

Code: [Select]
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

Öffne: templates/your_template/header.html

finde:

Code: [Select]
{if has_rss}
<link rel="alternate" type="application/rss+xml" title="{rss_title}" href="{rss_url}" />
{endif has_rss}

Füge darunterr:

Code: [Select]
{if register_suggest}
<script src="{template_url}/clienthint.js"></script>
{endif register_suggest}

Die URL der JS Datei muss so bleiben! Wenn der Pfad geändert wird, wird diese Modifikation nicht mehr funktionieren! Also bitte an die Anleitung zu 100% halten!
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: mawenzi on October 15, 2008, 05:33:23 PM
... ich hoffe doch stark, dass du nun auch den deutschen Support für diese Modifikation übernimmst ...
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: Sunny C. on October 15, 2008, 06:09:27 PM
Ähm... ja!
Sonst würde ich das nicht machen!  :wink:
Title: Re: [MOD] - AJAX - register with username suggestion
Post by: kai on October 15, 2008, 06:13:00 PM
@ Phisker B: Please use the php BB-code for posting php sourcecode.