]>
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.
28 if ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
30 $username = $_GET['username'];
31 $domain = $_GET['domain'];
32 // Now, do funky stuff.
34 Test if user exists in both 'kamailio.subscribers' and 'provision.users'
35 * Return 'response' => 'ok', 'type' => 'local', 'user' => complete user object.
36 Test if user exists in 'provision.user' only
37 * Return 'response' => 'ok', 'type' => 'remote', 'user' => complete user object.
38 If user does is neither local nor remote
39 * Return 'response' => 'failed' with 'cause' => 'nonexistant'
40 On failure, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
44 $userdata = get_userdata( $username, $domain );
47 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
51 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Request for user ' . $username . '@' . $domain . ' failed.'));
55 print json_encode ( array( 'response' => 'invalid') );
59 Simply list all users in user@domain format
60 Perform a search operation if 'search' exists as a GET-parameter
61 * The search should try to do a "smart search" on SIP-usernames:
62 * Try to search with names in in username@domain format
63 * Do the search with wildcards before and after input text.
64 * The search must be done in the provisioning tables, to be able
65 to match non-local users.
66 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
69 if ( array_key_exists ( 'search', $_GET ) )
70 $search = $_GET['search']; // TODO: Add some sanitation and input validation!
71 $list = list_users( $search );
72 print json_encode( array( 'response' => 'ok', 'list' => $list ));
77 Required parameters should be...
83 Verify that domain is local (lookup in the 'kamailio.domain' table.
84 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and provision.users?))
86 * Add username, domain, email and created password to the 'kamailio.subscriber' table
87 * Get the registrar+port, proxy+port from the 'provision.servers' table.
88 * standard dialplan from configuration.
89 * Add to the 'provision.users' table:
91 password -> generated password
92 displayname -> displayname
94 registrar -> provision.servers.registrar
95 r_port -> provision.servers.r_port
96 proxy -> provision.servers.proxy
97 p_port -> provision.servers.p_port
99 dialplan -> standard dialplan
101 * Return 'response' => 'ok' with a full user object in JSON format.
102 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
105 // Test required parameters:
106 if ( array_key_exists( 'username', $_GET)
107 && array_key_exists( 'domain', $_GET )
108 && array_key_exists( 'displayname', $_GET )
109 && array_key_exists( 'email', $_GET ) )
111 $username = $_GET['username'];
112 $domain = $_GET['domain'];
113 $password = generate_password();
114 $displayname = $_GET['displayname'];
115 $email = $_GET['email'];
117 if ( !is_kamailio_domain( $domain ) )
119 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
123 $servers = get_servers( $domain );
126 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
129 $registrar = $servers['registrar'];
130 $r_port = $servers['r_port'];
131 $proxy = $servers['proxy'];
132 $p_port = $servers['p_port'];
134 $linetext = $username;
135 $dialplan = $config['standard_dialplan'];
137 if ( is_provision_user ( $username, $domain ) )
139 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
142 if ( is_kamailio_subscriber ( $username, $domain ) )
144 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
147 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
150 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
153 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
156 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
157 delete_kamailio_subscriber( $username, $domain );
158 // Give errormessage, and quit.
159 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
162 $userdata = get_userdata( $username, $domain );
165 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
166 delete_kamailio_subscriber( $username, $domain );
167 delete_provision_user( $username, $domain );
168 // Give errormessage, and quit.
169 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
172 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
175 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
179 Required parameters should be...
193 Verify that the username+domain is not already registered in 'provision.users'.
194 * If r_port is empty, set to 5060
195 * If proxy/port is empty, set to registrar/port
196 * If authid is empty, set to username
197 * If dialplan is empty, set to standard dialplan
198 * If linetext is empty, set to username@domain
199 * Add to the 'provision.users' table:
201 password -> supplied password
202 displayname -> displayname
204 registrar -> registrar
211 * Return 'response' => 'ok' with a full user object in JSON format.
212 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
216 // Test required parameters:
217 if ( array_key_exists( 'username', $_GET)
218 && array_key_exists( 'password', $_GET )
219 && array_key_exists( 'displayname', $_GET )
220 && array_key_exists( 'domain', $_GET )
221 && array_key_exists( 'registrar', $_GET ) )
223 $username = $_GET['username'];
224 $password = $_GET['password'];
225 $domain = $_GET['domain'];
226 $displayname = $_GET['displayname'];
227 $registrar = $_GET['registrar'];
228 $r_port = ( array_key_exists('r_port', $_GET) ) ?
$_GET['r_port'] : 5060;
230 $proxy = ( array_key_exists('proxy', $_GET) ) ?
$_GET['proxy'] : $registrar;
231 $p_port = ( array_key_exists('p_port', $_GET) ) ?
$_GET['p_port'] : $r_port;
232 $authid = ( array_key_exists('authid', $_GET) ) ?
$_GET['authid'] : $username;
233 $dialplan = ( array_key_exists('dialplan', $_GET) ) ?
$_GET['dialplan'] : $config['standard_dialplan'];
234 $linetext = ( array_key_exists('linetext', $_GET) ) ?
$_GET['linetext'] : $username . '@' . $domain;
236 if ( is_provision_user ( $username, $domain ) )
238 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
241 if ( is_kamailio_subscriber ( $username, $domain ) )
243 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
247 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
250 // Give errormessage, and quit.
251 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
254 $userdata = get_userdata( $username, $domain );
257 // Rollback data added!
258 delete_provision_user( $username, $domain );
259 // Give errormessage, and quit.
260 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
263 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
266 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
270 Required parameters should be...
274 * Verify that no associations/relations exist in 'provision.phones'
275 * Verify that the user exists in 'provision.users'
276 * Remove from 'provision.users'
277 * Test to see of user exists in 'kamailio.subscriber'.
278 * Remove from 'kamailio.subscribers'
279 * Return response' => 'ok', 'type' => 'local'
280 * If not in 'kamailio.subscribers'
281 * Return response' => 'ok', 'type' => 'remote'
282 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
283 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
284 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
286 if ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) )
288 $username = $_GET['username'];
289 $domain = $_GET['domain'];
290 if ( get_user_phones ( $username, $domain ) )
292 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
295 if ( is_provision_user( $username, $domain ) ||
is_kamailio_subscriber( $username, $domain ) )
297 delete_provision_user( $username, $domain );
298 delete_kamailio_subscriber( $username, $domain );
299 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
304 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
313 print generate_password();
316 print json_encode ( array( 'response' => 'invalid') );
318 mysql_close( $config['sql_link'] );