Project

General

Profile

Download (11.3 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
##|*MATCH=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
				<input type="button" class="btn btn-default btn-sm"	  onclick="loadFile();" value="<?=gettext('Load')?>" />
145
				<input type="button" class="btn btn-default btn-sm"	  id="fbOpen"		   value="<?=gettext('Browse')?>" />
146
				<input type="button" class="btn btn-default btn-sm"	  onclick="saveFile();" value="<?=gettext('Save')?>" />
147 b54c2035 Stephen Beaver
				<span class="pull-right">
148 e4c0e9b1 NOYB
					<button id="btngoto" class="btn btn-default btn-sm"><?=gettext("GoTo Line #")?></button> <input type="number" id="gotoline" size="6" />
149 b54c2035 Stephen Beaver
				</span>
150 2ca4eec2 Jared Dillard
			</form>
151 45d6ada5 Sjon Hortensius
152 2ca4eec2 Jared Dillard
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
153 45d6ada5 Sjon Hortensius
154 2ca4eec2 Jared Dillard
			<div style="background:#eeeeee;" id="fileOutput">
155
				<script type="text/javascript">
156
				//<![CDATA[
157 947141fd Phil Davis
				window.onload=function() {
158 2ca4eec2 Jared Dillard
					document.getElementById("fileContent").wrap='off';
159
				}
160
				//]]>
161
				</script>
162 e4c0e9b1 NOYB
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
163 2ca4eec2 Jared Dillard
			</div>
164 45d6ada5 Sjon Hortensius
		</div>
165
	</div>
166
</div>
167
168 8fd9052f Colin Fleming
<script type="text/javascript">
169
//<![CDATA[
170 b54c2035 Stephen Beaver
	events.push(function(){
171
172
		function showLine(tarea, lineNum) {
173
174
			lineNum--; // array starts at 0
175
			var lines = tarea.value.split("\n");
176
177
			// calculate start/end
178
			var startPos = 0, endPos = tarea.value.length;
179
			for(var x = 0; x < lines.length; x++) {
180
				if(x == lineNum) {
181
					break;
182
				}
183
				startPos += (lines[x].length+1);
184
185
			}
186
187
			var endPos = lines[lineNum].length+startPos;
188
189
			// do selection
190
			// Chrome / Firefox
191
192
			if(typeof(tarea.selectionStart) != "undefined") {
193
				tarea.focus();
194
				tarea.selectionStart = startPos;
195
				tarea.selectionEnd = endPos;
196
				return true;
197
			}
198
199
			// IE
200
			if (document.selection && document.selection.createRange) {
201
				tarea.focus();
202
				tarea.select();
203
				var range = document.selection.createRange();
204
				range.collapse(true);
205
				range.moveEnd("character", endPos);
206
				range.moveStart("character", startPos);
207
				range.select();
208
				return true;
209
			}
210
211
			return false;
212
		}
213
214
		$("#btngoto").prop('type','button');
215
216
		$('#btngoto').click(function() {
217
			var tarea = document.getElementById("fileContent");
218
			showLine(tarea, $('#gotoline').val());
219
		});
220
	});
221
222 0d6a185a Scott Ullrich
	function loadFile() {
223 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
224
		$("#fileStatusBox").show(500);
225
		$.ajax(
226 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
227 e3c1ea9b Vinicius Coque
				type: "post",
228 3f98044a Francisco Cavalcante
				data: "action=load&file=" + $("#fbTarget").val(),
229 e3c1ea9b Vinicius Coque
				complete: loadComplete
230 0d6a185a Scott Ullrich
			}
231
		);
232
	}
233 5b237745 Scott Ullrich
234 0d6a185a Scott Ullrich
	function loadComplete(req) {
235 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
236 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
237
		values.shift(); values.pop();
238
239 41b1ff89 Phil Davis
		if (values.shift() == "0") {
240 0d6a185a Scott Ullrich
			var file = values.shift();
241 45d6ada5 Sjon Hortensius
			var fileContent = window.atob(values.join("|"));
242 0d6a185a Scott Ullrich
243 3f98044a Francisco Cavalcante
			$("#fileContent").val(fileContent);
244 947141fd Phil Davis
		} else {
245 3f98044a Francisco Cavalcante
			$("#fileStatus").html(values[0]);
246
			$("#fileContent").val("");
247 0d6a185a Scott Ullrich
		}
248 45d6ada5 Sjon Hortensius
249 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
250 0d6a185a Scott Ullrich
	}
