Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1 447611c4 Scott Ullrich
<?php
2 a7f908db Scott Ullrich
/*
3 6173d1f5 Colin Fleming
 * diag_edit.php
4 fd9ebcd5 Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * All rights reserved.
10 fd9ebcd5 Stephen Beaver
 *
11 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14 fd9ebcd5 Stephen Beaver
 *
15 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
16 fd9ebcd5 Stephen Beaver
 *
17 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22 fd9ebcd5 Stephen Beaver
 */
23 a7f908db Scott Ullrich
24 9fbb3599 Ermal
##|+PRIV
25
##|*IDENT=page-diagnostics-edit
26 9599211d jim-p
##|*NAME=Diagnostics: Edit File
27 9fbb3599 Ermal
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
28 57188e47 Phil Davis
##|*WARN=standard-warning-root
29 a09f3d8e Phil Davis
##|*MATCH=diag_edit.php*
30 c0f613e2 Ermal
##|*MATCH=browser.php*
31 18e7bc46 Stephen Beaver
##|*MATCH=vendor/filebrowser/browser.php*
32 9fbb3599 Ermal
##|-PRIV
33
34 a6a6ee00 k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
35 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
36 859329c8 Scott Ullrich
37 41b1ff89 Phil Davis
if ($_POST['action']) {
38
	switch ($_POST['action']) {
39 0d6a185a Scott Ullrich
		case 'load':
40 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
41 1bd5e62d k-paulius
				print('|5|');
42 ed98ef92 Phil Davis
				print_info_box(gettext("No file name specified."), 'danger');
43 1bd5e62d k-paulius
				print('|');
44 288a2a0f Phil Davis
			} elseif (is_dir($_POST['file'])) {
45 1bd5e62d k-paulius
				print('|4|');
46 ed98ef92 Phil Davis
				print_info_box(gettext("Loading a directory is not supported."), 'danger');
47 1bd5e62d k-paulius
				print('|');
48 288a2a0f Phil Davis
			} elseif (!is_file($_POST['file'])) {
49 1bd5e62d k-paulius
				print('|3|');
50 ed98ef92 Phil Davis
				print_info_box(gettext("File does not exist or is not a regular file."), 'danger');
51 1bd5e62d k-paulius
				print('|');
52 0d6a185a Scott Ullrich
			} else {
53 55344e2c Ermal
				$data = file_get_contents(urldecode($_POST['file']));
54 288a2a0f Phil Davis
				if ($data === false) {
55 1bd5e62d k-paulius
					print('|1|');
56 ed98ef92 Phil Davis
					print_info_box(gettext("Failed to read file."), 'danger');
57 1bd5e62d k-paulius
					print('|');
58 0d6a185a Scott Ullrich
				} else {
59 b71f0cbb Ermal
					$data = base64_encode($data);
60 45d6ada5 Sjon Hortensius
					print("|0|{$_POST['file']}|{$data}|");
61 0d6a185a Scott Ullrich
				}
62
			}
63
			exit;
64 45d6ada5 Sjon Hortensius
65 0d6a185a Scott Ullrich
		case 'save':
66 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
67 1bd5e62d k-paulius
				print('|');
68 ed98ef92 Phil Davis
				print_info_box(gettext("No file name specified."), 'danger');
69 1bd5e62d k-paulius
				print('|');
70 0d6a185a Scott Ullrich
			} else {
71 55344e2c Ermal
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
72
				$ret = file_put_contents($_POST['file'], $_POST['data']);
73 41b1ff89 Phil Davis
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
74
					if (file_exists("/tmp/config.cache")) {
75 5f05c1e8 Scott Ullrich
						unlink("/tmp/config.cache");
76 41b1ff89 Phil Davis
					}
77 0f806eca Erik Fonnesbeck
					disable_security_checks();
78
				}
