]>
git.defcon.no Git - hermes/blob - api/alias.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');
10 $config = get_config();
12 $config['sql_link'] = @mysql_connect
(
13 $config['sql_server'],
14 $config['sql_username'],
15 $config['sql_password']
17 if ( !$config['sql_link'] )
19 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
25 //*************************************************************************************
26 switch ( $_SERVER['PATH_INFO'] )
30 Required parameters should be...
37 if ( array_key_exists( 'destination', $_POST) )
39 $tmp = split_sipaddress($_POST['destination']);
42 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
45 list ( $dest_username, $dest_domain ) = $tmp;
47 if ( array_key_exists( 'e164', $_POST ) )
49 if ( strtolower($_POST['e164']) == "true" ) $e164_only = true;
50 $list = get_e164_alias( $dest_username, $dest_domain );
52 else if ( $dest_username && $dest_domain )
54 $list = get_aliases( $dest_username, $dest_domain );
57 else if ( array_key_exists( 'alias', $_POST) )
59 $tmp = split_sipaddress($_POST['alias']);
62 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
65 list ( $alias_username, $alias_domain ) = $tmp;
67 $list = get_destination( $alias_username, $alias_domain );
70 $list = get_aliases( null, null );
73 print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
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) ) )
81 if ( array_key_exists('alias_username', $_POST) )
83 $alias_username = $_POST['alias_username'];
84 $alias_domain = $_POST['alias_domain'];
88 $alias = split_sipaddress($_POST['alias']);
91 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
94 list ( $alias_username, $alias_domain ) = $alias;
97 if ( !verify_sipadress($_POST['destination']) )
99 // TODO: Provide a better response..
100 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
103 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
105 // TODO: Provide a better response..
106 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
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) )
113 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
117 list ( $dest_username, $dest_domain ) = split_sipaddress( $_POST['destination']);
119 if ( (!$dest_username)||
(!$dest_domain))
121 // TODO: Provide a better response..
122 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
125 if ( !is_kamailio_domain( $alias_domain ) )
127 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
130 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
132 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
135 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
137 $t = get_e164_alias( $dest_username, $dest_domain );
140 print json_encode ( array(
141 'response' => 'failed',
143 'detail' => 'User already has E164 number alias',
144 'alias' => $t['alias'] ));
148 if ( alias_exists ( $alias_username, $alias_domain ) )
150 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
153 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
155 print json_encode( array ('response' => 'ok',
156 'alias' => $alias_username . "@" . $alias_domain,
157 'destination' => $dest_username . "@" . $dest_domain ));
160 print json_encode ( array ( 'response' => 'error' ));
165 print json_encode ( array( 'response' => 'invalid') );
169 if ( ( array_key_exists( 'alias_username', $_POST) && array_key_exists( 'alias_domain', $_POST ) ) ||
array_key_exists('alias', $_POST) )
171 $alias_username = "";
173 if ( array_key_exists('alias_username', $_POST) )
175 $alias_username = $_POST['alias_username'];
176 $alias_domain = $_POST['alias_domain'];
180 $alias = split_sipaddress($_POST['alias']);
183 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
186 list ( $alias_username, $alias_domain ) = $alias;
189 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
191 // TODO: Provide a better response..
192 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
195 if ( ! alias_exists ( $alias_username, $alias_domain ) )
197 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
200 if ( remove_alias( $alias_username, $alias_domain ) )
202 print json_encode( array ('response' => 'ok',
203 'alias' => $alias_username . "@" . $alias_domain));
206 print json_encode ( array ( 'response' => 'error' ));
209 print json_encode ( array( 'response' => 'invalid') );
212 print json_encode ( array( 'response' => 'invalid') );
214 mysql_close( $config['sql_link'] );