Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Tag: | Revision:
1 447611c4 Scott Ullrich
<?php
2 a7f908db Scott Ullrich
/*
3 a09f3d8e Phil Davis
	diag_edit.php
4 a7f908db Scott Ullrich
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
6 e561ccdf Stephen Beaver
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 fd9ebcd5 Stephen Beaver
 *
8 e561ccdf Stephen Beaver
 *	Redistribution and use in source and binary forms, with or without modification,
9
 *	are permitted provided that the following conditions are met:
10 fd9ebcd5 Stephen Beaver
 *
11 e561ccdf Stephen Beaver
 *	1. Redistributions of source code must retain the above copyright notice,
12
 *		this list of conditions and the following disclaimer.
13 fd9ebcd5 Stephen Beaver
 *
14 e561ccdf Stephen Beaver
 *	2. Redistributions in binary form must reproduce the above copyright
15
 *		notice, this list of conditions and the following disclaimer in
16
 *		the documentation and/or other materials provided with the
17
 *		distribution.
18 fd9ebcd5 Stephen Beaver
 *
19 e561ccdf Stephen Beaver
 *	3. All advertising materials mentioning features or use of this software
20
 *		must display the following acknowledgment:
21
 *		"This product includes software developed by the pfSense Project
22
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
23 fd9ebcd5 Stephen Beaver
 *
24 e561ccdf Stephen Beaver
 *	4. The names "pfSense" and "pfSense Project" must not be used to
25
 *		 endorse or promote products derived from this software without
26
 *		 prior written permission. For written permission, please contact
27
 *		 coreteam@pfsense.org.
28 fd9ebcd5 Stephen Beaver
 *
29 e561ccdf Stephen Beaver
 *	5. Products derived from this software may not be called "pfSense"
30
 *		nor may "pfSense" appear in their names without prior written
31
 *		permission of the Electric Sheep Fencing, LLC.
32 fd9ebcd5 Stephen Beaver
 *
33 e561ccdf Stephen Beaver
 *	6. Redistributions of any form whatsoever must retain the following
34
 *		acknowledgment:
35 fd9ebcd5 Stephen Beaver
 *
36 e561ccdf Stephen Beaver
 *	"This product includes software developed by the pfSense Project
37
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
38 919d91f9 Phil Davis
 *
39 e561ccdf Stephen Beaver
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
51 fd9ebcd5 Stephen Beaver
 *
52 e561ccdf Stephen Beaver
 *	====================================================================
53 fd9ebcd5 Stephen Beaver
 *
54
 */
55 a7f908db Scott Ullrich
56 9fbb3599 Ermal
##|+PRIV
57
##|*IDENT=page-diagnostics-edit
58 9599211d jim-p
##|*NAME=Diagnostics: Edit File
59 9fbb3599 Ermal
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
60 a09f3d8e Phil Davis
##|*MATCH=diag_edit.php*
61 c0f613e2 Ermal
##|*MATCH=browser.php*
62 18e7bc46 Stephen Beaver
##|*MATCH=vendor/filebrowser/browser.php*
63 9fbb3599 Ermal
##|-PRIV
64
65 a6a6ee00 k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
66 859329c8 Scott Ullrich
require("guiconfig.inc");
67
68 41b1ff89 Phil Davis
if ($_POST['action']) {
69
	switch ($_POST['action']) {
70 0d6a185a Scott Ullrich
		case 'load':
71 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
72 1bd5e62d k-paulius
				print('|5|');
73 ed98ef92 Phil Davis
				print_info_box(gettext("No file name specified."), 'danger');
74 1bd5e62d k-paulius
				print('|');
75 288a2a0f Phil Davis
			} elseif (is_dir($_POST['file'])) {
76 1bd5e62d k-paulius
				print('|4|');
77 ed98ef92 Phil Davis
				print_info_box(gettext("Loading a directory is not supported."), 'danger');
78 1bd5e62d k-paulius
				print('|');
79 288a2a0f Phil Davis
			} elseif (!is_file($_POST['file'])) {
80 1bd5e62d k-paulius
				print('|3|');
81 ed98ef92 Phil Davis
				print_info_box(gettext("File does not exist or is not a regular file."), 'danger');
82 1bd5e62d k-paulius
				print('|');
83 0d6a185a Scott Ullrich
			} else {
84 55344e2c Ermal
				$data = file_get_contents(urldecode($_POST['file']));
85 288a2a0f Phil Davis
				if ($data === false) {
86 1bd5e62d k-paulius
					print('|1|');
87 ed98ef92 Phil Davis
					print_info_box(gettext("Failed to read file."), 'danger');
88 1bd5e62d k-paulius
					print('|');
89 0d6a185a Scott Ullrich
				} else {
90 b71f0cbb Ermal
					$data = base64_encode($data);
91 45d6ada5 Sjon Hortensius
					print("|0|{$_POST['file']}|{$data}|");
92 0d6a185a Scott Ullrich
				}
93
			}
94
			exit;
95 45d6ada5 Sjon Hortensius
96 0d6a185a Scott Ullrich
		case 'save':
97 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
98 1bd5e62d k-paulius
				print('|');
99 ed98ef92 Phil Davis
				print_info_box(gettext("No file name specified."), 'danger');
100 1bd5e62d k-paulius
				print('|');
101 0d6a185a Scott Ullrich
			} else {
102 5a557f44 jim-p
				conf_mount_rw();
103 55344e2c Ermal
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
104
				$ret = file_put_contents($_POST['file'], $_POST['data']);
105 5a557f44 jim-p
				conf_mount_ro();
106 41b1ff89 Phil Davis
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
107
					if (file_exists("/tmp/config.cache")) {
108 5f05c1e8 Scott Ullrich
						unlink("/tmp/config.cache");
109 41b1ff89 Phil Davis
					}
110 0f806eca Erik Fonnesbeck
					disable_security_checks();
111
				}
