_binary From 7271eaa246c92924b8ca176a9fa78276932bd116 Mon Sep 17 00:00:00 2001
From: Sarika Padmashali <sarika.padmashali92@yahoo.in>
Date: Wed, 30 Nov 2016 15:02:34 -0800
Subject: [PATCH 1/2] Adding input_validator.js - registerPage client side
 validation patch

---
 src/controllers/RegisterController.php |  47 ++++--
 src/scripts/input_validator.js         | 261 +++++++++++++++++++++++++++++++++
 src/views/RegisterView.php             |  25 ++--
 3 files changed, 311 insertions(+), 22 deletions(-)
 create mode 100644 src/scripts/input_validator.js

diff --git a/src/controllers/RegisterController.php b/src/controllers/RegisterController.php
index 1247ed6..7b59226 100755
--- a/src/controllers/RegisterController.php
+++ b/src/controllers/RegisterController.php
@@ -30,7 +30,6 @@
  */
 namespace seekquarry\yioop\controllers;
 
-use seekquarry\yioop as B;
 use seekquarry\yioop\configs as C;
 use seekquarry\yioop\library as L;
 use seekquarry\yioop\library\CrawlConstants;
@@ -66,6 +65,7 @@ class RegisterController extends Controller implements CrawlConstants
      * @var array
      */
     public $forbidden_usernames = "admin moderator root";
+
     /**
      * An array of triples, each triple consisting of a question of the form
      * Which is the most..? followed by one of the form Which is the least ..?
@@ -162,6 +162,7 @@ class RegisterController extends Controller implements CrawlConstants
      */
     public function processRequest()
     {
+
         $visitor_model = $this->model("visitor");
         if (isset($_SESSION['USER_ID'])) {
             $user = $_SESSION['USER_ID'];
@@ -169,16 +170,6 @@ class RegisterController extends Controller implements CrawlConstants
             $user = $_SERVER['REMOTE_ADDR'];
         }
         $visitor_check_names = ['captcha_time_out','suggest_day_exceeded'];
-        foreach($visitor_check_names as $name) {
-            $visitor = $visitor_model->getVisitor($_SERVER['REMOTE_ADDR'],
-                $name);
-            if(isset($visitor['END_TIME']) && $visitor['END_TIME'] > time()) {
-                $_SESSION['value'] = date('Y-m-d H:i:s', $visitor['END_TIME']);
-                $url = B\wikiUrl($visitor['PAGE_NAME']);
-                header("Location:" . $url);
-                exit();
-            }
-        }
         $data = [];
         $data['REFRESH'] = "register";
         $activity = isset($_REQUEST['a']) ?
@@ -273,8 +264,37 @@ class RegisterController extends Controller implements CrawlConstants
                $data['INCLUDE_SCRIPTS'] = ["sha1", "hash_captcha"];
             }
         }
