Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Tag: | Revision:
1 447611c4 Scott Ullrich
<?php
2 a7f908db Scott Ullrich
/*
3 0d6a185a Scott Ullrich
	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
##|*MATCH=edit.php*
61 c0f613e2 Ermal
##|*MATCH=browser.php*
62
##|*MATCH=filebrowser/browser.php*
63 9fbb3599 Ermal
##|-PRIV
64
65 fdb38c10 Scott Ullrich
$pgtitle = array(gettext("Diagnostics"), gettext("Edit file"));
66 859329c8 Scott Ullrich
require("guiconfig.inc");
67
68 41b1ff89 Phil Davis
if ($_POST['action']) {
69 028f26a2 Phil Davis
	$button_html = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
70
	$alert_danger_html = '<div class="alert alert-danger" role="alert">' . $button_html;
71
	$alert_success_html = '<div class="alert alert-success" role="alert">' . $button_html;
72 41b1ff89 Phil Davis
	switch ($_POST['action']) {
73 0d6a185a Scott Ullrich
		case 'load':
74 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
75 028f26a2 Phil Davis
				print('|5|' . $alert_danger_html . gettext("No file name specified") . '</div>' . '|');
76 288a2a0f Phil Davis
			} elseif (is_dir($_POST['file'])) {
77 028f26a2 Phil Davis
				print('|4|' . $alert_danger_html . gettext("Loading a directory is not supported") . '</div>' . '|');
78 288a2a0f Phil Davis
			} elseif (!is_file($_POST['file'])) {
79 028f26a2 Phil Davis
				print('|3|' . $alert_danger_html . gettext("File does not exist or is not a regular file") . '</div>' . '|');
80 0d6a185a Scott Ullrich
			} else {
81 55344e2c Ermal
				$data = file_get_contents(urldecode($_POST['file']));
82 288a2a0f Phil Davis
				if ($data === false) {
83 028f26a2 Phil Davis
					print('|1|' . $alert_danger_html . gettext("Failed to read file") . '</div>' . '|');
84 0d6a185a Scott Ullrich
				} else {
85 b71f0cbb Ermal
					$data = base64_encode($data);
86 45d6ada5 Sjon Hortensius
					print("|0|{$_POST['file']}|{$data}|");
87 0d6a185a Scott Ullrich
				}
88
			}
89
			exit;
90 45d6ada5 Sjon Hortensius
91 0d6a185a Scott Ullrich
		case 'save':
92 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
93 028f26a2 Phil Davis
				print('|' . $alert_danger_html . gettext("No file name specified") . '</div>' . '|');
94 0d6a185a Scott Ullrich
			} else {
95 5a557f44 jim-p
				conf_mount_rw();
96 55344e2c Ermal
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
97
				$ret = file_put_contents($_POST['file'], $_POST['data']);
98 5a557f44 jim-p
				conf_mount_ro();
99 41b1ff89 Phil Davis
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
100
					if (file_exists("/tmp/config.cache")) {
101 5f05c1e8 Scott Ullrich
						unlink("/tmp/config.cache");
102 41b1ff89 Phil Davis
					}
103 0f806eca Erik Fonnesbeck
					disable_security_checks();
104
				}
105 288a2a0f Phil Davis
				if ($ret === false) {
106 028f26a2 Phil Davis
					print('|' . $alert_danger_html . gettext("Failed to write file") . '</div>' . '|');
107 288a2a0f Phil Davis
				} elseif ($ret != strlen($_POST['data'])) {
108 028f26a2 Phil Davis
					print('|' . $alert_danger_html . gettext("Error while writing file") . '</div>' . '|');
109 0d6a185a Scott Ullrich
				} else {
110 028f26a2 Phil Davis
					print('|' . $alert_success_html . gettext("File saved successfully") . '</div>' . '|');
111 0d6a185a Scott Ullrich
				}
112
			}
113
			exit;
114 5124d619 Scott Ullrich
	}
115 0d6a185a Scott Ullrich
	exit;
116 5124d619 Scott Ullrich
}
117
118 0d6a185a Scott Ullrich
require("head.inc");
119 5b237745 Scott Ullrich
?>
120 45d6ada5 Sjon Hortensius
<!-- file status box -->
121
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
122 b7edfd3c Phil Davis
	<strong id="fileStatus"></strong>
123 45d6ada5 Sjon Hortensius
</div>
124
125
<div class="panel panel-default">
126 f17594c7 Sjon Hortensius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a file from the filesystem")?></h2></div>
127 45d6ada5 Sjon Hortensius
	<div class="panel-body">
128 2ca4eec2 Jared Dillard
		<div class="content">
129
			<form>
130
				<input type="text" class="form-control" id="fbTarget"/>
131
				<input type="button" class="btn btn-default btn-sm"	  onclick="loadFile();" value="<?=gettext('Load')?>" />
132
				<input type="button" class="btn btn-default btn-sm"	  id="fbOpen"		   value="<?=gettext('Browse')?>" />
133
				<input type="button" class="btn btn-default btn-sm"	  onclick="saveFile();" value="<?=gettext('Save')?>" />
134 b54c2035 Stephen Beaver
				<span class="pull-right">
135
					<button id="btngoto" class="btn btn-default btn-sm"><?=gettext("GoTo Line #")?></button> <input type="number" id="gotoline" width="6"></input>
136
				</span>
137 2ca4eec2 Jared Dillard
			</form>
138 45d6ada5 Sjon Hortensius
139 2ca4eec2 Jared Dillard
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
140 45d6ada5 Sjon Hortensius
141 2ca4eec2 Jared Dillard
			<div style="background:#eeeeee;" id="fileOutput">
142
				<script type="text/javascript">
143
				//<![CDATA[
144 947141fd Phil Davis
				window.onload=function() {
145 2ca4eec2 Jared Dillard
					document.getElementById("fileContent").wrap='off';
146
				}
147
				//]]>
148
				</script>
149
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols=""></textarea>
150
			</div>
151 45d6ada5 Sjon Hortensius
		</div>
152
	</div>
153
</div>
154
155 8fd9052f Colin Fleming
<script type="text/javascript">
156
//<![CDATA[
157 b54c2035 Stephen Beaver
	events.push(function(){
158
159
		function showLine(tarea, lineNum) {
160
161
			lineNum--; // array starts at 0
162
			var lines = tarea.value.split("\n");
163
164
			// calculate start/end
165
			var startPos = 0, endPos = tarea.value.length;
166
			for(var x = 0; x < lines.length; x++) {
167
				if(x == lineNum) {
168
					break;
169
				}
170
				startPos += (lines[x].length+1);
171
172
			}
173
174
			var endPos = lines[lineNum].length+startPos;
175
176
			// do selection
177
			// Chrome / Firefox
178
179
			if(typeof(tarea.selectionStart) != "undefined") {
180
				tarea.focus();
181
				tarea.selectionStart = startPos;
182
				tarea.selectionEnd = endPos;
183
				return true;
184
			}
185
186
			// IE
187
			if (document.selection && document.selection.createRange) {
188
				tarea.focus();
189
				tarea.select();
190
				var range = document.selection.createRange();
191
				range.collapse(true);
192
				range.moveEnd("character", endPos);
193
				range.moveStart("character", startPos);
194
				range.select();
195
				return true;
196
			}
197
198
			return false;
199
		}
200
201
		$("#btngoto").prop('type','button');
202
203
		$('#btngoto').click(function() {
204
			var tarea = document.getElementById("fileContent");
205
			showLine(tarea, $('#gotoline').val());
206
		});
207
	});
208
209 0d6a185a Scott Ullrich
	function loadFile() {
210 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
211
		$("#fileStatusBox").show(500);
212
		$.ajax(
213 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
214 e3c1ea9b Vinicius Coque
				type: "post",
215 3f98044a Francisco Cavalcante
				data: "action=load&file=" + $("#fbTarget").val(),
216 e3c1ea9b Vinicius Coque
				complete: loadComplete
217 0d6a185a Scott Ullrich
			}
218
		);
219
	}
220 5b237745 Scott Ullrich
221 0d6a185a Scott Ullrich
	function loadComplete(req) {
222 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
223 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
224
		values.shift(); values.pop();
225
226 41b1ff89 Phil Davis
		if (values.shift() == "0") {
227 0d6a185a Scott Ullrich
			var file = values.shift();
228 45d6ada5 Sjon Hortensius
			var fileContent = window.atob(values.join("|"));
229 0d6a185a Scott Ullrich
230 3f98044a Francisco Cavalcante
			$("#fileContent").val(fileContent);
231 947141fd Phil Davis
		} else {
232 3f98044a Francisco Cavalcante
			$("#fileStatus").html(values[0]);
233
			$("#fileContent").val("");
234 0d6a185a Scott Ullrich
		}
235 45d6ada5 Sjon Hortensius
236 3f98044a Francisco Cavalcante
		$("#fileContent").show(1000);
237 0d6a185a Scott Ullrich
	}
238 5b237745 Scott Ullrich
239 0d6a185a Scott Ullrich
	function saveFile(file) {
240 3f98044a Francisco Cavalcante
		$("#fileStatus").html("");
241
		$("#fileStatusBox").show(500);
242 41b1ff89 Phil Davis
243 3f98044a Francisco Cavalcante
		var fileContent = Base64.encode($("#fileContent").val());
244 6c07db48 Phil Davis
		fileContent = fileContent.replace(/\+/g, "%2B");
245 45d6ada5 Sjon Hortensius
246 3f98044a Francisco Cavalcante
		$.ajax(
247 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
248 e3c1ea9b Vinicius Coque
				type: "post",
249 3f98044a Francisco Cavalcante
				data: "action=save&file=" + $("#fbTarget").val() +
250 ee650539 Scott Ullrich
							"&data=" + fileContent,
251 e3c1ea9b Vinicius Coque
				complete: function(req) {
252 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
253 3f98044a Francisco Cavalcante
					$("#fileStatus").html(values[1]);
254 0d6a185a Scott Ullrich
				}
255
			}
256
		);
257
	}
258
259 e561ccdf Stephen Beaver
/**
260
 *
261
 *	Base64 encode / decode
262
 *	http://www.webtoolkit.info/
263
 *	http://www.webtoolkit.info/licence
264
 **/