112 288a2a0f Phil Davis
				if ($ret === false) {
113 1bd5e62d k-paulius
					print('|');
114 ed98ef92 Phil Davis
					print_info_box(gettext("Failed to write file."), 'danger');
115 1bd5e62d k-paulius
					print('|');
116 288a2a0f Phil Davis
				} elseif ($ret != strlen($_POST['data'])) {
117 1bd5e62d k-paulius
					print('|');
118 ed98ef92 Phil Davis
					print_info_box(gettext("Error while writing file."), 'danger');
119 1bd5e62d k-paulius
					print('|');
120 0d6a185a Scott Ullrich
				} else {
121 1bd5e62d k-paulius
					print('|');
122 ed98ef92 Phil Davis
					print_info_box(gettext("File saved successfully."), 'success');
123 1bd5e62d k-paulius
					print('|');
124 0d6a185a Scott Ullrich
				}
125
			}
126
			exit;
127 5124d619 Scott Ullrich
	}
128 0d6a185a Scott Ullrich
	exit;
129 5124d619 Scott Ullrich
}
130
131 0d6a185a Scott Ullrich
require("head.inc");
132 5b237745 Scott Ullrich
?>
133 45d6ada5 Sjon Hortensius
<!-- file status box -->
134
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
135 1bd5e62d k-paulius
	<div id="fileStatus"></div>
136 45d6ada5 Sjon Hortensius
</div>
137
138
<div class="panel panel-default">
139 3d7a8696 k-paulius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
140 45d6ada5 Sjon Hortensius
	<div class="panel-body">
141 2ca4eec2 Jared Dillard
		<div class="content">
142
			<form>
143
				<input type="text" class="form-control" id="fbTarget"/>
144 37676f4e jim-p
				<div class="btn-group">
145
					<button type="button" class="btn btn-default btn-sm" onclick="loadFile();"	value="<?=gettext('Load')?>">
146
						<i class="fa fa-file-text-o"></i>
147
						<?=gettext('Load')?>
148
					</button>
149
					<button type="button" class="btn btn-default btn-sm" id="fbOpen"		value="<?=gettext('Browse')?>">
150
						<i class="fa fa-list"></i>
151
						<?=gettext('Browse')?>
152
					</button>
153
					<button type="button" class="btn btn-default btn-sm" onclick="saveFile();"	value="<?=gettext('Save')?>">
154
						<i class="fa fa-save"></i>
155
						<?=gettext('Save')?>
156
					</button>
157
				</div>
158 b54c2035 Stephen Beaver
				<span class="pull-right">
159 37676f4e jim-p
					<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" />
160 b54c2035 Stephen Beaver
				</span>
161 2ca4eec2 Jared Dillard
			</form>
162 45d6ada5 Sjon Hortensius
163 2ca4eec2 Jared Dillard
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
164 45d6ada5 Sjon Hortensius
165 2ca4eec2 Jared Dillard
			<div style="background:#eeeeee;" id="fileOutput">
