]> git.defcon.no Git - hermes/blob - alias.php
Changed list node to provide more data. Added change_pw functionality/node. Changed...
[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 if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
105 {
106 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
107 break;
108 }
109
110 list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
111
112 if ( (!$dest_username)||(!$dest_domain))
113 {
114 // TODO: Provide a better response..
115 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
116 break;
117 }
118 if ( !is_kamailio_domain( $alias_domain ) )
119 {
120 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
121 break;
122 }
123 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
124 {
125 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
126 break;
127 }
128 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
129 {
130 $t = get_e164_alias( $dest_username, $dest_domain );
131 if ( $t )
132 {
133 print json_encode ( array(
134 'response' => 'failed',
135 'cause' => 'exists',
136 'detail' => 'User already has E164 number alias',
137 'alias' => $t ));
138 break;
139 }
140 }
141 if ( alias_exists ( $alias_username, $alias_domain ) )
142 {
143 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
144 break;
145 }
146 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
147 {
148 print json_encode( array ('response' => 'ok',
149 'alias' => $alias_username . "@" . $alias_domain,
150 'destination' => $dest_username . "@" . $dest_domain ));
151 break;
152 }
153 print json_encode ( array ( 'response' => 'error' ));
154 break;
155 }
156 else
157 {
158 print json_encode ( array( 'response' => 'invalid') );
159 }
160 break;
161 case "/remove":
162 if ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) )
163 {
164 $alias_username = "";
165 $alias_domain = "";
166 if ( array_key_exists('alias_username', $_GET) )
167 {
168 $alias_username = $_GET['alias_username'];
169 $alias_domain = $_GET['alias_domain'];
170 }
171 else
172 {
173 $alias = split_sipaddress($_GET['alias']);
174 if ( !$alias )
175 {
176 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
177 break;
178 }
179 list ( $alias_username, $alias_domain ) = $alias;
180 }
181
182 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
183 {
184 // TODO: Provide a better response..
185 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
186 break;
187 }
188 if ( ! alias_exists ( $alias_username, $alias_domain ) )
189 {
190 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
191 break;
192 }
193 if ( remove_alias( $alias_username, $alias_domain ) )
194 {
195 print json_encode( array ('response' => 'ok',
196 'alias' => $alias_username . "@" . $alias_domain));
197 break;
198 }
199 print json_encode ( array ( 'response' => 'error' ));
200 break;
201 }
202 print json_encode ( array( 'response' => 'invalid') );
203 break;
204 default:
205 print json_encode ( array( 'response' => 'invalid') );
206 }
207 mysql_close( $config['sql_link'] );
208 ?>