Project

General

Profile

Download (11.6 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=vendor/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
				<div class="btn-group">
145
					<button type="button" class="btn btn-default btn-sm" onclick="loadFile();"	value="<?=gettext('Load')?>">
146
						<i class="fa fa-file-text-o"></i>
147
						<?=gettext('Load')?>
148
					</button>
149
					<button type="button" class="btn btn-default btn-sm" id="fbOpen"		value="<?=gettext('Browse')?>">
150
						<i class="fa fa-list"></i>
151
						<?=gettext('Browse')?>
152
					</button>
153
					<button type="button" class="btn btn-default btn-sm" onclick="saveFile();"	value="<?=gettext('Save')?>">
154
						<i class="fa fa-save"></i>
155
						<?=gettext('Save')?>
156
					</button>
157
				</div>
158
				<span class="pull-right">
159
					<button id="btngoto" class="btn btn-default btn-sm"><i class="fa fa-forward"></i><?=gettext("GoTo Line #")?></button> <input type="number" id="gotoline" size="6" />
160
				</span>
161
			</form>
162

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

    
165
			<div style="background:#eeeeee;" id="fileOutput">
166
				<script type="text/javascript">
167
				//<![CDATA[
168
				window.onload=function() {
169
					document.getElementById("fileContent").wrap='off';
170
				}
171
				//]]>
172
				</script>
173
				<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
174
			</div>
175
		</div>
176
	</div>
177
</div>
178

    
179
<script type="text/javascript">
180
//<![CDATA[
181
	events.push(function(){
182

    
183
		function showLine(tarea, lineNum) {
184

    
185
			lineNum--; // array starts at 0
186
			var lines = tarea.value.split("\n");
187

    
188
			// calculate start/end
189
			var startPos = 0, endPos = tarea.value.length;
190
			for(var x = 0; x < lines.length; x++) {
191
				if(x == lineNum) {
192
					break;
193
				}
194
				startPos += (lines[x].length+1);
195

    
196
			}
197

    
198
			var endPos = lines[lineNum].length+startPos;
199

    
200
			// do selection
201
			// Chrome / Firefox
202

    
203
			if(typeof(tarea.selectionStart) != "undefined") {
204
				tarea.focus();
205
				tarea.selectionStart = startPos;
206
				tarea.selectionEnd = endPos;
207
				return true;
208
			}
209

    
210
			// IE
211
			if (document.selection && document.selection.createRange) {
212
				tarea.focus();
213
				tarea.select();
214
				var range = document.selection.createRange();
215
				range.collapse(true);
216
				range.moveEnd("character", endPos);
217
				range.moveStart("character", startPos);
218
				range.select();
219
				return true;
220
			}
221

    
222
			return false;
223
		}
224

    
225
		$("#btngoto").prop('type','button');
226

    
227
		$('#btngoto').click(function() {
228
			var tarea = document.getElementById("fileContent");
229
			showLine(tarea, $('#gotoline').val());
230
		});
231
	});
232

    
233
	function loadFile() {
234
		$("#fileStatus").html("");
235
		$("#fileStatusBox").show(500);
236
		$.ajax(
237
			"<?=$_SERVER['SCRIPT_NAME']?>", {
238
				type: "post",
239
				data: "action=load&file=" + $("#fbTarget").val(),
240
				complete: loadComplete
241
			}
242
		);
243
	}
244

    
245
	function loadComplete(req) {
246
		$("#fileContent").show(1000);
247
		var values = req.responseText.split("|");
248
		values.shift(); values.pop();
249

    
250
		if (values.shift() == "0") {
251
			var file = values.shift();
252
			var fileContent = window.atob(values.join("|"));
253

    
254
			$("#fileContent").val(fileContent);
255
		} else {
256
			$("#fileStatus").html(values[0]);
257
			$("#fileContent").val("");
258
		}
259

    
260
		$("#fileContent").show(1000);
261
	}
262

    
263
	function saveFile(file) {
264
		$("#fileStatus").html("");
265
		$("#fileStatusBox").show(500);
266

    
267
		var fileContent = Base64.encode($("#fileContent").val());
268
		fileContent = fileContent.replace(/\+/g, "%2B");
269

    
270
		$.ajax(
271
			"<?=$_SERVER['SCRIPT_NAME']?>", {
272
				type: "post",
273
				data: "action=save&file=" + $("#fbTarget").val() +
274
							"&data=" + fileContent,
275
				complete: function(req) {
276
					var values = req.responseText.split("|");
277
					$("#fileStatus").html(values[1]);
278
				}
279
			}
280
		);
281
	}
282

    
283
/**
284
 *
285
 *	Base64 encode / decode
286
 *	http://www.webtoolkit.info/
287
 *	http://www.webtoolkit.info/licence
288
 **/
289

    
290
var Base64 = {
291

    
292
	// private property
293
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
294

    
295
	// public method for encoding
296
	encode : function (input) {
297
		var output = "";
298
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
299
		var i = 0;
300

    
301
		input = Base64._utf8_encode(input);
302

    
303
		while (i < input.length) {
304

    
305
			chr1 = input.charCodeAt(i++);
306
			chr2 = input.charCodeAt(i++);
307
			chr3 = input.charCodeAt(i++);
308

    
309
			enc1 = chr1 >> 2;
310
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
311
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
312
			enc4 = chr3 & 63;
313

    
314
			if (isNaN(chr2)) {
315
				enc3 = enc4 = 64;
316
			} else if (isNaN(chr3)) {
317
				enc4 = 64;
318
			}
319

    
320
			output = output +
321
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
322
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
323

    
324
		}
325

    
326
		return output;
327
	},
328

    
329
	// public method for decoding
330
	decode : function (input) {
331
		var output = "";
332
		var chr1, chr2, chr3;
333
		var enc1, enc2, enc3, enc4;
334
		var i = 0;
335

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

    
338
		while (i < input.length) {
339

    
340
			enc1 = this._keyStr.indexOf(input.charAt(i++));
341
			enc2 = this._keyStr.indexOf(input.charAt(i++));
342
			enc3 = this._keyStr.indexOf(input.charAt(i++));
343
			enc4 = this._keyStr.indexOf(input.charAt(i++));
344

    
345
			chr1 = (enc1 << 2) | (enc2 >> 4);
346
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
347
			chr3 = ((enc3 & 3) << 6) | enc4;
348

    
349
			output = output + String.fromCharCode(chr1);
350

    
351
			if (enc3 != 64) {
352
				output = output + String.fromCharCode(chr2);
353
			}
354
			if (enc4 != 64) {
355
				output = output + String.fromCharCode(chr3);
356
			}
357

    
358
		}
359

    
360
		output = Base64._utf8_decode(output);
361

    
362
		return output;
363

    
364
	},
365

    
366
	// private method for UTF-8 encoding
367
	_utf8_encode : function (string) {
368
		string = string.replace(/\r\n/g,"\n");
369
		var utftext = "";
370

    
371
		for (var n = 0; n < string.length; n++) {
372

    
373
			var c = string.charCodeAt(n);
374

    
375
			if (c < 128) {
376
				utftext += String.fromCharCode(c);
377
			} else if ((c > 127) && (c < 2048)) {
378
				utftext += String.fromCharCode((c >> 6) | 192);
379
				utftext += String.fromCharCode((c & 63) | 128);
380
			} else {
381
				utftext += String.fromCharCode((c >> 12) | 224);
382
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
383
				utftext += String.fromCharCode((c & 63) | 128);
384
			}
385

    
386
		}
387

    
388
		return utftext;
389
	},
390

    
391
	// private method for UTF-8 decoding
392
	_utf8_decode : function (utftext) {
393
		var string = "";
394
		var i = 0;
395
		var c = c1 = c2 = 0;
396

    
397
		while (i < utftext.length) {
398

    
399
			c = utftext.charCodeAt(i);
400

    
401
			if (c < 128) {
402
				string += String.fromCharCode(c);
403
				i++;
404
			} else if ((c > 191) && (c < 224)) {
405
				c2 = utftext.charCodeAt(i+1);
406
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
407
				i += 2;
408
			} else {
409
				c2 = utftext.charCodeAt(i+1);
410
				c3 = utftext.charCodeAt(i+2);
411
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
412
				i += 3;
413
			}
414

    
415
		}
416

    
417
		return string;
418
	}
419

    
420
};
421

    
422
	<?php if ($_GET['action'] == "load"): ?>
423
		events.push(function() {
424
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
425
			loadFile();
426
		});
427
	<?php endif; ?>
428
//]]>
429
</script>
430

    
431
<?php include("foot.inc");
432

    
433
outputJavaScriptFileInline("vendor/filebrowser/browser.js");
(14-14/225)