Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | Revision:
1 447611c4 Scott Ullrich
<?php
2 a7f908db Scott Ullrich
/*
3 4ae5b96f Colin Fleming
 * diag_edit.php
4 fd9ebcd5 Stephen Beaver
 *
5 aaec5634 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 aaec5634 Renato Botelho
 * All rights reserved.
8 fd9ebcd5 Stephen Beaver
 *
9 aaec5634 Renato Botelho
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11 fd9ebcd5 Stephen Beaver
 *
12 aaec5634 Renato Botelho
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14 fd9ebcd5 Stephen Beaver
 *
15 aaec5634 Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
20 aaec5634 Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
25 aaec5634 Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
30 aaec5634 Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
34 aaec5634 Renato Botelho
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36 919d91f9 Phil Davis
 *
37 aaec5634 Renato Botelho
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39 fd9ebcd5 Stephen Beaver
 *
40 aaec5634 Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
53 a7f908db Scott Ullrich
54 9fbb3599 Ermal
##|+PRIV
55
##|*IDENT=page-diagnostics-edit
56 9599211d jim-p
##|*NAME=Diagnostics: Edit File
57 9fbb3599 Ermal
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
58 a09f3d8e Phil Davis
##|*MATCH=diag_edit.php*
59 c0f613e2 Ermal
##|*MATCH=browser.php*
60 18e7bc46 Stephen Beaver
##|*MATCH=vendor/filebrowser/browser.php*
61 9fbb3599 Ermal
##|-PRIV
62
63 a6a6ee00 k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
64 aceaf18c Phil Davis
require_once("guiconfig.inc");
65 859329c8 Scott Ullrich
66 41b1ff89 Phil Davis
if ($_POST['action']) {
67
	switch ($_POST['action']) {
68 0d6a185a Scott Ullrich
		case 'load':
69 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
70 1bd5e62d k-paulius
				print('|5|');
71 ed98ef92 Phil Davis
				print_info_box(gettext("No file name specified."), 'danger');
72 1bd5e62d k-paulius
				print('|');
73 288a2a0f Phil Davis
			} elseif (is_dir($_POST['file'])) {
74 1bd5e62d k-paulius
				print('|4|');
75 ed98ef92 Phil Davis
				print_info_box(gettext("Loading a directory is not supported."), 'danger');
76 1bd5e62d k-paulius
				print('|');
77 288a2a0f Phil Davis
			} elseif (!is_file($_POST['file'])) {
78 1bd5e62d k-paulius
				print('|3|');
79 ed98ef92 Phil Davis
				print_info_box(gettext("File does not exist or is not a regular file."), 'danger');
80 1bd5e62d k-paulius
				print('|');
81 0d6a185a Scott Ullrich
			} else {
82 55344e2c Ermal
				$data = file_get_contents(urldecode($_POST['file']));
83 288a2a0f Phil Davis
				if ($data === false) {
84 1bd5e62d k-paulius
					print('|1|');
85 ed98ef92 Phil Davis
					print_info_box(gettext("Failed to read file."), 'danger');
86 1bd5e62d k-paulius
					print('|');
87 0d6a185a Scott Ullrich
				} else {
88 b71f0cbb Ermal
					$data = base64_encode($data);
89 45d6ada5 Sjon Hortensius
					print("|0|{$_POST['file']}|{$data}|");
90 0d6a185a Scott Ullrich
				}
91
			}
92
			exit;
93 45d6ada5 Sjon Hortensius
94 0d6a185a Scott Ullrich
		case 'save':
95 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
96 1bd5e62d k-paulius
				print('|');
97 ed98ef92 Phil Davis
				print_info_box(gettext("No file name specified."), 'danger');
98 1bd5e62d k-paulius
				print('|');
99 0d6a185a Scott Ullrich
			} else {
100 5a557f44 jim-p
				conf_mount_rw();
101 55344e2c Ermal
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
102
				$ret = file_put_contents($_POST['file'], $_POST['data']);
103 5a557f44 jim-p
				conf_mount_ro();
104 41b1ff89 Phil Davis
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
105
					if (file_exists("/tmp/config.cache")) {
106 5f05c1e8 Scott Ullrich
						unlink("/tmp/config.cache");
107 41b1ff89 Phil Davis
					}
108 0f806eca Erik Fonnesbeck
					disable_security_checks();
109
				}
110 288a2a0f Phil Davis
				if ($ret === false) {
111 1bd5e62d k-paulius
					print('|');
112 ed98ef92 Phil Davis
					print_info_box(gettext("Failed to write file."), 'danger');
113 1bd5e62d k-paulius
					print('|');
114 288a2a0f Phil Davis
				} elseif ($ret != strlen($_POST['data'])) {
115 1bd5e62d k-paulius
					print('|');
116 ed98ef92 Phil Davis
					print_info_box(gettext("Error while writing file."), 'danger');
117 1bd5e62d k-paulius
					print('|');
118 0d6a185a Scott Ullrich
				} else {
119 1bd5e62d k-paulius
					print('|');
120 ed98ef92 Phil Davis
					print_info_box(gettext("File saved successfully."), 'success');
121 1bd5e62d k-paulius
					print('|');
122 0d6a185a Scott Ullrich
				}
123
			}
124
			exit;
125 5124d619 Scott Ullrich
	}
126 0d6a185a Scott Ullrich
	exit;
127 5124d619 Scott Ullrich
}
128
129 aceaf18c Phil Davis
require_once("head.inc");
130 2fef3ed9 Jared Dillard
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 5b237745 Scott Ullrich
?>
134 45d6ada5 Sjon Hortensius
<!-- file status box -->
135
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
136 1bd5e62d k-paulius
	<div id="fileStatus"></div>
137 45d6ada5 Sjon Hortensius
</div>
138
139
<div class="panel panel-default">
140 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
141 45d6ada5 Sjon Hortensius
	<div class="panel-body">
142 2ca4eec2 Jared Dillard
		<div class="content">
143
			<form>
144 e0eadf80 Stephen Beaver
				<p><input type="text" class="form-control" id="fbTarget" placeholder="<?=gettext('Path to file to be edited')?>"/></p>
145 37676f4e jim-p
				<div class="btn-group">
146 2fef3ed9 Jared Dillard
					<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 37676f4e jim-p
				</div>
161 2fef3ed9 Jared Dillard
				<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 2ca4eec2 Jared Dillard
			</form>
165 45d6ada5 Sjon Hortensius
166 2fef3ed9 Jared Dillard
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%; padding:10px"></div>
167 45d6ada5 Sjon Hortensius
168 2fef3ed9 Jared Dillard
			<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 45d6ada5 Sjon Hortensius
		</div>
177
	</div>
178
</div>
179
180 8fd9052f Colin Fleming
<script type="text/javascript">
181
//<![CDATA[
182 b54c2035 Stephen Beaver
	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 9488f42b Phil Davis
			for (var x = 0; x < lines.length; x++) {
192
				if (x == lineNum) {
193 b54c2035 Stephen Beaver
					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 9488f42b Phil Davis
			if (typeof(tarea.selectionStart) != "undefined") {
205 b54c2035 Stephen Beaver
				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 d4879244 Stephen Beaver
		//On clicking the GoTo button, validate the entered value
229
		// and highlight the required line
230 b54c2035 Stephen Beaver
		$('#btngoto').click(function() {
231
			var tarea = document.getElementById("fileContent");
232 d4879244 Stephen Beaver
			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 b54c2035 Stephen Beaver
		});
245 4b1b6bed Stephen Beaver
246
		// Goto the specified line on pressing the Enter key within the "Goto line" input element
247
		$('#gotoline').keyup(function(e) {
248
			if(e.keyCode == 13) {
249
				$('#btngoto').click();
250
			}
251
		});
252
253
	}); // e-o-events.push()
254 b54c2035 Stephen Beaver
255 0d6a185a Scott Ullrich
	function loadFile() {
256 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
257
		$("#fileStatusBox").show(500);
258
		$.ajax(
259 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
260 e3c1ea9b Vinicius Coque
				type: "post",
261 3f98044a Francisco Cavalcante
				data: "action=load&file=" + $("#fbTarget").val(),
262 e3c1ea9b Vinicius Coque
				complete: loadComplete
263 0d6a185a Scott Ullrich
			}
264
		);
265
	}
266 5b237745 Scott Ullrich
267 0d6a185a Scott Ullrich
	function loadComplete(req) {
268 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
269 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
270
		values.shift(); values.pop();
271
272 41b1ff89 Phil Davis
		if (values.shift() == "0") {
273 0d6a185a Scott Ullrich
			var file = values.shift();
274 45d6ada5 Sjon Hortensius
			var fileContent = window.atob(values.join("|"));
275 0d6a185a Scott Ullrich
276 3f98044a Francisco Cavalcante
			$("#fileContent").val(fileContent);
277 947141fd Phil Davis
		} else {
278 3f98044a Francisco Cavalcante
			$("#fileStatus").html(values[0]);
279
			$("#fileContent").val("");
280 0d6a185a Scott Ullrich
		}
281 45d6ada5 Sjon Hortensius
282 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
283 0d6a185a Scott Ullrich
	}
284 5b237745 Scott Ullrich
285 0d6a185a Scott Ullrich
	function saveFile(file) {
286 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
287
		$("#fileStatusBox").show(500);
288 41b1ff89 Phil Davis
289 3f98044a Francisco Cavalcante
		var fileContent = Base64.encode($("#fileContent").val());
290 6c07db48 Phil Davis
		fileContent = fileContent.replace(/\+/g, "%2B");
291 45d6ada5 Sjon Hortensius
292 3f98044a Francisco Cavalcante
		$.ajax(
293 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
294 e3c1ea9b Vinicius Coque
				type: "post",
295 3f98044a Francisco Cavalcante
				data: "action=save&file=" + $("#fbTarget").val() +
296 ee650539 Scott Ullrich
							"&data=" + fileContent,
297 e3c1ea9b Vinicius Coque
				complete: function(req) {
298 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
299 3f98044a Francisco Cavalcante
					$("#fileStatus").html(values[1]);
300 0d6a185a Scott Ullrich
				}
301
			}
302
		);
303
	}
304
305 e561ccdf Stephen Beaver
/**
306
 *
307
 *	Base64 encode / decode
308
 *	http://www.webtoolkit.info/
309
 *	http://www.webtoolkit.info/licence
310
 **/
