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