<?php

/**
 * Created by PhpStorm.
 * User: MainUser
 * Date: 08.07.2016
 * Time: 13:42
 */
class VisitorStatistics
{

    private static function getRealIpAddr() {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))        // Определяем IP
        { $ip=$_SERVER['HTTP_CLIENT_IP']; }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
        { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; }
        else { $ip=$_SERVER['REMOTE_ADDR']; }
        return $ip;
    }
    public function Run()
    {
        $file = "Logs/visitors.log";    // файл для записи истории посещения сайта
        $col_zap = 4999;    // ограничиваем количество строк log-файла

        if (strstr($_SERVER['HTTP_USER_AGENT'], 'YandexBot')) {
            $bot = 'YandexBot';
        } //Выявляем поисковых ботов
        elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'Googlebot')) {
            $bot = 'Googlebot';
        } else {
            $bot = $_SERVER['HTTP_USER_AGENT'];
        }

        $ip = $this->getRealIpAddr();
        $date = date("H:i:s d.m.Y");        // определяем дату и время события
        $home = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];    // определяем страницу сайта
        $lines = file($file);
        while (count($lines) > $col_zap) array_shift($lines);
        $lines[] = $date . "|" . $bot . "|" . $ip . "|" . $home . "|\r\n";
        file_put_contents($file, $lines);
    }
    public static function GetStatistic(){
        $file = "Logs/visitors.log";    // файл для записи истории посещения сайта
        $file = file($file);

        $Statistic = array();
        foreach ($file as $line){
            if($line != '' && $line != ' '){
            $temp = explode("|", $line);
            $Statistic[] = array(
                'date' => new DateTime(strtotime('@'.$temp[0])),// new DateTime($temp[0]),
                'bot' => $temp[1],
                'ip' => $temp[2],
                'home' => $temp[3]
            );

	        }
        }

        $ResultArr['Monday']['Views']       = 0;
        $ResultArr['Tuesday']['Views']      = 0;
        $ResultArr['Wednesday']['Views']    = 0;
        $ResultArr['Thursday']['Views']     = 0;
        $ResultArr['Friday']['Views']       = 0;
        $ResultArr['Saturday']['Views']     = 0;
        $ResultArr['Sunday']['Views']       = 0;

        $MondayIps = array();
        $TuesdayIps = array();
        $WednesdayIps = array();
        $ThursdayIps = array();
        $FridayIps = array();
        $SaturdayIps = array();
        $SundayIps = array();

        foreach ($Statistic as $stats) {
            switch ($stats['date']->format('w')) {
                case 1: {
                    $ResultArr['Monday']['Views']++;
                    if (!in_array($stats['ip'], $MondayIps))
                        $MondayIps[] = $stats['ip'];
                }
                    break;
                case 2: {
                    $ResultArr['Tuesday']['Views']++;
                    if (!in_array($stats['ip'], $TuesdayIps))
                        $TuesdayIps[] = $stats['ip'];
                }
                    break;
                case 3: {
                    $ResultArr['Wednesday']['Views']++;
                    if (!in_array($stats['ip'], $WednesdayIps))
                        $WednesdayIps[] = $stats['ip'];
                }
                    break;
                case 4: {
                    $ResultArr['Thursday']['Views']++;
                    if (!in_array($stats['ip'], $ThursdayIps))
                        $ThursdayIps[] = $stats['ip'];
                }
                    break;
                case 5: {
                    $ResultArr['Friday']['Views']++;
                    if (!in_array($stats['ip'], $FridayIps))
                        $FridayIps[] = $stats['ip'];
                }
                    break;
                case 6: {
                    $ResultArr['Saturday']['Views']++;
                    if (!in_array($stats['ip'], $SaturdayIps))
                        $SaturdayIps[] = $stats['ip'];
                }
                    break;
                case 7: {
                    $ResultArr['Sunday']['Views']++;
                    if (!in_array($stats['ip'], $SundayIps))
                        $SundayIps[] = $stats['ip'];
                }
                    break;
            }
        }

        $ResultArr['Monday']['Visitors']    = count($MondayIps);
        $ResultArr['Tuesday']['Visitors']   = count($TuesdayIps);
        $ResultArr['Wednesday']['Visitors'] = count($WednesdayIps);
        $ResultArr['Thursday']['Visitors']  = count($ThursdayIps);
        $ResultArr['Friday']['Visitors']    = count($FridayIps);
        $ResultArr['Saturday']['Visitors']  = count($SaturdayIps);
        $ResultArr['Sunday']['Visitors']    = count($SundayIps);
        return $ResultArr;
    }

}