1
0
Fork 0
SemmelstatzR/trunk/semmelstatzR.php

396 Zeilen
15 KiB
PHP

<?php
/*
2011-11-14 08:13:34 +00:00
Plugin Name: semmelstatzR
2011-08-23 14:08:57 +00:00
Plugin URI: http://sourceforge.net/projects/semmelstatz/
Description: Visitorstatistic for Wordpress, based upon the original wp-plugin semmelstatz by Andreas 'Redunzl' Mueller (http://www.kopfhoch-studio.de)
2011-12-17 15:26:24 +00:00
Version: 1.0 RC
2011-11-14 08:13:34 +00:00
Author: SEM-Team
Author URI: http://semmelstatz.sf.net
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* file semmelstatzR.php
*
* @package SemmelstatzR
* @version $Rev$
* @since 1.0.0 Beta
* @author SEM-Team
2011-12-17 15:26:24 +00:00
* @copyright (c)2011-2012 SEM-Team
* @link http://sourceforge.net/projects/semmelstatz/
* @license http://www.gnu.org/licenses/gpl-3.0.html
2011-11-14 08:13:34 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* $Id$
2011-11-14 08:13:34 +00:00
*
*/
/**
* based upon semmelstatz
* Copyright (c) 2005-2009 Andreas 'Redunzl' Mueller (redunzl@gmx.de)
2011-11-14 08:13:34 +00:00
*
* @license http://www.gnu.org/licenses/gpl-2.0.html
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2) as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2011-11-14 08:13:34 +00:00
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'semmelstatzR_startup.php';
2011-08-23 14:08:57 +00:00
if(is_admin() == true) {
2011-12-19 17:48:13 +00:00
/*
add_filter('screen_settings', 'screen_options_demo', 10, 2);
function screen_options_demo($current, $screen){
$desired_screen = convert_to_screen('plugins.php');
if ( $screen->id == $desired_screen->id ){
$current .= "Hello WordPress!";
}
return $current;
}
*
*/
2011-08-23 14:08:57 +00:00
/**
* add CSS
2011-11-15 23:36:35 +00:00
*
* @uses wp add_action()
2011-08-23 14:08:57 +00:00
*
* @return void
*/
function semmelstatzR_css() {
2011-11-15 23:36:35 +00:00
print "<link rel='stylesheet' href='".
get_option('siteurl').
"/wp-content/plugins/semmelstatzR/semmelstatzR.css' type='text/css' media='screen' />\n";
2011-08-23 14:08:57 +00:00
}
add_action('admin_head', 'semmelstatzR_css');
### End add CSS
/**
* add toplevel menu into admin
*
2011-11-15 23:36:35 +00:00
* @uses wp add_action() admin_menu
2011-08-23 14:08:57 +00:00
*
* @return void
*/
function semmelstatzR_menu() {
if (function_exists('add_menu_page')) {
2011-11-15 23:36:35 +00:00
add_menu_page('SemmelstatzR','SemmelstatzR',
'manage_statz', 'semmelstatzR/semmelstatzR-statz.php', '',
plugins_url('semmelstatzR/img/statz_small.png'));
}
if (function_exists('add_submenu_page')) {
global $semr_hook;
$semr_hook['statz'] = add_submenu_page('semmelstatzR/semmelstatzR-statz.php',
semr_i18n('Current Statz'), semr_i18n('Current Statz'),
'manage_statz', 'semmelstatzR/semmelstatzR-statz.php');
$semr_hook['history'] = add_submenu_page('semmelstatzR/semmelstatzR-statz.php',
semr_i18n('History'), semr_i18n('History'), 'manage_statz',
'semmelstatzR/semmelstatzR-history.php');
$semr_hook['log'] = add_submenu_page('semmelstatzR/semmelstatzR-statz.php',
semr_i18n('Log'), semr_i18n('Log'), 'manage_statz',
'semmelstatzR/semmelstatzR-log.php');
$semr_hook['database'] = add_submenu_page('semmelstatzR/semmelstatzR-statz.php',
semr_i18n('Database'), semr_i18n('Database'), 'manage_statz',
'semmelstatzR/semmelstatzR-database.php');
$semr_hook['templates'] = add_submenu_page('semmelstatzR/semmelstatzR-statz.php',
semr_i18n('Templates'), semr_i18n('Templates'), 'manage_statz',
'semmelstatzR/semmelstatzR-templates.php');
$semr_hook['options'] = add_submenu_page('semmelstatzR/semmelstatzR-statz.php',
semr_i18n('Options'), semr_i18n('Options'), 'manage_statz',
'semmelstatzR/semmelstatzR-options.php');
}
2011-08-23 14:08:57 +00:00
}
add_action('admin_menu', 'semmelstatzR_menu');
2011-11-15 23:36:35 +00:00
### end add toplevel adminmenu
// include context help system
include SEMMELSTATZR_SRVPATH."includes/help/start.php";
2011-08-23 14:08:57 +00:00
/**
* write defaults to options-array
*
* @return void
*/
function sem_init_options() {
2011-08-23 14:08:57 +00:00
global $wpdb;
if(get_option('statz_options')) {
2011-12-17 15:26:24 +00:00
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'statz%';"); // L<>schen der alten Optionen
2011-08-23 14:08:57 +00:00
}
if(!get_option('semmelstatzR_options')) {
2011-08-23 14:08:57 +00:00
$sem_options = array(
"statz_comment_limit" => 10,
"statz_datarecs_limit" => 100,
"statz_days_limit" => 7,
"statz_do_cron" => false,
"statz_encode_ip" => false,
"statz_keyword_limit" =>10,
"statz_referer_limit" => 10,
"statz_mask_referer" => false,
"statz_on_dashboard" => false,
"statz_online_time" => 180,
"statz_recdays_limit" => 20,
"statz_show_commenters" => true,
"statz_show_dailystatz" => true,
"statz_show_keywords" => true,
"statz_show_reads" => true,
"statz_show_referers" => true,
"statz_show_todayreads" => true,
"statz_show_todayusers" => true,
"statz_show_topcommposts" => true,
"statz_show_useronline" => true,
"statz_topreads_limit" => 10,
"statz_write_admins" => false,
"statz_write_users" => false,
2011-12-17 15:26:24 +00:00
"statz_use_img" => false
2011-08-23 14:08:57 +00:00
);
add_option( 'semmelstatzR_options', $sem_options );
}
}
2011-08-23 14:08:57 +00:00
add_action('init', 'sem_init_options');
### End write defaults to options-array
}
### Anlegen der Tabellen, Optionen schreiben
2011-08-23 14:08:57 +00:00
function sem_statzsetup() {
global $wpdb;
if(@is_file(ABSPATH.'/wp-admin/upgrade-functions.php')) {
include_once(ABSPATH.'/wp-admin/upgrade-functions.php');
} elseif(@is_file(ABSPATH.'/wp-admin/includes/upgrade.php')) {
include_once(ABSPATH.'/wp-admin/includes/upgrade.php');
} else {
2011-12-17 15:26:24 +00:00
die(semr_i18n('Cannot find <i>wp-admin/upgrade-functions.php</i> and <i>wp-admin/includes/upgrade.php</i>!'));
}
2011-08-23 14:08:57 +00:00
$create_table = array();
$create_table['statz'] = "CREATE TABLE $wpdb->statz (
id int(11) unsigned NOT NULL auto_increment,
ip varchar(39) default NULL,
time datetime NOT NULL default '0000-00-00 00:00:00',
referer varchar(255) default NULL,
page int(11) unsigned NOT NULL default '0',
username varchar(255) default NULL,
PRIMARY KEY (`id`))";
$create_table['statzhist'] = "CREATE TABLE $wpdb->statzhist (
id int(10) unsigned NOT NULL auto_increment,
date datetime NOT NULL default '0000-00-00 00:00:00',
visitors int(10) unsigned NOT NULL default '0',
hits int(10) unsigned NOT NULL default '0',
referers int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`))";
maybe_create_table($wpdb->statz, $create_table['statz']);
maybe_create_table($wpdb->statzhist, $create_table['statzhist']);
$sql = $wpdb->query("SELECT host FROM $wpdb->statz");
if($sql) { $wpdb->query("ALTER TABLE $wpdb->statz DROP host"); }
$sql = $wpdb->query("SELECT keyword FROM $wpdb->statz");
if($sql) { $wpdb->query("ALTER TABLE $wpdb->statz DROP keyword"); }
maybe_add_column($wpdb->posts, 'hits', "ALTER TABLE $wpdb->posts ADD hits bigint(20) NOT NULL DEFAULT '0';"); // ab 3.2
$wpdb->query("ALTER TABLE $wpdb->statz CHANGE ip ip VARCHAR(39) NULL DEFAULT NULL;");
sem_init_options();
sem_writeOldDaysToHist();
$role = get_role('administrator');
if(!$role->has_cap('manage_statz')) {
$role->add_cap('manage_statz');
}
}
2011-11-15 23:36:35 +00:00
add_action('activate_semmelstatzR/semmelstatzR.php', 'sem_statzsetup');
2011-08-23 14:08:57 +00:00
### Zugriff analysieren und in statz-Tabelle schreiben
2011-08-23 14:08:57 +00:00
add_action('wp_footer', 'sem_writeStatz');
function sem_writeStatz() {
global $wpdb, $wp_query, $isBot, $isAdmin, $page;
$plugin_dir = basename(dirname(__FILE__));
$sem_options = get_option('semmelstatzR_options'); ### optionsarray auslesen
// true === $sem_options['statz_use_img'] &&
if(true == $sem_options['statz_use_img'] && !defined('SEMMELSTATZR_STATZIMG')) {
print '<img src="'.SEMMELSTATZR_HTML_RELPATH.'semr_image.php" alt="" />';
2011-12-17 15:26:24 +00:00
return; ### nothing else todo if stat image is used
2011-08-23 14:08:57 +00:00
}
2011-12-17 15:26:24 +00:00
2011-08-23 14:08:57 +00:00
sem_writeYesterdayToHist();
$useragent = $wpdb->escape($_SERVER['HTTP_USER_AGENT']);
$isBot = sem_AreYouBot($useragent);
$username = sem_getUsername();
if(($isBot == true)
|| ($isAdmin == 0 && $sem_options['statz_write_users'] == false)
|| ($isAdmin == 1 && $sem_options['statz_write_admins'] == false)
|| is_404()
) return;
$userip = $wpdb->escape($_SERVER['REMOTE_ADDR']);
if($sem_options['statz_encode_ip'] == true) $userip = sem_encodeIP($userip);
2011-12-17 15:26:24 +00:00
$referer = $wpdb->escape(urldecode($_SERVER['HTTP_REFERER']));
2011-08-23 14:08:57 +00:00
$blogtime = gmdate('Y-m-d H:i:s', current_time('timestamp'));
if (!empty($referer)) {
if(get_option('blog_charset') == 'iso-8859-1') $referer = utf8_decode($referer);
2011-12-17 15:26:24 +00:00
$referer = "'".$referer."'";
} else {
$referer = 'NULL';
2011-08-23 14:08:57 +00:00
}
$readingnow = wp_title('', false);
if(empty($readingnow)) {
$page = 0;
} else {
$page = $wp_query->post->ID;
}
2011-12-17 15:26:24 +00:00
$wpdb->query("INSERT INTO $wpdb->statz (ip, time, referer, page, username)
VALUES('$userip', '$blogtime', $referer, $page, '$username')");
2011-08-23 14:08:57 +00:00
if($wpdb->query("SELECT COUNT(hits) FROM $wpdb->posts")) {
$wpdb->query("UPDATE $wpdb->posts SET hits = hits + 1 WHERE ID = $page"); // neu ab 3.2
}
2011-08-23 14:08:57 +00:00
}
### Zeitgesteuertes Limitieren der statz-Tabelle
2011-12-17 15:26:24 +00:00
// @todo add action cache
// Wenn semmelKron JA UND statz_next_cron älter JETZT, dann...
$sem_options = get_option('semmelstatzR_options'); ### optionsarray auslesen
if($sem_options['statz_do_cron'] == true && $sem_options['statz_next_cron'] < time()) {
add_action('shutdown', 'sem_doCronStatzLimit'); // ...limitiere statz-Tabelle auf vorgegebenen Wert
}
2011-12-17 15:26:24 +00:00
/**
* renders a ministatz for dashboard
* Idee: René Tauchnitz
*
* @return void
*/
2011-08-23 14:08:57 +00:00
function sem_dashboardStatz_show() {
2011-11-15 23:36:35 +00:00
$widget_options = get_option( 'dashboard_widget_options' );
echo '<p>';
2011-11-15 23:36:35 +00:00
printf(semr_i18np("%d Visitor online", "%d Visitors online", sem_showNumUsersOnline()), sem_showNumUsersOnline());
echo ' - ';
2011-11-15 23:36:35 +00:00
printf(semr_i18n("Today: %s"),sem_showTodayStatz());
echo ' <a title="'.semr_i18n("more...").'" href="'.get_option('siteurl')
.'/wp-admin/admin.php?page=semmelstatzR/semmelstatzR-statz.php">&raquo;&raquo;</a>';
echo '</p>';
echo '<p style="line-height: 2.0em;">';
printf(semr_i18n("This blog is online since %s that are %s days.<br>"),
'<span class="rahmen">'.sem_showFirstPostDate()."</span>",
'<span class="rahmen">'.sem_showPostDays()."</span>");
printf(semr_i18n("SemmelstatzR collects the statistics since %s."),
'<span class="rahmen">'.sem_showStatzStartDate()."</span>");
echo '</p>';
}
2011-08-23 14:08:57 +00:00
2011-12-17 15:26:24 +00:00
/**
* dashboard-control for ministatz
*
* @author Ortwin Pinke
* @since semR 1.0
*
* @return void
*/
2011-08-23 14:08:57 +00:00
function sem_dashboardStatz_control() {
2011-11-15 23:36:35 +00:00
if (!$widget_options = get_option('dashboard_widget_options'))
2011-08-23 14:08:57 +00:00
$widget_options = array();
2011-11-15 23:36:35 +00:00
if (!isset($widget_options['dashboard_sem_dashboardStatz']))
2011-08-23 14:08:57 +00:00
$widget_options['dashboard_sem_dashboardStatz'] = array();
2011-11-15 23:36:35 +00:00
if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['dashboard-sem-dashboardStatz'])) {
2011-08-23 14:08:57 +00:00
$number = absint( $_POST['dashboard-sem-dashboardStatz']['items'] );
2011-08-24 09:20:32 +00:00
$number = ($number > 20)?20:$number;
2011-08-23 14:08:57 +00:00
$widget_options['dashboard_sem_dashboardStatz']['items'] = $number;
2011-11-15 23:36:35 +00:00
$widget_options['dashboard_sem_dashboardStatz']['showinfo'] =
($_POST['dashboard-sem-dashboardStatz']['showinfo'] == true)?true:false;
2011-08-23 14:08:57 +00:00
update_option( 'dashboard_widget_options', $widget_options );
}
2011-11-15 23:36:35 +00:00
$number = isset( $widget_options['dashboard_sem_dashboardStatz']['items'] )?
(int) $widget_options['dashboard_sem_dashboardStatz']['items'] : '';
$checked = isset($widget_options['dashboard_sem_dashboardStatz']['showinfo'])?'checked="checked"':'';
echo '<p><label for="sem-info">' . semr_i18n('Show semR Info:').'</label>';
echo '<input id="sem-ninfo" name="dashboard-sem-dashboardStatz[showinfo]" type="checkbox" value="true"'.$checked.' /></p>';
echo '<p><label for="sem-number">' . semr_i18n('Number of Items (max. 10):') . '</label>';
echo '<input id="sem-number" name="dashboard-sem-dashboardStatz[items]" type="text" value="'.$number.'" size="3" /></p>';
2011-08-23 14:08:57 +00:00
2011-08-24 09:20:32 +00:00
//print_r($widget_options['dashboard_sem_dashboardStatz']);
2011-08-23 14:08:57 +00:00
}
2011-08-24 09:20:32 +00:00
/**
* Callback generating a small dashboard widget
*
* @uses sem_showTodayStatz() to generate the content
* @uses wp_add_dashboard_widget() to generate widget
*
* @return void
*/
function sem_dashboardStatz_setup() {
$sem_options = get_option('semmelstatzR_options'); ### optionsarray auslesen
if(!current_user_can('manage_statz')) return;
if($sem_options['statz_on_dashboard'] != true) return;
2011-08-23 14:08:57 +00:00
wp_add_dashboard_widget( 'sem_dashboardStatz', 'SemmelstatzR', 'sem_dashboardStatz_show', 'sem_dashboardStatz_control' );
}
2011-08-24 09:20:32 +00:00
add_action('wp_dashboard_setup', 'sem_dashboardStatz_setup');
/**
* generates and returns content for a small dashboard widget
*
* @global wpdb $wpdb
* @return string formated string/html for dashboard
*/
function sem_showTodayStatz() {
global $wpdb;
$thedays = $wpdb->get_results("SELECT COUNT(ip) AS hits, COUNT(DISTINCT ip) AS visitors, COUNT(DISTINCT referer)
AS referers, substring(time,1,10) AS date FROM ".$wpdb->statz." WHERE time >= CURDATE() GROUP BY date");
2011-08-23 14:08:57 +00:00
//$thedays = array(array('hits' => 33));
if (empty($thedays)) {
2011-08-24 09:20:32 +00:00
return "<small>".semr_i18n("... no entries")."</small>";
}
foreach($thedays as $key=>$theday) {
$date = mysql2date("l",$theday->date);
$hits = $theday->hits;
$visitors = $theday->visitors;
$referers = $theday->referers;
}
2011-08-24 09:20:32 +00:00
return sprintf(semr_i18np("%d Visitor","%d Visitors",$visitors),$visitors)." - "
.sprintf(semr_i18np("%d Hit","%d Hits",$hits),$hits)." - "
.sprintf(semr_i18np("%d Referer","%d Referers",$referers),$referers);
}
?>