]> git.defcon.no Git - hermes/blob - alias.php
Added alias/list functionality! Also, moved some data around in lib/alias_functions...
[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 $list = array();
31 $dest_username = "";
32 $dest_domain = "";
33 $e164_only = false;
34 if ( array_key_exists( 'destination', $_GET) )
35 {
36 $tmp = split_sipaddress($_GET['destination']);
37 if ( !$tmp )
38 {
39 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
40 break;
41 }
42 list ( $dest_username, $dest_domain ) = $tmp;
43
44 if ( array_key_exists( 'e164', $_GET ) )
45 {
46 if ( strtolower($_GET['e164']) == "true" ) $e164_only = true;
47 $list = get_e164_alias( $dest_username, $dest_domain );
48 }
49 else if ( $dest_username && $dest_domain )
50 {
51 $list = get_aliases( $dest_username, $dest_domain );
52 }
53 }
54 else if ( array_key_exists( 'alias', $_GET) )
55 {
56 $tmp = split_sipaddress($_GET['alias']);
57 if ( !$tmp )
58 {
59 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
60 break;
61 }
62 list ( $alias_username, $alias_domain ) = $tmp;
63
64 $list = get_destination( $alias_username, $alias_domain );
65 }
66 else
67 $list = get_aliases( null, null );
68
69
70 print json_encode ( array( 'response' => 'ok', 'aliases' => $list ) );
71 break;
72 case "/add":
73 if ( array_key_exists( 'destination', $_GET)
74 && ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) ) )
75 {
76 $alias_username = "";
77 $alias_domain = "";
78 if ( array_key_exists('alias_username', $_GET) )
79 {
80 $alias_username = $_GET['alias_username'];
81 $alias_domain = $_GET['alias_domain'];
82 }
83 else
84 {
85 $alias = split_sipaddress($_GET['alias']);
86 if ( !$alias )
87 {
88 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
89 break;
90 }
91 list ( $alias_username, $alias_domain ) = $alias;
92 }
93
94 if ( !verify_sipadress($_GET['destination']) )
95 {
96 // TODO: Provide a better response..
97 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
98 break;
99 }
100 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
101 {
102 // TODO: Provide a better response..
103 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
104 break;
105 }
106
107 // TODO: Verify that alias does not collide with existing SIP-adress
108 if ( is_kamailio_domain( $alias_domain) && is_kamailio_subscriber($alias_username, $alias_domain) )
109 {
110 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The desired alias collides with an existing non-alias.' ));
111 break;
112 }
113
114 list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
115
116 if ( (!$dest_username)||(!$dest_domain))
117 {
118 // TODO: Provide a better response..
119 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
120 break;
121 }
122 if ( !is_kamailio_domain( $alias_domain ) )
123 {
124 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
125 break;
126 }
127 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
128 {
129 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
130 break;
131 }
132 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
133 {
134 $t = get_e164_alias( $dest_username, $dest_domain );
135 if ( $t )
136 {
137 print json_encode ( array(
138 'response' => 'failed',
139 'cause' => 'exists',
140 'detail' => 'User already has E164 number alias',
141 'alias' => $t['alias'] ));
142 break;
143 }
144 }
145 if ( alias_exists ( $alias_username, $alias_domain ) )
146 {
147 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
148 break;
149 }
150 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
151 {
152 print json_encode( array ('response' => 'ok',
153 'alias' => $alias_username . "@" . $alias_domain,
154 'destination' => $dest_username . "@" . $dest_domain ));
155 break;
156 }
157 print json_encode ( array ( 'response' => 'error' ));
158 break;
159 }
160 else
161 {
162 print json_encode ( array( 'response' => 'invalid') );
163 }
164 break;
165 case "/remove":
166 if ( ( array_key_exists( 'alias_username', $_GET) && array_key_exists( 'alias_domain', $_GET ) ) || array_key_exists('alias', $_GET) )
167 {
168 $alias_username = "";
169 $alias_domain = "";
170 if ( array_key_exists('alias_username', $_GET) )
171 {
172 $alias_username = $_GET['alias_username'];
173 $alias_domain = $_GET['alias_domain'];
174 }
175 else
176 {
177 $alias = split_sipaddress($_GET['alias']);
178 if ( !$alias )
179 {
180 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
181 break;
182 }
183 list ( $alias_username, $alias_domain ) = $alias;
184 }
185
186 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
187 {
188 // TODO: Provide a better response..
189 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
190 break;
191 }
192 if ( ! alias_exists ( $alias_username, $alias_domain ) )
193 {
194 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
195 break;
196 }
197 if ( remove_alias( $alias_username, $alias_domain ) )
198 {
199 print json_encode( array ('response' => 'ok',
200 'alias' => $alias_username . "@" . $alias_domain));
201 break;
202 }
203 print json_encode ( array ( 'response' => 'error' ));
204 break;
205 }
206 print json_encode ( array( 'response' => 'invalid') );
207 break;
208 default:
209 print json_encode ( array( 'response' => 'invalid') );
210 }
211 mysql_close( $config['sql_link'] );
212 ?>