79 288a2a0f Phil Davis
				if ($ret === false) {
80 1bd5e62d k-paulius
					print('|');
81 ed98ef92 Phil Davis
					print_info_box(gettext("Failed to write file."), 'danger');
82 1bd5e62d k-paulius
					print('|');
83 288a2a0f Phil Davis
				} elseif ($ret != strlen($_POST['data'])) {
84 1bd5e62d k-paulius
					print('|');
85 ed98ef92 Phil Davis
					print_info_box(gettext("Error while writing file."), 'danger');
86 1bd5e62d k-paulius
					print('|');
87 0d6a185a Scott Ullrich
				} else {
88 1bd5e62d k-paulius
					print('|');
89 ed98ef92 Phil Davis
					print_info_box(gettext("File saved successfully."), 'success');
90 1bd5e62d k-paulius
					print('|');
91 0d6a185a Scott Ullrich
				}
92
			}
93
			exit;
94 5124d619 Scott Ullrich
	}
95 0d6a185a Scott Ullrich
	exit;
96 5124d619 Scott Ullrich
}
97
98 c81ef6e2 Phil Davis
require_once("head.inc");
99 ee092d36 Jared Dillard
100
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'));
101
102 5b237745 Scott Ullrich
?>
103 45d6ada5 Sjon Hortensius
<!-- file status box -->
104
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
105 1bd5e62d k-paulius
	<div id="fileStatus"></div>
106 45d6ada5 Sjon Hortensius
</div>
107
108
<div class="panel panel-default">
109 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
110 45d6ada5 Sjon Hortensius
	<div class="panel-body">
111 2ca4eec2 Jared Dillard
		<div class="content">
112
			<form>
113 e5343844 Stephen Beaver
				<p><input type="text" class="form-control" id="fbTarget" placeholder="<?=gettext('Path to file to be edited')?>"/></p>
114 37676f4e jim-p
				<div class="btn-group">
115 ee092d36 Jared Dillard
					<p>
116
						<button type="button" class="btn btn-default btn-sm" onclick="loadFile();"	value="<?=gettext('Load')?>">
117
							<i class="fa fa-file-text-o"></i>
118
							<?=gettext('Load')?>
119
						</button>
120
						<button type="button" class="btn btn-default btn-sm" id="fbOpen"		value="<?=gettext('Browse')?>">
121
							<i class="fa fa-list"></i>
122
							<?=gettext('Browse')?>
123
						</button>
124
						<button type="button" class="btn btn-default btn-sm" onclick="saveFile();"	value="<?=gettext('Save')?>">
125
							<i class="fa fa-save"></i>
126
							<?=gettext('Save')?>
127
						</button>
128
					</p>
129 37676f4e jim-p
				</div>
130 ee092d36 Jared Dillard
				<p class="pull-right">
131
					<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;"/>
132
				</p>
133 2ca4eec2 Jared Dillard
			</form>
134 45d6ada5 Sjon Hortensius
135 ee092d36 Jared Dillard
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%; padding:10px"></div>
136 45d6ada5 Sjon Hortensius
137 ee092d36 Jared Dillard
			<script type="text/javascript">
138
			//<![CDATA[
139
			window.onload=function() {
140
				document.getElementById("fileContent").wrap='off';
141
			}
142
			//]]>
143
			</script>
144
			<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
145 45d6ada5 Sjon Hortensius
		</div>
146
	</div>
