Project

General

Profile

Download (8.91 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_arp.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * Copyright (c) 2005 Colin Smith
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-diagnostics-configurationhistory
25
##|*NAME=Diagnostics: Configuration History
26
##|*DESCR=Allow access to the 'Diagnostics: Configuration History' page.
27
##|*MATCH=diag_confbak.php*
28
##|-PRIV
29

    
30
require_once("guiconfig.inc");
31

    
32
if (isset($_POST['backupcount'])) {
33
	if (is_numericint($_POST['backupcount'])) {
34
		$config['system']['backupcount'] = $_POST['backupcount'];
35
		$changedescr = $config['system']['backupcount'];
36
	} else {
37
		unset($config['system']['backupcount']);
38
		$changedescr = gettext("(platform default)");
39
	}
40
	write_config(sprintf(gettext("Changed backup revision count to %s"), $changedescr));
41
} elseif ($_GET) {
42
	if (!isset($_GET['newver']) && !isset($_GET['rmver']) && !isset($_GET['getcfg']) && !isset($_GET['diff'])) {
43
		header("Location: diag_confbak.php");
44
		return;
45
	}
46

    
47
	conf_mount_rw();
48
	$confvers = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
49

    
50
	if ($_GET['newver'] != "") {
51
		if (config_restore($g['conf_path'] . '/backup/config-' . $_GET['newver'] . '.xml') == 0) {
52
			$savemsg = sprintf(gettext('Successfully reverted to timestamp %1$s with description "%2$s".'), date(gettext("n/j/y H:i:s"), $_GET['newver']), htmlspecialchars($confvers[$_GET['newver']]['description']));
53
		} else {
54
			$savemsg = gettext("Unable to revert to the selected configuration.");
55
		}
56
	}
57
	if ($_GET['rmver'] != "") {
58
		unlink_if_exists($g['conf_path'] . '/backup/config-' . $_GET['rmver'] . '.xml');
59
		$savemsg = sprintf(gettext('Deleted backup with timestamp %1$s and description "%2$s".'), date(gettext("n/j/y H:i:s"), $_GET['rmver']), htmlspecialchars($confvers[$_GET['rmver']]['description']));
60
	}
61
	conf_mount_ro();
62
}
63

    
64
if ($_GET['getcfg'] != "") {
65
	$_GET['getcfg'] = basename($_GET['getcfg']);
66
	$file = $g['conf_path'] . '/backup/config-' . $_GET['getcfg'] . '.xml';
67

    
68
	$exp_name = urlencode("config-{$config['system']['hostname']}.{$config['system']['domain']}-{$_GET['getcfg']}.xml");
69
	$exp_data = file_get_contents($file);
70
	$exp_size = strlen($exp_data);
71

    
72
	header("Content-Type: application/octet-stream");
73
	header("Content-Disposition: attachment; filename={$exp_name}");
74
	header("Content-Length: $exp_size");
75
	echo $exp_data;
76
	exit;
77
}
78

    
79
if (($_GET['diff'] == 'Diff') && isset($_GET['oldtime']) && isset($_GET['newtime']) &&
80
    (is_numeric($_GET['oldtime'])) &&
81
    (is_numeric($_GET['newtime']) || ($_GET['newtime'] == 'current'))) {
82
	$diff = "";
83
	$oldfile = $g['conf_path'] . '/backup/config-' . $_GET['oldtime'] . '.xml';
84
	$oldtime = $_GET['oldtime'];
85
	if ($_GET['newtime'] == 'current') {
86
		$newfile = $g['conf_path'] . '/config.xml';
87
		$newtime = $config['revision']['time'];
88
	} else {
89
		$newfile = $g['conf_path'] . '/backup/config-' . $_GET['newtime'] . '.xml';
90
		$newtime = $_GET['newtime'];
91
	}
92
	if (file_exists($oldfile) && file_exists($newfile)) {
93
		exec("/usr/bin/diff -u " . escapeshellarg($oldfile) . " " . escapeshellarg($newfile), $diff);
94
	}
95
}
96

    
97
cleanup_backupcache(false);
98
$confvers = get_backups();
99
unset($confvers['versions']);
100

    
101
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), gettext("Config History"));
102
include("head.inc");
103

    
104
if ($savemsg) {
105
	print_info_box($savemsg, 'success');
106
}
107

    
108
$tab_array = array();
109
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), false, "diag_backup.php");
110
$tab_array[] = array(gettext("Config History"), true, "diag_confbak.php");
111
display_top_tabs($tab_array);
112

    
113
if ($diff) {
114
?>
115
<div class="panel panel-default">
116
	<div class="panel-heading">
117
		<h2 class="panel-title">
118
			<?=sprintf(gettext('Configuration Diff from %1$s to %2$s'), date(gettext("n/j/y H:i:s"), $oldtime), date(gettext("n/j/y H:i:s"), $newtime))?>
119
		</h2>
120
	</div>
121
	<div class="panel-body table-responsive">
122
	<!-- This table is left un-bootstrapped to maintain the original diff format output -->
123
		<table style="padding-top: 4px; padding-bottom: 4px; vertical-align:middle;">
124

    
125
<?php
126
	foreach ($diff as $line) {
127
		switch (substr($line, 0, 1)) {
128
			case "+":
129
				$color = "#caffd3";
130
				break;
131
			case "-":
132
				$color = "#ffe8e8";
133
				break;
134
			case "@":
135
				$color = "#a0a0a0";
136
				break;
137
			default:
138
				$color = "#ffffff";
139
		}
140
?>
141
			<tr>
142
				<td class="diff-text" style="vertical-align:middle; background-color:<?=$color;?>; white-space:pre-wrap;"><?=htmlentities($line)?></td>
143
			</tr>
144
<?php
145
	}
146
?>
147
		</table>
148
	</div>
149
</div>
150
<?php
151
}
152

    
153
$form = new Form(false);
154

    
155
$section = new Form_Section('Configuration Backup Cache Settings', 'configsettings', COLLAPSIBLE|SEC_CLOSED);
156

    
157
$section->addInput(new Form_Input(
158
	'backupcount',
159
	'Backup Count',
160
	'number',
161
	$config['system']['backupcount']
162
))->setHelp('Maximum number of old configurations to keep in the cache, 0 for no backups, or leave blank for the default value (' . $g['default_config_backup_count'] . ' for the current platform).');
163

    
164
$space = exec("/usr/bin/du -sh /conf/backup | /usr/bin/awk '{print $1;}'");
165

    
166
$section->addInput(new Form_StaticText(
167
	'Current space used by backups',
168
	$space
169
));
170

    
171
$section->addInput(new Form_Button(
172
	'Submit',
173
	gettext("Save"),
174
	null,
175
	'fa-save'
176
))->addClass('btn-primary');
177

    
178
$form->add($section);
179

    
180
print($form);
181

    
182
if (is_array($confvers)) {
183
?>
184
<div>
185
	<div class="infoblock blockopen">
186
		<?php print_info_box(
187
			gettext(
188
				'To view the differences between an older configuration and a newer configuration, ' .
189
				'select the older configuration using the left column of radio options and select the newer configuration in the right column, ' .
190
				'then press the "Diff" button.'),
191
			'info', false); ?>
192
	</div>
193
</div>
194
<?php
195
}
196
?>
197

    
198
<form action="diag_confbak.php" method="get">
199
	<div class="table-responsive">
