Make password checker easily pluggable to any password change prompt.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 3 May 2008 21:34:22 +0000 (23:34 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 3 May 2008 21:59:10 +0000 (23:59 +0200)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/xorg.js
plugins/compiler.checkpasswd.php [new file with mode: 0644]
templates/platal/motdepasse.tpl

index 0e7f8e6..7cb2fe3 100644 (file)
@@ -32,7 +32,7 @@ function getNow() {
     if (yr<1000) yr += 1900;
     hr = dt.getHours();
     mi = dt.getMinutes();
-    
+
     time   = (mi < 10) ? hr +':0'+mi : hr+':'+mi;
     days   = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
     months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet',
@@ -66,7 +66,7 @@ function addSearchEngine()
     try {
         window.external.AddSearchProvider(searchURI);
     } catch(e) {
-        alert("Impossible d'installer la barre de recherche"); 
+        alert("Impossible d'installer la barre de recherche");
     }
   }
 }
@@ -89,7 +89,7 @@ function attachEvent(obj, evt, f, useCapture) {
         return true;
     } else if (obj.attachEvent) {
         return obj.attachEvent("on"+evt, f);
-    } 
+    }
     return false;
 }
 
@@ -280,6 +280,61 @@ function auto_links_nodes(nodes) {
 
 // }}}
 
+
+/***************************************************************************
+ * Password check
+ */
+
+// {{{ function checkPassword
+
+function getType(char) {
+    if (char >= 'a' && char <= 'z') {
+        return 1;
+    } else if (char >= 'A' && char <= 'Z') {
+        return 2;
+    } else if (char >= '0' && char <= '9') {
+        return 3;
+    } else {
+        return 4;
+    }
+}
+
+function checkPassword(box) {
+    var prev = 0;
+    var prop = 0;
+    var pass = box.value;
+    var types = Array(0, 0, 0, 0, 0);
+    for (i = 0 ; i < pass.length ; ++i) {
+        type = getType(pass.charAt(i));
+        if (prev != 0 && prev != type) {
+            prop += 5;
+        }
+        if (i >= 5) {
+            prop += 5;
+        }
+        if (types[type] == 0) {
+            prop += 10;
+        }
+        types[type]++;
+        prev = type;
+    }
+    if (prop > 100) {
+        prop = 100;
+    } else if (prop < 0) {
+        prop = 0;
+    }
+    ok = (prop >= 60);
+    $("#passwords_measure").width(prop + "%").css("background-color", ok ? "green" : "red");
+    if (ok) {
+        $(":submit").removeAttr("disabled");
+    } else {
+        $(":submit").attr("disabled", "disabled");
+    }
+}
+
+// }}}
+
+
 /***************************************************************************
  * The real OnLoad
  */
diff --git a/plugins/compiler.checkpasswd.php b/plugins/compiler.checkpasswd.php
new file mode 100644 (file)
index 0000000..dbfbc71
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  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 2 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, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+require_once 'platal.inc.php';
+
+function smarty_compiler_checkpasswd($tag_attrs, &$compiler)
+{
+    extract($compiler->_parse_attrs($tag_attrs));
+    if (!isset($width)) {
+      $width = '250px';
+    }
+    if (!isset($id)) {
+      $id = "newpassword";
+    }
+
+    return '?><script type="text/javascript" src="javascript/jquery.js" ></script>
+              <script type="text/javascript">//<![CDATA[
+                $(":input[@name=' . $id . ']").keyup(function(event) { checkPassword(event.target); });
+              //]]></script>
+              <div style="border: 1px solid white; width: ' . $width . '; height: 7px; background-color: #444">
+                <div id="passwords_measure" style="height: 100%; background-color: red; width: 0px"></div>
+              </div><?php';
+}
+
+/* vim: set expandtab enc=utf-8: */
+
+?>
index bbd48c0..91cb319 100644 (file)
 </p>
 <br />
 <form action="{$smarty.server.REQUEST_URI}" method="post" id="changepass">
-  {javascript name="jquery"}
-  <script type="text/javascript">//<![CDATA[
-  {literal}
-    function getType(char) {
-      if (char >= 'a' && char <= 'z') {
-        return 1;
-      } else if (char >= 'A' && char <= 'Z') {
-        return 2;
-      } else if (char >= '0' && char <= '9') {
-        return 3;
-      } else {
-        return 4;
-      }
-    }
-    function checkPassword(box) {
-      var prev = 0;
-      var prop = 0;
-      var pass = box.value;
-      var types = Array(0, 0, 0, 0, 0);
-      for (i = 0 ; i < pass.length ; ++i) {
-        type = getType(pass.charAt(i));
-        if (prev != 0 && prev != type) {
-          prop += 5;
-        }
-        if (i >= 5) {
-          prop += 5;
-        }
-        if (types[type] == 0) {
-          prop += 10;
-        }
-        types[type]++;
-        prev = type;
-      }
-      if (prop > 100) {
-        prop = 100;
-      } else if (prop < 0) {
-        prop = 0;
-      }
-      ok = (prop >= 60);
-      $("#passwords").width(prop + "%").css("background-color", ok ? "green" : "red");
-      if (ok) {
-        $(":submit").removeAttr("disabled");
-      } else {
-        $(":submit").attr("disabled", "disabled");
-      }
-    }
-    {/literal}
-  //]]></script>
   <table class="tinybicol" cellpadding="3" cellspacing="0"
     summary="Formulaire de mot de passe">
     <tr>
@@ -95,7 +47,7 @@
         Nouveau mot de passe&nbsp;:
       </td>
       <td>
-        <input type="password" size="10" maxlength="10" name="nouveau" onkeyup="checkPassword(this)" />
+        <input type="password" size="10" maxlength="10" name="nouveau" />
       </td>
     </tr>
     <tr>
         Sécurité
       </td>
       <td>
-        <div style="border: 1px solid white; width: 250px; height: 7px; background-color: #444">
-          <div id="passwords" style="height: 100%; background-color: red; width: 0px"></div>
-        </div>
+        {checkpasswd id="nouveau"}
       </td>
     </tr>
     <tr>