Project

General

Profile

Download (4.56 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * getserviceproviders.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 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
	return $provider_list;
46
}
47

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

    
60
function providers_list($country) {
61
	$serviceproviders = get_country_providers($country);
62
	foreach ($serviceproviders as $sp) {
63
		echo $sp['name']['value'] . "\n";
64
	}
65
}
66

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

    
93
				$dns_arr = is_array($conndata['dns'][0]) ? $conndata['dns'] : array($conndata['dns']);
94
				foreach ($dns_arr as $dns) {
95
					echo '<dns>' . $dns['value'] . "</dns>\n";
96
				}
97
			}
98
			break;
99
		}
100
	}
101
	echo "</connection>";
102
}
103

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

    
127
$_GET_OR_POST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? $_POST : $_GET;
128

    
129
if (isset($_GET_OR_POST['country']) && !isset($_GET_OR_POST['provider'])) {
130
	providers_list($_GET_OR_POST['country']);
131
} elseif (isset($_GET_OR_POST['country']) && isset($_GET_OR_POST['provider'])) {
132
	if (isset($_GET_OR_POST['plan'])) {
133
		provider_plan_data($_GET_OR_POST['country'], $_GET_OR_POST['provider'], $_GET_OR_POST['plan']);
134
	} else {
135
		provider_plans_list($_GET_OR_POST['country'], $_GET_OR_POST['provider']);
136
	}
137
} else {
138
	country_list();
139
}
140
?>
(56-56/223)