#################################################################
## Mod Title:	   PHPCounter_MySQL_rb
## Mod Version:	   1.0.3
## Rev Date:	   March 01, 2005
##
## MODAuthor:	   svbomber < http://www.svbomber.de >
## CoAutor:	   Many thanks to oxpus < http://www.oxpus.de > for help
## Function Author: unknown
##
## Description:	This MOD add a Visit Counter to your phpbb Plus board in Portal and Index.
##		Use MySQL to store data, including reload barrier by IP.
##               Reload-time can set in ACP.
##
## Installation Level:	easy
## Installation Time:	~15 Minutes
## Files To Edit:	10
##
##			index.php
##                       portal.php
##			admin/admin_board.php
##			includes/functions.php
##			includes/page_header.php
##			language/lang_german/lang_admin.php
##			language/lang_german/lang_main.php
##			templates/fisubsilversh/index_body.tpl
##			templates/fisubsilversh/index_body_plus.tpl
##			templates/fisubsilversh/portal_body.tpl
##			templates/fisubsilversh/admin/board_config_body.tpl
##
## Included Files:	phpcounter_db_install.php
##
#################################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
#################################################################
##
## Version History:
##
## v1.0.3 (svbomber)
##   	- add Prfix phpbb_
##	- add phpcounter_db_install.php
##
## v1.0.2 (svbomber)
##   	- Counter displaying in 'Who ist online' when Index in ACP set to phpBB2 Default
##	- BugFix in SQL INSERT command
##
## v1.0.1 (svbomber)
##   	- BugFix in functions.php, wrong var counter time
##
## v1.0.0 (svbomber)
##	- Set reload-time via ACP
##
## v0.1.0 beta (svbomber)
##	- Initial
##
#################################################################
## Author Notes:
##
## Instead entering the given SQL-Statement, you can upload the file phpcounter_db_install.php
## to your phpbb root directory and run this with your browser.
## This file will do all nessassary changes in the database for you.
## After using this file, please delete it to avoid errors.
##
#################################################################
#
#-----[ SQL ]------------------------------------------
#
CREATE TABLE `phpbb_counter` (
  `num` int(10) unsigned NOT NULL default '0',
  `page` varchar(100) default NULL,
  KEY `page` (`page`)
) TYPE=MyISAM;

CREATE TABLE `phpbb_counter_ips` (
  `IP` varchar(16) NOT NULL default '',
  `page` varchar(100) default NULL,
  `viewtime` int(11) unsigned NOT NULL default '0',
  KEY `IP` (`IP`),
  KEY `viewtime` (`viewtime`),
  KEY `page` (`page`)
) TYPE=MyISAM;

INSERT INTO `phpbb_counter` VALUES (1, '');
INSERT INTO `phpbb_config` VALUES ('counter_reload_time', '3600');

#
#-----[ OPEN ]------------------------------------------
#
index.php

#
#-----[ FIND ]------------------------------------------
#
        	'TOTAL_POSTS' => $total_posts,

#
#-----[ AFTER ADD ]------------------------------------------
#
	'COUNTER' => Counter(),

#
#-----[ FIND ]------------------------------------------
#
		'TOTAL_POSTS' => sprintf($l_total_post_s, $total_posts),

#
#-----[ AFTER ADD ]------------------------------------------
#
		'COUNTER' => Counter(),


#
#-----[ OPEN ]------------------------------------------
#
portal.php

#
#-----[ FIND ]------------------------------------------
#
	'TOTAL_POSTS' => sprintf($l_total_post_s, $total_posts),

#
#-----[ AFTER ADD ]------------------------------------------
#
	'COUNTER' => Counter(),


#
#-----[ OPEN ]---------------------------------
#
admin/admin_board.php

#
#-----[ FIND ]---------------------------------
#
	"L_ENABLE_PRUNE" => $lang['Enable_prune'],

#
#-----[ AFTER, ADD ]---------------------------------
#
         "L_COUNTER_RELOAD_TIME" => $lang['counter_reload_time'],
   	"L_COUNTER_RELOAD_TIME_EXPLAIN" => $lang['counter_reload_time_explain'],

#
#-----[ FIND ]---------------------------------
#
	"PRUNE_NO" => $prune_no,

#
#-----[ AFTER, ADD ]---------------------------------
#
         "COUNTER_RELOAD_TIME" => $new['counter_reload_time'],


#
#-----[ OPEN ]------------------------------------------
#
includes/functions.php

#
#-----[ FIND ]------------------------------------------
#
?>

#
#-----[ BEFORE ADD ]------------------------------------------
#
# Default reload time set to 3600s = 1h in MySQL
#
// Start add - PHP Counter
function counter($page = '') {
    /* this function will take $page as an arguement.
     * using different values for $page will allow
     * you to use a different counter on each page.
     * if you want the same counter for all of your
     * site then don't pass in $page */

    global $board_config;
    $expire = time() - $board_config['counter_reload_time'];
    /* delete from table where ip has expired */
    $query = "DELETE FROM phpbb_counter_ips " .
             "WHERE viewtime < $expire " .
             "AND page = '$page'";
    mysql_query ($query);

    /* now we check to see if the users
     * ip exists in the table */
    $query = "SELECT ip FROM phpbb_counter_ips " .
             "WHERE ip = '{$_SERVER['REMOTE_ADDR']}'" .
             "AND page = '$page'";
    $result = mysql_query ($query);
    if (mysql_num_rows ($result) > 0) {
        /* if the ip existed, update the view time */
        $viewtime = time();
        $query = "UPDATE phpbb_counter_ips SET viewtime = '$viewtime'" .
                 "WHERE ip = '{$_SERVER['REMOTE_ADDR']}'" .
                 "AND page = '$page'";
        mysql_query ($query);
    } else {
        /* ok time to record a hit and the ip */

        /* first check if a record exists for $page */
        $query = "SELECT num FROM phpbb_counter " .
                 "WHERE page = '$page'";
        $result = mysql_query ($query);
        if (mysql_num_rows ($result) == 0) {
            /* if no row, add one */
            $query = "INSERT INTO phpbb_counter VALUES " .
                     "(1,'$page')";
            mysql_query ($query);
        } else {
            /* there is a row so just update it */
            $query = "UPDATE phpbb_counter SET num = num + 1 " .
                     "WHERE page = '$page'";
            mysql_query ($query);
        }
        /* add ip to ip table */
        $viewtime = time();
        $query = "INSERT INTO phpbb_counter_ips VALUES " .
                 "('{$_SERVER['REMOTE_ADDR']}', '$page', $viewtime)";
        mysql_query ($query);
    }

    /* now we get the counter value and return it */
    $query = "SELECT num FROM phpbb_counter WHERE page = '$page'";
    $row = mysql_fetch_assoc (mysql_query ($query));

     return $row['num'];
}
// End add - PHP Counter


