]> git.defcon.no Git - hermes/blob - api/domain.php
First stab at a naive permissions-tool
[hermes] / api / domain.php
1 <?php
2 /*
3 # Copyright (c) 2012, Gjøvik University College
4 # All rights reserved.
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Gjøvik University College nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 require_once('config.php');
29 require_once('lib/auth_base.php');
30 require_once('lib/common_functions.php');
31 require_once('lib/db_functions.php');
32 require_once('lib/domain_functions.php');
33
34 $config = get_config();
35
36 $config['sql_link'] = @mysql_connect(
37 $config['sql_server'],
38 $config['sql_username'],
39 $config['sql_password']
40 );
41 if ( !$config['sql_link'] )
42 {
43 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
44 exit;
45 }
46
47 token_auth();
48
49 //*************************************************************************************
50 switch ( $_SERVER['PATH_INFO'] )
51 {
52 case "/list":
53 // Very simple call: Provide a list of domains registered with kamailio.
54 $domains = get_domains();
55 if ( $domains == -1 )
56 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.'));
57 else
58 print json_encode ( array( 'response' => 'ok', 'list' => $domains ) );
59 break;
60 case "/get_servers":
61 if ( array_key_exists('domain', $_POST))
62 {
63 $domain = $_POST['domain'];
64 if ( !$domain || $domain == "" )
65 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
66
67 $servers = get_servers( $domain );
68 if ( !$servers )
69 {
70 print json_encode( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Servers lookup failed for domain '. $domain ) );
71 break;
72 }
73 $servers['domain'] = $domain;
74 print json_encode ( array ( 'response' => 'ok', 'servers' => $servers ) );
75 break;
76 }
77 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
78 break;
79
80 case "/set_servers":
81 if ( array_key_exists('domain', $_POST)
82 && array_key_exists('registrar', $_POST)
83 && array_key_exists('r_port', $_POST)
84 && array_key_exists('proxy', $_POST)
85 && array_key_exists('p_port', $_POST)
86 && array_key_exists('prov_url', $_POST))
87 {
88 $domain = $_POST['domain'];
89 $registrar = $_POST['registrar'];
90 $r_port = $_POST['r_port'];
91 $proxy = $_POST['proxy'];
92 $p_port = $_POST['p_port'];
93 $prov_url = $_POST['prov_url'];
94
95 if (!($domain && $registrar && $r_port && $proxy && $p_port && $prov_url ))
96 {
97 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters', 'detail' => 'One or more parameters NULL') );
98 break;
99 }
100
101 if ( ! ( is_numeric( $r_port ) && is_numeric( $p_port ) ) )
102 {
103 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters', 'detail' => 'One of the ports is not numeric.') );
104 break;
105 }
106
107 if ( ! set_servers ( $domain, $registrar, $r_port, $proxy, $p_port, $prov_url ) )
108 {
109 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.'));
110 break;
111 }
112
113 $servers = get_servers( $domain );
114 $servers['domain'] = $domain;
115 print json_encode ( array ( 'response' => 'ok', 'servers' => $servers ) );
116 break;
117 }
118 print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters') );
119 break;
120
121
122
123 default:
124 print json_encode ( array( 'response' => 'invalid') );
125 }
126 //*************************************************************************************
127 mysql_close( $config['sql_link'] );
128 ?>