]> git.defcon.no Git - hermes/blob - api/alias.php
Permitedcalls is now called permittedcalls in the DB. Fixed a failure-failure resulti...
[hermes] / api / alias.php
1 <?php
2 /*
3 # Copyright (c) 2012, Gjøvik University College
4 # All rights reserved.
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Gjøvik University College nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 require_once('config.php');
29 require_once('lib/auth_base.php');
30 require_once('lib/user_functions.php');
31 require_once('lib/number_functions.php');
32 require_once('lib/common_functions.php');
33 require_once('lib/db_functions.php');
34 require_once('lib/alias_functions.php');
35
36 $config = get_config();
37
38 $config['sql_link'] = @mysql_connect(
39 $config['sql_server'],
40 $config['sql_username'],
41 $config['sql_password']
42 );
43 if ( !$config['sql_link'] )
44 {
45 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
46 exit;
47 }
48
49 token_auth();
50
51 //*************************************************************************************
52 switch ( $_SERVER['PATH_INFO'] )
53 {
54 case "/list":
55 /*
56 Required parameters should be...
57 destination
58 */
59 $list = array();
60 $dest_username = "";
61 $dest_domain = "";
62 $e164_only = false;
63 if ( array_key_exists( 'destination', $_POST) )
64 {
65 $tmp = split_sipaddress($_POST['destination']);
66 if ( !$tmp )
67 {
68 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
69 break;
70 }
71 list ( $dest_username, $dest_domain ) = $tmp;
72
73 if ( array_key_exists( 'e164', $_POST ) )
74 {
75 if ( strtolower($_POST['e164']) == "true" ) $e164_only = true;
76 $list = get_e164_alias( $dest_username, $dest_domain );
77 }
78 else if ( $dest_username && $dest_domain )
79 {
80 $list = get_aliases( $dest_username, $dest_domain );
81 }
82 }
83 else if ( array_key_exists( 'alias', $_POST) )
84 {
85 $tmp = split_sipaddress($_POST['alias']);
86 if ( !$tmp )
87 {
88 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
89 break;
90 }
91 list ( $alias_username, $alias_domain ) = $tmp;
92
93 $list = get_destination( $alias_username, $alias_domain );
94 }
95 else
96 $list = get_aliases( null, null );
97
98
99 print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
100 break;
101 case "/add":
102 if ( array_key_exists( 'destination', $_POST)
103 && ( ( array_key_exists( 'alias_username', $_POST) && array_key_exists( 'alias_domain', $_POST ) ) || array_key_exists('alias', $_POST) ) )
104 {
105 $alias_username = "";
106 $alias_domain = "";
107 if ( array_key_exists('alias_username', $_POST) )
108 {
109 $alias_username = $_POST['alias_username'];
110 $alias_domain = $_POST['alias_domain'];
111 }
112 else
113 {
114 $alias = split_sipaddress($_POST['alias']);
115 if ( !$alias )
116 {
117 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
118 break;
119 }
120 list ( $alias_username, $alias_domain ) = $alias;
121 }
122
123 if ( !verify_sipadress($_POST['destination']) )
124 {
125 // TODO: Provide a better response..
126 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
127 break;
128 }
129 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
130 {
131 // TODO: Provide a better response..
132 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
133 break;
134 }
135
136 // TODO: Verify that alias does not collide with existing SIP-adress
137 if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
138 {
139 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
140 break;
141 }
142
143 list ( $dest_username, $dest_domain ) = split_sipaddress( $_POST['destination']);
144
145 if ( (!$dest_username)||(!$dest_domain))
146 {
147 // TODO: Provide a better response..
148 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
149 break;
150 }
151 if ( !is_kamailio_domain( $alias_domain ) )
152 {
153 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
154 break;
155 }
156 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
157 {
158 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
159 break;
160 }
161 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
162 {
163 $t = get_e164_alias( $dest_username, $dest_domain );
164 if ( $t )
165 {
166 print json_encode ( array(
167 'response' => 'failed',
168 'cause' => 'exists',
169 'detail' => 'User already has E164 number alias',
170 'alias' => $t['alias'] ));
171 break;
172 }
173 }
174 if ( alias_exists ( $alias_username, $alias_domain ) )
175 {
176 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
177 break;
178 }
179 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
180 {
181 print json_encode( array ('response' => 'ok',
182 'alias' => $alias_username . "@" . $alias_domain,
183 'destination' => $dest_username . "@" . $dest_domain ));
184 break;
185 }
186 print json_encode ( array ( 'response' => 'error' ));
187 break;
188 }
189 else
190 {
191 print json_encode ( array( 'response' => 'invalid') );
192 }
193 break;
194 case "/remove":
195 if ( ( array_key_exists( 'alias_username', $_POST) && array_key_exists( 'alias_domain', $_POST ) ) || array_key_exists('alias', $_POST) )
196 {
197 $alias_username = "";
198 $alias_domain = "";
199 if ( array_key_exists('alias_username', $_POST) )
200 {
201 $alias_username = $_POST['alias_username'];
202 $alias_domain = $_POST['alias_domain'];
203 }
204 else
205 {
206 $alias = split_sipaddress($_POST['alias']);
207 if ( !$alias )
208 {
209 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
210 break;
211 }
212 list ( $alias_username, $alias_domain ) = $alias;
213 }
214
215 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
216 {
217 // TODO: Provide a better response..
218 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
219 break;
220 }
221 if ( ! alias_exists ( $alias_username, $alias_domain ) )
222 {
223 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
224 break;
225 }
226 if ( remove_alias( $alias_username, $alias_domain ) )
227 {
228 print json_encode( array ('response' => 'ok',
229 'alias' => $alias_username . "@" . $alias_domain));
230 break;
231 }
232 print json_encode ( array ( 'response' => 'error' ));
233 break;
234 }
235 print json_encode ( array( 'response' => 'invalid') );
236 break;
237 default:
238 print json_encode ( array( 'response' => 'invalid') );
239 }
240 mysql_close( $config['sql_link'] );
241 ?>