-        //used to ensure that we have sessions active
+        /*used to ensure that we have sessions active
+        *
+        */
         $_SESSION['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
+        /*used to manage locales
+        *
+        * localization of the warning messages 
+        */
+        $tl = ["input_validator_js_valid_email" => 
+        tl('input_validator_js_valid_email'),
+        "input_validator_js_invalid_email" => 
+        tl('input_validator_js_invalid_email'),
+        "input_validator_js_more_characters" => 
+        tl('input_validator_js_more_characters'),
+        "input_validator_js_strong_password" =>
+        tl('input_validator_js_strong_password'),
+        "input_validator_js_weak_password" =>
+        tl('input_validator_js_weak_password'),
+        "input_validator_js_enter_username" =>
+        tl('input_validator_js_enter_username'),
+        "input_validator_js_enter_firstname" => 
+        tl('input_validator_js_enter_firstname'),
+        "input_validator_js_enter_lastname" => 
+        tl('input_validator_js_enter_lastname'),
+        "input_validator_js_retype_password_matched" => 
+        tl('input_validator_js_retype_password_matched'),
+        "input_validator_js_retype_password_not_matched" => 
+        tl('input_validator_js_retype_password_not_matched'),
+        "input_validator_js_type_password" => 
+        tl('input_validator_js_type_password')];
+        $data['SCRIPT'] = 'tl = '.json_encode($tl).';';
         $this->displayView($view, $data);
     }
     /**
@@ -938,13 +958,14 @@ class RegisterController extends Controller implements CrawlConstants
         foreach ($fields as $field) {
             $data[strtoupper($field)] = "";
         }
+        $data['INCLUDE_SCRIPTS'] = ["input_validator"];
         for ($i = 0; $i < self::NUM_CAPTCHA_QUESTIONS +
             self::NUM_CAPTCHA_CHOICES; $i++) {
             $data["question_$i"] = "-1";
         }
         if (C\AUTHENTICATION_MODE == C\ZKP_AUTHENTICATION) {
             $data['AUTHENTICATION_MODE'] = C\ZKP_AUTHENTICATION;
-            $data['INCLUDE_SCRIPTS'] = ["sha1", "zkp"," big_int"];
+            $data['INCLUDE_SCRIPTS'] = ["sha1", "zkp"," big_int","input_validator"];
         } else {
             $data['AUTHENTICATION_MODE'] = C\NORMAL_AUTHENTICATION;
         }
diff --git a/src/scripts/input_validator.js b/src/scripts/input_validator.js
new file mode 100644
index 0000000..8ee9a73
--- /dev/null
+++ b/src/scripts/input_validator.js
@@ -0,0 +1,261 @@
+/**
+ * SeekQuarry/Yioop --
+ * Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ * Copyright (C) 2009 - 2016  Chris Pollett chris@pollett.org
+ *
+ * LICENSE:
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * END LICENSE
+ *
+ * @author Sarika Padmashali 
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009 - 2016
+ * @filesource
+ */
+/*
+     * checks whether the password retype field has been touched or not
+     * defaults to false
+     * @var boolean
+*/
+var clicked = false;
+/*
+     * checks whether the email is valid or not
+     * defaults to false
+     * @var boolean
+*/
+var emailValid = false;
+/*
+     * checks whether the password is valid or not
+     * defaults to false
+     * @var boolean
+*/
+var passwordValid = false;
+/*
+     * checks whether the retyped password is valid or not
+     * defaults to false
+     * @var boolean
+*/
+var passwordMatchValid = false; 
+/*
+     * defining translation variable
+*/
+var tl = document.tl;
+/*
+ * To validate email address
+ *
+ * Checks if the email address entered is in the correct format
+ * @return boolean true if the email address entered is valid else return false
+ */
+function validateEmail()  
+{  
+    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
+    var inputText = elt("email");
+    if(inputText.value.match(mailformat)){  
+            var emailErr = elt("email-error");
+            emailErr.innerHTML = "<span class=\"green\">" +
+             tl['input_validator_js_valid_email'] + "</span>";
+            emailValid  = true;
+            submitDisabled();
+            return true;  
+    }else  
+    {  
+            var emailErr = elt("email-error");
+            emailErr.innerHTML = "<span class=\"red\">" +
+             tl['input_validator_js_invalid_email'] + "</span>";
+            emailValid  = false;
+            submitDisabled();
+            return false;  
+    }  
+}
+/*
+ * To validate password
+ *
+ * Checks if the password entered is strong enough 
+ * Warns the user of the password is weak or blank
+ * It also checks if the retyped password matches     
+ */ 
+function passwordChanged() 
+{
+    var strength = elt("strength");
+    var strongRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
+    var enoughRegex = new RegExp("(?=.{6,}).*", "g");
+    var pwd = elt("pass-word");
+    if (pwd.value.length==0){
+        strength.innerHTML = tl['input_validator_js_type_password'];
+        passwordValid = false;
+        submitDisabled();
+
+    }else if (enoughRegex.test(pwd.value) == false) 
+    {
+        strength.innerHTML = "<span class=\"red\">" +
+         tl['input_validator_js_more_characters'] + "</span>";
+        passwordValid = false;
+        submitDisabled();
+    }else if (strongRegex.test(pwd.value)) 
+    {
+        strength.innerHTML = "<span class=\"green\">" +
+         tl['input_validator_js_strong_password'] + "</span>";
+        passwordValid = true;   
+        if(clicked == true)
+        {
+            passwordMatch();
+        }
+        submitDisabled();
+    }else 
+    {
+        strength.innerHTML = "<span class=\"red\">" +
+         tl['input_validator_js_weak_password'] + "</span>";
+        passwordValid = false;
+        submitDisabled();
+    }
+}
+/*
+ * To check if retyped password matches
+ *
+ * Checks if the retyped password matches 
+ * If not warns the user about it 
+ */ 
+function passwordMatch()
+{
+    clicked = true;
+    var retyped_pass = elt("retype-password");
+    var pass= elt("pass-word");
+    var passMatch = elt("pass-match");
+    if ( retyped_pass.value == pass.value){
+        passwordMatchValid = true;
+        passMatch.innerHTML = "<span class=\"green\">" +
+         tl['input_validator_js_retype_password_matched'] + "</span>";
+        submitDisabled();
+    }else
+    {
+        passwordMatchValid = false;
+        passMatch.innerHTML = "<span class=\"red\">" +
+         tl['input_validator_js_retype_password_not_matched'] + "</span>";
+        submitDisabled();
+    }
+} 
+/*
+ * To check if user name is not blank
+ *
+ * Checks if the user name has been entered
+ * If not warns the user to enter the username    
+ */ 
+function checkUser()
+{
+    var user =  elt("username").value;
+    var userErr = elt("user-error");
+    if (user == ""){
+        userValid = false;
+        userErr.innerHTML = "<span class=\"red\">" +
+         tl['input_validator_js_enter_username'] + "</span>";
+        
+    }else
+    {
+        userErr.innerHTML = "<span class=\"red\"></span>";
+    }
+    submitDisabled();
+}
+/*
+ * To check if first name is not blank
+ *
+ * Checks if the first name has been entered
+ * If not warns the user to enter the first name    
+ */  
+function checkFirst()
+{
+    var first =  elt("firstname").value;
+    var firstErr = elt("first-error");
+    if (first == ""){
+        firstValid = false;
+        firstErr.innerHTML = "<span class=\"red\">" +
+         tl['input_validator_js_enter_firstname'] + "</span>";
+    }else
+    {
+        firstErr.innerHTML = "<span class=\"red\"></span>";
+    }
+    submitDisabled();
+}
+/*
+ * To check if last name is not blank
+ *
+ * Checks if the last name has been entered
+ * If not warns the user to enter the last name    
+ */ 
+function checkLast()
+{
+    var last =  elt("lastname").value;
+    var lastErr = elt("last-error");
+    if (last == ""){
+        lastValid = false;
+        lastErr.innerHTML = "<span class=\"red\">" +
+         tl['input_validator_js_enter_lastname'] + "</span>";
+    }else
+    {
+        lastErr.innerHTML = "<span class=\"red\"></span>";
+    }
+    submitDisabled();
+}
+/*
+ * Disables the submit button
+ *
+ * Enables the submit button only if all input fields are valid
+ * If not keeps the submit button disabled until the user corrects all the fields   
+ */ 
+function submitDisabled()
+{
+    var user =  elt("username").value;
+    var first = elt("firstname").value;
+    var last = elt("lastname").value;
+    if (emailValid == true && passwordValid == true && 
+        passwordMatchValid == true && first != "" &&last != "" && user!= ""){
+        elt("submit").disabled = false;
+    }else
+    {
+        elt("submit").disabled = true;
+    }
+}
+/*
+ * event handler to check first name
+ *
+ */  
+elt("firstname").addEventListener("keyup", checkFirst); 
+/*
+ * event handler to check last name
+ *
+ */ 
+elt("lastname").addEventListener("keyup", checkLast);
+/*
+ * event handler to check user name
+ *
+ */ 
+elt("username").addEventListener("keyup", checkUser);
+/*
+ * event handler to validate email address
+ *
+ */ 
+elt("email").addEventListener("keyup",validateEmail);
+/*
+ * event handler to validate password
+ *
+ */ 
+elt("pass-word").addEventListener("keyup", passwordChanged); 
+/*
+ * event handler to validate retyped password
+ *
+ */ 
+elt("retype-password").addEventListener("keyup", passwordMatch); 
diff --git a/src/views/RegisterView.php b/src/views/RegisterView.php
index 1768bec..2e2e34f 100755
--- a/src/views/RegisterView.php
+++ b/src/views/RegisterView.php
@@ -193,12 +193,12 @@ class RegisterView extends View
                         </th>
                         <td class="table-input">
                             <input id="firstname" type="text"
