Project

General

Profile

Download (11.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_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
##|+PRIV
57
##|*IDENT=page-diagnostics-edit
58
##|*NAME=Diagnostics: Edit File
59
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
60
##|*MATCH=diag_edit.php*
61
##|*MATCH=browser.php*
62
##|*MATCH=filebrowser/browser.php*
63
##|-PRIV
64

    
65
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
66
require("guiconfig.inc");
67

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

    
96
		case 'save':
97
			if (strlen($_POST['file']) < 1) {
98
				print('|');
99
				print_info_box(gettext("No file name specified."), 'danger');
100
				print('|');
101
			} else {
102
				conf_mount_rw();
103
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
104
				$ret = file_put_contents($_POST['file'], $_POST['data']);
105
				conf_mount_ro();
106
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
107
					if (file_exists("/tmp/config.cache")) {
108
						unlink("/tmp/config.cache");
109
					}
110
					disable_security_checks();
111
				}
112
				if ($ret === false) {
113
					print('|');
114
					print_info_box(gettext("Failed to write file."), 'danger');
115
					print('|');
116
				} elseif ($ret != strlen($_POST['data'])) {
117
					print('|');
118
					print_info_box(gettext("Error while writing file."), 'danger');
119
					print('|');
120
				} else {
121
					print('|');
122
					print_info_box(gettext("File saved successfully."), 'success');
123
					print('|');
124
				}
125
			}
126
			exit;
127
	}
128
	exit;
129
}
130

    
131
require("head.inc");
132
?>
133
<!-- file status box -->
134
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
135
	<div id="fileStatus"></div>
136
</div>
137

    
138
<div class="panel panel-default">
139
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
140
	<div class="panel-body">
141
		<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
				<span class="pull-right">
148
					<button id="btngoto" class="btn btn-default btn-sm"><?=gettext("GoTo Line #")?></button> <input type="number" id="gotoline" size="6" />
149
				</span>
150
			</form>
151

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

    
154
			<div style="background:#eeeeee;" id="fileOutput">
155
				<script type="text/javascript">
156
				//<![CDATA[
157
				window.onload=function() {
158
					document.getElementById("fileContent").wrap='off';
159
				}
160
				//]]>
161
				</script>
162
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
163
			</div>
164
		</div>
165
	</div>
166
</div>
167

    
168
<script type="text/javascript">
169
//<![CDATA[
170
	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
	function loadFile() {
223
		$("#fileStatus").html("");
224
		$("#fileStatusBox").show(500);
225
		$.ajax(
226
			"<?=$_SERVER['SCRIPT_NAME']?>", {
227
				type: "post",
228
				data: "action=load&file=" + $("#fbTarget").val(),
229
				complete: loadComplete
230
			}
231
		);
232
	}
233

    
234
	function loadComplete(req) {
235
		$("#fileContent").show(1000);
236
		var values = req.responseText.split("|");
237
		values.shift(); values.pop();
238

    
239
		if (values.shift() == "0") {
240
			var file = values.shift();
241
			var fileContent = window.atob(values.join("|"));
242

    
243
			$("#fileContent").val(fileContent);
244
		} else {
245
			$("#fileStatus").html(values[0]);
246
			$("#fileContent").val("");
247
		}
248

    
249
		$("#fileContent").show(1000);
250
	}
251

    
252
	function saveFile(file) {
253
		$("#fileStatus").html("");
254
		$("#fileStatusBox").show(500);
255

    
256
		var fileContent = Base64.encode($("#fileContent").val());
257
		fileContent = fileContent.replace(/\+/g, "%2B");
258

    
259
		$.ajax(
260
			"<?=$_SERVER['SCRIPT_NAME']?>", {
261
				type: "post",
262
				data: "action=save&file=" + $("#fbTarget").val() +
263
							"&data=" + fileContent,
264
				complete: function(req) {
265
					var values = req.responseText.split("|");
266
					$("#fileStatus").html(values[1]);
267
				}
268
			}
269
		);
270
	}
271

    
272
/**
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
			} else if ((c > 127) && (c < 2048)) {
367
				utftext += String.fromCharCode((c >> 6) | 192);
368
				utftext += String.fromCharCode((c & 63) | 128);
369
			} else {
370
				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
		while (i < utftext.length) {
387

    
388
			c = utftext.charCodeAt(i);
389

    
390
			if (c < 128) {
391
				string += String.fromCharCode(c);
392
				i++;
393
			} else if ((c > 191) && (c < 224)) {
394
				c2 = utftext.charCodeAt(i+1);
395
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
396
				i += 2;
397
			} else {
398
				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
	<?php if ($_GET['action'] == "load"): ?>
412
		events.push(function() {
413
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
414
			loadFile();
415
		});
416
	<?php endif; ?>
417
//]]>
418
</script>
419

    
420
<?php include("foot.inc");
421

    
422
outputJavaScriptFileInline("filebrowser/browser.js");
(14-14/227)