]>
git.defcon.no Git - hermes/blob - api/lib/user_functions.php
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 = 24 )
11 while ( strlen ( $string ) < $length )
12 $string .= substr ( md5 ( rand (). rand ()), 0 , $length );
13 return substr ( $string , 0 , $length );
18 function is_kamailio_subscriber ( $user , $domain )
21 $query = sprintf ( "SELECT username FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
22 $config [ 'kamailio_subscriber_table' ],
26 return sql_dbtest_numrows ( $config [ 'kamailio_db' ], $query , 1 );
29 function is_provision_user ( $user , $domain )
32 $query = sprintf ( "SELECT username FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
33 $config [ 'provision_users_table' ],
37 return sql_dbtest_numrows ( $config [ 'provision_db' ], $query , 1 );
40 function add_kamailio_subscriber ( $username , $domain , $password , $email )
45 $ha1 = md5 ( $username . ":" . $domain . ":" . $password );
46 $ha1b = md5 ( $username . "@" . $domain . ":" . $domain . ":" . $password );
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' ],
57 if ( ! sql_dbexec ( $config [ 'kamailio_db' ], $query ) ) return false ;
61 function delete_kamailio_subscriber ( $username , $domain )
66 $query = sprintf ( "SELECT id FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
67 $config [ 'kamailio_subscriber_table' ],
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 ;
76 $query = sprintf ( "DELETE FROM %s WHERE id = %d AND username = ' %s ' AND domain = ' %s '" ,
77 $config [ 'kamailio_subscriber_table' ],
82 if ( sql_dbexec_rows ( $config [ 'kamailio_db' ], $query ) != 1 ) return false ;
87 function update_kamailio_pw ( $username , $domain , $password )
91 if ( ! ( $username && $domain && $password ))
94 $query = sprintf ( "UPDATE %s SET password = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
95 $config [ 'kamailio_subscriber_table' ],
101 if ( sql_dbexec_rows ( $config [ 'kamailio_db' ], $query ) != 1 ) return false ;
105 function update_kamailio_email ( $username , $domain , $email )
109 if ( ! ( $username && $domain && $email ))
112 $query = sprintf ( "UPDATE %s SET email_address = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
113 $config [ 'kamailio_subscriber_table' ],
115 sql_clean ( $username ),
119 if ( sql_dbexec_rows ( $config [ 'kamailio_db' ], $query ) != 1 ) return false ;
124 function add_provision_user ( $username , $password , $domain , $authid , $registrar , $r_port , $proxy , $p_port , $displayname , $dialplan , $linetext )
127 if ( is_provision_user ( $username , $password ) ) return false ;
128 $query = sprintf ( "INSERT INTO %s ( username, password, displayname, domain, registrar, r_port, proxy, p_port, dialplan, authid, linetext )
129 VALUES (' %s ', ' %s ', ' %s ', ' %s ', ' %s ', %d , ' %s ', %d , ' %s ', ' %s ', ' %s ')" ,
130 $config [ 'provision_users_table' ],
131 sql_clean ( $username ),
132 sql_clean ( $password ),
133 sql_clean ( $displayname ),
135 sql_clean ( $registrar ),
139 sql_clean ( $dialplan ),
143 if ( ! sql_dbexec ( $config [ 'provision_db' ], $query ) ) return false ;
146 function get_provision_userid ( $username , $domain )
150 $query = sprintf ( "SELECT id FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
151 $config [ 'provision_users_table' ],
152 sql_clean ( $username ),
156 $row = sql_dbquery_single ( $config [ 'provision_db' ], $query );
157 if (! $row ) return false ;
158 $user_rowid = $row [ 'id' ];
162 function delete_provision_user ( $username , $domain )
166 $user_rowid = get_provision_userid ( $username , $domain );
167 if ( ! $user_rowid ) return false ;
169 $query = sprintf ( "DELETE FROM %s WHERE id = %d AND username = ' %s ' AND domain = ' %s '" ,
170 $config [ 'provision_users_table' ],
172 sql_clean ( $username ),
175 if ( sql_dbexec_rows ( $config [ 'provision_db' ], $query ) != 1 ) return false ;
179 function update_provision_data ( $param , $username , $domain , $data )
184 $param == "displayname" ||
185 $param == "dialplan" ||
186 $param == "linetext" ||
187 $param == "registrar" ||
188 $param == "r_port" ||
194 if ( ! ( $username && $domain ))
197 if ( ! is_provision_user ( $username , $domain ) )
200 $query = sprintf ( "UPDATE %s SET %s = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
201 $config [ 'provision_users_table' ],
204 sql_clean ( $username ),
208 $res = sql_dbexec_rows ( $config [ 'provision_db' ], $query );
210 if ( $res < 0 ) return - 2 ;
211 if ( $res > 1 ) return - 2 ;
215 function update_provision_pw ( $username , $domain , $password )
219 if ( ! ( $username && $domain && $password ))
222 $query = sprintf ( "UPDATE %s SET password = ' %s ' WHERE username = ' %s ' AND domain = ' %s '" ,
223 $config [ 'provision_users_table' ],
224 sql_clean ( $password ),
225 sql_clean ( $username ),
229 if ( sql_dbexec_rows ( $config [ 'provision_db' ], $query ) != 1 ) return false ;
233 function list_users ( $search = null )
236 $query = sprintf ( "SELECT CONCAT(username, '@', domain), displayname FROM %s ORDER BY username,domain" , $config [ 'provision_users_table' ] );
238 if ( array_key_exists ( 'search' , $_GET ) )
240 $search = $_GET [ 'search' ]; // TODO: Add some sanitation and input validation!
241 $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 ) );
244 $result = sql_dbquery ( $config [ 'provision_db' ], $query );
245 if ( ! $result ) return null ;
247 while ( $row = @mysql_fetch_row
( $result ) )
249 array_push ( $list , array ( "user" => $row [ 0 ], "displayname" => $row [ 1 ] ) );
252 print json_encode ( array ( 'response' => 'ok' , 'list' => $list ));
256 function get_userdata ( $username , $domain )
259 if ( is_kamailio_subscriber ( $username , $domain ) // User must be present in both!
260 && is_provision_user ( $username , $domain ) ) $type = 'local' ;
261 else if ( is_provision_user ( $username , $domain ) ) $type = 'remote' ;
264 $provision_data = null ;
265 $kamailio_data = null ;
267 $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 '" ,
268 $config [ 'provision_users_table' ],
269 sql_clean ( $username ),
272 $provision_data = sql_dbquery_single ( $config [ 'provision_db' ] , $query_provision );
273 if ( ! $provision_data ) return false ;
275 if ( $type == 'local' )
277 // WARNING: Note the typo in the name of the 'permittedcalls' column!
278 $query_kamailio = sprintf ( "SELECT id, username, domain, password, email_address, ha1, ha1b, rpid, permitedcalls FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
279 $config [ 'kamailio_subscriber_table' ],
280 sql_clean ( $username ),
282 $kamailio_data = sql_dbquery_single ( $config [ 'kamailio_db' ] , $query_kamailio );
283 if ( ! $kamailio_data ) return false ;
285 $user [ 'type' ] = $type ;
286 $user [ 'username' ] = $provision_data [ 'username' ];
287 $user [ 'password' ] = $provision_data [ 'password' ];
288 $user [ 'domain' ] = $provision_data [ 'domain' ];
289 $user [ 'authid' ] = $provision_data [ 'authid' ];
290 $user [ 'registrar' ] = $provision_data [ 'registrar' ];
291 $user [ 'r_port' ] = $provision_data [ 'r_port' ];
292 $user [ 'proxy' ] = $provision_data [ 'proxy' ];
293 $user [ 'p_port' ] = $provision_data [ 'p_port' ];
294 $user [ 'dialplan' ] = $provision_data [ 'dialplan' ];
295 $user [ 'displayname' ] = $provision_data [ 'displayname' ];
296 $user [ 'linetext' ] = $provision_data [ 'linetext' ];
297 if ( $type == 'local' )
299 $user [ 'email' ] = $kamailio_data [ 'email_address' ];
300 $user [ 'ha1' ] = $kamailio_data [ 'ha1' ];
301 $user [ 'ha1b' ] = $kamailio_data [ 'ha1b' ];
302 $user [ 'rpid' ] = $kamailio_data [ 'rpid' ];
303 $user [ 'permittedcalls' ] = $kamailio_data [ 'permitedcalls' ];