]>
git.defcon.no Git - hermes/blob - 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...
33 if ( array_key_exists( 'destination', $_GET) )
35 $tmp = split_sipaddress($_GET['destination']);
38 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
41 list ( $dest_username, $dest_domain ) = $tmp;
43 if ( array_key_exists( 'e164', $_GET ) )
45 if ( strtolower($_GET['e164']) == "true" ) $e164_only = true;
47 // TODO: Well. Code. This is a stub
49 print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) );
51 else if ( array_key_exists( 'alias', $_GET) )
53 $tmp = split_sipaddress($_GET['alias']);
56 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
59 list ( $alias_username, $alias_domain ) = $tmp;
61 // TODO: Well. Code. This is a stub
63 print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) );
66 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
69 if ( array_key_exists( 'destination', $_GET)
70 && ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) ||
array_key_exists('alias', $_GET) ) )
74 if ( array_key_exists('alias_username', $_GET) )
76 $alias_username = $_GET['alias_username'];
77 $alias_domain = $_GET['alias_domain'];
81 $alias = split_sipaddress($_GET['alias']);
84 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
87 list ( $alias_username, $alias_domain ) = $alias;
90 if ( !verify_sipadress($_GET['destination']) )
92 // TODO: Provide a better response..
93 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
96 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
98 // TODO: Provide a better response..
99 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
103 // TODO: Verify that alias does not collide with existing SIP-adress
104 if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
106 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
110 list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
112 if ( (!$dest_username)||
(!$dest_domain))
114 // TODO: Provide a better response..
115 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
118 if ( !is_kamailio_domain( $alias_domain ) )
120 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
123 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
125 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
128 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
130 $t = get_e164_alias( $dest_username, $dest_domain );
133 print json_encode ( array(
134 'response' => 'failed',
136 'detail' => 'User already has E164 number alias',
141 if ( alias_exists ( $alias_username, $alias_domain ) )
143 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
146 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
148 print json_encode( array ('response' => 'ok',
149 'alias' => $alias_username . "@" . $alias_domain,
150 'destination' => $dest_username . "@" . $dest_domain ));
153 print json_encode ( array ( 'response' => 'error' ));
158 print json_encode ( array( 'response' => 'invalid') );
162 if ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) ||
array_key_exists('alias', $_GET) )
164 $alias_username = "";
166 if ( array_key_exists('alias_username', $_GET) )
168 $alias_username = $_GET['alias_username'];
169 $alias_domain = $_GET['alias_domain'];
173 $alias = split_sipaddress($_GET['alias']);
176 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
179 list ( $alias_username, $alias_domain ) = $alias;
182 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
184 // TODO: Provide a better response..
185 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
188 if ( ! alias_exists ( $alias_username, $alias_domain ) )
190 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
193 if ( remove_alias( $alias_username, $alias_domain ) )
195 print json_encode( array ('response' => 'ok',
196 'alias' => $alias_username . "@" . $alias_domain));
199 print json_encode ( array ( 'response' => 'error' ));
202 print json_encode ( array( 'response' => 'invalid') );
205 print json_encode ( array( 'response' => 'invalid') );
207 mysql_close( $config['sql_link'] );