Project

General

Profile

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

    
54
##|+PRIV
55
##|*IDENT=page-diagnostics-edit
56
##|*NAME=Diagnostics: Edit File
57
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
58
##|*MATCH=diag_edit.php*
59
##|*MATCH=browser.php*
60
##|*MATCH=vendor/filebrowser/browser.php*
61
##|-PRIV
62

    
63
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
64
require_once("guiconfig.inc");
65

    
66
if ($_POST['action']) {
67
	switch ($_POST['action']) {
68
		case 'load':
69
			if (strlen($_POST['file']) < 1) {
70
				print('|5|');
71
				print_info_box(gettext("No file name specified."), 'danger');
72
				print('|');
73
			} elseif (is_dir($_POST['file'])) {
74
				print('|4|');
75
				print_info_box(gettext("Loading a directory is not supported."), 'danger');
76
				print('|');
77
			} elseif (!is_file($_POST['file'])) {
78
				print('|3|');
79
				print_info_box(gettext("File does not exist or is not a regular file."), 'danger');
80
				print('|');
81
			} else {
82
				$data = file_get_contents(urldecode($_POST['file']));
83
				if ($data === false) {
84
					print('|1|');
85
					print_info_box(gettext("Failed to read file."), 'danger');
86
					print('|');
87
				} else {
88
					$data = base64_encode($data);
89
					print("|0|{$_POST['file']}|{$data}|");
90
				}
91
			}
92
			exit;
93

    
94
		case 'save':
95
			if (strlen($_POST['file']) < 1) {
96
				print('|');
97
				print_info_box(gettext("No file name specified."), 'danger');
98
				print('|');
99
			} else {
100
				conf_mount_rw();
101
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
102
				$ret = file_put_contents($_POST['file'], $_POST['data']);
103
				conf_mount_ro();
104
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
105
					if (file_exists("/tmp/config.cache")) {
106
						unlink("/tmp/config.cache");
107
					}
108
					disable_security_checks();
109
				}
110
				if ($ret === false) {
111
					print('|');
112
					print_info_box(gettext("Failed to write file."), 'danger');
113
					print('|');
114
				} elseif ($ret != strlen($_POST['data'])) {
115
					print('|');
116
					print_info_box(gettext("Error while writing file."), 'danger');
117
					print('|');
118
				} else {
119
					print('|');
120
					print_info_box(gettext("File saved successfully."), 'success');
121
					print('|');
122
				}
123
			}
124
			exit;
125
	}
126
	exit;
127
}
128

    
129
require_once("head.inc");
130

    
131
print_callout(gettext("The capabilities offered here can be dangerous. No support is available. Use them at your own risk!"), 'danger', gettext('Advanced Users Only'));
132

    
133
?>
134
<!-- file status box -->
135
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
136
	<div id="fileStatus"></div>
137
</div>
138

    
139
<div class="panel panel-default">
140
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
141
	<div class="panel-body">
142
		<div class="content">
143
			<form>
144
				<p><input type="text" class="form-control" id="fbTarget" placeholder="<?=gettext('Path to file to be edited')?>"/></p>
145
				<div class="btn-group">
146
					<p>
147
						<button type="button" class="btn btn-default btn-sm" onclick="loadFile();"	value="<?=gettext('Load')?>">
148
							<i class="fa fa-file-text-o"></i>
149
							<?=gettext('Load')?>
150
						</button>
151
						<button type="button" class="btn btn-default btn-sm" id="fbOpen"		value="<?=gettext('Browse')?>">
152
							<i class="fa fa-list"></i>
153
							<?=gettext('Browse')?>
154
						</button>
155
						<button type="button" class="btn btn-default btn-sm" onclick="saveFile();"	value="<?=gettext('Save')?>">
156
							<i class="fa fa-save"></i>
157
							<?=gettext('Save')?>
158
						</button>
159
					</p>
160
				</div>
161
				<p class="pull-right">
162
					<button id="btngoto" class="btn btn-default btn-sm"><i class="fa fa-forward"></i><?=gettext("GoTo Line #")?></button> <input type="number" id="gotoline" size="6" style="padding: 3px 0px;"/>
