Exemplo n.º 1
0
function gs_user_change($user, $pin, $firstname, $lastname, $language, $host_id_or_ip, $force = false, $email = '', $reload = true)
{
    if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) {
        return new GsError('User must be alphanumeric.');
    }
    if (!preg_match('/^[0-9]+$/', $pin)) {
        return new GsError('PIN must be numeric.');
    }
    if (strLen($pin) < 3) {
        return new GsError('PIN too short (min. 3 digits).');
    } elseif (strLen($pin) > 10) {
        return new GsError('PIN too long (max. 10 digits).');
    }
    //if (! preg_match( '/^[a-zA-Z\d.\-\_ ]+$/', $firstname ))
    //	return new GsError( 'Invalid characters in first name.' );
    $firstname = preg_replace('/\\s+/', ' ', trim($firstname));
    //if (! preg_match( '/^[a-zA-Z\d.\-\_ ]+$/', $lastname ))
    //	return new GsError( 'Invalid characters in last name.' );
    $lastname = preg_replace('/\\s+/', ' ', trim($lastname));
    // prepare language code
    $language = substr(trim($language), 0, 2);
    if (strlen($language) != 2) {
        return new GsError('Invalid language code.');
    }
    if (!defined('GS_EMAIL_PATTERN_VALID')) {
        return new GsError('GS_EMAIL_PATTERN_VALID not defined.');
    }
    if ($email != '' && !preg_match(GS_EMAIL_PATTERN_VALID, $email)) {
        return new GsError('Invalid e-mail address.');
    }
    include_once GS_DIR . 'lib/utf8-normalize/gs_utf_normal.php';
    # connect to db
    #
    $db = gs_db_master_connect();
    if (!$db) {
        return new GsError('Could not connect to database.');
    }
    # start transaction
    #
    gs_db_start_trans($db);
    # get user_id
    #
    $user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\'');
    if (!$user_id) {
        gs_db_rollback_trans($db);
        return new GsError('Unknown user.');
    }
    # get old host_id
    #
    $old_host_id = (int) $db->executeGetOne('SELECT `host_id` FROM `users` WHERE `id`=' . $user_id);
    $old_host = gs_host_by_id_or_ip($old_host_id);
    if (isGsError($old_host) || !is_array($old_host)) {
        $old_host = false;
    }
    # get user's peer name (extension)
    #
    $ext = $db->executeGetOne('SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`=' . $user_id);
    # check if (new) host exists
    #
    $host = gs_host_by_id_or_ip($host_id_or_ip);
    if (isGsError($host)) {
        gs_db_rollback_trans($db);
        return new GsError($host->getMsg());
    }
    if (!is_array($host)) {
        gs_db_rollback_trans($db);
        return new GsError('Unknown host.');
    }
    if ($old_host_id != $host['id'] && !$force) {
        gs_db_rollback_trans($db);
        return new GsError('Changing the host will result in loosing mailbox messages etc. and thus will not be done without force.');
    }
    /*
    # check if queue with same ext exists
    #
    $num = (int)$db->executeGetOne( 'SELECT COUNT(*) FROM `ast_queues` WHERE `name`=\''. $db->escape($ext) .'\'' );
    if ($num > 0) {
    	gs_db_rollback_trans($db);
    	return new GsError( 'A queue with that name already exists.' );
    }
    */
    # update user
    #
    $ok = $db->execute('UPDATE `users` SET `pin`=\'' . $db->escape($pin) . '\', `firstname`=\'' . $db->escape($firstname) . '\', `lastname`=\'' . $db->escape($lastname) . '\', `email`=\'' . $db->escape($email) . '\', `host_id`=' . $host['id'] . ' WHERE `id`=' . $user_id);
    if (!$ok) {
        gs_db_rollback_trans($db);
        return new GsError('Failed to change user.');
    }
    # update sip account (including language code)
    #
    $calleridname = trim(gs_utf8_decompose_to_ascii($firstname . ' ' . $lastname));
    $ok = $db->execute('UPDATE `ast_sipfriends` SET `callerid`=CONCAT(_utf8\'' . $db->escape($calleridname) . '\', \' <\', `name`, \'>\'), `language`=\'' . $db->escape($language) . '\' WHERE `_user_id`=' . $user_id);
    if (!$ok) {
        gs_db_rollback_trans($db);
        return new GsError('Failed to change SIP account.');
    }
    # delete stuff not used for users on foreign hosts
    #
    if ($host['is_foreign']) {
        $db->execute('DELETE FROM `clir` WHERE `user_id`=' . $user_id);
        $db->execute('DELETE FROM `dial_log` WHERE `user_id`=' . $user_id);
        $db->execute('DELETE FROM `callforwards` WHERE `user_id`=' . $user_id);
        $db->execute('DELETE FROM `pickupgroups_users` WHERE `user_id`=' . $user_id);
        $db->execute('DELETE FROM `ast_queue_members` WHERE `_user_id`=' . $user_id);
        $db->execute('DELETE FROM `vm` WHERE `user_id`=' . $user_id);
        $db->execute('DELETE FROM `ast_voicemail` WHERE `_user_id`=' . $user_id);
    }
    # update mailbox
    #
    if (!$host['is_foreign']) {
        $ok = $db->execute('UPDATE `ast_voicemail` SET `password`=\'' . $db->escape($pin) . '\', `fullname`=\'' . $db->escape($firstname . ' ' . $lastname) . '\' WHERE `_user_id`=' . $user_id);
        if (!$ok) {
            gs_db_rollback_trans($db);
            return new GsError('Failed to change mailbox.');
        }
    }
    # new host?
    #
    if ($host['id'] != $old_host_id) {
        # delete from queue members
        #
        $db->execute('DELETE FROM `ast_queue_members` WHERE `_user_id`=' . $user_id);
        # delete from pickup groups
        #
        $db->execute('DELETE FROM `pickupgroups_users` WHERE `user_id`=' . $user_id);
    }
    # get info needed for foreign hosts
    #
    if (is_array($old_host) && $old_host['is_foreign'] || $host['is_foreign']) {
        # get user's sip name and password
        $rs = $db->execute('SELECT `name`, `secret` FROM `ast_sipfriends` WHERE `_user_id`=' . $user_id);
        if (!$rs || !($r = $rs->fetchRow())) {
            gs_db_rollback_trans($db);
            return new GsError('DB error.');
        }
        $ext = $r['name'];
        $sip_pwd = $r['secret'];
    }
    # modify user on foreign host(s)
    #
    if ($host['id'] != $old_host_id) {
        # host changed. delete user on old host and add on new one
        if (is_array($old_host) && $old_host['is_foreign']) {
            include_once GS_DIR . 'inc/boi-soap/boi-api.php';
            $api = gs_host_get_api($old_host_id);
            switch ($api) {
                case 'm01':
                case 'm02':
                    //if (! class_exists('SoapClient')) {
                    if (!extension_loaded('soap')) {
                        gs_db_rollback_trans($db);
                        return new GsError('Failed to delete user on old foreign host (SoapClient not available).');
                    } else {
                        $hp_route_prefix = (string) $db->executeGetOne('SELECT `value` FROM `host_params` ' . 'WHERE `host_id`=' . (int) $old_host['id'] . ' AND `param`=\'route_prefix\'');
                        $sub_ext = subStr($ext, 0, strLen($hp_route_prefix)) === $hp_route_prefix ? subStr($ext, strLen($hp_route_prefix)) : $ext;
                        gs_log(GS_LOG_DEBUG, "Mapping ext. {$ext} to {$sub_ext} for SOAP call");
                        include_once GS_DIR . 'inc/boi-soap/boi-soap.php';
                        $soap_faultcode = null;
                        $ok = gs_boi_delete_extension($api, $old_host['host'], $hp_route_prefix, $sub_ext, $soap_faultcode);
                        if (!$ok) {
                            gs_db_rollback_trans($db);
                            return new GsError('Failed to delete user on old foreign host (SOAP error).');
                        }
                    }
                    break;
                case '':
                    # host does not provide any API
                    gs_log(GS_LOG_NOTICE, 'Deleting user ' . $user . ' on foreign host ' . $old_host['host'] . ' without any API');
                    break;
                default:
                    gs_log(GS_LOG_WARNING, 'Failed to delete user ' . $user . ' on foreign host ' . $old_host['host'] . ' - invalid API "' . $api . '"');
                    gs_db_rollback_trans($db);
                    return new GsError('Failed to delete user on foreign host (Invalid API).');
            }
        }
        if ($host['is_foreign']) {
            include_once GS_DIR . 'inc/boi-soap/boi-api.php';
            $api = gs_host_get_api($host['id']);
            switch ($api) {
                case 'm01':
                case 'm02':
                    //if (! class_exists('SoapClient')) {
                    if (!extension_loaded('soap')) {
                        gs_db_rollback_trans($db);
                        return new GsError('Failed to add user on new foreign host (SoapClient not available).');
                    } else {
                        $hp_route_prefix = (string) $db->executeGetOne('SELECT `value` FROM `host_params` ' . 'WHERE `host_id`=' . (int) $host['id'] . ' AND `param`=\'route_prefix\'');
                        $sub_ext = subStr($ext, 0, strLen($hp_route_prefix)) === $hp_route_prefix ? subStr($ext, strLen($hp_route_prefix)) : $ext;
                        gs_log(GS_LOG_DEBUG, "Mapping ext. {$ext} to {$sub_ext} for SOAP call");
                        include_once GS_DIR . 'inc/boi-soap/boi-soap.php';
                        $soap_faultcode = null;
                        $ok = gs_boi_update_extension($api, $host['host'], $hp_route_prefix, $sub_ext, $user, $sip_pwd, $pin, $firstname, $lastname, $email, $soap_faultcode);
                        if (!$ok) {
                            gs_db_rollback_trans($db);
                            return new GsError('Failed to add user on new foreign host (SOAP error).');
                        }
                    }
                    break;
                case '':
                    # host does not provide any API
                    gs_log(GS_LOG_NOTICE, 'Adding user ' . $user . ' on foreign host ' . $host['host'] . ' without any API');
                    break;
                default:
                    gs_log(GS_LOG_WARNING, 'Failed to add user ' . $user . ' on foreign host ' . $host['host'] . ' - invalid API "' . $api . '"');
                    gs_db_rollback_trans($db);
                    return new GsError('Failed to add user on foreign host (Invalid API).');
            }
        }
    } else {
        # host did not change
        if ($host['is_foreign']) {
            include_once GS_DIR . 'inc/boi-soap/boi-api.php';
            $api = gs_host_get_api($host['id']);
            switch ($api) {
                case 'm01':
                case 'm02':
                    //if (! class_exists('SoapClient')) {
                    if (!extension_loaded('soap')) {
                        gs_db_rollback_trans($db);
                        return new GsError('Failed to modify user on foreign host (SoapClient not available).');
                    } else {
                        $hp_route_prefix = (string) $db->executeGetOne('SELECT `value` FROM `host_params` ' . 'WHERE `host_id`=' . (int) $host['id'] . ' AND `param`=\'route_prefix\'');
                        $sub_ext = subStr($ext, 0, strLen($hp_route_prefix)) === $hp_route_prefix ? subStr($ext, strLen($hp_route_prefix)) : $ext;
                        gs_log(GS_LOG_DEBUG, "Mapping ext. {$ext} to {$sub_ext} for SOAP call");
                        include_once GS_DIR . 'inc/boi-soap/boi-soap.php';
                        $soap_faultcode = null;
                        $ok = gs_boi_update_extension($api, $host['host'], $hp_route_prefix, $sub_ext, $user, $sip_pwd, $pin, $firstname, $lastname, $email, $soap_faultcode);
                        if (!$ok) {
                            gs_db_rollback_trans($db);
                            return new GsError('Failed to modify user on foreign host (SOAP error).');
                        }
                    }
                    break;
                case '':
                    # host does not provide any API
                    gs_log(GS_LOG_NOTICE, 'Modifying user ' . $user . ' on foreign host ' . $host['host'] . ' without any API');
                    break;
                default:
                    gs_log(GS_LOG_WARNING, 'Failed to modify user ' . $user . ' on foreign host ' . $host['host'] . ' - invalid API "' . $api . '"');
                    gs_db_rollback_trans($db);
                    return new GsError('Failed to modify user on foreign host (Invalid API).');
            }
        }
    }
    # commit transaction
    #
    if (!gs_db_commit_trans($db)) {
        return new GsError('Failed to modify user.');
    }
    # new host?
    #
    if ($host['id'] != $old_host_id) {
        # reload dialplan (hints!)
        #
        if (is_array($old_host) && !$old_host['is_foreign']) {
            $ok = @gs_asterisks_prune_peer($ext, array($old_host_id));
            if ($reload) {
                @gs_asterisks_reload(array($old_host_id), true);
            }
        }
        if (!$host['is_foreign']) {
            if ($reload) {
                @gs_asterisks_reload(array($host['id']), true);
            }
        }
    } else {
        $ok = @gs_asterisks_prune_peer($ext, array($host['id']));
    }
    # reboot the phone
    #
    //@ shell_exec( 'asterisk -rx \'sip notify snom-reboot '. $ext .'\' >>/dev/null' );
    @gs_prov_phone_checkcfg_by_ext($ext, true);
    # update fax authentication file if fax enabled
    #
    if (gs_get_conf('GS_FAX_ENABLED')) {
        $ok = gs_hylafax_authfile_sync();
        if (isGsError($ok)) {
            return new GsError($ok->getMsg());
        }
        if (!$ok) {
            return new GsError('Failed to update fax authentication file.');
        }
    }
    return true;
}
Exemplo n.º 2
0
function gs_user_del($user, $reload = true)
{
    if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) {
        return new GsError('User must be alphanumeric.');
    }
    # connect to db
    #
    $db = gs_db_master_connect();
    if (!$db) {
        return new GsError('Could not connect to database.');
    }
    # get user_id, nobody_index and softkey_profile_id
    #
    $rs = $db->execute('SELECT `id`, `nobody_index`, `softkey_profile_id`, `prov_param_profile_id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\'');
    if (!$rs) {
        return new GsError('DB error.');
    }
    if (!($r = $rs->fetchRow())) {
        return new GsError('Unknown user.');
    }
    $user_id = (int) $r['id'];
    $softkey_profile_id = (int) $r['softkey_profile_id'];
    $prov_profile_id = (int) $r['prov_param_profile_id'];
    /*
    if ($r['nobody_index'] > 0)
    	return new GsError( 'Cannot delete nobody user.' );
    */
    # get host_id
    #
    $host_id = (int) $db->executeGetOne('SELECT `host_id` FROM `users` WHERE `id`=' . $user_id);
    //if (! $host_id)
    //	return new GsError( 'Unknown host.' );
    $host = gs_host_by_id_or_ip($host_id);
    if (isGsError($host) || !is_array($host)) {
        $host = false;
    }
    # get user's sip name
    #
    $ext = $db->executeGetOne('SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`=' . $user_id);
    # reboot phone
    #
    //$user_name = $db->executeGetOne( 'SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`='. $user_id );
    //@ shell_exec( 'asterisk -rx \'sip notify snom-reboot '. $user_name .'\' >>/dev/null' );
    @gs_prov_phone_checkcfg_by_user($user, true);
    #delete user from all groups
    #
    gs_group_members_purge_by_type('user', array($user_id));
    # delete clir settings
    #
    $db->execute('DELETE FROM `clir` WHERE `user_id`=' . $user_id);
    # delete dial log
    #
    $db->execute('DELETE FROM `dial_log` WHERE `user_id`=' . $user_id);
    $db->execute('UPDATE `dial_log` SET `remote_user_id`=NULL WHERE `remote_user_id`=' . $user_id);
    # delete call waiting settings
    #
    $db->execute('DELETE FROM `callwaiting` WHERE `user_id`=' . $user_id);
    # delete call forward settings
    #
    $db->execute('DELETE FROM `callforwards` WHERE `user_id`=' . $user_id);
    # delete anounce files
    #
    $db->execute('DELETE FROM `vm_rec_messages` WHERE `_user_id`=' . $user_id);
    # delete parallel-call definition
    #
    $db->execute('DELETE FROM `cf_parallelcall` WHERE `_user_id`=' . $user_id);
    # delete timerules definition
    #
    $db->execute('DELETE FROM `cf_timerules` WHERE `_user_id`=' . $user_id);
    # delete from pickup groups
    #
    $db->execute('DELETE FROM `pickupgroups_users` WHERE `user_id`=' . $user_id);
    # delete from queue members
    #
    $db->execute('DELETE FROM `ast_queue_members` WHERE `_user_id`=' . $user_id);
    # delete external numbers
    #
    $db->execute('DELETE FROM `users_external_numbers` WHERE `user_id`=' . $user_id);
    # delete info about voicemail messages //FIXME - delete files?
    #
    $db->execute('DELETE FROM `vm_msgs` WHERE `user_id`=' . $user_id);
    # delete mailbox settings
    #
    $db->execute('DELETE FROM `vm` WHERE `user_id`=' . $user_id);
    # delete private phonebook
    #
    $db->execute('DELETE FROM `pb_prv` WHERE `user_id`=' . $user_id);
    # delete mailbox
    #
    $db->execute('DELETE FROM `ast_voicemail` WHERE `_user_id`=' . $user_id);
    # delete callblocking rules
    #
    $db->execute('DELETE FROM `callblocking` WHERE `user_id`=' . $user_id);
    # delete callerids
    #
    $db->execute('DELETE FROM `users_callerids` WHERE `user_id`=' . $user_id);
    # delete sip account
    #
    $db->execute('DELETE FROM `ast_sipfriends` WHERE `_user_id`=' . $user_id);
    # delete BOI permissions
    #
    $db->execute('DELETE FROM `boi_perms` WHERE `user_id`=' . $user_id);
    # delete ringtones
    #
    $db->execute('DELETE FROM `ringtones` WHERE `user_id`=' . $user_id);
    # delete softkeys
    #
    if ($softkey_profile_id > 0) {
        $db->execute('DELETE FROM `softkeys` WHERE `profile_id`=' . $softkey_profile_id);
        $db->execute('DELETE FROM `softkey_profiles` WHERE `id`=' . $softkey_profile_id . ' AND `is_user_profile`=1');
    }
    # delete prov_params
    #
    if ($prov_profile_id > 0) {
        $db->execute('DELETE FROM `prov_params` WHERE `profile_id`=' . $prov_profile_id);
        $db->execute('DELETE FROM `prov_param_profiles` WHERE `id`=' . $prov_profile_id . ' AND `is_group_profile`=0');
    }
    # delete watchlist buddies
    #
    $db->execute('DELETE FROM `user_watchlist` WHERE `user_id`=' . $user_id);
    $db->execute('DELETE FROM `user_watchlist` WHERE `buddy_user_id`=' . $user_id);
    # delete instant messaging
    #
    $db->execute('DELETE FROM `instant_messaging` WHERE `user_id`=' . $user_id);
    # delete monitor data
    #
    $db->execute('DELETE FROM `monitor` WHERE `user_id`=' . $user_id);
    $db->execute('DELETE FROM `monitor_queues` WHERE `user_id`=' . $user_id);
    $db->execute('DELETE FROM `monitor_colors` WHERE `user_id`=' . $user_id);
    # do a clean logout from the current phone
    #
    $db->execute('UPDATE `phones` SET `user_id`=NULL WHERE `user_id`=' . $user_id);
    # delete huntgroup memberships
    #
    $db->execute('DELETE FROM `huntgroups` WHERE `user_id`=' . $user_id);
    # delete drop targets
    #
    $db->execute('DELETE FROM `user_calldrop` WHERE `user_id`=' . $user_id);
    # delete dnd
    #
    $db->execute('DELETE FROM `dnd` WHERE `_user_id`=' . $user_id);
    # delete user
    #
    $db->execute('DELETE FROM `users` WHERE `id`=' . $user_id);
    # astbuttond
    if (GS_BUTTONDAEMON_USE == true) {
        gs_user_remove_ui($ext);
    }
    # reload dialplan (to update hints) and prune realtime peer
    #
    if ($host_id > 0) {
        if (is_array($host) && !$host['is_foreign']) {
            @gs_asterisks_prune_peer($ext, array($host_id));
            if ($reload) {
                @gs_asterisks_reload(array($host_id), true);
            }
        }
    }
    # delete user on foreign host
    #
    if (is_array($host) && $host['is_foreign']) {
        if (trim($ext) != '') {
            include_once GS_DIR . 'inc/boi-soap/boi-api.php';
            $api = gs_host_get_api($host['id']);
            switch ($api) {
                case 'm01':
                case 'm02':
                    $hp_route_prefix = (string) $db->executeGetOne('SELECT `value` FROM `host_params` ' . 'WHERE `host_id`=' . (int) $host['id'] . ' AND `param`=\'route_prefix\'');
                    $sub_ext = subStr($ext, 0, strLen($hp_route_prefix)) === $hp_route_prefix ? subStr($ext, strLen($hp_route_prefix)) : $ext;
                    gs_log(GS_LOG_DEBUG, "Mapping ext. {$ext} to {$sub_ext} for SOAP call");
                    //if (! class_exists('SoapClient')) {
                    if (!extension_loaded('soap')) {
                        return new GsError('Failed to delete user on foreign host (SoapClient not available).');
                    }
                    include_once GS_DIR . 'inc/boi-soap/boi-soap.php';
                    $soap_faultcode = null;
                    $ok = gs_boi_delete_extension($api, $host['host'], $hp_route_prefix, $sub_ext, $soap_faultcode);
                    if (!$ok) {
                        return new GsError('Failed to delete user on foreign host (SOAP error).');
                    }
                    break;
                case '':
                    # host does not provide any API
                    gs_log(GS_LOG_NOTICE, 'Deleting user ' . $user . ' on foreign host ' . $host['host'] . ' without any API');
                    break;
                default:
                    gs_log(GS_LOG_WARNING, 'Failed to delete user ' . $user . ' on foreign host ' . $host['host'] . ' - invalid API "' . $api . '"');
                    return new GsError('Failed to delete user on foreign host (Invalid API).');
            }
        }
    }
    # update fax authentication file if fax enabled
    #
    if (gs_get_conf('GS_FAX_ENABLED')) {
        $ok = gs_hylafax_authfile_sync();
        if (isGsError($ok)) {
            return new GsError($ok->getMsg());
        }
        if (!$ok) {
            return new GsError('Failed to update fax authentication file.');
        }
    }
    return true;
}