<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * Counter
 *
 * @access      public
 * @package     yana
 * @subpackage  utilities
 */
class Counter
{
    /**#@+
     * @access private
     * @ignore
     */

    var $directory;
    var $file;
    var $ip_file;
    var $use_ip;

    /**#@-*/

    /**
     * constructor
     *
     * @param string $filename (optional)
     * @param int $use_ip (optional) default = YANA_COUNTER_USE_IP
     */
    function Counter($filename = "", $use_ip = YANA_COUNTER_USE_IP)
    {
        settype($filename, "string");
        global $YANA;

        $filename = strtolower(preg_replace("/[^\w\d_]/", "", $filename));
        if (empty($filename)) {
            /* default value */
            $filename = "statistik";
        }

        if (isset($YANA)) {
            $this->directory = $YANA->getVar("COUNTERDIR");
        } else {
            $this->directory = 'config/counter/';
        }
        $this->file    = new smlfile($this->directory.$filename.".cnt");
        $this->ip_file = new smlfile($this->directory.$filename.".ip");
        $this->use_ip  = ($use_ip === YANA_COUNTER_USE_IP);

        if ($this->use_ip && $this->ip_file->exists() && filemtime($this->ip_file->getFilename()) + 10000 < time()) {
            /* flush old data */
            $this->ip_file->read();       /* read */
            $this->ip_file->set(array()); /* flush */
            $this->ip_file->write();      /* write empty file */
        }
    }

    /**
     * get
     *
     * @access public
     * @param string $id (optional)
     * @return array
     */
    function get($id = "*")
    {
        settype($id, "string");
        if (!preg_match("/^([\w\d_]+|\*)$/i", $id)) {
            /* syntax error */
            return false;
        }

        if (empty($id) || $id == "*") {
            /* return all */
            return $this->file->get();
        } else {
            /* return single entry */
            return $this->file->get($id);
        }
    }

    /**
     * getInfo
     *
     * @access public
     * @param string $id (optional)
     * @return mixed
     */
    function getInfo($id = "*")
    {
        settype($id, "string");
        if (!preg_match("/^([\w\d_]+|\*)$/i", $id)) { 
            /* syntax error */
            return false;
        }

        if (empty($id) || $id == "*") {
            /* return all */
            $A = $this->file->get();
            $B = array();
            foreach ( $A as $a => $b )
            {
                if(!empty($b['INFO'])) $B[$a] = $b['INFO'];
            }
            sort($B, SORT_STRING);
            return $B;
        } else {
            /* return single entry */
            return $this->file->get("$id.INFO");
        }
    }

    /**
     * getCount
     *
     * @access public
     * @param string $id (optional)
     * @return mixed
     */
    function getCount($id = "*")
    {
        settype($id, "string");
        if (!preg_match("/^([\w\d_]+|\*)$/i", $id)) return false;

        if (empty($id) || $id == "*") {
            /* return all */
            $A = $this->file->get();
            $B = array();
            foreach ( $A as $a => $b )
            {
                $B[$a] = $b['SORT_NUMERIC'];
            }
            sort($B, SORT_STRING);
            return $B;
        } else {
            /* return single entry */
            return $this->file->get("$id.COUNT");
        }
    }

    /**
     * count
     *
     * @access public
     * @param string $id
     * @param string $info (optional)
     * @param int $ammount (optional) default = 1
     * @return int
     */
    function count($id, $info = "", $ammount = 1)
    {
        settype($id, "string");
        settype($info, "string");
        settype($ammount, "integer");

        if (!preg_match("/^[\w\d_]+$/i", $id)) {
            /* syntax error */
            return false;
        }

        /* BEGIN count */
            if (!$this->file->exists()) {
                /* create on first access */
                $this->file->create();
            }
            $this->file->read();
            $NEW_COUNTER                = array();
            $NEW_COUNTER[$id]           = $this->file->get($id);
            settype($NEW_COUNTER[$id]['COUNT'], "integer");
            $NEW_COUNTER[$id]['COUNT'] += $ammount;
            if (!empty($info)) {
                /* add info when avialable */
                $NEW_COUNTER[$id]['INFO'] = $info;
            }
        /* END count */

        if ($this->use_ip) {
            /* ip */
            if (!$this->ip_file->exists()) {
                /* create on first access */
                $this->ip_file->create();
            }
            $this->ip_file->read();
            $ip               = preg_replace("/[^\w\d_]/", "_", $_SERVER['REMOTE_ADDR']);
            $last_visit       = (int) $this->ip_file->get("$id.$ip");
            $NEW_IP           = array();
            $NEW_IP[$id][$ip] = time() + 10000;
        }

        if (!$this->use_ip || $last_visit < time()) {
            /* if ip is ignored or no recent entry for this ip found */
            $this->file->insert($NEW_COUNTER);

            if ($this->file->write()) {
                /* write changes to file */
                if ($this->use_ip) {
                    /* dont write ip to file if ip is ignored */
                    $this->ip_file->insert($NEW_IP);
                    $this->ip_file->write();
                }
                return $NEW_COUNTER[$id]['COUNT'];
            }
        }
        
        return $this->getCount($id);

    }

}

?>