Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_edit.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-diagnostics-edit
24
##|*NAME=Diagnostics: Edit File
25
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
26
##|*MATCH=diag_edit.php*
27
##|*MATCH=browser.php*
28
##|*MATCH=vendor/filebrowser/browser.php*
29
##|-PRIV
30

    
31
$pgtitle = array(gettext("Diagnostics"), gettext("Edit File"));
32
require_once("guiconfig.inc");
33

    
34
if ($_POST['action']) {
35
	switch ($_POST['action']) {
36
		case 'load':
37
			if (strlen($_POST['file']) < 1) {
38
				print('|5|');
39
				print_info_box(gettext("No file name specified."), 'danger');
40
				print('|');
41
			} elseif (is_dir($_POST['file'])) {
42
				print('|4|');
43
				print_info_box(gettext("Loading a directory is not supported."), 'danger');
44
				print('|');
45
			} elseif (!is_file($_POST['file'])) {
46
				print('|3|');
47
				print_info_box(gettext("File does not exist or is not a regular file."), 'danger');
48
				print('|');
49
			} else {
50
				$data = file_get_contents(urldecode($_POST['file']));
51
				if ($data === false) {
52
					print('|1|');
53
					print_info_box(gettext("Failed to read file."), 'danger');
54
					print('|');
55
				} else {
56
					$data = base64_encode($data);
57
					print("|0|{$_POST['file']}|{$data}|");
58
				}
59
			}
60
			exit;
61

    
62
		case 'save':
63
			if (strlen($_POST['file']) < 1) {
64
				print('|');
65
				print_info_box(gettext("No file name specified."), 'danger');
66
				print('|');
67
			} else {
68
				conf_mount_rw();
69
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
70
				$ret = file_put_contents($_POST['file'], $_POST['data']);
71
				conf_mount_ro();
72
				if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
73
					if (file_exists("/tmp/config.cache")) {
74
						unlink("/tmp/config.cache");
75
					}
76
					disable_security_checks();
77
				}
78
				if ($ret === false) {
79
					print('|');
80
					print_info_box(gettext("Failed to write file."), 'danger');
81
					print('|');
82
				} elseif ($ret != strlen($_POST['data'])) {
83
					print('|');
84
					print_info_box(gettext("Error while writing file."), 'danger');
85
					print('|');
86
				} else {
87
					print('|');
88
					print_info_box(gettext("File saved successfully."), 'success');
89
					print('|');
90
				}
91
			}
92
			exit;
93
	}
94
	exit;
95
}
96

    
97
require_once("head.inc");
98

    
99
print_callout(gettext("The capabilities offered here can be dangerous. No support is available. Use them at your own risk!"), 'danger', gettext('Advanced Users Only'));
100

    
101
?>
102
<!-- file status box -->
103
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
104
	<div id="fileStatus"></div>
105
</div>
106

    
107
<div class="panel panel-default">
108
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a File from the Filesystem")?></h2></div>
109
	<div class="panel-body">
110
		<div class="content">
111
			<form>
112
				<p><input type="text" class="form-control" id="fbTarget" placeholder="<?=gettext('Path to file to be edited')?>"/></p>
113
				<div class="btn-group">
114
					<p>
115
						<button type="button" class="btn btn-default btn-sm" onclick="loadFile();"	value="<?=gettext('Load')?>">
116
							<i class="fa fa-file-text-o"></i>
117
							<?=gettext('Load')?>
118
						</button>
119
						<button type="button" class="btn btn-default btn-sm" id="fbOpen"		value="<?=gettext('Browse')?>">
120
							<i class="fa fa-list"></i>
121
							<?=gettext('Browse')?>
122
						</button>
123
						<button type="button" class="btn btn-default btn-sm" onclick="saveFile();"	value="<?=gettext('Save')?>">
124
							<i class="fa fa-save"></i>
125
							<?=gettext('Save')?>
126
						</button>
127
					</p>
128
				</div>
129
				<p class="pull-right">
130
					<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" style="padding: 3px 0px;"/>
131
				</p>
132
			</form>
