]> git.defcon.no Git - hermes/blob - lib/user_functions.php
Hermes is a framework for managing SIP-accounts, aliases and automatic phone-provisio...
[hermes] / lib / user_functions.php
1 <?php
2 require_once('config.php');
3 require_once('lib/common_functions.php');
4 require_once('lib/db_functions.php');
5
6 $config = get_config();
7
8 function generate_password( $length = 32 )
9 {
10 $string = "";
11 while ( strlen( $string ) < $length )
12 $string .= substr(md5(rand().rand()), 0, $length);
13 return substr( $string, 0, $length );
14
15 }
16
17
18 function is_kamailio_subscriber ( $user, $domain )
19 {
20 global $config;
21 $query = sprintf("SELECT username FROM %s WHERE username = '%s' AND domain = '%s'",
22 $config['kamailio_subscriber_table'],
23 sql_clean( $user ),
24 sql_clean( $domain )
25 );
26 return sql_dbtest_numrows( $config['kamailio_db'], $query, 1);
27 }
28
29 function is_provision_user ( $user, $domain )
30 {
31 global $config;
32 $query = sprintf("SELECT username FROM %s WHERE username = '%s' AND domain = '%s'",
33 $config['provision_users_table'],
34 sql_clean( $user ),
35 sql_clean( $domain )
36 );
37 return sql_dbtest_numrows( $config['provision_db'], $query, 1);
38 }
39
40 function add_kamailio_subscriber( $username, $domain, $password, $email )
41 {
42
43 global $config;
44
45 $ha1 = md5( $username . ":" . $domain . ":" . $password );
46 $ha1b = md5( $username . "@" . $domain . ":" . $domain . ":" . $password );
47
48 $query = sprintf( "INSERT INTO %s (username, domain, password, email_address, ha1, ha1b) VALUES ('%s','%s','%s', '%s', '%s', '%s')",
49 $config['kamailio_subscriber_table'],
50 sql_clean($username),
51 sql_clean($domain),
52 sql_clean($password),
53 sql_clean($email),
54 $ha1,
55 $ha1b
56 );
57 if ( ! sql_dbexec( $config['kamailio_db'], $query ) ) return false;
58 return true;
59 }
60
61 function delete_kamailio_subscriber( $username, $domain )
62 {
63 global $config;
64
65
66 $query = sprintf("SELECT id FROM %s WHERE username = '%s' AND domain = '%s'",
67 $config['kamailio_subscriber_table'],
68 sql_clean($username),
69 sql_clean($domain)
70 );
71 $row = sql_dbquery_single( $config['kamailio_db'], $query );
72 if (!$row) return false;
73 $user_rowid = $row['id'];
74 if ( !$user_rowid ) return false;
75
76 $query = sprintf( "DELETE FROM %s WHERE id = %d AND username = '%s' AND domain = '%s'",
77 $config['kamailio_subscriber_table'],
78 $user_rowid,
79 sql_clean($username),
80 sql_clean($domain)
81 );
82 if ( ! sql_dbexec( $config['kamailio_db'], $query ) ) return false;
83 return true;
84 }
85
86 function add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext )
87 { global $config;
88
89 if ( is_provision_user( $username, $password ) ) return false;
90 $query = sprintf ("INSERT INTO %s ( username, password, displayname, domain, registrar, r_port, proxy, p_port, dialplan, authid, linetext )
91 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, '%s', '%s', '%s')",
92 $config['provision_users_table'],
93 sql_clean($username),
94 sql_clean($password),
95 sql_clean($displayname),
96 sql_clean($domain),
97 sql_clean($registrar),
98 $r_port,
99 sql_clean($proxy),
100 $p_port,
101 sql_clean($dialplan),
102 sql_clean($authid),
103 sql_clean($linetext)
104 );
105 if ( ! sql_dbexec( $config['provision_db'], $query ) ) return false;
106 return true;
107 }
108 function get_provision_userid ( $username, $domain )
109 {
110 global $config;
111
112 $query = sprintf("SELECT id FROM %s WHERE username = '%s' AND domain = '%s'",
113 $config['provision_users_table'],
114 sql_clean($username),
115 sql_clean($domain)
116 );
117
118 $row = sql_dbquery_single( $config['provision_db'], $query );
119 if (!$row) return false;
120 $user_rowid = $row['id'];
121 return $user_rowid;
122 }
123
124 function delete_provision_user( $username, $domain )
125 {
126 global $config;
127
128 $user_rowid = get_provision_userid( $username, $domain );
129 if ( !$user_rowid ) return false;
130
131 $query = sprintf( "DELETE FROM %s WHERE id = %d AND username = '%s' AND domain = '%s'",
132 $config['provision_users_table'],
133 $user_rowid,
134 sql_clean($username),
135 sql_clean($domain)
136 );
137 if ( ! sql_dbexec( $config['provision_db'], $query ) ) return false;
138 return true;
139 }
140
141 function list_users ( $search = null )
142 {
143 global $config;
144 $query = sprintf("SELECT CONCAT(username, '@', domain) FROM %s ORDER BY username,domain", $config['provision_users_table'] );
145
146 if ( array_key_exists ( 'search', $_GET ) )
147 {
148 $search = $_GET['search']; // TODO: Add some sanitation and input validation!
149 $query = sprintf("SELECT CONCAT(username, '@', domain) FROM %s WHERE CONCAT(username, '@', domain) LIKE '%%%s%%' ORDER BY username,domain", $config['provision_users_table'], sql_clean( $search ) );
150 }
151
152 $result = sql_dbquery( $config['provision_db'], $query );
153 if ( !$result ) return null;
154 $list = array();
155 while ( $row = mysql_fetch_row( $result ) )
156 {
157 array_push( $list, $row[0] );
158 }
159 return $list;
160 print json_encode( array( 'response' => 'ok', 'list' => $list ));
161
162
163 }
164 function get_userdata( $username, $domain )
165 {
166 global $config;
167 if ( is_kamailio_subscriber( $username, $domain ) // User must be present in both!
168 && is_provision_user( $username, $domain ) ) $type = 'local';
169 else if ( is_provision_user( $username, $domain ) ) $type = 'remote';
170 else return null;
171
172 $provision_data = null;
173 $kamailio_data = null;
174
175 $query_provision = sprintf ("SELECT id, username, password, displayname, domain, registrar, r_port, proxy, p_port, dialplan, authid, linetext FROM %s WHERE username = '%s' AND domain = '%s'",
176 $config['provision_users_table'],
177 sql_clean($username),
178 sql_clean($domain));
179
180 $provision_data = sql_dbquery_single( $config['provision_db'] , $query_provision );
181 if ( ! $provision_data ) return false;
182
183 if ( $type == 'local' )
184 {
185 // WARNING: Note the typo in the name of the 'permittedcalls' column!
186 $query_kamailio = sprintf ("SELECT id, username, domain, password, email_address, ha1, ha1b, rpid, permitedcalls FROM %s WHERE username = '%s' AND domain = '%s'",
187 $config['kamailio_subscriber_table'],
188 sql_clean($username),
189 sql_clean($domain));
190 $kamailio_data = sql_dbquery_single( $config['kamailio_db'] , $query_kamailio );
191 if ( ! $kamailio_data ) return false;
192 }
193 $user['type'] = $type;
194 $user['username'] = $provision_data['username'];
195 $user['password'] = $provision_data['password'];
196 $user['domain'] = $provision_data['domain'];
197 $user['authid'] = $provision_data['authid'];
198 $user['registrar'] = $provision_data['registrar'];
199 $user['r_port'] = $provision_data['r_port'];
200 $user['proxy'] = $provision_data['proxy'];
201 $user['p_port'] = $provision_data['p_port'];
202 $user['dialplan'] = $provision_data['dialplan'];
203 $user['displayname'] = $provision_data['displayname'];
204 $user['linetext'] = $provision_data['linetext'];
205 if ( $type == 'local' )
206 {
207 $user['email'] = $kamailio_data['email'];
208 $user['ha1'] = $kamailio_data['ha1'];
209 $user['ha1b'] = $kamailio_data['ha1b'];
210 $user['rpid'] = $kamailio_data['rpid'];
211 $user['permittedcalls'] = $kamailio_data['permitedcalls'];
212 }
213
214 return $user;
215 }
216
217 ?>