Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | 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) {
63
	var oScorebar = $("scorebar");
64
	var oScore = $("score");
65
	var oComplexity = $("complexity");
66
	// Simultaneous variable declaration and value assignment aren't supported in IE apparently
67
	// so I'm forced to assign the same value individually per var to support a crappy browser *sigh* 
68
	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;
69
	var nMultRepChar=1, nMultConsecSymbol=1;
70
	var nMultMidChar=2, nMultRequirements=2, nMultConsecAlphaUC=2, nMultConsecAlphaLC=2, nMultConsecNumber=2;
71
	var nReqCharType=3, nMultAlphaUC=3, nMultAlphaLC=3, nMultSeqAlpha=3, nMultSeqNumber=3, nMultSeqSymbol=3;
72
	var nMultLength=4, nMultNumber=4;
73
	var nMultSymbol=6;
74
	var nTmpAlphaUC="", nTmpAlphaLC="", nTmpNumber="", nTmpSymbol="";
75
	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";
76
	var sAlphas = "abcdefghijklmnopqrstuvwxyz";
77
	var sNumerics = "01234567890";
78
	var sSymbols = ")!@#$%^&*()";
79
	var sComplexity = "Trop court";
80
	var sStandards = "Below";
81
	var nMinPwdLen = 8;
82
	if (document.all) { var nd = 0; } else { var nd = 1; }
83
	if (pwd) {
84
		nScore = parseInt(pwd.length * nMultLength);
85
		nLength = pwd.length;
86
		var arrPwd = pwd.replace(/\s+/g,"").split(/\s*/);
87
		var arrPwdLen = arrPwd.length;
88
 
89
		/* Loop through password to check for Symbol, Numeric, Lowercase and Uppercase pattern matches */
90
		for (var a=0; a < arrPwdLen; a++) {
91
			if (arrPwd[a].match(/[A-Z]/g)) {
92
				if (nTmpAlphaUC !== "") { if ((nTmpAlphaUC + 1) == a) { nConsecAlphaUC++; nConsecCharType++; } }
93
				nTmpAlphaUC = a;
94
				nAlphaUC++;
95
			}
96
			else if (arrPwd[a].match(/[a-z]/g)) { 
97
				if (nTmpAlphaLC !== "") { if ((nTmpAlphaLC + 1) == a) { nConsecAlphaLC++; nConsecCharType++; } }
98
				nTmpAlphaLC = a;
99
				nAlphaLC++;
100
			}
101
			else if (arrPwd[a].match(/[0-9]/g)) { 
102
				if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
103
				if (nTmpNumber !== "") { if ((nTmpNumber + 1) == a) { nConsecNumber++; nConsecCharType++; } }
104
				nTmpNumber = a;
105
				nNumber++;
106
			}
107
			else if (arrPwd[a].match(/[^a-zA-Z0-9_]/g)) { 
108
				if (a > 0 && a < (arrPwdLen - 1)) { nMidChar++; }
109
				if (nTmpSymbol !== "") { if ((nTmpSymbol + 1) == a) { nConsecSymbol++; nConsecCharType++; } }
110
				nTmpSymbol = a;
111
				nSymbol++;
112
			}
113
			/* Internal loop through password to check for repeat characters */
114
			var bCharExists = false;
115
			for (var b=0; b < arrPwdLen; b++) {
116
				if (arrPwd[a] == arrPwd[b] && a != b) { /* repeat character exists */
117
					bCharExists = true;
118
					/* 
119
					Calculate icrement deduction based on proximity to identical characters
120
					Deduction is incremented each time a new match is discovered
121
					Deduction amount is based on total password length divided by the
122
					difference of distance between currently selected match
123
					*/
124
					nRepInc += Math.abs(arrPwdLen/(b-a));
125
				}
126
			}
127
			if (bCharExists) { 
128
				nRepChar++; 
129
				nUnqChar = arrPwdLen-nRepChar;
130
				nRepInc = (nUnqChar) ? Math.ceil(nRepInc/nUnqChar) : Math.ceil(nRepInc); 
131
			}
132
		}
133
 
134
		/* Check for sequential alpha string patterns (forward and reverse) */
135
		for (var s=0; s < 23; s++) {
136
			var sFwd = sAlphas.substring(s,parseInt(s+3));
137
			var sRev = sFwd.strReverse();
138
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqAlpha++; nSeqChar++;}
139
		}
140
 
141
		/* Check for sequential numeric string patterns (forward and reverse) */
142
		for (var s=0; s < 8; s++) {
143
			var sFwd = sNumerics.substring(s,parseInt(s+3));
144
			var sRev = sFwd.strReverse();
145
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqNumber++; nSeqChar++;}
146
		}
