function gs_user_logout($user, $reboot = true) { $ret = gs_user_is_valid_name($user); if (isGsError($ret)) { return $ret; } elseif (!$ret) { return new GsError('Invalid username.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if ($user_id < 1) { return new GsError('Unknown user.'); } $ip_addr = $db->executeGetOne('SELECT `current_ip` FROM `users` WHERE `id`=' . $user_id); $rs = $db->execute('SELECT `id`, `mac_addr`, `nobody_index` FROM `phones` WHERE `user_id`=' . $user_id); while ($phone = $rs->fetchRow()) { # assign the default nobody # $phone['nobody_index'] = (int) $phone['nobody_index']; if ($phone['nobody_index'] < 1) { $new_user_id = null; } else { $new_user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `nobody_index`=' . $phone['nobody_index']); if ($new_user_id < 1) { //? } } $db->execute('UPDATE `phones` SET `user_id`=' . ($new_user_id > 0 ? $new_user_id : 'NULL') . ' WHERE `id`=' . (int) $phone['id'] . ' AND `user_id`=' . $user_id); } # log out of all queues # $user_ext = $db->executeGetOne('SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`=' . $user_id); $user_ext = preg_replace('/[^0-9]/', '', $user_ext); if ($user_ext != '') { ob_start(); @exec(GS_DIR . 'dialplan-scripts/fake-agi-env.php' . ' ' . qsa(GS_DIR . 'dialplan-scripts/queue-login-logout.agi') . ' ' . qsa($user_ext) . ' 0 logoutall 1>>/dev/null 2>>/dev/null'); ob_end_clean(); } # restart phone # if ($ip_addr != '') { $ret = @gs_prov_phone_checkcfg_by_ip($ip_addr, $reboot); } if (isGsError($ret)) { gs_script_error($ret->getMsg()); } return true; }
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; }