Project

General

Profile

Download (7.17 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 85e84623 sbeaver
			    print('|5|' . '<div class="alert alert-danger" role="alert">'.gettext("No file name specified").'</div>' . '|');
50 55344e2c Ermal
			} elseif(is_dir($_POST['file'])) {
51 85e84623 sbeaver
				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 85e84623 sbeaver
				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 85e84623 sbeaver
					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 85e84623 sbeaver
					print("|0|{$_POST['file']}|{$data}|");	
61 0d6a185a Scott Ullrich
				}
62
			}
63
			exit;
64 85e84623 sbeaver
			
65 0d6a185a Scott Ullrich
		case 'save':
66 55344e2c Ermal
			if(strlen($_POST['file']) < 1) {
67 85e84623 sbeaver
				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 85e84623 sbeaver
					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 85e84623 sbeaver
					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 74286b9e Colin Fleming
$closehead = false;
92 0d6a185a Scott Ullrich
require("head.inc");
93 85e84623 sbeaver
?>
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 0d6a185a Scott Ullrich
outputJavaScriptFileInline("filebrowser/browser.js");
129 c13b0b31 Ermal
outputJavaScriptFileInline("javascript/base64.js");
130 5b237745 Scott Ullrich
?>
131 85e84623 sbeaver
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 5b237745 Scott Ullrich
135 0d6a185a Scott Ullrich
<script type="text/javascript">	
136 74286b9e Colin Fleming
//<![CDATA[
137 0d6a185a Scott Ullrich
	function loadFile() {
138 85e84623 sbeaver
	    jQuery("#fileStatus").html("");
139 e3c1ea9b Vinicius Coque
		jQuery("#fileStatusBox").show(500);
140
		jQuery.ajax(
141 85e84623 sbeaver
			"<?=$_SERVER['SCRIPT_NAME']?>", {
142 e3c1ea9b Vinicius Coque
				type: "post",
143
				data: "action=load&file=" + jQuery("#fbTarget").val(),
144
				complete: loadComplete
145 0d6a185a Scott Ullrich
			}
146
		);
147
	}
148 5b237745 Scott Ullrich
149 0d6a185a Scott Ullrich
	function loadComplete(req) {
150 e3c1ea9b Vinicius Coque
		jQuery("#fileContent").show(1000);
151 0d6a185a Scott Ullrich
		var values = req.responseText.split("|");
152
		values.shift(); values.pop();
153
154
		if(values.shift() == "0") {
155
			var file = values.shift();
156 b71f0cbb Ermal
			var fileContent = Base64.decode(values.join("|"));
157 85e84623 sbeaver
			
158 b71f0cbb Ermal
			jQuery("#fileContent").val(fileContent);
159 0d6a185a Scott Ullrich
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 e3c1ea9b Vinicius Coque
			if(jQuery("#highlight").checked && lang != "none") {
168
				jQuery("fileContent").prop("className",lang + ":showcolumns");
169 0d6a185a Scott Ullrich
				dp.SyntaxHighlighter.HighlightAll("fileContent", true, false);
170
			}
171
		}
172
		else {
173 e3c1ea9b Vinicius Coque
			jQuery("#fileStatus").html(values[0]);
174
			jQuery("#fileContent").val("");
175 0d6a185a Scott Ullrich
		}
176 e3c1ea9b Vinicius Coque
		jQuery("#fileContent").show(1000);
177 0d6a185a Scott Ullrich
	}
178 5b237745 Scott Ullrich
179 0d6a185a Scott Ullrich
	function saveFile(file) {
180 85e84623 sbeaver
	    jQuery("#fileStatus").html("");
181
	    jQuery("#fileStatusBox").show(500);
182 e3c1ea9b Vinicius Coque
		var fileContent = Base64.encode(jQuery("#fileContent").val());
183 df61b7b4 mcrane
		fileContent = fileContent.replace(/\+/g,"%2B");
184
		
185 e3c1ea9b Vinicius Coque
		jQuery.ajax(
186 85e84623 sbeaver
			"<?=$_SERVER['SCRIPT_NAME']?>", {
187 e3c1ea9b Vinicius Coque
				type: "post",
188
				data: "action=save&file=" + jQuery("#fbTarget").val() +
189 ee650539 Scott Ullrich
							"&data=" + fileContent,
190 e3c1ea9b Vinicius Coque
				complete: function(req) {
191 0d6a185a Scott Ullrich
					var values = req.responseText.split("|");
192 e3c1ea9b Vinicius Coque
					jQuery("#fileStatus").html(values[1]);
193 0d6a185a Scott Ullrich
				}
194
			}
195
		);
196
	}
197 48581bb7 Scott Ullrich
198 e3c1ea9b Vinicius Coque
	jQuery(window).load(
199 0d6a185a Scott Ullrich
		function() {
200 e3c1ea9b Vinicius Coque
			jQuery("#fbTarget").focus();
201 0d6a185a Scott Ullrich
202
			NiftyCheck();
203
			Rounded("div#fileStatusBox", "all", "#ffffff", "#eeeeee", "smooth");
204
		}
205
	);
206
207
	<?php if($_GET['action'] == "load"): ?>
208 e3c1ea9b Vinicius Coque
		jQuery(window).load(
209 0d6a185a Scott Ullrich
			function() {
210 85e84623 sbeaver
				jQuery("#fbTarget").val("<?=htmlspecialchars($_GET['path'])?>");
211 0d6a185a Scott Ullrich
				loadFile();
212
			}
213
		);
214
	<?php endif; ?>
215 74286b9e Colin Fleming
//]]>
216 85e84623 sbeaver
</script>