]>
git.defcon.no Git - hermes/blob - api/phone.php
12ae05702da72152eda3fb9fc69acecb79d4ab37
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', 'data' => array (
140 'mac' => $mac, 'username' => $username, 'domain' => $domain) ));
146 print json_encode ( array( 'response' => 'invalid') );
154 mac The MAC-address of the phone to add an entry for
156 user A registered username on user@domain form (SIP address)
158 username A registered username, combines with:
159 domain A valid domain .. to form a registered user@domain combo :)
162 if ( array_key_exists('mac', $_POST ) &&
163 ( array_key_exists('user', $_POST) ||
164 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
168 if ( array_key_exists('username', $_POST) )
170 $username = $_POST['username'];
171 $domain = $_POST['domain'];
175 $user = split_sipaddress($_POST['user']);
178 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
181 list ( $username, $domain ) = $user;
183 $mac = clean_mac($_POST['mac']);
186 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
190 $phones = get_user_phones ( $username, $domain);
191 if ( ! $phones ||
!in_array( $mac, $phones ) )
193 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to locate requested combination'));
196 $res = delete_phone_user ( $mac, $username, $domain );
199 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to remove phone from database.'));
204 print json_encode( array ( 'response' => 'ok' ));
210 print json_encode ( array( 'response' => 'invalid') );
215 print json_encode ( array( 'response' => 'invalid') );
217 mysql_close( $config['sql_link'] );