]>
git.defcon.no Git - hermes/blob - user.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');
8 $config = get_config();
10 $config['sql_link'] = @mysql_connect
(
11 $config['sql_server'],
12 $config['sql_username'],
13 $config['sql_password']
15 if ( !$config['sql_link'] )
17 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
21 //*************************************************************************************
22 switch ( $_SERVER['PATH_INFO'] )
25 // Required GET parameters:
26 // user: authentication username, SIP-username without domain component
27 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
29 if ( array_key_exists('user', $_GET) ||
30 ( array_key_exists('username', $_GET) && array_key_exists('domain', $_GET )))
34 if ( array_key_exists('username', $_GET) )
36 $username = $_GET['username'];
37 $domain = $_GET['domain'];
41 $user = split_sipaddress($_GET['user']);
44 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
47 list ( $username, $domain ) = $user;
50 // Now, do funky stuff.
52 Test if user exists in both 'kamailio.subscribers' and 'provision.users'
53 * Return 'response' => 'ok', 'type' => 'local', 'user' => complete user object.
54 Test if user exists in 'provision.user' only
55 * Return 'response' => 'ok', 'type' => 'remote', 'user' => complete user object.
56 If user does is neither local nor remote
57 * Return 'response' => 'failed' with 'cause' => 'nonexistant'
58 On failure, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
62 $userdata = get_userdata( $username, $domain );
65 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
69 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Request for user ' . $username . '@' . $domain . ' failed.'));
73 print json_encode ( array( 'response' => 'invalid') );
77 Simply list all users in user@domain format
78 Perform a search operation if 'search' exists as a GET-parameter
79 * The search should try to do a "smart search" on SIP-usernames:
80 * Try to search with names in in username@domain format
81 * Do the search with wildcards before and after input text.
82 * The search must be done in the provisioning tables, to be able
83 to match non-local users.
84 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
87 if ( array_key_exists ( 'search', $_GET ) )
88 $search = $_GET['search']; // TODO: Add some sanitation and input validation!
89 $list = list_users( $search );
90 print json_encode( array( 'response' => 'ok', 'list' => $list ));
95 Required parameters should be...
96 ( username & domain ) | user
100 Verify that domain is local (lookup in the 'kamailio.domain' table.
101 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and provision.users?))
102 * Autocreate password
103 * Add username, domain, email and created password to the 'kamailio.subscriber' table
104 * Get the registrar+port, proxy+port from the 'provision.servers' table.
105 * standard dialplan from configuration.
106 * Add to the 'provision.users' table:
108 password -> generated password
109 displayname -> displayname
111 registrar -> provision.servers.registrar
112 r_port -> provision.servers.r_port
113 proxy -> provision.servers.proxy
114 p_port -> provision.servers.p_port
116 dialplan -> standard dialplan
118 * Return 'response' => 'ok' with a full user object in JSON format.
119 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
122 // Test required parameters:
124 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) ) ||
array_key_exists('user', $_GET) )
125 && array_key_exists( 'displayname', $_GET )
126 && array_key_exists( 'email', $_GET ) )
130 if ( array_key_exists('username', $_GET) )
132 $username = $_GET['username'];
133 $domain = $_GET['domain'];
137 $user = split_sipaddress($_GET['user']);
140 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
143 list ( $username, $domain ) = $user;
146 $password = generate_password();
147 $displayname = $_GET['displayname'];
148 $email = $_GET['email'];
150 if ( !is_kamailio_domain( $domain ) )
152 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
156 $servers = get_servers( $domain );
159 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
162 $registrar = $servers['registrar'];
163 $r_port = $servers['r_port'];
164 $proxy = $servers['proxy'];
165 $p_port = $servers['p_port'];
167 $linetext = $username;
168 $dialplan = $config['standard_dialplan'];
170 if ( is_provision_user ( $username, $domain ) )
172 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
175 if ( is_kamailio_subscriber ( $username, $domain ) )
177 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
180 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
183 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
186 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
189 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
190 delete_kamailio_subscriber( $username, $domain );
191 // Give errormessage, and quit.
192 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
195 $userdata = get_userdata( $username, $domain );
198 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
199 delete_kamailio_subscriber( $username, $domain );
200 delete_provision_user( $username, $domain );
201 // Give errormessage, and quit.
202 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
205 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
208 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
212 Required parameters should be...
213 ( username & domain ) | user
225 Verify that the domain is not a local kamailio domain (REMOTE user..)
226 Verify that the username+domain is not already registered in 'provision.users'.
227 * If r_port is empty, set to 5060
228 * If proxy/port is empty, set to registrar/port
229 * If authid is empty, set to username
230 * If dialplan is empty, set to standard dialplan
231 * If linetext is empty, set to username@domain
232 * Add to the 'provision.users' table:
234 password -> supplied password
235 displayname -> displayname
237 registrar -> registrar
244 * Return 'response' => 'ok' with a full user object in JSON format.
245 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
249 // Test required parameters:
251 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) ) ||
array_key_exists('user', $_GET) )
252 && array_key_exists( 'displayname', $_GET )
253 && array_key_exists( 'password', $_GET )
254 && array_key_exists( 'registrar', $_GET ) )
258 if ( array_key_exists('username', $_GET) )
260 $username = $_GET['username'];
261 $domain = $_GET['domain'];
265 $user = split_sipaddress($_GET['user']);
268 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
271 list ( $username, $domain ) = $user;
274 $password = $_GET['password'];
275 $displayname = $_GET['displayname'];
276 $registrar = $_GET['registrar'];
277 $r_port = ( array_key_exists('r_port', $_GET) ) ?
$_GET['r_port'] : 5060;
279 $proxy = ( array_key_exists('proxy', $_GET) ) ?
$_GET['proxy'] : $registrar;
280 $p_port = ( array_key_exists('p_port', $_GET) ) ?
$_GET['p_port'] : $r_port;
281 $authid = ( array_key_exists('authid', $_GET) ) ?
$_GET['authid'] : $username;
282 $dialplan = ( array_key_exists('dialplan', $_GET) ) ?
$_GET['dialplan'] : $config['standard_dialplan'];
283 $linetext = ( array_key_exists('linetext', $_GET) ) ?
$_GET['linetext'] : $username . '@' . $domain;
285 if ( is_kamailio_domain( $domain ) )
287 print json_encode ( array( 'response' => 'failed', 'cause' => 'domain', 'detail' => 'The selected domain is local, cannot add remote user' ));
291 if ( is_provision_user ( $username, $domain ) )
293 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
296 if ( is_kamailio_subscriber ( $username, $domain ) )
298 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
302 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
305 // Give errormessage, and quit.
306 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
309 $userdata = get_userdata( $username, $domain );
312 // Rollback data added!
313 delete_provision_user( $username, $domain );
314 // Give errormessage, and quit.
315 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
318 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
321 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
325 Required parameters should be...
326 ( username & domain ) | user
328 * Verify that no associations/relations exist in 'provision.phones'
329 * Verify that the user exists in 'provision.users'
330 * Remove from 'provision.users'
331 * Test to see of user exists in 'kamailio.subscriber'.
332 * Remove from 'kamailio.subscribers'
333 * Return response' => 'ok', 'type' => 'local'
334 * If not in 'kamailio.subscribers'
335 * Return response' => 'ok', 'type' => 'remote'
336 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
337 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
338 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
340 if ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
341 ||
array_key_exists('user', $_GET) )
346 if ( array_key_exists('username', $_GET) )
348 $username = $_GET['username'];
349 $domain = $_GET['domain'];
353 $user = split_sipaddress($_GET['user']);
356 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
359 list ( $username, $domain ) = $user;
362 if ( get_user_phones ( $username, $domain ) )
364 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
367 if ( is_provision_user( $username, $domain ) ||
is_kamailio_subscriber( $username, $domain ) )
369 delete_provision_user( $username, $domain );
370 delete_kamailio_subscriber( $username, $domain );
371 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
376 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
383 print json_encode ( array( 'response' => 'invalid') );
387 Required parameters should be...
388 ( username & domain ) | user
391 * Verify that no associations/relations exist in 'provision.phones'
392 * Verify that the user exists ...
393 * Test to see of user exists in 'provision.users'
394 * Test to see of user exists in 'kamailio.subscriber'.
395 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
396 * Update user passwords in 'provision' and 'kamailio' as appropriate
397 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
399 if ( array_key_exists('password', $_GET) &&
400 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
401 ||
array_key_exists('user', $_GET) ))
406 if ( array_key_exists('username', $_GET) )
408 $username = $_GET['username'];
409 $domain = $_GET['domain'];
413 $user = split_sipaddress($_GET['user']);
416 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
419 list ( $username, $domain ) = $user;
421 $password = $_GET['password'];
423 // Check compatibility of password? TODO...
424 // Fetch old password for rollback? TODO...
425 // Verify that user exists for provisioning
426 if ( ! is_provision_user( $username, $domain ) )
428 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
431 if ( is_provision_user( $username, $domain ) )
433 // Update provisioning password
434 if (! update_provision_pw( $username, $domain, $password ) )
436 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update provisioning password' ) );
440 // Check for user in kamailio
441 if ( is_kamailio_subscriber( $username, $domain ) )
443 // Update kamailio password
444 if (! update_kamailio_pw( $username, $domain, $password ) )
446 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio password' ) );
450 print json_encode( array ( 'response' => 'ok', 'detail' => 'Password changed for user '.$username.'@'.$domain.'.'));
454 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
459 Required parameters should be...
460 ( username & domain ) | user
462 * Verify that no associations/relations exist in 'provision.phones'
463 * Verify that the user exists ...
464 * Test to see of user exists in 'provision.users'
465 * Test to see of user exists in 'kamailio.subscriber'.
466 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
467 * Get update parameters, and change as appropriate ;)
468 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
470 if ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
471 ||
array_key_exists('user', $_GET) )
476 if ( array_key_exists('username', $_GET) )
478 $username = $_GET['username'];
479 $domain = $_GET['domain'];
483 $user = split_sipaddress($_GET['user']);
486 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
489 list ( $username, $domain ) = $user;
491 print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) );
494 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
497 print generate_password();
500 print json_encode ( array( 'response' => 'invalid') );
502 mysql_close( $config['sql_link'] );