From: jonl Date: Mon, 16 Jan 2012 21:08:11 +0000 (+0100) Subject: Added alias/list functionality! Also, moved some data around in lib/alias_functions... X-Git-Url: https://git.defcon.no/?a=commitdiff_plain;h=e9dd114b2570a41c4f517bdee270fbc2c791de92;p=hermes Added alias/list functionality! Also, moved some data around in lib/alias_functions to ease the implementation of alias/list --- diff --git a/alias.php b/alias.php index 9adb434..6451b2e 100644 --- a/alias.php +++ b/alias.php @@ -27,6 +27,7 @@ if ( !$config['sql_link'] ) Required parameters should be... destination */ + $list = array(); $dest_username = ""; $dest_domain = ""; $e164_only = false; @@ -43,10 +44,12 @@ if ( !$config['sql_link'] ) 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 ); } - // TODO: Well. Code. This is a stub - - print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) ); } else if ( array_key_exists( 'alias', $_GET) ) { @@ -58,12 +61,13 @@ if ( !$config['sql_link'] ) } list ( $alias_username, $alias_domain ) = $tmp; - // TODO: Well. Code. This is a stub - - print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) ); + $list = get_destination( $alias_username, $alias_domain ); } else - print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) ); + $list = get_aliases( null, null ); + + + print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) ); break; case "/add": if ( array_key_exists( 'destination', $_GET) @@ -134,7 +138,7 @@ if ( !$config['sql_link'] ) 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already has E164 number alias', - 'alias' => $t )); + 'alias' => $t['alias'] )); break; } } diff --git a/api-nodes.txt b/api-nodes.txt index 9dc240a..7ea2100 100644 --- a/api-nodes.txt +++ b/api-nodes.txt @@ -164,7 +164,22 @@ alias/list alias/list?destination=foo@bar.bz alias/list?destination=foo@bar.bz&e164=true alias/list?alias=foo@bar.bz - Currently not implemented + With no parameters, this will return all defined aliases (potentially a + huge list). With the destination parameter set, only aliases dfor that + destination will be listed, and with the e164 option set to true, only + an e164 alias will be returned (if one/it exists). The alias parameter + gives the same behaviour, butr looks up an alias address instead of the + destination. The e164 option is not valid for the alias search + (naturally). + + Returns 'ok' on success, with an array of 'destination' and 'alias' + pairs. + + Returns 'ok' with an empty array if the search gave no results. + + Returns 'ok' with an empty array if the database search fails. + + Returns 'failed' with 'cause' = 'invalid' on invalid SIP addresses. alias/add?alias_username=foo&alias_domain=bar.bz&destination=bar@qux.zx alias/add?alias=foo@bar.bz&destination=bar@qux.zx @@ -204,10 +219,6 @@ alias/remove?alias=foo@bar.bz TODO list: --------------------- -Planned, but not implemented nodes/functions: - user/update?username=foo&domain=bar - alias/list - alias/user?destination=foo@bar.bz authentication-mechanism :) diff --git a/lib/alias_functions.php b/lib/alias_functions.php index 4a6b3aa..51c3b58 100644 --- a/lib/alias_functions.php +++ b/lib/alias_functions.php @@ -7,15 +7,51 @@ require_once('lib/db_functions.php'); function get_destination ( $alias_username, $alias_domain ) { global $config; + + $aliases = array(); + + // Terminate early on missing param. + if (!( $alias_username && $alias_domain)) return $aliases; + + $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" + . " WHERE alias_username = '%s' AND alias_domain = '%s'", + $config['kamailio_alias_table'], + sql_clean($alias_username), + sql_clean($alias_domain) + ); + + $result = sql_dbquery( $config['kamailio_db'], $query); + // If result is empty, there was either an SQL error, or simply no results. + // At this point, no error checking is performed, instead the empty array is returned. + if ( ! $result ) return $aliases; + + while ( $row = mysql_fetch_assoc( $result ) ) + { + array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) ); + } + return $aliases; } + function get_aliases ( $dest_username, $dest_domain ) { global $config; $aliases = array(); - $query = sprintf("SELECT CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" - . " WHERE username = '%s' AND domain = '%s'", - $config['kamailio_alias_table'], $dest_username, $dest_domain); + $query = ""; + if (( $dest_username == null ) && ( $dest_domain == null) ) + { + $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" , + $config['kamailio_alias_table']); + } + else + { + $query = sprintf("SELECT '%s' AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" + . " WHERE username = '%s' AND domain = '%s'", + sql_clean($dest_username. '@' . $dest_domain), + $config['kamailio_alias_table'], + sql_clean($dest_username), + sql_clean($dest_domain)); + } $result = sql_dbquery( $config['kamailio_db'], $query); // If result is empty, there was either an SQL error, or simply no results. // At this point, no error checking is performed, instead the empty array is returned. @@ -23,7 +59,7 @@ function get_aliases ( $dest_username, $dest_domain ) while ( $row = mysql_fetch_assoc( $result ) ) { - array_push( $aliases, $row['alias'] ); + array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) ); } return $aliases; } @@ -34,7 +70,7 @@ function get_e164_alias( $dest_username, $dest_domain ) $cur_aliases = get_aliases( $dest_username, $dest_domain ); foreach ( $cur_aliases as $testalias ) { - list( $tau, $foo ) = split_sipaddress( $testalias ); + list( $tau, $foo ) = split_sipaddress( $testalias['alias'] ); if ( verify_e164 ( $tau ) ) return $testalias; } return false;