From e3ea78bd48d210947fcfcb73977e6a34e6b31293 Mon Sep 17 00:00:00 2001 From: Jon Langseth Date: Tue, 6 Aug 2013 12:38:14 +0200 Subject: [PATCH] backlog add --- api/config.php.sample | 3 ++- api/lib/user_functions.php | 34 ++++++++++++++++++++++++++++++---- api/t/tests_common.pm | 4 ++-- api/t/user.t | 2 +- api/user.php | 34 ++++++++++++++++++++++++++++++++++ doc/api-nodes.txt | 26 ++++++++++++++++++++++++++ guc-clients/listsipusers | 1 + 7 files changed, 96 insertions(+), 8 deletions(-) diff --git a/api/config.php.sample b/api/config.php.sample index 997812a..a537807 100644 --- a/api/config.php.sample +++ b/api/config.php.sample @@ -40,6 +40,7 @@ function get_config() 'kamailio_subscriber_table' => 'subscriber', 'kamailio_alias_table' => 'dbaliases', 'kamailio_domain_table' => 'domain', + 'kamailio_location_table' => 'location', 'hermes_users_table' => 'users', 'hermes_phones_table' => 'phones', @@ -51,7 +52,7 @@ function get_config() 'auth_backend' => 'permitall', 'sessionkey_lifetime' => 5, // Minutes - 'standard_dialplan' => '(*xx*.|xxx.)', + 'standard_dialplan' => '(**x*x.|*xx*|*xx*x.|*xx*x.*|*xx|*xx.|xxx.)', 'default_domain' => 'hig.no', 'permit_multiple_e164alias' => false, 'standard_password_length' => 10, diff --git a/api/lib/user_functions.php b/api/lib/user_functions.php index c8556f1..2deef71 100644 --- a/api/lib/user_functions.php +++ b/api/lib/user_functions.php @@ -265,12 +265,12 @@ function update_provision_pw ( $username, $domain, $password ) function list_users ( $search = null ) { global $config; - $query = sprintf("SELECT CONCAT(username, '@', domain), displayname FROM %s ORDER BY username,domain", $config['hermes_users_table'] ); + $query = sprintf("SELECT CONCAT(username, '@', domain), displayname, username, domain FROM %s ORDER BY username,domain", $config['hermes_users_table'] ); if ( array_key_exists ( 'search', $_POST ) ) { $search = $_POST['search']; // TODO: Add some sanitation and input validation! - $query = sprintf("SELECT CONCAT(username, '@', domain) FROM %s WHERE CONCAT(username, '@', domain) LIKE '%%%s%%' ORDER BY username,domain", $config['hermes_users_table'], sql_clean( $search ) ); + $query = sprintf("SELECT CONCAT(username, '@', domain), displayname, username, domain FROM %s WHERE CONCAT(username, '@', domain) LIKE '%%%s%%' ORDER BY username,domain", $config['hermes_users_table'], sql_clean( $search ) ); } $result = sql_dbquery( $config['hermes_db'], $query ); @@ -278,7 +278,8 @@ function list_users ( $search = null ) $list = array(); while ( $row = @mysql_fetch_row( $result ) ) { - array_push( $list, array( "user" => $row[0], "displayname" => $row[1] ) ); + $online = is_online( $row[2], $row[3] ) ? online : offline; + array_push( $list, array( "user" => $row[0], "displayname" => $row[1], "status" => $online ) ); } return $list; print json_encode( array( 'response' => 'ok', 'list' => $list )); @@ -337,5 +338,30 @@ function get_userdata( $username, $domain ) return $user; } - + +function get_locations( $username, $domain ) +{ + global $config; + + $query = sprintf ("SELECT contact, UNIX_TIMESTAMP(expires) as expires, user_agent, socket FROM %s WHERE username = '%s' AND (domain = '%s' OR domain is NULL)", + $config['kamailio_location_table'], + sql_clean($username), + sql_clean($domain)); + + $result = sql_dbquery( $config['kamailio_db'], $query ); + if ( !$result ) return null; + $list = array(); + while ( $row = @mysql_fetch_row( $result ) ) + { + array_push( $list, array( "contact" => $row[0], "expires" => $row[1], "useragent" => $row[2] ) ); + } + return $list; +} + +function is_online( $username, $domain ) +{ + if ( get_locations( $username, $domain ) != null ) return true; + else return false; +} + ?> diff --git a/api/t/tests_common.pm b/api/t/tests_common.pm index ee6d3a5..4335632 100644 --- a/api/t/tests_common.pm +++ b/api/t/tests_common.pm @@ -41,8 +41,8 @@ BEGIN { %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); } -our $api_base = "http://10.0.2.5/hermes/api/"; -our $api_key = "4hErgw3QFJLeuXGW"; +our $api_base = "http://sippbx.hig.no/hermes/api/"; +our $api_key = "e065gBpUwqzY0a5o"; our $invalid_key = "invalidkey" . int(rand(255)); our $test_username = "testauth-" . int(rand(255)); diff --git a/api/t/user.t b/api/t/user.t index 6373c6c..21d675d 100644 --- a/api/t/user.t +++ b/api/t/user.t @@ -91,7 +91,7 @@ ok( $data->{'user'}->{'ha1'} =~ m/[a-z0-9]{16,}/, 'user/get ha1 hash generated') undef $data; $data = exec_apinode("user/gen_pw", undef); -ok( $data =~ m/[a-zA-Z0-9]{24}/, 'user/gen_pw format OK'); +ok( $data =~ m/[a-zA-Z0-9]{8}/, 'user/gen_pw format OK'); $temp = $data; undef $data; diff --git a/api/user.php b/api/user.php index 260ac35..e8099d9 100644 --- a/api/user.php +++ b/api/user.php @@ -101,6 +101,40 @@ token_auth(); else print json_encode ( array( 'response' => 'invalid') ); break; + + case "/location": + if ( array_key_exists('user', $_POST) || + ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))) + { + $username = ""; + $domain = ""; + if ( array_key_exists('username', $_POST) ) + { + $username = $_POST['username']; + $domain = $_POST['domain']; + } + else + { + $user = split_sipaddress($_POST['user']); + if ( !$user ) + { + print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') ); + break; + } + list ( $username, $domain ) = $user; + } + $location = get_locations( $username, $domain ); + if ( $location == null ) + { + print json_encode( array( 'response' => 'failed', 'cause' => 'offline' )); + break; + } + print json_encode( array( 'response' => 'ok', 'locations' => $location )); + } + else + print json_encode ( array( 'response' => 'invalid') ); + break; + case "/list": /* Simply list all users in user@domain format diff --git a/doc/api-nodes.txt b/doc/api-nodes.txt index 4215059..a3cafc9 100644 --- a/doc/api-nodes.txt +++ b/doc/api-nodes.txt @@ -250,6 +250,27 @@ user/get Returns 'failed' with 'cause' = 'nonexistant' if user does not exist.. Returns 'failed' with 'cause' = 'invalid' if SIP address is malformed. +user/location +------------------- + Required: + user=user@bar.bz * + + Description: + Return registered user agent locations for a given user, with + contact and user agent information. + + Return: + On success, returns the array 'locations', each element contianing: + 'contact' is the registration contact (the location) + 'useragent' is the UA string from the client + 'expires' is the expiration time of the registration, as unix timestamp + + Returns 'failed' with 'cause' = 'offline' if user is not registered ... + Returns 'failed' with 'cause' = 'invalid' if SIP address is malformed. + + TODO: + Add a return code for non-existant subscriber? + user/list ------------------- Optional: @@ -705,6 +726,11 @@ api/alias/list destination=foo@bar.bz e164=true TODO list: --------------------- +user/forward To set up permanent forwarding. Call forward is stored as a user + preference value in the kamailio database. Evample of diverting calls to + 'jonl@hig.no' to the number +4761135280': +insert into usr_preferences (username, domain, attribute, value, last_modified) values ( 'jonl', 'hig.no', 'cfu', '+4761135280', now()); + Some TODO's listed above ... Permissions! (user/permissions?user=...) diff --git a/guc-clients/listsipusers b/guc-clients/listsipusers index fccedb0..10cdb96 100755 --- a/guc-clients/listsipusers +++ b/guc-clients/listsipusers @@ -90,6 +90,7 @@ my @users = @$t; foreach my $u ( @users ) { printf("%-24s", $u->{'user'}); + printf("%-12s", $u->{'status'}); my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} }); if ( $alias_data->{'response'} eq 'ok' ) { -- 2.39.2