Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_edit.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * 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
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * 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
 */
23

    
24
##|+PRIV
25
##|*IDENT=page-diagnostics-edit
26
##|*NAME=Diagnostics: Edit File
27
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
28
##|*WARN=standard-warning-root
29
##|*MATCH=diag_edit.php*
30
##|*MATCH=browser.php*
31
##|*MATCH=vendor/filebrowser/browser.php*
32
##|-PRIV
33

    
34
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
35
require_once("guiconfig.inc");
36

    
37
if ($_POST['action']) {
38
	switch ($_POST['action']) {
39
		case 'load':
40
			if (strlen($_POST['file']) < 1) {
41
				print('|5|');
42
				print_info_box(gettext("No file name specified."), 'danger');
43
				print('|');
44
			} elseif (is_dir($_POST['file'])) {
45
				print('|4|');
46
				print_info_box(gettext("Loading a directory is not supported."), 'danger');
47
				print('|');
48
			} elseif (!is_file($_POST['file'])) {
49
				print('|3|');
50
				print_info_box(gettext("File does not exist or is not a regular file."), 'danger');
51
				print('|');
52
			} else {
53
				$data = file_get_contents(urldecode($_POST['file']));
54
				if ($data === false) {
55
					print('|1|');
56
					print_info_box(gettext("Failed to read file."), 'danger');
57
					print('|');
58
				} else {
59
					$data = base64_encode($data);
60
					print("|0|{$_POST['file']}|{$data}|");
61
				}
62
			}
63
			exit;
64

    
65
		case 'save':
66
			if (strlen($_POST['file']) < 1) {
67
				print('|');
68
				print_info_box(gettext("No file name specified."), 'danger');
69
				print('|');
70
			} else {
71
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
72
				$ret = file_put_contents($_POST['file'], $_POST['data']);
73
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
74
					if (file_exists("/tmp/config.cache")) {
75
						unlink("/tmp/config.cache");
76
					}
77
					disable_security_checks();
78
				}
79
				if ($ret === false) {
80
					print('|');
81
					print_info_box(gettext("Failed to write file."), 'danger');
82
					print('|');
83
				} elseif ($ret != strlen($_POST['data'])) {
84
					print('|');
85
					print_info_box(gettext("Error while writing file."), 'danger');
86
					print('|');
87
				} else {
88
					print('|');
89
					print_info_box(gettext("File saved successfully."), 'success');
90
					print('|');
91
				}
92
			}
93
			exit;
94
	}
95
	exit;
96
}
97

    
98
require_once("head.inc");
99

    
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
?>
103
<!-- file status box -->
104
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
105
	<div id="fileStatus"></div>
106
</div>
107

    
108
<div class="panel panel-default">
109
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
110
	<div class="panel-body">
111
		<div class="content">
112
			<form>
113
				<p><input type="text" class="form-control" id="fbTarget" placeholder="<?=gettext('Path to file to be edited')?>"/></p>
114
				<div class="btn-group">
115
					<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
				</div>
130
				<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
			</form>
134

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

    
137
			<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
		</div>
146
	</div>
147
</div>
148

    
149
<script type="text/javascript">
150
//<![CDATA[
151
	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
			for (var x = 0; x < lines.length; x++) {
161
				if (x == lineNum) {
162
					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
			if (typeof(tarea.selectionStart) != "undefined") {
174
				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
		//On clicking the GoTo button, validate the entered value
198
		// and highlight the required line
199
		$('#btngoto').click(function() {
200
			var tarea = document.getElementById("fileContent");
201
			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
		});
214

    
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

    
224
	function loadFile() {
225
		$("#fileStatus").html("");
226
		$("#fileStatusBox").show(500);
227
		$.ajax(
228
			"<?=$_SERVER['SCRIPT_NAME']?>", {
229
				type: "post",
230
				data: "action=load&file=" + $("#fbTarget").val(),
231
				complete: loadComplete
232
			}
233
		);
234
	}
235

    
236
	function loadComplete(req) {
237
		$("#fileContent").show(1000);
238
		var values = req.responseText.split("|");
239
		values.shift(); values.pop();
240

    
241
		if (values.shift() == "0") {
242
			var file = values.shift();
243
			var fileContent = window.Base64.decode(values.join("|"));
244

    
245
			$("#fileContent").val(fileContent);
246
		} else {
247
			$("#fileStatus").html(values[0]);
248
			$("#fileContent").val("");
249
		}
250

    
251
		$("#fileContent").show(1000);
252
	}
253

    
254
	function saveFile(file) {
255
		$("#fileStatus").html("");
256
		$("#fileStatusBox").show(500);
257

    
258
		var fileContent = Base64.encode($("#fileContent").val());
259
		fileContent = fileContent.replace(/\+/g, "%2B");
260

    
261
		$.ajax(
262
			"<?=$_SERVER['SCRIPT_NAME']?>", {
263
				type: "post",
264
				data: "action=save&file=" + $("#fbTarget").val() +
265
							"&data=" + fileContent,
266
				complete: function(req) {
267
					var values = req.responseText.split("|");
268
					$("#fileStatus").html(values[1]);
269
				}
270
			}
271
		);
272
	}
273

    
274
/**
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
			} else if ((c > 127) && (c < 2048)) {
369
				utftext += String.fromCharCode((c >> 6) | 192);
370
				utftext += String.fromCharCode((c & 63) | 128);
371
			} else {
372
				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
		while (i < utftext.length) {
389

    
390
			c = utftext.charCodeAt(i);
391

    
392
			if (c < 128) {
393
				string += String.fromCharCode(c);
394
				i++;
395
			} else if ((c > 191) && (c < 224)) {
396
				c2 = utftext.charCodeAt(i+1);
397
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
398
				i += 2;
399
			} else {
400
				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
	<?php if ($_POST['action'] == "load"): ?>
414
		events.push(function() {
415
			$("#fbTarget").val("<?=htmlspecialchars($_POST['path'])?>");
416
			loadFile();
417
		});
418
	<?php endif; ?>
419

    
420
//]]>
421
</script>
422

    
423
<?php include("foot.inc");
424

    
425
outputJavaScriptFileInline("vendor/filebrowser/browser.js");
(17-17/227)