265
266
var Base64 = {
267
268
	// private property
269
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
270
271
	// public method for encoding
272
	encode : function (input) {
273
		var output = "";
274
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
275
		var i = 0;
276
277
		input = Base64._utf8_encode(input);
278
279
		while (i < input.length) {
280
281
			chr1 = input.charCodeAt(i++);
282
			chr2 = input.charCodeAt(i++);
283
			chr3 = input.charCodeAt(i++);
284
285
			enc1 = chr1 >> 2;
286
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
287
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
288
			enc4 = chr3 & 63;
289
290
			if (isNaN(chr2)) {
291
				enc3 = enc4 = 64;
292
			} else if (isNaN(chr3)) {
293
				enc4 = 64;
294
			}
295
296
			output = output +
297
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
298
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
299
300
		}
301
302
		return output;
303
	},
304
305
	// public method for decoding
306
	decode : function (input) {
307
		var output = "";
308
		var chr1, chr2, chr3;
309
		var enc1, enc2, enc3, enc4;
310
		var i = 0;
311
312
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
313
314
		while (i < input.length) {
315
316
			enc1 = this._keyStr.indexOf(input.charAt(i++));
317
			enc2 = this._keyStr.indexOf(input.charAt(i++));
318
			enc3 = this._keyStr.indexOf(input.charAt(i++));
319
			enc4 = this._keyStr.indexOf(input.charAt(i++));
320
321
			chr1 = (enc1 << 2) | (enc2 >> 4);
322
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
323
			chr3 = ((enc3 & 3) << 6) | enc4;
324
325
			output = output + String.fromCharCode(chr1);
326
327
			if (enc3 != 64) {
328
				output = output + String.fromCharCode(chr2);
329
			}
330
			if (enc4 != 64) {
331
				output = output + String.fromCharCode(chr3);
332
			}
333
334
		}
335
336
		output = Base64._utf8_decode(output);
337
338
		return output;
339
340
	},
341
342
	// private method for UTF-8 encoding
343
	_utf8_encode : function (string) {
344
		string = string.replace(/\r\n/g,"\n");
345
		var utftext = "";
346
347
		for (var n = 0; n < string.length; n++) {
348
349
			var c = string.charCodeAt(n);
350
351
			if (c < 128) {
352
				utftext += String.fromCharCode(c);
353 947141fd Phil Davis
			} else if ((c > 127) && (c < 2048)) {
354 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 6) | 192);
355
				utftext += String.fromCharCode((c & 63) | 128);
356 947141fd Phil Davis
			} else {
357 e561ccdf Stephen Beaver
				utftext += String.fromCharCode((c >> 12) | 224);
358
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
359
				utftext += String.fromCharCode((c & 63) | 128);
360
			}
361
362
		}