147
 
148
		/* Check for sequential symbol string patterns (forward and reverse) */
149
		for (var s=0; s < 8; s++) {
150
			var sFwd = sSymbols.substring(s,parseInt(s+3));
151
			var sRev = sFwd.strReverse();
152
			if (pwd.toLowerCase().indexOf(sFwd) != -1 || pwd.toLowerCase().indexOf(sRev) != -1) { nSeqSymbol++; nSeqChar++;}
153
		}
154
 
155
	/* Modify overall score value based on usage vs requirements */
156
 
157
		/* General point assignment */
158
		$("nLengthBonus").innerHTML = "+ " + nScore; 
159
		if (nAlphaUC > 0 && nAlphaUC < nLength) {	
160
			nScore = parseInt(nScore + ((nLength - nAlphaUC) * 2));
161
			sAlphaUC = "+ " + parseInt((nLength - nAlphaUC) * 2); 
162
		}
163
		if (nAlphaLC > 0 && nAlphaLC < nLength) {	
164
			nScore = parseInt(nScore + ((nLength - nAlphaLC) * 2)); 
165
			sAlphaLC = "+ " + parseInt((nLength - nAlphaLC) * 2);
166
		}
167
		if (nNumber > 0 && nNumber < nLength) {	
168
			nScore = parseInt(nScore + (nNumber * nMultNumber));
169
			sNumber = "+ " + parseInt(nNumber * nMultNumber);
170
		}
171
		if (nSymbol > 0) {	
172
			nScore = parseInt(nScore + (nSymbol * nMultSymbol));
173
			sSymbol = "+ " + parseInt(nSymbol * nMultSymbol);
174
		}
175
		if (nMidChar > 0) {	
176
			nScore = parseInt(nScore + (nMidChar * nMultMidChar));
177
			sMidChar = "+ " + parseInt(nMidChar * nMultMidChar);
178
		}
179
		$("nAlphaUCBonus").innerHTML = sAlphaUC; 
180
		$("nAlphaLCBonus").innerHTML = sAlphaLC;
181
		$("nNumberBonus").innerHTML = sNumber;
182
		$("nSymbolBonus").innerHTML = sSymbol;
183
		$("nMidCharBonus").innerHTML = sMidChar;
184
 
185
		/* Point deductions for poor practices */
186
		if ((nAlphaLC > 0 || nAlphaUC > 0) && nSymbol === 0 && nNumber === 0) {  // Only Letters
187
			nScore = parseInt(nScore - nLength);
188
			nAlphasOnly = nLength;
189
			sAlphasOnly = "- " + nLength;
190
		}
191
		if (nAlphaLC === 0 && nAlphaUC === 0 && nSymbol === 0 && nNumber > 0) {  // Only Numbers
192
			nScore = parseInt(nScore - nLength); 
193
			nNumbersOnly = nLength;
194
			sNumbersOnly = "- " + nLength;
195
		}
196
		if (nRepChar > 0) {  // Same character exists more than once
197
			nScore = parseInt(nScore - nRepInc);
198
			sRepChar = "- " + nRepInc;
199
		}
200
		if (nConsecAlphaUC > 0) {  // Consecutive Uppercase Letters exist
201
			nScore = parseInt(nScore - (nConsecAlphaUC * nMultConsecAlphaUC)); 
202
			sConsecAlphaUC = "- " + parseInt(nConsecAlphaUC * nMultConsecAlphaUC);
203
		}
204
		if (nConsecAlphaLC > 0) {  // Consecutive Lowercase Letters exist
205
			nScore = parseInt(nScore - (nConsecAlphaLC * nMultConsecAlphaLC)); 
206
			sConsecAlphaLC = "- " + parseInt(nConsecAlphaLC * nMultConsecAlphaLC);
207
		}
208
		if (nConsecNumber > 0) {  // Consecutive Numbers exist
209
			nScore = parseInt(nScore - (nConsecNumber * nMultConsecNumber));  
210
			sConsecNumber = "- " + parseInt(nConsecNumber * nMultConsecNumber);
211
		}
