Project

General

Profile

« Previous | Next » 

Revision 067109d9

Added by Steve Beaver almost 8 years ago

Re-wrote ipsec widgt to return JSON formatted AJAX data

View differences:

src/usr/local/www/widgets/widgets/ipsec.widget.php
154 154
	}
155 155

  
156 156
	// Only generate the data for the tab that is currently being viewed
157
	switch ($_REQUEST['tab']) {
158
		case "Overview" :
159
			print("	<tr>\n");
160
			print(		"<td>" . $activecounter . "</td>\n");
161
			print(		"<td>" . $inactivecounter . "</td>\n");
162
			print(		"<td>" . (is_array($mobile['pool']) ? htmlspecialchars($mobile['pool'][0]['usage']) : '0') . "</td>\n");
163
			print(	"</tr>\n");
164
		break;
165

  
166
		case "tunnel" :
167
			foreach ($ipsec_detail_array as $ipsec) {
168
				print("	<tr>\n");
169
				print(		"<td>" . htmlspecialchars($ipsec['src']) . "</td>\n");
170
				print(		"<td>" . $ipsec['remote-subnet'] . "<br />(" . htmlspecialchars($ipsec['dest']) . ")</td>\n");
171
				print(		"<td>" . htmlspecialchars($ipsec['descr']) . "</td>\n");
172

  
173
				if ($ipsec['status'] == "true") {
174
					print('<td><i class="fa fa-arrow-up text-success"></i></td>' . "\n");
175
				} else {
176
					print('<td><i class="fa fa-arrow-down text-danger"></i></td>' . "\n");
177
				}
157
	$jsondata = "{";
158

  
159
	$jsondata .= "\"overview\":\"";
160
	$jsondata .= "<tr>";
161
	$jsondata .= "<td>" . $activecounter . "</td>";
162
	$jsondata .= "<td>" . $inactivecounter . "</td>";
163
	$jsondata .= "<td>" . (is_array($mobile['pool']) ? htmlspecialchars($mobile['pool'][0]['usage']) : '0') . "</td>";
164
	$jsondata .= "</tr>";
165
	$jsondata .= "\",\n";
166

  
167
	$jsondata .= "\"tunnel\":\"";
168
	foreach ($ipsec_detail_array as $ipsec) {
169
		$jsondata .= "<tr>";
170
		$jsondata .= "<td>" . htmlspecialchars($ipsec['src']) . "</td>";
171
		$jsondata .= "<td>" . $ipsec['remote-subnet'] . "<br />(" . htmlspecialchars($ipsec['dest']) . ")</td>";
172
		$jsondata .= "<td>" . htmlspecialchars($ipsec['descr']) . "</td>";
173

  
174
		if ($ipsec['status'] == "true") {
175
			$jsondata .= '<td><i class=\"fa fa-arrow-up text-success\"></i></td>';
176
		} else {
177
			$jsondata .= '<td><i class=\"fa fa-arrow-down text-danger\"></i></td>';
178
		}
179

  
180
		$jsondata .= "</tr>";
181
	}
182

  
183
	$jsondata .= "\",\n";
178 184

  
179
				print(	"</tr>\n");
180
			}
181
		break;
182 185

  
183
		case "mobile" :
184
			if (!is_array($mobile['pool'])) {
185
				break;
186
$jsondata .= "\"mobile\":\"";
187
	if (is_array($mobile['pool'])) {
188
		foreach ($mobile['pool'] as $pool) {
189
			if (!is_array($pool['lease'])) {
190
				continue;
186 191
			}
187
			foreach ($mobile['pool'] as $pool) {
188
				if (!is_array($pool['lease'])) {
189
					continue;
190
				}
191 192

  
192
				foreach ($pool['lease'] as $muser) {
193
					print("	<tr>\n");
194
					print(		"<td>" . htmlspecialchars($muser['id']) . "</td>\n");
195
					print(		"<td>" . htmlspecialchars($muser['host']) . "</td>\n");
196
					print(		"<td>" . htmlspecialchars($muser['status']) . "</td>\n");
197
					print("	</tr>\n");
198
				}
193
			foreach ($pool['lease'] as $muser) {
194
				$jsondata .= "<tr>";
195
				$jsondata .= "<td>" . htmlspecialchars($muser['id']) . "</td>";
196
				$jsondata .= "<td>" . htmlspecialchars($muser['host']) . "</td>";
197
				$jsondata .= "<td>" . htmlspecialchars($muser['status']) . "</td>";
198
				$jsondata .= "</tr>";
199 199
			}
200
		break;
200
		}
201
	} else {
202
		$jsondata .= "\"}";
201 203
	}
202 204

  
205
	print($jsondata);
203 206
	exit;
204 207
}
205 208

  
206 209
if (isset($config['ipsec']['phase1'])) {
207 210
	$tab_array = array();
208
	$tab_array[] = array(gettext("Overview"), true, "ipsec-Overview");
209
	$tab_array[] = array(gettext("Tunnels"), false, "ipsec-tunnel");
210
	$tab_array[] = array(gettext("Mobile"), false, "ipsec-mobile");
211
	$tab_array[] = array(gettext("Overview"), true, "ipsec-Overviews");
212
	$tab_array[] = array(gettext("Tunnels"), false, "ipsec-Tunnels");
213
	$tab_array[] = array(gettext("Mobile"), false, "ipsec-Mobiles");
211 214

  
212 215
	display_widget_tabs($tab_array);
213 216
}
......
216 219
$widgetperiod = isset($config['widgets']['period']) ? $config['widgets']['period'] * 1000 : 10000;
217 220

  
218 221
if (isset($config['ipsec']['phase2'])): ?>
219
<div id="ipsec-Overview" style="display:block;"  class="table-responsive">
222
<div id="ipsec-Overviews" style="display:block;"  class="table-responsive">
220 223
	<table class="table table-striped table-hover">
221 224
		<thead>
222 225
		<tr>
......
225 228
			<th><?=gettext("Mobile Users")?></th>
226 229
		</tr>
227 230
		</thead>
228
		<tbody>
231
		<tbody id="body-overview">
229 232
			<tr><td colspan="3"><?=gettext("Retrieving overview data ")?><i class="fa fa-cog fa-spin"></i></td></tr>
230 233
		</tbody>
231 234
	</table>
232 235
</div>
233
<div class="table-responsive" id="ipsec-tunnel" style="display:none;">
236
<div class="table-responsive" id="ipsec-Tunnels" style="display:none;">
234 237
	<table class="table table-striped table-hover">
235 238
	<thead>
236 239
	<tr>
......
240 243
		<th><?=gettext("Status")?></th>
241 244
	</tr>
242 245
	</thead>
243
	<tbody>
246
	<tbody id="body-tunnel">
244 247
		<tr><td colspan="4"><?=gettext("Retrieving tunnel data ")?><i class="fa fa-cog fa-spin"></i></td></tr>
245 248
	</tbody>
246 249
	</table>
247 250
</div>
248 251

  
249 252
	<?php if (is_array($mobile['pool'])): ?>
250
<div id="ipsec-mobile" style="display:none;" class="table-responsive">
253
<div id="ipsec-Mobiles" style="display:none;" class="table-responsive">
251 254
		<table class="table table-striped table-hover">
252 255
		<thead>
253 256
		<tr>
......
256 259
			<th><?=gettext("Status")?></th>
257 260
		</tr>
258 261
		</thead>
259
		<tbody>
262
		<tbody id="body-mobile">
260 263
			<tr><td colspan="3"><?=gettext("Retrieving mobile data ")?><i class="fa fa-cog fa-spin"></i></td></tr>
261 264
		</tbody>
262 265
		</table>
263 266
	</div>
267

  
268
	<span id="poo"></span>
264 269
	<?php endif;?>
265 270
<?php else: ?>
266 271
	<div>
......
321 326
	}
322 327
}
323 328

  
324
function get_ipsec_stats() {
325
	var ajaxRequest;
329
events.push(function(){
326 330

  
327
	ajaxRequest = $.ajax({
328
			url: "/widgets/widgets/ipsec.widget.php",
329
			type: "post",
330
			data: {
331
					ajax: "ajax",
332
					tab:  curtab
333
				  }
334
		});
331
	// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
335 332

  
336
	// Deal with the results of the above ajax call
337
	ajaxRequest.done(function (response, textStatus, jqXHR) {
333
	// Callback function called by refresh system when data is retrieved
334
	function ipsec_callback(s) {
335
		var obj = JSON.parse(s);
338 336

  
339
		$('tbody', '#ipsec-' + curtab).html(response);
337
		$('tbody', '#ipsec-Overviews').html(obj.overview);
338
		$('tbody', '#ipsec-Tunnels').html(obj.tunnel);
339
		$('tbody', '#body-Mobiles').html(obj.mobile);
340
	}
340 341

  
341
		// and do it again
342
		setTimeout(get_ipsec_stats, "<?=$widgetperiod?>");
343
	});
344
}
342
	// POST data to send via AJAX
343
	var postdata = {
344
		ajax: "ajax",
345
	 	tab : curtab
346
	 };
345 347

  
346
events.push(function(){
347
	// Start polling for updates some small random number of seconds from now (so that all the widgets don't
348
	// hit the server at exactly the same time)
349
	setTimeout(get_ipsec_stats, Math.floor((Math.random() * 10000) + 1000));
348
	// Create an object defining the widget refresh AJAX call
349
	var ipsecObject = new Object();
350
	ipsecObject.name = "IPsec";
351
	ipsecObject.url = "/widgets/widgets/ipsec.widget.php";
352
	ipsecObject.callback = ipsec_callback;
353
	ipsecObject.parms = postdata;
354
	ipsecObject.freq = 4;
355

  
356
	// Register the AJAX object
357
	register_ajax(ipsecObject);
358

  
359
	// ---------------------------------------------------------------------------------------------------
350 360
});
351 361
//]]>
352 362
</script>

Also available in: Unified diff