Project

General

Profile

Download (7.17 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
$closehead = false;
92
require("head.inc");
93
?>
94

    
95
<!-- file status box -->
96
<div style="display:none; background:#eeeeee;" id="fileStatusBox">
97
		<strong id="fileStatus"></strong>
98
</div>
99

    
100
<div class="panel panel-info">
101
	<div class="panel-heading">
102
        <?=gettext("Save / Load from path"); ?>:
103
        <input type="text"   class="formfld file" id="fbTarget" size="45" />
104
        <input type="button" class="btn btn-default btn-sm"	  onclick="loadFile();" value="<?=gettext('Load')?>" />
105
        <input type="button" class="btn btn-default btn-sm"	  id="fbOpen"		   value="<?=gettext('Browse')?>" />
106
        <input type="button" class="btn btn-default btn-sm"	  onclick="saveFile();" value="<?=gettext('Save')?>" />
107
    </div>
108
    
109
	<div class="panel-body">
110
        <div id="fbBrowser" style="display:none; border:1px dashed gray; width:98%;">
111
        </div>
112
    
113
    	<div style="background:#eeeeee;" id="fileOutput">
114
    		<script type="text/javascript">
115
    		//<![CDATA[
116
    		window.onload=function(){
117
    			document.getElementById("fileContent").wrap='off';
118
    		}
119
    		//]]>
120
    		</script>
121
    		<textarea id="fileContent" name="fileContent" style="width:100%;" rows="30" cols=""></textarea>
122
    	</div>
123
    </div>
124
</div>
125

    
126
<?php include("foot.inc"); 
127

    
128
outputJavaScriptFileInline("filebrowser/browser.js");
129
outputJavaScriptFileInline("javascript/base64.js");
130
?>
131

    
132
<!-- Since the jQuery, bootstrap etc libraries are included from foot.inc, JavaScript functions that require jQuery need to move down here -->
133
<script type="text/javascript" src="/javascript/niftyjsCode.js"></script>
134

    
135
<script type="text/javascript">	
136
//<![CDATA[
137
	function loadFile() {
138
	    jQuery("#fileStatus").html("");
139
		jQuery("#fileStatusBox").show(500);
140
		jQuery.ajax(
141
			"<?=$_SERVER['SCRIPT_NAME']?>", {
142
				type: "post",
143
				data: "action=load&file=" + jQuery("#fbTarget").val(),
144
				complete: loadComplete
145
			}
146
		);
147
	}
148

    
149
	function loadComplete(req) {
150
		jQuery("#fileContent").show(1000);
151
		var values = req.responseText.split("|");
152
		values.shift(); values.pop();
153

    
154
		if(values.shift() == "0") {
155
			var file = values.shift();
156
			var fileContent = Base64.decode(values.join("|"));
157
			
158
			jQuery("#fileContent").val(fileContent);
159

    
160
			var lang = "none";
161
				 if(file.indexOf(".php") > 0) lang = "php";
162
			else if(file.indexOf(".inc") > 0) lang = "php";
163
			else if(file.indexOf(".xml") > 0) lang = "xml";
164
			else if(file.indexOf(".js" ) > 0) lang = "js";
165
			else if(file.indexOf(".css") > 0) lang = "css";
166

    
167
			if(jQuery("#highlight").checked && lang != "none") {
168
				jQuery("fileContent").prop("className",lang + ":showcolumns");
169
				dp.SyntaxHighlighter.HighlightAll("fileContent", true, false);
170
			}
171
		}
172
		else {
173
			jQuery("#fileStatus").html(values[0]);
174
			jQuery("#fileContent").val("");
175
		}
176
		jQuery("#fileContent").show(1000);
177
	}
178

    
179
	function saveFile(file) {
180
	    jQuery("#fileStatus").html("");
181
	    jQuery("#fileStatusBox").show(500);
182
		var fileContent = Base64.encode(jQuery("#fileContent").val());
183
		fileContent = fileContent.replace(/\+/g,"%2B");
184
		
185
		jQuery.ajax(
186
			"<?=$_SERVER['SCRIPT_NAME']?>", {
187
				type: "post",
188
				data: "action=save&file=" + jQuery("#fbTarget").val() +
189
							"&data=" + fileContent,
190
				complete: function(req) {
191
					var values = req.responseText.split("|");
192
					jQuery("#fileStatus").html(values[1]);
193
				}
194
			}
195
		);
196
	}
197

    
198
	jQuery(window).load(
199
		function() {
200
			jQuery("#fbTarget").focus();
201

    
202
			NiftyCheck();
203
			Rounded("div#fileStatusBox", "all", "#ffffff", "#eeeeee", "smooth");
204
		}
205
	);
206

    
207
	<?php if($_GET['action'] == "load"): ?>
208
		jQuery(window).load(
209
			function() {
210
				jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
211
				loadFile();
212
			}
213
		);
214
	<?php endif; ?>
215
//]]>
216
</script>
(55-55/252)