251 5b237745 Scott Ullrich
252 0d6a185a Scott Ullrich
	function saveFile(file) {
253 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
254
		$("#fileStatusBox").show(500);
255 41b1ff89 Phil Davis
256 3f98044a Francisco Cavalcante
		var fileContent = Base64.encode($("#fileContent").val());
257 6c07db48 Phil Davis
		fileContent = fileContent.replace(/\+/g, "%2B");
258 45d6ada5 Sjon Hortensius
259 3f98044a Francisco Cavalcante
		$.ajax(
260 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
261 e3c1ea9b Vinicius Coque
				type: "post",
262 3f98044a Francisco Cavalcante
				data: "action=save&file=" + $("#fbTarget").val() +
263 ee650539 Scott Ullrich
							"&data=" + fileContent,
264 e3c1ea9b Vinicius Coque
				complete: function(req) {
265 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
266 3f98044a Francisco Cavalcante
					$("#fileStatus").html(values[1]);
267 0d6a185a Scott Ullrich
				}
268
			}
269
		);
270
	}
271
272 e561ccdf Stephen Beaver
/**
273
 *
274
 *	Base64 encode / decode
275
 *	http://www.webtoolkit.info/
276
 *	http://www.webtoolkit.info/licence
277
 **/
278
279
var Base64 = {
280
281
	// private property
282
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
283
284
	// public method for encoding
285
	encode : function (input) {
286
		var output = "";
287
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
288
		var i = 0;
289
290
		input = Base64._utf8_encode(input);
291
292
		while (i < input.length) {
293
294
			chr1 = input.charCodeAt(i++);
295
			chr2 = input.charCodeAt(i++);
296
			chr3 = input.charCodeAt(i++);
297
298
			enc1 = chr1 >> 2;
299
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
300
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
301
			enc4 = chr3 & 63;
302
303
			if (isNaN(chr2)) {
304
				enc3 = enc4 = 64;
305
			} else if (isNaN(chr3)) {
306
				enc4 = 64;
307
			}
308
309
			output = output +
310
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
311
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
312
313
		}
314
315
		return output;
316
	},
317
318
	// public method for decoding
319
	decode : function (input) {
320
		var output = "";
321
		var chr1, chr2, chr3;
322
		var enc1, enc2, enc3, enc4;
323
		var i = 0;
324
325
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
326
327
		while (i < input.length) {
328
329
			enc1 = this._keyStr.indexOf(input.charAt(i++));
330
			enc2 = this._keyStr.indexOf(input.charAt(i++));
331
			enc3 = this._keyStr.indexOf(input.charAt(i++));
332
			enc4 = this._keyStr.indexOf(input.charAt(i++));
333
334
			chr1 = (enc1 << 2) | (enc2 >> 4);
335
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
336
			chr3 = ((enc3 & 3) << 6) | enc4;
337
338
			output = output + String.fromCharCode(chr1);
339
340
			if (enc3 != 64) {
341
				output = output + String.fromCharCode(chr2);
342
			}
343
			if (enc4 != 64) {
344
				output = output + String.fromCharCode(chr3);
345
			}
346
347
		}
348
349
		output = Base64._utf8_decode(output);
350
351
		return output;
352
353
	},
354
355
	// private method for UTF-8 encoding
356
	_utf8_encode : function (string) {
357
		string = string.replace(/\r\n/g,"\n");
358
		var utftext = "";
359
360
		for (var n = 0; n < string.length; n++) {
361
362
			var c = string.charCodeAt(n);
363
364
			if (c < 128) {
365
				utftext += String.fromCharCode(c);
366 947141fd Phil Davis
			} else if ((c > 127) && (c < 2048)) {
367 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 6) | 192);
368
				utftext += String.fromCharCode((c & 63) | 128);
369 947141fd Phil Davis
			} else {
370 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 12) | 224);
371
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
372
				utftext += String.fromCharCode((c & 63) | 128);
373
			}
374
375
		}
376
377
		return utftext;
378
	},
379
380
	// private method for UTF-8 decoding
381
	_utf8_decode : function (utftext) {
382
		var string = "";
383
		var i = 0;
384
		var c = c1 = c2 = 0;
385
386 947141fd Phil Davis
		while (i < utftext.length) {
387 e561ccdf Stephen Beaver
388
			c = utftext.charCodeAt(i);
389
390
			if (c < 128) {
391
				string += String.fromCharCode(c);
392
				i++;
393 947141fd Phil Davis
			} else if ((c > 191) && (c < 224)) {
394 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
395
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
396
				i += 2;
397 947141fd Phil Davis
			} else {
398 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
399
				c3 = utftext.charCodeAt(i+2);
400
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
401
				i += 3;
402
			}
403
404
		}
405
406
		return string;
407
	}
408
409
};
410
411 288a2a0f Phil Davis
	<?php if ($_GET['action'] == "load"): ?>
412 45d6ada5 Sjon Hortensius
		events.push(function() {
413 3f98044a Francisco Cavalcante
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
414 45d6ada5 Sjon Hortensius
			loadFile();
415
		});
416 0d6a185a Scott Ullrich
	<?php endif; ?>
417 8fd9052f Colin Fleming
//]]>
418 0d6a185a Scott Ullrich
</script>
419 ab541dbb Scott Ullrich
420 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
421
422 c10cb196 Stephen Beaver
outputJavaScriptFileInline("filebrowser/browser.js");