Project

General

Profile

Download (6.14 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	edit.php
4
	Copyright (C) 2004, 2005 Scott Ullrich
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, 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 the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28
*/
29
/*
30
	pfSense_MODULE:	shell
31
*/
32

    
33
##|+PRIV
34
##|*IDENT=page-diagnostics-edit
35
##|*NAME=Diagnostics: Edit FIle
36
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
37
##|*MATCH=edit.php*
38
##|*MATCH=browser.php*
39
##|*MATCH=filebrowser/browser.php*
40
##|-PRIV
41

    
42
$pgtitle = array(gettext("Diagnostics"), gettext("Edit file"));
43
require("guiconfig.inc");
44

    
45
if($_POST['action']) {
46
	switch($_POST['action']) {
47
		case 'load':
48
			if(strlen($_POST['file']) < 1) {
49
				print('|5|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
50
			} elseif(is_dir($_POST['file'])) {
51
				print('|4|' . '<div class="alert alert-danger" role="alert">' . gettext("Loading a directory is not supported") .'</div>' . '|');
52
			} elseif(! is_file($_POST['file'])) {
53
				print('|3|' . '<div class="alert alert-danger" role="alert">' . gettext("File does not exist or is not a regular file") . '</div>' . '|');
54
			} else {
55
				$data = file_get_contents(urldecode($_POST['file']));
56
				if($data === false) {
57
					print('|1|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to read file") . '</div>' . '|');
58
				} else {
59
					$data = base64_encode($data);
60
					print("|0|{$_POST['file']}|{$data}|");
61
				}
62
			}
63
			exit;
64

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

    
91
require("head.inc");
92
?>
93
<!-- file status box -->
94
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
95
		<strong id="fileStatus"></strong>
96
</div>
97

    
98
<div class="panel panel-default">
99
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a file from the filesystem")?></h2></div>
100
	<div class="panel-body">
101
		<form>
102
			<input type="text" class="form-control" id="fbTarget"/>
103
			<input type="button" class="btn btn-default btn-sm"	  onclick="loadFile();" value="<?=gettext('Load')?>" />
104
			<input type="button" class="btn btn-default btn-sm"	  id="fbOpen"		   value="<?=gettext('Browse')?>" />
105
			<input type="button" class="btn btn-default btn-sm"	  onclick="saveFile();" value="<?=gettext('Save')?>" />
106
		</form>
107

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

    
110
		<div style="background:#eeeeee;" id="fileOutput">
111
			<script type="text/javascript">
112
			//<![CDATA[
113
			window.onload=function(){
114
				document.getElementById("fileContent").wrap='off';
115
			}
116
			//]]>
117
			</script>
118
			<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols=""></textarea>
119
		</div>
120

    
121
	</div>
122
</div>
123

    
124
<script>
125
	function loadFile() {
126
		jQuery("#fileStatus").html("");
127
		jQuery("#fileStatusBox").show(500);
128
		jQuery.ajax(
129
			"<?=$_SERVER['SCRIPT_NAME']?>", {
130
				type: "post",
131
				data: "action=load&file=" + jQuery("#fbTarget").val(),
132
				complete: loadComplete
133
			}
134
		);
135
	}
136

    
137
	function loadComplete(req) {
138
		jQuery("#fileContent").show(1000);
139
		var values = req.responseText.split("|");
140
		values.shift(); values.pop();
141

    
142
		if(values.shift() == "0") {
143
			var file = values.shift();
144
			var fileContent = window.atob(values.join("|"));
145

    
146
			jQuery("#fileContent").val(fileContent);
147
		}
148
		else {
149
			jQuery("#fileStatus").html(values[0]);
150
			jQuery("#fileContent").val("");
151
		}
152

    
153
		jQuery("#fileContent").show(1000);
154
	}
155

    
156
	function saveFile(file) {
157
		jQuery("#fileStatus").html("");
158
		jQuery("#fileStatusBox").show(500);
159
		var fileContent = Base64.encode(jQuery("#fileContent").val());
160
		fileContent = fileContent.replace(/\+/g,"%2B");
161

    
162
		jQuery.ajax(
163
			"<?=$_SERVER['SCRIPT_NAME']?>", {
164
				type: "post",
165
				data: "action=save&file=" + jQuery("#fbTarget").val() +
166
							"&data=" + fileContent,
167
				complete: function(req) {
168
					var values = req.responseText.split("|");
169
					jQuery("#fileStatus").html(values[1]);
170
				}
171
			}
172
		);
173
	}
174

    
175
	<?php if($_GET['action'] == "load"): ?>
176
		events.push(function() {
177
			jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
178
			loadFile();
179
		});
180
	<?php endif; ?>
181
</script>
182

    
183
<?php include("foot.inc");
184

    
185
outputJavaScriptFileInline("filebrowser/browser.js");
(44-44/241)