]> git.defcon.no Git - hermes/blob - api/alias.php
Adding tests for phone-nodes
[hermes] / api / alias.php
1 <?php
2 require_once('config.php');
3 require_once('lib/auth_base.php');
4 require_once('lib/user_functions.php');
5 require_once('lib/number_functions.php');
6 require_once('lib/common_functions.php');
7 require_once('lib/db_functions.php');
8 require_once('lib/alias_functions.php');
9
10 $config = get_config();
11
12 $config['sql_link'] = @mysql_connect(
13 $config['sql_server'],
14 $config['sql_username'],
15 $config['sql_password']
16 );
17 if ( !$config['sql_link'] )
18 {
19 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
20 exit;
21 }
22
23 token_auth();
24
25 //*************************************************************************************
26 switch ( $_SERVER['PATH_INFO'] )
27 {
28 case "/list":
29 /*
30 Required parameters should be...
31 destination
32 */
33 $list = array();
34 $dest_username = "";
35 $dest_domain = "";
36 $e164_only = false;
37 if ( array_key_exists( 'destination', $_POST) )
38 {
39 $tmp = split_sipaddress($_POST['destination']);
40 if ( !$tmp )
41 {
42 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
43 break;
44 }
45 list ( $dest_username, $dest_domain ) = $tmp;
46
47 if ( array_key_exists( 'e164', $_POST ) )
48 {
49 if ( strtolower($_POST['e164']) == "true" ) $e164_only = true;
50 $list = get_e164_alias( $dest_username, $dest_domain );
51 }
52 else if ( $dest_username && $dest_domain )
53 {
54 $list = get_aliases( $dest_username, $dest_domain );
55 }
56 }
57 else if ( array_key_exists( 'alias', $_POST) )
58 {
59 $tmp = split_sipaddress($_POST['alias']);
60 if ( !$tmp )
61 {
62 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
63 break;
64 }
65 list ( $alias_username, $alias_domain ) = $tmp;
66
67 $list = get_destination( $alias_username, $alias_domain );
68 }
69 else
70 $list = get_aliases( null, null );
71
72
73 print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
74 break;
75 case "/add":
76 if ( array_key_exists( 'destination', $_POST)
77 && ( ( array_key_exists( 'alias_username', $_POST) && array_key_exists( 'alias_domain', $_POST ) ) || array_key_exists('alias', $_POST) ) )
78 {
79 $alias_username = "";
80 $alias_domain = "";
81 if ( array_key_exists('alias_username', $_POST) )
82 {
83 $alias_username = $_POST['alias_username'];
84 $alias_domain = $_POST['alias_domain'];
85 }
86 else
87 {
88 $alias = split_sipaddress($_POST['alias']);
89 if ( !$alias )
90 {
91 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
92 break;
93 }
94 list ( $alias_username, $alias_domain ) = $alias;
95 }
96
97 if ( !verify_sipadress($_POST['destination']) )
98 {
99 // TODO: Provide a better response..
100 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
101 break;
102 }
103 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
104 {
105 // TODO: Provide a better response..
106 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
107 break;
108 }
109
110 // TODO: Verify that alias does not collide with existing SIP-adress
111 if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
112 {
113 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
114 break;
115 }
116
117 list ( $dest_username, $dest_domain ) = split_sipaddress( $_POST['destination']);
118
119 if ( (!$dest_username)||(!$dest_domain))
120 {
121 // TODO: Provide a better response..
122 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
123 break;
124 }
125 if ( !is_kamailio_domain( $alias_domain ) )
126 {
127 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
128 break;
129 }
130 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
131 {
132 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
133 break;
134 }
135 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
136 {
137 $t = get_e164_alias( $dest_username, $dest_domain );
138 if ( $t )
139 {
140 print json_encode ( array(
141 'response' => 'failed',
142 'cause' => 'exists',
143 'detail' => 'User already has E164 number alias',
144 'alias' => $t['alias'] ));
145 break;
146 }
147 }
148 if ( alias_exists ( $alias_username, $alias_domain ) )
149 {
150 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
151 break;
152 }
153 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
154 {
155 print json_encode( array ('response' => 'ok',
156 'alias' => $alias_username . "@" . $alias_domain,
157 'destination' => $dest_username . "@" . $dest_domain ));
158 break;
159 }
160 print json_encode ( array ( 'response' => 'error' ));
161 break;
162 }
163 else
164 {
165 print json_encode ( array( 'response' => 'invalid') );
166 }
167 break;
168 case "/remove":
169 if ( ( array_key_exists( 'alias_username', $_POST) && array_key_exists( 'alias_domain', $_POST ) ) || array_key_exists('alias', $_POST) )
170 {
171 $alias_username = "";
172 $alias_domain = "";
173 if ( array_key_exists('alias_username', $_POST) )
174 {
175 $alias_username = $_POST['alias_username'];
176 $alias_domain = $_POST['alias_domain'];
177 }
178 else
179 {
180 $alias = split_sipaddress($_POST['alias']);
181 if ( !$alias )
182 {
183 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
184 break;
185 }
186 list ( $alias_username, $alias_domain ) = $alias;
187 }
188
189 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
190 {
191 // TODO: Provide a better response..
192 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
193 break;
194 }
195 if ( ! alias_exists ( $alias_username, $alias_domain ) )
196 {
197 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
198 break;
199 }
200 if ( remove_alias( $alias_username, $alias_domain ) )
201 {
202 print json_encode( array ('response' => 'ok',
203 'alias' => $alias_username . "@" . $alias_domain));
204 break;
205 }
206 print json_encode ( array ( 'response' => 'error' ));
207 break;
208 }
209 print json_encode ( array( 'response' => 'invalid') );
210 break;
211 default:
212 print json_encode ( array( 'response' => 'invalid') );
213 }
214 mysql_close( $config['sql_link'] );
215 ?>