]> git.defcon.no Git - hermes/blob - api/user.php
Forgot to die on error in --remove option path
[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
105 case "/location":
106 if ( array_key_exists('user', $_POST) ||
107 ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST )))
108 {
109 $username = "";
110 $domain = "";
111 if ( array_key_exists('username', $_POST) )
112 {
113 $username = $_POST['username'];
114 $domain = $_POST['domain'];
115 }
116 else
117 {
118 $user = split_sipaddress($_POST['user']);
119 if ( !$user )
120 {
121 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
122 break;
123 }
124 list ( $username, $domain ) = $user;
125 }
126 $location = get_locations( $username, $domain );
127 if ( $location == null )
128 {
129 print json_encode( array( 'response' => 'failed', 'cause' => 'offline' ));
130 break;
131 }
132 print json_encode( array( 'response' => 'ok', 'locations' => $location ));
133 }
134 else
135 print json_encode ( array( 'response' => 'invalid') );
136 break;
137
138 case "/list":
139 /*
140 Simply list all users in user@domain format
141 Perform a search operation if 'search' exists as a GET-parameter
142 * The search should try to do a "smart search" on SIP-usernames:
143 * Try to search with names in in username@domain format
144 * Do the search with wildcards before and after input text.
145 * The search must be done in the provisioning tables, to be able
146 to match non-local users.
147 * SQL SELECT CONCAT() WHERE CONCAT() must be used *shrug*
148 */
149 $search = null;
150 if ( array_key_exists ( 'search', $_POST ) )
151 $search = $_POST['search']; // TODO: Add some sanitation and input validation!
152 $list = list_users( $search );
153 print json_encode( array( 'response' => 'ok', 'list' => $list ));
154 break;
155 case "/add_local":
156 /*
157 What to do??
158 Required parameters should be...
159 ( username & domain ) | user
160 displayname
161 email
162
163 Verify that domain is local (lookup in the 'kamailio.domain' table.
164 Verify that the username is available (nonexistant for domain in kamilio.subscribers (and hermes.users?))
165 * Autocreate password
166 * Add username, domain, email and created password to the 'kamailio.subscriber' table
167 * Get the registrar+port, proxy+port from the 'hermes.servers' table.
168 * standard dialplan from configuration.
169 * Add to the 'hermes.users' table:
170 username -> username
171 password -> generated password
172 displayname -> displayname
173 domain -> domain
174 registrar -> hermes.servers.registrar
175 r_port -> hermes.servers.r_port
176 proxy -> hermes.servers.proxy
177 p_port -> hermes.servers.p_port
178 authid -> username
179 dialplan -> standard dialplan
180 linetext -> username
181 * Return 'response' => 'ok' with a full user object in JSON format.
182 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" on JSON format.
183
184 */
185 // Test required parameters:
186 if (
187 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) || array_key_exists('user', $_POST) )
188 && array_key_exists( 'displayname', $_POST )
189 && array_key_exists( 'email', $_POST ) )
190 {
191 $username = "";
192 $domain = "";
193 if ( array_key_exists('username', $_POST) )
194 {
195 $username = $_POST['username'];
196 $domain = $_POST['domain'];
197 }
198 else
199 {
200 $user = split_sipaddress($_POST['user']);
201 if ( !$user )
202 {
203 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
204 break;
205 }
206 list ( $username, $domain ) = $user;
207 }
208
209 $password = generate_password();
210 $displayname = $_POST['displayname'];
211 $email = $_POST['email'];
212
213 if ( !is_kamailio_domain( $domain ) )
214 {
215 print json_encode ( array( 'response' => 'failed', 'cause' => 'nxdomain', 'detail' => 'The selected domain is not local' ));
216 break;
217 }
218
219 $servers = get_servers( $domain );
220 if ( !$servers )
221 {
222 print json_encode( array( 'response' => 'failed', 'cause' => 'servfail', 'detail' => 'Servers lookup failed for domain '. $domain ) );
223 break;
224 }
225 $registrar = $servers['registrar'];
226 $r_port = $servers['r_port'];
227 $proxy = $servers['proxy'];
228 $p_port = $servers['p_port'];
229 $authid = $username;
230 $linetext = $username;
231 $dialplan = $config['standard_dialplan'];
232
233 if ( is_provision_user ( $username, $domain ) )
234 {
235 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
236 break;
237 }
238 if ( is_kamailio_subscriber ( $username, $domain ) )
239 {
240 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
241 break;
242 }
243 if ( alias_exists ( $username, $domain ) )
244 {
245 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
246 break;
247 }
248
249 $kam_res = add_kamailio_subscriber( $username, $domain, $password, $email );
250 if ( !$kam_res )
251 {
252 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add kamailio subscriber.' ) );
253 break;
254 }
255 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
256 if ( !$pro_res )
257 {
258 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
259 delete_kamailio_subscriber( $username, $domain );
260 // Give errormessage, and quit.
261 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning. Rolled back kamailio subscriber' ) );
262 break;
263 }
264 $userdata = get_userdata( $username, $domain );
265 if ( !$userdata )
266 {
267 // Rollback data added to Kamailio! Try to simulate atomicity, or atleast maintain integrity...
268 delete_kamailio_subscriber( $username, $domain );
269 delete_provision_user( $username, $domain );
270 // Give errormessage, and quit.
271 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
272 break;
273 }
274 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
275 break;
276 }
277 else
278 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
279 break;
280 case "/add_remote":
281 /*
282 Required parameters should be...
283 ( username & domain ) | user
284 displayname
285 password
286 registrar
287 Optional parameters
288 r_port
289 proxy
290 p_port
291 authid
292 dialplan
293 linetext
294
295 Verify that the domain is not a local kamailio domain (REMOTE user..)
296 Verify that the username+domain is not already registered in 'hermes.users'.
297 * If r_port is empty, set to 5060
298 * If proxy/port is empty, set to registrar/port
299 * If authid is empty, set to username
300 * If dialplan is empty, set to standard dialplan
301 * If linetext is empty, set to username@domain
302 * Add to the 'hermes.users' table:
303 username -> username
304 password -> supplied password
305 displayname -> displayname
306 domain -> domain
307 registrar -> registrar
308 r_port -> r_port
309 proxy -> proxy
310 p_port -> p_port
311 authid -> authid
312 dialplan -> dialplan
313 linetext -> linetext
314 * Return 'response' => 'ok' with a full user object in JSON format.
315 If any of the tests fail, return 'response' => 'failed' with 'cause' => "description" in JSON format.
316 */
317
318
319 // Test required parameters:
320 if (
321 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) ) || array_key_exists('user', $_POST) )
322 && array_key_exists( 'displayname', $_POST )
323 && array_key_exists( 'password', $_POST )
324 && array_key_exists( 'registrar', $_POST ) )
325 {
326 $username = "";
327 $domain = "";
328 if ( array_key_exists('username', $_POST) )
329 {
330 $username = $_POST['username'];
331 $domain = $_POST['domain'];
332 }
333 else
334 {
335 $user = split_sipaddress($_POST['user']);
336 if ( !$user )
337 {
338 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
339 break;
340 }
341 list ( $username, $domain ) = $user;
342 }
343
344 $password = $_POST['password'];
345 $displayname = $_POST['displayname'];
346 $registrar = $_POST['registrar'];
347 $r_port = ( array_key_exists('r_port', $_POST) ) ? $_POST['r_port'] : 5060;
348
349 $proxy = ( array_key_exists('proxy', $_POST) ) ? $_POST['proxy'] : $registrar;
350 $p_port = ( array_key_exists('p_port', $_POST) ) ? $_POST['p_port'] : $r_port;
351 $authid = ( array_key_exists('authid', $_POST) ) ? $_POST['authid'] : $username;
352 $dialplan = ( array_key_exists('dialplan', $_POST) ) ? $_POST['dialplan'] : $config['standard_dialplan'];
353 $linetext = ( array_key_exists('linetext', $_POST) ) ? $_POST['linetext'] : $username . '@' . $domain;
354
355 if ( is_kamailio_domain( $domain ) )
356 {
357 print json_encode ( array( 'response' => 'failed', 'cause' => 'domain', 'detail' => 'The selected domain is local, cannot add remote user' ));
358 break;
359 }
360
361 if ( is_provision_user ( $username, $domain ) )
362 {
363 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
364 break;
365 }
366 if ( is_kamailio_subscriber ( $username, $domain ) )
367 {
368 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
369 break;
370 }
371
372 // Should be impossible to hit this test, all aliases are required to be local.
373 if ( alias_exists ( $username, $domain ) )
374 {
375 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
376 break;
377 }
378
379
380 $pro_res = add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext );
381 if ( !$pro_res )
382 {
383 // Give errormessage, and quit.
384 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to add user for provisioning.' ) );
385 break;
386 }
387 $userdata = get_userdata( $username, $domain );
388 if ( !$userdata )
389 {
390 // Rollback data added!
391 delete_provision_user( $username, $domain );
392 // Give errormessage, and quit.
393 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to read recently added data. Operations rolled back' ) );
394 break;
395 }
396 print json_encode( array( 'response' => 'ok', 'user' => $userdata ));
397 break;
398 }
399 else
400 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
401 break;
402 case "/remove":
403 /*
404 Required parameters should be...
405 ( username & domain ) | user
406
407 * Verify that no associations/relations exist in 'hermes.phones'
408 * Verify that the user exists in 'hermes.users'
409 * Remove from 'hermes.users'
410 * Test to see of user exists in 'kamailio.subscriber'.
411 * Remove from 'kamailio.subscribers'
412 * Return response' => 'ok', 'type' => 'local'
413 * If not in 'kamailio.subscribers'
414 * Return response' => 'ok', 'type' => 'remote'
415 * If associations exist, return 'response' => 'failed', 'cause' => 'inuse'
416 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
417 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
418 */
419 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
420 || array_key_exists('user', $_POST) )
421
422 {
423 $username = "";
424 $domain = "";
425 if ( array_key_exists('username', $_POST) )
426 {
427 $username = $_POST['username'];
428 $domain = $_POST['domain'];
429 }
430 else
431 {
432 $user = split_sipaddress($_POST['user']);
433 if ( !$user )
434 {
435 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
436 break;
437 }
438 list ( $username, $domain ) = $user;
439 }
440
441 if ( get_user_phones ( $username, $domain ) )
442 {
443 print json_encode( array( 'response' => 'failed', 'cause' => 'inuse', 'detail' => 'User has associated provisioning. Remove and retry.' ) );
444 break;
445 }
446 if ( is_provision_user( $username, $domain ) || is_kamailio_subscriber( $username, $domain ) )
447 {
448 delete_provision_user( $username, $domain );
449 delete_kamailio_subscriber( $username, $domain );
450 print json_encode( array ( 'response' => 'ok', 'detail' => 'User ' . $username . '@' . $domain . ' deleted.'));
451 break;
452 }
453 else
454 {
455 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Unable to remove nonexistant user.'));
456 break;
457 }
458
459 break;
460
461 }
462 print json_encode ( array( 'response' => 'invalid') );
463 break;
464 case "/change_pw":
465 /*
466 Required parameters should be...
467 ( username & domain ) | user
468 password
469
470 * Verify that no associations/relations exist in 'hermes.phones'
471 * Verify that the user exists ...
472 * Test to see of user exists in 'hermes.users'
473 * Test to see of user exists in 'kamailio.subscriber'.
474 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
475 * Update user passwords in 'hermes' and 'kamailio' as appropriate
476 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
477 */
478 if ( array_key_exists('password', $_POST) &&
479 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
480 || array_key_exists('user', $_POST) ))
481
482 {
483 $username = "";
484 $domain = "";
485 if ( array_key_exists('username', $_POST) )
486 {
487 $username = $_POST['username'];
488 $domain = $_POST['domain'];
489 }
490 else
491 {
492 $user = split_sipaddress($_POST['user']);
493 if ( !$user )
494 {
495 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
496 break;
497 }
498 list ( $username, $domain ) = $user;
499 }
500 $password = $_POST['password'];
501
502 // Check compatibility of password? TODO...
503 // Fetch old password for rollback? TODO...
504 // Verify that user exists for provisioning
505 if ( ! is_provision_user( $username, $domain ) )
506 {
507 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
508 break;
509 }
510 if ( is_provision_user( $username, $domain ) )
511 {
512 // Update provisioning password
513 if ( update_provision_pw( $username, $domain, $password ) < 0 )
514 {
515 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update provisioning password' ) );
516 break;
517 }
518 }
519 // Check for user in kamailio
520 if ( is_kamailio_subscriber( $username, $domain ) )
521 {
522 // Update kamailio password
523 if ( update_kamailio_pw( $username, $domain, $password ) < 0 )
524 {
525 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio password' ) );
526 break;
527 }
528 }
529 print json_encode( array ( 'response' => 'ok', 'detail' => 'Password changed for user '.$username.'@'.$domain.'.'));
530 break;
531 }
532 else
533 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
534 break;
535
536
537 case "/change_email":
538 /*
539 Required parameters should be...
540 ( username & domain ) | user
541 email
542 */
543 if ( array_key_exists('email', $_POST) &&
544 ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
545 || array_key_exists('user', $_POST) ))
546
547 {
548 $username = "";
549 $domain = "";
550 if ( array_key_exists('username', $_POST) )
551 {
552 $username = $_POST['username'];
553 $domain = $_POST['domain'];
554 }
555 else
556 {
557 $user = split_sipaddress($_POST['user']);
558 if ( !$user )
559 {
560 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
561 break;
562 }
563 list ( $username, $domain ) = $user;
564 }
565 $email = $_POST['email'];
566
567 // Check for user in kamailio
568 if ( is_kamailio_subscriber( $username, $domain ) )
569 {
570 // Update kamailio email
571 if ( update_kamailio_email( $username, $domain, $email ) < 0 )
572 {
573 print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio email' ) );
574 break;
575 }
576 }
577 print json_encode( array ( 'response' => 'ok', 'user' => $username.'@'.$domain, 'email' => $email));
578 break;
579 }
580 else
581 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
582 break;
583
584
585
586
587
588
589
590 case "/update":
591 /*
592 Required parameters should be...
593 ( username & domain ) | user
594
595 * Verify that no associations/relations exist in 'hermes.phones'
596 * Verify that the user exists ...
597 * Test to see of user exists in 'hermes.users'
598 * Test to see of user exists in 'kamailio.subscriber'.
599 * If no such user exists, return 'response' => 'failed' with 'cause' => 'nonexistant'
600 * Get update parameters, and change as appropriate ;)
601 * On other failures, return 'response' => 'failed' with 'cause' => 'error' (may set 'detail' => 'message')
602 */
603 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
604 || array_key_exists('user', $_POST) )
605
606 {
607 $username = "";
608 $domain = "";
609 if ( array_key_exists('username', $_POST) )
610 {
611 $username = $_POST['username'];
612 $domain = $_POST['domain'];
613 }
614 else
615 {
616 $user = split_sipaddress($_POST['user']);
617 if ( !$user )
618 {
619 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
620 break;
621 }
622 list ( $username, $domain ) = $user;
623 }
624 if ( ! is_provision_user ( $username, $domain ) )
625 {
626 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
627 break;
628 }
629 $updated = array();
630 $failed = array();
631 $error = 1;
632 $result_text = "";
633 $params = array('displayname', 'dialplan', 'linetext', 'registrar', 'r_port', 'proxy', 'p_port');
634 foreach ( $params as $p )
635 {
636 if ( array_key_exists($p, $_POST ) )
637 {
638 $data = $_POST[$p];
639 $t = update_provision_data($p, $username, $domain, $data);
640 if ( $t != true )
641 {
642 $error = $t;
643 array_push($failed, $p);
644 }
645 else
646 {
647 array_push( $updated, $p);
648 }
649 }
650 }
651 $res = array();
652 if ( ( $error == 1 ) || ( $error == 0 ) )
653 {
654 $res['response'] = 'ok';
655 $res['skipped'] = $failed;
656 }
657 else if ( $error == -1 )
658 {
659 $res['response'] = 'failed';
660 $res['cause'] = 'param';
661 $res['detail'] = 'Invalid parameters';
662 $res['failed'] = $failed;
663 }
664 else if ( $error == -2 )
665 {
666 $res['response'] = 'failed';
667 $res['cause'] = 'dbfail';
668 $res['detail'] = 'Database failure';
669 $res['failed'] = $failed;
670 }
671 else
672 $res['response'] = 'error'; // Wait, what?
673
674 $res['updated'] = $updated;
675
676 print json_encode ( $res );
677 }
678 else
679 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
680 break;
681 case "/available":
682 if ( ( array_key_exists( 'username', $_POST) && array_key_exists( 'domain', $_POST ) )
683 || array_key_exists('user', $_POST) )
684
685 {
686 $username = "";
687 $domain = "";
688 if ( array_key_exists('username', $_POST) )
689 {
690 $username = $_POST['username'];
691 $domain = $_POST['domain'];
692 }
693 else
694 {
695 $user = split_sipaddress($_POST['user']);
696 if ( !$user )
697 {
698 print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
699 break;
700 }
701 list ( $username, $domain ) = $user;
702 }
703 if ( is_provision_user ( $username, $domain ) )
704 {
705 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists in provisioning configuration' ));
706 break;
707 }
708 if ( is_kamailio_subscriber ( $username, $domain ) )
709 {
710 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'User already exists as a Kamailio subscriber' ));
711 break;
712 }
713 if ( alias_exists ( $username, $domain ) )
714 {
715 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'Username exists as an alias' ));
716 break;
717 }
718 print json_encode( array ( 'response' => 'ok', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
719
720 }
721 else
722 print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
723 break;
724 case "/gen_pw":
725 print generate_password();
726 break;
727 default:
728 print json_encode ( array( 'response' => 'invalid') );
729 }
730 mysql_close( $config['sql_link'] );
731 ?>