]>
git.defcon.no Git - hermes/blob - lib/user_functions.php
180843717388df5547d7883c6e14c2d8bd660a2d
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 add_provision_user ( $username , $password , $domain , $authid , $registrar , $r_port , $proxy , $p_port , $displayname , $dialplan , $linetext )
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 ),
116 sql_clean ( $registrar ),
120 sql_clean ( $dialplan ),
124 if ( ! sql_dbexec ( $config [ 'provision_db' ], $query ) ) return false ;
127 function get_provision_userid ( $username , $domain )
131 $query = sprintf ( "SELECT id FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
132 $config [ 'provision_users_table' ],
133 sql_clean ( $username ),
137 $row = sql_dbquery_single ( $config [ 'provision_db' ], $query );
138 if (! $row ) return false ;
139 $user_rowid = $row [ 'id' ];
143 function delete_provision_user ( $username , $domain )
147 $user_rowid = get_provision_userid ( $username , $domain );
148 if ( ! $user_rowid ) return false ;
150 $query = sprintf ( "DELETE FROM %s WHERE id = %d AND username = ' %s ' AND domain = ' %s '" ,
151 $config [ 'provision_users_table' ],
153 sql_clean ( $username ),
156 if ( sql_dbexec_rows ( $config [ 'provision_db' ], $query ) != 1 ) return false ;
160 function update_provision_pw ( $username , $domain , $password )
164 if ( ! ( $username && $domain && $password ))
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 ),
174 if ( sql_dbexec_rows ( $config [ 'provision_db' ], $query ) != 1 ) return false ;
178 function list_users ( $search = null )
181 $query = sprintf ( "SELECT CONCAT(username, '@', domain), displayname FROM %s ORDER BY username,domain" , $config [ 'provision_users_table' ] );
183 if ( array_key_exists ( 'search' , $_GET ) )
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 ) );
189 $result = sql_dbquery ( $config [ 'provision_db' ], $query );
190 if ( ! $result ) return null ;
192 while ( $row = mysql_fetch_row ( $result ) )
194 array_push ( $list , array ( "user" => $row [ 0 ], "displayname" => $row [ 1 ] ) );
197 print json_encode ( array ( 'response' => 'ok' , 'list' => $list ));
201 function get_userdata ( $username , $domain )
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' ;
209 $provision_data = null ;
210 $kamailio_data = null ;
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 ),
217 $provision_data = sql_dbquery_single ( $config [ 'provision_db' ] , $query_provision );
218 if ( ! $provision_data ) return false ;
220 if ( $type == 'local' )
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 ),
227 $kamailio_data = sql_dbquery_single ( $config [ 'kamailio_db' ] , $query_kamailio );
228 if ( ! $kamailio_data ) return false ;
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' )
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' ];