]> git.defcon.no Git - hermes/blob - alias.php
Adding ignore file, initially ignore the running config file
[hermes] / alias.php
1 <?php
2 require_once('config.php');
3 require_once('lib/user_functions.php');
4 require_once('lib/number_functions.php');
5 require_once('lib/common_functions.php');
6 require_once('lib/db_functions.php');
7 require_once('lib/alias_functions.php');
8
9 $config = get_config();
10
11 $config['sql_link'] = @mysql_connect(
12 $config['sql_server'],
13 $config['sql_username'],
14 $config['sql_password']
15 );
16 if ( !$config['sql_link'] )
17 {
18 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
19 exit;
20 }
21
22 //*************************************************************************************
23 switch ( $_SERVER['PATH_INFO'] )
24 {
25 case "/list":
26 break;
27 case "/add":
28 if ( array_key_exists( 'destination', $_GET)
29 //&& array_key_exists( 'domain', $_GET )
30 && array_key_exists( 'alias_username', $_GET )
31 && array_key_exists( 'alias_domain', $_GET ) )
32 {
33 $alias_username = $_GET['alias_username'];
34 $alias_domain = $_GET['alias_domain'];
35
36 if ( !verify_sipadress($_GET['destination']) )
37 {
38 // TODO: Provide a better response..
39 print json_encode ( array( 'response' => 'invalid', 'cause' => 'destination' ) );
40 break;
41 }
42 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
43 {
44 // TODO: Provide a better response..
45 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
46 break;
47 }
48
49 list ( $dest_username, $dest_domain ) = split_sipaddress( $_GET['destination']);
50
51 if ( (!$dest_username)||(!$dest_domain))
52 {
53 // TODO: Provide a better response..
54 print json_encode ( array( 'response' => 'invalid', 'cause' => 'alias' ) );
55 break;
56 }
57 if ( !is_kamailio_domain( $alias_domain ) )
58 {
59 print json_encode ( array( 'response' => 'invalid', 'cause' => 'nxdomain' ) );
60 break;
61 }
62 if ( is_kamailio_domain( $dest_domain) && ( !is_kamailio_subscriber($dest_username, $dest_domain) ) )
63 {
64 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'Requesting a local alias, but there is no such user'));
65 break;
66 }
67 if ( verify_e164( $alias_username ) && is_kamailio_subscriber($dest_username, $dest_domain) )
68 {
69 $t = get_e164_alias( $dest_username, $dest_domain );
70 if ( $t )
71 {
72 print json_encode ( array(
73 'response' => 'failed',
74 'cause' => 'exists',
75 'detail' => 'User already has E164 number alias',
76 'alias' => $t ));
77 break;
78 }
79 }
80 if ( alias_exists ( $alias_username, $alias_domain ) )
81 {
82 print json_encode( array ( 'response' => 'failed', 'cause' => 'exists', 'detail' => 'The requested alias is already present.'));
83 break;
84 }
85 if ( add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain ) )
86 {
87 print json_encode( array ('response' => 'ok',
88 'alias' => $alias_username . "@" . $alias_domain,
89 'destination' => $dest_username . "@" . $dest_domain ));
90 break;
91 }
92 print json_encode ( array ( 'response' => 'error' ));
93 break;
94 }
95 else
96 {
97 print json_encode ( array( 'response' => 'invalid') );
98 }
99 break;
100 case "/remove":
101 if ( array_key_exists( 'alias_username', $_GET )
102 && array_key_exists( 'alias_domain', $_GET ) )
103 {
104 $alias_username = $_GET['alias_username'];
105 $alias_domain = $_GET['alias_domain'];
106
107 if ( !verify_sipadress( $alias_username . "@" . $alias_domain) )
108 {
109 // TODO: Provide a better response..
110 print json_encode ( array( 'response' => 'invalid', 'cause' => 'address', 'detail' => 'Not a valid SIP address' ) );
111 break;
112 }
113 if ( ! alias_exists ( $alias_username, $alias_domain ) )
114 {
115 print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => 'The requested alias does not exist.'));
116 break;
117 }
118 if ( remove_alias( $alias_username, $alias_domain ) )
119 {
120 print json_encode( array ('response' => 'ok',
121 'alias' => $alias_username . "@" . $alias_domain));
122 break;
123 }
124 print json_encode ( array ( 'response' => 'error' ));
125 break;
126 }
127 print json_encode ( array( 'response' => 'invalid') );
128 break;
129 default:
130 print json_encode ( array( 'response' => 'invalid') );
131 }
132 mysql_close( $config['sql_link'] );
133 ?>