1
|
<?php
|
2
|
/*
|
3
|
edit.php
|
4
|
*/
|
5
|
/* ====================================================================
|
6
|
* Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
|
7
|
* Copyright (c) 2004, 2005 Scott Ullrich
|
8
|
*
|
9
|
* Redistribution and use in source and binary forms, with or without modification,
|
10
|
* are permitted provided that the following conditions are met:
|
11
|
*
|
12
|
* 1. Redistributions of source code must retain the above copyright notice,
|
13
|
* this list of conditions and the following disclaimer.
|
14
|
*
|
15
|
* 2. Redistributions in binary form must reproduce the above copyright
|
16
|
* notice, this list of conditions and the following disclaimer in
|
17
|
* the documentation and/or other materials provided with the
|
18
|
* distribution.
|
19
|
*
|
20
|
* 3. All advertising materials mentioning features or use of this software
|
21
|
* must display the following acknowledgment:
|
22
|
* "This product includes software developed by the pfSense Project
|
23
|
* for use in the pfSense software distribution. (http://www.pfsense.org/).
|
24
|
*
|
25
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
26
|
* endorse or promote products derived from this software without
|
27
|
* prior written permission. For written permission, please contact
|
28
|
* coreteam@pfsense.org.
|
29
|
*
|
30
|
* 5. Products derived from this software may not be called "pfSense"
|
31
|
* nor may "pfSense" appear in their names without prior written
|
32
|
* permission of the Electric Sheep Fencing, LLC.
|
33
|
*
|
34
|
* 6. Redistributions of any form whatsoever must retain the following
|
35
|
* acknowledgment:
|
36
|
*
|
37
|
* "This product includes software developed by the pfSense Project
|
38
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
39
|
*
|
40
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
41
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
42
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
43
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
44
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
45
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
46
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
47
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
48
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
49
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
50
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
51
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
52
|
*
|
53
|
* ====================================================================
|
54
|
*
|
55
|
*/
|
56
|
/*
|
57
|
pfSense_MODULE: shell
|
58
|
*/
|
59
|
|
60
|
##|+PRIV
|
61
|
##|*IDENT=page-diagnostics-edit
|
62
|
##|*NAME=Diagnostics: Edit FIle
|
63
|
##|*DESCR=Allow access to the 'Diagnostics: Edit File' page.
|
64
|
##|*MATCH=edit.php*
|
65
|
##|*MATCH=browser.php*
|
66
|
##|*MATCH=filebrowser/browser.php*
|
67
|
##|-PRIV
|
68
|
|
69
|
$pgtitle = array(gettext("Diagnostics"), gettext("Edit file"));
|
70
|
require("guiconfig.inc");
|
71
|
|
72
|
if ($_POST['action']) {
|
73
|
switch ($_POST['action']) {
|
74
|
case 'load':
|
75
|
if (strlen($_POST['file']) < 1) {
|
76
|
print('|5|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
|
77
|
} elseif (is_dir($_POST['file'])) {
|
78
|
print('|4|' . '<div class="alert alert-danger" role="alert">' . gettext("Loading a directory is not supported") .'</div>' . '|');
|
79
|
} elseif (!is_file($_POST['file'])) {
|
80
|
print('|3|' . '<div class="alert alert-danger" role="alert">' . gettext("File does not exist or is not a regular file") . '</div>' . '|');
|
81
|
} else {
|
82
|
$data = file_get_contents(urldecode($_POST['file']));
|
83
|
if ($data === false) {
|
84
|
print('|1|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to read file") . '</div>' . '|');
|
85
|
} else {
|
86
|
$data = base64_encode($data);
|
87
|
print("|0|{$_POST['file']}|{$data}|");
|
88
|
}
|
89
|
}
|
90
|
exit;
|
91
|
|
92
|
case 'save':
|
93
|
if (strlen($_POST['file']) < 1) {
|
94
|
print('|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
|
95
|
} else {
|
96
|
conf_mount_rw();
|
97
|
$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
|
98
|
$ret = file_put_contents($_POST['file'], $_POST['data']);
|
99
|
conf_mount_ro();
|
100
|
if ($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
|
101
|
if (file_exists("/tmp/config.cache")) {
|
102
|
unlink("/tmp/config.cache");
|
103
|
}
|
104
|
disable_security_checks();
|
105
|
}
|
106
|
if ($ret === false) {
|
107
|
print('|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to write file") . '</div>' . '|');
|
108
|
} elseif ($ret != strlen($_POST['data'])) {
|
109
|
print('|' . '<div class="alert alert-danger" role="alert">' . gettext("Error while writing file") . '</div>' . '|');
|
110
|
} else {
|
111
|
print('|' . '<div class="alert alert-success" role="alert">' . gettext("File saved successfully") . '</div>' . '|');
|
112
|
}
|
113
|
}
|
114
|
exit;
|
115
|
}
|
116
|
exit;
|
117
|
}
|
118
|
|
119
|
require("head.inc");
|
120
|
?>
|
121
|
<!-- file status box -->
|
122
|
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
|
123
|
<strong id="fileStatus"></strong>
|
124
|
</div>
|
125
|
|
126
|
<div class="panel panel-default">
|
127
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext("Save / Load a file from the filesystem")?></h2></div>
|
128
|
<div class="panel-body">
|
129
|
<form>
|
130
|
<input type="text" class="form-control" id="fbTarget"/>
|
131
|
<input type="button" class="btn btn-default btn-sm" onclick="loadFile();" value="<?=gettext('Load')?>" />
|
132
|
<input type="button" class="btn btn-default btn-sm" id="fbOpen" value="<?=gettext('Browse')?>" />
|
133
|
<input type="button" class="btn btn-default btn-sm" onclick="saveFile();" value="<?=gettext('Save')?>" />
|
134
|
</form>
|
135
|
|
136
|
<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
|
137
|
|
138
|
<div style="background:#eeeeee;" id="fileOutput">
|
139
|
<script type="text/javascript">
|
140
|
//<![CDATA[
|
141
|
window.onload=function(){
|
142
|
document.getElementById("fileContent").wrap='off';
|
143
|
}
|
144
|
//]]>
|
145
|
</script>
|
146
|
<textarea id="fileContent" name="fileContent" class="form-control" rows="30" cols=""></textarea>
|
147
|
</div>
|
148
|
|
149
|
</div>
|
150
|
</div>
|
151
|
|
152
|
<script>
|
153
|
function loadFile() {
|
154
|
jQuery("#fileStatus").html("");
|
155
|
jQuery("#fileStatusBox").show(500);
|
156
|
jQuery.ajax(
|
157
|
"<?=$_SERVER['SCRIPT_NAME']?>", {
|
158
|
type: "post",
|
159
|
data: "action=load&file=" + jQuery("#fbTarget").val(),
|
160
|
complete: loadComplete
|
161
|
}
|
162
|
);
|
163
|
}
|
164
|
|
165
|
function loadComplete(req) {
|
166
|
jQuery("#fileContent").show(1000);
|
167
|
var values = req.responseText.split("|");
|
168
|
values.shift(); values.pop();
|
169
|
|
170
|
if (values.shift() == "0") {
|
171
|
var file = values.shift();
|
172
|
var fileContent = window.atob(values.join("|"));
|
173
|
|
174
|
jQuery("#fileContent").val(fileContent);
|
175
|
}
|
176
|
else {
|
177
|
jQuery("#fileStatus").html(values[0]);
|
178
|
jQuery("#fileContent").val("");
|
179
|
}
|
180
|
|
181
|
jQuery("#fileContent").show(1000);
|
182
|
}
|
183
|
|
184
|
function saveFile(file) {
|
185
|
jQuery("#fileStatus").html("");
|
186
|
jQuery("#fileStatusBox").show(500);
|
187
|
|
188
|
var fileContent = Base64.encode(jQuery("#fileContent").val());
|
189
|
fileContent = fileContent.replace(/\+/g, "%2B");
|
190
|
|
191
|
jQuery.ajax(
|
192
|
"<?=$_SERVER['SCRIPT_NAME']?>", {
|
193
|
type: "post",
|
194
|
data: "action=save&file=" + jQuery("#fbTarget").val() +
|
195
|
"&data=" + fileContent,
|
196
|
complete: function(req) {
|
197
|
var values = req.responseText.split("|");
|
198
|
jQuery("#fileStatus").html(values[1]);
|
199
|
}
|
200
|
}
|
201
|
);
|
202
|
}
|
203
|
|
204
|
<?php if ($_GET['action'] == "load"): ?>
|
205
|
events.push(function() {
|
206
|
jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
|
207
|
loadFile();
|
208
|
});
|
209
|
<?php endif; ?>
|
210
|
</script>
|
211
|
|
212
|
<?php include("foot.inc");
|
213
|
|
214
|
outputJavaScriptFileInline("filebrowser/browser.js");
|