]>
git.defcon.no Git - hermes/blob - api/phone.php
3 # Copyright (c) 2012, Gjøvik University College
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Gjøvik University College nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 require_once('config.php');
29 require_once('lib/auth_base.php');
30 require_once('lib/user_functions.php');
31 require_once('lib/common_functions.php');
32 require_once('lib/db_functions.php');
33 require_once('lib/phone_functions.php');
35 $config = get_config();
37 $config['sql_link'] = @mysql_connect
(
38 $config['sql_server'],
39 $config['sql_username'],
40 $config['sql_password']
42 if ( !$config['sql_link'] )
44 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
49 //*************************************************************************************
50 switch ( $_SERVER['PATH_INFO'] )
53 // Required GET parameters:
54 // user: authentication username, SIP-username without domain component
55 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
56 if ( array_key_exists( 'mac', $_POST) )
59 $relations = get_phone_users ( $mac );
62 print json_encode( array( 'response' => 'ok', 'list' => $relations ));
64 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
66 else if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) ||
array_key_exists('user', $_POST) )
70 if ( array_key_exists('username', $_POST) )
72 $username = $_POST['username'];
73 $domain = $_POST['domain'];
77 $user = split_sipaddress($_POST['user']);
80 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
83 list ( $username, $domain ) = $user;
86 $userdata = get_user_phones( $username, $domain );
89 print json_encode( array( 'response' => 'ok', 'list' => $userdata ));
91 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
95 print json_encode ( array( 'response' => 'invalid') );
98 // List all (distinct) phone MAC-adresses registered...
100 if ( array_key_exists('search', $_POST ) )
101 $search = $_POST['search'];
103 $phones = list_phones( $search );
104 print json_encode( array( 'response' => 'ok', 'list' => $phones ));
110 mac The MAC-address of the phone to add an entry for
112 user A registered username on user@domain form (SIP address)
114 username A registered username, combines with:
115 domain A valid domain .. to form a registered user@domain combo :)
118 if ( array_key_exists('mac', $_POST ) &&
119 ( array_key_exists('user', $_POST) ||
120 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
124 if ( array_key_exists('username', $_POST) )
126 $username = $_POST['username'];
127 $domain = $_POST['domain'];
131 $user = split_sipaddress($_POST['user']);
134 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
137 list ( $username, $domain ) = $user;
139 $mac = clean_mac($_POST['mac']);
142 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
146 if ( !is_provision_user ( $username, $domain ) )
148 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'User not registered.'));
151 $phones = get_user_phones ( $username, $domain);
152 if ( $phones && in_array( $mac, $phones ) )
154 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'This phone and user combination is already configured..'));
157 $res = add_phone_user ( $mac, $username, $domain );
160 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to add phone to database.'));
165 print json_encode( array ( 'response' => 'ok', 'mac' => $mac, 'username' => $username, 'domain' => $domain) );
171 print json_encode ( array( 'response' => 'invalid') );
179 mac The MAC-address of the phone to add an entry for
181 user A registered username on user@domain form (SIP address)
183 username A registered username, combines with:
184 domain A valid domain .. to form a registered user@domain combo :)
187 if ( array_key_exists('mac', $_POST ) &&
188 ( array_key_exists('user', $_POST) ||
189 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST ))))
193 if ( array_key_exists('username', $_POST) )
195 $username = $_POST['username'];
196 $domain = $_POST['domain'];
200 $user = split_sipaddress($_POST['user']);
203 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
206 list ( $username, $domain ) = $user;
208 $mac = clean_mac($_POST['mac']);
211 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
215 $phones = get_user_phones ( $username, $domain);
216 if ( ! $phones ||
!in_array( $mac, $phones ) )
218 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to locate requested combination'));
221 $res = delete_phone_user ( $mac, $username, $domain );
224 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to remove phone from database.'));
229 print json_encode( array ( 'response' => 'ok', 'mac' => $mac, 'username' => $username, 'domain' => $domain ));
235 print json_encode ( array( 'response' => 'invalid') );
240 print json_encode ( array( 'response' => 'invalid') );
242 mysql_close( $config['sql_link'] );