]>
git.defcon.no Git - hermes/blob - lib/user_functions.php
79445510bd58cb9e96410ffffad1146c42289b12
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 = 32 )
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 ( $config [ 'kamailio_db' ], $query ) ) return false ;
86 function add_provision_user ( $username , $password , $domain , $authid , $registrar , $r_port , $proxy , $p_port , $displayname , $dialplan , $linetext )
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' ],
95 sql_clean ( $displayname ),
97 sql_clean ( $registrar ),
101 sql_clean ( $dialplan ),
105 if ( ! sql_dbexec ( $config [ 'provision_db' ], $query ) ) return false ;
108 function get_provision_userid ( $username , $domain )
112 $query = sprintf ( "SELECT id FROM %s WHERE username = ' %s ' AND domain = ' %s '" ,
113 $config [ 'provision_users_table' ],
114 sql_clean ( $username ),
118 $row = sql_dbquery_single ( $config [ 'provision_db' ], $query );
119 if (! $row ) return false ;
120 $user_rowid = $row [ 'id' ];
124 function delete_provision_user ( $username , $domain )
128 $user_rowid = get_provision_userid ( $username , $domain );
129 if ( ! $user_rowid ) return false ;
131 $query = sprintf ( "DELETE FROM %s WHERE id = %d AND username = ' %s ' AND domain = ' %s '" ,
132 $config [ 'provision_users_table' ],
134 sql_clean ( $username ),
137 if ( ! sql_dbexec ( $config [ 'provision_db' ], $query ) ) return false ;
141 function list_users ( $search = null )
144 $query = sprintf ( "SELECT CONCAT(username, '@', domain) FROM %s ORDER BY username,domain" , $config [ 'provision_users_table' ] );
146 if ( array_key_exists ( 'search' , $_GET ) )
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 ) );
152 $result = sql_dbquery ( $config [ 'provision_db' ], $query );
153 if ( ! $result ) return null ;
155 while ( $row = mysql_fetch_row ( $result ) )
157 array_push ( $list , $row [ 0 ] );
160 print json_encode ( array ( 'response' => 'ok' , 'list' => $list ));
164 function get_userdata ( $username , $domain )
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' ;
172 $provision_data = null ;
173 $kamailio_data = null ;
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 ),
180 $provision_data = sql_dbquery_single ( $config [ 'provision_db' ] , $query_provision );
181 if ( ! $provision_data ) return false ;
183 if ( $type == 'local' )
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 ),
190 $kamailio_data = sql_dbquery_single ( $config [ 'kamailio_db' ] , $query_kamailio );
191 if ( ! $kamailio_data ) return false ;
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' )
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' ];