Project

General

Profile

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