200
		<table class="table table-striped table-hover table-condensed">
201
<?php
202
if (is_array($confvers)):
203
?>
204
			<thead>
205
				<tr>
206
					<th colspan="2">
207
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
208
							<i class="fa fa-exchange icon-embed-btn"></i>
209
							<?=gettext("Diff"); ?>
210
						</button>
211
					</th>
212
					<th><?=gettext("Date")?></th>
213
					<th><?=gettext("Version")?></th>
214
					<th><?=gettext("Size")?></th>
215
					<th><?=gettext("Configuration Change")?></th>
216
					<th><?=gettext("Actions")?></th>
217
				</tr>
218
			</thead>
219
			<tbody>
220
				<!-- First row is the current configuration -->
221
				<tr style="vertical-align:top;">
222
					<td></td>
223
					<td>
224
						<input type="radio" name="newtime" value="current" />
225
					</td>
226
					<td><?= date(gettext("n/j/y H:i:s"), $config['revision']['time']) ?></td>
227
					<td><?= $config['version'] ?></td>
228
					<td><?= format_bytes(filesize("/conf/config.xml")) ?></td>
229
					<td><?= htmlspecialchars($config['revision']['description']) ?></td>
230
					<td><?=gettext("Current configuration")?></td>
231
				</tr>
232
<?php
233
	// And now for the table of prior backups
234
	$c = 0;
235
	foreach ($confvers as $version):
236
		if ($version['time'] != 0) {
237
			$date = date(gettext("n/j/y H:i:s"), $version['time']);
238
		} else {
239
			$date = gettext("Unknown");
240
		}
241
?>
242
				<tr>
243
					<td>
244
						<input type="radio" name="oldtime" value="<?=$version['time']?>" />
245
					</td>
246
					<td>
247
<?php
248
		if ($c < (count($confvers) - 1)) {
249
?>
250
								<input type="radio" name="newtime" value="<?=$version['time']?>" />
251
<?php
252
		}
253
		$c++;
254
?>
255
					</td>
256
					<td><?= $date ?></td>
257
					<td><?= $version['version'] ?></td>
258
					<td><?= format_bytes($version['filesize']) ?></td>
259
					<td><?= htmlspecialchars($version['description']) ?></td>
260
					<td>
261
						<a class="fa fa-undo"		title="<?=gettext('Revert config')?>"	href="diag_confbak.php?newver=<?=$version['time']?>"	onclick="return confirm('<?=gettext("Confirmation Required to replace the current configuration with this backup.")?>')"></a>
262
						<a class="fa fa-download"	title="<?=gettext('Download config')?>"	href="diag_confbak.php?getcfg=<?=$version['time']?>"></a>
263
						<a class="fa fa-trash"		title="<?=gettext('Delete config')?>"	href="diag_confbak.php?rmver=<?=$version['time']?>"></a>
264
					</td>
265
				</tr>
266
<?php
267
	endforeach;
268
?>
269
				<tr>
270
					<td colspan="2">
271
						<button type="submit" name="diff" class="btn btn-info btn-xs" value="<?=gettext("Diff"); ?>">
272
							<i class="fa fa-exchange icon-embed-btn"></i>
273
							<?=gettext("Diff"); ?>
274
						</button>
275
					</td>
276
					<td colspan="5"></td>
277
				</tr>
278
<?php
279
else:
280
	print_info_box(gettext("No backups found."), 'danger');
281
endif;
282
?>
283
			</tbody>
284
		</table>
285
	</div>
286
</form>
287

    
288
<?php include("foot.inc");
(8-8/227)