Completed
Pull Request — release-2.1 (#4823)
by
unknown
08:48
created

Themes/default/scripts/register.js (2 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
function smfRegister(formID, passwordDifficultyLevel, regTextStrings)
2
{
3
	this.addVerify = addVerificationField;
4
	this.autoSetup = autoSetup;
5
	this.refreshMainPassword = refreshMainPassword;
6
	this.refreshVerifyPassword = refreshVerifyPassword;
7
8
	var verificationFields = new Array();
9
	var verificationFieldLength = 0;
10
	var textStrings = regTextStrings ? regTextStrings : new Array();
11
	var passwordLevel = passwordDifficultyLevel ? passwordDifficultyLevel : 0;
12
13
	// Setup all the fields!
14
	autoSetup(formID);
15
16
	// This is a field which requires some form of verification check.
17
	function addVerificationField(fieldType, fieldID)
18
	{
19
		// Check the field exists.
20
		if (!document.getElementById(fieldID))
21
			return;
22
23
		// Get the handles.
24
		var inputHandle = document.getElementById(fieldID);
25
		var imageHandle = document.getElementById(fieldID + '_img') ? document.getElementById(fieldID + '_img') : false;
26
		var divHandle = document.getElementById(fieldID + '_div') ? document.getElementById(fieldID + '_div') : false;
27
28
		// What is the event handler?
29
		var eventHandler = false;
30
		if (fieldType == 'pwmain')
31
			eventHandler = refreshMainPassword;
32
		else if (fieldType == 'pwverify')
33
			eventHandler = refreshVerifyPassword;
34
		else if (fieldType == 'username')
35
			eventHandler = refreshUsername;
36
		else if (fieldType == 'reserved')
37
			eventHandler = refreshMainPassword;
38
39
		// Store this field.
40
		var vFieldIndex = fieldType == 'reserved' ? fieldType + verificationFieldLength : fieldType;
41
		verificationFields[vFieldIndex] = Array(6);
42
		verificationFields[vFieldIndex][0] = fieldID;
43
		verificationFields[vFieldIndex][1] = inputHandle;
44
		verificationFields[vFieldIndex][2] = imageHandle;
45
		verificationFields[vFieldIndex][3] = divHandle;
46
		verificationFields[vFieldIndex][4] = fieldType;
47
		verificationFields[vFieldIndex][5] = inputHandle.className;
48
49
		// Keep a count to it!
50
		verificationFieldLength++;
51
52
		// Step to it!
53
		if (eventHandler)
54
		{
55
			createEventListener(inputHandle);
56
			inputHandle.addEventListener('keyup', eventHandler, false);
57
			eventHandler();
58
59
			// Username will auto check on blur!
60
			inputHandle.addEventListener('blur', autoCheckUsername, false);
61
		}
62
63
		// Make the div visible!
64
		if (divHandle)
65
			divHandle.style.display = '';
66
	}
67
68
	// A button to trigger a username search?
69
	function addUsernameSearchTrigger(elementID)
70
	{
71
		var buttonHandle = document.getElementById(elementID);
72
73
		// Attach the event to this element.
74
		createEventListener(buttonHandle);
75
		buttonHandle.addEventListener('click', checkUsername, false);
76
	}
77
78
	// This function will automatically pick up all the necessary verification fields and initialise their visual status.
79
	function autoSetup(formID)
80
	{
81
		if (!document.getElementById(formID))
82
			return false;
83
84
		var curElement, curType;
85
		for (var i = 0, n = document.getElementById(formID).elements.length; i < n; i++)
86
		{
87
			curElement = document.getElementById(formID).elements[i];
88
89
			// Does the ID contain the keyword 'autov'?
90
			if (curElement.id.indexOf('autov') != -1 && (curElement.type == 'text' || curElement.type == 'password'))
91
			{
92
				// This is probably it - but does it contain a field type?
93
				curType = 0;
94
				// Username can only be done with XML.
95
				if (curElement.id.indexOf('username') != -1 && window.XMLHttpRequest)
96
					curType = 'username';
97
				else if (curElement.id.indexOf('pwmain') != -1)
98
					curType = 'pwmain';
99
				else if (curElement.id.indexOf('pwverify') != -1)
100
					curType = 'pwverify';
101
				// This means this field is reserved and cannot be contained in the password!
102
				else if (curElement.id.indexOf('reserve') != -1)
103
					curType = 'reserved';
104
105
				// If we're happy let's add this element!
106
				if (curType)
107
					addVerificationField(curType, curElement.id);
108
109
				// If this is the username do we also have a button to find the user?
110
				if (curType == 'username' && document.getElementById(curElement.id + '_link'))
111
				{
112
					addUsernameSearchTrigger(curElement.id + '_link');
113
				}
114
			}
115
		}
116
117
		return true;
118
	}
119
120
	// What is the password state?
121
	function refreshMainPassword(called_from_verify)
122
	{
123
		if (!verificationFields['pwmain'])
124
			return false;
125
126
		var curPass = verificationFields['pwmain'][1].value;
127
		var stringIndex = '';
128
129
		// Is it a valid length?
130
		if ((curPass.length < 8 && passwordLevel >= 1) || curPass.length < 4)
131
			stringIndex = 'password_short';
132
133
		// More than basic?
134
		if (passwordLevel >= 1)
135
		{
136
			// If there is a username check it's not in the password!
137
			if (verificationFields['username'] && verificationFields['username'][1].value && curPass.indexOf(verificationFields['username'][1].value) != -1)
138
				stringIndex = 'password_reserved';
139
140
			// Any reserved fields?
141
			for (var i in verificationFields)
142
			{
143
				if (verificationFields[i][4] == 'reserved' && verificationFields[i][1].value && curPass.indexOf(verificationFields[i][1].value) != -1)
144
					stringIndex = 'password_reserved';
145
			}
146
147
			// Finally - is it hard and as such requiring mixed cases and numbers?
148
			if (passwordLevel > 1)
149
			{
150
				if (curPass == curPass.toLowerCase())
151
					stringIndex = 'password_numbercase';
152
				if (!curPass.match(/(\D\d|\d\D)/))
153
					stringIndex = 'password_numbercase';
154
			}
155
		}
156
157
		var isValid = stringIndex == '' ? true : false;
158
		if (stringIndex == '')
159
			stringIndex = 'password_valid';
160
161
		// Set the image.
162
		setVerificationImage(verificationFields['pwmain'][0], isValid, textStrings[stringIndex] ? textStrings[stringIndex] : '');
163
		verificationFields['pwmain'][1].className = verificationFields['pwmain'][5] + ' ' + (isValid ? 'valid_input' : 'invalid_input');
164
165
		// As this has changed the verification one may have too!
166
		if (verificationFields['pwverify'] && !called_from_verify)
167
			refreshVerifyPassword();
168
169
		return isValid;
170
	}
171
172
	// Check that the verification password matches the main one!
173
	function refreshVerifyPassword()
174
	{
175
		// Can't do anything without something to check again!
176
		if (!verificationFields['pwmain'])
177
			return false;
178
179
		// Check and set valid status!
180
		var isValid = verificationFields['pwmain'][1].value == verificationFields['pwverify'][1].value && refreshMainPassword(true);
181
		var alt = textStrings[isValid == 1 ? 'password_valid' : 'password_no_match'] ? textStrings[isValid == 1 ? 'password_valid' : 'password_no_match'] : '';
182
		setVerificationImage(verificationFields['pwverify'][0], isValid, alt);
183
		verificationFields['pwverify'][1].className = verificationFields['pwverify'][5] + ' ' + (isValid ? 'valid_input' : 'invalid_input');
184
185
		return true;
186
	}
187
188
	// If the username is changed just revert the status of whether it's valid!
189
	function refreshUsername()
190
	{
191
		if (!verificationFields['username'])
192
			return false;
193
194
		// Restore the class name.
195
		if (verificationFields['username'][1].className)
196
			verificationFields['username'][1].className = verificationFields['username'][5];
197
		// Check the image is correct.
198
		var alt = textStrings['username_check'] ? textStrings['username_check'] : '';
199
		setVerificationImage(verificationFields['username'][0], 'check', alt);
200
201
		// Check the password is still OK.
202
		refreshMainPassword();
203
204
		return true;
205
	}
206
207
	// This is a pass through function that ensures we don't do any of the AJAX notification stuff.
208
	function autoCheckUsername()
209
	{
210
		checkUsername(true);
211
	}
212
213
	// Check whether the username exists?
214
	function checkUsername(is_auto)
215
	{
216
		if (!verificationFields['username'])
217
			return false;
218
219
		// Get the username and do nothing without one!
220
		var curUsername = verificationFields['username'][1].value;
221
		if (!curUsername)
222
			return false;
223
224
		if (!is_auto)
225
			ajax_indicator(true);
226
227
		// Request a search on that username.
228
		checkName = curUsername.php_to8bit().php_urlencode();
0 ignored issues
show
The variable checkName seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.checkName.
Loading history...
229
		getXMLDocument(smf_prepareScriptUrl(smf_scripturl) + 'action=signup;sa=usernamecheck;xml;username=' + checkName, checkUsernameCallback);
230
231
		return true;
232
	}
233
234
	// Callback for getting the username data.
235
	function checkUsernameCallback(XMLDoc)
236
	{
237
		if (XMLDoc.getElementsByTagName("username"))
238
			isValid = XMLDoc.getElementsByTagName("username")[0].getAttribute("valid");
0 ignored issues
show
The variable isValid seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.isValid.
Loading history...
239
		else
240
			isValid = true;
241
242
		// What to alt?
243
		var alt = textStrings[isValid == 1 ? 'username_valid' : 'username_invalid'] ? textStrings[isValid == 1 ? 'username_valid' : 'username_invalid'] : '';
244
245
		verificationFields['username'][1].className = verificationFields['username'][5] + ' ' + (isValid == 1 ? 'valid_input' : 'invalid_input');
246
		setVerificationImage(verificationFields['username'][0], isValid == 1, alt);
247
248
		ajax_indicator(false);
249
	}
250
251
	// Set the image to be the correct type.
252
	function setVerificationImage(fieldID, imageIcon, alt)
253
	{
254
		if (!fieldID)
255
			return false;
256
		if (!alt)
257
			alt = '*';
258
259
		$('#' + fieldID + '_img').removeClass('valid check invalid').attr('alt', alt).attr('title', alt);
260
		if (imageIcon)
261
			$('#' + fieldID + '_img').addClass(imageIcon == 'check' ? 'check' : 'valid');
262
		else
263
			$('#' + fieldID + '_img').addClass('invalid');
264
265
		return true;
266
	}
267
}
268
269
function onCheckChange()
270
{
271
	if (document.forms.postForm.emailActivate.checked || document.forms.postForm.password.value == '')
272
	{
273
		document.forms.postForm.emailPassword.disabled = true;
274
		document.forms.postForm.emailPassword.checked = true;
275
	}
276
	else
277
		document.forms.postForm.emailPassword.disabled = false;
278
}