]> git.defcon.no Git - hermes/blob - api/user.php
260ac35016365afc7f19a8d703beccd74ce019e5
[hermes] / api / user.php
1 <?php
2 /*
3 # Copyright (c) 2012, Gjøvik University College
4 # All rights reserved.
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Gjøvik University College nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 require_once('config.php');
29 require_once('lib/auth_base.php');
30 require_once('lib/user_functions.php');
31 require_once('lib/common_functions.php');
32 require_once('lib/db_functions.php');
33 require_once('lib/phone_functions.php');
34 require_once('lib/alias_functions.php');
35
36 $config = get_config();
37
38 $config['sql_link'] = @mysql_connect(
39 $config['sql_server'],
40 $config['sql_username'],
41 $config['sql_password']
42 );
43 if ( !$config['sql_link'] )
44 {
45 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
46 exit;
47 }
48 token_auth();
49
50 //*************************************************************************************
51 switch ( $_SERVER['PATH_INFO'] )
52 {
53 case "/get":
54 // Required GET parameters:
55 // user: authentication username, SIP-username without domain component
56 // domain: Domain/realm of the user. username + '@' + domain == SIP address.
57
58 if ( array_key_exists('user', $_POST) ||
59 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST )))
60 {
61 $username = "";
62 $domain = "";
63 if ( array_key_exists('username', $_POST) )
64 {
65 $username = $_POST['username'];
66 $domain = $_POST['domain'];
67 }
68 else
69 {
70 $user = split_sipaddress($_POST['user']);
71 if ( !$user )
72 {
73 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
74 break;
75 }
76 list ( $username, $domain ) = $user;
77 }
78
79 // Now, do funky stuff.
80 /*
81 Test if user exists in both 'kamailio.subscribers' and 'hermes.users'
82 * Return 'response' => 'ok', 'type' => 'local', 'user' => complete user object.
83 Test if user exists in 'hermes.user' only
84 * Return 'response' => 'ok', 'type' => 'remote', 'user' => complete user object.
85 If user does is neither local nor remote
86 * Return 'response' => 'failed' with 'cause' => 'nonexistant'
87 On failure, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
88
89 */
90 // Dummy-response:
91 $userdata = get_userdata( $username, $domain );
92 if ( $userdata )
93 {
94 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
95 }
96 else
97 {
98 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Request for user ' . $username . '@' . $domain . ' failed.'));
99 }
100 }
101 else
102 print json_encode ( array( 'response' => 'invalid') );
103 break;
104 case "/list":
105 /*
106 Simply list all users in user@domain format
107 Perform a search operation if 'search' exists as a GET-parameter
108 * The search should try to do a "smart search" on SIP-usernames:
109 * Try to search with names in in username@domain format
110 * Do the search with wildcards before and after input text.
111 * The search must be done in the provisioning tables, to be able
112 to match non-local users.
113 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
114 */
115 $search = null;
116 if ( array_key_exists ( 'search', $_POST ) )
117 $search = $_POST['search']; // TODO: Add some sanitation and input validation!
118 $list = list_users( $search );
119 print json_encode( array( 'response' => 'ok', 'list' => $list ));
120 break;
121 case "/add_local":
122 /*
123 What to do??
124 Required parameters should be...
125 ( username & domain ) | user
126 displayname
127 email
128
129 Verify that domain is local (lookup in the 'kamailio.domain' table.
130 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and hermes.users?))
131 * Autocreate password
132 * Add username, domain, email and created password to the 'kamailio.subscriber' table
133 * Get the registrar+port, proxy+port from the 'hermes.servers' table.
134 * standard dialplan from configuration.
135 * Add to the 'hermes.users' table:
136 username -> username
137 password -> generated password
138 displayname -> displayname
139 domain -> domain
140 registrar -> hermes.servers.registrar
141 r_port -> hermes.servers.r_port
142 proxy -> hermes.servers.proxy
143 p_port -> hermes.servers.p_port
144 authid -> username
145 dialplan -> standard dialplan
146 linetext -> username
147 * Return 'response' => 'ok' with a full user object in JSON format.
148 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
149
150 */
151 // Test required parameters:
152 if (
153 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) || array_key_exists('user', $_POST) )
154 && array_key_exists( 'displayname', $_POST )
155 && array_key_exists( 'email', $_POST ) )
156 {
157 $username = "";
158 $domain = "";
159 if ( array_key_exists('username', $_POST) )
160 {
161 $username = $_POST['username'];
162 $domain = $_POST['domain'];
163 }
164 else
165 {
166 $user = split_sipaddress($_POST['user']);
167 if ( !$user )
168 {
169 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
170 break;
171 }
172 list ( $username, $domain ) = $user;
173 }
174
175 $password = generate_password();
176 $displayname = $_POST['displayname'];
177 $email = $_POST['email'];
178
179 if ( !is_kamailio_domain( $domain ) )
180 {
181 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
182 break;
183 }
184
185 $servers = get_servers( $domain );
186 if ( !$servers )
187 {
188 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
189 break;
190 }
191 $registrar = $servers['registrar'];
192 $r_port = $servers['r_port'];
193 $proxy = $servers['proxy'];
194 $p_port = $servers['p_port'];
195 $authid = $username;
196 $linetext = $username;
197 $dialplan = $config['standard_dialplan'];
198
199 if ( is_provision_user ( $username, $domain ) )
200 {
201 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
202 break;
203 }
204 if ( is_kamailio_subscriber ( $username, $domain ) )
205 {
206 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
207 break;
208 }
209 if ( alias_exists ( $username, $domain ) )
210 {
211 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
212 break;
213 }
214
215 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
216 if ( !$kam_res )
217 {
218 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
219 break;
220 }
221 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
222 if ( !$pro_res )
223 {
224 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
225 delete_kamailio_subscriber( $username, $domain );
226 // Give errormessage, and quit.
227 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
228 break;
229 }
230 $userdata = get_userdata( $username, $domain );
231 if ( !$userdata )
232 {
233 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
234 delete_kamailio_subscriber( $username, $domain );
235 delete_provision_user( $username, $domain );
236 // Give errormessage, and quit.
237 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
238 break;
239 }
240 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
241 break;
242 }
243 else
244 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
245 break;
246 case "/add_remote":
247 /*
248 Required parameters should be...
249 ( username & domain ) | user
250 displayname
251 password
252 registrar
253 Optional parameters
254 r_port
255 proxy
256 p_port
257 authid
258 dialplan
259 linetext
260
261 Verify that the domain is not a local kamailio domain (REMOTE user..)
262 Verify that the username+domain is not already registered in 'hermes.users'.
263 * If r_port is empty, set to 5060
264 * If proxy/port is empty, set to registrar/port
265 * If authid is empty, set to username
266 * If dialplan is empty, set to standard dialplan
267 * If linetext is empty, set to username@domain
268 * Add to the 'hermes.users' table:
269 username -> username
270 password -> supplied password
271 displayname -> displayname
272 domain -> domain
273 registrar -> registrar
274 r_port -> r_port
275 proxy -> proxy
276 p_port -> p_port
277 authid -> authid
278 dialplan -> dialplan
279 linetext -> linetext
280 * Return 'response' => 'ok' with a full user object in JSON format.
281 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
282 */
283
284
285 // Test required parameters:
286 if (
287 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) || array_key_exists('user', $_POST) )
288 && array_key_exists( 'displayname', $_POST )
289 && array_key_exists( 'password', $_POST )
290 && array_key_exists( 'registrar', $_POST ) )
291 {
292 $username = "";
293 $domain = "";
294 if ( array_key_exists('username', $_POST) )
295 {
296 $username = $_POST['username'];
297 $domain = $_POST['domain'];
298 }
299 else
300 {
301 $user = split_sipaddress($_POST['user']);
302 if ( !$user )
303 {
304 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
305 break;
306 }
307 list ( $username, $domain ) = $user;
308 }
309
310 $password = $_POST['password'];
311 $displayname = $_POST['displayname'];
312 $registrar = $_POST['registrar'];
313 $r_port = ( array_key_exists('r_port', $_POST) ) ? $_POST['r_port'] : 5060;
314
315 $proxy = ( array_key_exists('proxy', $_POST) ) ? $_POST['proxy'] : $registrar;
316 $p_port = ( array_key_exists('p_port', $_POST) ) ? $_POST['p_port'] : $r_port;
317 $authid = ( array_key_exists('authid', $_POST) ) ? $_POST['authid'] : $username;
318 $dialplan = ( array_key_exists('dialplan', $_POST) ) ? $_POST['dialplan'] : $config['standard_dialplan'];
319 $linetext = ( array_key_exists('linetext', $_POST) ) ? $_POST['linetext'] : $username . '@' . $domain;
320
321 if ( is_kamailio_domain( $domain ) )
322 {
323 print json_encode ( array( 'response' => 'failed', 'cause' => 'domain', 'detail' => 'The selected domain is local, cannot add remote user' ));
324 break;
325 }
326
327 if ( is_provision_user ( $username, $domain ) )
328 {
329 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
330 break;
331 }
332 if ( is_kamailio_subscriber ( $username, $domain ) )
333 {
334 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
335 break;
336 }
337
338 // Should be impossible to hit this test, all aliases are required to be local.
339 if ( alias_exists ( $username, $domain ) )
340 {
341 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
342 break;
343 }
344
345
346 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
347 if ( !$pro_res )
348 {
349 // Give errormessage, and quit.
350 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
351 break;
352 }
353 $userdata = get_userdata( $username, $domain );
354 if ( !$userdata )
355 {
356 // Rollback data added!
357 delete_provision_user( $username, $domain );
358 // Give errormessage, and quit.
359 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
360 break;
361 }
362 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
363 break;
364 }
365 else
366 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
367 break;
368 case "/remove":
369 /*
370 Required parameters should be...
371 ( username & domain ) | user
372
373 * Verify that no associations/relations exist in 'hermes.phones'
374 * Verify that the user exists in 'hermes.users'
375 * Remove from 'hermes.users'
376 * Test to see of user exists in 'kamailio.subscriber'.
377 * Remove from 'kamailio.subscribers'
378 * Return response' => 'ok', 'type' => 'local'
379 * If not in 'kamailio.subscribers'
380 * Return response' => 'ok', 'type' => 'remote'
381 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
382 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
383 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
384 */
385 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
386 || array_key_exists('user', $_POST) )
387
388 {
389 $username = "";
390 $domain = "";
391 if ( array_key_exists('username', $_POST) )
392 {
393 $username = $_POST['username'];
394 $domain = $_POST['domain'];
395 }
396 else
397 {
398 $user = split_sipaddress($_POST['user']);
399 if ( !$user )
400 {
401 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
402 break;
403 }
404 list ( $username, $domain ) = $user;
405 }
406
407 if ( get_user_phones ( $username, $domain ) )
408 {
409 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
410 break;
411 }
412 if ( is_provision_user( $username, $domain ) || is_kamailio_subscriber( $username, $domain ) )
413 {
414 delete_provision_user( $username, $domain );
415 delete_kamailio_subscriber( $username, $domain );
416 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
417 break;
418 }
419 else
420 {
421 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
422 break;
423 }
424
425 break;
426
427 }
428 print json_encode ( array( 'response' => 'invalid') );
429 break;
430 case "/change_pw":
431 /*
432 Required parameters should be...
433 ( username & domain ) | user
434 password
435
436 * Verify that no associations/relations exist in 'hermes.phones'
437 * Verify that the user exists ...
438 * Test to see of user exists in 'hermes.users'
439 * Test to see of user exists in 'kamailio.subscriber'.
440 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
441 * Update user passwords in 'hermes' and 'kamailio' as appropriate
442 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
443 */
444 if ( array_key_exists('password', $_POST) &&
445 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
446 || array_key_exists('user', $_POST) ))
447
448 {
449 $username = "";
450 $domain = "";
451 if ( array_key_exists('username', $_POST) )
452 {
453 $username = $_POST['username'];
454 $domain = $_POST['domain'];
455 }
456 else
457 {
458 $user = split_sipaddress($_POST['user']);
459 if ( !$user )
460 {
461 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
462 break;
463 }
464 list ( $username, $domain ) = $user;
465 }
466 $password = $_POST['password'];
467
468 // Check compatibility of password? TODO...
469 // Fetch old password for rollback? TODO...
470 // Verify that user exists for provisioning
471 if ( ! is_provision_user( $username, $domain ) )
472 {
473 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
474 break;
475 }
476 if ( is_provision_user( $username, $domain ) )
477 {
478 // Update provisioning password
479 if ( update_provision_pw( $username, $domain, $password ) < 0 )
480 {
481 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update provisioning password' ) );
482 break;
483 }
484 }
485 // Check for user in kamailio
486 if ( is_kamailio_subscriber( $username, $domain ) )
487 {
488 // Update kamailio password
489 if ( update_kamailio_pw( $username, $domain, $password ) < 0 )
490 {
491 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio password' ) );
492 break;
493 }
494 }
495 print json_encode( array ( 'response' => 'ok', 'detail' => 'Password changed for user '.$username.'@'.$domain.'.'));
496 break;
497 }
498 else
499 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
500 break;
501
502
503 case "/change_email":
504 /*
505 Required parameters should be...
506 ( username & domain ) | user
507 email
508 */
509 if ( array_key_exists('email', $_POST) &&
510 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
511 || array_key_exists('user', $_POST) ))
512
513 {
514 $username = "";
515 $domain = "";
516 if ( array_key_exists('username', $_POST) )
517 {
518 $username = $_POST['username'];
519 $domain = $_POST['domain'];
520 }
521 else
522 {
523 $user = split_sipaddress($_POST['user']);
524 if ( !$user )
525 {
526 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
527 break;
528 }
529 list ( $username, $domain ) = $user;
530 }
531 $email = $_POST['email'];
532
533 // Check for user in kamailio
534 if ( is_kamailio_subscriber( $username, $domain ) )
535 {
536 // Update kamailio email
537 if ( update_kamailio_email( $username, $domain, $email ) < 0 )
538 {
539 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio email' ) );
540 break;
541 }
542 }
543 print json_encode( array ( 'response' => 'ok', 'user' => $username.'@'.$domain, 'email' => $email));
544 break;
545 }
546 else
547 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
548 break;
549
550
551
552
553
554
555
556 case "/update":
557 /*
558 Required parameters should be...
559 ( username & domain ) | user
560
561 * Verify that no associations/relations exist in 'hermes.phones'
562 * Verify that the user exists ...
563 * Test to see of user exists in 'hermes.users'
564 * Test to see of user exists in 'kamailio.subscriber'.
565 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
566 * Get update parameters, and change as appropriate ;)
567 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
568 */
569 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
570 || array_key_exists('user', $_POST) )
571
572 {
573 $username = "";
574 $domain = "";
575 if ( array_key_exists('username', $_POST) )
576 {
577 $username = $_POST['username'];
578 $domain = $_POST['domain'];
579 }
580 else
581 {
582 $user = split_sipaddress($_POST['user']);
583 if ( !$user )
584 {
585 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
586 break;
587 }
588 list ( $username, $domain ) = $user;
589 }
590 if ( ! is_provision_user ( $username, $domain ) )
591 {
592 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
593 break;
594 }
595 $updated = array();
596 $failed = array();
597 $error = 1;
598 $result_text = "";
599 $params = array('displayname', 'dialplan', 'linetext', 'registrar', 'r_port', 'proxy', 'p_port');
600 foreach ( $params as $p )
601 {
602 if ( array_key_exists($p, $_POST ) )
603 {
604 $data = $_POST[$p];
605 $t = update_provision_data($p, $username, $domain, $data);
606 if ( $t != true )
607 {
608 $error = $t;
609 array_push($failed, $p);
610 }
611 else
612 {
613 array_push( $updated, $p);
614 }
615 }
616 }
617 $res = array();
618 if ( ( $error == 1 ) || ( $error == 0 ) )
619 {
620 $res['response'] = 'ok';
621 $res['skipped'] = $failed;
622 }
623 else if ( $error == -1 )
624 {
625 $res['response'] = 'failed';
626 $res['cause'] = 'param';
627 $res['detail'] = 'Invalid parameters';
628 $res['failed'] = $failed;
629 }
630 else if ( $error == -2 )
631 {
632 $res['response'] = 'failed';
633 $res['cause'] = 'dbfail';
634 $res['detail'] = 'Database failure';
635 $res['failed'] = $failed;
636 }
637 else
638 $res['response'] = 'error'; // Wait, what?
639
640 $res['updated'] = $updated;
641
642 print json_encode ( $res );
643 }
644 else
645 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
646 break;
647 case "/available":
648 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
649 || array_key_exists('user', $_POST) )
650
651 {
652 $username = "";
653 $domain = "";
654 if ( array_key_exists('username', $_POST) )
655 {
656 $username = $_POST['username'];
657 $domain = $_POST['domain'];
658 }
659 else
660 {
661 $user = split_sipaddress($_POST['user']);
662 if ( !$user )
663 {
664 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
665 break;
666 }
667 list ( $username, $domain ) = $user;
668 }
669 if ( is_provision_user ( $username, $domain ) )
670 {
671 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
672 break;
673 }
674 if ( is_kamailio_subscriber ( $username, $domain ) )
675 {
676 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
677 break;
678 }
679 if ( alias_exists ( $username, $domain ) )
680 {
681 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
682 break;
683 }
684 print json_encode( array ( 'response' => 'ok', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
685
686 }
687 else
688 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
689 break;
690 case "/gen_pw":
691 print generate_password();
692 break;
693 default:
694 print json_encode ( array( 'response' => 'invalid') );
695 }
696 mysql_close( $config['sql_link'] );
697 ?>