163
				</p>
164
			</form>
165

    
166
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%; padding:10px"></div>
167

    
168
			<script type="text/javascript">
169
			//<![CDATA[
170
			window.onload=function() {
171
				document.getElementById("fileContent").wrap='off';
172
			}
173
			//]]>
174
			</script>
175
			<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
176
		</div>
177
	</div>
178
</div>
179

    
180
<script type="text/javascript">
181
//<![CDATA[
182
	events.push(function(){
183

    
184
		function showLine(tarea, lineNum) {
185

    
186
			lineNum--; // array starts at 0
187
			var lines = tarea.value.split("\n");
188

    
189
			// calculate start/end
190
			var startPos = 0, endPos = tarea.value.length;
191
			for(var x = 0; x < lines.length; x++) {
192
				if(x == lineNum) {
193
					break;
194
				}
195
				startPos += (lines[x].length+1);
196

    
197
			}
198

    
199
			var endPos = lines[lineNum].length+startPos;
200

    
201
			// do selection
202
			// Chrome / Firefox
203

    
204
			if(typeof(tarea.selectionStart) != "undefined") {
205
				tarea.focus();
206
				tarea.selectionStart = startPos;
207
				tarea.selectionEnd = endPos;
208
				return true;
209
			}
210

    
211
			// IE
212
			if (document.selection && document.selection.createRange) {
213
				tarea.focus();
214
				tarea.select();
215
				var range = document.selection.createRange();
216
				range.collapse(true);
217
				range.moveEnd("character", endPos);
218
				range.moveStart("character", startPos);
219
				range.select();
220
				return true;
221
			}
222

    
223
			return false;
224
		}
225

    
226
		$("#btngoto").prop('type','button');
227

    
228
		//On clicking the GoTo button, validate the entered value
229
		// and highlight the required line
230
		$('#btngoto').click(function() {
231
			var tarea = document.getElementById("fileContent");
232
			var gtl = $('#gotoline').val();
233
			var lines = $("#fileContent").val().split(/\r|\r\n|\n/).length;
234

    
235
			if (gtl < 1) {
236
				gtl = 1;
237
			}
238

    
239
			if (gtl > lines) {
240
				gtl = lines;
241
			}
242

    
243
			showLine(tarea, gtl);
244
		});
245
	});
246

    
247
	function loadFile() {
248
		$("#fileStatus").html("");
249
		$("#fileStatusBox").show(500);
250
		$.ajax(
251
			"<?=$_SERVER['SCRIPT_NAME']?>", {
252
				type: "post",
253
				data: "action=load&file=" + $("#fbTarget").val(),
254
				complete: loadComplete
255
			}
256
		);
257
	}
258

    
259
	function loadComplete(req) {
260
		$("#fileContent").show(1000);
261
		var values = req.responseText.split("|");
262
		values.shift(); values.pop();
263

    
264
		if (values.shift() == "0") {
265
			var file = values.shift();
266
			var fileContent = window.atob(values.join("|"));
267

    
268
			$("#fileContent").val(fileContent);
269
		} else {
270
			$("#fileStatus").html(values[0]);
271
			$("#fileContent").val("");
272
		}
273

    
274
		$("#fileContent").show(1000);
275
	}
276

    
277
	function saveFile(file) {
278
		$("#fileStatus").html("");
279
		$("#fileStatusBox").show(500);
280

    
281
		var fileContent = Base64.encode($("#fileContent").val());
282
		fileContent = fileContent.replace(/\+/g, "%2B");
283

    
284
		$.ajax(
285
			"<?=$_SERVER['SCRIPT_NAME']?>", {
286
				type: "post",
287
				data: "action=save&file=" + $("#fbTarget").val() +
288
							"&data=" + fileContent,
289
				complete: function(req) {
290
					var values = req.responseText.split("|");
291
					$("#fileStatus").html(values[1]);
292
				}
293
			}
294
		);
295
	}
296

    
297
/**
298
 *
299
 *	Base64 encode / decode
300
 *	http://www.webtoolkit.info/
301
 *	http://www.webtoolkit.info/licence
302
 **/
