<?PHP
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
                              Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
 Last update : 2005-11-24                               
 Part of     : Update to Version 01.00.02    
******************************************************************************/
if (!is_object($perm)) die();
function user_set_active($iduser, $is_active) {
        global $db, $dedi_db;

        $sql = "UPDATE ".$dedi_db['users']." SET is_active='$is_active' WHERE user_id='$iduser'";
        $db->query($sql);
}

function user_save() {
        global $db, $dedi_db, $username, $password, $password_validate, $name, $surname, $email, $group, $iduser, $idgroup, $order, $ascdesc, $oldusername, $comment;
        global $salutation, $street, $street_alt, $zip, $location, $state, $country, $phone, $fax, $mobile, $pager, $homepage, $birthday, $firm, $position, $firm_street, $firm_street_alt, $firm_zip, $firm_location, $firm_state, $firm_country, $firm_email, $firm_phone, $firm_fax, $firm_mobile, $firm_pager, $firm_homepage;

        // Passwort vergleich
        $password = trim($password);
         $password_validate = trim($password_validate);
         $username = trim($username);
        if ((!empty($password) || (empty($password) && empty($iduser))) && ($password != $password_validate || strlen($password) < 3)) return 'incorrect';

        // keine Passwort
        if (empty($iduser) && empty($password)) return 'incorrect';

        // Kein Loginname
        if (empty($username)) return 'nologinname';

        // Username auf Existenz prüfen
        if ($username != $oldusername) {
                $sql = "SELECT username FROM ".$dedi_db['users']." WHERE username='$username' LIMIT 0, 1";
                $db->query($sql);
                if ($db->affected_rows()) return 'existusername';
        }
        if (!is_array($group)) $group['0'] = $idgroup;

        // Wenn Sysadmin gewählt wurde, alle anderen kicken
        if (in_array ('2', $group)) {
                 unset($group);
                $group['0'] = '2';
         }
        set_magic_quotes_gpc($username);
        set_magic_quotes_gpc($name);
        set_magic_quotes_gpc($surname);
        set_magic_quotes_gpc($email);
        set_magic_quotes_gpc($password);
        set_magic_quotes_gpc($salutation);
        set_magic_quotes_gpc($street);
        set_magic_quotes_gpc($street_alt);
        set_magic_quotes_gpc($zip);
        set_magic_quotes_gpc($location);
        set_magic_quotes_gpc($state);
        set_magic_quotes_gpc($country);
        set_magic_quotes_gpc($phone);
        set_magic_quotes_gpc($fax);
        set_magic_quotes_gpc($mobile);
        set_magic_quotes_gpc($pager);
        set_magic_quotes_gpc($homepage);
        set_magic_quotes_gpc($birthday);
        set_magic_quotes_gpc($firm);
        set_magic_quotes_gpc($position);
        set_magic_quotes_gpc($firm_street);
        set_magic_quotes_gpc($firm_street_alt);
        set_magic_quotes_gpc($firm_zip);
        set_magic_quotes_gpc($firm_location);
        set_magic_quotes_gpc($firm_state);
        set_magic_quotes_gpc($firm_country);
        set_magic_quotes_gpc($firm_email);
        set_magic_quotes_gpc($firm_phone);
        set_magic_quotes_gpc($firm_fax);
        set_magic_quotes_gpc($firm_mobile);
        set_magic_quotes_gpc($firm_pager);
        set_magic_quotes_gpc($firm_homepage);
        set_magic_quotes_gpc($comment);

        // Besteht User bereits?
        if (!empty($iduser)) {
                $sql = "DELETE FROM ".$dedi_db['users_groups']." WHERE user_id='$iduser'";
                $db->query($sql);
                $password = (!empty($password)) ? ", password='".md5($password).'\'' : '';
                $sql = "UPDATE ".$dedi_db['users']." SET
                                username='$username',
                                name='$name',
                                surname='$surname',
                                email='$email',
                                salutation='$salutation',
                                street='$street',
                                street_alt='$street_alt',
                                zip='$zip',
                                location='$location',
                                state='$state',
                                country='$country',
                                phone='$phone',
                                fax='$fax',
                                mobile='$mobile',
                                pager='$pager',
                                homepage='$homepage',
                                birthday='$birthday',
                                firm='$firm',
                                position='$position',
                                firm_street='$firm_street',
                                firm_street_alt='$firm_street_alt',
                                firm_zip='$firm_zip',
                                firm_location='$firm_location',
                                firm_state='$firm_state',
                                firm_country='$firm_country',
                                firm_email='$firm_email',
                                firm_phone='$firm_phone',
                                firm_fax='$firm_fax',
                                firm_mobile='$firm_mobile',
                                firm_pager='$firm_pager',
                                firm_homepage='$firm_homepage',
                                comment='$comment'$password
                        WHERE user_id ='$iduser'";
                $db->query($sql);
        } else {
                 $sql = "INSERT INTO ".$dedi_db['users']."
                                (username, password, name, surname, email, is_active, is_deletable, salutation, street,
                                street_alt, zip, location, state, country, phone, fax, mobile, pager, homepage, birthday,
                                firm, position, firm_street, firm_street_alt, firm_zip, firm_location, firm_state, firm_country,
                                firm_email, firm_phone, firm_fax, firm_mobile, firm_pager, firm_homepage, comment)
                               VALUES
                                 ('$username', '".md5($password)."', '$name', '$surname', '$email','1', '1', '$salutation',
                                '$street', '$street_alt', '$zip', '$location', '$state', '$country', '$phone', '$fax',
                                '$mobile', '$pager', '$homepage', '$birthday', '$firm', '$position', '$firm_street',
                                '$firm_street_alt', '$firm_zip', '$firm_location', '$firm_state', '$firm_country',
                                '$firm_email', '$firm_phone', '$firm_fax', '$firm_mobile', '$firm_pager', '$firm_homepage',
                                '$comment')";
                $db->query($sql);
                $sql = "SELECT user_id FROM ".$dedi_db['users']." WHERE username='$username'";
                $db->query($sql);
                $db->next_record();
                $iduser = $db->f('user_id');
        }
        foreach ($group as $value) {
                $sql = "INSERT INTO ".$dedi_db['users_groups']." VALUES ('', '$iduser', '$value')";
                $db->query($sql);
        }
}

