Project

General

Profile

Download (4.72 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * getserviceproviders.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2010 Vinicius Coque <vinicius.coque@bluepex.com>
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-getserviceproviders
25
##|*NAME=AJAX: Get Service Providers
26
##|*DESCR=Allow access to the 'AJAX: Service Providers' page.
27
##|*MATCH=getserviceproviders.php*
28
##|-PRIV
29
require_once("guiconfig.inc");
30
require_once("pfsense-utils.inc");
31

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

    
36
$serviceproviders = &$serviceproviders_attr['serviceproviders']['country'];
37

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

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

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

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

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

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

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

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