#
#-----[ OPEN ]------------------------------------------
#
includes/page_header.php

#
#-----[ FIND ]------------------------------------------
#
	'L_FAQ' => $lang['FAQ'],

#
#-----[ AFTER ADD ]------------------------------------------
#
         'L_TOTAL_VISIT_COUNTER' => $lang['Total_visit_counter'],
         'L_TOTAL_VISITORS_TXT' => $lang['Total_visitors_txt'],
         'L_TOTAL_VISITORS_DATE' => $lang['Total_visitors_date'],


#
#-----[ OPEN ]------------------------------------------------
#
language/lang_german/lang_admin.php

#
#-----[ FIND ]------------------------------------------------
#
// That's all Folks!

#
#-----[ BEFORE, ADD ]------------------------------------------
#
// PHPCounter
$lang['counter_reload_time'] = "Reloadsperre Besucherzhler";
$lang['counter_reload_time_explain'] = "Legt die Zeit fr die Reloadsperre in Sekunden fest.";


#
#-----[ OPEN ]------------------------------------------
#
language/lang_german/lang_main.php

#
#-----[ FIND ]------------------------------------------
#
$lang['Total_posts'] = 'Beitrge insgesamt';

#
#-----[ AFTER ADD ]------------------------------------------
#
# Replace dd.mm.yyyy with date of start counting
#
#
$lang['Total_visit_counter'] = 'Besucherzhler';
$lang['Total_visitors_txt'] = 'Besucher seit dem';
$lang['Total_visitors_date'] = 'dd.mm.yyyy';


#
#-----[ OPEN ]------------------------------------------
#
templates/fisubsilversh/index_body_plus.tpl

#
#-----[ FIND ]------------------------------------------
#
{GOOGLE_VISIT_COUNTER}</span></td>

#
#-----[ REPLACE WITH ]------------------------------------------
#
{GOOGLE_VISIT_COUNTER}<br /><br />
{L_TOTAL_VISITORS_TXT}<br />{L_TOTAL_VISITORS_DATE}:&nbsp;<strong>{COUNTER}</strong></span></td>


#
#-----[ OPEN ]------------------------------------------
#
templates/fisubsilversh/index_body.tpl

#
#-----[ FIND ]------------------------------------------
#
{USERS_TODAY_LIST}</br></span></td>

#
#-----[ REPLACE WITH ]------------------------------------------
#
{USERS_TODAY_LIST}</br>
{L_TOTAL_VISITORS_TXT}&nbsp;{L_TOTAL_VISITORS_DATE}:&nbsp;<strong>{COUNTER}</strong></br></span></td>


#
#-----[ OPEN ]------------------------------------------
#
templates/fisubsilversh/portal_body.tpl

#
#-----[ FIND ]------------------------------------------
#
# or any position on the portal
#

<table width="100%" cellpadding="3" cellspacing="1" border="0" class="forumline">
<tr>
<th>{L_LINKS}</th>

#
#-----[ BEFORE ADD ]------------------------------------------
#
<table width="100%" cellpadding="2" cellspacing="1" border="0" class="forumline">
<tr>
<th>{L_TOTAL_VISIT_COUNTER}</th>
</tr>
<td class="row1" align="center"><span class="genmed"><strong>{COUNTER}</strong></span>
<br />
<span class="gensmall">{L_TOTAL_VISITORS_TXT}<br />{L_TOTAL_VISITORS_DATE}</span><br>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="tbl"><tr><td class="tbll"><img src="images/spacer.gif" alt="" width="8" height="4" /></td><td class="tblbot"><img src="images/spacer.gif" alt="" width="8" height="4" /></td><td class="tblr"><img src="images/spacer.gif" alt="" width="8" height="4" /></td></tr></table>
<br />


#
#-----[ OPEN ]---------------------------------
#
templates/fisubsilversh/admin/board_config_body.tpl

#
#-----[ FIND ]---------------------------------
#
<td class="row1">{L_ENABLE_PRUNE}</td>
<td class="row2">
<input type="radio" name="prune_enable" value="1" {PRUNE_YES} />
{L_YES}&nbsp;&nbsp;
<input type="radio" name="prune_enable" value="0" {PRUNE_NO} />
{L_NO}</td>
</tr>

#
#-----[ AFTER, ADD ]---------------------------------
#
<tr>
<td class="row1">{L_COUNTER_RELOAD_TIME}<br />
<span class="gensmall">{L_COUNTER_RELOAD_TIME_EXPLAIN}</span></td>
<td class="row2">
<input class="post" type="text" maxlength="5" size="5" name="counter_reload_time" value="{COUNTER_RELOAD_TIME}" /></td>
</tr>


#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM