Project

General

Profile

« Previous | Next » 

Revision 6c9ca678

Added by Scott Ullrich almost 15 years ago

Only make one XMLRPC call

View differences:

etc/inc/voucher.inc
33 33

  
34 34
/* include all configuration functions */
35 35

  
36
function xmlrpc_sync_rolls() {
36
function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password, $username) {
37 37
	global $g, $config;
38
	if($config['voucher']['vouchersyncpass'] && $config['voucher']['vouchersyncusername'] && 
39
	   $config['voucher']['vouchersyncport'] && $config['voucher']['vouchersyncdbip']) {
40
		// Synchronize the voucher DB from the master node
41
		require_once("xmlrpc.inc");
42
		if($config['voucher']['vouchersyncport'] == "443") 
43
			$url = "https://{$config['voucher']['vouchersyncdbip']}:{$config['voucher']['vouchersyncport']}";
44
		else 
45
			$url = "http://{$config['voucher']['vouchersyncdbip']}:{$config['voucher']['vouchersyncport']}";
46
		$execcmd  = <<<EOF
47
		\$toreturn['voucher']['roll'] = \$config['voucher']['roll'];
48
		
38
	require_once("xmlrpc.inc");
39
	if($port == "443") 
40
		$url = "https://{$syncip}:{$port}";
41
	else 
42
		$url = "http://{$syncip}:{$port}";
43

  
44
	/* Construct code that is run on remote machine */
45
	$method = 'pfsense.exec_php';
46
	$execcmd  = <<<EOF
47
	require_once('/etc/inc/voucher.inc');
48
	\$timeleft = voucher_auth($voucher_received);
49
	\$toreturn = array();
50
	\$toreturn['timeleft'] = \$timeleft;
51
	\$toreturn['voucher']['roll'] = \$config['voucher']['roll'];
52

  
49 53
EOF;
50
		/* assemble xmlrpc payload */
51
		$params = array(
52
			XML_RPC_encode($config['voucher']['vouchersyncpass']),
53
			XML_RPC_encode($execcmd)
54
		);
55
		$msg = new XML_RPC_Message('pfsense.exec_php', $params);
56
		$cli = new XML_RPC_Client('/xmlrpc.php', $url, $config['voucher']['vouchersyncport']);
57
		$cli->setCredentials($config['voucher']['vouchersyncusername'], $config['voucher']['vouchersyncpass']);
54

  
55
	/* assemble xmlrpc payload */
56
	$params = array(
57
		XML_RPC_encode($password),
58
		XML_RPC_encode($execcmd)
59
	);
60

  
61
	log_error("Captive Portal Voucher XMLRPC sync data {$url}:{$port}.");
62
	$msg = new XML_RPC_Message($method, $params);
63
	$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
64
	$cli->setCredentials('admin', $password);
65
	$resp = $cli->send($msg, "250");
66
	if(!$resp) {
67
		$error = "A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with {$url}:{$port} (pfsense.exec_php).";
68
		log_error($error);
69
		file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
70
		return array("timeleft" => "0");
71
	} elseif($resp->faultCode()) {
72
		$cli->setDebug(1);
58 73
		$resp = $cli->send($msg, "250");
59
		if(!$resp) {
60
			$error = "A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with {$url}:{$port} (pfsense.exec_php).";
61
			log_error($error);
62
			file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
63
			$input_errors[] = $error;
64
		} elseif($resp->faultCode()) {
65
			$cli->setDebug(1);
66
			$resp = $cli->send($msg, "250");
67
			$error = "An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
68
			log_error($error);
69
			file_notice("CaptivePortalVoucherSync", $error, "Error code received", "");
70
			$input_errors[] = $error;
71
		}
72
		$toreturn =  XML_RPC_Decode($resp->value());
73
		if(is_array($toreturn) && $toreturn['voucher'] && $config['voucher']['roll']) {
74
			// If we received back the voucher roll and the local roll count differs then sync
75
			if(count($toreturn['voucher']['roll']) <> count($config['voucher']['roll'])) {
76
				$config['voucher']['roll'] = $toreturn['voucher']['roll'];
77
				write_config("Captive Portal voucher database synchronized with {$url}");
78
				voucher_configure();
79
			}
80
		}
74
		$error = "An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
75
		log_error($error);
76
		file_notice("CaptivePortalVoucherSync", $error, "Error code received", "");
77
		return array("timeleft" => "0");
78
	} else {
79
		log_error("CaptivePortalVoucherSync XMLRPC reload data success with {$url}:{$port} (pfsense.exec_php).");
81 80
	}
81
	$toreturn =  XML_RPC_Decode($resp->value());
82
	if(count($toreturn['voucher']['roll']) <> count($config['voucher']['roll'])) {
83
		$config['voucher']['roll'] = $toreturn['voucher']['roll'];
84
		write_config("Captive Portal Voucher database synchronized with {$url}");
85
		voucher_configure();
86
	}
87
    return $toreturn['timeleft'];
82 88
}
83 89

  
84 90
/* 
......
95 101

  
96 102
    $voucherlck = lock('voucher');
97 103

  
98
	// If defined check to see if the roll count needs to be sycnrhonized
99
	xmlrpc_sync_rolls();
104
	// XMLRPC Call over to the master Voucher node
105
    $a_voucher = &$config['voucher'];
106
	if($a_voucher['vouchersyncdbip']) {
107
		$syncip   = $a_voucher['vouchersyncdbip'];
108
		$syncport = $a_voucher['vouchersyncport'];
109
		$syncpass = $a_voucher['vouchersyncpass'];
110
		$syncpass = $a_voucher['vouchersyncusername'];
111
		$remote_time_used = xmlrpc_sync_used_voucher($voucher_received, $syncip, $syncport, $syncpass, $vouchersyncusername);
112
	}
100 113

  
101 114
    // read rolls into assoc array with rollid as key and minutes as value
102 115
    $a_roll = &$config['voucher']['roll'];
......
193 206
        return $total_minutes;       // well, at least one voucher had errors. Say NO ACCESS
194 207
    }
195 208

  
196
	// XMLRPC Call over to the master Voucher node
197
    $a_voucher = &$config['voucher'];
198
	if($a_voucher['vouchersyncdbip']) {
199
		$syncip   = $a_voucher['vouchersyncdbip'];
200
		$syncport = $a_voucher['vouchersyncport'];
201
		$syncpass = $a_voucher['vouchersyncpass'];
202
		$syncpass = $a_voucher['vouchersyncusername'];
203
		$remote_time_used = sync_used_voucher($voucher_received, $syncip, $syncport, $syncpass, $vouchersyncusername);
209
	// If we did a XMLRPC sync earlier check the timeleft
210
	if($a_voucher['vouchersyncdbip']) 
204 211
		if($remote_time_used['timeleft'] < 1) 
205 212
			$total_minutes = $remote_time_used['timeleft'];
206
	}
207 213

  
208 214
    // All given vouchers were valid and this isn't simply a test.
209 215
    // Write back the used DB's
......
242 248
    return $total_minutes;
243 249
}
244 250

  
245
function sync_used_voucher($voucher_received, $syncip, $port, $password, $username) {
246
	require_once("xmlrpc.inc");
247
	if($port == "443") 
248
		$url = "https://{$syncip}:{$port}";
249
	else 
250
		$url = "http://{$syncip}:{$port}";
251

  
252
	/* Construct code that is run on remote machine */
253
	$method = 'pfsense.exec_php';
254
	$execcmd  = <<<EOF
255
	require_once('/etc/inc/voucher.inc');
256
	\$timeleft = voucher_auth($voucher_received);
257
	\$toreturn = array();
258
	\$toreturn['timeleft'] = \$timeleft;
259

  
260
EOF;
261

  
262
	/* assemble xmlrpc payload */
263
	$params = array(
264
		XML_RPC_encode($password),
265
		XML_RPC_encode($execcmd)
266
	);
267

  
268
	log_error("voucher XMLRPC sync data {$url}:{$port}.");
269
	$msg = new XML_RPC_Message($method, $params);
270
	$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
271
	$cli->setCredentials('admin', $password);
272
	$resp = $cli->send($msg, "250");
273
	if(!$resp) {
274
		$error = "A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with {$url}:{$port} (pfsense.exec_php).";
275
		log_error($error);
276
		file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
277
		return array("timeleft" => "0");
278
	} elseif($resp->faultCode()) {
279
		$cli->setDebug(1);
280
		$resp = $cli->send($msg, "250");
281
		$error = "An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
282
		log_error($error);
283
		file_notice("CaptivePortalVoucherSync", $error, "Error code received", "");
284
		return array("timeleft" => "0");
285
	} else {
286
		log_error("CaptivePortalVoucherSync XMLRPC reload data success with {$url}:{$port} (pfsense.exec_php).");
287
	}
288
	$timeleft =  XML_RPC_Decode($resp->value());
289
	//print_r($timeleft);
290
    return $timeleft;
291
}
292

  
293 251
function voucher_configure() {
294 252
    global $config, $g;
295 253
    

Also available in: Unified diff