]> git.defcon.no Git - hermes/blob - lib/user_functions.php
180843717388df5547d7883c6e14c2d8bd660a2d
[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 = 24 )
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_rows( $config['kamailio_db'], $query) != 1 ) return false;
83 return true;
84 }
85
86
87 function update_kamailio_pw ( $username, $domain, $password )
88 {
89 global $config;
90
91 if ( ! ($username && $domain && $password))
92 return false;
93
94 $query = sprintf("UPDATE %s SET password = '%s' WHERE username = '%s' AND domain = '%s'",
95 $config['kamailio_subscriber_table'],
96 sql_clean($password),
97 sql_clean($username),
98 sql_clean($domain)
99
100 );
101 if ( sql_dbexec_rows( $config['kamailio_db'], $query) != 1 ) return false;
102 return true;
103 }
104
105 function add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext )
106 { global $config;
107
108 if ( is_provision_user( $username, $password ) ) return false;
109 $query = sprintf ("INSERT INTO %s ( username, password, displayname, domain, registrar, r_port, proxy, p_port, dialplan, authid, linetext )
110 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, '%s', '%s', '%s')",
111 $config['provision_users_table'],
112 sql_clean($username),
113 sql_clean($password),
114 sql_clean($displayname),
115 sql_clean($domain),
116 sql_clean($registrar),
117 $r_port,
118 sql_clean($proxy),
119 $p_port,
120 sql_clean($dialplan),
121 sql_clean($authid),
122 sql_clean($linetext)
123 );
124 if ( ! sql_dbexec( $config['provision_db'], $query ) ) return false;
125 return true;
126 }
127 function get_provision_userid ( $username, $domain )
128 {
129 global $config;
130
131 $query = sprintf("SELECT id FROM %s WHERE username = '%s' AND domain = '%s'",
132 $config['provision_users_table'],
133 sql_clean($username),
134 sql_clean($domain)
135 );
136
137 $row = sql_dbquery_single( $config['provision_db'], $query );
138 if (!$row) return false;
139 $user_rowid = $row['id'];
140 return $user_rowid;
141 }
142
143 function delete_provision_user( $username, $domain )
144 {
145 global $config;
146
147 $user_rowid = get_provision_userid( $username, $domain );
148 if ( !$user_rowid ) return false;
149
150 $query = sprintf( "DELETE FROM %s WHERE id = %d AND username = '%s' AND domain = '%s'",
151 $config['provision_users_table'],
152 $user_rowid,
153 sql_clean($username),
154 sql_clean($domain)
155 );
156 if ( sql_dbexec_rows( $config['provision_db'], $query) != 1 ) return false;
157 return true;
158 }
159
160 function update_provision_pw ( $username, $domain, $password )
161 {
162 global $config;
163
164 if ( ! ($username && $domain && $password))
165 return false;
166
167 $query = sprintf("UPDATE %s SET password = '%s' WHERE username = '%s' AND domain = '%s'",
168 $config['provision_users_table'],
169 sql_clean($password),
170 sql_clean($username),
171 sql_clean($domain)
172
173 );
174 if ( sql_dbexec_rows( $config['provision_db'], $query) != 1 ) return false;
175 return true;
176 }
177
178 function list_users ( $search = null )
179 {
180 global $config;
181 $query = sprintf("SELECT CONCAT(username, '@', domain), displayname FROM %s ORDER BY username,domain", $config['provision_users_table'] );
182
183 if ( array_key_exists ( 'search', $_GET ) )
184 {
185 $search = $_GET['search']; // TODO: Add some sanitation and input validation!
186 $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 ) );
187 }
188
189 $result = sql_dbquery( $config['provision_db'], $query );
190 if ( !$result ) return null;
191 $list = array();
192 while ( $row = mysql_fetch_row( $result ) )
193 {
194 array_push( $list, array( "user" => $row[0], "displayname" => $row[1] ) );
195 }
196 return $list;
197 print json_encode( array( 'response' => 'ok', 'list' => $list ));
198
199
200 }
201 function get_userdata( $username, $domain )
202 {
203 global $config;
204 if ( is_kamailio_subscriber( $username, $domain ) // User must be present in both!
205 && is_provision_user( $username, $domain ) ) $type = 'local';
206 else if ( is_provision_user( $username, $domain ) ) $type = 'remote';
207 else return null;
208
209 $provision_data = null;
210 $kamailio_data = null;
211
212 $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'",
213 $config['provision_users_table'],
214 sql_clean($username),
215 sql_clean($domain));
216
217 $provision_data = sql_dbquery_single( $config['provision_db'] , $query_provision );
218 if ( ! $provision_data ) return false;
219
220 if ( $type == 'local' )
221 {
222 // WARNING: Note the typo in the name of the 'permittedcalls' column!
223 $query_kamailio = sprintf ("SELECT id, username, domain, password, email_address, ha1, ha1b, rpid, permitedcalls FROM %s WHERE username = '%s' AND domain = '%s'",
224 $config['kamailio_subscriber_table'],
225 sql_clean($username),
226 sql_clean($domain));
227 $kamailio_data = sql_dbquery_single( $config['kamailio_db'] , $query_kamailio );
228 if ( ! $kamailio_data ) return false;
229 }
230 $user['type'] = $type;
231 $user['username'] = $provision_data['username'];
232 $user['password'] = $provision_data['password'];
233 $user['domain'] = $provision_data['domain'];
234 $user['authid'] = $provision_data['authid'];
235 $user['registrar'] = $provision_data['registrar'];
236 $user['r_port'] = $provision_data['r_port'];
237 $user['proxy'] = $provision_data['proxy'];
238 $user['p_port'] = $provision_data['p_port'];
239 $user['dialplan'] = $provision_data['dialplan'];
240 $user['displayname'] = $provision_data['displayname'];
241 $user['linetext'] = $provision_data['linetext'];
242 if ( $type == 'local' )
243 {
244 $user['email'] = $kamailio_data['email'];
245 $user['ha1'] = $kamailio_data['ha1'];
246 $user['ha1b'] = $kamailio_data['ha1b'];
247 $user['rpid'] = $kamailio_data['rpid'];
248 $user['permittedcalls'] = $kamailio_data['permitedcalls'];
249 }
250
251 return $user;
252 }
253
254 ?>