303

    
304
var Base64 = {
305

    
306
	// private property
307
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
308

    
309
	// public method for encoding
310
	encode : function (input) {
311
		var output = "";
312
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
313
		var i = 0;
314

    
315
		input = Base64._utf8_encode(input);
316

    
317
		while (i < input.length) {
318

    
319
			chr1 = input.charCodeAt(i++);
320
			chr2 = input.charCodeAt(i++);
321
			chr3 = input.charCodeAt(i++);
322

    
323
			enc1 = chr1 >> 2;
324
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
325
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
326
			enc4 = chr3 & 63;
327

    
328
			if (isNaN(chr2)) {
329
				enc3 = enc4 = 64;
330
			} else if (isNaN(chr3)) {
331
				enc4 = 64;
332
			}
333

    
334
			output = output +
335
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
336
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
337

    
338
		}
339

    
340
		return output;
341
	},
342

    
343
	// public method for decoding
344
	decode : function (input) {
345
		var output = "";
346
		var chr1, chr2, chr3;
347
		var enc1, enc2, enc3, enc4;
348
		var i = 0;
349

    
350
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
351

    
352
		while (i < input.length) {
353

    
354
			enc1 = this._keyStr.indexOf(input.charAt(i++));
355
			enc2 = this._keyStr.indexOf(input.charAt(i++));
356
			enc3 = this._keyStr.indexOf(input.charAt(i++));
357
			enc4 = this._keyStr.indexOf(input.charAt(i++));
358

    
359
			chr1 = (enc1 << 2) | (enc2 >> 4);
360
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
361
			chr3 = ((enc3 & 3) << 6) | enc4;
362

    
363
			output = output + String.fromCharCode(chr1);
364

    
365
			if (enc3 != 64) {
366
				output = output + String.fromCharCode(chr2);
367
			}
368
			if (enc4 != 64) {
369
				output = output + String.fromCharCode(chr3);
370
			}
371

    
372
		}
373

    
374
		output = Base64._utf8_decode(output);
375

    
376
		return output;
377

    
378
	},
379

    
380
	// private method for UTF-8 encoding
381
	_utf8_encode : function (string) {
382
		string = string.replace(/\r\n/g,"\n");
383
		var utftext = "";
384

    
385
		for (var n = 0; n < string.length; n++) {
386

    
387
			var c = string.charCodeAt(n);
388

    
389
			if (c < 128) {
390
				utftext += String.fromCharCode(c);
391
			} else if ((c > 127) && (c < 2048)) {
392
				utftext += String.fromCharCode((c >> 6) | 192);
393
				utftext += String.fromCharCode((c & 63) | 128);
394
			} else {
395
				utftext += String.fromCharCode((c >> 12) | 224);
396
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
397
				utftext += String.fromCharCode((c & 63) | 128);
398
			}
399

    
400
		}
401

    
402
		return utftext;
403
	},
404

    
405
	// private method for UTF-8 decoding
406
	_utf8_decode : function (utftext) {
407
		var string = "";
408
		var i = 0;
409
		var c = c1 = c2 = 0;
410

    
411
		while (i < utftext.length) {
412

    
413
			c = utftext.charCodeAt(i);
414

    
415
			if (c < 128) {
416
				string += String.fromCharCode(c);
417
				i++;
418
			} else if ((c > 191) && (c < 224)) {
419
				c2 = utftext.charCodeAt(i+1);
420
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
421
				i += 2;
422
			} else {
423
				c2 = utftext.charCodeAt(i+1);
424
				c3 = utftext.charCodeAt(i+2);
425
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
426
				i += 3;
427
			}
428

    
429
		}
430

    
431
		return string;
432
	}
433

    
434
};
435

    
436
	<?php if ($_GET['action'] == "load"): ?>
437
		events.push(function() {
438
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
439
			loadFile();
440
		});
441
	<?php endif; ?>
442
//]]>
443
</script>
444

    
445
<?php include("foot.inc");
446

    
447
outputJavaScriptFileInline("vendor/filebrowser/browser.js");
(13-13/225)