<?php // vim: ts=4 sw=4 sts=4 fdm=marker ai noet:
// $Id: bcounter.inc.php,v 1.4 2004/12/08 23:37:30 ishii Exp $

// counter file
if (!defined('COUNTER_DIR'))
{
	define('COUNTER_DIR', './counter/');
}

if (!defined('COUNTER_EXT'))
{
	define('COUNTER_EXT','.count');
}

function plugin_bcounter_action()
{
	global $script, $adminpass;
	global $xoopsDB;










	return '';

}

function plugin_bcounter_inline()
{
	global $vars;
	
	$arg = '';
	if (func_num_args() > 0)
	{
		$args = func_get_args();
		$arg = strtolower($args[0]);
	}
	
	$counter = plugin_bcounter_get_count($vars['page']);
	
	switch ($arg)
	{
		case 'today':
		case 'yesterday':
			$count = $counter[$arg];
			break;
		default:
			$count = $counter['total'];
	}
	return $count;
}

function plugin_bcounter_convert()
{
	global $vars;
	
	$counter = plugin_bcounter_get_count($vars['page']);
	
	return <<<EOD
BCounter: {$counter['total']},
today: {$counter['today']},
yesterday: {$counter['yesterday']}
EOD;
}

function plugin_bcounter_get_count($page)
{
	global $vars;
	static $counters = array();
	static $default;
	global $xoopsDB;

	$table_name = $xoopsDB->prefix('bwiki_bcounter');

	// カウンタのデフォルト値
	if (!isset($default))
	{
		$default = array(
			'total'     => 0,
			'date'      => get_date('Y/m/d'), // PukiWiki/1.4
			'today'     => 0,
			'yesterday' => 0,
			'ip'        => ''
		);
	}

	if (!is_page($page))
	{
		return $default;
	}

	if (array_key_exists($page,$counters))
	{
		return $counters[$page];
	}

	// カウンタのデフォルト値をセット
	$counters[$page] = $default;

	// カウンタが存在する場合は読み込む
	$sql     = "select * from {$table_name} where page_name = '{$page}'";
	$result  = $xoopsDB->query($sql);
	$count   = $xoopsDB->fetchArray($result);
	$isExist = (int)$count['total'];

	if($isExist)
	{
		foreach ($default as $key=>$val)
		{
			$counters[$page][$key] = $count[$key];
		}

		// DBから取ってくるとハイフン区切りなのでスラッシュに直す。
		$counters[$page]['date'] = preg_replace('/-/', '/', $counters[$page]['date']);
	}

	// 更新が必要か?
	$modify = FALSE;

	// 日付が変わった
	if ($counters[$page]['date'] != $default['date'])
	{
		$modify = TRUE;
		$counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
		$counters[$page]['date']      = $default['date'];
		$counters[$page]['yesterday'] = $counters[$page]['today'];
		$counters[$page]['today']     = 1;
		$counters[$page]['total']++;
	}
	// IPアドレスが異なる
	else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR'])
	{
		$modify = TRUE;
		$counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
		$counters[$page]['today']++;
		$counters[$page]['total']++;
	}

	//ページ読み出し時のみ更新
	$is_read = ($vars['cmd'] == 'read');

	if ($modify and $is_read)
	{
		if($isExist)
		{
			$sql =<<<_SQL_
				update
					{$table_name}
				set
					total     = '{$counters[$page]['total']}',
					date      = '{$counters[$page]['date']}',
					today     = '{$counters[$page]['today']}',
					yesterday = '{$counters[$page]['yesterday']}',
					ip        = '{$counters[$page]['ip']}'
				where
					page_name = '{$page}'
_SQL_;
		}
		else
		{
			$sql =<<<_SQL_
				insert into
					{$table_name}
				set
					total     = '{$counters[$page]['total']}',
					date      = '{$counters[$page]['date']}',
					today     = '{$counters[$page]['today']}',
					yesterday = '{$counters[$page]['yesterday']}',
					ip        = '{$counters[$page]['ip']}',
					page_name = '{$page}'
_SQL_;
		}

		// クエリー実行
		// XXX 何故かinsertとupdateはqueryFじゃないと拒否される。
		// REQUEST_METHOD が POST でないとほんとはダメなのかな？
		$xoopsDB->queryF($sql);
	}

	return $counters[$page];
}
?>
