]> git.defcon.no Git - hermes/blob - api/domain.php
Added support for user-based authentication, resulting changes: Implemented auth...
[hermes] / api / domain.php
1 <?php
2 require_once('config.php');
3 require_once('lib/auth_base.php');
4 require_once('lib/common_functions.php');
5 require_once('lib/db_functions.php');
6 require_once('lib/domain_functions.php');
7
8 $config = get_config();
9
10 $config['sql_link'] = @mysql_connect(
11 $config['sql_server'],
12 $config['sql_username'],
13 $config['sql_password']
14 );
15 if ( !$config['sql_link'] )
16 {
17 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
18 exit;
19 }
20
21 token_auth();
22
23 //*************************************************************************************
24 switch ( $_SERVER['PATH_INFO'] )
25 {
26 case "/list":
27 // Very simple call: Provide a list of domains registered with kamailio.
28 $domains = get_domains();
29 if ( $domains == -1 )
30 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.'));
31 else
32 print json_encode ( array( 'response' => 'ok', 'list' => $domains ) );
33 break;
34 case "/get_servers":
35 if ( array_key_exists('domain', $_GET))
36 {
37 $domain = $_GET['domain'];
38 if ( !$domain || $domain == "" )
39 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
40
41 $servers = get_servers( $domain );
42 if ( !$servers )
43 {
44 print json_encode( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Servers lookup failed for domain '. $domain ) );
45 break;
46 }
47 $servers['domain'] = $domain;
48 print json_encode ( array ( 'response' => 'ok', 'servers' => $servers ) );
49 break;
50 }
51 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
52 break;
53
54 case "/set_servers":
55 if ( array_key_exists('domain', $_GET)
56 && array_key_exists('registrar', $_GET)
57 && array_key_exists('r_port', $_GET)
58 && array_key_exists('proxy', $_GET)
59 && array_key_exists('p_port', $_GET)
60 && array_key_exists('prov_url', $_GET))
61 {
62 $domain = $_GET['domain'];
63 $registrar = $_GET['registrar'];
64 $r_port = $_GET['r_port'];
65 $proxy = $_GET['proxy'];
66 $p_port = $_GET['p_port'];
67 $prov_url = $_GET['prov_url'];
68
69 if (!($domain && $registrar && $r_port && $proxy && $p_port && $prov_url ))
70 {
71 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters', 'detail' => 'One or more parameters NULL') );
72 break;
73 }
74
75 if ( ! ( is_numeric( $r_port ) && is_numeric( $p_port ) ) )
76 {
77 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters', 'detail' => 'One of the ports is not numeric.') );
78 break;
79 }
80
81 if ( ! set_servers ( $domain, $registrar, $r_port, $proxy, $p_port, $prov_url ) )
82 {
83 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.'));
84 break;
85 }
86
87 $servers = get_servers( $domain );
88 $servers['domain'] = $domain;
89 print json_encode ( array ( 'response' => 'ok', 'servers' => $servers ) );
90 break;
91 }
92 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
93 break;
94
95
96
97 default:
98 print json_encode ( array( 'response' => 'invalid') );
99 }
100 //*************************************************************************************
101 mysql_close( $config['sql_link'] );
102 ?>