Project

General

Profile

Download (5.31 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-2024 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 (array_get_path($sp, 'attr/code', '') == strtolower($country)) {
44
			return is_array(array_get_path($sp, 'provider/0')) ? array_get_path($sp, 'provider') : [ array_get_path($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 (!is_array($country) || empty($country)) {
57
				continue;
58
			}
59
			if (strtoupper(array_get_path($sp, 'attr/code')) == array_get_path($country, 'code')) {
60
				echo array_get_path($country, 'name', '') . ":" . array_get_path($country, 'code', '') . "\n";
61
			}
62
		}
63
	}
64
}
65

    
66
function providers_list($country) {
67
	$serviceproviders = get_country_providers($country);
68
	if (is_array($serviceproviders)) {
69
		foreach ($serviceproviders as $sp) {
70
			echo array_get_path($sp, 'name/value', '') . "\n";
71
		}
72
	} else {
73
		$serviceproviders = array();
74
	}
75
}
76

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

    
104
				$dns_arr = is_array(array_get_path($conndata, 'dns/0')) ? array_get_path($conndata, 'dns') : [ array_get_path($conndata, 'dns') ];
105
				foreach ($dns_arr as $dns) {
106
					if (is_array($dns) && !empty($dns)) {
107
						echo '<dns>' . array_get_path($dns, 'value') . "</dns>\n";
108
					}
109
				}
110
			}
111
			break;
112
		}
113
	}
114
	echo "</connection>";
115
}
116

    
117
function provider_plans_list($country, $provider) {
118
	$serviceproviders = get_country_providers($country);
119
	foreach ($serviceproviders as $sp) {
120
		if (strtolower(array_get_path($sp, 'name/value', '')) == strtolower($provider)) {
121
			if (array_key_exists('gsm', $sp)) {
122
				if (array_key_exists('attr', array_get_path($sp, 'gsm/apn', []))) {
123
					$name = array_get_path($sp, 'gsm/apn/name/value', array_get_path($sp, 'name/value', ''));
124
					echo $name . ":" . array_get_path($sp, 'gsm/apn/attr/value', '');
125
				} else {
126
					foreach (array_get_path($sp, 'gsm/apn', []) as $apn_info) {
127
						$name = array_get_path($apn_info, 'name/value', array_get_path($apn_info, 'gsm/apn/name', ''));
128
						echo $name . ":" . array_get_path($apn_info, 'attr/value', '') . "\n";
129
					}
130
				}
131
			}
132
			if (array_key_exists('cdma', $sp)) {
133
				$name = array_get_path($sp, 'cdma/name/value', array_get_path($sp, 'name/value', ''));
134
				echo $name . ":" . "CDMA";
135
			}
136
		}
137
	}
138
}
139

    
140
if (!empty($_POST)) {
141
	if (isset($_POST['country']) && !isset($_POST['provider'])) {
142
		providers_list($_POST['country']);
143
	} elseif (isset($_POST['country']) && isset($_POST['provider'])) {
144
		if (isset($_POST['plan'])) {
145
			provider_plan_data($_POST['country'], $_POST['provider'], $_POST['plan']);
146
		} else {
147
			provider_plans_list($_POST['country'], $_POST['provider']);
148
		}
149
	} else {
150
		country_list();
151
	}
152
}
153
?>
(64-64/230)