]> git.defcon.no Git - hermes/blob - api/phone.php
First stab at auth. Flow-changes to make things sort'a work
[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', $_GET) )
31 {
32 $mac = $_GET['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', $_GET) && array_key_exists( 'domain', $_GET ) ) || array_key_exists('user', $_GET) )
41 {
42 $username = "";
43 $domain = "";
44 if ( array_key_exists('username', $_GET) )
45 {
46 $username = $_GET['username'];
47 $domain = $_GET['domain'];
48 }
49 else
50 {
51 $user = split_sipaddress($_GET['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', $_GET ) )
75 $search = $_GET['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', $_GET ) &&
93 ( array_key_exists('user', $_GET) ||
94 ( array_key_exists('username', $_GET) && array_key_exists('domain', $_GET ))))
95 {
96 $username = "";
97 $domain = "";
98 if ( array_key_exists('username', $_GET) )
99 {
100 $username = $_GET['username'];
101 $domain = $_GET['domain'];
102 }
103 else
104 {
105 $user = split_sipaddress($_GET['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($_GET['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', 'data' => array (
140 'mac' => $mac, 'username' => $username, 'domain' => $domain) ));
141 break;
142 }
143 break;
144 }
145 else
146 print json_encode ( array( 'response' => 'invalid') );
147 break;
148
149
150 case "/remove":
151 // Del a MAC+user...
152 /*
153 Parameters:
154 mac The MAC-address of the phone to add an entry for
155 Either:
156 user A registered username on user@domain form (SIP address)
157 Or:
158 username A registered username, combines with:
159 domain A valid domain .. to form a registered user@domain combo :)
160
161 */
162 if ( array_key_exists('mac', $_GET ) &&
163 ( array_key_exists('user', $_GET) ||
164 ( array_key_exists('username', $_GET) && array_key_exists('domain', $_GET ))))
165 {
166 $username = "";
167 $domain = "";
168 if ( array_key_exists('username', $_GET) )
169 {
170 $username = $_GET['username'];
171 $domain = $_GET['domain'];
172 }
173 else
174 {
175 $user = split_sipaddress($_GET['user']);
176 if ( !$user )
177 {
178 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
179 break;
180 }
181 list ( $username, $domain ) = $user;
182 }
183 $mac = clean_mac($_GET['mac']);
184 if ( !$mac )
185 {
186 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
187 break;
188 }
189
190 $phones = get_user_phones ( $username, $domain);
191 if ( ! $phones || !in_array( $mac, $phones ) )
192 {
193 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to locate requested combination'));
194 break;
195 }
196 $res = delete_phone_user ( $mac, $username, $domain );
197 if ( !$res )
198 {
199 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to remove phone from database.'));
200 break;
201 }
202 else
203 {
204 print json_encode( array ( 'response' => 'ok' ));
205 break;
206 }
207 break;
208 }
209 else
210 print json_encode ( array( 'response' => 'invalid') );
211 break;
212
213
214 default:
215 print json_encode ( array( 'response' => 'invalid') );
216 }
217 mysql_close( $config['sql_link'] );
218 ?>