<?php
// +---------------------------------------------+
// |     Copyright 2003 - 2005 weenCompany       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


if(!defined('IN_WEENCOMPANY'))
  die('File not found!');


function DisplayDefault()
{
  global $hits, $start, $style, $digits, $counttime, $message;
  global $DB, $refreshpage;

  $resultconf = $DB->query("SELECT hits, start, style, digits, cookietimeout, message FROM " . TABLE_PREFIX . "m18_hitcounter");
  list($hits,$start,$style,$digits,$counttime,$message) = mysql_fetch_row($resultconf);


  PrintSection('重置計數器');
   echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr><td class="tdrow2" width="70%">計數器清零:</td>
            <td class="tdrow3" style="padding-left: 40px;">
            <form method="post" action="'.$refreshpage.'">
            <input type="hidden" name="action" value="rCounter" />
            <input type="submit" value=" 清零 " />
            </form>
            </td></tr>
        </table>';
  EndSection();

  PrintSection('模塊設置');
   echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr><td class="tdrow2" width="70%">網站點擊總數:</td>
        <td class="tdrow3" style="padding-left: 40px;">'.$hits.'</td></tr>
        <form method="post" action="'.$refreshpage.'">
        <tr><td class="tdrow2" width="70%">起始點擊數:</td>
        <td class="tdrow3" style="padding-left: 40px;">
        <input type="text" name="start" value="'.$start.'" maxlength="6" size="10"></td></tr>
        <tr><td class="tdrow2" width="70%">計數器位數:</td>
        <td class="tdrow3" style="padding-left: 40px;">
        <input type="text" name="digits" value="'.$digits.'" maxlength="6" size="10"></td></tr>
		<tr><td class="tdrow2" width="70%">過期時間(秒):<br/>注: 同一IP地址重新計數的時間間隔, 設置為\'0\'表示每次點擊均計數.</td>
        <td class="tdrow3" style="padding-left: 40px;">
        <input type="text" name="counttime" value="'.$counttime.'" maxlength="6" size="10"></td></tr>
        <tr><td class="tdrow2" width="70%">提示信息:</td>
        <td class="tdrow3" style="padding-left: 40px;">
        <input type="text" name="message" value="'.$message.'" size="30"></td></tr>
        <tr><td class="tdrow1" colspan="2">顯示樣式:</td></tr>
        <tr><td class="tdrow3" colspan="2">
		<table width="100%" cellpadding="5" border="0"><tr>';

        $d = dir("../modules/m18_hitcounter/styles");
        $column=0;
		while(false !== ($entry = $d->read())) {
   			if ($entry != '.' && $entry != '..') {
                if (is_dir("../modules/m18_hitcounter/styles/".$entry)) {
                   	$column++;
                   	if ($column==3) {
                   		echo '</tr><tr>';
                    	$column=1;
                   	}
                    echo '<td align="left" valign="bottom" width="50%"><input type="radio" name="style" value='.$entry;
                    if ($entry==$style) {
                    	echo ' CHECKED>';
                    } else {
                        echo '>';
                    }
                    for ($e = 0; $e <= 9; $e++) {
                        echo '<img src="../modules/m18_hitcounter/styles/'.$entry.'/'.$e.'.gif">';
                    }
                    echo '</td>';
                }
            }
		}
		$d->close();

   echo '</tr></table>
         </td></tr><tr>';
   echo '<td class="tdrow1" colspan="2" align="center">
        <input type="hidden" name="action" value="savesettings">
        <input type="submit" value=" 保存更新 "></form>
        </td></tr></table>';
   EndSection();
}

function rCounter()
{
	global $DB;
	$DB->query("UPDATE " . TABLE_PREFIX . "m18_hitcounter SET hits=0");
	displayMessage();
}

function savesettings($start,$style,$digits,$counttime,$message)
{
	global $DB;

	if(!is_numeric($start) OR !is_numeric($digits) OR !is_numeric($counttime) OR $start<0 OR $digits<0 OR $counttime<0)
	{
	   $errors[] = '請輸入正整數!';
	}
	if(!isset($errors))
    {
		$DB->query("UPDATE " . TABLE_PREFIX . "m18_hitcounter SET start=".$start.", digits=".$digits.", cookietimeout=".$counttime.", message='".$message."', style=".$style);
    	displayMessage();
	}
	else
	{
	    PrintErrors($errors);
		DisplayDefault();
	}
	
}

function displayMessage()
{
	global $refreshpage;
 	PrintRedirect($refreshpage,1);
}

switch($action)
{
  case 'savesettings':
    savesettings($start,$style,$digits,$counttime,$message);
  break;

  case 'rCounter':
  	rCounter();
  break;

  default:
    DisplayDefault();
}

?>
