View source
<?php
if (!defined('CLICK_NEW')) {
define('CLICK_NEW', 0);
define('CLICK_VALID', 1);
define('CLICK_DUPLICATE', 2);
define('CLICK_OWNER', 3);
define('CLICK_FILTER_ROLE', 4);
define('CLICK_BOT', 5);
}
function click_filter_install() {
$valid = $duplicate = $owner = $bot = 0;
$result = db_query("SELECT DISTINCT(aid) FROM {ad_clicks} WHERE status = %d", CLICK_NEW);
while ($ad = db_fetch_object($result)) {
$clicks = db_query('SELECT cid, uid, hostname, timestamp, user_agent FROM {ad_clicks} WHERE status = %d AND aid = %d', CLICK_NEW, $ad->aid);
while ($click = db_fetch_object($clicks)) {
if (preg_match("/bot/i", $click->user_agent)) {
db_query('UPDATE {ad_clicks} SET status = %d WHERE cid = %d', CLICK_BOT, $click->cid);
$bot++;
continue;
}
$result = db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $ad->aid, $click->uid);
if (db_result($result)) {
db_query('UPDATE {ad_clicks} SET status = %d WHERE cid = %d', CLICK_OWNER, $click->cid);
db_query("UPDATE {ad_statistics} SET count = count - 1 WHERE date = %d AND aid = %d AND action = 'click' AND hostid = '%s'", date('YmdH', $click->timestamp), $ad->aid, $click->hostid);
$owner++;
continue;
}
$result = db_query("SELECT cid FROM {ad_clicks} WHERE aid = %d AND status = %d AND cid <> %d AND (uid = %d OR hostname = '%s')", $ad->aid, CLICK_VALID, $click->cid, $click->uid, $click->hostname);
if (db_result($result)) {
db_query('UPDATE {ad_clicks} SET status = %d WHERE cid = %d', CLICK_DUPLICATE, $click->cid);
db_query("UPDATE {ad_statistics} SET count = count - 1 WHERE date = %d AND aid = %d AND action = 'click' AND hostid = '%s'", date('YmdH', $click->timestamp), $ad->aid, $click->hostid);
$duplicate++;
}
else {
db_query('UPDATE {ad_clicks} SET status = %d WHERE cid = %d', CLICK_VALID, $click->cid);
$valid++;
}
}
}
drupal_set_message(st('Updated existing clicks: %valid valid, filtered %duplicate duplicate, %owner by owner and %bot by search engine bots.', array(
'%valid' => $valid,
'%duplicate' => $duplicate,
'%owner' => $owner,
'%bot' => $bot,
)));
}