212
		if (nSeqAlpha > 0) {  // Sequential alpha strings exist (3 characters or more)
213
			nScore = parseInt(nScore - (nSeqAlpha * nMultSeqAlpha)); 
214
			sSeqAlpha = "- " + parseInt(nSeqAlpha * nMultSeqAlpha);
215
		}
216
		if (nSeqNumber > 0) {  // Sequential numeric strings exist (3 characters or more)
217
			nScore = parseInt(nScore - (nSeqNumber * nMultSeqNumber)); 
218
			sSeqNumber = "- " + parseInt(nSeqNumber * nMultSeqNumber);
219
		}
220
		if (nSeqSymbol > 0) {  // Sequential symbol strings exist (3 characters or more)
221
			nScore = parseInt(nScore - (nSeqSymbol * nMultSeqSymbol)); 
222
			sSeqSymbol = "- " + parseInt(nSeqSymbol * nMultSeqSymbol);
223
		}
224
		$("nAlphasOnlyBonus").innerHTML = sAlphasOnly; 
225
		$("nNumbersOnlyBonus").innerHTML = sNumbersOnly; 
226
		$("nRepCharBonus").innerHTML = sRepChar; 
227
		$("nConsecAlphaUCBonus").innerHTML = sConsecAlphaUC; 
228
		$("nConsecAlphaLCBonus").innerHTML = sConsecAlphaLC; 
229
		$("nConsecNumberBonus").innerHTML = sConsecNumber;
230
		$("nSeqAlphaBonus").innerHTML = sSeqAlpha; 
231
		$("nSeqNumberBonus").innerHTML = sSeqNumber; 
232
		$("nSeqSymbolBonus").innerHTML = sSeqSymbol; 
233
 
234
		/* Determine if mandatory requirements have been met and set image indicators accordingly */
235
		var arrChars = [nLength,nAlphaUC,nAlphaLC,nNumber,nSymbol];
236
		var arrCharsIds = ["nLength","nAlphaUC","nAlphaLC","nNumber","nSymbol"];
237
		var arrCharsLen = arrChars.length;
238
		for (var c=0; c < arrCharsLen; c++) {
239
			var oImg = $('div_' + arrCharsIds[c]);
240
			var oBonus = $(arrCharsIds[c] + 'Bonus');
241
			$(arrCharsIds[c]).innerHTML = arrChars[c];
242
			if (arrCharsIds[c] == "nLength") { var minVal = parseInt(nMinPwdLen - 1); } else { var minVal = 0; }
243
			if (arrChars[c] == parseInt(minVal + 1)) { nReqChar++; oImg.className = "pass"; oBonus.parentNode.className = "pass"; }
244
			else if (arrChars[c] > parseInt(minVal + 1)) { nReqChar++; oImg.className = "exceed"; oBonus.parentNode.className = "exceed"; }
245
			else { oImg.className = "fail"; oBonus.parentNode.className = "fail"; }
246
		}
247
		nRequirements = nReqChar;
248
		if (pwd.length >= nMinPwdLen) { var nMinReqChars = 3; } else { var nMinReqChars = 4; }
249
		if (nRequirements > nMinReqChars) {  // One or more required characters exist
250
			nScore = parseInt(nScore + (nRequirements * 2)); 
251
			sRequirements = "+ " + parseInt(nRequirements * 2);
252
		}
253
		$("nRequirementsBonus").innerHTML = sRequirements;
254
 
255
		/* Determine if additional bonuses need to be applied and set image indicators accordingly */
256
		var arrChars = [nMidChar,nRequirements];
257
		var arrCharsIds = ["nMidChar","nRequirements"];
258
		var arrCharsLen = arrChars.length;
259
		for (var c=0; c < arrCharsLen; c++) {
260
			var oImg = $('div_' + arrCharsIds[c]);
261
			var oBonus = $(arrCharsIds[c] + 'Bonus');
262
			$(arrCharsIds[c]).innerHTML = arrChars[c];
263
			if (arrCharsIds[c] == "nRequirements") { var minVal = nMinReqChars; } else { var minVal = 0; }
264
			if (arrChars[c] == parseInt(minVal + 1)) { oImg.className = "pass"; oBonus.parentNode.className = "pass"; }
265
			else if (arrChars[c] > parseInt(minVal + 1)) { oImg.className = "exceed"; oBonus.parentNode.className = "exceed"; }
266
			else { oImg.className = "fail"; oBonus.parentNode.className = "fail"; }
267
		}