133

    
134
			<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%; padding:10px"></div>
135

    
136
			<script type="text/javascript">
137
			//<![CDATA[
138
			window.onload=function() {
139
				document.getElementById("fileContent").wrap='off';
140
			}
141
			//]]>
142
			</script>
143
			<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols="20"></textarea>
144
		</div>
145
	</div>
146
</div>
147

    
148
<script type="text/javascript">
149
//<![CDATA[
150
	events.push(function(){
151

    
152
		function showLine(tarea, lineNum) {
153

    
154
			lineNum--; // array starts at 0
155
			var lines = tarea.value.split("\n");
156

    
157
			// calculate start/end
158
			var startPos = 0, endPos = tarea.value.length;
159
			for (var x = 0; x < lines.length; x++) {
160
				if (x == lineNum) {
161
					break;
162
				}
163
				startPos += (lines[x].length+1);
164

    
165
			}
166

    
167
			var endPos = lines[lineNum].length+startPos;
168

    
169
			// do selection
170
			// Chrome / Firefox
171

    
172
			if (typeof(tarea.selectionStart) != "undefined") {
173
				tarea.focus();
174
				tarea.selectionStart = startPos;
175
				tarea.selectionEnd = endPos;
176
				return true;
177
			}
178

    
179
			// IE
180
			if (document.selection && document.selection.createRange) {
181
				tarea.focus();
182
				tarea.select();
183
				var range = document.selection.createRange();
184
				range.collapse(true);
185
				range.moveEnd("character", endPos);
186
				range.moveStart("character", startPos);
187
				range.select();
188
				return true;
189
			}
190

    
191
			return false;
192
		}
193

    
194
		$("#btngoto").prop('type','button');
195

    
196
		//On clicking the GoTo button, validate the entered value
197
		// and highlight the required line
198
		$('#btngoto').click(function() {
199
			var tarea = document.getElementById("fileContent");
200
			var gtl = $('#gotoline').val();
201
			var lines = $("#fileContent").val().split(/\r|\r\n|\n/).length;
202

    
203
			if (gtl < 1) {
204
				gtl = 1;
205
			}
206

    
207
			if (gtl > lines) {
208
				gtl = lines;
209
			}
210

    
211
			showLine(tarea, gtl);
212
		});
213
	});
214

    
215
	function loadFile() {
216
		$("#fileStatus").html("");
217
		$("#fileStatusBox").show(500);
218
		$.ajax(
219
			"<?=$_SERVER['SCRIPT_NAME']?>", {
220
				type: "post",
221
				data: "action=load&file=" + $("#fbTarget").val(),
222
				complete: loadComplete
223
			}
224
		);
225
	}
226

    
227
	function loadComplete(req) {
228
		$("#fileContent").show(1000);
229
		var values = req.responseText.split("|");
230
		values.shift(); values.pop();
231

    
232
		if (values.shift() == "0") {
233
			var file = values.shift();
234
			var fileContent = window.atob(values.join("|"));
235

    
236
			$("#fileContent").val(fileContent);
237
		} else {
238
			$("#fileStatus").html(values[0]);
239
			$("#fileContent").val("");
240
		}
241

    
242
		$("#fileContent").show(1000);
243
	}
244

    
245
	function saveFile(file) {
246
		$("#fileStatus").html("");
247
		$("#fileStatusBox").show(500);
248

    
249
		var fileContent = Base64.encode($("#fileContent").val());
250
		fileContent = fileContent.replace(/\+/g, "%2B");
251

    
252
		$.ajax(
253
			"<?=$_SERVER['SCRIPT_NAME']?>", {
254
				type: "post",
255
				data: "action=save&file=" + $("#fbTarget").val() +
256
							"&data=" + fileContent,
257
				complete: function(req) {
258
					var values = req.responseText.split("|");
259
					$("#fileStatus").html(values[1]);
260
				}
261
			}
262
		);
263
	}
264

    
265
/**
266
 *
267
 *	Base64 encode / decode
268
 *	http://www.webtoolkit.info/
269
 *	http://www.webtoolkit.info/licence
270
 **/
