]>
git.defcon.no Git - hermes/blob - api/alias.php
2 require_once('config.php');
3 require_once('lib/user_functions.php');
4 require_once('lib/number_functions.php');
5 require_once('lib/common_functions.php');
6 require_once('lib/db_functions.php');
7 require_once('lib/alias_functions.php');
9 $config = get_config();
11 $config['sql_link'] = @mysql_connect
(
12 $config['sql_server'],
13 $config['sql_username'],
14 $config['sql_password']
16 if ( !$config['sql_link'] )
18 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
22 //*************************************************************************************
23 switch ( $_SERVER['PATH_INFO'] )
27 Required parameters should be...
34 if ( array_key_exists( 'destination', $_GET) )
36 $tmp = split_sipaddress($_GET['destination']);
39 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
42 list ( $dest_username, $dest_domain ) = $tmp;
44 if ( array_key_exists( 'e164', $_GET ) )
46 if ( strtolower($_GET['e164']) == "true" ) $e164_only = true;
47 $list = get_e164_alias( $dest_username, $dest_domain );
49 else if ( $dest_username && $dest_domain )
51 $list = get_aliases( $dest_username, $dest_domain );
54 else if ( array_key_exists( 'alias', $_GET) )
56 $tmp = split_sipaddress($_GET['alias']);
59 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
62 list ( $alias_username, $alias_domain ) = $tmp;
64 $list = get_destination( $alias_username, $alias_domain );
67 $list = get_aliases( null, null );
70 print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
73 if ( array_key_exists( 'destination', $_GET)
74 && ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) ||
array_key_exists('alias', $_GET) ) )
78 if ( array_key_exists('alias_username', $_GET) )
80 $alias_username = $_GET['alias_username'];
81 $alias_domain = $_GET['alias_domain'];
85 $alias = split_sipaddress($_GET['alias']);
88 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
91 list ( $alias_username, $alias_domain ) = $alias;
94 if ( !verify_sipadress($_GET['destination']) )
96 // TODO: Provide a better response..
97 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
100 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
102 // TODO: Provide a better response..
103 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
107 // TODO: Verify that alias does not collide with existing SIP-adress
108 if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
110 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
114 list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
116 if ( (!$dest_username)||
(!$dest_domain))
118 // TODO: Provide a better response..
119 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
122 if ( !is_kamailio_domain( $alias_domain ) )
124 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
127 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
129 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
132 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
134 $t = get_e164_alias( $dest_username, $dest_domain );
137 print json_encode ( array(
138 'response' => 'failed',
140 'detail' => 'User already has E164 number alias',
141 'alias' => $t['alias'] ));
145 if ( alias_exists ( $alias_username, $alias_domain ) )
147 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
150 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
152 print json_encode( array ('response' => 'ok',
153 'alias' => $alias_username . "@" . $alias_domain,
154 'destination' => $dest_username . "@" . $dest_domain ));
157 print json_encode ( array ( 'response' => 'error' ));
162 print json_encode ( array( 'response' => 'invalid') );
166 if ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) ||
array_key_exists('alias', $_GET) )
168 $alias_username = "";
170 if ( array_key_exists('alias_username', $_GET) )
172 $alias_username = $_GET['alias_username'];
173 $alias_domain = $_GET['alias_domain'];
177 $alias = split_sipaddress($_GET['alias']);
180 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
183 list ( $alias_username, $alias_domain ) = $alias;
186 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
188 // TODO: Provide a better response..
189 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
192 if ( ! alias_exists ( $alias_username, $alias_domain ) )
194 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
197 if ( remove_alias( $alias_username, $alias_domain ) )
199 print json_encode( array ('response' => 'ok',
200 'alias' => $alias_username . "@" . $alias_domain));
203 print json_encode ( array ( 'response' => 'error' ));
206 print json_encode ( array( 'response' => 'invalid') );
209 print json_encode ( array( 'response' => 'invalid') );
211 mysql_close( $config['sql_link'] );