query($sql);
}
header("Content-Type: image/gif");
echo base64_decode("R0lGODlhAQABAIAAANvf7wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
return;
}
if (isset($_GET["url"])){
require("./../../wp-config.php");
$url = base64_decode($_GET["url"]);
if (get_option("feed_statistics_track_clickthroughs")){
global $table_prefix;
global $wpdb;
$link_id = 0;
$wpdb->hide_errors();
$sql = "SELECT id FROM ".$table_prefix."feed_links WHERE url='".mysql_real_escape_string($url)."'";
$result = $wpdb->query($sql);
if ($result) {
$link_id = $wpdb->last_result[0]->id;
}
else {
$sql = "INSERT INTO ".$table_prefix."feed_links SET url='".mysql_real_escape_string($url)."'";
if ($wpdb->query($sql)) {
$link_id = $wpdb->insert_id;
}
}
if ($link_id) {
$sql = "INSERT INTO ".$table_prefix."feed_clickthroughs SET
link_id=$link_id,
time=NOW()";
$wpdb->query($sql);
}
}
$wpdb->show_errors();
header("Location: ".$url);
return;
}
class FEED_STATS {
function init(){
global $table_prefix;
global $wpdb;
$version = get_option("feed_statistics_version");
if (isset($_GET["reset_feed_statistics"])) $version = '';
switch ($version) {
case '1.0':
case '1.0.1':
case '1.0.2':
case '1.0.3':
case '1.0.4':
$sql = "ALTER TABLE ".$table_prefix."feed_subscribers ADD user_agent VARCHAR(255) NOT NULL DEFAULT ''";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_clickthroughs (
id INT(11) NOT NULL auto_increment,
link_id INT(11) NOT NULL DEFAULT '0',
referrer_id INT(11) NOT NULL DEFAULT '0',
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)
)";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_links (
id INT(11) NOT NULL auto_increment,
url VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY `url` (`url`)
)";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_referrers (
id INT(11) NOT NULL auto_increment,
url VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY `url` (`url`)
)";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_postviews (
id INT(11) NOT NULL auto_increment,
post_id INT(11) NOT NULL DEFAULT '0',
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)
)";
$wpdb->query($sql);
update_option("feed_statistics_track_clickthroughs", "0");
update_option("feed_statistics_track_postviews", "1");
case '1.1':
case '1.1.1':
case '1.1.2':
$sql = "ALTER TABLE ".$table_prefix."feed_subscribers ADD `feed` VARCHAR( 255 ) NOT NULL AFTER `identifier`";
$wpdb->query($sql);
$sql = "ALTER TABLE ".$table_prefix."feed_subscribers DROP PRIMARY KEY, ADD PRIMARY KEY (identifier, feed)";
$wpdb->query($sql);
case '1.2':
case '1.3':
$sql = "DROP TABLE ".$table_prefix."feed_referrers";
$wpdb->query($sql);
$sql = "ALTER TABLE ".$table_prefix."feed_clickthroughs DROP referrer_id";
$wpdb->query($sql);
update_option("feed_statistics_version","1.3.1");
case '1.3.1':
break;
default:
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_subscribers (
subscribers INT(11) NOT NULL DEFAULT 0,
identifier VARCHAR(200) NOT NULL DEFAULT '',
feed VARCHAR( 200 ) NOT NULL,
date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_agent VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (identifier, feed)
)";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_clickthroughs (
id INT(11) NOT NULL auto_increment,
link_id INT(11) NOT NULL DEFAULT '0',
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)
)";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_links (
id INT(11) NOT NULL auto_increment,
url VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY `url` (`url`)
)";
$wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$table_prefix."feed_postviews (
id INT(11) NOT NULL auto_increment,
post_id INT(11) NOT NULL DEFAULT '0',
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)
)";
$wpdb->query($sql);
update_option("feed_statistics_expiration_days","3");
update_option("feed_statistics_version","1.3.1");
update_option("feed_statistics_track_clickthroughs", "0");
update_option("feed_statistics_track_postviews", "1");
break;
}
if (FEED_STATS::is_feed_url()){
$user_agent = $_SERVER["HTTP_USER_AGENT"];
if (!preg_match("/(Mozilla|Opera|subscriber|user|feed)/Uis", $user_agent)){
if (strlen($user_agent) > 3){
return;
}
}
if (!preg_match("/(readers|subscriber|user|feed)/Uis", $user_agent)){
if (preg_match("/(slurp|bot|spider)/Uis", $user_agent)){
return;
}
}
$m = array();
if (preg_match("/([0-9]+) subscriber/Uis", $user_agent, $m)) {
// Not a typo below - should have been replacing $m[1], but screwed it up the first time around, so it's here to stay
$identifier = str_replace($m[0], "###", $user_agent);
$subscribers = $m[1];
}
else if (preg_match("/users ([0-9]+);/Uis", $user_agent, $m)) {
// For Yahoo!'s bot
$identifier = str_replace($m[1], "###", $user_agent);
$subscribers = $m[1];
}
else if (preg_match("/ ([0-9]+) readers/Uis", $user_agent, $m)) {
// For LiveJournal's bot
$identifier = str_replace($m[1], "###", $user_agent);
$subscribers = $m[1];
}
else {
$identifier = $_SERVER["REMOTE_ADDR"];
$subscribers = 1;
}
$feed = $_SERVER["REQUEST_URI"];
if (!preg_match("/(\/|\.php|\?.*)$/Uis", $feed)){
$feed .= "/";
}
$q = "SELECT * FROM ".$table_prefix."feed_subscribers
WHERE identifier='".mysql_real_escape_string($identifier)."'
AND feed=''";
$results = $wpdb->get_results($q);
if (!empty($results)) {
$q = "UPDATE ".$table_prefix."feed_subscribers
SET
subscribers=".intval($subscribers).",
identifier='".mysql_real_escape_string($identifier)."',
user_agent='".mysql_real_escape_string($user_agent)."',
feed='".mysql_real_escape_string($feed)."',
date=NOW()
WHERE
identifier='".mysql_real_escape_string($identifier)."'
AND feed=''";
$wpdb->query($q);
}
else {
$q = "SELECT * FROM ".$table_prefix."feed_subscribers WHERE identifier='".mysql_real_escape_string($identifier)."' AND feed='".mysql_real_escape_string($feed)."'";
$result = $wpdb->query($q);
if ($result == 0) {
$q = "INSERT INTO ".$table_prefix."feed_subscribers
SET
subscribers=".intval($subscribers).",
identifier='".mysql_real_escape_string($identifier)."',
user_agent='".mysql_real_escape_string($user_agent)."',
feed='".mysql_real_escape_string($feed)."',
date=NOW()";
$wpdb->query($q);
}
else {
$row = $wpdb->last_result[0];
if ($user_agent != $row->user_agent || $subscribers != $row->subscribers){
$q = "UPDATE ".$table_prefix."feed_subscribers
SET
date=NOW(),
user_agent='".mysql_real_escape_string($user_agent)."',
subscribers=".intval($subscribers)."
WHERE identifier='".mysql_real_escape_string($identifier)."' AND feed='".mysql_real_escape_string($feed)."'";
$wpdb->query($q);
}
}
}
}
}
function is_feed_url() {
switch (basename($_SERVER['PHP_SELF'])) {
case 'wp-rdf.php':
case 'wp-rss.php':
case 'wp-rss2.php':
case 'wp-atom.php':
case 'feed':
case 'rss2':
case 'atom':
return true;
break;
}
if (isset($_GET["feed"])) {
return true;
}
if (preg_match("/^\/(feed|rss2?|atom|rdf)/Uis", $_SERVER["REQUEST_URI"])) {
return true;
}
if (preg_match("/\/(feed|rss2?|atom|rdf)\/?$/Uis", $_SERVER["REQUEST_URI"])){
return true;
}
return false;
}
function how_many_subscribers() {
global $table_prefix;
global $wpdb;
$q = "SELECT
subscribers,
CASE WHEN subscribers = 1 THEN identifier ELSE CONCAT(identifier, feed) END AS ident
FROM ".$table_prefix."feed_subscribers
WHERE
(
(date > '".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * get_option("feed_statistics_expiration_days")))."')
OR
(
LOCATE('###',identifier) != 0 AND
date > '".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * get_option("feed_statistics_expiration_days") * 3))."'
)
)
ORDER BY ident ASC, date DESC";
$results = $wpdb->get_results($q);
$s = 0;
$current_ident = '';
if (!empty($results)) {
foreach ($results as $row){
if ($row->ident != $current_ident){
$s += $row->subscribers;
$current_ident = $row->ident;
}
}
}
return intval($s);
}
function add_options_menu() {
add_menu_page('Feed Options', 'Feed', 8, basename(__FILE__), 'feed_statistics_feed_page');
add_submenu_page(basename(__FILE__), 'Top Feeds', 'Top Feeds', 8, 'feedstats-topfeeds', 'feed_statistics_topfeeds_page');
add_submenu_page(basename(__FILE__), 'Feed Readers', 'verwendete Feedreader', 8, 'feedstats-feedreaders', 'feed_statistics_feedreaders_page');
add_submenu_page(basename(__FILE__), 'Post Views', 'angeklickte Beiträge', 8, 'feedstats-postviews', 'feed_statistics_postviews_page');
add_submenu_page(basename(__FILE__), 'Clickthroughs', 'ausgehende Feedklicks', 8, 'feedstats-clickthroughs', 'feed_statistics_clickthroughs_page');
}
function clickthroughs_page(){
global $table_prefix;
global $wpdb;
?>
Zur Zeit ist das Tracking für ausgehende Links
.
Die am meist geklickten Beitragslinks der letzten 30 Tage
| |
Ziel des Links |
geklickt |
|
'".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * 30))."'
GROUP BY c.link_id
ORDER BY clicks DESC";
$results = $wpdb->get_results($sql);
$i = 1;
if (!empty($results)) {
$max = $results[0]->clicks;
foreach ($results as $row){
$percentage = ceil($row->clicks / $max * 100);
echo '| '.$i++.'. | '.$row->link.' | '.$row->clicks.' |
|
';
}
}
?>
Die meist abonnierten Feeds
| |
Feed Url |
Anzahl Abonnenten |
|
'".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * get_option("feed_statistics_expiration_days")))."')
OR
(
LOCATE('###',identifier) != 0 AND
date > '".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * get_option("feed_statistics_expiration_days") * 3))."'
)
)
GROUP BY feed
ORDER BY subscribers DESC";
$results = $wpdb->get_results($q);
$feeds = array();
$i = 1;
if (!empty($results)){
foreach ($results as $feed) {
if (!isset($max)) $max = $feed->subscribers;
$percentage = ceil($feed->subscribers / $max * 100);
echo '| '.$i++.'. | '.$feed->feed.' | '.$feed->subscribers.' | |
';
}
}
echo "
";
}
function postviews_page(){
global $table_prefix;
global $wpdb;
?>
Zur Zeit ist das Beitrags-Tracking
.
Meist gelesene Beiträge der letzten 30 Tage
| |
Beitragstitel |
Anzahl Leser |
|
'".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * 30))."'
GROUP BY v.post_id
ORDER BY views DESC
LIMIT 20";
$results = $wpdb->get_results($sql);
if (!empty($results)) {
$i = 1;
$max = $results[0]->views;
foreach ($results as $row) {
$percentage = ceil($row->views / $max * 100);
echo '
| '.$i++.'. |
'.$row->title.' |
'.$row->views.' |
|
';
}
}
?>
Meist verwendete Feedreader
'".date("Y-m-d H:i:s", time() - (60 * 60 * 24 * get_option("feed_statistics_expiration_days")))."'
GROUP BY reader
ORDER BY readers DESC";
$results = $wpdb->get_results($q);
$readers = array();
if (!empty($results)){
foreach ($results as $row){
$reader = $row->reader;
$version = array();
if ($reader == '') {
$reader = "Unknown (Pending)";
}
else if (preg_match("/Navigator\/([0-9abpre\.]+)/is", $reader, $version)){
$reader = "Netscape Navigator ".$version[1];
}
else if (preg_match("/Opera\/([0-9abpre\.]+)/is", $reader, $version)){
$reader = "Opera ".$version[1];
}
else if (preg_match("/Flock\/([0-9abpre\.]+)/is", $reader, $version)){
$reader = "Flock ".$version[1];
}
else if (preg_match("/(Firefox|BonEcho|GranParadiso)\/([0-9abpre\.]+)/is", $reader, $version)) {
$reader = "Mozilla ".$version[1]." ".$version[2];
}
else if (preg_match("/MSIE ([0-9abpre\.]+)/is", $reader, $version)){
$reader = "Internet Explorer ".$version[1];
}
else if (preg_match("/Gecko/Uis", $reader)) {
$reader = "Other Mozilla browser";
}
else if (!preg_match("/Mozilla/Uis", $reader)){
$reader = preg_replace("/[\/;].*$/Uis", "", $reader);
}
else {
continue;
}
foreach ($readers as $key => $d) {
if ($d["reader"] == $reader){
$readers[$key]["readers"] += $row->readers;
continue 2;
}
}
$readers[] = array("reader" => $reader, "readers" => $row->readers);
}
}
function sort_reader_array($a, $b) {
return $b["readers"] - $a["readers"];
}
usort($readers, 'sort_reader_array');
$max = $readers[0]["readers"];
$rv = '
';
$rv .= '| | Feedreader | Anzahl Verwender | |
';
$i = 1;
foreach ($readers as $reader) {
$percentage = ceil($reader["readers"] / $max * 100);
$rv .= '| '.$i++.'. | '.$reader["reader"].' | '.$reader["readers"].' | |
';
}
$rv .= "
";
return $rv;
}
function widget_register() {
if (function_exists('register_sidebar_widget')) {
register_sidebar_widget('Feed Statistics', 'feed_statistics_widget');
}
}
function widget($args) {
extract($args);
echo $before_widget;
echo '
';
feed_subscribers();
echo '';
echo $after_widget;
}
function clickthrough_replace($content) {
if (is_feed()) {
$redirect_url = get_bloginfo('url') . "/wp-content/plugins/feed-statistics.php?url=";
$content = preg_replace("/(
]+href=)(['\"])([^'\"]+)(['\"])([^>]*>)/e", "'$1\"$redirect_url'.base64_encode('\\3').'\"$5'", $content);
}
return $content;
}
function postview_tracker($content) {
global $id;
if (is_feed()) {
$content .= '
';
}
return $content;
}
function admin_head() {
?>