271

    
272
var Base64 = {
273

    
274
	// private property
275
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
276

    
277
	// public method for encoding
278
	encode : function (input) {
279
		var output = "";
280
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
281
		var i = 0;
282

    
283
		input = Base64._utf8_encode(input);
284

    
285
		while (i < input.length) {
286

    
287
			chr1 = input.charCodeAt(i++);
288
			chr2 = input.charCodeAt(i++);
289
			chr3 = input.charCodeAt(i++);
290

    
291
			enc1 = chr1 >> 2;
292
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
293
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
294
			enc4 = chr3 & 63;
295

    
296
			if (isNaN(chr2)) {
297
				enc3 = enc4 = 64;
298
			} else if (isNaN(chr3)) {
299
				enc4 = 64;
300
			}
301

    
302
			output = output +
303
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
304
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
305

    
306
		}
307

    
308
		return output;
309
	},
310

    
311
	// public method for decoding
312
	decode : function (input) {
313
		var output = "";
314
		var chr1, chr2, chr3;
315
		var enc1, enc2, enc3, enc4;
316
		var i = 0;
317

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

    
320
		while (i < input.length) {
321

    
322
			enc1 = this._keyStr.indexOf(input.charAt(i++));
323
			enc2 = this._keyStr.indexOf(input.charAt(i++));
324
			enc3 = this._keyStr.indexOf(input.charAt(i++));
325
			enc4 = this._keyStr.indexOf(input.charAt(i++));
326

    
327
			chr1 = (enc1 << 2) | (enc2 >> 4);
328
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
329
			chr3 = ((enc3 & 3) << 6) | enc4;
330

    
331
			output = output + String.fromCharCode(chr1);
332

    
333
			if (enc3 != 64) {
334
				output = output + String.fromCharCode(chr2);
335
			}
336
			if (enc4 != 64) {
337
				output = output + String.fromCharCode(chr3);
338
			}
339

    
340
		}
341

    
342
		output = Base64._utf8_decode(output);
343

    
344
		return output;
345

    
346
	},
347

    
348
	// private method for UTF-8 encoding
349
	_utf8_encode : function (string) {
350
		string = string.replace(/\r\n/g,"\n");
351
		var utftext = "";
352

    
353
		for (var n = 0; n < string.length; n++) {
354

    
355
			var c = string.charCodeAt(n);
356

    
357
			if (c < 128) {
358
				utftext += String.fromCharCode(c);
359
			} else if ((c > 127) && (c < 2048)) {
360
				utftext += String.fromCharCode((c >> 6) | 192);
361
				utftext += String.fromCharCode((c & 63) | 128);
362
			} else {
363
				utftext += String.fromCharCode((c >> 12) | 224);
364
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
365
				utftext += String.fromCharCode((c & 63) | 128);
366
			}
367

    
368
		}
369

    
370
		return utftext;
371
	},
372

    
373
	// private method for UTF-8 decoding
374
	_utf8_decode : function (utftext) {
375
		var string = "";
376
		var i = 0;
377
		var c = c1 = c2 = 0;
378

    
379
		while (i < utftext.length) {
380

    
381
			c = utftext.charCodeAt(i);
382

    
383
			if (c < 128) {
384
				string += String.fromCharCode(c);
385
				i++;
386
			} else if ((c > 191) && (c < 224)) {
387
				c2 = utftext.charCodeAt(i+1);
388
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
389
				i += 2;
390
			} else {
391
				c2 = utftext.charCodeAt(i+1);
392
				c3 = utftext.charCodeAt(i+2);
393
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
394
				i += 3;
395
			}
396

    
397
		}
398

    
399
		return string;
400
	}
401

    
402
};
403

    
404
	<?php if ($_GET['action'] == "load"): ?>
405
		events.push(function() {
406
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
407
			loadFile();
408
		});
409
	<?php endif; ?>
410
//]]>
411
</script>
412

    
413
<?php include("foot.inc");
414

    
415
outputJavaScriptFileInline("vendor/filebrowser/browser.js");
(13-13/227)