311
312
var Base64 = {
313
314
	// private property
315
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
316
317
	// public method for encoding
318
	encode : function (input) {
319
		var output = "";
320
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
321
		var i = 0;
322
323
		input = Base64._utf8_encode(input);
324
325
		while (i < input.length) {
326
327
			chr1 = input.charCodeAt(i++);
328
			chr2 = input.charCodeAt(i++);
329
			chr3 = input.charCodeAt(i++);
330
331
			enc1 = chr1 >> 2;
332
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
333
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
334
			enc4 = chr3 & 63;
335
336
			if (isNaN(chr2)) {
337
				enc3 = enc4 = 64;
338
			} else if (isNaN(chr3)) {
339
				enc4 = 64;
340
			}
341
342
			output = output +
343
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
344
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
345
346
		}
347
348
		return output;
349
	},
350
351
	// public method for decoding
352
	decode : function (input) {
353
		var output = "";
354
		var chr1, chr2, chr3;
355
		var enc1, enc2, enc3, enc4;
356
		var i = 0;
357
358
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
359
360
		while (i < input.length) {
361
362
			enc1 = this._keyStr.indexOf(input.charAt(i++));
363
			enc2 = this._keyStr.indexOf(input.charAt(i++));
364
			enc3 = this._keyStr.indexOf(input.charAt(i++));
365
			enc4 = this._keyStr.indexOf(input.charAt(i++));
366
367
			chr1 = (enc1 << 2) | (enc2 >> 4);
368
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
369
			chr3 = ((enc3 & 3) << 6) | enc4;
370
371
			output = output + String.fromCharCode(chr1);
372
373
			if (enc3 != 64) {
374
				output = output + String.fromCharCode(chr2);
375
			}
376
			if (enc4 != 64) {
377
				output = output + String.fromCharCode(chr3);
378
			}
379
380
		}
381
382
		output = Base64._utf8_decode(output);
383
384
		return output;
385
386
	},
387
388
	// private method for UTF-8 encoding
389
	_utf8_encode : function (string) {
390
		string = string.replace(/\r\n/g,"\n");
391
		var utftext = "";
392
393
		for (var n = 0; n < string.length; n++) {
394
395
			var c = string.charCodeAt(n);
396
397
			if (c < 128) {
398
				utftext += String.fromCharCode(c);
399 947141fd Phil Davis
			} else if ((c > 127) && (c < 2048)) {
400 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 6) | 192);
401
				utftext += String.fromCharCode((c & 63) | 128);
402 947141fd Phil Davis
			} else {
403 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 12) | 224);
404
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
405
				utftext += String.fromCharCode((c & 63) | 128);
