prefix . "landingpage_links", $matches[1]); $res1 = $wpdb->get_row($linksql); if (strlen($res1->link) == 0) { // it doesn't exist $wpdb->query( sprintf('INSERT INTO %s (link) VALUES ("%s")', $wpdb->prefix . "landingpage_links", $matches[1]) ); $linkid = $wpdb->insert_id; } else { // It exists $linkid = $res1->id; } return get_option('siteurl') . '/outbound/' . $linkid; } function getReferralInformation() { global $landingpage_uniqueid; // set here global $wpdb; // Only proceed if the cookie is nonexistent if(isset($_COOKIE['zart'])) { $landingpage_uniqueid = $_COOKIE['zart']; return; } // Figure out who got us here, what keyword, etc $info = getTrackingInfo(); // Save referral info to the database $sql = sprintf('INSERT INTO %s (time, landed_on, keywords, referrer, ip) VALUES (NOW(), "%s", "%s", "%s", "%s")', $wpdb->prefix . "landingpage_in", $info["page"], $info["keywords"], $info["wholestring"], $info["ip"]); $wpdb->query($sql); // Get our Unique ID for this visitor, and save it to a global $landingpage_uniqueid = $wpdb->insert_id; // Save the ID to a cookie for persistance setcookie('zart', $landingpage_uniqueid, time() + 86400, "/"); } function landingpageInstall() { $in_table = array("id mediumint(9) NOT NULL AUTO_INCREMENT", "time datetime NOT NULL", "landed_on VARCHAR(255) NOT NULL", "keywords VARCHAR(255) NOT NULL", "ip VARCHAR(40) NOT NULL", "referrer text", "UNIQUE KEY id (id)"); installTable("landingpage_in", $in_table); $out_table = array("id mediumint(9) NOT NULL AUTO_INCREMENT", "in_id mediumint(9) NOT NULL", "time datetime NOT NULL", "outlink mediumint(9)", "UNIQUE KEY id (id)"); installTable("landingpage_out", $out_table); $links_table = array("id mediumint(9) NOT NULL AUTO_INCREMENT", "link VARCHAR(255)", "UNIQUE KEY id (id)"); installTable("landingpage_links", $links_table); } // Catches outbound links in the form of // /outbound/1 // where 1 is the reference to the link in landingpage_links // The ID is grabbed from a cookie // Filtering of the data is implicit here, as only integers will pass the regex function catchOutboundLinks() { if ( preg_match('#outbound/(\d+)#', $_SERVER['REQUEST_URI'], $matches) ) { $linkid = $matches[1]; $userid = $_COOKIE['zart']; global $wpdb; // Get the link from the database and validate it $linksql = sprintf('SELECT link FROM %s WHERE id=%d', $wpdb->prefix . "landingpage_links", $linkid); $theRealLink = $wpdb->get_row($linksql, ARRAY_N); if (strlen($theRealLink[0]) == 0) { return; } // log $logsql = sprintf('INSERT INTO %s (in_id, time, outlink) VALUES (%d, NOW(), %d)', $wpdb->prefix . "landingpage_out", $userid, $linkid); $wpdb->query($logsql); // send the user along // sub in the id if appropriate header("Location: " . preg_replace("/%d/", $userid, $theRealLink[0])); } } function installTable($table, $instructions) { global $wpdb; $table_name = $wpdb->prefix . $table; if($wpdb->get_var("show tables like '$table_name'") != $table_name) { $sql = "CREATE TABLE " . $table_name . " ( " . join (",\n", $instructions) . ");"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); add_option("landingpage_db_version", "1.0"); } } function getTrackingInfo() { $qs=after("?",$_SERVER['HTTP_REFERER']); $dn=before("?",$_SERVER['HTTP_REFERER']); $pairs=explode("&",$qs); if (eregi("google",$dn)) { foreach ($pairs as $k=>$v) { $args=explode("=",$v); if ($args[0]=="q" || $args[0]=="as_q" || $args[0]=="as_epq") { if (strlen($args)>0 && $args[1]!="0") { $keys=urldecode($args[1]); } } } } elseif (eregi("msn|live",$dn)) { foreach ($pairs as $k=>$v) { $args=explode("=",$v); if ($args[0]=="q") { if (strlen($args[1])>0 && $args[1]!="0") { $keys=urldecode($args[1]); } } } } elseif (eregi("yahoo",$dn)) { foreach ($pairs as $k=>$v) { $args=explode("=",$v); if ($args[0]=="p") { if (strlen($args[1])>0 && $args[1]!="0") { $keys=urldecode($args[1]); } } } } elseif (eregi("aol",$dn)) { foreach ($pairs as $k=>$v) { $args=explode("=",$v); if ($args[0]=="query") { if (strlen($args[1])>0 && $args[1]!="0") { $keys=urldecode($args[1]); } } } } elseif (eregi("hotbot",$dn)) { foreach ($pairs as $k=>$v) { $args=explode("=",$v); if ($args[0]=="query") { if (strlen($args[1])>0 && $args[1]!="0") { $keys=urldecode($args[1]); } } } } $result = Array(); $result["wholestring"] = $_SERVER['HTTP_REFERER']; $result["keywords"] = strtolower(trim($keys)); $result["page"] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ; $result["ip"] = $_SERVER['REMOTE_ADDR']; return $result; } function after ($this, $inthat) { if (!is_bool(strpos($inthat, $this))) return substr($inthat, strpos($inthat,$this)+strlen($this)); } function before ($this, $inthat) { return substr($inthat, 0, strpos($inthat, $this)); } function adminMainPage() { echo 'main page'; } function adminViewLinks() { global $wpdb; echo "

All links in the landingpage_links table

"; $alllinks = $wpdb->get_results( sprintf('SELECT id, link FROM %s ORDER BY id', $wpdb->prefix . "landingpage_links") ); echo '
'; foreach ($alllinks as $l) { echo '
'; echo '
'; echo $l->id; echo '
'; echo '
'; echo $l->link; echo '
'; echo '
'; echo '[edit]'; echo '
'; echo '
'; } echo '
'; } function adminViewStats() { global $wpdb; echo "

All visitors

"; $alllinks = $wpdb->get_results( sprintf('SELECT t_in.id, t_in.time AS in_time, t_in.landed_on, t_in.keywords, t_in.ip, t_in.referrer, t_out.time AS out_time, t_links.link FROM %slandingpage_in AS t_in LEFT OUTER JOIN %slandingpage_out AS t_out ON (t_in.id=t_out.in_id) LEFT OUTER JOIN %slandingpage_links AS t_links ON (t_out.outlink = t_links.id) ORDER BY t_in.id DESC', $wpdb->prefix, $wpdb->prefix, $wpdb->prefix ), /* sprintf('SELECT t_in.id, t_in.time AS in_time, t_in.landed_on, t_in.keywords, t_in.ip, t_in.referrer, t_out.time AS out_time, t_links.link FROM %slandingpage_in AS t_in, %slandingpage_out AS t_out, %slandingpage_links AS t_links WHERE t_in.id = t_out.in_id AND t_out.outlink = t_links.id ORDER BY t_in.id DESC', $wpdb->prefix, $wpdb->prefix, $wpdb->prefix ), */ ARRAY_N ); echo '
'; echo '
';
	echo "id,in_time,landed_on,keywords,ip,referrer,out_time,link\n";
	foreach ($alllinks as $l) {
		echo implode(",", $l);
		echo "\n";
	}
	echo '
'; echo '
'; } ?>