PageRenderTime 2ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/engine/statistic.php

https://github.com/foxadmin/ReloadCMS
PHP | 116 lines | 89 code | 14 blank | 13 comment | 27 complexity | 2ef98453eeb677f3a37eeb99da7680cf MD5 | raw file
  1<?php
  2////////////////////////////////////////////////////////////////////////////////
  3//   Copyright (C) ReloadCMS Development Team                                 //
  4//   http://reloadcms.com                                                     //
  5//   This product released under GNU General Public License v2                //
  6////////////////////////////////////////////////////////////////////////////////
  7function statistic_register(){
  8    global $system;
  9    if(is_file(DATA_PATH . 'stats.dat')) {
 10        $stats = @file(DATA_PATH . 'stats.dat');
 11        $stats = @unserialize($stats[0]);
 12    }
 13    $userip    = ($_SERVER['REMOTE_ADDR']);
 14    $referer   = ($_SERVER['HTTP_REFERER']);
 15    $page      = ($_SERVER['REQUEST_URI']);
 16    
 17    // Add popularity to referer
 18    if(!empty($referer) && $referer = parse_url($referer)) {
 19        if(!empty($stats['ref'][$referer['host']])) {
 20            $stats['ref'][$referer['host']]++;
 21        } else {
 22            $stats['ref'][$referer['host']] = 1;
 23        }
 24    }
 25    
 26    //Add popularity to page
 27     if(!empty($stats['popular'][$page])) {
 28         $stats['popular'][$page]++;
 29     } else {
 30         $stats['popular'][$page] = 1;
 31     }
 32    
 33    // Register last user's visit time
 34    $stats['ips'][$userip] = time();
 35    
 36    // Register user in total hits count
 37    if(!empty($stats['totalhits'])) {
 38        $stats['totalhits']++;
 39    } else {
 40        $stats['totalhits'] = 1;
 41    }
 42    
 43    // Check the last update time
 44    if(!empty($stats['update']) && $stats['update'] < mktime(0, 0, 0, date('n'), date('j'), date('Y'))) {
 45     if (!empty($stats['ref']))  { 
 46		rcms_log_put(__('Today referers'), 'Last', $stats['ref']); // Save yestarday's referers
 47        unset($stats['ref']);  // Remove yestarday's referers
 48		}         
 49     unset($stats['todayhits']);     // Remove yestarday's hits
 50     unset($stats['todayhosts']);    // Remove yestarday's hosts
 51    }
 52    
 53    if(!empty($stats['todayhits'])) {
 54        $stats['todayhits']++;
 55    } else {
 56        $stats['todayhits'] = 1;
 57    }
 58    
 59    // Today hosts counter
 60    if(!empty($stats['todayhosts'][$userip])) {
 61        $stats['todayhosts'][$userip]++;
 62    } else $stats['todayhosts'][$userip] = 1;
 63    
 64    // Online users
 65    $stats['online'][$userip]['name'] = $system->user['username'];
 66    $stats['online'][$userip]['nick'] = $system->user['nickname'];
 67    $stats['online'][$userip]['time'] = rcms_get_time();
 68    $online = array();
 69    $registered_already = array();
 70    if(!empty($stats['online'])){
 71        foreach ($stats['online'] as $ip => $data) {
 72            if($data['time'] > rcms_get_time() - 5 * 60 && !in_array($data['name'], $registered_already)) {
 73                $online[$ip] = $data;
 74                $registered_already[] = $data['name'];
 75            }
 76        }
 77    }
 78    $stats['online'] = $online;
 79    
 80    // Update time's update
 81    $stats['update'] = rcms_get_time();
 82    
 83    @file_write_contents(DATA_PATH . 'stats.dat', serialize($stats));
 84    return true;    
 85}
 86
 87function statistic_get(){
 88    if(is_file(DATA_PATH . 'stats.dat')) {
 89        $stats = @file(DATA_PATH . 'stats.dat');
 90        $stats = @unserialize($stats[0]);
 91        if(!empty($stats)) {
 92            striptags_array($stats);
 93        }
 94        return $stats;
 95    } else {
 96        return false;
 97    }
 98}
 99
100function striptags_array(&$array){
101    foreach ($array as $key => $value) {
102        if(is_array($array[$key])) {
103            striptags_array($array[$key]);
104        } else {
105            $array[$key] = strip_tags($value);
106        }
107    }
108    return true;
109}
110
111function statistic_clean(){
112    return rcms_delete_files(DATA_PATH . 'stats.dat');
113}
114                                  
115if(basename($_SERVER['SCRIPT_FILENAME']) == 'index.php' && !empty($system->config['disable_stats'])) statistic_register();
116?>