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