166
				<script type="text/javascript">
167
				//<![CDATA[
168 947141fd Phil Davis
				window.onload=function() {
169 2ca4eec2 Jared Dillard
					document.getElementById("fileContent").wrap='off';
170
				}
171
				//]]>
172
				</script>
173 e4c0e9b1 NOYB
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
174 2ca4eec2 Jared Dillard
			</div>
175 45d6ada5 Sjon Hortensius
		</div>
176
	</div>
177
</div>
178
179 8fd9052f Colin Fleming
<script type="text/javascript">
180
//<![CDATA[
181 b54c2035 Stephen Beaver
	events.push(function(){
182
183
		function showLine(tarea, lineNum) {
184
185
			lineNum--; // array starts at 0
186
			var lines = tarea.value.split("\n");
187
188
			// calculate start/end
189
			var startPos = 0, endPos = tarea.value.length;
190
			for(var x = 0; x < lines.length; x++) {
191
				if(x == lineNum) {
192
					break;
193
				}
194
				startPos += (lines[x].length+1);
195
196
			}
197
198
			var endPos = lines[lineNum].length+startPos;
199
200
			// do selection
201
			// Chrome / Firefox
202
203
			if(typeof(tarea.selectionStart) != "undefined") {
204
				tarea.focus();
205
				tarea.selectionStart = startPos;
206
				tarea.selectionEnd = endPos;
207
				return true;
208
			}
209
210
			// IE
211
			if (document.selection && document.selection.createRange) {
212
				tarea.focus();
213
				tarea.select();
214
				var range = document.selection.createRange();
215
				range.collapse(true);
216
				range.moveEnd("character", endPos);
217
				range.moveStart("character", startPos);
218
				range.select();
219
				return true;
220
			}
221
222
			return false;
223
		}
224
225
		$("#btngoto").prop('type','button');
226
227
		$('#btngoto').click(function() {
228
			var tarea = document.getElementById("fileContent");
229
			showLine(tarea, $('#gotoline').val());
230
		});
231
	});
232
233 0d6a185a Scott Ullrich
	function loadFile() {
234 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
235
		$("#fileStatusBox").show(500);
236
		$.ajax(
237 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
238 e3c1ea9b Vinicius Coque
				type: "post",
239 3f98044a Francisco Cavalcante
				data: "action=load&file=" + $("#fbTarget").val(),
240 e3c1ea9b Vinicius Coque
				complete: loadComplete
241 0d6a185a Scott Ullrich
			}
242
		);
243
	}
244 5b237745 Scott Ullrich
245 0d6a185a Scott Ullrich
	function loadComplete(req) {
246 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
247 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
248
		values.shift(); values.pop();
249
250 41b1ff89 Phil Davis
		if (values.shift() == "0") {
251 0d6a185a Scott Ullrich
			var file = values.shift();
252 45d6ada5 Sjon Hortensius
			var fileContent = window.atob(values.join("|"));
253 0d6a185a Scott Ullrich
254 3f98044a Francisco Cavalcante
			$("#fileContent").val(fileContent);
255 947141fd Phil Davis
		} else {
256 3f98044a Francisco Cavalcante
			$("#fileStatus").html(values[0]);
257
			$("#fileContent").val("");
258 0d6a185a Scott Ullrich
		}
259 45d6ada5 Sjon Hortensius
260 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
261 0d6a185a Scott Ullrich
	}
262 5b237745 Scott Ullrich
263 0d6a185a Scott Ullrich
	function saveFile(file) {
264 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
265
		$("#fileStatusBox").show(500);
266 41b1ff89 Phil Davis
267 3f98044a Francisco Cavalcante
		var fileContent = Base64.encode($("#fileContent").val());
268 6c07db48 Phil Davis
		fileContent = fileContent.replace(/\+/g, "%2B");
269 45d6ada5 Sjon Hortensius
270 3f98044a Francisco Cavalcante
		$.ajax(
271 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
272 e3c1ea9b Vinicius Coque
				type: "post",
273 3f98044a Francisco Cavalcante
				data: "action=save&file=" + $("#fbTarget").val() +
274 ee650539 Scott Ullrich
							"&data=" + fileContent,
275 e3c1ea9b Vinicius Coque
				complete: function(req) {
276 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
277 3f98044a Francisco Cavalcante
					$("#fileStatus").html(values[1]);
278 0d6a185a Scott Ullrich
				}
279
			}
280
		);
