Subversion Repositories ALCASAR

Rev

Rev 2242 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2242 tom.houday 1
/*
2
**    Created by: Jeff Todnem (http://www.todnem.com/)
3
**    Created on: 2007-08-14
4
**    Last modified: 2010-05-03
5
**
6
**    License Information:
7
**    -------------------------------------------------------------------------
8
**    Copyright (C) 2007 Jeff Todnem
9
**
10
**    This program is free software; you can redistribute it and/or modify it
11
**    under the terms of the GNU General Public License as published by the
12
**    Free Software Foundation; either version 2 of the License, or (at your
13
**    option) any later version.
14
**    
15
**    This program is distributed in the hope that it will be useful, but
16
**    WITHOUT ANY WARRANTY; without even the implied warranty of
17
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
**    General Public License for more details.
19
**    
20
**    You should have received a copy of the GNU General Public License along
21
**    with this program; if not, write to the Free Software Foundation, Inc.,
22
**    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
**    
24
*/
25
 
26
function addLoadEvent(func) {
27
	var oldonload = window.onload;
28
	if (typeof window.onload != "function") {
29
		window.onload = func;
30
	}
31
	else {
32
		window.onload = function() {
33
			if (oldonload) {
34
				oldonload();
35
			}
36
			func();
37
		};
38
	}
39
}
40
 
41
function $() {
42
	var arrElms = [];
43
	for (var i=0; i < arguments.length; i++) {
44
		var elm = arguments[i];
45
		if (typeof(elm == "string")) { elm = document.getElementById(elm); }
46
		if (arguments.length == 1) { return elm; }
47
		arrElms.push(elm);
48
	}
49
	return arrElms;
50
}
51
 
52
String.prototype.strReverse = function() {
53
	var newstring = "";
54
	for (var s=0; s < this.length; s++) {
55
		newstring = this.charAt(s) + newstring;
56
	}
57
	return newstring;
58
	//strOrig = ' texttotrim ';
59
	//strReversed = strOrig.revstring();
60
};
61
 
