astpp.xml.php ➔ load_acl()   C
last analyzed

Complexity

Conditions 10
Paths 80

Size

Total Lines 68
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 41
nc 80
nop 3
dl 0
loc 68
rs 6.0995
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
###############################################################################
3
# ASTPP - Open Source VoIP Billing Solution
4
#
5
# Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
6
# Samir Doshi <[email protected]>
7
# ASTPP Version 3.0 and above
8
# License https://www.gnu.org/licenses/agpl-3.0.html
9
#
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU Affero General Public License as
12
# published by the Free Software Foundation, either version 3 of the
13
# License, or (at your option) any later version.
14
# 
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU Affero General Public License for more details.
19
# 
20
# You should have received a copy of the GNU Affero General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
###############################################################################
23
24
//If module/app not found
25
function xml_not_found() {
26
	header('Content-Type: text/xml');
27
28
	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
29
	$xml .= "<document type=\"freeswitch/xml\">\n";
30
	$xml .= "  <section name=\"result\">\n";
31
	$xml .= "    <result status=\"not found\"/>\n";
32
	$xml .= "  </section>\n";
33
	$xml .= "</document>\n";
34
	echo $xml;
35
	exit();
36
}
37
38
//Build acl xml
39
function load_acl($logger, $db,$config) {
40
	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
41
	$xml .= "<document type=\"freeswitch/xml\">\n";
42
	$xml .= "   <section name=\"Configuration\" description=\"Configuration\">\n";
43
	$xml .= "       <configuration name=\"acl.conf\" description=\"Network List\">\n";
44
	$xml .= "           <network-lists>\n";
45
	$xml .= "       <list name=\"default\" default=\"deny\">\n";
46
	
47
	//For customer and provider ips
48
	$query = "SELECT ip FROM ip_map,accounts WHERE ip_map.accountid=accounts.id AND accounts.status=0 AND deleted=0";
49
	$logger->log("ACL Query : " . $query);
50
	$res_acl = $db->run($query);
51
	$logger->log($res_acl);
52
53
	foreach ($res_acl as $res_acl_key => $res_acl_value) {
54
		$xml .= "       <node type=\"allow\" cidr=\"" . $res_acl_value['ip'] . "\"/>\n";
55
	}
56
57
	//For gateways
58
	$query = "SELECT * FROM gateways WHERE status=0";
59
	$logger->log("Sofia Gateway Query : " . $query);
60
	$sp_gw = $db->run($query);
61
	$logger->log($sp_gw);
62
63
	foreach ($sp_gw as $sp_gw_key => $sp_gw_value) {
64
65
			$sp_gw_settings  = json_decode($sp_gw_value['gateway_data'], true);
66
			foreach ($sp_gw_settings as $sp_gw_settings_key => $sp_gw_settings_value) {
67
				if ($sp_gw_settings_value != "" && $sp_gw_settings_key == "proxy")
68
				{
69
					$tmp_ip_arr = explode(":",$sp_gw_settings_value);
70
					if (!filter_var($tmp_ip_arr[0], FILTER_VALIDATE_IP) === false) {
71
						$xml .= "   <node type=\"allow\" cidr=\"" . $tmp_ip_arr[0] . "/32\"/>\n";
72
					}
73
				}
74
	   	}
75
76
	}
77
78
79
	//For opensips
80
	if($config['opensips'] == '0')
81
	{
82
		$xml .= "<node type=\"allow\" cidr=\"".$config['opensips_domain']."/32\"/>\n";     
83
	}
84
85
	$xml .= "       </list>\n";
86
87
	//For loopback 
88
	if($config['opensips'] == '0')
89
	{
90
		$xml .= "<list name=\"loopback.auto\" default=\"allow\">\n";
91
			$xml .= "<node type=\"allow\" cidr=\"".$config['opensips_domain']."/32\"/>\n";
92
		$xml .= "</list>\n";
93
	}
94
95
	//For event handing
96
	$xml .= "<list name=\"event\" default=\"deny\">\n";
97
		$xml .= ($config['opensips'] == "0")?"<node type=\"allow\" cidr=\"".$config['opensips_domain']."/32\"/>\n":"\n";
98
		$xml .= "<node type=\"allow\" cidr=\"127.0.0.0/8\"/>\n";
99
	$xml .= "</list>\n";
100
	$xml .= "           </network-lists>\n";
101
	$xml .= "       </configuration>\n";
102
	$xml .= "   </section>\n";
103
	$xml .= "</document>\n";
104
	$logger->log($xml);
105
	return $xml;
106
}
107
108
//Build sofia xml
109
function load_sofia($logger, $db) {
110
	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
111
	$xml .= "<document type=\"freeswitch/xml\">\n";
112
	$xml .= "   <section name=\"Configuration\" description=\"Configuration\">\n";
113
	$xml .= "   <configuration name=\"sofia.conf\" description=\"SIP Profile\">\n";
114
	$xml .= "   <profiles>\n";
115
116
	$query = "SELECT * FROM sip_profiles WHERE status=0";
117
	$logger->log("Sofia Query : " . $query);
118
	$res_sp = $db->run($query);
119
	//$logger->log($res_sp);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
121
	foreach ($res_sp as $sp_key => $sp_value) {
122
123
		$settings = json_decode($sp_value['profile_data'], true);
124
		//$logger->log(print_r($settings,true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
		$xml .= "   <profile name=\"" . $sp_value['name'] . "\">\n";
126
127
		$xml .= "   <domains>\n";
128
			$xml .= "       <domain name=\"" . $sp_value['sip_ip'] . "\" alias=\"true\" parse=\"true\"/>\n";
129
		$xml .= "   </domains>\n";
130
		/*$xml .= "   <aliases>\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
131
            $xml .= "       <alias name=\"" . $sp_value['sip_ip'] . "\"/>\n";
132
        $xml .= "   </aliases>\n";*/
133
134
	$xml .= "   <settings>\n";
135
	$xml .= "       <param name=\"sip-ip\" value=\"" . $sp_value['sip_ip'] . "\"/>\n";
136
	$xml .= "       <param name=\"sip-port\" value=\"" . $sp_value['sip_port'] . "\"/>\n";    
137
	foreach($settings as $set_key => $set_val)
138
	{
139
			$xml .= "       <param name=\"" . $set_key . "\" value=\"" . $set_val . "\"/>\n";
140
	}
141
		$xml .= "   </settings>\n";
142
	
143
144
		//Gateway block start 
145
		$xml .= "   <gateways>\n";
146
		$query = "SELECT * FROM gateways WHERE sip_profile_id=" . $sp_value['id'] . " AND status=0";
147
		$logger->log("Sofia Gateway Query : " . $query);
148
		$sp_gw = $db->run($query);
149
		$logger->log($sp_gw);
150
		foreach ($sp_gw as $sp_gw_key => $sp_gw_value) {
151
			$xml .= "       <gateway name=\"" . $sp_gw_value['name'] . "\">\n";
152
153
154
155
		$sp_gw_settings  = json_decode($sp_gw_value['gateway_data'], true);
156
			foreach ($sp_gw_settings as $sp_gw_settings_key => $sp_gw_settings_value) {
157
				if ($sp_gw_settings_value != "")
158
					$xml .= "           <param name=\"" . $sp_gw_settings_key . "\" value=\"" . $sp_gw_settings_value . "\"/>\n";
159
			}
160
			$xml .= "       </gateway>\n";
161
		}
162
		$xml .= "   </gateways>\n";
163
		//Gateway block end 
164
165
		$xml .= "   </profile>\n";
166
	}
167
	//echo $xml;
168
	$xml .= "   </profiles>\n";
169
	$xml .= "   </configuration>\n";
170
	$xml .= "   </section>\n";
171
	$xml .= "</document>\n";
172
	$logger->log($xml);
173
	return $xml;
174
}
175
176
//Build directory xml
177
function load_directory($logger, $db) {
178
	$xml = "";
179
180
  $query = "SELECT username,dir_params,dir_vars,number as accountcode,accountid FROM sip_devices,accounts WHERE sip_devices.status=0 AND accounts.status=0 AND accounts.deleted=0 AND accounts.id=sip_devices.accountid AND username='" . $_REQUEST['user']."' limit 1";
181
	
182
	$logger->log("Directory Query : " . $query);
183
	$res_dir = $db->run($query);
184
	$logger->log($res_dir);
185
186
	foreach ($res_dir as $res_dir_key => $res_dir_value) {
187
		$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
188
		$xml .= "<document type=\"freeswitch/xml\">\n";
189
		$xml .= "   <section name=\"Directory\" description=\"Directory\">\n";
190
		$xml .= "       <domain name=\"" . $_REQUEST['domain'] . "\" alias=\"true\">\n";
191
		$xml .= "           <user id=\"" . $_REQUEST['user'] . "\">\n";
192
193
	$params = json_decode($res_dir_value['dir_params'], true);
194
195
	$vars   = json_decode($res_dir_value['dir_vars'], true);
196
	$param_xml = $var_xml = "";
197
	foreach($params as $parms_key => $res_dir_params)
198
	{	
199
						$param_xml .= "<param name=\"" . $parms_key . "\" value=\"" . $res_dir_params . "\"/>\n";
200
	}
201
202
	foreach($vars as $var_key => $res_dir_vars)
203
	{	
204
						$var_xml .= "<variable name=\"" . $var_key . "\" value=\"" . $res_dir_vars . "\"/>\n";
205
	}		
206
207
208
		$xml .= "               <params>\n";
209
		$xml .= $param_xml;
210
		$xml .= "<param name=\"allow-empty-password\" value=\"false\"/>\n";
211
		$xml .= "<param name=\"dial-string\" value=\"{sip_invite_domain=\${domain_name},presence_id=\${dialed_user}@\${domain_name}}\${sofia_contact(*/\${dialed_user}@\${domain_name})}\"/>\n";
212
		$xml .= "               </params>\n";
213
214
		$xml .= "               <variables>\n";
215
		$xml .= $var_xml;
216
		$xml .= "<variable name=\"sipcall\" value=\"true\"/>\n";
217
		$xml .= "<variable name=\"accountcode\" value=\"" . $res_dir_value['accountcode'] . "\"/>\n";
218
		$xml .= "<variable name=\"domain_name\" value=\"" . $_REQUEST['domain'] . "\"/>\n";
219
		$xml .= "               </variables>\n";
220
221
		$xml .= "           </user>\n";
222
		$xml .= "       </domain>\n";
223
		$xml .= "   </section>\n";
224
		$xml .= "</document>\n";
225
	}
226
	
227
	$logger->log($xml);
228
	return $xml;
229
}
230
231
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
232