2, 'portability' => DB_PORTABILITY_ALL ^ DB_PORTABILITY_LOWERCASE)); $db->setFetchMode(DB_FETCHMODE_ASSOC); /** * Import 'url' param that we want to log a click through against */ $sURL = isset($_GET['url']) ? $_GET['url'] : false; /** * Very basic URL validation. This could be improved by only allowing * specific scheme types (http, ftp...), verifying host names etc */ $aURL = parse_url($sURL); if (!isset($aURL['scheme']) || !isset($aURL['host'])) { $sURL = false; } /** * If we have a URL, do something with it */ if ($sURL) { /** * We'll use a hash of the URL for some queries as it means we can set a DB table * with a fixed 32 char column, indexed. */ $sHash = md5($sURL); $sIP = $_SERVER['REMOTE_ADDR']; $sUserAgent = $_SERVER['HTTP_USER_AGENT']; $iLinkID = local_getLinkID($db, $debugObj, $sHash); if ($iLinkID === false) { $iLinkID = local_logNewLink($db, $debugObj, $sHash, $sURL); } local_logLinkClick($db, $debugObj, $iLinkID, $sIP, $sUserAgent); } /** * Output transparent 1x1 gif, even though the image will be hidden */ header('Content-type: image/gif'); $sTransGIF = 'R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='; echo base64_decode($sTransGIF); /** * Here endeth the script */ exit; /** * Functions */ /** * Based on a hash of our loggable URL, get its linkID from our links table, return * false if it doesn't exist */ function local_getLinkID($db, $debugObj, $sHash) { $sql = sprintf('SELECT linkID FROM ct_Links WHERE hash = %s', $db->quoteSmart($sHash)); $debugObj->output("local_getLinkID($db, $debugObj, $sHash) : $sql"); $iID = $db->getOne($sql); if (!$iID || $iID == 0) { return false; } return $iID; } /** * Log a new link in the DB and return its linkID */ function local_logNewLink($db, $debugObj, $sHash, $sURL) { $sql = sprintf('INSERT INTO ct_Links (hash, url) VALUES (%s, %s)', $db->quoteSmart($sHash), $db->quoteSmart($sURL) ); $debugObj->output("local_logNewLink($db, $debugObj, $sHash, $sURL) : $sql"); $db->query($sql); return $db->getOne('SELECT LAST_INSERT_ID()'); } /** * Prior to calling this function, one or both of local_getLinkID and local_logNewLink * should have been called. We'll add an entry to the log table to indicate a URL * has been accessed. IP and useragent are logged for information purposes. */ function local_logLinkClick($db, $debugObj, $iLinkID, $sIP, $sUserAgent) { $sql = sprintf('INSERT INTO ct_Log (linkID, ip, useragent) VALUES (%d, INET_ATON(%s), %s)', $iLinkID, $db->quoteSmart($sIP), $db->quoteSmart($sUserAgent) ); $debugObj->output("local_logLinkClick($db, $debugObj, $iLinkID, $sIP, $sUserAgent) : $sql"); $db->query($sql); return; } ?>