You are here

function spam_save_token in Spam 5

1 call to spam_save_token()
spam_update_1 in ./spam.install
Updates

File

./spam.install, line 276

Code

function spam_save_token($token, $old) {
  $existing = db_fetch_object(db_query("SELECT * from {spam_tokens} WHERE token = '%s'", $token));
  if (isset($existing->token)) {
    $total = $existing->spam + $existing->notspam + $old->spam + $old->notspam;
    $probability = ($existing->spam + $old->spam) / $total * 100;
    if ($probability > 99) {
      $probability = 99;
    }
    else {
      if ($probability < 1) {
        $probability = 1;
      }
    }
    db_query("UPDATE {spam_tokens} SET spam = %d, notspam = %d, probability = %d, last = %d WHERE token = '%s'", $existing->spam + $old->spam, $existing->notspam + $old->notspam, $probability, $existing->last >= $old->last ? $existing->last : $old->last, $existing->token);
    return 0;
  }
  else {

    // we shouldn't have to make changes here, but let's be sure things are sane    $spam = $old->spam < 1 ? 1 : $old->spam;
    $notspam = $old->notspam < 1 ? 1 : $old->notspam;
    $total = $spam + $notspam;
    $probability = $spam / $total * 100;
    if ($probability > 99) {
      $probability = 99;
    }
    else {
      if ($probability < 1) {
        $probability = 1;
      }
    }
    db_query("INSERT INTO {spam_tokens} (token, spam, notspam, probability, last) VALUES('%s', %d, %d, %d, %d)", $token, $spam, $notspam, $probability, $old->last);
    return 1;
  }
}