-                                class="narrow-field" maxlength="<?=
+                                 class="narrow-field" maxlength="<?=
                                 C\NAME_LEN ?>"
                                 name="first" autocomplete="off"
                                 value = "<?= $data['FIRST'] ?>"/>
                             <?= in_array("first", $missing)
-                                ?'<span class="red">*</span>':'' ?></td>
+                                ?'<span class="red">*</span>':'' ?><span id="first-error"></span></td>
                     </tr>
                     <tr>
                         <th class="table-label">
@@ -213,7 +213,7 @@ class RegisterView extends View
                                 name="last" autocomplete="off"
                                 value = "<?= $data['LAST']  ?>"/>
                             <?= in_array("last", $missing) ?
-                                '<span class="red">*</span>':'' ?></td>
+                                '<span class="red">*</span>':'' ?><span id="last-error"></span></td>
                     </tr>
                     <tr>
                         <th class="table-label"><label for="username">
@@ -226,7 +226,7 @@ class RegisterView extends View
                                 name="user" autocomplete="off"
                                 value = "<?= $data['USER'] ?>"/>
                             <?= in_array("user", $missing) ?
-                                '<span class="red">*</span>':'' ?></td>
+                                '<span class="red">*</span>':'' ?><span id="user-error"></span></td>
                     </tr>
                     <tr>
                         <th class="table-label"><label for="email"><?=