function user_delete() {
        global $db, $dedi_db, $idgroup, $iduser;

        $sql = "SELECT user_id, idgroup FROM ".$dedi_db['users_groups']." WHERE user_id='$iduser'";
        $db->query($sql);
        $db->next_record();
        if ($db->f('user_id') == $iduser) {
                $sql = "DELETE FROM ".$dedi_db['users_groups']." WHERE user_id='$iduser' AND idgroup='$idgroup'";
                $db->query($sql);
                $sql = "SELECT u_g_id FROM ".$dedi_db['users_groups']." WHERE user_id='$iduser' LIMIT 0, 1";
                $db->query($sql);
                if (!$db->affected_rows()) {
                        $sql = "DELETE FROM ".$dedi_db['users']." WHERE user_id='$iduser'";
                        $db->query($sql);

                        $update_data = array( $dedi_db['cat_lang'], $dedi_db['clients'], $dedi_db['clients_lang'],
                                                           $dedi_db['content'], $dedi_db['css'], $dedi_db['directory'],
                                                           $dedi_db['filetype'], $dedi_db['js'], $dedi_db['lang'],
                                                           $dedi_db['mod'], $dedi_db['side_lang'], $dedi_db['tpl'], $dedi_db['upl']
                                                         );
                        foreach($update_data AS $v)
                        {
                                $sql = "UPDATE ". $v . "
                                                SET author = 1
                                                WHERE author=$iduser";
                                $db-> query($sql);
                        }

        }
        }
}
?>