国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

150 行
5.5KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. require_once(DEDEINC."/libraries/agent.class.php");
  4. /**
  5. * 流量统计
  6. * 一个轻量级流量统计功能
  7. *
  8. * @version $Id: statistics.class.php 1 11:42 2022年03月26日Z tianya $
  9. * @package DedeBIZ.Libraries
  10. * @copyright Copyright (c) 2022, DedeBIZ.COM
  11. * @license https://www.dedebiz.com/license
  12. * @link https://www.dedebiz.com
  13. */
  14. class DedeStatistics {
  15. function __construct()
  16. {
  17. }
  18. // 获取统计JS
  19. function GetStat()
  20. {
  21. global $envs,$cfg_cookie_encode;
  22. $agent = new Agent();
  23. $pm = array();
  24. $pm['dduuid'] = GetCookie("DedeStUUID");
  25. if (empty($pm['dduuid'])) {
  26. $pm['dduuid'] = $this->_uniqidReal();
  27. PutCookie('DedeStUUID', $pm['dduuid'], 60 * 24 * 365);
  28. }
  29. $pm['ssid'] = session_id();
  30. if (empty($pm['ssid'])) {
  31. session_start();
  32. $pm['ssid'] = session_id();
  33. }
  34. $url_type = isset($_GET['url_type'])? $_GET['url_type'] : 0;
  35. $typeid = isset($_GET['typeid'])? $_GET['typeid'] : 0;
  36. $aid = isset($_GET['aid'])? $_GET['aid'] : 0;
  37. $value = isset($_GET['value'])? $_GET['value'] : '';
  38. $pm['browser'] = $agent->browser();
  39. $pm['device'] = $agent->device();
  40. $pm['device_type'] = $agent->deviceType();
  41. $pm['os'] = $agent->platform();
  42. $pm['t'] = time();
  43. $pm['created_date'] = MyDate("Ymd",$pm['t']);
  44. $pm['created_hour'] = MyDate("H",$pm['t']);
  45. $pm['url_type'] = isset($envs['url_type'])? $envs['url_type'] : $url_type;
  46. $pm['typeid'] = isset($envs['typeid'])? $envs['typeid'] : $typeid;
  47. $pm['aid'] = isset($envs['aid'])? $envs['aid'] : $aid;
  48. $pm['value'] = isset($envs['value'])? $envs['value'] : $value;
  49. ksort($pm);
  50. $pm['sign'] = sha1(http_build_query($pm).md5($cfg_cookie_encode));
  51. $pm['dopost'] = "stat";
  52. $url = $GLOBALS['cfg_cmspath'].'/apps/statistics.php?'.http_build_query($pm);
  53. return <<<EOT
  54. (function() {
  55. let u = '{$url}';
  56. fetch(u);
  57. })();
  58. EOT;
  59. }
  60. // 统计
  61. function Record()
  62. {
  63. global $dsql,$cfg_cookie_encode;
  64. // 进行统计
  65. $pm = array('dduuid','ssid','browser','device','device_type','os','t','created_date','created_hour','url_type','typeid','aid','value','sign');
  66. $pmvalue = array();
  67. foreach ($pm as $v) {
  68. $pmvalue[$v] = $_GET[$v];
  69. }
  70. ksort($pmvalue);
  71. $sign = $pmvalue['sign'];
  72. unset($pmvalue['sign']);
  73. $cs = sha1(http_build_query($pmvalue).md5($cfg_cookie_encode));
  74. if ($sign !== $cs) {
  75. die("DedeBIZ:check sign failed");
  76. }
  77. $pmvalue['ip'] = GetIP();
  78. $kstr = $vstr = array();
  79. foreach ($pmvalue as $key => $value) {
  80. $kstr[] = "`{$key}`";
  81. $vstr[] = "'".addslashes($value)."'";
  82. }
  83. $insql = "INSERT INTO `#@__statistics_detail`(".implode(",",$kstr).") VALUES (".implode(",",$vstr).")";
  84. return $dsql->ExecuteNoneQuery($insql);
  85. }
  86. // 生成uuid
  87. function _uniqidReal($lenght = 13) {
  88. if (function_exists("random_bytes")) {
  89. $bytes = random_bytes(ceil($lenght / 2));
  90. } elseif (function_exists("openssl_random_pseudo_bytes")) {
  91. $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
  92. } else {
  93. throw new Exception("no cryptographically secure random function available");
  94. }
  95. return substr(bin2hex($bytes), 0, $lenght);
  96. }
  97. // 获取某天的统计信息
  98. function GetInfoByDate($d=0)
  99. {
  100. global $dsql;
  101. if ($d == -1) {
  102. $pv = $dsql->GetOne("SELECT SUM(pv) as total FROM `#@__statistics`");
  103. $uv = $dsql->GetOne("SELECT SUM(uv) as total FROM `#@__statistics`");
  104. $ip = $dsql->GetOne("SELECT SUM(ip) as total FROM `#@__statistics`");
  105. $vv = $dsql->GetOne("SELECT SUM(vv) as total FROM `#@__statistics`");
  106. return array(
  107. "sdate" => $d,
  108. "pv" => $pv['total'],
  109. "uv" => $uv['total'],
  110. "ip" => $ip['total'],
  111. "vv" => $vv['total'],
  112. );
  113. }
  114. $today = MyDate("Ymd",time());
  115. if ($d==0) {
  116. $d = $today;
  117. }
  118. $d = intval($d);
  119. // 如果统计数据中存在,则直接查询统计表
  120. $info = $dsql->GetOne("SELECT * FROM `#@__statistics` WHERE sdate = $d");
  121. if (is_array($info)) {
  122. return $info;
  123. }
  124. $pv = $dsql->GetOne("SELECT COUNT(*) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  125. $uv = $dsql->GetOne("SELECT COUNT(DISTINCT dduuid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  126. $ip = $dsql->GetOne("SELECT COUNT(DISTINCT ip) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  127. $vv = $dsql->GetOne("SELECT COUNT(DISTINCT ssid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  128. if ($d < intval($today)) {
  129. $insql = "INSERT INTO `#@__statistics`(`sdate`,`pv`,`uv`,`ip`,`vv`) VALUES ('$d', '{$pv['total']}','{$uv['total']}','{$ip['total']}','{$vv['total']}')";
  130. // var_dump($insql);
  131. return $dsql->ExecuteNoneQuery($insql);
  132. }
  133. return array(
  134. "sdate" => $d,
  135. "pv" => $pv['total'],
  136. "uv" => $uv['total'],
  137. "ip" => $ip['total'],
  138. "vv" => $vv['total'],
  139. );
  140. }
  141. }