--- /dev/null
+<?php
+require_once('config.php');
+require_once('lib/common_functions.php');
+require_once('lib/db_functions.php');
+require_once('lib/domain_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":
+ // Very simple call: Provide a list of domains registered with kamailio.
+ $domains = get_domains();
+ if ( $domains == -1 )
+ print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.'));
+ else
+ print json_encode ( array( 'response' => 'ok', 'list' => $domains ) );
+ break;
+ case "/get_servers":
+ if ( array_key_exists('domain', $_GET))
+ {
+ $domain = $_GET['domain'];
+ if ( !$domain || $domain == "" )
+ print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
+
+ $servers = get_servers( $domain );
+ if ( !$servers )
+ {
+ print json_encode( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Servers lookup failed for domain '. $domain ) );
+ break;
+ }
+ $servers['domain'] = $domain;
+ print json_encode ( array ( 'response' => 'ok', 'servers' => $servers ) );
+ break;
+ }
+ print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
+ break;
+
+ case "/set_servers":
+ if ( array_key_exists('domain', $_GET)
+ && array_key_exists('registrar', $_GET)
+ && array_key_exists('r_port', $_GET)
+ && array_key_exists('proxy', $_GET)
+ && array_key_exists('p_port', $_GET)
+ && array_key_exists('prov_url', $_GET))
+ {
+ $domain = $_GET['domain'];
+ $registrar = $_GET['registrar'];
+ $r_port = $_GET['r_port'];
+ $proxy = $_GET['proxy'];
+ $p_port = $_GET['p_port'];
+ $prov_url = $_GET['prov_url'];
+
+ if (!($domain && $registrar && $r_port && $proxy && $p_port && $prov_url ))
+ {
+ print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters', 'detail' => 'One or more parameters NULL') );
+ break;
+ }
+
+ if ( ! ( is_numeric( $r_port ) && is_numeric( $p_port ) ) )
+ {
+ print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters', 'detail' => 'One of the ports is not numeric.') );
+ break;
+ }
+
+ if ( ! set_servers ( $domain, $registrar, $r_port, $proxy, $p_port, $prov_url ) )
+ {
+ print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.'));
+ break;
+ }
+
+ $servers = get_servers( $domain );
+ $servers['domain'] = $domain;
+ print json_encode ( array ( 'response' => 'ok', 'servers' => $servers ) );
+ break;
+ }
+ print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
+ break;
+
+
+
+ default:
+ print json_encode ( array( 'response' => 'invalid') );
+ }
+//*************************************************************************************
+mysql_close( $config['sql_link'] );
+?>
--- /dev/null
+<?php
+require_once('config.php');
+require_once('lib/common_functions.php');
+require_once('lib/db_functions.php');
+
+function get_domains()
+{
+ global $config;
+ $query = sprintf("SELECT domain FROM %s",
+ $config['kamailio_domain_table']);
+
+ $result = sql_dbquery( $config['kamailio_db'], $query);
+ if ( ! $result ) return -1;
+
+ $domains = array();
+ while ( $row = mysql_fetch_row($result) ) { array_push($domains, $row[0]); }
+ return $domains;
+}
+
+function set_servers ( $domain, $registrar, $r_port, $proxy, $p_port, $prov_url )
+{
+ global $config;
+
+ $query = "";
+
+ $prev_servers = get_servers( $domain );
+ if ( $prev_servers )
+ {
+ $query = sprintf("UPDATE %s SET
+ registrar = '%s', r_port = %d,
+ proxy = '%s', p_port = %d,
+ prov_url = '%s'
+ WHERE domain = '%s'",
+ $config['provision_servers_table'],
+ sql_clean($registrar),
+ $r_port,
+ sql_clean($proxy),
+ $p_port,
+ sql_clean($prov_url),
+ sql_clean($domain)
+ );
+ }
+ else
+ {
+ $query = sprintf("INSERT INTO %s
+ (domain, registrar, r_port, proxy, p_port, prov_url)
+ VALUES ('%s', '%s', %d, '%s', %d, '%s')",
+ $config['provision_servers_table'],
+ sql_clean($domain),
+ sql_clean($registrar),
+ $r_port,
+ sql_clean($proxy),
+ $p_port,
+ sql_clean($prov_url)
+ );
+
+ }
+ return sql_dbexec( $config['provision_db'], $query );
+ return false;
+}
+?>