62
function chkPass(pwd) {
3026 rexy 63
	var oBar = $("pwprogress");
2242 tom.houday 64
	// Simultaneous variable declaration and value assignment aren't supported in IE apparently
65
	// so I'm forced to assign the same value individually per var to support a crappy browser *sigh* 
66
	var nScore=0, nLength=0, nAlphaUC=0, nAlphaLC=0, nNumber=0, nSymbol=0, nMidChar=0, nRequirements=0, nAlphasOnly=0, nNumbersOnly=0, nUnqChar=0, nRepChar=0, nRepInc=0, nConsecAlphaUC=0, nConsecAlphaLC=0, nConsecNumber=0, nConsecSymbol=0, nConsecCharType=0, nSeqAlpha=0, nSeqNumber=0, nSeqSymbol=0, nSeqChar=0, nReqChar=0, nMultConsecCharType=0;
67
	var nMultRepChar=1, nMultConsecSymbol=1;
68
	var nMultMidChar=2, nMultRequirements=2, nMultConsecAlphaUC=2, nMultConsecAlphaLC=2, nMultConsecNumber=2;
69
	var nReqCharType=3, nMultAlphaUC=3, nMultAlphaLC=3, nMultSeqAlpha=3, nMultSeqNumber=3, nMultSeqSymbol=3;
70
	var nMultLength=4, nMultNumber=4;
71
	var nMultSymbol=6;
72
	var nTmpAlphaUC="", nTmpAlphaLC="", nTmpNumber="", nTmpSymbol="";
73
	var sAlphaUC="0", sAlphaLC="0", sNumber="0", sSymbol="0", sMidChar="0", sRequirements="0", sAlphasOnly="0", sNumbersOnly="0", sRepChar="0", sConsecAlphaUC="0", sConsecAlphaLC="0", sConsecNumber="0", sSeqAlpha="0", sSeqNumber="0", sSeqSymbol="0";
74
	var sAlphas = "abcdefghijklmnopqrstuvwxyz";
75
	var sNumerics = "01234567890";
76
	var sSymbols = ")!@#$%^&*()";
3026 rexy 77
	var sColor = "";
2242 tom.houday 78
	var sComplexity = "Trop court";
79
	var nMinPwdLen = 8;
80
	if (document.all) { var nd = 0; } else { var nd = 1; }
81
	if (pwd) {
82
		nScore = parseInt(pwd.length * nMultLength);
83
		nLength = pwd.length;
84
		var arrPwd = pwd.replace(/\s+/g,"").split(/\s*/);
85
		var arrPwdLen = arrPwd.length;
86
 
87
		/* Loop through password to check for Symbol, Numeric, Lowercase and Uppercase pattern matches */
88
		for (var a=0; a < arrPwdLen; a++) {
89
			if (arrPwd[a].match(/[A-Z]/g)) {
90
				if (nTmpAlphaUC !== "") { if ((nTmpAlphaUC + 1) == a) { nConsecAlphaUC++; nConsecCharType++; } }
91
				nTmpAlphaUC = a;
92
				nAlphaUC++;
93
			}
94
			else if (arrPwd[a].match(/[a-z]/g)) { 
95
				if (nTmpAlphaLC !== "") { if ((nTmpAlphaLC + 1) == a) { nConsecAlphaLC++; nConsecCharType++; } }
96
				nTmpAlphaLC = a;
97
				nAlphaLC++;
98
			}
99
			else if (arrPwd[a].match(/[0-9]/g)) { 
100
				if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
101
				if (nTmpNumber !== "") { if ((nTmpNumber + 1) == a) { nConsecNumber++; nConsecCharType++; } }
102
				nTmpNumber = a;
103
				nNumber++;
104
			}
105
			else if (arrPwd[a].match(/[^a-zA-Z0-9_]/g)) { 
106
				if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
107
				if (nTmpSymbol !== "") { if ((nTmpSymbol + 1) == a) { nConsecSymbol++; nConsecCharType++; } }
108
				nTmpSymbol = a;
109
				nSymbol++;
110
			}
111
			/* Internal loop through password to check for repeat characters */
112
			var bCharExists = false;
113
			for (var b=0; b < arrPwdLen; b++) {
114
				if (arrPwd[a] == arrPwd[b] && a != b) { /* repeat character exists */
115
					bCharExists = true;
116
					/* 
117
					Calculate icrement deduction based on proximity to identical characters
118
					Deduction is incremented each time a new match is discovered
119
					Deduction amount is based on total password length divided by the
120
					difference of distance between currently selected match
121
					*/
122
					nRepInc += Math.abs(arrPwdLen/(b-a));
123
				}
124
			}
125
			if (bCharExists) { 
126
				nRepChar++; 
127
				nUnqChar = arrPwdLen-nRepChar;
128
				nRepInc = (nUnqChar) ? Math.ceil(nRepInc/nUnqChar) : Math.ceil(nRepInc); 
129
			}
130
		}
131
 
132
		/* Check for sequential alpha string patterns (forward and reverse) */
133
		for (var s=0; s < 23; s++) {
134
			var sFwd = sAlphas.substring(s,parseInt(s+3));
135
			var sRev = sFwd.strReverse();
136
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqAlpha++; nSeqChar++;}
137
		}
138
 
139
		/* Check for sequential numeric string patterns (forward and reverse) */
140
		for (var s=0; s < 8; s++) {
141
			var sFwd = sNumerics.substring(s,parseInt(s+3));
142
			var sRev = sFwd.strReverse();
143
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqNumber++; nSeqChar++;}
144
		}
145
 
146
		/* Check for sequential symbol string patterns (forward and reverse) */
147
		for (var s=0; s < 8; s++) {
148
			var sFwd = sSymbols.substring(s,parseInt(s+3));
149
			var sRev = sFwd.strReverse();
150
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqSymbol++; nSeqChar++;}
151
		}
152
 
153
	/* Modify overall score value based on usage vs requirements */
154
 
155
		/* General point assignment */
156
		$("nLengthBonus").innerHTML = "+ " + nScore; 
157
		if (nAlphaUC > 0 && nAlphaUC < nLength) {	
158
			nScore = parseInt(nScore + ((nLength - nAlphaUC) * 2));
159
			sAlphaUC = "+ " + parseInt((nLength - nAlphaUC) * 2); 
160
		}
