Author Topic: [MOD] - AJAX - register with username suggestion  (Read 37013 times)

0 Members and 2 Guests are viewing this topic.

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
[MOD] - AJAX - register with username suggestion
« 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 ... ;)
« Last Edit: August 27, 2011, 02:30:06 PM by thunderstrike »
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Nicky

  • Administrator
  • 4images Guru
  • *****
  • Posts: 3.195
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #1 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? :)

« Last Edit: May 16, 2008, 12:46:37 PM by Nicky »
cheers
Nicky
Your first three "must do" before you ask a question ! (© by V@no)
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

nicky.net 4 4images
Signature stolen from mawenzi

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #2 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.
« Last Edit: November 16, 2007, 12:59:51 PM by thunderstrike »
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline Loda

  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
    • Fotosucht Schweiz
Re: [MOD] - AJAX - register with username suggestion
« Reply #3 on: November 24, 2007, 06:12:35 PM »
hi,
works in version 1.7 too, except the "Available !"
thank you very much.

Offline thunderstrike

  • 4images Guru
  • *******
  • Posts: 2.327
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #4 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 ...
8 steps need when ask question -

- PHP version (ACP - > phpinfo())
- mySQL version (ACP - > phpinfo())
- 4images version
- Post screenshot / URL
- Post code in BB Code (no need full file for code) or post attach file
- It doesn't work. What is say - what is do for no work
- Install MOD ? If so - please say (troubleshooting)
- Read FAQ ? Install Bug fixes ?

Offline masterred

  • Sr. Member
  • ****
  • Posts: 494
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #5 on: February 08, 2008, 05:03:53 AM »
hi
this MOD working fine in  4images 1.7.6

i see
Apache/2.2.21 (Win32)
PHP/5.3.5
Mysql Version: 5.5.19
4images Version: 1.7.?



Offline ccsakuweb

  • Sr. Member
  • ****
  • Posts: 498
  • Patri
    • View Profile
    • My Art
Re: [MOD] - AJAX - register with username suggestion
« Reply #6 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 _
:arrow: 4images Paid Mods: Links, Blog, Albums, Subdomains for users, Diferent templates for user profile, Related picture in details, Last pictures in details.
And the mod that you request me.   Demo: http://www.myart.es

A website dedicated to artist people who loves drawing, design, writing and more

Offline Nicky

  • Administrator
  • 4images Guru
  • *****
  • Posts: 3.195
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #7 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...
cheers
Nicky
Your first three "must do" before you ask a question ! (© by V@no)
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

nicky.net 4 4images
Signature stolen from mawenzi

Offline rockboy_62

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #8 on: June 06, 2008, 08:27:34 AM »
hi.thanks for this mode.i want use check username and disable username suggestion.possible?

Tobi.L

  • Guest
Re: [MOD] - AJAX - register with username suggestion
« Reply #9 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.

Offline nobby

  • 4images Guru
  • *******
  • Posts: 2.873
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #10 on: June 06, 2008, 06:35:11 PM »
Hallo,

was heißt reggen

nobby

Offline Nicky

  • Administrator
  • 4images Guru
  • *****
  • Posts: 3.195
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #11 on: June 06, 2008, 06:39:17 PM »
registrieren
cheers
Nicky
Your first three "must do" before you ask a question ! (© by V@no)
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

nicky.net 4 4images
Signature stolen from mawenzi

Offline nobby

  • 4images Guru
  • *******
  • Posts: 2.873
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #12 on: June 06, 2008, 07:01:13 PM »
registrieren

und was für eine sprache?

Offline Nicky

  • Administrator
  • 4images Guru
  • *****
  • Posts: 3.195
    • View Profile
Re: [MOD] - AJAX - register with username suggestion
« Reply #13 on: June 06, 2008, 07:12:52 PM »
kommt aus den irc chats und ist auf deutsch ;)
cheers
Nicky
Your first three "must do" before you ask a question ! (© by V@no)
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

nicky.net 4 4images
Signature stolen from mawenzi

Offline Jan-Lukas

  • Addicted member
  • ******
  • Posts: 1.289
    • View Profile
    • Discover the New World of Kindersurprise
Re: [MOD] - AJAX - register with username suggestion
« Reply #14 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:
Danke Harald