<?php
/*
Plugin Name: semmelstatzR
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)
Version: 1.0.0 RC
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
 * @author      SEM-Team
 * @copyright   (c)2011-2012 SEM-Team
 * @link        http://sourceforge.net/projects/semmelstatz/
 * @license     http://www.gnu.org/licenses/gpl-3.0.html
 * 
 * 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$
 * 
 */
/**  
 * based upon semmelstatz
 * Copyright (c) 2005-2009 Andreas 'Redunzl' Mueller (redunzl@gmx.de)
 *    
 * @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
*/

require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'semmelstatzR_startup.php';


if(is_admin() == true) {
    
    /*
    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;
    }
     * 
     */

    /**
     * add CSS
     * 
     * @uses wp add_action()
     *
     * @return void
     */
    function semmelstatzR_css() {
        print "<link rel='stylesheet' href='".
                get_option('siteurl').
                "/wp-content/plugins/semmelstatzR/semmelstatzR.css' type='text/css' media='screen' />\n";
    }
    add_action('admin_head', 'semmelstatzR_css');
    ### End add CSS

    /**
     * add toplevel menu into admin
     *
     * @uses wp add_action() admin_menu
     *
     * @return void
     */
    function semmelstatzR_menu() {
        if (function_exists('add_menu_page')) {
            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');
        }
    }
    add_action('admin_menu', 'semmelstatzR_menu');
    ### end add toplevel adminmenu
    
    // include context help system
    include SEMMELSTATZR_SRVPATH."includes/help/start.php";


    /**
     * write defaults to options-array
     *
     * @return void
     */
    function sem_init_options() {
        global $wpdb;

        if(get_option('statz_options')) {
            $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'statz%';"); // L�schen der alten Optionen
        }

        if(!get_option('semmelstatzR_options')) {
            $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,
                "statz_use_img" => false
            );
            add_option( 'semmelstatzR_options', $sem_options );
        }
    }
    add_action('init', 'sem_init_options');
    ### End write defaults to options-array

}

    
### Anlegen der Tabellen, Optionen schreiben   
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 {
        die(semr_i18n('Cannot find <i>wp-admin/upgrade-functions.php</i> and <i>wp-admin/includes/upgrade.php</i>!'));
    }

    $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');
    }
}
add_action('activate_semmelstatzR/semmelstatzR.php', 'sem_statzsetup');

### Zugriff analysieren und in statz-Tabelle schreiben
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="" />';
        return; ### nothing else todo if stat image is used
    }
    
    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);
    
    $referer = $wpdb->escape(urldecode($_SERVER['HTTP_REFERER']));
    $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);
        $referer = "'".$referer."'";
    } else {
        $referer = 'NULL';
    }

    $readingnow = wp_title('', false);
    if(empty($readingnow)) {
        $page = 0;
    } else {
        $page = $wp_query->post->ID;
    }
    
    $wpdb->query("INSERT INTO $wpdb->statz (ip, time, referer, page, username)
            VALUES('$userip', '$blogtime', $referer, $page, '$username')");

    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
    }
}
    
### Zeitgesteuertes Limitieren der statz-Tabelle
// @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
}
	
/**
 * renders a ministatz for dashboard
 * Idee: René Tauchnitz
 * 
 * @return void
 */
function sem_dashboardStatz_show() {

    $widget_options = get_option( 'dashboard_widget_options' );
    
    echo '<p>';
    printf(semr_i18np("%d Visitor online", "%d Visitors online", sem_showNumUsersOnline()), sem_showNumUsersOnline());
    echo ' - ';
    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>';
}

/**
 * dashboard-control for ministatz
 * 
 * @author Ortwin Pinke
 * @since semR 1.0
 * 
 * @return void
 */
function sem_dashboardStatz_control() {
    if (!$widget_options = get_option('dashboard_widget_options'))
        $widget_options = array();

    if (!isset($widget_options['dashboard_sem_dashboardStatz']))
        $widget_options['dashboard_sem_dashboardStatz'] = array();

    if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['dashboard-sem-dashboardStatz'])) {
        $number = absint( $_POST['dashboard-sem-dashboardStatz']['items'] );
        $number = ($number > 20)?20:$number;
        $widget_options['dashboard_sem_dashboardStatz']['items'] = $number;
        $widget_options['dashboard_sem_dashboardStatz']['showinfo'] = 
        ($_POST['dashboard-sem-dashboardStatz']['showinfo'] == true)?true:false;
        update_option( 'dashboard_widget_options', $widget_options );
    }

	$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>';

    //print_r($widget_options['dashboard_sem_dashboardStatz']);
}
	
/**
 * 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;
    wp_add_dashboard_widget( 'sem_dashboardStatz', 'SemmelstatzR', 'sem_dashboardStatz_show', 'sem_dashboardStatz_control' );
}

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");
        //$thedays = array(array('hits' => 33));
        if (empty($thedays)) {
            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;
        }
        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);
	}
?>