]> git.defcon.no Git - hermes/blob - phone.php
Hermes is a framework for managing SIP-accounts, aliases and automatic phone-provisio...
[hermes] / 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( 'user', $_GET ) )
39 {
40 $user = split_sipaddress($_GET['user']);
41 if (! $user )
42 {
43 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Invalid SIP address') );
44 break;
45 }
46 list ( $username, $domain ) = $user;
47 $userdata = get_user_phones( $username, $domain );
48 if ( $userdata )
49 {
50 print json_encode( array( 'response' => 'ok', 'list' => $userdata ));
51 }
52 else print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'No results.'));
53
54 }
55 else
56 print json_encode ( array( 'response' => 'invalid') );
57 break;
58 case "/list":
59 // List all (distinct) phone MAC-adresses registered...
60 $search = null;
61 if ( array_key_exists('search', $_GET ) )
62 $search = $_GET['search'];
63
64 $phones = list_phones( $search );
65 print json_encode( array( 'response' => 'ok', 'list' => $phones ));
66 break;
67 case "/add":
68 // Add a MAC+user...
69 /*
70 Parameters:
71 mac The MAC-address of the phone to add an entry for
72 Either:
73 user A registered username on user@domain form (SIP address)
74 Or:
75 username A registered username, combines with:
76 domain A valid domain .. to form a registered user@domain combo :)
77
78 */
79 if ( array_key_exists('mac', $_GET ) &&
80 ( array_key_exists('user', $_GET) ||
81 ( array_key_exists('username', $_GET) && array_key_exists('domain', $_GET ))))
82 {
83 $username = "";
84 $domain = "";
85 if ( array_key_exists('username', $_GET) )
86 {
87 $username = $_GET['username'];
88 $domain = $_GET['domain'];
89 }
90 else
91 {
92 $user = split_sipaddress($_GET['user']);
93 if ( !$user )
94 {
95 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
96 break;
97 }
98 list ( $username, $domain ) = $user;
99 }
100 $mac = clean_mac($_GET['mac']);
101 if ( !$mac )
102 {
103 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
104 break;
105 }
106
107 if ( !is_provision_user ( $username, $domain ) )
108 {
109 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'User not registered.'));
110 break;
111 }
112 $phones = get_user_phones ( $username, $domain);
113 if ( $phones && in_array( $mac, $phones ) )
114 {
115 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'This phone and user combination is already configured..'));
116 break;
117 }
118 $res = add_phone_user ( $mac, $username, $domain );
119 if ( !$res )
120 {
121 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to add phone to database.'));
122 break;
123 }
124 else
125 {
126 print json_encode( array ( 'response' => 'ok', 'data' => array (
127 'mac' => $mac, 'username' => $username, 'domain' => $domain) ));
128 break;
129 }
130 break;
131 }
132 else
133 print json_encode ( array( 'response' => 'invalid') );
134 break;
135
136
137 case "/remove":
138 // Del a MAC+user...
139 /*
140 Parameters:
141 mac The MAC-address of the phone to add an entry for
142 Either:
143 user A registered username on user@domain form (SIP address)
144 Or:
145 username A registered username, combines with:
146 domain A valid domain .. to form a registered user@domain combo :)
147
148 */
149 if ( array_key_exists('mac', $_GET ) &&
150 ( array_key_exists('user', $_GET) ||
151 ( array_key_exists('username', $_GET) && array_key_exists('domain', $_GET ))))
152 {
153 $username = "";
154 $domain = "";
155 if ( array_key_exists('username', $_GET) )
156 {
157 $username = $_GET['username'];
158 $domain = $_GET['domain'];
159 }
160 else
161 {
162 $user = split_sipaddress($_GET['user']);
163 if ( !$user )
164 {
165 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
166 break;
167 }
168 list ( $username, $domain ) = $user;
169 }
170 $mac = clean_mac($_GET['mac']);
171 if ( !$mac )
172 {
173 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'No valid MAC address given.') );
174 break;
175 }
176
177 $phones = get_user_phones ( $username, $domain);
178 if ( ! $phones || !in_array( $mac, $phones ) )
179 {
180 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to locate requested combination'));
181 break;
182 }
183 $res = delete_phone_user ( $mac, $username, $domain );
184 if ( !$res )
185 {
186 print json_encode( array ( 'response' => 'failed', 'cause' =>'dbfail', 'detail' => 'Failed to remove phone from database.'));
187 break;
188 }
189 else
190 {
191 print json_encode( array ( 'response' => 'ok' ));
192 break;
193 }
194 break;
195 }
196 else
197 print json_encode ( array( 'response' => 'invalid') );
198 break;
199
200
201 default:
202 print json_encode ( array( 'response' => 'invalid') );
203 }
204 mysql_close( $config['sql_link'] );
205 ?>