Project

General

Profile

Download (7.33 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	edit.php
4
	Copyright (C) 2004, 2005 Scott Ullrich
5
	All rights reserved.
6

    
7
	Redistribution and use in source and binary forms, with or without
8
	modification, are permitted provided that the following conditions are met:
9

    
10
	1. Redistributions of source code must retain the above copyright notice,
11
	   this list of conditions and the following disclaimer.
12

    
13
	2. Redistributions in binary form must reproduce the above copyright
14
	   notice, this list of conditions and the following disclaimer in the
15
	   documentation and/or other materials provided with the distribution.
16

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

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

    
39
if($_REQUEST['action'] === "load" || $_REQUEST['action'] === "save")
40
	$nocsrf = true;
41

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

    
45
if($_REQUEST['action']) {
46
	switch($_REQUEST['action']) {
47
		case 'load':
48
			if(strlen($_REQUEST['file']) < 1) {
49
				echo "|5|" . gettext("No file name specified") . ".|";
50
			} elseif(is_dir($_REQUEST['file'])) {
51
				echo "|4|" . gettext("Loading a directory is not supported") . ".|";
52
			} elseif(! is_file($_REQUEST['file'])) {
53
				echo "|3|" . gettext("File does not exist or is not a regular file") . ".|";
54
			} else {
55
				$data = file_get_contents(urldecode($_REQUEST['file']));
56
				if($data === false) {
57
					echo "|1|" . gettext("Failed to read file") . ".|";
58
				} else {
59
					echo "|0|{$_REQUEST['file']}|{$data}|";	
60
				}
61
			}
62
			exit;
63
		case 'save':
64
			if(strlen($_REQUEST['file']) < 1) {
65
				echo "|" . gettext("No file name specified") . ".|";
66
			} else {
67
				conf_mount_rw();
68
				$_REQUEST['data'] = str_replace("\r", "", base64_decode($_REQUEST['data']));
69
				$ret = file_put_contents($_REQUEST['file'], $_REQUEST['data']);
70
				conf_mount_ro();
71
				if($_REQUEST['file'] == "/conf/config.xml" || $_REQUEST['file'] == "/cf/conf/config.xml") {
72
					if(file_exists("/tmp/config.cache"))
73
						unlink("/tmp/config.cache");
74
					disable_security_checks();
75
				}
76
				if($ret === false) {
77
					echo "|" . gettext("Failed to write file") . ".|";
78
				} elseif($ret <> strlen($_REQUEST['data'])) {
79
					echo "|" . gettext("Error while writing file") . ".|";
80
				} else {
81
					echo "|" . gettext("File successfully saved") . ".|";
82
				}
83
			}
84
			exit;
85
	}
86
	exit;
87
}
88

    
89
require("head.inc");
90
outputCSSFileInline("code-syntax-highlighter/SyntaxHighlighter.css");
91
outputJavaScriptFileInline("filebrowser/browser.js");
92
outputJavaScriptFileInline("javascript/base64.js");
93

    
94
?>
95

    
96
<body link="#000000" vlink="#000000" alink="#000000">
97
<?php include("fbegin.inc"); ?>
98

    
99
<script type="text/javascript">	
100
	function loadFile() {
101
		jQuery("#fileStatus").html("<?=gettext("Loading file"); ?> ...");
102
		jQuery("#fileStatusBox").show(500);
103

    
104
		jQuery.ajax(
105
			"<?=$_SERVER['SCRIPT_NAME'];?>", {
106
				type: "post",
107
				data: "action=load&file=" + jQuery("#fbTarget").val(),
108
				complete: loadComplete
109
			}
110
		);
111
	}
112

    
113
	function loadComplete(req) {
114
		jQuery("#fileContent").show(1000);
115
		var values = req.responseText.split("|");
116
		values.shift(); values.pop();
117

    
118
		if(values.shift() == "0") {
119
			var file = values.shift();
120
			jQuery("#fileStatus").html("<?=gettext("File successfully loaded"); ?>.");
121
			jQuery("#fileContent").val(values.join("|"));
122

    
123
			var lang = "none";
124
				 if(file.indexOf(".php") > 0) lang = "php";
125
			else if(file.indexOf(".inc") > 0) lang = "php";
126
			else if(file.indexOf(".xml") > 0) lang = "xml";
127
			else if(file.indexOf(".js" ) > 0) lang = "js";
128
			else if(file.indexOf(".css") > 0) lang = "css";
129

    
130
			if(jQuery("#highlight").checked && lang != "none") {
131
				jQuery("fileContent").prop("className",lang + ":showcolumns");
132
				dp.SyntaxHighlighter.HighlightAll("fileContent", true, false);
133
			}
134
		}
135
		else {
136
			jQuery("#fileStatus").html(values[0]);
137
			jQuery("#fileContent").val("");
138
		}
139
		jQuery("#fileContent").show(1000);
140
	}
141

    
142
	function saveFile(file) {
143
		jQuery("#fileStatus").html("<?=gettext("Saving file"); ?> ...");
144
		jQuery("#fileStatusBox").show(500);
145
		
146
		var fileContent = Base64.encode(jQuery("#fileContent").val());
147
		fileContent = fileContent.replace(/\+/g,"%2B");
148
		
149
		jQuery.ajax(
150
			"<?=$_SERVER['SCRIPT_NAME'];?>", {
151
				type: "post",
152
				data: "action=save&file=" + jQuery("#fbTarget").val() +
153
							"&data=" + fileContent,
154
				complete: function(req) {
155
					var values = req.responseText.split("|");
156
					jQuery("#fileStatus").html(values[1]);
157
				}
158
			}
159
		);
160
	}
161
</script>
162

    
163
<!-- file status box -->
164
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
165
	<div class="vexpl" style="padding-left:15px;">
166
		<strong id="fileStatus"></strong>
167
	</div>
168
</div>
169

    
170
<br />
171

    
172
<table width="100%" border="0" cellpadding="0" cellspacing="0">
173
	<tr>
174
		<td class="tabcont" align="center">
175

    
176
<!-- controls -->
177
<table width="100%" cellpadding="9" cellspacing="9">
178
	<tr>
179
		<td align="center" class="list">
180
			<?=gettext("Save / Load from path"); ?>:
181
			<input type="text"   class="formfld file" id="fbTarget"         size="45" />
182
			<input type="button" class="formbtn"      onclick="loadFile();" value="<?=gettext('Load');?>" />
183
			<input type="button" class="formbtn"      id="fbOpen"           value="<?=gettext('Browse');?>" />
184
			<input type="button" class="formbtn"      onclick="saveFile();" value="<?=gettext('Save');?>" />
185
			<br />
186
			<?php
187
			/*
188
			<input type="checkbox" id="highlight" /><?=gettext("Enable syntax highlighting");
189
			*/
190
			?>
191
		</td>
192
	</tr>
193
</table>
194

    
195
<!-- filebrowser -->
196
<div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;"></div>
197

    
198
<!-- file viewer/editor -->
199
<table width="100%">
200
	<tr>
201
		<td valign="top" class="label">
202
			<div style="background:#eeeeee;" id="fileOutput">
203
				<textarea id="fileContent" name="fileContent" style="width:100%;" rows="30" wrap="off"></textarea>
204
			</div>
205
		</td>
206
	</tr>
207
</table>
208

    
209
		</td>
210
	</tr>
211
</table>
212

    
213
<script type="text/javascript" src="/code-syntax-highlighter/shCore.js"></script>
214
<script type="text/javascript" src="/code-syntax-highlighter/shBrushCss.js"></script>
215
<script type="text/javascript" src="/code-syntax-highlighter/shBrushJScript.js"></script>
216
<script type="text/javascript" src="/code-syntax-highlighter/shBrushPhp.js"></script>
217
<script type="text/javascript" src="/code-syntax-highlighter/shBrushXml.js"></script>
218
<script type="text/javascript">
219
	jQuery(window).load(
220
		function() {
221
			jQuery("#fbTarget").focus();
222

    
223
			NiftyCheck();
224
			Rounded("div#fileStatusBox", "all", "#ffffff", "#eeeeee", "smooth");
225
		}
226
	);
227

    
228
	<?php if($_GET['action'] == "load"): ?>
229
		jQuery(window).load(
230
			function() {
231
				jQuery("#fbTarget").val("<?=$_GET['path'];?>");
232
				loadFile();
233
			}
234
		);
235
	<?php endif; ?>
236
</script>
237

    
238
<?php include("fend.inc"); ?>
239
</body>
240
</html>
(50-50/245)