Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	edit.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Redistribution and use in source and binary forms, with or without modification,
9
 *	are permitted provided that the following conditions are met:
10
 *
11
 *	1. Redistributions of source code must retain the above copyright notice,
12
 *		this list of conditions and the following disclaimer.
13
 *
14
 *	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
 *
19
 *	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
 *
24
 *	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
 *
29
 *	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
 *
33
 *	6. Redistributions of any form whatsoever must retain the following
34
 *		acknowledgment:
35
 *
36
 *	"This product includes software developed by the pfSense Project
37
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *	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
 *
52
 *	====================================================================
53
 *
54
 */
55
/*
56
	pfSense_MODULE: shell
57
*/
58

    
59
##|+PRIV
60
##|*IDENT=page-diagnostics-edit
61
##|*NAME=Diagnostics: Edit File
62
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
63
##|*MATCH=edit.php*
64
##|*MATCH=browser.php*
65
##|*MATCH=filebrowser/browser.php*
66
##|-PRIV
67

    
68
$pgtitle = array(gettext("Diagnostics"), gettext("Edit file"));
69
require("guiconfig.inc");
70

    
71
if ($_POST['action']) {
72
	switch ($_POST['action']) {
73
		case 'load':
74
			if (strlen($_POST['file']) < 1) {
75
				print('|5|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
76
			} elseif (is_dir($_POST['file'])) {
77
				print('|4|' . '<div class="alert alert-danger" role="alert">' . gettext("Loading a directory is not supported") .'</div>' . '|');
78
			} elseif (!is_file($_POST['file'])) {
79
				print('|3|' . '<div class="alert alert-danger" role="alert">' . gettext("File does not exist or is not a regular file") . '</div>' . '|');
80
			} else {
81
				$data = file_get_contents(urldecode($_POST['file']));
82
				if ($data === false) {
83
					print('|1|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to read file") . '</div>' . '|');
84
				} else {
85
					$data = base64_encode($data);
86
					print("|0|{$_POST['file']}|{$data}|");
87
				}
88
			}
89
			exit;
90

    
91
		case 'save':
92
			if (strlen($_POST['file']) < 1) {
93
				print('|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
94
			} else {
95
				conf_mount_rw();
96
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
97
				$ret = file_put_contents($_POST['file'], $_POST['data']);
98
				conf_mount_ro();
99
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
100
					if (file_exists("/tmp/config.cache")) {
101
						unlink("/tmp/config.cache");
102
					}
103
					disable_security_checks();
104
				}
105
				if ($ret === false) {
106
					print('|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to write file") . '</div>' . '|');
107
				} elseif ($ret != strlen($_POST['data'])) {
108
					print('|' . '<div class="alert alert-danger" role="alert">' . gettext("Error while writing file") . '</div>' . '|');
109
				} else {
110
					print('|' . '<div class="alert alert-success" role="alert">' . gettext("File saved successfully") . '</div>' . '|');
111
				}
112
			}
113
			exit;
114
	}
115
	exit;
116
}
117

    
118
require("head.inc");
119
?>
120
<!-- file status box -->
121
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
122
		<strong id="fileStatus"></strong>
123
</div>
124

    
125
<div class="panel panel-default">
126
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a file from the filesystem")?></h2></div>
127
	<div class="panel-body">
128
		<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
			</form>
135

    
136
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
137

    
138
			<div style="background:#eeeeee;" id="fileOutput">
139
				<script type="text/javascript">
140
				//<![CDATA[
141
				window.onload=function() {
142
					document.getElementById("fileContent").wrap='off';
143
				}
144
				//]]>
145
				</script>
146
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols=""></textarea>
147
			</div>
148
		</div>
149
	</div>
150
</div>
151

    
152
<script type="text/javascript">
153
//<![CDATA[
154
	function loadFile() {
155
		jQuery("#fileStatus").html("");
156
		jQuery("#fileStatusBox").show(500);
157
		jQuery.ajax(
158
			"<?=$_SERVER['SCRIPT_NAME']?>", {
159
				type: "post",
160
				data: "action=load&file=" + jQuery("#fbTarget").val(),
161
				complete: loadComplete
162
			}
163
		);
164
	}
165

    
166
	function loadComplete(req) {
167
		jQuery("#fileContent").show(1000);
168
		var values = req.responseText.split("|");
169
		values.shift(); values.pop();
170

    
171
		if (values.shift() == "0") {
172
			var file = values.shift();
173
			var fileContent = window.atob(values.join("|"));
174

    
175
			jQuery("#fileContent").val(fileContent);
176
		} else {
177
			jQuery("#fileStatus").html(values[0]);
178
			jQuery("#fileContent").val("");
179
		}
180

    
181
		jQuery("#fileContent").show(1000);
182
	}
183

    
184
	function saveFile(file) {
185
		jQuery("#fileStatus").html("");
186
		jQuery("#fileStatusBox").show(500);
187

    
188
		var fileContent = Base64.encode(jQuery("#fileContent").val());
189
		fileContent = fileContent.replace(/\+/g, "%2B");
190

    
191
		jQuery.ajax(
192
			"<?=$_SERVER['SCRIPT_NAME']?>", {
193
				type: "post",
194
				data: "action=save&file=" + jQuery("#fbTarget").val() +
195
							"&data=" + fileContent,
196
				complete: function(req) {
197
					var values = req.responseText.split("|");
198
					jQuery("#fileStatus").html(values[1]);
199
				}
200
			}
201
		);
202
	}
203

    
204
/**
205
 *
206
 *	Base64 encode / decode
207
 *	http://www.webtoolkit.info/
208
 *	http://www.webtoolkit.info/licence
209
 **/
210

    
211
var Base64 = {
212

    
213
	// private property
214
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
215

    
216
	// public method for encoding
217
	encode : function (input) {
218
		var output = "";
219
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
220
		var i = 0;
221

    
222
		input = Base64._utf8_encode(input);
223

    
224
		while (i < input.length) {
225

    
226
			chr1 = input.charCodeAt(i++);
227
			chr2 = input.charCodeAt(i++);
228
			chr3 = input.charCodeAt(i++);
229

    
230
			enc1 = chr1 >> 2;
231
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
232
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
233
			enc4 = chr3 & 63;
234

    
235
			if (isNaN(chr2)) {
236
				enc3 = enc4 = 64;
237
			} else if (isNaN(chr3)) {
238
				enc4 = 64;
239
			}
240

    
241
			output = output +
242
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
243
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
244

    
245
		}
246

    
247
		return output;
248
	},
249

    
250
	// public method for decoding
251
	decode : function (input) {
252
		var output = "";
253
		var chr1, chr2, chr3;
254
		var enc1, enc2, enc3, enc4;
255
		var i = 0;
256

    
257
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
258

    
259
		while (i < input.length) {
260

    
261
			enc1 = this._keyStr.indexOf(input.charAt(i++));
262
			enc2 = this._keyStr.indexOf(input.charAt(i++));
263
			enc3 = this._keyStr.indexOf(input.charAt(i++));
264
			enc4 = this._keyStr.indexOf(input.charAt(i++));
265

    
266
			chr1 = (enc1 << 2) | (enc2 >> 4);
267
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
268
			chr3 = ((enc3 & 3) << 6) | enc4;
269

    
270
			output = output + String.fromCharCode(chr1);
271

    
272
			if (enc3 != 64) {
273
				output = output + String.fromCharCode(chr2);
274
			}
275
			if (enc4 != 64) {
276
				output = output + String.fromCharCode(chr3);
277
			}
278

    
279
		}
280

    
281
		output = Base64._utf8_decode(output);
282

    
283
		return output;
284

    
285
	},
286

    
287
	// private method for UTF-8 encoding
288
	_utf8_encode : function (string) {
289
		string = string.replace(/\r\n/g,"\n");
290
		var utftext = "";
291

    
292
		for (var n = 0; n < string.length; n++) {
293

    
294
			var c = string.charCodeAt(n);
295

    
296
			if (c < 128) {
297
				utftext += String.fromCharCode(c);
298
			} else if ((c > 127) && (c < 2048)) {
299
				utftext += String.fromCharCode((c >> 6) | 192);
300
				utftext += String.fromCharCode((c & 63) | 128);
301
			} else {
302
				utftext += String.fromCharCode((c >> 12) | 224);
303
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
304
				utftext += String.fromCharCode((c & 63) | 128);
305
			}
306

    
307
		}
308

    
309
		return utftext;
310
	},
311

    
312
	// private method for UTF-8 decoding
313
	_utf8_decode : function (utftext) {
314
		var string = "";
315
		var i = 0;
316
		var c = c1 = c2 = 0;
317

    
318
		while (i < utftext.length) {
319

    
320
			c = utftext.charCodeAt(i);
321

    
322
			if (c < 128) {
323
				string += String.fromCharCode(c);
324
				i++;
325
			} else if ((c > 191) && (c < 224)) {
326
				c2 = utftext.charCodeAt(i+1);
327
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
328
				i += 2;
329
			} else {
330
				c2 = utftext.charCodeAt(i+1);
331
				c3 = utftext.charCodeAt(i+2);
332
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
333
				i += 3;
334
			}
335

    
336
		}
337

    
338
		return string;
339
	}
340

    
341
};
342

    
343
	<?php if ($_GET['action'] == "load"): ?>
344
		events.push(function() {
345
			jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
346
			loadFile();
347
		});
348
	<?php endif; ?>
349
//]]>
350
</script>
351

    
352
<?php include("foot.inc");
353

    
354
outputJavaScriptFileInline("filebrowser/browser.js");
(33-33/228)