]>
git.defcon.no Git - hermes/blob - api/lib/user_functions.php
b15b7410437bf091132f98a6b5f2c3d9f042e748
2 require_once ( 'config.php' );
3 require_once ( 'lib/common_functions.php' );
4 require_once ( 'lib/db_functions.php' );
6 $config = get_config ();
8 function generate_password ( $length = null )
11 if ( ! $length ) $length = $config [ 'standard_password_length' ];
14 while ( strlen ( $string ) < $length )
16 $string .= crypt ( substr ( md5 ( rand (). rand ()), 0 , $length +
1 ));
17 $string = preg_replace ( '/\W/' , '' , $string );
19 return substr ( $string , 1 , $length );
24 function is_kamailio_subscriber ( $user , $domain )
27 $query = sprintf ( "SELECT username FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
28 $config [ 'kamailio_subscriber_table' ],
32 return sql_dbtest_numrows ( $config [ 'kamailio_db' ], $query , 1 );
35 function is_provision_user ( $user , $domain )
38 $query = sprintf ( "SELECT username FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
39 $config [ 'hermes_users_table' ],
43 return sql_dbtest_numrows ( $config [ 'hermes_db' ], $query , 1 );
46 function add_kamailio_subscriber ( $username , $domain , $password , $email )
51 $ha1 = md5 ( $username . ":" . $domain . ":" . $password );
52 $ha1b = md5 ( $username . "@" . $domain . ":" . $domain . ":" . $password );
54 $query = sprintf ( "INSERT INTO %s (username, domain, password, email_address, ha1, ha1b) VALUES (' %s ',' %s ',' %s ', ' %s ', ' %s ', ' %s ')" ,
55 $config [ 'kamailio_subscriber_table' ],
63 if ( ! sql_dbexec ( $config [ 'kamailio_db' ], $query ) ) return false ;
67 function delete_kamailio_subscriber ( $username , $domain )
72 $query = sprintf ( "SELECT id FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
73 $config [ 'kamailio_subscriber_table' ],
77 $row = sql_dbquery_single ( $config [ 'kamailio_db' ], $query );
78 if (! $row ) return false ;
79 $user_rowid = $row [ 'id' ];
80 if ( ! $user_rowid ) return false ;
82 $query = sprintf ( "DELETE FROM %s WHERE id = %d AND username = ' %s ' AND domain = ' %s '" ,
83 $config [ 'kamailio_subscriber_table' ],
88 if ( sql_dbexec_rows ( $config [ 'kamailio_db' ], $query ) != 1 ) return false ;
93 function update_kamailio_pw ( $username , $domain , $password )
97 if ( ! ( $username && $domain && $password ))
100 $query = sprintf ( "UPDATE %s SET password = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
101 $config [ 'kamailio_subscriber_table' ],
102 sql_clean ( $password ),
103 sql_clean ( $username ),
107 if ( sql_dbexec_rows ( $config [ 'kamailio_db' ], $query ) != 1 ) return false ;
111 function update_kamailio_email ( $username , $domain , $email )
115 if ( ! ( $username && $domain && $email ))
118 $query = sprintf ( "UPDATE %s SET email_address = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
119 $config [ 'kamailio_subscriber_table' ],
121 sql_clean ( $username ),
125 if ( sql_dbexec_rows ( $config [ 'kamailio_db' ], $query ) != 1 ) return false ;
130 function add_provision_user ( $username , $password , $domain , $authid , $registrar , $r_port , $proxy , $p_port , $displayname , $dialplan , $linetext )
133 if ( is_provision_user ( $username , $password ) ) return false ;
134 $query = sprintf ( "INSERT INTO %s ( username, password, displayname, domain, registrar, r_port, proxy, p_port, dialplan, authid, linetext )
135 VALUES (' %s ', ' %s ', ' %s ', ' %s ', ' %s ', %d , ' %s ', %d , ' %s ', ' %s ', ' %s ')" ,
136 $config [ 'hermes_users_table' ],
137 sql_clean ( $username ),
138 sql_clean ( $password ),
139 sql_clean ( $displayname ),
141 sql_clean ( $registrar ),
145 sql_clean ( $dialplan ),
149 if ( ! sql_dbexec ( $config [ 'hermes_db' ], $query ) ) return false ;
152 function get_provision_userid ( $username , $domain )
156 $query = sprintf ( "SELECT id FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
157 $config [ 'hermes_users_table' ],
158 sql_clean ( $username ),
162 $row = sql_dbquery_single ( $config [ 'hermes_db' ], $query );
163 if (! $row ) return false ;
164 $user_rowid = $row [ 'id' ];
168 function delete_provision_user ( $username , $domain )
172 $user_rowid = get_provision_userid ( $username , $domain );
173 if ( ! $user_rowid ) return false ;
175 $query = sprintf ( "DELETE FROM %s WHERE id = %d AND username = ' %s ' AND domain = ' %s '" ,
176 $config [ 'hermes_users_table' ],
178 sql_clean ( $username ),
181 if ( sql_dbexec_rows ( $config [ 'hermes_db' ], $query ) != 1 ) return false ;
185 function update_provision_data ( $param , $username , $domain , $data )
190 $param == "displayname" ||
191 $param == "dialplan" ||
192 $param == "linetext" ||
193 $param == "registrar" ||
194 $param == "r_port" ||
200 if ( ! ( $username && $domain ))
203 if ( ! is_provision_user ( $username , $domain ) )
206 $query = sprintf ( "UPDATE %s SET %s = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
207 $config [ 'hermes_users_table' ],
210 sql_clean ( $username ),
214 $res = sql_dbexec_rows ( $config [ 'hermes_db' ], $query );
216 if ( $res < 0 ) return - 2 ;
217 if ( $res > 1 ) return - 2 ;
221 function update_provision_pw ( $username , $domain , $password )
225 if ( ! ( $username && $domain && $password ))
228 $query = sprintf ( "UPDATE %s SET password = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
229 $config [ 'hermes_users_table' ],
230 sql_clean ( $password ),
231 sql_clean ( $username ),
235 if ( sql_dbexec_rows ( $config [ 'hermes_db' ], $query ) != 1 ) return false ;
239 function list_users ( $search = null )
242 $query = sprintf ( "SELECT CONCAT(username, '@', domain), displayname FROM %s ORDER BY username,domain" , $config [ 'hermes_users_table' ] );
244 if ( array_key_exists ( 'search' , $_POST ) )
246 $search = $_POST [ 'search' ]; // TODO: Add some sanitation and input validation!
247 $query = sprintf ( "SELECT CONCAT(username, '@', domain) FROM %s WHERE CONCAT(username, '@', domain) LIKE '%% %s %%' ORDER BY username,domain" , $config [ 'hermes_users_table' ], sql_clean ( $search ) );
250 $result = sql_dbquery ( $config [ 'hermes_db' ], $query );
251 if ( ! $result ) return null ;
253 while ( $row = @mysql_fetch_row
( $result ) )
255 array_push ( $list , array ( "user" => $row [ 0 ], "displayname" => $row [ 1 ] ) );
258 print json_encode ( array ( 'response' => 'ok' , 'list' => $list ));
262 function get_userdata ( $username , $domain )
265 if ( is_kamailio_subscriber ( $username , $domain ) // User must be present in both!
266 && is_provision_user ( $username , $domain ) ) $type = 'local' ;
267 else if ( is_provision_user ( $username , $domain ) ) $type = 'remote' ;
270 $provision_data = null ;
271 $kamailio_data = null ;
273 $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 '" ,
274 $config [ 'hermes_users_table' ],
275 sql_clean ( $username ),
278 $provision_data = sql_dbquery_single ( $config [ 'hermes_db' ] , $query_provision );
279 if ( ! $provision_data ) return false ;
281 if ( $type == 'local' )
283 // WARNING: Note the typo in the name of the 'permittedcalls' column!
284 $query_kamailio = sprintf ( "SELECT id, username, domain, password, email_address, ha1, ha1b, rpid, permitedcalls FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
285 $config [ 'kamailio_subscriber_table' ],
286 sql_clean ( $username ),
288 $kamailio_data = sql_dbquery_single ( $config [ 'kamailio_db' ] , $query_kamailio );
289 if ( ! $kamailio_data ) return false ;
291 $user [ 'type' ] = $type ;
292 $user [ 'username' ] = $provision_data [ 'username' ];
293 $user [ 'password' ] = $provision_data [ 'password' ];
294 $user [ 'domain' ] = $provision_data [ 'domain' ];
295 $user [ 'authid' ] = $provision_data [ 'authid' ];
296 $user [ 'registrar' ] = $provision_data [ 'registrar' ];
297 $user [ 'r_port' ] = $provision_data [ 'r_port' ];
298 $user [ 'proxy' ] = $provision_data [ 'proxy' ];
299 $user [ 'p_port' ] = $provision_data [ 'p_port' ];
300 $user [ 'dialplan' ] = $provision_data [ 'dialplan' ];
301 $user [ 'displayname' ] = $provision_data [ 'displayname' ];
302 $user [ 'linetext' ] = $provision_data [ 'linetext' ];
303 if ( $type == 'local' )
305 $user [ 'email' ] = $kamailio_data [ 'email_address' ];
306 $user [ 'ha1' ] = $kamailio_data [ 'ha1' ];
307 $user [ 'ha1b' ] = $kamailio_data [ 'ha1b' ];
308 $user [ 'rpid' ] = $kamailio_data [ 'rpid' ];
309 $user [ 'permittedcalls' ] = $kamailio_data [ 'permitedcalls' ];