@@ -239,7 +239,10 @@ class RegisterView extends View
                                 name="email" autocomplete="off"
                                 value = "<?= $data['EMAIL'] ?>"/>
                             <?= in_array("email", $missing)
-                                ? '<span class="red">*</span>':'' ?></td>
+                                ? '<span class="red">*</span>':'' ?>
+                                <span id="email-error"><?=
+                            tl('register_view_type_email_address')
+                            ?></span></td>
                     </tr>
                     <tr>
                         <th class="table-label">
@@ -254,7 +257,10 @@ class RegisterView extends View
                                 name="password" value="<?=
                                 $data['PASSWORD'] ?>" />
                             <?= in_array("password", $missing)
-                                ? '<span class="red">*</span>':'' ?></td>
+                                ? '<span class="red">*</span>':'' ?><span id="strength"><?=
+                            tl('register_view_type_password')
+                            ?></span></td>
+                               
                     </tr>
                     <tr>
                         <th class="table-label">
@@ -269,7 +275,8 @@ class RegisterView extends View
                                 name="repassword" value="<?=
                                 $data['REPASSWORD'] ?>" />
                             <?= in_array("repassword", $missing)
-                                ?'<span class="red">*</span>' : '' ?></td>
+                                ?'<span class="red">*</span>' : '' ?>
+                                <span id="pass-match"></span></td>
                     </tr>
                     <?php
                     if (isset($_SESSION["random_string"]) ||
@@ -352,7 +359,7 @@ class RegisterView extends View
                         <input type="hidden"
                             name="<?php e(C\CSRF_TOKEN);?>"
                             value="<?php e($data[C\CSRF_TOKEN]); ?>"/>
-                            <button  type="submit"><?= 
+                            <button  id ="submit" type="submit" disabled><?= 
                             tl('register_view_create_account')
                             ?></button>
                         </td>
@@ -367,7 +374,7 @@ class RegisterView extends View
             </div>
         </div>
         </div>
-        <div class='tall-landing-spacer'></div>
+        <div class='tall-landing-spacer'></div></td>
         <?php
         if (isset($_SESSION["random_string"])) { ?>
             <script type="text/javascript" >
-- 
2.6.4


From 959083313d94217c925c7eb85c2d7ec9e3f43d4c Mon Sep 17 00:00:00 2001
From: Sarika Padmashali <sarika.padmashali92@yahoo.in>
Date: Wed, 30 Nov 2016 16:27:36 -0800
Subject: [PATCH 2/2] Client side validation register page

---
 src/controllers/RegisterController.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/controllers/RegisterController.php b/src/controllers/RegisterController.php
index 7b59226..3c6391d 100755
--- a/src/controllers/RegisterController.php
+++ b/src/controllers/RegisterController.php
@@ -29,7 +29,7 @@
  * @filesource
  */
 namespace seekquarry\yioop\controllers;
-
+use seekquarry\yioop as B;
 use seekquarry\yioop\configs as C;
 use seekquarry\yioop\library as L;
 use seekquarry\yioop\library\CrawlConstants;
-- 
2.6.4

