function lhg_update_donation_settings()
{
    global $lang;
    global $lhg_price_db;
    error_log("Update settings");
    //echo "Found??: ".$_POST['lhg_user_donation_target'];
    if (!isset($_POST['lhg_user_donation_target'])) {
        return;
    }
    //validate submitted value otherwise set to default
    $user_donation_target = $_POST['lhg_user_donation_target'];
    // get user id
    // check if own or other user's profile is edited
    $user_id = get_current_user_id();
    $edituser = $_POST['user_id'];
    if ($edituser > 0 && $edituser != $user_id) {
        #error_log("Editing of other user: "******"UID settings to be conged for  changed: $user_id - $guid");
    }
    // first add to history DB before value is overwritten
    $timestamp = time();
    // check if old value exists, if not, store for history reasons
    $sql = "SELECT id FROM `lhgtransverse_donations` WHERE guid = \"" . $guid . "\" ";
    $result = $lhg_price_db->get_var($sql);
    if ($result == "") {
        # old value was not stored. Entry needed. First get old value
        #error_log("No entry found for $guid");
        $sql = "SELECT *  FROM `lhgtransverse_users` WHERE id = \"" . $guid . "\" ";
        $results = $lhg_price_db->get_results($sql);
        #var_dump($results);
        #error_log("DB res: ".$results[0]->donation_target_com);
        #error_log("DB res: ".$results[0]->donation_target_de);
        if ($results[0]->donation_target_com != "" && $results[0]->donation_target_date_com > $results[0]->donation_target_date_de) {
            # found an old and valid com value
            $sql = "INSERT INTO lhgtransverse_donations (guid, timestamp, donation_target) VALUES (%s, %s, %s) ";
            $safe_sql = $lhg_price_db->prepare($sql, $guid, $timestamp - 1, $results[0]->donation_target_com);
            $result = $lhg_price_db->query($safe_sql);
        } elseif ($results[0]->donation_target_d != "" && $results[0]->donation_target_date_de > $results[0]->donation_target_date_com) {
            # found an old and valid de value
            $sql = "INSERT INTO lhgtransverse_donations (guid, timestamp, donation_target) VALUES (%s, %s, %s) ";
            $safe_sql = $lhg_price_db->prepare($sql, $guid, $timestamp - 1, $results[0]->donation_target_de);
            $result = $lhg_price_db->query($safe_sql);
        } else {
            #nothing found, fallback value used
            # found an old and valid de value
            $sql = "INSERT INTO lhgtransverse_donations (guid, timestamp, donation_target) VALUES (%s, %s, %s) ";
            $safe_sql = $lhg_price_db->prepare($sql, $guid, $timestamp - 1, 1);
            $result = $lhg_price_db->query($safe_sql);
        }
    }
    #write new settings to DB
    $sql = "INSERT INTO lhgtransverse_donations (guid, timestamp, donation_target) VALUES (%s, %s, %s) ";
    $safe_sql = $lhg_price_db->prepare($sql, $guid, $timestamp, $user_donation_target);
    $result = $lhg_price_db->query($safe_sql);
    //afterwards, modify user entires (locally and in transverse DB)
    update_user_meta($user_id, 'user_donation_target', $user_donation_target);
    //also store in priceDB
    if ($lang != "de") {
        lhg_update_userdb_by_uid("donation_target_com", $user_id, $user_donation_target);
    }
    if ($lang == "de") {
        lhg_update_userdb_by_uid("donation_target_de", $user_id, $user_donation_target);
    }
    if ($lang != "de") {
        lhg_update_userdb_by_uid("donation_target_date_com", $user_id, time());
    }
    if ($lang == "de") {
        lhg_update_userdb_by_uid("donation_target_date_de", $user_id, time());
    }
}
function lhg_user_linking()
{
    global $lhg_price_db;
    global $lang;
    # transverse linking. Look if email is also used for accounts on other servers
    $current_user = wp_get_current_user();
    $email = $current_user->user_email;
    $cuid = $current_user->ID;
    if ($lang != "de") {
        if (email != "") {
            # email found but no
            #error_log("email & wpuid found: ".$result->email." ".$result->wp_uid );
            lhg_update_userdb_by_uid('wpuid', $cuid, $cuid);
        }
    }
    if ($lang == "de") {
        if (email != "") {
            # email found but no
            #error_log("email & wpuid found: ".$email." ".$cuid );
            lhg_update_userdb_by_uid('wpuid_de', $cuid, $cuid);
        }
    }
    # Link users with available scans
    $sql = "SELECT * FROM `lhgscansessions` ORDER BY id DESC LIMIT 100";
    $results = $lhg_price_db->get_results($sql);
    foreach ($results as $result) {
        if ($lang != "de") {
            if ($result->email != "" && $result->wp_uid != 0) {
                # email found but no
                #error_log("email & wpuid found: ".$result->email." ".$result->wp_uid );
                lhg_update_userdb_by_uid('email', $result->wp_uid, $result->email);
            }
        }
        if ($lang == "de") {
            if ($result->email != "" && $result->wp_uid_de != 0) {
                # email found but no
                #error_log("email & wpuid found: ".$result->email." ".$result->wp_uid );
                lhg_update_userdb_by_uid('email', $result->wp_uid_de, $result->email);
            }
        }
    }
    # check for user ids that were added by scan server but not yet linked with karma
    # Not needed because already handled by lhg_get_scan_points
    #$sql = "SELECT * FROM `lhgscansessions` WHERE `wp_uid` != 0 and `karma` IS NULL";
    #$results = $lhg_price_db->get_results($sql);
    #
    #var_dump($results);
    #foreach($results as $result){
    #        error_log("not linked SID: $result->sid - $result->wp_uid");
    #}
}