Project

General

Profile

Download (10.3 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
 *	Copyright (c)  2004, 2005 Scott Ullrich
8 fd9ebcd5 Stephen Beaver
 *
9 e561ccdf Stephen Beaver
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11 fd9ebcd5 Stephen Beaver
 *
12 e561ccdf Stephen Beaver
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14 fd9ebcd5 Stephen Beaver
 *
15 e561ccdf Stephen Beaver
 *	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 fd9ebcd5 Stephen Beaver
 *
20 e561ccdf Stephen Beaver
 *	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 fd9ebcd5 Stephen Beaver
 *
25 e561ccdf Stephen Beaver
 *	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 fd9ebcd5 Stephen Beaver
 *
30 e561ccdf Stephen Beaver
 *	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 fd9ebcd5 Stephen Beaver
 *
34 e561ccdf Stephen Beaver
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36 fd9ebcd5 Stephen Beaver
 *
37 e561ccdf Stephen Beaver
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39 fd9ebcd5 Stephen Beaver
  *
40 e561ccdf Stephen Beaver
 *	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 fd9ebcd5 Stephen Beaver
 *
53 e561ccdf Stephen Beaver
 *	====================================================================
54 fd9ebcd5 Stephen Beaver
 *
55
 */
56 7ac5a4cb Scott Ullrich
/*
57 e561ccdf Stephen Beaver
	pfSense_MODULE: shell
58 7ac5a4cb Scott Ullrich
*/
59 a7f908db Scott Ullrich
60 9fbb3599 Ermal
##|+PRIV
61
##|*IDENT=page-diagnostics-edit
62
##|*NAME=Diagnostics: Edit FIle
63
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
64
##|*MATCH=edit.php*
65 c0f613e2 Ermal
##|*MATCH=browser.php*
66
##|*MATCH=filebrowser/browser.php*
67 9fbb3599 Ermal
##|-PRIV
68
69 fdb38c10 Scott Ullrich
$pgtitle = array(gettext("Diagnostics"), gettext("Edit file"));
70 859329c8 Scott Ullrich
require("guiconfig.inc");
71
72 41b1ff89 Phil Davis
if ($_POST['action']) {
73
	switch ($_POST['action']) {
74 0d6a185a Scott Ullrich
		case 'load':
75 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
76 45d6ada5 Sjon Hortensius
				print('|5|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
77 288a2a0f Phil Davis
			} elseif (is_dir($_POST['file'])) {
78 45d6ada5 Sjon Hortensius
				print('|4|' . '<div class="alert alert-danger" role="alert">' . gettext("Loading a directory is not supported") .'</div>' . '|');
79 288a2a0f Phil Davis
			} elseif (!is_file($_POST['file'])) {
80 45d6ada5 Sjon Hortensius
				print('|3|' . '<div class="alert alert-danger" role="alert">' . gettext("File does not exist or is not a regular file") . '</div>' . '|');
81 0d6a185a Scott Ullrich
			} else {
82 55344e2c Ermal
				$data = file_get_contents(urldecode($_POST['file']));
83 288a2a0f Phil Davis
				if ($data === false) {
84 45d6ada5 Sjon Hortensius
					print('|1|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to read file") . '</div>' . '|');
85 0d6a185a Scott Ullrich
				} else {
86 b71f0cbb Ermal
					$data = base64_encode($data);
87 45d6ada5 Sjon Hortensius
					print("|0|{$_POST['file']}|{$data}|");
88 0d6a185a Scott Ullrich
				}
89
			}
90
			exit;
91 45d6ada5 Sjon Hortensius
92 0d6a185a Scott Ullrich
		case 'save':
93 288a2a0f Phil Davis
			if (strlen($_POST['file']) < 1) {
94 45d6ada5 Sjon Hortensius
				print('|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
95 0d6a185a Scott Ullrich
			} else {
96 5a557f44 jim-p
				conf_mount_rw();
97 55344e2c Ermal
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
98
				$ret = file_put_contents($_POST['file'], $_POST['data']);
99 5a557f44 jim-p
				conf_mount_ro();
100 41b1ff89 Phil Davis
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
101
					if (file_exists("/tmp/config.cache")) {
102 5f05c1e8 Scott Ullrich
						unlink("/tmp/config.cache");
103 41b1ff89 Phil Davis
					}
104 0f806eca Erik Fonnesbeck
					disable_security_checks();
105
				}
106 288a2a0f Phil Davis
				if ($ret === false) {
107 45d6ada5 Sjon Hortensius
					print('|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to write file") . '</div>' . '|');
108 288a2a0f Phil Davis
				} elseif ($ret != strlen($_POST['data'])) {
109 45d6ada5 Sjon Hortensius
					print('|' . '<div class="alert alert-danger" role="alert">' . gettext("Error while writing file") . '</div>' . '|');
110 0d6a185a Scott Ullrich
				} else {
111 45d6ada5 Sjon Hortensius
					print('|' . '<div class="alert alert-success" role="alert">' . gettext("File saved successfully") . '</div>' . '|');
112 0d6a185a Scott Ullrich
				}
113
			}
114
			exit;
115 5124d619 Scott Ullrich
	}
116 0d6a185a Scott Ullrich
	exit;
117 5124d619 Scott Ullrich
}
118
119 0d6a185a Scott Ullrich
require("head.inc");
120 5b237745 Scott Ullrich
?>
121 45d6ada5 Sjon Hortensius
<!-- file status box -->
122
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
123
		<strong id="fileStatus"></strong>
124
</div>
125
126
<div class="panel panel-default">
127 f17594c7 Sjon Hortensius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a file from the filesystem")?></h2></div>
128 45d6ada5 Sjon Hortensius
	<div class="panel-body">
129 2ca4eec2 Jared Dillard
		<div class="content">
130
			<form>
131
				<input type="text" class="form-control" id="fbTarget"/>
132
				<input type="button" class="btn btn-default btn-sm"	  onclick="loadFile();" value="<?=gettext('Load')?>" />
133
				<input type="button" class="btn btn-default btn-sm"	  id="fbOpen"		   value="<?=gettext('Browse')?>" />
134
				<input type="button" class="btn btn-default btn-sm"	  onclick="saveFile();" value="<?=gettext('Save')?>" />
135
			</form>
136 45d6ada5 Sjon Hortensius
137 2ca4eec2 Jared Dillard
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
138 45d6ada5 Sjon Hortensius
139 2ca4eec2 Jared Dillard
			<div style="background:#eeeeee;" id="fileOutput">
140
				<script type="text/javascript">
141
				//<![CDATA[
142
				window.onload=function(){
143
					document.getElementById("fileContent").wrap='off';
144
				}
145
				//]]>
146
				</script>
147
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols=""></textarea>
148
			</div>
149 45d6ada5 Sjon Hortensius
		</div>
150
	</div>
151
</div>
152
153
<script>
154 0d6a185a Scott Ullrich
	function loadFile() {
155 45d6ada5 Sjon Hortensius
		jQuery("#fileStatus").html("");
156 e3c1ea9b Vinicius Coque
		jQuery("#fileStatusBox").show(500);
157
		jQuery.ajax(
158 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
159 e3c1ea9b Vinicius Coque
				type: "post",
160
				data: "action=load&file=" + jQuery("#fbTarget").val(),
161
				complete: loadComplete
162 0d6a185a Scott Ullrich
			}
163
		);
164
	}
165 5b237745 Scott Ullrich
166 0d6a185a Scott Ullrich
	function loadComplete(req) {
167 e3c1ea9b Vinicius Coque
		jQuery("#fileContent").show(1000);
168 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
169
		values.shift(); values.pop();
170
171 41b1ff89 Phil Davis
		if (values.shift() == "0") {
172 0d6a185a Scott Ullrich
			var file = values.shift();
173 45d6ada5 Sjon Hortensius
			var fileContent = window.atob(values.join("|"));
174 0d6a185a Scott Ullrich
175 45d6ada5 Sjon Hortensius
			jQuery("#fileContent").val(fileContent);
176 0d6a185a Scott Ullrich
		}
177
		else {
178 e3c1ea9b Vinicius Coque
			jQuery("#fileStatus").html(values[0]);
179
			jQuery("#fileContent").val("");
180 0d6a185a Scott Ullrich
		}
181 45d6ada5 Sjon Hortensius
182 e3c1ea9b Vinicius Coque
		jQuery("#fileContent").show(1000);
183 0d6a185a Scott Ullrich
	}
184 5b237745 Scott Ullrich
185 0d6a185a Scott Ullrich
	function saveFile(file) {
186 45d6ada5 Sjon Hortensius
		jQuery("#fileStatus").html("");
187 e3c1ea9b Vinicius Coque
		jQuery("#fileStatusBox").show(500);
188 41b1ff89 Phil Davis
189 e3c1ea9b Vinicius Coque
		var fileContent = Base64.encode(jQuery("#fileContent").val());
190 6c07db48 Phil Davis
		fileContent = fileContent.replace(/\+/g, "%2B");
191 45d6ada5 Sjon Hortensius
192 e3c1ea9b Vinicius Coque
		jQuery.ajax(
193 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
194 e3c1ea9b Vinicius Coque
				type: "post",
195
				data: "action=save&file=" + jQuery("#fbTarget").val() +
196 ee650539 Scott Ullrich
							"&data=" + fileContent,
197 e3c1ea9b Vinicius Coque
				complete: function(req) {
198 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
199 e3c1ea9b Vinicius Coque
					jQuery("#fileStatus").html(values[1]);
200 0d6a185a Scott Ullrich
				}
201
			}
202
		);
203
	}
204
205 e561ccdf Stephen Beaver
/**
206
 *
207
 *	Base64 encode / decode
208
 *	http://www.webtoolkit.info/
209
 *	http://www.webtoolkit.info/licence
210
 **/
211
212
var Base64 = {
213
214
	// private property
215
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
216
217
	// public method for encoding
218
	encode : function (input) {
219
		var output = "";
220
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
221
		var i = 0;
222
223
		input = Base64._utf8_encode(input);
224
225
		while (i < input.length) {
226
227
			chr1 = input.charCodeAt(i++);
228
			chr2 = input.charCodeAt(i++);
229
			chr3 = input.charCodeAt(i++);
230
231
			enc1 = chr1 >> 2;
232
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
233
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
234
			enc4 = chr3 & 63;
235
236
			if (isNaN(chr2)) {
237
				enc3 = enc4 = 64;
238
			} else if (isNaN(chr3)) {
239
				enc4 = 64;
240
			}
241
242
			output = output +
243
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
244
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
245
246
		}
247
248
		return output;
249
	},
250
251
	// public method for decoding
252
	decode : function (input) {
253
		var output = "";
254
		var chr1, chr2, chr3;
255
		var enc1, enc2, enc3, enc4;
256
		var i = 0;
257
258
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
259
260
		while (i < input.length) {
261
262
			enc1 = this._keyStr.indexOf(input.charAt(i++));
263
			enc2 = this._keyStr.indexOf(input.charAt(i++));
264
			enc3 = this._keyStr.indexOf(input.charAt(i++));
265
			enc4 = this._keyStr.indexOf(input.charAt(i++));
266
267
			chr1 = (enc1 << 2) | (enc2 >> 4);
268
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
269
			chr3 = ((enc3 & 3) << 6) | enc4;
270
271
			output = output + String.fromCharCode(chr1);
272
273
			if (enc3 != 64) {
274
				output = output + String.fromCharCode(chr2);
275
			}
276
			if (enc4 != 64) {
277
				output = output + String.fromCharCode(chr3);
278
			}
279
280
		}
281
282
		output = Base64._utf8_decode(output);
283
284
		return output;
285
286
	},
287
288
	// private method for UTF-8 encoding
289
	_utf8_encode : function (string) {
290
		string = string.replace(/\r\n/g,"\n");
291
		var utftext = "";
292
293
		for (var n = 0; n < string.length; n++) {
294
295
			var c = string.charCodeAt(n);
296
297
			if (c < 128) {
298
				utftext += String.fromCharCode(c);
299
			}
300
			else if((c > 127) && (c < 2048)) {
301
				utftext += String.fromCharCode((c >> 6) | 192);
302
				utftext += String.fromCharCode((c & 63) | 128);
303
			}
304
			else {
305
				utftext += String.fromCharCode((c >> 12) | 224);
306
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
307
				utftext += String.fromCharCode((c & 63) | 128);
308
			}
309
310
		}
311
312
		return utftext;
313
	},
314
315
	// private method for UTF-8 decoding
316
	_utf8_decode : function (utftext) {
317
		var string = "";
318
		var i = 0;
319
		var c = c1 = c2 = 0;
320
321
		while ( i < utftext.length ) {
322
323
			c = utftext.charCodeAt(i);
324
325
			if (c < 128) {
326
				string += String.fromCharCode(c);
327
				i++;
328
			}
329
			else if((c > 191) && (c < 224)) {
330
				c2 = utftext.charCodeAt(i+1);
331
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
332
				i += 2;
333
			}
334
			else {
335
				c2 = utftext.charCodeAt(i+1);
336
				c3 = utftext.charCodeAt(i+2);
337
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
338
				i += 3;
339
			}
340
341
		}
342
343
		return string;
344
	}
345
346
};
347
348 288a2a0f Phil Davis
	<?php if ($_GET['action'] == "load"): ?>
349 45d6ada5 Sjon Hortensius
		events.push(function() {
350
			jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
351
			loadFile();
352
		});
353 0d6a185a Scott Ullrich
	<?php endif; ?>
354
</script>
355 ab541dbb Scott Ullrich
356 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
357
358
outputJavaScriptFileInline("filebrowser/browser.js");