Project

General

Profile

Download (10.6 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
		// Goto the specified line on pressing the Enter key within the "Goto line" input element
215
		$('#gotoline').keyup(function(e) {
216
			if(e.keyCode == 13) {
217
				$('#btngoto').click();
218
			}
219
		});
220

    
221
	}); // e-o-events.push()
222

    
223
	function loadFile() {
224
		$("#fileStatus").html("");
225
		$("#fileStatusBox").show(500);
226
		$.ajax(
227
			"<?=$_SERVER['SCRIPT_NAME']?>", {
228
				type: "post",
229
				data: "action=load&file=" + $("#fbTarget").val(),
230
				complete: loadComplete
231
			}
232
		);
233
	}
234

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

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

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

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

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

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

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

    
273
/**
274
 *
275
 *	Base64 encode / decode
276
 *	http://www.webtoolkit.info/
277
 *	http://www.webtoolkit.info/licence
278
 **/
279

    
280
var Base64 = {
281

    
282
	// private property
283
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
284

    
285
	// public method for encoding
286
	encode : function (input) {
287
		var output = "";
288
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
289
		var i = 0;
290

    
291
		input = Base64._utf8_encode(input);
292

    
293
		while (i < input.length) {
294

    
295
			chr1 = input.charCodeAt(i++);
296
			chr2 = input.charCodeAt(i++);
297
			chr3 = input.charCodeAt(i++);
298

    
299
			enc1 = chr1 >> 2;
300
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
301
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
302
			enc4 = chr3 & 63;
303

    
304
			if (isNaN(chr2)) {
305
				enc3 = enc4 = 64;
306
			} else if (isNaN(chr3)) {
307
				enc4 = 64;
308
			}
309

    
310
			output = output +
311
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
312
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
313

    
314
		}
315

    
316
		return output;
317
	},
318

    
319
	// public method for decoding
320
	decode : function (input) {
321
		var output = "";
322
		var chr1, chr2, chr3;
323
		var enc1, enc2, enc3, enc4;
324
		var i = 0;
325

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

    
328
		while (i < input.length) {
329

    
330
			enc1 = this._keyStr.indexOf(input.charAt(i++));
331
			enc2 = this._keyStr.indexOf(input.charAt(i++));
332
			enc3 = this._keyStr.indexOf(input.charAt(i++));
333
			enc4 = this._keyStr.indexOf(input.charAt(i++));
334

    
335
			chr1 = (enc1 << 2) | (enc2 >> 4);
336
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
337
			chr3 = ((enc3 & 3) << 6) | enc4;
338

    
339
			output = output + String.fromCharCode(chr1);
340

    
341
			if (enc3 != 64) {
342
				output = output + String.fromCharCode(chr2);
343
			}
344
			if (enc4 != 64) {
345
				output = output + String.fromCharCode(chr3);
346
			}
347

    
348
		}
349

    
350
		output = Base64._utf8_decode(output);
351

    
352
		return output;
353

    
354
	},
355

    
356
	// private method for UTF-8 encoding
357
	_utf8_encode : function (string) {
358
		string = string.replace(/\r\n/g,"\n");
359
		var utftext = "";
360

    
361
		for (var n = 0; n < string.length; n++) {
362

    
363
			var c = string.charCodeAt(n);
364

    
365
			if (c < 128) {
366
				utftext += String.fromCharCode(c);
367
			} else if ((c > 127) && (c < 2048)) {
368
				utftext += String.fromCharCode((c >> 6) | 192);
369
				utftext += String.fromCharCode((c & 63) | 128);
370
			} else {
371
				utftext += String.fromCharCode((c >> 12) | 224);
372
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
373
				utftext += String.fromCharCode((c & 63) | 128);
374
			}
375

    
376
		}
377

    
378
		return utftext;
379
	},
380

    
381
	// private method for UTF-8 decoding
382
	_utf8_decode : function (utftext) {
383
		var string = "";
384
		var i = 0;
385
		var c = c1 = c2 = 0;
386

    
387
		while (i < utftext.length) {
388

    
389
			c = utftext.charCodeAt(i);
390

    
391
			if (c < 128) {
392
				string += String.fromCharCode(c);
393
				i++;
394
			} else if ((c > 191) && (c < 224)) {
395
				c2 = utftext.charCodeAt(i+1);
396
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
397
				i += 2;
398
			} else {
399
				c2 = utftext.charCodeAt(i+1);
400
				c3 = utftext.charCodeAt(i+2);
401
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
402
				i += 3;
403
			}
404

    
405
		}
406

    
407
		return string;
408
	}
409

    
410
};
411

    
412
	<?php if ($_GET['action'] == "load"): ?>
413
		events.push(function() {
414
			$("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
415
			loadFile();
416
		});
417
	<?php endif; ?>
418

    
419
//]]>
420
</script>
421

    
422
<?php include("foot.inc");
423

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