161
		if (nAlphaLC > 0 && nAlphaLC < nLength) {	
162
			nScore = parseInt(nScore + ((nLength - nAlphaLC) * 2)); 
163
			sAlphaLC = "+ " + parseInt((nLength - nAlphaLC) * 2);
164
		}
165
		if (nNumber > 0 && nNumber < nLength) {	
166
			nScore = parseInt(nScore + (nNumber * nMultNumber));
167
			sNumber = "+ " + parseInt(nNumber * nMultNumber);
168
		}
169
		if (nSymbol > 0) {	
170
			nScore = parseInt(nScore + (nSymbol * nMultSymbol));
171
			sSymbol = "+ " + parseInt(nSymbol * nMultSymbol);
172
		}
173
		if (nMidChar > 0) {	
174
			nScore = parseInt(nScore + (nMidChar * nMultMidChar));
175
			sMidChar = "+ " + parseInt(nMidChar * nMultMidChar);
176
		}
177
		$("nAlphaUCBonus").innerHTML = sAlphaUC; 
178
		$("nAlphaLCBonus").innerHTML = sAlphaLC;
179
		$("nNumberBonus").innerHTML = sNumber;
180
		$("nSymbolBonus").innerHTML = sSymbol;
181
		$("nMidCharBonus").innerHTML = sMidChar;
182
 
183
		/* Point deductions for poor practices */
184
		if ((nAlphaLC > 0 || nAlphaUC > 0) && nSymbol === 0 && nNumber === 0) {  // Only Letters
185
			nScore = parseInt(nScore - nLength);
186
			nAlphasOnly = nLength;
187
			sAlphasOnly = "- " + nLength;
188
		}
189
		if (nAlphaLC === 0 && nAlphaUC === 0 && nSymbol === 0 && nNumber > 0) {  // Only Numbers
190
			nScore = parseInt(nScore - nLength); 
191
			nNumbersOnly = nLength;
192
			sNumbersOnly = "- " + nLength;
193
		}
194
		if (nRepChar > 0) {  // Same character exists more than once
195
			nScore = parseInt(nScore - nRepInc);
196
			sRepChar = "- " + nRepInc;
197
		}
198
		if (nConsecAlphaUC > 0) {  // Consecutive Uppercase Letters exist
199
			nScore = parseInt(nScore - (nConsecAlphaUC * nMultConsecAlphaUC)); 
200
			sConsecAlphaUC = "- " + parseInt(nConsecAlphaUC * nMultConsecAlphaUC);
201
		}
202
		if (nConsecAlphaLC > 0) {  // Consecutive Lowercase Letters exist
203
			nScore = parseInt(nScore - (nConsecAlphaLC * nMultConsecAlphaLC)); 
204
			sConsecAlphaLC = "- " + parseInt(nConsecAlphaLC * nMultConsecAlphaLC);
205
		}
206
		if (nConsecNumber > 0) {  // Consecutive Numbers exist
207
			nScore = parseInt(nScore - (nConsecNumber * nMultConsecNumber));  
208
			sConsecNumber = "- " + parseInt(nConsecNumber * nMultConsecNumber);
209
		}
210
		if (nSeqAlpha > 0) {  // Sequential alpha strings exist (3 characters or more)
211
			nScore = parseInt(nScore - (nSeqAlpha * nMultSeqAlpha)); 
212
			sSeqAlpha = "- " + parseInt(nSeqAlpha * nMultSeqAlpha);
213
		}
214
		if (nSeqNumber > 0) {  // Sequential numeric strings exist (3 characters or more)
215
			nScore = parseInt(nScore - (nSeqNumber * nMultSeqNumber)); 
216
			sSeqNumber = "- " + parseInt(nSeqNumber * nMultSeqNumber);
217
		}
218
		if (nSeqSymbol > 0) {  // Sequential symbol strings exist (3 characters or more)
219
			nScore = parseInt(nScore - (nSeqSymbol * nMultSeqSymbol)); 
220
			sSeqSymbol = "- " + parseInt(nSeqSymbol * nMultSeqSymbol);
221
		}
222
		$("nAlphasOnlyBonus").innerHTML = sAlphasOnly; 
223
		$("nNumbersOnlyBonus").innerHTML = sNumbersOnly; 
224
		$("nRepCharBonus").innerHTML = sRepChar; 
225
		$("nConsecAlphaUCBonus").innerHTML = sConsecAlphaUC; 