406
			}
407
408
		}
409
410
		return utftext;
411
	},
412
413
	// private method for UTF-8 decoding
414
	_utf8_decode : function (utftext) {
415
		var string = "";
416
		var i = 0;
417
		var c = c1 = c2 = 0;
418
419 947141fd Phil Davis
		while (i < utftext.length) {
420 e561ccdf Stephen Beaver
421
			c = utftext.charCodeAt(i);
422
423
			if (c < 128) {
424
				string += String.fromCharCode(c);
425
				i++;
426 947141fd Phil Davis
			} else if ((c > 191) && (c < 224)) {
427 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
428
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
429
				i += 2;
430 947141fd Phil Davis
			} else {
431 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
432
				c3 = utftext.charCodeAt(i+2);
433
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
434
				i += 3;
435
			}
436
437
		}
438
439
		return string;
440
	}
441
442
};
443
444 288a2a0f Phil Davis
	<?php if ($_GET['action'] == "load"): ?>
445 45d6ada5 Sjon Hortensius
		events.push(function() {
446 3f98044a Francisco Cavalcante
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
447 45d6ada5 Sjon Hortensius
			loadFile();
448
		});
449 0d6a185a Scott Ullrich
	<?php endif; ?>
450 4b1b6bed Stephen Beaver
451 8fd9052f Colin Fleming
//]]>
452 0d6a185a Scott Ullrich
</script>
453 ab541dbb Scott Ullrich
454 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
455
456 18e7bc46 Stephen Beaver
outputJavaScriptFileInline("vendor/filebrowser/browser.js");