1 (edited by 2012-02-06 18:16:36)

Topic: [HACK] Counter

Name: Counter
Author: Eyece, liketobemad added a small modification
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: Keep statistics on your articles.
Discussion Topic:  here
Instructions:
first put this in a file named counter.php in the cutenews core folder:

<?php

if(!$cn_do)die;

$file_path = $cutepath.'/data/counter.txt';
$online_timeout = 120; // seconds
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();

// load current stats into array
$counter = array();
foreach(file($file_path) as $line_num => $line){
    $line = explode("|",$line);
    $counter[$line[0]] = array("views"=>0,"unique views"=>0,"online"=>0);
    if($line[1]!="")
        foreach(explode(" ",$line[1]) as $part_num => $part){
            $part = explode(":",$part);
            if($part[3]>=($time-$online_timeout))$counter[$line[0]]["online"] += 1;
            $counter[$line[0]]["views"] += $part[1];
            $counter[$line[0]][$part[0]] = array($part[1],$part[2],$part[3]);
            $counter[$line[0]]["unique views"] += 1;
        }
}

if($cn_do=="update"){
    // update or write this users stats
    if(is_array($counter[$id])){
        if(is_array($counter[$id][$ip])){
            $counter[$id][$ip][0] += 1;
            $counter[$id]["views"] += 1;
            $counter[$id][$ip][2] = $time;
        }
        else{
            $counter[$id][$ip] = array(1,$time,$time);
            $counter[$id]["unique views"] += 1;
        }
    }
    elseif(is_numeric($id))$counter[$id] = array("views"=>1,"unique views"=>1,"online"=>0,$ip=>array(1,$time,$time));
    
    // re-write counter stats
    $fp = fopen($file_path,"w");
    $write = $counter;
    foreach($write as $item_id => $item_logs){
        foreach($item_logs as $log_ip => $log_values)
            if(is_array($log_values)){
                $write[$item_id][$log_ip] = $log_ip.":".implode(":",$log_values);
            }
            else unset($write[$item_id][$log_ip]);
        $write[$item_id] = $item_id."|".implode(" ",$write[$item_id])."|";
    }
    fwrite($fp,implode("\n",$write));
    fclose($fp);
}

?>


then create a blank file named counter.txt in the data folder (chmod 777)

shows.inc.php
find:

$output = str_replace("{title}", $news_arr[2], $template_full);

add under:

    $cn_do = "update";
        include($cutepath.'/counter.php');
        $output = str_replace("{page-views}", $counter[$news_arr[0]]["views"], $output);
        $output = str_replace("{unique-views}", $counter[$news_arr[0]]["unique views"], $output);
        $output = str_replace("{your-views}", $counter[$news_arr[0]][$ip][0], $output);
        $output = str_replace("{online}", $counter[$news_arr[0]]["online"], $output);
        $output = str_replace("{first-view}", date($config_timestamp_active,$counter[$news_arr[0]][$ip][1]), $output);


find:

$output = $template_active;

add under:

    $cn_do = "get";
        include($cutepath.'/counter.php');
        $output = str_replace("{page-views}", $counter[$news_arr[0]]["views"], $output);
        $output = str_replace("{unique-views}", $counter[$news_arr[0]]["unique views"], $output);
        $output = str_replace("{your-views}", $counter[$news_arr[0]][$ip][0], $output);
        $output = str_replace("{online}", $counter[$news_arr[0]]["online"], $output);
        $output = str_replace("{first-view}", date($config_timestamp_active,$counter[$news_arr[0]][$ip][1]), $output);

you can now use the following in the full-story & short-story templates:
{page-views} - total page loads from all unique visitors to current news
{unique-views} - count of all ip's logged in log file that are listed under the current news id
{your-views} - times viewer has loaded the page/news
{online} - count of all viewers that have been active within the timeout limit
{first-view} - time viewer first viewed news