Project

General

Profile

Download (21.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * pfSenseHelpers.js
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * 1. Redistributions of source code must retain the above copyright notice,
12
 *    this list of conditions and the following disclaimer.
13
 *
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in
16
 *    the documentation and/or other materials provided with the
17
 *    distribution.
18
 *
19
 * 3. All advertising materials mentioning features or use of this software
20
 *    must display the following acknowledgment:
21
 *    "This product includes software developed by the pfSense Project
22
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
23
 *
24
 * 4. The names "pfSense" and "pfSense Project" must not be used to
25
 *    endorse or promote products derived from this software without
26
 *    prior written permission. For written permission, please contact
27
 *    coreteam@pfsense.org.
28
 *
29
 * 5. Products derived from this software may not be called "pfSense"
30
 *    nor may "pfSense" appear in their names without prior written
31
 *    permission of the Electric Sheep Fencing, LLC.
32
 *
33
 * 6. Redistributions of any form whatsoever must retain the following
34
 *    acknowledgment:
35
 *
36
 * "This product includes software developed by the pfSense Project
37
 * for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 */
52

    
53
// These helper functions are used on many/most UI pages to hide/show/disable/enable form elements where required
54

    
55
// Hides the <div> in which the specified input element lives so that the input, its label and help text are hidden
56
function hideInput(id, hide) {
57
	if (hide)
58
		$('#' + id).parent().parent('div').addClass('hidden');
59
	else
60
		$('#' + id).parent().parent('div').removeClass('hidden');
61
}
62

    
63
// Hides the <div> in which the specified group input element lives so that the input,
64
// its label and help text are hidden
65
function hideGroupInput(id, hide) {
66
	if (hide)
67
		$('#' + id).parent('div').addClass('hidden');
68
	else
69
		$('#' + id).parent('div').removeClass('hidden');
70
}
71

    
72
// Hides the <div> in which the specified checkbox lives so that the checkbox, its label and help text are hidden
73
function hideCheckbox(id, hide) {
74
	if (hide)
75
		$('#' + id).parent().parent().parent('div').addClass('hidden');
76
	else
77
		$('#' + id).parent().parent().parent('div').removeClass('hidden');
78
}
79

    
80
// Disables the specified input element
81
function disableInput(id, disable) {
82
	$('#' + id).prop("disabled", disable);
83
}
84

    
85
// Hides all elements of the specified class. This will usually be a section
86
function hideClass(s_class, hide) {
87
	if (hide)
88
		$('.' + s_class).hide();
89
	else
90
		$('.' + s_class).show();
91
}
92

    
93
// Hides all elements of the specified class assigned to a group. This will usually be a group
94
function hideGroupClass(s_class, hide) {
95
	if (hide)
96
		$('.' + s_class).parent().parent().parent().hide();
97
	else
98
		$('.' + s_class).parent().parent().parent().show();
99
}
100

    
101
function hideSelect(id, hide) {
102
	if (hide)
103
		$('#' + id).parent('div').parent('div').addClass('hidden');
104
	else
105
		$('#' + id).parent('div').parent('div').removeClass('hidden');
106
}
107

    
108
function hideMultiCheckbox(id, hide) {
109
	if (hide)
110
		$("[name=" + id + "]").parent().addClass('hidden');
111
	else
112
		$("[name=" + id + "]").parent().removeClass('hidden');
113
}
114

    
115
// Hides the <div> in which the specified IP address element lives so that the input, its label and help text are hidden
116
function hideIpAddress(id, hide) {
117
	if (hide)
118
		$('#' + id).parent().parent().parent('div').addClass('hidden');
119
	else
120
		$('#' + id).parent().parent().parent('div').removeClass('hidden');
121
}
122

    
123
// Hides all elements of the specified class belonging to a multiselect.
124
function hideMultiClass(s_class, hide) {
125
	if (hide)
126
		$('.' + s_class).parent().parent().hide();
127
	else
128
		$('.' + s_class).parent().parent().show();
129
}
130

    
131
// Hides div whose label contains the specified text. (Good for StaticText)
132
function hideLabel(text, hide) {
133

    
134
	var element = $('label:contains(' + text + ')');
135

    
136
	if (hide)
137
		element.parent('div').addClass('hidden');
138
	else
139
		element.parent('div').removeClass('hidden');
140
}
141

    
142
// Hides the '/' and the subnet mask of an Ip_Address/subnet_mask group
143
function hideMask(name, hide) {
144
	if(hide) {
145
		$('[id^=' + name + ']').hide();
146
		$('[id^=' + name + ']').prev('span').hide();
147
		$('[id^=' + name + ']').parent('div').removeClass('input-group');
148
	} else {
149
		$('[id^=' + name + ']').show();
150
		$('[id^=' + name + ']').prev('span').show();
151
		$('[id^=' + name + ']').parent('div').addClass('input-group');
152
	}
153
}
154

    
155
// Toggle table row checkboxes and background colors on the pages that use sortable tables:
156
//	/usr/local/www/firewall_nat.php
157
//	/usr/local/www/firewall_nat_1to1.php
158
//	/usr/local/www/firewall_nat_out.php
159
//	/usr/local/www/firewall_rules.php
160
//	/usr/local/www/vpn_ipsec.php
161
// Striping of the tables is handled here, NOT with the Bootstrap table-striped class because it would
162
// get confused when rows are sorted or deleted.
163

    
164
function fr_toggle(id, prefix) {
165
	if (!prefix)
166
		prefix = 'fr';
167

    
168
	var checkbox = document.getElementById(prefix + 'c' + id);
169
	checkbox.checked = !checkbox.checked;
170
	fr_bgcolor(id, prefix);
171
}
172

    
173
// Change background color of selected row based on state of checkbox
174
function fr_bgcolor(id, prefix) {
175
	if (!prefix)
176
		prefix = 'fr';
177

    
178
	var row = $('#' + prefix + id);
179

    
180
	if ($('#' + prefix + 'c' + id).prop('checked') ) {
181
		row.addClass('active');
182
	} else {
183
		row.removeClass('active');
184
	}
185
}
186

    
187
// The following functions are used by Form_Groups assigned a class of "repeatable" and provide the ability
188
// to add/delete rows of sequentially numbered elements, their labels and their help text
189
// See firewall_aliases_edit.php for an example
190

    
191
// NOTE: retainhelp is a global var that when defined prevents any help text from being deleted as lines are inserted.
192
// IOW it causes every row to have help text, not just the last row
193

    
194
function setMasks() {
195
	// Find all ipaddress masks and make dynamic based on address family of input
196
	$('span.pfIpMask + select').each(function (idx, select){
197
		var input = $(select).prevAll('input[type=text]');
198

    
199
		input.on('change', function(e){
200
			var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
201
			if (!isV6)
202
				max = 32;
203

    
204
			if (input.val() == "")
205
				return;
206

    
207
			while (select.options.length > max)
208
				select.remove(0);
209

    
210
			if (select.options.length < max) {
211
				for (var i=select.options.length; i<=max; i++)
212
					select.options.add(new Option(i, i), 0);
213
			}
214
		});
215

    
216
		// Fire immediately
217
		input.change();
218
	});
219
}
220

    
221
// Complicated function to move all help text associated with this input id to the same id
222
// on the row above. That way if you delete the last row, you don't lose the help
223
function moveHelpText(id) {
224
	$('#' + id).parent('div').parent('div').find('input, select, checkbox').each(function() {	 // For each <span></span>
225
		var fromId = this.id;
226
		var toId = decrStringInt(fromId);
227
		var helpSpan;
228

    
229
		if (!$(this).hasClass('pfIpMask') && !$(this).hasClass('btn')) {
230
			if ($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group')) {
231
				helpSpan = $('#' + fromId).parent('div').parent('div').find('span:last').clone();
232
			} else {
233
				helpSpan = $('#' + fromId).parent('div').find('span:last').clone();
234
			}
235
			if ($(helpSpan).hasClass('help-block')) {
236
				if ($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group')) {
237
					$('#' + decrStringInt(fromId)).parent('div').after(helpSpan);
238
				} else {
239
					$('#' + decrStringInt(fromId)).after(helpSpan);
240
				}
241
			}
242
		}
243
	});
244
}
245

    
246
// Increment the number at the end of the string
247
function bumpStringInt( str )	{
248
  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
249

    
250
  if (data)
251
	newStr = data[ 1 ] + ( Number( data[ 2 ] ) + 1 ) + data[ 3 ];
252

    
253
  return newStr || str;
254
}
255

    
256
// Decrement the number at the end of the string
257
function decrStringInt( str )	{
258
  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
259

    
260
  if (data)
261
	newStr = data[ 1 ] + ( Number( data[ 2 ] ) - 1 ) + data[ 3 ];
262

    
263
  return newStr || str;
264
}
265

    
266
// Called after a delete so that there are no gaps in the numbering. Most of the time the config system doesn't care about
267
// gaps, but I do :)
268
function renumber() {
269
	var idx = 0;
270

    
271
	$('.repeatable').each(function() {
272

    
273
		$(this).find('input').each(function() {
274
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
275
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
276
		});
277

    
278
		$(this).find('select').each(function() {
279
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
280
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
281
		});
282

    
283
//		$(this).find('label').attr('for', $(this).find('label').attr('for').replace(/\d+$/, "") + idx);
284

    
285
		idx++;
286
	});
287
}
288

    
289
function delete_row(rowDelBtn) {
290
	var rowLabel;
291

    
292
	// If we are deleting row zero, we need to save/restore the label
293
	if (rowDelBtn == "deleterow0") {
294
		rowLabel = $('#' + rowDelBtn).parent('div').parent('div').find('label').text();
295
	}
296

    
297
	$('#' + rowDelBtn).parent('div').parent('div').remove();
298
	renumber();
299
	checkLastRow();
300

    
301
	if (rowDelBtn == "deleterow0") {
302
		$('#' + rowDelBtn).parent('div').parent('div').find('label').text(rowLabel);
303
	}
304
}
305

    
306
function checkLastRow() {
307
	if ($('.repeatable').length <= 1) {
308
		$('#deleterow0').hide();
309
	} else {
310
		$('[id^=deleterow]').show();
311
	}
312
}
313

    
314
function add_row() {
315
	// Find the last repeatable group
316
	var lastRepeatableGroup = $('.repeatable:last');
317

    
318
	// Clone it
319
	var newGroup = lastRepeatableGroup.clone();
320
	// Increment the suffix number for each input element in the new group
321
	$(newGroup).find('input').each(function() {
322
		$(this).prop("id", bumpStringInt(this.id));
323
		$(this).prop("name", bumpStringInt(this.name));
324
		if (!$(this).is('[id^=delete]'))
325
			$(this).val('');
326
	});
327

    
328
	// Increment the suffix number for the deleterow button element in the new group
329
	$(newGroup).find('[id^=deleterow]').each(function() {
330
		$(this).prop("id", bumpStringInt(this.id));
331
		$(this).prop("name", bumpStringInt(this.name));
332
	});
333

    
334
	// Do the same for selectors
335
	$(newGroup).find('select').each(function() {
336
		$(this).prop("id", bumpStringInt(this.id));
337
		$(this).prop("name", bumpStringInt(this.name));
338
		// If this selector lists mask bits, we need it to be reset to all 128 options
339
		// and no items selected, so that automatic v4/v6 selection still works
340
		if ($(this).is('[id^=address_subnet]')) {
341
			$(this).empty();
342
			for (idx=128; idx>0; idx--) {
343
				$(this).append($('<option>', {
344
					value: idx,
345
					text: idx
346
				}));
347
			}
348
		}
349
	});
350

    
351
	// And for "for" tags
352
//	$(newGroup).find('label').attr('for', bumpStringInt($(newGroup).find('label').attr('for')));
353

    
354
	$(newGroup).find('label').text(""); // Clear the label. We only want it on the very first row
355

    
356
	// Insert the updated/cloned row
357
	$(lastRepeatableGroup).after(newGroup);
358

    
359
	// Delete any help text from the group we have cloned
360
	$(lastRepeatableGroup).find('.help-block').each(function() {
361
		if ((typeof retainhelp) == "undefined")
362
			$(this).remove();
363
	});
364

    
365
	setMasks();
366

    
367
	checkLastRow();
368

    
369
	// Autocomplete
370
	if ( typeof addressarray !== 'undefined') {
371
		$('[id^=address]').each(function() {
372
			if (this.id.substring(0, 8) != "address_") {
373
				$(this).autocomplete({
374
					source: addressarray
375
				});
376
			}
377
		});
378
	}
379

    
380
	// Now that we are no longer cloning the event handlers, we need to remove and re-add after a new row
381
	// has been added to the table
382
	$('[id^=delete]').unbind();
383
	$('[id^=delete]').click(function(event) {
384
		if ($('.repeatable').length > 1) {
385
			if ((typeof retainhelp) == "undefined")
386
				moveHelpText(event.target.id);
387

    
388
			delete_row(event.target.id);
389
		} else {
390
			alert('The last row may not be deleted.');
391
		}
392
	});
393

    
394
}
395

    
396
// These are action buttons, not submit buttons
397
$('[id^=addrow]').prop('type','button');
398
$('[id^=delete]').prop('type','button');
399

    
400
// on click . .
401
$('[id^=addrow]').click(function() {
402
	add_row();
403
});
404

    
405
$('[id^=delete]').click(function(event) {
406
	if ($('.repeatable').length > 1) {
407
		if ((typeof retainhelp) == "undefined")
408
			moveHelpText(event.target.id);
409

    
410
		delete_row(event.target.id);
411
	} else {
412
		alert('The last row may not be deleted.');
413
	}
414
});
415

    
416
// "More information" handlers --------------------------------------------------------------------
417

    
418
// If there is an infoblock, automatically add an info icon that toggles its display
419

    
420
var sfx = 0;
421

    
422
$('.infoblock').each(function() {
423
	// If the block has the class "blockopen" it is initially open
424
	if (! $(this).hasClass("blockopen")) {
425
		$(this).hide();
426
	} else {
427
		$(this).removeClass("blockopen");
428
	}
429

    
430
	// Add the "i" icon before the infoblock, incrementing the icon id for each block (in case there are multiple infoblocks on a page)
431
	$(this).before('<i class="fa fa-info-circle icon-pointer" style="color: #337AB7; font-size:20px; margin-left: 10px; margin-bottom: 10px;" id="showinfo' + sfx.toString() + '" title="More information"></i>');
432
	$(this).removeClass("infoblock");
433
	$(this).addClass("infoblock" + sfx.toString());
434
	sfx++;
435
});
436

    
437
// Show the help on clicking the info icon
438
$('[id^="showinfo"]').click(function() {
439
	var id = $(this).attr("id");
440

    
441
	$('.' + "infoblock" + id.substr(8)).toggle();
442
	document.getSelection().removeAllRanges();		// Ensure the text is un-selected (Chrome browser quirk)
443
});
444
// ------------------------------------------------------------------------------------------------
445

    
446
// Put a dummy row into any empty table to keep IE happy
447
$('tbody').each(function(){
448
	$(this).html($.trim($(this).html()))
449
});
450

    
451
$('tbody:empty').html("<tr><td></td></tr>");
452

    
453
// Hide configuration button for panels without configuration
454
$('.container .panel-heading a.config').each(function (idx, el){
455
	var config = $(el).parents('.panel').children('.panel-footer');
456
	if (config.length == 1)
457
		$(el).removeClass('hidden');
458
});
459

    
460
// Initial state & toggle icons of collapsed panel
461
$('.container .panel-heading a[data-toggle="collapse"]').each(function (idx, el){
462
	var body = $(el).parents('.panel').children('.panel-body')
463
	var isOpen = body.hasClass('in');
464

    
465
	$(el).children('i').toggleClass('fa-plus-circle', !isOpen);
466
	$(el).children('i').toggleClass('fa-minus-circle', isOpen);
467

    
468
	body.on('shown.bs.collapse', function(){
469
		$(el).children('i').toggleClass('fa-minus-circle', true);
470
		$(el).children('i').toggleClass('fa-plus-circle', false);
471
	});
472

    
473
	body.on('hidden.bs.collapse', function(){
474
		$(el).children('i').toggleClass('fa-minus-circle', false);
475
		$(el).children('i').toggleClass('fa-plus-circle', true);
476
	});
477
});
478

    
479
// Separator bar stuff ------------------------------------------------------------------------
480

    
481
// Globals
482
gColor = 'bg-info';
483
newSeparator = false;
484
saving = false;
485
dirty = false;
486

    
487
$("#addsep").prop('type' ,'button');
488

    
489
$("#addsep").click(function() {
490
	if (newSeparator) {
491
		return(false);
492
	}
493

    
494
	gColor = 'bg-info';
495
	// Insert a temporary bar in which the user can enter some optional text
496
	sepcols = $( "#ruletable tr th" ).length - 2;
497

    
498
	$('#ruletable > tbody:last').append('<tr>' +
499
		'<td class="' + gColor + '" colspan="' + sepcols + '"><input id="newsep" placeholder="' + svbtnplaceholder + '" class="col-md-12" type="text" /></td>' +
500
		'<td class="' + gColor + '" colspan="2"><button class="btn btn-primary btn-sm" id="btnnewsep"><i class="fa fa-save icon-embed-btn"></i>' + svtxt + '</button>' +
501
		'<button class="btn btn-info btn-sm" id="btncncsep"><i class="fa fa-undo icon-embed-btn"></i>' + cncltxt + '</button>' +
502
		'&nbsp;&nbsp;&nbsp;&nbsp;' +
503
		'&nbsp;&nbsp;<a id="sepclrblue" value="bg-info"><i class="fa fa-circle text-info icon-pointer"></i></a>' +
504
		'&nbsp;&nbsp;<a id="sepclrred" value="bg-danger"><i class="fa fa-circle text-danger icon-pointer"></i></a>' +
505
		'&nbsp;&nbsp;<a id="sepclrgreen" value="bg-success"><i class="fa fa-circle text-success icon-pointer"></i></a>' +
506
		'&nbsp;&nbsp;<a id="sepclrorange" value="bg-warning"><i class="fa fa-circle text-warning icon-pointer"></i></button>' +
507
		'</td></tr>');
508

    
509
	$('#newsep').focus();
510
	newSeparator = true;
511

    
512
	$("#btnnewsep").prop('type' ,'button');
513

    
514
	// Watch escape and enter keys
515
	$('#newsep').keyup(function(e) {
516
		if (e.which == 27) {
517
			$('#btncncsep').trigger('click');
518
		}
519
	});
520

    
521
	$('#newsep').keypress(function(e) {
522
		if (e.which == 13) {
523
			$('#btnnewsep').trigger('click');
524
		}
525
	});
526

    
527
	handle_colors();
528

    
529
	// Replace the temporary separator bar with the final version containing the
530
	// user's text and a delete icon
531
	$("#btnnewsep").click(function() {
532
		var septext = escapeHtml($('#newsep').val());
533
		sepcols = $( "#ruletable tr th" ).length - 1;
534

    
535
		$(this).parents('tr').replaceWith('<tr class="ui-sortable-handle separator">' +
536
			'<td class="' + gColor + '" colspan="' + sepcols + '">' + '<span class="' + gColor + '">' + septext + '</span></td>' +
537
			'<td class="' + gColor + '"><a href="#"><i class="fa fa-trash sepdel"></i></a>' +
538
			'</td></tr>');
539

    
540
		$('#order-store').removeAttr('disabled');
541
		newSeparator = false;
542
		dirty = true;
543
	});
544

    
545
	// Cancel button
546
	$('#btncncsep').click(function(e) {
547
		e.preventDefault();
548
		$(this).parents('tr').remove();
549
		newSeparator = false;
550
	});
551
});
552

    
553
// Delete a separator row
554
$(function(){
555
	$('table').on('click','tr a .sepdel',function(e){
556
		e.preventDefault();
557
		$(this).parents('tr').remove();
558
		$('#order-store').removeAttr('disabled');
559
		dirty = true;
560
	});
561
});
562

    
563
// Compose an input array containing the row #, color and text for each separator
564
function save_separators() {
565
	var row = 0;
566
	var sepinput;
567
	var sepnum = 0;
568

    
569
	$('#ruletable > tbody > tr').each(function() {
570
		if ($(this).hasClass('separator')) {
571
			seprow = $(this).next('tr').attr("id");
572
			if (seprow == undefined) {
573
				seprow = "fr" + row;
574
			}
575

    
576
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][row]" value="' + seprow + '"></input>';
577
			$('form').append(sepinput);
578
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][text]" value="' + escapeHtml($(this).find('td').text()) + '"></input>';
579
			$('form').append(sepinput);
580
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][color]" value="' + $(this).find('td').prop('class') + '"></input>';
581
			$('form').append(sepinput);
582
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][if]" value="' + iface + '"></input>';
583
			$('form').append(sepinput);
584
			sepnum++;
585
		} else {
586
			if ($(this).parent('tbody').hasClass('user-entries')) {
587
				row++;
588
			}
589
		}
