$menu = array('links/' => i18n::s('Links')); $context['text'] .= Skin::build_list($menu, 'menu_bar'); // normalize referrals } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'normalize') { // scan links $context['text'] .= '<p>' . sprintf(i18n::s('Analyzing table %s...'), SQL::table_name('links')) . "</p>\n"; // process all links, but stop after CHUNK_SIZE updates $links_offset = 0; $changes = 0; while ($changes < MAXIMUM_SIZE) { // seek the database and check newest referrals include_once '../agents/referrals.php'; if ($result = Referrals::list_by_dates($links_offset, CHUNK_SIZE)) { // analyze each link while ($item = SQL::fetch($result)) { list($link, $domain, $keywords) = Referrals::normalize($item['referer']); // we suppose the referral is already ok $ok = TRUE; // link has been changed if ($item['referer'] != $link) { $context['text'] .= BR . '< ' . htmlspecialchars($item['referer']) . BR . '> ' . htmlspecialchars($link) . BR; $item['referer'] = $link; $ok = FALSE; } // domain has been changed if (!isset($item['domain']) || $item['domain'] != $domain) { if (isset($item['domain']) && $item['domain']) { $context['text'] .= BR . '< ' . htmlspecialchars($item['domain']) . BR . '> ' . htmlspecialchars($domain) . BR; } else { $context['text'] .= BR . 'd ' . htmlspecialchars($domain) . BR; }
/** * process one single HTTP request * * This function removes any PHPSESSID data in the query string, if any * * @return void * * @see agents/referrals_hook.php */ public static function check_request() { global $context; // don't bother with HEAD requests if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD') { return; } // the target url if (!isset($_SERVER['REQUEST_URI']) || !($url = $_SERVER['REQUEST_URI'])) { return; } // only remember viewed pages and index pages if (!preg_match('/\\/(index|view).php/', $url)) { return; } // continue only if we have a referer if (!isset($_SERVER['HTTP_REFERER']) || !($referer = $_SERVER['HTTP_REFERER'])) { return; } // do not memorize cache referrals if (preg_match('/cache:/i', $referer)) { return; } // block pernicious attacks $referer = strip_tags($referer); // only remember external referrals if (preg_match('/\\b' . preg_quote(str_replace('www.', '', $context['host_name']), '/') . '\\b/i', $referer)) { return; } // stop crawlers if (Surfer::is_crawler()) { return; } // avoid banned sources include_once $context['path_to_root'] . 'servers/servers.php'; if (preg_match(Servers::get_banned_pattern(), $referer)) { return; } // normalize the referral, extract keywords, and domain list($referer, $domain, $keywords) = Referrals::normalize($referer); // if a record exists for this url $query = "SELECT id FROM " . SQL::table_name('referrals') . " AS referrals" . " WHERE referrals.url LIKE '" . SQL::escape($url) . "' AND referrals.referer LIKE '" . SQL::escape($referer) . "'"; if (!($item = SQL::query_first($query))) { return; } // update figures if (isset($item['id'])) { $query = "UPDATE " . SQL::table_name('referrals') . " SET" . " hits=hits+1," . " stamp='" . gmstrftime('%Y-%m-%d %H:%M:%S') . "'" . " WHERE id = " . $item['id']; // create a new record } else { // ensure the referer is accessible if (($content = http::proceed($referer)) === FALSE) { return; } // we have to find a reference to ourself in this page if (strpos($content, $context['url_to_home']) === FALSE) { return; } $query = "INSERT INTO " . SQL::table_name('referrals') . " SET" . " url='" . SQL::escape($url) . "'," . " referer='" . SQL::escape($referer) . "'," . " domain='" . SQL::escape($domain) . "'," . " keywords='" . SQL::escape($keywords) . "'," . " hits=1," . " stamp='" . gmstrftime('%Y-%m-%d %H:%M:%S') . "'"; } // actual database update if (SQL::query($query) === FALSE) { return; } // prune with a probability of 1/100 if (rand(1, 100) != 50) { return; } // purge oldest records -- 100 days = 8640000 seconds $query = "DELETE FROM " . SQL::table_name('referrals') . " WHERE stamp < '" . gmstrftime('%Y-%m-%d %H:%M:%S', time() - 8640000) . "'"; SQL::query($query); }