]>
git.defcon.no Git - hermes/blob - api/alias.php
3 # Copyright (c) 2012, Gjøvik University College
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.
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.
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');
36 $config = get_config();
38 $config['sql_link'] = @mysql_connect
(
39 $config['sql_server'],
40 $config['sql_username'],
41 $config['sql_password']
43 if ( !$config['sql_link'] )
45 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
51 //*************************************************************************************
52 switch ( $_SERVER['PATH_INFO'] )
56 Required parameters should be...
63 if ( array_key_exists( 'destination', $_POST) )
65 $tmp = split_sipaddress($_POST['destination']);
68 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
71 list ( $dest_username, $dest_domain ) = $tmp;
73 if ( array_key_exists( 'e164', $_POST ) )
75 if ( strtolower($_POST['e164']) == "true" ) $e164_only = true;
76 $list = get_e164_alias( $dest_username, $dest_domain );
78 else if ( $dest_username && $dest_domain )
80 $list = get_aliases( $dest_username, $dest_domain );
83 else if ( array_key_exists( 'alias', $_POST) )
85 $tmp = split_sipaddress($_POST['alias']);
88 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
91 list ( $alias_username, $alias_domain ) = $tmp;
93 $list = get_destination( $alias_username, $alias_domain );
96 $list = get_aliases( null, null );
99 print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
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) ) )
105 $alias_username = "";
107 if ( array_key_exists('alias_username', $_POST) )
109 $alias_username = $_POST['alias_username'];
110 $alias_domain = $_POST['alias_domain'];
114 $alias = split_sipaddress($_POST['alias']);
117 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
120 list ( $alias_username, $alias_domain ) = $alias;
123 if ( !verify_sipadress($_POST['destination']) )
125 // TODO: Provide a better response..
126 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
129 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
131 // TODO: Provide a better response..
132 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
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) )
139 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
143 list ( $dest_username, $dest_domain ) = split_sipaddress( $_POST['destination']);
145 if ( (!$dest_username)||
(!$dest_domain))
147 // TODO: Provide a better response..
148 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
151 if ( !is_kamailio_domain( $alias_domain ) )
153 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
156 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
158 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
161 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
163 $t = get_e164_alias( $dest_username, $dest_domain );
166 print json_encode ( array(
167 'response' => 'failed',
169 'detail' => 'User already has E164 number alias',
170 'alias' => $t['alias'] ));
174 if ( alias_exists ( $alias_username, $alias_domain ) )
176 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
179 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
181 print json_encode( array ('response' => 'ok',
182 'alias' => $alias_username . "@" . $alias_domain,
183 'destination' => $dest_username . "@" . $dest_domain ));
186 print json_encode ( array ( 'response' => 'error' ));
191 print json_encode ( array( 'response' => 'invalid') );
195 if ( ( array_key_exists( 'alias_username', $_POST) && array_key_exists( 'alias_domain', $_POST ) ) ||
array_key_exists('alias', $_POST) )
197 $alias_username = "";
199 if ( array_key_exists('alias_username', $_POST) )
201 $alias_username = $_POST['alias_username'];
202 $alias_domain = $_POST['alias_domain'];
206 $alias = split_sipaddress($_POST['alias']);
209 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
212 list ( $alias_username, $alias_domain ) = $alias;
215 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
217 // TODO: Provide a better response..
218 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
221 if ( ! alias_exists ( $alias_username, $alias_domain ) )
223 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
226 if ( remove_alias( $alias_username, $alias_domain ) )
228 print json_encode( array ('response' => 'ok',
229 'alias' => $alias_username . "@" . $alias_domain));
232 print json_encode ( array ( 'response' => 'error' ));
235 print json_encode ( array( 'response' => 'invalid') );
238 print json_encode ( array( 'response' => 'invalid') );
240 mysql_close( $config['sql_link'] );