]> git.defcon.no Git - hermes/blob - alias.php
Several changes: Parameter-changes to several nodes, stubs added, verification of...
[hermes] / alias.php
1 <?php
2 require_once('config.php');
3 require_once('lib/user_functions.php');
4 require_once('lib/number_functions.php');
5 require_once('lib/common_functions.php');
6 require_once('lib/db_functions.php');
7 require_once('lib/alias_functions.php');
8
9 $config = get_config();
10
11 $config['sql_link'] = @mysql_connect(
12 $config['sql_server'],
13 $config['sql_username'],
14 $config['sql_password']
15 );
16 if ( !$config['sql_link'] )
17 {
18 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
19 exit;
20 }
21
22 //*************************************************************************************
23 switch ( $_SERVER['PATH_INFO'] )
24 {
25 case "/list":
26 /*
27 Required parameters should be...
28 destination
29 */
30 $dest_username = "";
31 $dest_domain = "";
32 $e164_only = false;
33 if ( array_key_exists( 'destination', $_GET) )
34 {
35 $tmp = split_sipaddress($_GET['destination']);
36 if ( !$tmp )
37 {
38 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
39 break;
40 }
41 list ( $dest_username, $dest_domain ) = $tmp;
42
43 if ( array_key_exists( 'e164', $_GET ) )
44 {
45 if ( strtolower($_GET['e164']) == "true" ) $e164_only = true;
46 }
47 // TODO: Well. Code. This is a stub
48
49 print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) );
50 }
51 else if ( array_key_exists( 'alias', $_GET) )
52 {
53 $tmp = split_sipaddress($_GET['alias']);
54 if ( !$tmp )
55 {
56 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
57 break;
58 }
59 list ( $alias_username, $alias_domain ) = $tmp;
60
61 // TODO: Well. Code. This is a stub
62
63 print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) );
64 }
65 else
66 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
67 break;
68 case "/add":
69 if ( array_key_exists( 'destination', $_GET)
70 && ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) ) )
71 {
72 $alias_username = "";
73 $alias_domain = "";
74 if ( array_key_exists('alias_username', $_GET) )
75 {
76 $alias_username = $_GET['alias_username'];
77 $alias_domain = $_GET['alias_domain'];
78 }
79 else
80 {
81 $alias = split_sipaddress($_GET['alias']);
82 if ( !$alias )
83 {
84 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
85 break;
86 }
87 list ( $alias_username, $alias_domain ) = $alias;
88 }
89
90 if ( !verify_sipadress($_GET['destination']) )
91 {
92 // TODO: Provide a better response..
93 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
94 break;
95 }
96 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
97 {
98 // TODO: Provide a better response..
99 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
100 break;
101 }
102
103 // TODO: Verify that alias does not collide with existing SIP-adress
104
105 list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
106
107 if ( (!$dest_username)||(!$dest_domain))
108 {
109 // TODO: Provide a better response..
110 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
111 break;
112 }
113 if ( !is_kamailio_domain( $alias_domain ) )
114 {
115 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
116 break;
117 }
118 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
119 {
120 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
121 break;
122 }
123 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
124 {
125 $t = get_e164_alias( $dest_username, $dest_domain );
126 if ( $t )
127 {
128 print json_encode ( array(
129 'response' => 'failed',
130 'cause' => 'exists',
131 'detail' => 'User already has E164 number alias',
132 'alias' => $t ));
133 break;
134 }
135 }
136 if ( alias_exists ( $alias_username, $alias_domain ) )
137 {
138 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
139 break;
140 }
141 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
142 {
143 print json_encode( array ('response' => 'ok',
144 'alias' => $alias_username . "@" . $alias_domain,
145 'destination' => $dest_username . "@" . $dest_domain ));
146 break;
147 }
148 print json_encode ( array ( 'response' => 'error' ));
149 break;
150 }
151 else
152 {
153 print json_encode ( array( 'response' => 'invalid') );
154 }
155 break;
156 case "/remove":
157 if ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) )
158 {
159 $alias_username = "";
160 $alias_domain = "";
161 if ( array_key_exists('alias_username', $_GET) )
162 {
163 $alias_username = $_GET['alias_username'];
164 $alias_domain = $_GET['alias_domain'];
165 }
166 else
167 {
168 $alias = split_sipaddress($_GET['alias']);
169 if ( !$alias )
170 {
171 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
172 break;
173 }
174 list ( $alias_username, $alias_domain ) = $alias;
175 }
176
177 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
178 {
179 // TODO: Provide a better response..
180 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
181 break;
182 }
183 if ( ! alias_exists ( $alias_username, $alias_domain ) )
184 {
185 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
186 break;
187 }
188 if ( remove_alias( $alias_username, $alias_domain ) )
189 {
190 print json_encode( array ('response' => 'ok',
191 'alias' => $alias_username . "@" . $alias_domain));
192 break;
193 }
194 print json_encode ( array ( 'response' => 'error' ));
195 break;
196 }
197 print json_encode ( array( 'response' => 'invalid') );
198 break;
199 default:
200 print json_encode ( array( 'response' => 'invalid') );
201 }
202 mysql_close( $config['sql_link'] );
203 ?>