268
 
269
		/* Determine if suggested requirements have been met and set image indicators accordingly */
270
		var arrChars = [nAlphasOnly,nNumbersOnly,nRepChar,nConsecAlphaUC,nConsecAlphaLC,nConsecNumber,nSeqAlpha,nSeqNumber,nSeqSymbol];
271
		var arrCharsIds = ["nAlphasOnly","nNumbersOnly","nRepChar","nConsecAlphaUC","nConsecAlphaLC","nConsecNumber","nSeqAlpha","nSeqNumber","nSeqSymbol"];
272
		var arrCharsLen = arrChars.length;
273
		for (var c=0; c < arrCharsLen; c++) {
274
			var oImg = $('div_' + arrCharsIds[c]);
275
			var oBonus = $(arrCharsIds[c] + 'Bonus');
276
			$(arrCharsIds[c]).innerHTML = arrChars[c];
277
			if (arrChars[c] > 0) { oImg.className = "warn"; oBonus.parentNode.className = "warn"; }
278
			else { oImg.className = "pass"; oBonus.parentNode.className = "pass"; }
279
		}
280
 
281
		/* Determine complexity based on overall score */
282
		if (nScore > 100) { nScore = 100; } else if (nScore < 0) { nScore = 0; }
283
		if (nScore >= 0 && nScore < 20) { sComplexity = "Tr&egrave;s Faible"; }
284
		else if (nScore >= 20 && nScore < 40) { sComplexity = "Faible"; }
285
		else if (nScore >= 40 && nScore < 60) { sComplexity = "Moyen"; }
286
		else if (nScore >= 60 && nScore < 80) { sComplexity = "Bon"; }
287
		else if (nScore >= 80 && nScore <= 100) { sComplexity = "Tr&egrave;s bon"; }
288
 
289
		/* Display updated score criteria to client */
290
		oScorebar.style.backgroundPosition = "-" + parseInt(nScore * 4) + "px";
291
		oScore.innerHTML = nScore + "%";
292
		oComplexity.innerHTML = sComplexity;
293
	}
294
	else {
295
		/* Display default score criteria to client */
296
		initPwdChk();
297
		oScore.innerHTML = nScore + "%";
298
		oComplexity.innerHTML = sComplexity;
299
	}
300
}
301
 
302
function initPwdChk(restart) {
303
	/* Reset all form values to their default */
304
	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"];
305
	var arrPassPars = ["nAlphasOnlyBonus","nNumbersOnlyBonus","nRepCharBonus","nConsecAlphaUCBonus","nConsecAlphaLCBonus","nConsecNumberBonus","nSeqAlphaBonus","nSeqNumberBonus","nSeqSymbolBonus"];
306
	var arrPassDivs = ["div_nAlphasOnly","div_nNumbersOnly","div_nRepChar","div_nConsecAlphaUC","div_nConsecAlphaLC","div_nConsecNumber","div_nSeqAlpha","div_nSeqNumber","div_nSeqSymbol"];
307
	var arrFailPars = ["nLengthBonus","nAlphaUCBonus","nAlphaLCBonus","nNumberBonus","nSymbolBonus","nMidCharBonus","nRequirementsBonus"];
308
	var arrFailDivs = ["div_nLength","div_nAlphaUC","div_nAlphaLC","div_nNumber","div_nSymbol","div_nMidChar","div_nRequirements"];
309
	for (var i in arrZeros) { $(arrZeros[i]).innerHTML = "0"; }
310
	for (var i in arrPassPars) { $(arrPassPars[i]).parentNode.className = "pass"; }
311
	for (var i in arrPassDivs) { $(arrPassDivs[i]).className = "pass"; }
312
	for (var i in arrFailPars) { $(arrFailPars[i]).parentNode.className = "fail"; }
313
	for (var i in arrFailDivs) { $(arrFailDivs[i]).className = "fail"; }
314
	$("passwordPwd").value = "";
315
	$("passwordTxt").value = "";
316
	$("scorebar").style.backgroundPosition = "0";
317
	if (restart) {
318
		$("passwordPwd").className = "";
319
		$("passwordTxt").className = "hide";
320
	}
321
}
322
 
323
addLoadEvent(function() { initPwdChk(1); });
324