226
		$("nConsecAlphaLCBonus").innerHTML = sConsecAlphaLC; 
227
		$("nConsecNumberBonus").innerHTML = sConsecNumber;
228
		$("nSeqAlphaBonus").innerHTML = sSeqAlpha; 
229
		$("nSeqNumberBonus").innerHTML = sSeqNumber; 
230
		$("nSeqSymbolBonus").innerHTML = sSeqSymbol; 
231
 
232
		/* Determine if mandatory requirements have been met and set image indicators accordingly */
233
		var arrChars = [nLength,nAlphaUC,nAlphaLC,nNumber,nSymbol];
234
		var arrCharsIds = ["nLength","nAlphaUC","nAlphaLC","nNumber","nSymbol"];
235
		var arrCharsLen = arrChars.length;
236
		for (var c=0; c < arrCharsLen; c++) {
237
			var oImg = $('div_' + arrCharsIds[c]);
238
			var oBonus = $(arrCharsIds[c] + 'Bonus');
239
			$(arrCharsIds[c]).innerHTML = arrChars[c];
240
			if (arrCharsIds[c] == "nLength") { var minVal = parseInt(nMinPwdLen - 1); } else { var minVal = 0; }
241
			if (arrChars[c] == parseInt(minVal + 1)) { nReqChar++; oImg.className = "pass"; oBonus.parentNode.className = "pass"; }
242
			else if (arrChars[c] > parseInt(minVal + 1)) { nReqChar++; oImg.className = "exceed"; oBonus.parentNode.className = "exceed"; }
243
			else { oImg.className = "fail"; oBonus.parentNode.className = "fail"; }
244
		}
245
		nRequirements = nReqChar;
246
		if (pwd.length >= nMinPwdLen) { var nMinReqChars = 3; } else { var nMinReqChars = 4; }
247
		if (nRequirements > nMinReqChars) {  // One or more required characters exist
248
			nScore = parseInt(nScore + (nRequirements * 2)); 
249
			sRequirements = "+ " + parseInt(nRequirements * 2);
250
		}
251
		$("nRequirementsBonus").innerHTML = sRequirements;
252
 
253
		/* Determine if additional bonuses need to be applied and set image indicators accordingly */
254
		var arrChars = [nMidChar,nRequirements];
255
		var arrCharsIds = ["nMidChar","nRequirements"];
256
		var arrCharsLen = arrChars.length;
257
		for (var c=0; c < arrCharsLen; c++) {
258
			var oImg = $('div_' + arrCharsIds[c]);
259
			var oBonus = $(arrCharsIds[c] + 'Bonus');
260
			$(arrCharsIds[c]).innerHTML = arrChars[c];
261
			if (arrCharsIds[c] == "nRequirements") { var minVal = nMinReqChars; } else { var minVal = 0; }
262
			if (arrChars[c] == parseInt(minVal + 1)) { oImg.className = "pass"; oBonus.parentNode.className = "pass"; }
263
			else if (arrChars[c] > parseInt(minVal + 1)) { oImg.className = "exceed"; oBonus.parentNode.className = "exceed"; }
264
			else { oImg.className = "fail"; oBonus.parentNode.className = "fail"; }
265
		}
266
 
267
		/* Determine if suggested requirements have been met and set image indicators accordingly */
268
		var arrChars = [nAlphasOnly,nNumbersOnly,nRepChar,nConsecAlphaUC,nConsecAlphaLC,nConsecNumber,nSeqAlpha,nSeqNumber,nSeqSymbol];
269
		var arrCharsIds = ["nAlphasOnly","nNumbersOnly","nRepChar","nConsecAlphaUC","nConsecAlphaLC","nConsecNumber","nSeqAlpha","nSeqNumber","nSeqSymbol"];
270
		var arrCharsLen = arrChars.length;
271
		for (var c=0; c < arrCharsLen; c++) {
272
			var oImg = $('div_' + arrCharsIds[c]);
273
			var oBonus = $(arrCharsIds[c] + 'Bonus');
274
			$(arrCharsIds[c]).innerHTML = arrChars[c];
275
			if (arrChars[c] > 0) { oImg.className = "warn"; oBonus.parentNode.className = "warn"; }
276
			else { oImg.className = "pass"; oBonus.parentNode.className = "pass"; }
277
		}