363
364
		return utftext;
365
	},
366
367
	// private method for UTF-8 decoding
368
	_utf8_decode : function (utftext) {
369
		var string = "";
370
		var i = 0;
371
		var c = c1 = c2 = 0;
372
373 947141fd Phil Davis
		while (i < utftext.length) {
374 e561ccdf Stephen Beaver
375
			c = utftext.charCodeAt(i);
376
377
			if (c < 128) {
378
				string += String.fromCharCode(c);
379
				i++;
380 947141fd Phil Davis
			} else if ((c > 191) && (c < 224)) {
381 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
382
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
383
				i += 2;
384 947141fd Phil Davis
			} else {
385 e561ccdf Stephen Beaver
				c2 = utftext.charCodeAt(i+1);
386
				c3 = utftext.charCodeAt(i+2);
387
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
388
				i += 3;
389
			}
390
391
		}
392
393
		return string;
394
	}
395
396
};
397
398 288a2a0f Phil Davis
	<?php if ($_GET['action'] == "load"): ?>
399 45d6ada5 Sjon Hortensius
		events.push(function() {
400 3f98044a Francisco Cavalcante
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
401 45d6ada5 Sjon Hortensius
			loadFile();
402
		});
403 0d6a185a Scott Ullrich
	<?php endif; ?>
404 8fd9052f Colin Fleming
//]]>
405 0d6a185a Scott Ullrich
</script>
406 ab541dbb Scott Ullrich
407 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
408
409 c10cb196 Stephen Beaver
outputJavaScriptFileInline("filebrowser/browser.js");