Author Topic: User input validation for pre-chat survey  (Read 22246 times)

0 Members and 2 Guests are viewing this topic.

bobscan

  • Full Member
  • ***
  • Posts: 10
User input validation for pre-chat survey
« on: November 20, 2012, 07:20:54 PM »
#######################################################
Fix survey blank/bad input                                                                                              #
#######################################################
There's pre-existing functionality of leavemessage.php's validation methods, which requires   input, and requiring a properly formed email address.

thankfully, we can easily integrate this into our pre-chat survey.


insert this @ line 80 of a distro-default client.php. (append it as part of the 'if survey is on' function, just before the following }else{ clause)

Code: [Select]
$errors = NULL;
if( !$email ) {
$errors[] = no_field("form.field.email");
} else if( !$newname ) {
$errors[] = no_field("form.field.name");
} else if( !$firstmessage ) {
$errors[] = no_field("form.field.message");
} else {
if( !is_valid_email($email)) {
$errors[] = wrong_field("form.field.email");
}
}

         
then insert at line 115, now 128 if you added the above statement. (following the 'IP connections exceeded' clause, and before $thread string definition)
Code: [Select]
if( count($errors) > 0 ){
$page = array();
setup_logo();
setup_survey($visitor['name'], $email, $groupid, $info, $referrer);
expand("styles", getchatstyle(), "survey.tpl");
exit;
}

dremhmrk2

  • Jr. Member
  • **
  • Posts: 2
Re: User input validation for pre-chat survey
« Reply #1 on: February 07, 2014, 07:07:04 PM »
Just wanted to update this for the current version of Mibew (1.6.9)

The provided code does still work perfectly, BUT the line numbers are different, and I also added my own touch.

In the root Mibew folder, open client.php.

The following code now gets added right after line 86. (after the userchangename function.)
Code: [Select]
$errors = NULL;
if( !$email ) {
$errors[] = no_field("form.field.email");
} else if( !$newname ) {
$errors[] = no_field("form.field.name");
} else if( !$firstmessage ) {
$errors[] = no_field("form.field.message");
} else {
if( !is_valid_email($email)) {
$errors[] = wrong_field("form.field.email");
}
}

After adding that, this second block of code goes right after line 141. (after the check multiple IP function.)
Code: [Select]
if( count($errors) > 0 ){
$page = array();
setup_logo();
setup_survey($visitor['name'], $email, $groupid, $info, $referrer);
expand("styles", getchatstyle(), "survey.tpl");
exit;
}

Not so different so far, BUT what the original author didn't consider is that the form auto-fills, at least in my case, the word 'Guest' as a placeholder. The problem there is that 'Guest' passes the validation! D'oh! But no worries, there is a super simple solution. Bby the way, this applies specifically to the English localization, but probably also applies to other languages as well.

Open mibew/locales/en/properties (note the lack of an extension, make sure your editor doesn't add an extension when saving!)

On line 44, find:
Code: [Select]
chat.default.username=Guest
and change it to:
Code: [Select]
chat.default.username=
This keeps it from auto-filling the word 'guest', and forces the user to enter SOMETHING.

Otherwise the original poster had it all perfect for his version of Mibew, and I am just adapting it to my needs. I was getting driven crazy by people entering guest and talking to us as if we knew who they were, such as: "Did my order ship?" and we would have to always ask them to identify themselves.

ibrahim52

  • Jr. Member
  • **
  • Posts: 2
Re: User input validation for pre-chat survey
« Reply #2 on: May 15, 2014, 10:58:57 PM »
I don't know why people don't bother to say THANK YOU for some one putting so much of efforts for others. Seriously man, thanks a lot for this solution you have posted. Just one doubt I have, on the chat window at NAME field it keep saying     Please fill "Your email". Is there any way to force just a normal name

ibrahim52

  • Jr. Member
  • **
  • Posts: 2
Re: User input validation for pre-chat survey
« Reply #3 on: May 15, 2014, 11:11:12 PM »
 :o not a programmer but figured out.

Instead of

$errors = NULL;
         if( !$email ) {
            $errors[] = no_field("form.field.email");
         } else if( !$newname ) {
            $errors[] = no_field("form.field.name");
         } else if( !$firstmessage ) {
            $errors[] = no_field("form.field.message");
         } else {
            if( !is_valid_email($email)) {
               $errors[] = wrong_field("form.field.email");
            }
         }


Just use

      $errors = NULL;
          if( !$newname ) {
            $errors[] = no_field("form.field.name");

this is only if you do not want visitors to enter their email on first window of chat.

Dmitriy Simushev

  • Native
  • *****
  • Posts: 345
Re: User input validation for pre-chat survey
« Reply #4 on: May 16, 2014, 06:21:44 AM »
I want to insert my two cents.

It would be better to use
Code: [Select]
$errors = array();instead of
Code: [Select]
$errors = NULL;to avoid PHP notices.

yaisel

  • Full Member
  • ***
  • Posts: 5
    • GoldenStar de Mexico
Re: User input validation for pre-chat survey
« Reply #5 on: May 16, 2014, 03:52:00 PM »
to improve the validation of email common.php file and go to the regular expression to replace the line 563 for this.

original (validate client@mibew like correct) line 563
Code: [Select]
return preg_match("/^[^@]+@[^\.]+(\.[^\.]+)*$/", $email);
Replace for this (now validate client@mibew like wrong the correct email is client@mibew.org)
Code: [Select]
return preg_match("/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/", $email);

yaisel

  • Full Member
  • ***
  • Posts: 5
    • GoldenStar de Mexico
Re: User input validation for pre-chat survey
« Reply #6 on: May 21, 2014, 03:54:25 PM »
to function properly in the verification form entry to chat in version 1.6.11 should add the following code at line 87 of client.php
Code: [Select]
                       /*newcode*/
                        $errors = array();
if( !$email ) {
$errors[] = no_field("presurvey.mail");
} else if( !$newname ) {
$errors[] = no_field("presurvey.name");
} else if( !$firstmessage ) {
$errors[] = no_field("presurvey.question");
} else {
if( !is_valid_email($email)) {
$errors[] = wrong_field("presurvey.mail");
}
}
                        /*end this code*/
                        if( count($errors) > 0 ){
$page = array();
setup_logo();
                                setup_survey($visitor['name'], $email, $groupid, $info, $referrer, can_show_captcha());
expand("styles", getchatstyle(), "survey.tpl");
exit;
}
                /*###########*/

marcellocaetano

  • Full Member
  • ***
  • Posts: 13
Re: User input validation for pre-chat survey
« Reply #7 on: August 19, 2014, 03:58:36 PM »
to function properly in the verification form entry to chat in version 1.6.11 should add the following code at line 87 of client.php
Code: [Select]
                       /*newcode*/
                        $errors = array();
if( !$email ) {
$errors[] = no_field("presurvey.mail");
} else if( !$newname ) {
$errors[] = no_field("presurvey.name");
} else if( !$firstmessage ) {
$errors[] = no_field("presurvey.question");
} else {
if( !is_valid_email($email)) {
$errors[] = wrong_field("presurvey.mail");
}
}
                        /*end this code*/
                        if( count($errors) > 0 ){
$page = array();
setup_logo();
                                setup_survey($visitor['name'], $email, $groupid, $info, $referrer, can_show_captcha());
expand("styles", getchatstyle(), "survey.tpl");
exit;
}
                /*###########*/

How can I apply this in Mibew 2?