function _login_user($new_ext, $password) { $db = gs_db_master_connect(); $remote_addr = @$_SERVER['REMOTE_ADDR']; //FIXME $new_uid = (int) $db->executeGetOne('SELECT `_user_id` FROM `ast_sipfriends` WHERE `name`=\'' . $db->escape($new_ext) . '\''); if ($new_uid < 1) { # unknown user gs_log(GS_LOG_NOTICE, "Mobility: Unknown extension {$new_ext}"); return false; } $pin = $db->executeGetOne('SELECT `pin` FROM `users` WHERE `id`=' . $new_uid); if (trim($pin) == '') { gs_log(GS_LOG_NOTICE, "Mobility:Unknown user or no PIN"); return false; } if ((string) $pin != (string) $password) { # wrong password gs_log(GS_LOG_NOTICE, "Mobility: Login attempt for ext. {$new_ext} - Wrong PIN number"); return false; } # get id of the phone # $remote_addr = @$_SERVER['REMOTE_ADDR']; //FIXME $old_uid = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `current_ip`=\'' . $db->escape($remote_addr) . '\''); if ($old_uid < 1) { gs_log(GS_LOG_NOTICE, "Mobility: No user with current IP \"{$remote_addr}\" in database"); return false; } $phone_id = (int) $db->executeGetOne('SELECT `id` FROM `phones` WHERE `user_id`=' . $old_uid . ' LIMIT 1'); if ($phone_id < 1) { gs_log(GS_LOG_WARNING, "Mobility: Login attempt for ext. {$new_ext} - Could not find phone of last user ID {$old_uid}"); # try to find the phone by the corresponding nobody index $nobody_index = (int) $db->executeGetOne('SELECT `nobody_index` FROM `users` WHERE `id`=' . $old_uid); if ($nobody_index > 0) { $phone_id = (int) $db->executeGetOne('SELECT `id` FROM `phones` WHERE `nobody_index`=' . $nobody_index . ' LIMIT 1'); } if ($phone_id < 1) { gs_log(GS_LOG_WARNING, "Mobility: Login attempt for ext. {$new_ext} - Could not find phone of last nobody_index {$nobody_index}"); return false; } } # log out the old user, assign the default nobody # $rs = $db->execute('SELECT `id`, `mac_addr`, `nobody_index`, `user_id` FROM `phones` WHERE `user_id` IN (' . $old_uid . ',' . $new_uid . ') AND `id`<>' . $phone_id); while ($phone = $rs->fetchRow()) { $phone['nobody_index'] = (int) $phone['nobody_index']; $phone['user_id'] = (int) $phone['user_id']; if ($phone['nobody_index'] < 1) { gs_log(GS_LOG_WARNING, "Phone " . $phone['mac_addr'] . " does not have a default nobody user"); $nobody_user_id = null; } else { $nobody_user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `nobody_index`=' . $phone['nobody_index']); if ($nobody_user_id < 1) { gs_log(GS_LOG_WARNING, "Could not find user with nobody index " . $phone['nobody_index'] . " for phone " . $phone['mac_addr']); } } gs_log(GS_LOG_DEBUG, "Mobility: Assigning nobody user with ID " . ($nobody_user_id > 0 ? $nobody_user_id : 'NULL') . " to phone " . $phone['mac_addr']); $db->execute('UPDATE `phones` SET ' . '`user_id`=' . ($nobody_user_id > 0 ? $nobody_user_id : 'NULL') . ' ' . 'WHERE ' . '`id`=' . (int) $phone['id'] . ' AND ' . '`user_id` ' . ($phone['user_id'] > 0 ? '=' . $phone['user_id'] : 'IS NULL')); } //$db->execute( 'UPDATE `users` SET `current_ip`=NULL WHERE `id`='. $old_uid ); # log in the new user # $ok = $db->execute('UPDATE `phones` SET `user_id`=' . $new_uid . ' WHERE `id`=' . $phone_id); if (!$ok) { gs_log(GS_LOG_NOTICE, "Mobility: DB error"); echo 'SET VARIABLE ret ' . gs_agi_str_esc('error') . "\n"; //fFlush(STDOUT); // <- do not use. not defined in php-cgi! die; } # get new phone's IP addr. # $new_ip_addr = $db->executeGetOne('SELECT `current_ip` FROM `users` WHERE `id`=' . $new_uid); gs_log(GS_LOG_DEBUG, "Mobility: IP address found for new phone: {$new_ip_addr}"); # reboot old phone # gs_prov_phone_checkcfg_by_ip($remote_addr, true); # reboot new phone # if ($new_ip_addr) { gs_prov_phone_checkcfg_by_ip($new_ip_addr, true); } return true; }
function gs_agi_hangup($channel = '') { if ($channel != '' && !preg_match('/^[a-zA-Z0-9_\\-\\/]+$/', $channel)) { trigger_error("AGI: Invalid channel name \"{$channel}\"!", E_USER_WARNING); return false; } return gs_agi_do_bool('HANGUP' . ($channel != '' ? ' ' . gs_agi_str_esc($channel) : '')); }