-<?php
-require_once('config.php');
-require_once('lib/user_functions.php');
-require_once('lib/number_functions.php');
-require_once('lib/common_functions.php');
-require_once('lib/db_functions.php');
-require_once('lib/alias_functions.php');
-
-$config = get_config();
-
-$config['sql_link'] = @mysql_connect(
- $config['sql_server'],
- $config['sql_username'],
- $config['sql_password']
-);
-if ( !$config['sql_link'] )
-{
- print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
- exit;
-}
-
-//*************************************************************************************
- switch ( $_SERVER['PATH_INFO'] )
- {
- case "/list":
- /*
- Required parameters should be...
- destination
- */
- $list = array();
- $dest_username = "";
- $dest_domain = "";
- $e164_only = false;
- if ( array_key_exists( 'destination', $_GET) )
- {
- $tmp = split_sipaddress($_GET['destination']);
- if ( !$tmp )
- {
- print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
- break;
- }
- list ( $dest_username, $dest_domain ) = $tmp;
-
- if ( array_key_exists( 'e164', $_GET ) )
- {
- if ( strtolower($_GET['e164']) == "true" ) $e164_only = true;
- $list = get_e164_alias( $dest_username, $dest_domain );
- }
- else if ( $dest_username && $dest_domain )
- {
- $list = get_aliases( $dest_username, $dest_domain );
- }
- }
- else if ( array_key_exists( 'alias', $_GET) )
- {
- $tmp = split_sipaddress($_GET['alias']);
- if ( !$tmp )
- {
- print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
- break;
- }
- list ( $alias_username, $alias_domain ) = $tmp;
-
- $list = get_destination( $alias_username, $alias_domain );
- }
- else
- $list = get_aliases( null, null );
-
-
- print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
- break;
- case "/add":
- if ( array_key_exists( 'destination', $_GET)
- && ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) ) )
- {
- $alias_username = "";
- $alias_domain = "";
- if ( array_key_exists('alias_username', $_GET) )
- {
- $alias_username = $_GET['alias_username'];
- $alias_domain = $_GET['alias_domain'];
- }
- else
- {
- $alias = split_sipaddress($_GET['alias']);
- if ( !$alias )
- {
- print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
- break;
- }
- list ( $alias_username, $alias_domain ) = $alias;
- }
-
- if ( !verify_sipadress($_GET['destination']) )
- {
- // TODO: Provide a better response..
- print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
- break;
- }
- if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
- {
- // TODO: Provide a better response..
- print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
- break;
- }
-
- // TODO: Verify that alias does not collide with existing SIP-adress
- if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
- {
- print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
- break;
- }
-
- list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
-
- if ( (!$dest_username)||(!$dest_domain))
- {
- // TODO: Provide a better response..
- print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
- break;
- }
- if ( !is_kamailio_domain( $alias_domain ) )
- {
- print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
- break;
- }
- if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
- {
- print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
- break;
- }
- if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
- {
- $t = get_e164_alias( $dest_username, $dest_domain );
- if ( $t )
- {
- print json_encode ( array(
- 'response' => 'failed',
- 'cause' => 'exists',
- 'detail' => 'User already has E164 number alias',
- 'alias' => $t['alias'] ));
- break;
- }
- }
- if ( alias_exists ( $alias_username, $alias_domain ) )
- {
- print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
- break;
- }
- if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
- {
- print json_encode( array ('response' => 'ok',
- 'alias' => $alias_username . "@" . $alias_domain,
- 'destination' => $dest_username . "@" . $dest_domain ));
- break;
- }
- print json_encode ( array ( 'response' => 'error' ));
- break;
- }
- else
- {
- print json_encode ( array( 'response' => 'invalid') );
- }
- break;
- case "/remove":
- if ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) )
- {
- $alias_username = "";
- $alias_domain = "";
- if ( array_key_exists('alias_username', $_GET) )
- {
- $alias_username = $_GET['alias_username'];
- $alias_domain = $_GET['alias_domain'];
- }
- else
- {
- $alias = split_sipaddress($_GET['alias']);
- if ( !$alias )
- {
- print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
- break;
- }
- list ( $alias_username, $alias_domain ) = $alias;
- }
-
- if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
- {
- // TODO: Provide a better response..
- print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
- break;
- }
- if ( ! alias_exists ( $alias_username, $alias_domain ) )
- {
- print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
- break;
- }
- if ( remove_alias( $alias_username, $alias_domain ) )
- {
- print json_encode( array ('response' => 'ok',
- 'alias' => $alias_username . "@" . $alias_domain));
- break;
- }
- print json_encode ( array ( 'response' => 'error' ));
- break;
- }
- print json_encode ( array( 'response' => 'invalid') );
- break;
- default:
- print json_encode ( array( 'response' => 'invalid') );
- }
-mysql_close( $config['sql_link'] );
-?>