147
</div>
148
149 8fd9052f Colin Fleming
<script type="text/javascript">
150
//<![CDATA[
151 b54c2035 Stephen Beaver
	events.push(function(){
152
153
		function showLine(tarea, lineNum) {
154
155
			lineNum--; // array starts at 0
156
			var lines = tarea.value.split("\n");
157
158
			// calculate start/end
159
			var startPos = 0, endPos = tarea.value.length;
160 9d3e8723 Phil Davis
			for (var x = 0; x < lines.length; x++) {
161
				if (x == lineNum) {
162 b54c2035 Stephen Beaver
					break;
163
				}
164
				startPos += (lines[x].length+1);
165
166
			}
167
168
			var endPos = lines[lineNum].length+startPos;
169
170
			// do selection
171
			// Chrome / Firefox
172
173 9d3e8723 Phil Davis
			if (typeof(tarea.selectionStart) != "undefined") {
174 b54c2035 Stephen Beaver
				tarea.focus();
175
				tarea.selectionStart = startPos;
176
				tarea.selectionEnd = endPos;
177
				return true;
178
			}
179
180
			// IE
181
			if (document.selection && document.selection.createRange) {
182
				tarea.focus();
183
				tarea.select();
184
				var range = document.selection.createRange();
185
				range.collapse(true);
186
				range.moveEnd("character", endPos);
187
				range.moveStart("character", startPos);
188
				range.select();
189
				return true;
190
			}
191
192
			return false;
193
		}
194
195
		$("#btngoto").prop('type','button');
196
197 fd778d8b Stephen Beaver
		//On clicking the GoTo button, validate the entered value
198
		// and highlight the required line
199 b54c2035 Stephen Beaver
		$('#btngoto').click(function() {
200
			var tarea = document.getElementById("fileContent");
201 fd778d8b Stephen Beaver
			var gtl = $('#gotoline').val();
202
			var lines = $("#fileContent").val().split(/\r|\r\n|\n/).length;
203
204
			if (gtl < 1) {
205
				gtl = 1;
206
			}
207
208
			if (gtl > lines) {
209
				gtl = lines;
210
			}
211
212
			showLine(tarea, gtl);
213 b54c2035 Stephen Beaver
		});
214 86e94bec Stephen Beaver
215
		// Goto the specified line on pressing the Enter key within the "Goto line" input element
216
		$('#gotoline').keyup(function(e) {
217
			if(e.keyCode == 13) {
218
				$('#btngoto').click();
219
			}
220
		});
221
222
	}); // e-o-events.push()
223 b54c2035 Stephen Beaver
224 0d6a185a Scott Ullrich
	function loadFile() {
225 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
226
		$("#fileStatusBox").show(500);
227
		$.ajax(
228 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
229 e3c1ea9b Vinicius Coque
				type: "post",
230 3f98044a Francisco Cavalcante
				data: "action=load&file=" + $("#fbTarget").val(),
231 e3c1ea9b Vinicius Coque
				complete: loadComplete
232 0d6a185a Scott Ullrich
			}
233
		);
234
	}
235 5b237745 Scott Ullrich
236 0d6a185a Scott Ullrich
	function loadComplete(req) {
237 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
238 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
239
		values.shift(); values.pop();
240
241 41b1ff89 Phil Davis
		if (values.shift() == "0") {
242 0d6a185a Scott Ullrich
			var file = values.shift();
243 557e0826 Steve Beaver
			var fileContent = window.Base64.decode(values.join("|"));
244 0d6a185a Scott Ullrich
245 3f98044a Francisco Cavalcante
			$("#fileContent").val(fileContent);
246 947141fd Phil Davis
		} else {
247 3f98044a Francisco Cavalcante
			$("#fileStatus").html(values[0]);
248
			$("#fileContent").val("");
249 0d6a185a Scott Ullrich
		}
250 45d6ada5 Sjon Hortensius
251 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
252 0d6a185a Scott Ullrich
	}
253 5b237745 Scott Ullrich
254 0d6a185a Scott Ullrich
	function saveFile(file) {
255 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
256
		$("#fileStatusBox").show(500);
257 41b1ff89 Phil Davis
258 3f98044a Francisco Cavalcante
		var fileContent = Base64.encode($("#fileContent").val());
259 6c07db48 Phil Davis
		fileContent = fileContent.replace(/\+/g, "%2B");
260 45d6ada5 Sjon Hortensius
261 3f98044a Francisco Cavalcante
		$.ajax(
262 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
263 e3c1ea9b Vinicius Coque
				type: "post",
264 3f98044a Francisco Cavalcante
				data: "action=save&file=" + $("#fbTarget").val() +
265 ee650539 Scott Ullrich
							"&data=" + fileContent,
266 e3c1ea9b Vinicius Coque
				complete: function(req) {
267 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
268 3f98044a Francisco Cavalcante
					$("#fileStatus").html(values[1]);
269 0d6a185a Scott Ullrich
				}
270
			}
271
		);
272
	}
273
274 e561ccdf Stephen Beaver
/**
275
 *
276
 *	Base64 encode / decode
277
 *	http://www.webtoolkit.info/
278
 *	http://www.webtoolkit.info/licence
279
 **/
