Project

General

Profile

Download (12.3 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-2016 Rubicon Communications, LLC (Netgate)
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
		// 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

    
255
	function loadFile() {
256
		$("#fileStatus").html("");
257
		$("#fileStatusBox").show(500);
258
		$.ajax(
259
			"<?=$_SERVER['SCRIPT_NAME']?>", {
260
				type: "post",
261
				data: "action=load&file=" + $("#fbTarget").val(),
262
				complete: loadComplete
263
			}
264
		);
265
	}
266

    
267
	function loadComplete(req) {
268
		$("#fileContent").show(1000);
269
		var values = req.responseText.split("|");
270
		values.shift(); values.pop();
271

    
272
		if (values.shift() == "0") {
273
			var file = values.shift();
274
			var fileContent = window.atob(values.join("|"));
275

    
276
			$("#fileContent").val(fileContent);
277
		} else {
278
			$("#fileStatus").html(values[0]);
279
			$("#fileContent").val("");
280
		}
281

    
282
		$("#fileContent").show(1000);
283
	}
284

    
285
	function saveFile(file) {
286
		$("#fileStatus").html("");
287
		$("#fileStatusBox").show(500);
288

    
289
		var fileContent = Base64.encode($("#fileContent").val());
290
		fileContent = fileContent.replace(/\+/g, "%2B");
291

    
292
		$.ajax(
293
			"<?=$_SERVER['SCRIPT_NAME']?>", {
294
				type: "post",
295
				data: "action=save&file=" + $("#fbTarget").val() +
296
							"&data=" + fileContent,
297
				complete: function(req) {
298
					var values = req.responseText.split("|");
299
					$("#fileStatus").html(values[1]);
300
				}
301
			}
302
		);
303
	}
304

    
305
/**
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
			} else if ((c > 127) && (c < 2048)) {
400
				utftext += String.fromCharCode((c >> 6) | 192);
401
				utftext += String.fromCharCode((c & 63) | 128);
402
			} else {
403
				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
		while (i < utftext.length) {
420

    
421
			c = utftext.charCodeAt(i);
422

    
423
			if (c < 128) {
424
				string += String.fromCharCode(c);
425
				i++;
426
			} else if ((c > 191) && (c < 224)) {
427
				c2 = utftext.charCodeAt(i+1);
428
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
429
				i += 2;
430
			} else {
431
				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
	<?php if ($_GET['action'] == "load"): ?>
445
		events.push(function() {
446
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
447
			loadFile();
448
		});
449
	<?php endif; ?>
450

    
451
//]]>
452
</script>
453

    
454
<?php include("foot.inc");
455

    
456
outputJavaScriptFileInline("vendor/filebrowser/browser.js");
(13-13/227)