<?php

/*  Alternate WebKit
(C) 2014 by Alternate Tools
-----------------------------------------------------

    filename: class_count.php
*/

if (!defined ("CLASS_COUNTER"))
{
    define ("CLASS_COUNTER", "defined");

    class class_counter
    {
        var $current_count_m;
        var $current_count_file_m;
        var $current_log_file_m;
        var $current_ip_file_m;
        var $current_archive_folder_m;
        var $log_enabled_m;
        var $count_enabled_m;
        var $max_log_size_m;
        var $line_feed_m;

        function class_counter ()  // PHP 4
        {
            $this->init_counter();
        }

        function __construct()  // PHP 5
        {
            $this->init_counter();
        }

        function init_counter()
        {
            $this->current_count_m = 0;
            $this->current_count_file_m = "counter.txt";
            $this->current_log_file_m = "counter_log.txt";
            $this->current_ip_file_m = "counter_ipaddr.txt";
            $this->current_archive_folder_m = "";
            $this->log_enabled_m = true;
            $this->count_enabled_m = true;
	        $this->max_log_size_m = -1;   // No max logsize!
	        $this->line_feed_m = "\r\n";
        }

        function set_files($count_file_i, $log_file_i, $ip_file_i, $archive_folder_i = "")
        {
            $this->current_count_file_m = $count_file_i;
            $this->current_log_file_m = $log_file_i;
            $this->current_ip_file_m = $ip_file_i;
            $this->current_archive_folder_m = $archive_folder_i;
        }

        function set_count_log($count_i, $log_i)
        {
            $this->count_enabled_m = $count_i;
            $this->log_enabled_m = $log_i;
        }

        function set_max_logsize($size_i)
        {
            $this->max_log_size_m = $size_i;
        }

        function increase($log_message_i)
        {
            $ip_found = false;
            $referer = "";
            if (isset($_SERVER["HTTP_REFERER"]) == true)
                $referer = $_SERVER["HTTP_REFERER"];
            $remote_ip = $_SERVER["REMOTE_ADDR"];
            $browser = $_SERVER["HTTP_USER_AGENT"];
            $language = $_SERVER["HTTP_ACCEPT_LANGUAGE"];

            if ($this->current_ip_file_m != "") // Use IP addresses
            {
                if (!file_exists($this->current_ip_file_m))
                {
                    $ip_addresses = $remote_ip . $this->line_feed_m;
                    $fp = fopen($this->current_ip_file_m, "w");
                    fwrite($fp, $ip_addresses);
                    fclose($fp);
	            }
	            else
	            {
	                $fp = fopen($this->current_ip_file_m, "r");
	                if (filesize($this->current_ip_file_m) > 0)
	                    $ip_addresses = fread($fp, filesize($this->current_ip_file_m));
                    else
                        $ip_addresses = "";
                    fclose ($fp);
	                if (strstr($ip_addresses, $remote_ip) != NULL)
	                    $ip_found = true;
	                else
	                {
	                    $ip_addresses = $ip_addresses . $remote_ip . $this->line_feed_m;
	                    $fp = fopen($this->current_ip_file_m, "w");
	                    fwrite($fp, $ip_addresses);
	                    fclose($fp);
	                }
	            }
            }

            $this->get_counter();
            if ($this->count_enabled_m == true && $ip_found == false)
            {
                $this->current_count_m ++;
                $fp = fopen($this->current_count_file_m, "w");
                fwrite($fp, $this->current_count_m);
                fclose($fp);
            }

            if ($this->log_enabled_m == true)
            {
                if ($referer == "")
                    $referer = "not accessable";
                if ($remote_ip == "")
                    $remote_ip = "not accessable";
                if ($browser == "")
                    $browser = "not accessable";
                if ($language == "")
                    $language = "not accessable";
                $fp = fopen($this->current_log_file_m, "a+");
                // Check max logfile size...
                if ($this->max_log_size_m != -1 && filesize($this->current_log_file_m) > $this->max_log_size_m)
                {
                    fclose($fp);
                    // Archive it?
                    if ($this->current_archive_folder_m != "")
                    {
                        copy($this->current_log_file_m, $this->current_archive_folder_m . date("Y-m-d_") . basename($this->current_log_file_m));
                    }
                    $fp = fopen($this->current_log_file_m, "w");
                }
                $user_string = strftime("%H:%M:%S %d.%m.%Y") . "|" .
                $remote_ip . "|" .
                $referer . "|" .
                $browser . "|" .
                $language . "|" .
                $log_message_i . $this->line_feed_m;
                fwrite($fp, $user_string);
                fclose($fp);
            }
        }

        function get_counter()
        {
            if (file_exists($this->current_count_file_m) == false)
            {
                return 0;
            }
            $this->current_count_m = 0;
            $fp = fopen($this->current_count_file_m, "r");
            if (filesize($this->current_count_file_m) > 0)
                $this->current_count_m = fread($fp, filesize($this->current_count_file_m));
            fclose ($fp);
            return $this->current_count_m;
        }

        function get_log()
        {
            $fp = fopen($this->current_log_file_m, "r");
            if ($fp != 0)
                $log = fread($fp, filesize($this->current_log_file_m));
            fclose($fp);
            $output = "<table><tr>
                      <td>Time</td>
                      <td>IP</td>
		                 <td>Referer</td>
                      <td>Event</td><tr><tr><td>";
            for ($i = 0; $i < filesize($this->current_log_file_m); $i ++)
            {
                if ($log[$i] == "|")
                {
                    $output = $output . "</td><td>";
                    continue;
                }
                if ($log[$i] == "\r")
                {
                    $output = $output . "</tr><tr><td>";
                    $i ++;
                }
                $output = $output . $log[$i];
            }
            $output = $output . "</table>";

            return $output;
        }

    }  //End of class
}  // End of define
?>