590
	});
591
}
592

    
593
function reindex_rules(section) {
594
	var row = 0;
595

    
596
	section.find('tr').each(function() {
597
		if (this.id) {
598
			$(this).attr("id", "fr" + row);
599
			$(this).attr("onclick", "fr_toggle(" + row + ")")
600
			$(this).find('input:checkbox:first').each(function() {
601
				$(this).attr("id", "frc" + row);
602
				$(this).attr("onclick", "fr_toggle(" + row + ")");
603
			});
604

    
605
			row++;
606
		}
607
	});
608
}
609

    
610
function handle_colors() {
611
	$('[id^=sepclr]').prop("type", "button");
612

    
613
	$('[id^=sepclr]').click(function () {
614
		var color =	 $(this).attr('value');
615
		// Clear all the color classes
616
		$(this).parent('td').prop('class', '');
617
		$(this).parent('td').prev('td').prop('class', '');
618
		// Install our new color class
619
		$(this).parent('td').addClass(color);
620
		$(this).parent('td').prev('td').addClass(color);
621
		// Set the global color
622
		gColor = color;
623
	});
624
}
625

    
626
//JS equivalent to PHP htmlspecialchars()
627
function escapeHtml(text) {
628
	var map = {
629
		'&': '&amp;',
630
		'<': '&lt;',
631
		'>': '&gt;',
632
		'"': '&quot;',
633
		"'": '&#039;'
634
	};
635

    
636
	return text.replace(/[&<>"']/g, function(m) { return map[m]; });
637
}
638
// --------------------------------------------------------------------------------------------
639

    
640
// Select every option in the specified multiselect
641
function AllServers(id, selectAll) {
642
   for (i = 0; i < id.length; i++)	   {
643
	   id.eq(i).prop('selected', selectAll);
644
   }
645
}
646

    
647
// Move all selected options from one multiselect to another
648
function moveOptions(From, To)	{
649
	var len = From.length;
650
	var option;
651

    
652
	if (len > 0) {
653
		for (i=0; i<len; i++) {
654
			if (From.eq(i).is(':selected')) {
655
				option = From.eq(i).val();
656
				value  = From.eq(i).text();
657
				To.append(new Option(value, option));
658
				From.eq(i).remove();
659
			}
660
		}
661
	}
662
}
663

    
664

    
665
// ------------- Service start/stop/restart functions.
666
// If a start/stop/restart button is clicked, parse the button name and make a POST via AJAX
667
$('[id*=restartservice-], [id*=stopservice-], [id*=startservice-]').click(function(event) {
668
	var args = this.id.split('-');
669
	var action, name, mode_zone, id;
670

    
671
	if (args[0] == "openvpn") {
672
		action = args[1];
673
		name = args[0];
674
		mode_zone = args[2];
675
		id = args[3];
676
	} else if (args[0] == "cpativeportal") {
677
		action = args[1];
678
		name = args[0];
679
		mode_zone = args[2];
680
		id = args[3];
681
	} else {
682
		action = args[0];
683
		name = args[1];
684
	}
685

    
686
	$(this).children('i').removeClass().addClass('fa fa-cog fa-spin text-success');
687
	this.blur();
688

    
689
	ajaxRequest = $.ajax(
690
		{
691
			url: "/status_services.php",
692
			type: "post",
693
			data: {
694
				ajax: 		"ajax",
695
				mode: 		action,
696
				service: 	name,
697
				vpnmode: 	mode_zone,
698
				zone: 		mode_zone,
699
				id: 		id
700
			}
701
		}
702
	);
703

    
704
	// Once the AJAX call has returned, refresh the page to show the new service
705
	ajaxRequest.done(function (response, textStatus, jqXHR) {
706
		location.reload(true);
707
	});
708
});
(2-2/3)