Project

General

Profile

Download (6.11 KB) Statistics
| Branch: | Tag: | Revision:
1 447611c4 Scott Ullrich
<?php
2 a7f908db Scott Ullrich
/*
3 0d6a185a Scott Ullrich
	edit.php
4
	Copyright (C) 2004, 2005 Scott Ullrich
5 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6 0d6a185a Scott Ullrich
	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 a7f908db Scott Ullrich
*/
29 7ac5a4cb Scott Ullrich
/*
30
	pfSense_MODULE:	shell
31
*/
32 a7f908db Scott Ullrich
33 9fbb3599 Ermal
##|+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 c0f613e2 Ermal
##|*MATCH=browser.php*
39
##|*MATCH=filebrowser/browser.php*
40 9fbb3599 Ermal
##|-PRIV
41
42 fdb38c10 Scott Ullrich
$pgtitle = array(gettext("Diagnostics"), gettext("Edit file"));
43 859329c8 Scott Ullrich
require("guiconfig.inc");
44
45 55344e2c Ermal
if($_POST['action']) {
46
	switch($_POST['action']) {
47 0d6a185a Scott Ullrich
		case 'load':
48 55344e2c Ermal
			if(strlen($_POST['file']) < 1) {
49 45d6ada5 Sjon Hortensius
				print('|5|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
50 55344e2c Ermal
			} elseif(is_dir($_POST['file'])) {
51 45d6ada5 Sjon Hortensius
				print('|4|' . '<div class="alert alert-danger" role="alert">' . gettext("Loading a directory is not supported") .'</div>' . '|');
52 55344e2c Ermal
			} elseif(! is_file($_POST['file'])) {
53 45d6ada5 Sjon Hortensius
				print('|3|' . '<div class="alert alert-danger" role="alert">' . gettext("File does not exist or is not a regular file") . '</div>' . '|');
54 0d6a185a Scott Ullrich
			} else {
55 55344e2c Ermal
				$data = file_get_contents(urldecode($_POST['file']));
56 0d6a185a Scott Ullrich
				if($data === false) {
57 45d6ada5 Sjon Hortensius
					print('|1|' . '<div class="alert alert-danger" role="alert">' . gettext("Failed to read file") . '</div>' . '|');
58 0d6a185a Scott Ullrich
				} else {
59 b71f0cbb Ermal
					$data = base64_encode($data);
60 45d6ada5 Sjon Hortensius
					print("|0|{$_POST['file']}|{$data}|");
61 0d6a185a Scott Ullrich
				}
62
			}
63
			exit;
64 45d6ada5 Sjon Hortensius
65 0d6a185a Scott Ullrich
		case 'save':
66 55344e2c Ermal
			if(strlen($_POST['file']) < 1) {
67 45d6ada5 Sjon Hortensius
				print('|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
68 0d6a185a Scott Ullrich
			} else {
69 5a557f44 jim-p
				conf_mount_rw();
70 55344e2c Ermal
				$_POST['data'] = str_replace("\r", "", base64_decode($_POST['data']));
71
				$ret = file_put_contents($_POST['file'], $_POST['data']);
72 5a557f44 jim-p
				conf_mount_ro();
73 55344e2c Ermal
				if($_POST['file'] == "/conf/config.xml" || $_POST['file'] == "/cf/conf/config.xml") {
74 5f05c1e8 Scott Ullrich
					if(file_exists("/tmp/config.cache"))
75
						unlink("/tmp/config.cache");
76 0f806eca Erik Fonnesbeck
					disable_security_checks();
77
				}
78 0d6a185a Scott Ullrich
				if($ret === false) {
79 45d6ada5 Sjon Hortensius
					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 0d6a185a Scott Ullrich
				} else {
83 45d6ada5 Sjon Hortensius
					print('|' . '<div class="alert alert-success" role="alert">' . gettext("File saved successfully") . '</div>' . '|');
84 0d6a185a Scott Ullrich
				}
85
			}
86
			exit;
87 5124d619 Scott Ullrich
	}
88 0d6a185a Scott Ullrich
	exit;
89 5124d619 Scott Ullrich
}
90
91 0d6a185a Scott Ullrich
require("head.inc");
92 5b237745 Scott Ullrich
?>
93 45d6ada5 Sjon Hortensius
<!-- 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"><?=gettext("Save / Load a file from the filesystem")?></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 5b237745 Scott Ullrich
121 45d6ada5 Sjon Hortensius
	</div>
122
</div>
123
124
<script>
125 0d6a185a Scott Ullrich
	function loadFile() {
126 45d6ada5 Sjon Hortensius
		jQuery("#fileStatus").html("");
127 e3c1ea9b Vinicius Coque
		jQuery("#fileStatusBox").show(500);
128
		jQuery.ajax(
129 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
130 e3c1ea9b Vinicius Coque
				type: "post",
131
				data: "action=load&file=" + jQuery("#fbTarget").val(),
132
				complete: loadComplete
133 0d6a185a Scott Ullrich
			}
134
		);
135
	}
136 5b237745 Scott Ullrich
137 0d6a185a Scott Ullrich
	function loadComplete(req) {
138 e3c1ea9b Vinicius Coque
		jQuery("#fileContent").show(1000);
139 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
140
		values.shift(); values.pop();
141
142
		if(values.shift() == "0") {
143
			var file = values.shift();
144 45d6ada5 Sjon Hortensius
			var fileContent = window.atob(values.join("|"));
145 0d6a185a Scott Ullrich
146 45d6ada5 Sjon Hortensius
			jQuery("#fileContent").val(fileContent);
147 0d6a185a Scott Ullrich
		}
148
		else {
149 e3c1ea9b Vinicius Coque
			jQuery("#fileStatus").html(values[0]);
150
			jQuery("#fileContent").val("");
151 0d6a185a Scott Ullrich
		}
152 45d6ada5 Sjon Hortensius
153 e3c1ea9b Vinicius Coque
		jQuery("#fileContent").show(1000);
154 0d6a185a Scott Ullrich
	}
155 5b237745 Scott Ullrich
156 0d6a185a Scott Ullrich
	function saveFile(file) {
157 45d6ada5 Sjon Hortensius
		jQuery("#fileStatus").html("");
158 e3c1ea9b Vinicius Coque
		jQuery("#fileStatusBox").show(500);
159
		var fileContent = Base64.encode(jQuery("#fileContent").val());
160 df61b7b4 mcrane
		fileContent = fileContent.replace(/\+/g,"%2B");
161 45d6ada5 Sjon Hortensius
162 e3c1ea9b Vinicius Coque
		jQuery.ajax(
163 45d6ada5 Sjon Hortensius
			"<?=$_SERVER['SCRIPT_NAME']?>", {
164 e3c1ea9b Vinicius Coque
				type: "post",
165
				data: "action=save&file=" + jQuery("#fbTarget").val() +
166 ee650539 Scott Ullrich
							"&data=" + fileContent,
167 e3c1ea9b Vinicius Coque
				complete: function(req) {
168 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
169 e3c1ea9b Vinicius Coque
					jQuery("#fileStatus").html(values[1]);
170 0d6a185a Scott Ullrich
				}
171
			}
172
		);
173
	}
174
175
	<?php if($_GET['action'] == "load"): ?>
176 45d6ada5 Sjon Hortensius
		events.push(function() {
177
			jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
178
			loadFile();
179
		});
180 0d6a185a Scott Ullrich
	<?php endif; ?>
181
</script>
182 ab541dbb Scott Ullrich
183 45d6ada5 Sjon Hortensius
<?php include("foot.inc");
184
185
outputJavaScriptFileInline("filebrowser/browser.js");