]> git.defcon.no Git - hermes/blob - api/phone.php
Changed database-references, 'provision' is now called 'hermes', reflects that this...
[hermes] / api / phone.php
1 <?php
2 require_once('config.php');
3 require_once('lib/auth_base.php');
4 require_once('lib/user_functions.php');
5 require_once('lib/common_functions.php');
6 require_once('lib/db_functions.php');
7 require_once('lib/phone_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 token_auth();
22
23 //*************************************************************************************
24 switch ( $_SERVER['PATH_INFO'] )
25 {
26 case "/get":
27 // Required GET parameters:
28 // user: authentication username, SIP-username without domain component
29 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
30 if ( array_key_exists( 'mac', $_POST) )
31 {
32 $mac = $_POST['mac'];
33 $relations = get_phone_users ( $mac );
34 if ( $relations )
35 {
36 print json_encode( array( 'response' => 'ok', 'list' => $relations ));
37 }
38 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
39 }
40 else if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) || array_key_exists('user', $_POST) )
41 {
42 $username = "";
43 $domain = "";
44 if ( array_key_exists('username', $_POST) )
45 {
46 $username = $_POST['username'];
47 $domain = $_POST['domain'];
48 }
49 else
50 {
51 $user = split_sipaddress($_POST['user']);
52 if ( !$user )
53 {
54 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
55 break;
56 }
57 list ( $username, $domain ) = $user;
58 }
59
60 $userdata = get_user_phones( $username, $domain );
61 if ( $userdata )
62 {
63 print json_encode( array( 'response' => 'ok', 'list' => $userdata ));
64 }
65 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
66
67 }
68 else
69 print json_encode ( array( 'response' => 'invalid') );
70 break;
71 case "/list":
72 // List all (distinct) phone MAC-adresses registered...
73 $search = null;
74 if ( array_key_exists('search', $_POST ) )
75 $search = $_POST['search'];
76
77 $phones = list_phones( $search );
78 print json_encode( array( 'response' => 'ok', 'list' => $phones ));
79 break;
80 case "/add":
81 // Add a MAC+user...
82 /*
83 Parameters:
84 mac The MAC-address of the phone to add an entry for
85 Either:
86 user A registered username on user@domain form (SIP address)
87 Or:
88 username A registered username, combines with:
89 domain A valid domain .. to form a registered user@domain combo :)
90
91 */
92 if ( array_key_exists('mac', $_POST ) &&
93 ( array_key_exists('user', $_POST) ||
94 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
95 {
96 $username = "";
97 $domain = "";
98 if ( array_key_exists('username', $_POST) )
99 {
100 $username = $_POST['username'];
101 $domain = $_POST['domain'];
102 }
103 else
104 {
105 $user = split_sipaddress($_POST['user']);
106 if ( !$user )
107 {
108 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
109 break;
110 }
111 list ( $username, $domain ) = $user;
112 }
113 $mac = clean_mac($_POST['mac']);
114 if ( !$mac )
115 {
116 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
117 break;
118 }
119
120 if ( !is_provision_user ( $username, $domain ) )
121 {
122 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'User not registered.'));
123 break;
124 }
125 $phones = get_user_phones ( $username, $domain);
126 if ( $phones && in_array( $mac, $phones ) )
127 {
128 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'This phone and user combination is already configured..'));
129 break;
130 }
131 $res = add_phone_user ( $mac, $username, $domain );
132 if ( !$res )
133 {
134 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to add phone to database.'));
135 break;
136 }
137 else
138 {
139 print json_encode( array ( 'response' => 'ok', 'mac' => $mac, 'username' => $username, 'domain' => $domain) );
140 break;
141 }
142 break;
143 }
144 else
145 print json_encode ( array( 'response' => 'invalid') );
146 break;
147
148
149 case "/remove":
150 // Del a MAC+user...
151 /*
152 Parameters:
153 mac The MAC-address of the phone to add an entry for
154 Either:
155 user A registered username on user@domain form (SIP address)
156 Or:
157 username A registered username, combines with:
158 domain A valid domain .. to form a registered user@domain combo :)
159
160 */
161 if ( array_key_exists('mac', $_POST ) &&
162 ( array_key_exists('user', $_POST) ||
163 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
164 {
165 $username = "";
166 $domain = "";
167 if ( array_key_exists('username', $_POST) )
168 {
169 $username = $_POST['username'];
170 $domain = $_POST['domain'];
171 }
172 else
173 {
174 $user = split_sipaddress($_POST['user']);
175 if ( !$user )
176 {
177 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
178 break;
179 }
180 list ( $username, $domain ) = $user;
181 }
182 $mac = clean_mac($_POST['mac']);
183 if ( !$mac )
184 {
185 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
186 break;
187 }
188
189 $phones = get_user_phones ( $username, $domain);
190 if ( ! $phones || !in_array( $mac, $phones ) )
191 {
192 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to locate requested combination'));
193 break;
194 }
195 $res = delete_phone_user ( $mac, $username, $domain );
196 if ( !$res )
197 {
198 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to remove phone from database.'));
199 break;
200 }
201 else
202 {
203 print json_encode( array ( 'response' => 'ok', 'mac' => $mac, 'username' => $username, 'domain' => $domain ));
204 break;
205 }
206 break;
207 }
208 else
209 print json_encode ( array( 'response' => 'invalid') );
210 break;
211
212
213 default:
214 print json_encode ( array( 'response' => 'invalid') );
215 }
216 mysql_close( $config['sql_link'] );
217 ?>