281
	}
282
283 e561ccdf Stephen Beaver
/**
284
 *
285
 *	Base64 encode / decode
286
 *	http://www.webtoolkit.info/
287
 *	http://www.webtoolkit.info/licence
288
 **/
289
290
var Base64 = {
291
292
	// private property
293
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
294
295
	// public method for encoding
296
	encode : function (input) {
297
		var output = "";
298
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
299
		var i = 0;
300
301
		input = Base64._utf8_encode(input);
302
303
		while (i < input.length) {
304
305
			chr1 = input.charCodeAt(i++);
306
			chr2 = input.charCodeAt(i++);
307
			chr3 = input.charCodeAt(i++);
308
309
			enc1 = chr1 >> 2;
310
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
311
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
312
			enc4 = chr3 & 63;
313
314
			if (isNaN(chr2)) {
315
				enc3 = enc4 = 64;
316
			} else if (isNaN(chr3)) {
317
				enc4 = 64;
318
			}
319
320
			output = output +
321
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
322
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
323
324
		}
325
326
		return output;
327
	},
328
329
	// public method for decoding
330
	decode : function (input) {
331
		var output = "";
332
		var chr1, chr2, chr3;
333
		var enc1, enc2, enc3, enc4;
334
		var i = 0;
335
336
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
337
338
		while (i < input.length) {
339
340
			enc1 = this._keyStr.indexOf(input.charAt(i++));
341
			enc2 = this._keyStr.indexOf(input.charAt(i++));
342
			enc3 = this._keyStr.indexOf(input.charAt(i++));
343
			enc4 = this._keyStr.indexOf(input.charAt(i++));
344
345
			chr1 = (enc1 << 2) | (enc2 >> 4);
346
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
347
			chr3 = ((enc3 & 3) << 6) | enc4;
348
349
			output = output + String.fromCharCode(chr1);
350
351
			if (enc3 != 64) {
352
				output = output + String.fromCharCode(chr2);
353
			}
354
			if (enc4 != 64) {
355
				output = output + String.fromCharCode(chr3);
356
			}
357
358
		}
359
360
		output = Base64._utf8_decode(output);
361
362
		return output;
363
364
	},
365
366
	// private method for UTF-8 encoding
367
	_utf8_encode : function (string) {
368
		string = string.replace(/\r\n/g,"\n");
369
		var utftext = "";
370
371
		for (var n = 0; n < string.length; n++) {
372
373
			var c = string.charCodeAt(n);
374
375
			if (c < 128) {
376
				utftext += String.fromCharCode(c);
377 947141fd Phil Davis
			} else if ((c > 127) && (c < 2048)) {
378 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 6) | 192);
379
				utftext += String.fromCharCode((c & 63) | 128);
380 947141fd Phil Davis
			} else {
381 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 12) | 224);
382
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
383
				utftext += String.fromCharCode((c & 63) | 128);
384
			}
385
386
		}
387
388
		return utftext;
389
	},
390
391
	// private method for UTF-8 decoding
392
	_utf8_decode : function (utftext) {
393
		var string = "";
394
		var i = 0;
395
		var c = c1 = c2 = 0;
396
397 947141fd Phil Davis
		while (i < utftext.length) {
398 e561ccdf Stephen Beaver
399
			c = utftext.charCodeAt(i);
400
401
			if (c < 128) {
402
				string += String.fromCharCode(c);
403
				i++;
404 947141fd Phil Davis
			} else if ((c > 191) && (c < 224)) {
405 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
406
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
407
				i += 2;
408 947141fd Phil Davis
			} else {
409 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
410
				c3 = utftext.charCodeAt(i+2);
411
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
412
				i += 3;
413
			}
414
415
		}
416
417
		return string;
418
	}
419
420
};
421
422 288a2a0f Phil Davis
	<?php if ($_GET['action'] == "load"): ?>
423 45d6ada5 Sjon Hortensius
		events.push(function() {
424 3f98044a Francisco Cavalcante
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
425 45d6ada5 Sjon Hortensius
			loadFile();
426
		});
427 0d6a185a Scott Ullrich
	<?php endif; ?>
428 8fd9052f Colin Fleming
//]]>
429 0d6a185a Scott Ullrich
</script>
430 ab541dbb Scott Ullrich
431 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
432
433 18e7bc46 Stephen Beaver
outputJavaScriptFileInline("vendor/filebrowser/browser.js");