forked from FreePBX/sipsettings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
natget.html.php
69 lines (59 loc) · 2.37 KB
/
natget.html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/* $Id:$ */
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
// Original Release by Philippe Lindheimer
// Copyright Philippe Lindheimer (2009)
// Copyright Bandwidth.com (2009)
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
global $amp_conf;
$output = array();
$fn = "http://myip.freepbx.org:5060/whatismyip.php";
$json_array['status'] = _('Failed to auto-detect settings');
$json_array['externip'] = '';
/* Fetch the IP address of this system, expects xml formatted as:
<xml>
<ipaddress>
nnn.nnn.nnn.nnn
</ipaddress>
</xml>
*/
$ip_xml = file_get_contents_url($fn);
//TODO: check for === false and deal with detected error
preg_match('|^<xml><ipaddress>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</ipaddress></xml>$|',$ip_xml,$matches);
if (isset($matches[1])) {
$json_array['externip'] = $matches[1];
$json_array['status'] = _('Failed to auto-detect local network settings');
// TODO: Still find a better way to find patch to route command?
//
if (is_executable('/sbin/route')) {
$routecmd = "/sbin/route -nv";
} elseif (is_executable('/bin/route')) {
$routecmd = "/bin/route -nv";
} else {
$routecmd = "route -nv";
}
exec($routecmd,$output,$retcode);
foreach ($output as $line) {
preg_match('/^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/',$line,$matches);
if (isset($matches[3]) && $matches[2] == '0.0.0.0' && substr($matches[1],0,4) != '169.') {
$localnet[$matches[1]] = $matches[3];
$json_array['status'] = 'success';
}
}
} else {
$json_array['status'] = _('Failed to auto-detect settings');
}
$json_array['localnet'] = $localnet;
header("Content-type: application/json");
echo json_encode($json_array);