278
 
279
		/* Determine complexity based on overall score */
280
		if (nScore > 100) { nScore = 100; } else if (nScore < 0) { nScore = 0; }
3026 rexy 281
		if(nScore < 0){}
282
		else if (nScore < 20) {
283
			sColor = "bg-dark";
284
			sComplexity = "Tr&egrave;s Faible";
285
		}
286
		else if (nScore < 40) {
287
			sColor = "bg-danger";
288
			sComplexity = "Faible"; }
289
		else if (nScore < 60) {
290
			sColor = "bg-warning";
291
			sComplexity = "Moyen"; }
292
		else if (nScore < 80) {
293
			sColor = "bg-info";
294
			sComplexity = "Bon";
295
		}
296
		else if (nScore <= 100) {
297
			sColor = "bg-success";
298
			sComplexity = "Tr&egrave;s bon";
299
		}
2242 tom.houday 300
 
301
		/* Display updated score criteria to client */
3026 rexy 302
		oBar.innerHTML = nScore + "% / "+sComplexity;
303
		oBar.style.width = nScore + "%";
304
		oBar.className = "progress-bar " + sColor;
2242 tom.houday 305
	}
306
	else {
307
		/* Display default score criteria to client */
308
		initPwdChk();
3026 rexy 309
		oBar.innerHTML = nScore + "%"+sComplexity;
310
		oBar.style.width = nScore + "%";
311
		oBar.className = "progress-bar";
2242 tom.houday 312
	}
313
}
314
 
315
function initPwdChk(restart) {
316
	/* Reset all form values to their default */
317
	var arrZeros = ["nLength","nAlphaUC","nAlphaLC","nNumber","nSymbol","nMidChar","nRequirements","nAlphasOnly","nNumbersOnly","nRepChar","nConsecAlphaUC","nConsecAlphaLC","nConsecNumber","nSeqAlpha","nSeqNumber","nSeqSymbol","nLengthBonus","nAlphaUCBonus","nAlphaLCBonus","nNumberBonus","nSymbolBonus","nMidCharBonus","nRequirementsBonus","nAlphasOnlyBonus","nNumbersOnlyBonus","nRepCharBonus","nConsecAlphaUCBonus","nConsecAlphaLCBonus","nConsecNumberBonus","nSeqAlphaBonus","nSeqNumberBonus","nSeqSymbolBonus"];
318
	var arrPassPars = ["nAlphasOnlyBonus","nNumbersOnlyBonus","nRepCharBonus","nConsecAlphaUCBonus","nConsecAlphaLCBonus","nConsecNumberBonus","nSeqAlphaBonus","nSeqNumberBonus","nSeqSymbolBonus"];
319
	var arrPassDivs = ["div_nAlphasOnly","div_nNumbersOnly","div_nRepChar","div_nConsecAlphaUC","div_nConsecAlphaLC","div_nConsecNumber","div_nSeqAlpha","div_nSeqNumber","div_nSeqSymbol"];
320
	var arrFailPars = ["nLengthBonus","nAlphaUCBonus","nAlphaLCBonus","nNumberBonus","nSymbolBonus","nMidCharBonus","nRequirementsBonus"];
321
	var arrFailDivs = ["div_nLength","div_nAlphaUC","div_nAlphaLC","div_nNumber","div_nSymbol","div_nMidChar","div_nRequirements"];
322
	for (var i in arrZeros) { $(arrZeros[i]).innerHTML = "0"; }
323
	for (var i in arrPassPars) { $(arrPassPars[i]).parentNode.className = "pass"; }
324
	for (var i in arrPassDivs) { $(arrPassDivs[i]).className = "pass"; }
325
	for (var i in arrFailPars) { $(arrFailPars[i]).parentNode.className = "fail"; }
326
	for (var i in arrFailDivs) { $(arrFailDivs[i]).className = "fail"; }
327
	$("passwordPwd").value = "";
328
	$("passwordTxt").value = "";
329
	$("scorebar").style.backgroundPosition = "0";
330
	if (restart) {
331
		$("passwordPwd").className = "";
332
		$("passwordTxt").className = "hide";
333
	}
334
}
335
 
336
addLoadEvent(function() { initPwdChk(1); });
337