280
281
var Base64 = {
282
283
	// private property
284
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
285
286
	// public method for encoding
287
	encode : function (input) {
288
		var output = "";
289
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
290
		var i = 0;
291
292
		input = Base64._utf8_encode(input);
293
294
		while (i < input.length) {
295
296
			chr1 = input.charCodeAt(i++);
297
			chr2 = input.charCodeAt(i++);
298
			chr3 = input.charCodeAt(i++);
299
300
			enc1 = chr1 >> 2;
301
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
302
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
303
			enc4 = chr3 & 63;
304
305
			if (isNaN(chr2)) {
306
				enc3 = enc4 = 64;
307
			} else if (isNaN(chr3)) {
308
				enc4 = 64;
309
			}
310
311
			output = output +
312
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
313
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
314
315
		}
316
317
		return output;
318
	},
319
320
	// public method for decoding
321
	decode : function (input) {
322
		var output = "";
323
		var chr1, chr2, chr3;
324
		var enc1, enc2, enc3, enc4;
325
		var i = 0;
326
327
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
328
329
		while (i < input.length) {
330
331
			enc1 = this._keyStr.indexOf(input.charAt(i++));
332
			enc2 = this._keyStr.indexOf(input.charAt(i++));
333
			enc3 = this._keyStr.indexOf(input.charAt(i++));
334
			enc4 = this._keyStr.indexOf(input.charAt(i++));
335
336
			chr1 = (enc1 << 2) | (enc2 >> 4);
337
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
338
			chr3 = ((enc3 & 3) << 6) | enc4;
339
340
			output = output + String.fromCharCode(chr1);
341
342
			if (enc3 != 64) {
343
				output = output + String.fromCharCode(chr2);
344
			}
345
			if (enc4 != 64) {
346
				output = output + String.fromCharCode(chr3);
347
			}
348
349
		}
350
351
		output = Base64._utf8_decode(output);
352
353
		return output;
354
355
	},
356
357
	// private method for UTF-8 encoding
358
	_utf8_encode : function (string) {
359
		string = string.replace(/\r\n/g,"\n");
360
		var utftext = "";
361
362
		for (var n = 0; n < string.length; n++) {
363
364
			var c = string.charCodeAt(n);
365
366
			if (c < 128) {
367
				utftext += String.fromCharCode(c);
368 947141fd Phil Davis
			} else if ((c > 127) && (c < 2048)) {
369 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 6) | 192);
370
				utftext += String.fromCharCode((c & 63) | 128);
371 947141fd Phil Davis
			} else {
372 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 12) | 224);
373
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
374
				utftext += String.fromCharCode((c & 63) | 128);
375
			}
376
377
		}
378
379
		return utftext;
380
	},
381
382
	// private method for UTF-8 decoding
383
	_utf8_decode : function (utftext) {
384
		var string = "";
385
		var i = 0;
386
		var c = c1 = c2 = 0;
387
388 947141fd Phil Davis
		while (i < utftext.length) {
389 e561ccdf Stephen Beaver
390
			c = utftext.charCodeAt(i);
391
392
			if (c < 128) {
393
				string += String.fromCharCode(c);
394
				i++;
395 947141fd Phil Davis
			} else if ((c > 191) && (c < 224)) {
396 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
397
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
398
				i += 2;
399 947141fd Phil Davis
			} else {
400 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
401
				c3 = utftext.charCodeAt(i+2);
402
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
403
				i += 3;
404
			}
405
406
		}
407
408
		return string;
409
	}
410
411
};
412
413 7f4268b6 Steve Beaver
	<?php if ($_POST['action'] == "load"): ?>
414 45d6ada5 Sjon Hortensius
		events.push(function() {
415 7f4268b6 Steve Beaver
			$("#fbTarget").val("<?=htmlspecialchars($_POST['path'])?>");
416 45d6ada5 Sjon Hortensius
			loadFile();
417
		});
418 0d6a185a Scott Ullrich
	<?php endif; ?>
419 86e94bec Stephen Beaver
420 8fd9052f Colin Fleming
//]]>
421 0d6a185a Scott Ullrich
</script>
422 ab541dbb Scott Ullrich
423 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
424
425 18e7bc46 Stephen Beaver
outputJavaScriptFileInline("vendor/filebrowser/browser.js");