]>
git.defcon.no Git - hermes/blob - api/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');
7 require_once('lib/alias_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.'));
22 //*************************************************************************************
23 switch ( $_SERVER['PATH_INFO'] )
26 // Required GET parameters:
27 // user: authentication username, SIP-username without domain component
28 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
30 if ( array_key_exists('user', $_GET) ||
31 ( array_key_exists('username', $_GET) && array_key_exists('domain', $_GET )))
35 if ( array_key_exists('username', $_GET) )
37 $username = $_GET['username'];
38 $domain = $_GET['domain'];
42 $user = split_sipaddress($_GET['user']);
45 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
48 list ( $username, $domain ) = $user;
51 // Now, do funky stuff.
53 Test if user exists in both 'kamailio.subscribers' and 'provision.users'
54 * Return 'response' => 'ok', 'type' => 'local', 'user' => complete user object.
55 Test if user exists in 'provision.user' only
56 * Return 'response' => 'ok', 'type' => 'remote', 'user' => complete user object.
57 If user does is neither local nor remote
58 * Return 'response' => 'failed' with 'cause' => 'nonexistant'
59 On failure, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
63 $userdata = get_userdata( $username, $domain );
66 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
70 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Request for user ' . $username . '@' . $domain . ' failed.'));
74 print json_encode ( array( 'response' => 'invalid') );
78 Simply list all users in user@domain format
79 Perform a search operation if 'search' exists as a GET-parameter
80 * The search should try to do a "smart search" on SIP-usernames:
81 * Try to search with names in in username@domain format
82 * Do the search with wildcards before and after input text.
83 * The search must be done in the provisioning tables, to be able
84 to match non-local users.
85 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
88 if ( array_key_exists ( 'search', $_GET ) )
89 $search = $_GET['search']; // TODO: Add some sanitation and input validation!
90 $list = list_users( $search );
91 print json_encode( array( 'response' => 'ok', 'list' => $list ));
96 Required parameters should be...
97 ( username & domain ) | user
101 Verify that domain is local (lookup in the 'kamailio.domain' table.
102 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and provision.users?))
103 * Autocreate password
104 * Add username, domain, email and created password to the 'kamailio.subscriber' table
105 * Get the registrar+port, proxy+port from the 'provision.servers' table.
106 * standard dialplan from configuration.
107 * Add to the 'provision.users' table:
109 password -> generated password
110 displayname -> displayname
112 registrar -> provision.servers.registrar
113 r_port -> provision.servers.r_port
114 proxy -> provision.servers.proxy
115 p_port -> provision.servers.p_port
117 dialplan -> standard dialplan
119 * Return 'response' => 'ok' with a full user object in JSON format.
120 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
123 // Test required parameters:
125 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) ) ||
array_key_exists('user', $_GET) )
126 && array_key_exists( 'displayname', $_GET )
127 && array_key_exists( 'email', $_GET ) )
131 if ( array_key_exists('username', $_GET) )
133 $username = $_GET['username'];
134 $domain = $_GET['domain'];
138 $user = split_sipaddress($_GET['user']);
141 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
144 list ( $username, $domain ) = $user;
147 $password = generate_password();
148 $displayname = $_GET['displayname'];
149 $email = $_GET['email'];
151 if ( !is_kamailio_domain( $domain ) )
153 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
157 $servers = get_servers( $domain );
160 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
163 $registrar = $servers['registrar'];
164 $r_port = $servers['r_port'];
165 $proxy = $servers['proxy'];
166 $p_port = $servers['p_port'];
168 $linetext = $username;
169 $dialplan = $config['standard_dialplan'];
171 if ( is_provision_user ( $username, $domain ) )
173 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
176 if ( is_kamailio_subscriber ( $username, $domain ) )
178 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
181 if ( alias_exists ( $username, $domain ) )
183 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
187 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
190 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
193 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
196 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
197 delete_kamailio_subscriber( $username, $domain );
198 // Give errormessage, and quit.
199 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
202 $userdata = get_userdata( $username, $domain );
205 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
206 delete_kamailio_subscriber( $username, $domain );
207 delete_provision_user( $username, $domain );
208 // Give errormessage, and quit.
209 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
212 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
215 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
219 Required parameters should be...
220 ( username & domain ) | user
232 Verify that the domain is not a local kamailio domain (REMOTE user..)
233 Verify that the username+domain is not already registered in 'provision.users'.
234 * If r_port is empty, set to 5060
235 * If proxy/port is empty, set to registrar/port
236 * If authid is empty, set to username
237 * If dialplan is empty, set to standard dialplan
238 * If linetext is empty, set to username@domain
239 * Add to the 'provision.users' table:
241 password -> supplied password
242 displayname -> displayname
244 registrar -> registrar
251 * Return 'response' => 'ok' with a full user object in JSON format.
252 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
256 // Test required parameters:
258 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) ) ||
array_key_exists('user', $_GET) )
259 && array_key_exists( 'displayname', $_GET )
260 && array_key_exists( 'password', $_GET )
261 && array_key_exists( 'registrar', $_GET ) )
265 if ( array_key_exists('username', $_GET) )
267 $username = $_GET['username'];
268 $domain = $_GET['domain'];
272 $user = split_sipaddress($_GET['user']);
275 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
278 list ( $username, $domain ) = $user;
281 $password = $_GET['password'];
282 $displayname = $_GET['displayname'];
283 $registrar = $_GET['registrar'];
284 $r_port = ( array_key_exists('r_port', $_GET) ) ?
$_GET['r_port'] : 5060;
286 $proxy = ( array_key_exists('proxy', $_GET) ) ?
$_GET['proxy'] : $registrar;
287 $p_port = ( array_key_exists('p_port', $_GET) ) ?
$_GET['p_port'] : $r_port;
288 $authid = ( array_key_exists('authid', $_GET) ) ?
$_GET['authid'] : $username;
289 $dialplan = ( array_key_exists('dialplan', $_GET) ) ?
$_GET['dialplan'] : $config['standard_dialplan'];
290 $linetext = ( array_key_exists('linetext', $_GET) ) ?
$_GET['linetext'] : $username . '@' . $domain;
292 if ( is_kamailio_domain( $domain ) )
294 print json_encode ( array( 'response' => 'failed', 'cause' => 'domain', 'detail' => 'The selected domain is local, cannot add remote user' ));
298 if ( is_provision_user ( $username, $domain ) )
300 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
303 if ( is_kamailio_subscriber ( $username, $domain ) )
305 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
309 // Should be impossible to hit this test, all aliases are required to be local.
310 if ( alias_exists ( $username, $domain ) )
312 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
317 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
320 // Give errormessage, and quit.
321 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
324 $userdata = get_userdata( $username, $domain );
327 // Rollback data added!
328 delete_provision_user( $username, $domain );
329 // Give errormessage, and quit.
330 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
333 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
336 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
340 Required parameters should be...
341 ( username & domain ) | user
343 * Verify that no associations/relations exist in 'provision.phones'
344 * Verify that the user exists in 'provision.users'
345 * Remove from 'provision.users'
346 * Test to see of user exists in 'kamailio.subscriber'.
347 * Remove from 'kamailio.subscribers'
348 * Return response' => 'ok', 'type' => 'local'
349 * If not in 'kamailio.subscribers'
350 * Return response' => 'ok', 'type' => 'remote'
351 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
352 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
353 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
355 if ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
356 ||
array_key_exists('user', $_GET) )
361 if ( array_key_exists('username', $_GET) )
363 $username = $_GET['username'];
364 $domain = $_GET['domain'];
368 $user = split_sipaddress($_GET['user']);
371 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
374 list ( $username, $domain ) = $user;
377 if ( get_user_phones ( $username, $domain ) )
379 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
382 if ( is_provision_user( $username, $domain ) ||
is_kamailio_subscriber( $username, $domain ) )
384 delete_provision_user( $username, $domain );
385 delete_kamailio_subscriber( $username, $domain );
386 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
391 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
398 print json_encode ( array( 'response' => 'invalid') );
402 Required parameters should be...
403 ( username & domain ) | user
406 * Verify that no associations/relations exist in 'provision.phones'
407 * Verify that the user exists ...
408 * Test to see of user exists in 'provision.users'
409 * Test to see of user exists in 'kamailio.subscriber'.
410 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
411 * Update user passwords in 'provision' and 'kamailio' as appropriate
412 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
414 if ( array_key_exists('password', $_GET) &&
415 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
416 ||
array_key_exists('user', $_GET) ))
421 if ( array_key_exists('username', $_GET) )
423 $username = $_GET['username'];
424 $domain = $_GET['domain'];
428 $user = split_sipaddress($_GET['user']);
431 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
434 list ( $username, $domain ) = $user;
436 $password = $_GET['password'];
438 // Check compatibility of password? TODO...
439 // Fetch old password for rollback? TODO...
440 // Verify that user exists for provisioning
441 if ( ! is_provision_user( $username, $domain ) )
443 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
446 if ( is_provision_user( $username, $domain ) )
448 // Update provisioning password
449 if ( update_provision_pw( $username, $domain, $password ) < 0 )
451 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update provisioning password' ) );
455 // Check for user in kamailio
456 if ( is_kamailio_subscriber( $username, $domain ) )
458 // Update kamailio password
459 if ( update_kamailio_pw( $username, $domain, $password ) < 0 )
461 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio password' ) );
465 print json_encode( array ( 'response' => 'ok', 'detail' => 'Password changed for user '.$username.'@'.$domain.'.'));
469 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
473 case "/change_email":
475 Required parameters should be...
476 ( username & domain ) | user
479 if ( array_key_exists('email', $_GET) &&
480 ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
481 ||
array_key_exists('user', $_GET) ))
486 if ( array_key_exists('username', $_GET) )
488 $username = $_GET['username'];
489 $domain = $_GET['domain'];
493 $user = split_sipaddress($_GET['user']);
496 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
499 list ( $username, $domain ) = $user;
501 $email = $_GET['email'];
503 // Check for user in kamailio
504 if ( is_kamailio_subscriber( $username, $domain ) )
506 // Update kamailio email
507 if ( update_kamailio_email( $username, $domain, $email ) < 0 )
509 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio email' ) );
513 print json_encode( array ( 'response' => 'ok', 'user' => $username.'@'.$domain, 'email' => $email));
517 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
528 Required parameters should be...
529 ( username & domain ) | user
531 * Verify that no associations/relations exist in 'provision.phones'
532 * Verify that the user exists ...
533 * Test to see of user exists in 'provision.users'
534 * Test to see of user exists in 'kamailio.subscriber'.
535 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
536 * Get update parameters, and change as appropriate ;)
537 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
539 if ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
540 ||
array_key_exists('user', $_GET) )
545 if ( array_key_exists('username', $_GET) )
547 $username = $_GET['username'];
548 $domain = $_GET['domain'];
552 $user = split_sipaddress($_GET['user']);
555 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
558 list ( $username, $domain ) = $user;
560 if ( ! is_provision_user ( $username, $domain ) )
562 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
569 $params = array('displayname', 'dialplan', 'linetext', 'registrar', 'r_port', 'proxy', 'p_port');
570 foreach ( $params as $p )
572 if ( array_key_exists($p, $_GET ) )
575 $t = update_provision_data($p, $username, $domain, $data);
579 array_push($failed, $p);
583 array_push( $updated, $p);
588 if ( ( $error == 1 ) ||
( $error == 0 ) )
590 $res['response'] = 'ok';
591 $res['skipped'] = $failed;
593 else if ( $error == -1 )
595 $res['response'] = 'failed';
596 $res['cause'] = 'param';
597 $res['detail'] = 'Invalid parameters';
598 $res['failed'] = $failed;
600 else if ( $error == -2 )
602 $res['response'] = 'failed';
603 $res['cause'] = 'dbfail';
604 $res['detail'] = 'Database failure';
605 $res['failed'] = $failed;
608 $res['response'] = 'error'; // Wait, what?
610 $res['updated'] = $updated;
612 print json_encode ( $res );
615 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
618 if ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
619 ||
array_key_exists('user', $_GET) )
624 if ( array_key_exists('username', $_GET) )
626 $username = $_GET['username'];
627 $domain = $_GET['domain'];
631 $user = split_sipaddress($_GET['user']);
634 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
637 list ( $username, $domain ) = $user;
639 if ( is_provision_user ( $username, $domain ) )
641 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
644 if ( is_kamailio_subscriber ( $username, $domain ) )
646 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
649 if ( alias_exists ( $username, $domain ) )
651 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
654 print json_encode( array ( 'response' => 'ok', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
658 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
661 print generate_password();
664 print json_encode ( array( 'response' => 'invalid') );
666 mysql_close( $config['sql_link'] );