]>
git.defcon.no Git - hermes/blob - api/phone.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');
9 $config = get_config();
11 $config['sql_link'] = @mysql_connect
(
12 $config['sql_server'],
13 $config['sql_username'],
14 $config['sql_password']
16 if ( !$config['sql_link'] )
18 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
23 //*************************************************************************************
24 switch ( $_SERVER['PATH_INFO'] )
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) )
33 $relations = get_phone_users ( $mac );
36 print json_encode( array( 'response' => 'ok', 'list' => $relations ));
38 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
40 else if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) ||
array_key_exists('user', $_POST) )
44 if ( array_key_exists('username', $_POST) )
46 $username = $_POST['username'];
47 $domain = $_POST['domain'];
51 $user = split_sipaddress($_POST['user']);
54 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
57 list ( $username, $domain ) = $user;
60 $userdata = get_user_phones( $username, $domain );
63 print json_encode( array( 'response' => 'ok', 'list' => $userdata ));
65 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
69 print json_encode ( array( 'response' => 'invalid') );
72 // List all (distinct) phone MAC-adresses registered...
74 if ( array_key_exists('search', $_POST ) )
75 $search = $_POST['search'];
77 $phones = list_phones( $search );
78 print json_encode( array( 'response' => 'ok', 'list' => $phones ));
84 mac The MAC-address of the phone to add an entry for
86 user A registered username on user@domain form (SIP address)
88 username A registered username, combines with:
89 domain A valid domain .. to form a registered user@domain combo :)
92 if ( array_key_exists('mac', $_POST ) &&
93 ( array_key_exists('user', $_POST) ||
94 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
98 if ( array_key_exists('username', $_POST) )
100 $username = $_POST['username'];
101 $domain = $_POST['domain'];
105 $user = split_sipaddress($_POST['user']);
108 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
111 list ( $username, $domain ) = $user;
113 $mac = clean_mac($_POST['mac']);
116 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
120 if ( !is_provision_user ( $username, $domain ) )
122 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'User not registered.'));
125 $phones = get_user_phones ( $username, $domain);
126 if ( $phones && in_array( $mac, $phones ) )
128 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'This phone and user combination is already configured..'));
131 $res = add_phone_user ( $mac, $username, $domain );
134 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to add phone to database.'));
139 print json_encode( array ( 'response' => 'ok', 'mac' => $mac, 'username' => $username, 'domain' => $domain) );
145 print json_encode ( array( 'response' => 'invalid') );
153 mac The MAC-address of the phone to add an entry for
155 user A registered username on user@domain form (SIP address)
157 username A registered username, combines with:
158 domain A valid domain .. to form a registered user@domain combo :)
161 if ( array_key_exists('mac', $_POST ) &&
162 ( array_key_exists('user', $_POST) ||
163 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
167 if ( array_key_exists('username', $_POST) )
169 $username = $_POST['username'];
170 $domain = $_POST['domain'];
174 $user = split_sipaddress($_POST['user']);
177 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
180 list ( $username, $domain ) = $user;
182 $mac = clean_mac($_POST['mac']);
185 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
189 $phones = get_user_phones ( $username, $domain);
190 if ( ! $phones ||
!in_array( $mac, $phones ) )
192 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to locate requested combination'));
195 $res = delete_phone_user ( $mac, $username, $domain );
198 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to remove phone from database.'));
203 print json_encode( array ( 'response' => 'ok', 'mac' => $mac, 'username' => $username, 'domain' => $domain ));
209 print json_encode ( array( 'response' => 'invalid') );
214 print json_encode ( array( 'response' => 'invalid') );
216 mysql_close( $config['sql_link'] );