Project

General

Profile

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

    
25
##|+PRIV
26
##|*IDENT=page-getserviceproviders
27
##|*NAME=AJAX: Get Service Providers
28
##|*DESCR=Allow access to the 'AJAX: Service Providers' page.
29
##|*MATCH=getserviceproviders.php*
30
##|-PRIV
31
require_once("guiconfig.inc");
32
require_once("pfsense-utils.inc");
33

    
34
$serviceproviders_xml = "/usr/local/share/mobile-broadband-provider-info/serviceproviders.xml";
35
$serviceproviders_contents = file_get_contents($serviceproviders_xml);
36
$serviceproviders_attr = xml2array($serviceproviders_contents, 1, "attr");
37

    
38
$serviceproviders = &$serviceproviders_attr['serviceproviders']['country'];
39

    
40
function get_country_providers($country) {
41
	global $serviceproviders;
42
	foreach ($serviceproviders as $sp) {
43
		if ($sp['attr']['code'] == strtolower($country)) {
44
			return is_array($sp['provider'][0]) ? $sp['provider'] : array($sp['provider']);
45
		}
46
	}
47
	$provider_list = (is_array($provider_list)) ? $provider_list : array();
48
	return $provider_list;
49
}
50

    
51
function country_list() {
52
	global $serviceproviders;
53
	$country_list = get_country_name();
54
	foreach ($serviceproviders as $sp) {
55
		foreach ($country_list as $country) {
56
			if (strtoupper($sp['attr']['code']) == $country['code']) {
57
				echo $country['name'] . ":" . $country['code'] . "\n";
58
			}
59
		}
60
	}
61
}
62

    
63
function providers_list($country) {
64
	$serviceproviders = get_country_providers($country);
65
	if (is_array($serviceproviders)) {
66
		foreach ($serviceproviders as $sp) {
67
			echo $sp['name']['value'] . "\n";
68
		}
69
	} else {
70
		$serviceproviders = array();
71
	}
72
}
73

    
74
function provider_plan_data($country, $provider, $connection) {
75
	header("Content-type: application/xml;");
76
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
77
	echo "<connection>\n";
78
	$serviceproviders = get_country_providers($country);
79
	foreach ($serviceproviders as $sp) {
80
		if (strtolower($sp['name']['value']) == strtolower($provider)) {
81
			if (strtoupper($connection) == "CDMA") {
82
				$conndata = $sp['cdma'];
83
			} else {
84
				if (!is_array($sp['gsm']['apn'][0])) {
85
					$conndata = $sp['gsm']['apn'];
86
				} else {
87
					foreach ($sp['gsm']['apn'] as $apn) {
88
						if ($apn['attr']['value'] == $connection) {
89
							$conndata = $apn;
90
							break;
91
						}
92
					}
93
				}
94
			}
95
			if (is_array($conndata)) {
96
				echo "<apn>" . $connection . "</apn>\n";
97
				echo "<username>" . $conndata['username']['value'] . "</username>\n";
98
				echo "<password>" . $conndata['password']['value'] . "</password>\n";
99

    
100
				$dns_arr = is_array($conndata['dns'][0]) ? $conndata['dns'] : array($conndata['dns']);
101
				foreach ($dns_arr as $dns) {
102
					echo '<dns>' . $dns['value'] . "</dns>\n";
103
				}
104
			}
105
			break;
106
		}
107
	}
108
	echo "</connection>";
109
}
110

    
111
function provider_plans_list($country, $provider) {
112
	$serviceproviders = get_country_providers($country);
113
	foreach ($serviceproviders as $sp) {
114
		if (strtolower($sp['name']['value']) == strtolower($provider)) {
115
			if (array_key_exists('gsm', $sp)) {
116
				if (array_key_exists('attr', $sp['gsm']['apn'])) {
117
					$name = ($sp['gsm']['apn']['name'] ? $sp['gsm']['apn']['name'] : $sp['name']['value']);
118
					echo $name . ":" . $sp['gsm']['apn']['attr']['value'];
119
				} else {
120
					foreach ($sp['gsm']['apn'] as $apn_info) {
121
						$name = ($apn_info['name']['value'] ? $apn_info['name']['value'] : $apn_info['gsm']['apn']['name']);
122
						echo $name . ":" . $apn_info['attr']['value'] . "\n";
123
					}
124
				}
125
			}
126
			if (array_key_exists('cdma', $sp)) {
127
				$name = $sp['cdma']['name']['value'] ? $sp['cdma']['name']['value']:$sp['name']['value'];
128
				echo $name . ":" . "CDMA";
129
			}
130
		}
131
	}
132
}
133

    
134
$_REQ_OR_POST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? $_POST : $_REQUEST;
135

    
136
if (isset($_REQ_OR_POST['country']) && !isset($_REQ_OR_POST['provider'])) {
137
	providers_list($_REQ_OR_POST['country']);
138
} elseif (isset($_REQ_OR_POST['country']) && isset($_REQ_OR_POST['provider'])) {
139
	if (isset($_REQ_OR_POST['plan'])) {
140
		provider_plan_data($_REQ_OR_POST['country'], $_REQ_OR_POST['provider'], $_REQ_OR_POST['plan']);
141
	} else {
142
		provider_plans_list($_REQ_OR_POST['country'], $_REQ_OR_POST['provider']);
143
	}
144
} else {
145
	country_list();
146
}
147
?>
(62-62/228)