国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.4KB

  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. var ms_ie = false;
  57. var ua = window.navigator.userAgent;
  58. if ((ua.indexOf('MSIE ') > -1) || (ua.indexOf('Trident/') > -1)) {
  59. ms_ie = true;
  60. }
  61. if (ms_ie) {
  62. var xhr;
  63. if (window.XMLHttpRequest) {
  64. xhr = new XMLHttpRequest();
  65. } else if (window.ActiveXObject) { // IE
  66. try {
  67. xhr = new ActiveXObject('Msxml2.XMLHTTP');
  68. } catch (e) {
  69. try {
  70. xhr = new ActiveXObject('Microsoft.XMLHTTP');
  71. } catch (e) {}
  72. }
  73. }
  74. if (xhr) {
  75. xhr.open('GET', u, true);
  76. xhr.send(null);
  77. }
  78. } else {
  79. fetch(u);
  80. }
  81. })();
  82. EOT;
  83. }
  84. // 统计
  85. function Record()
  86. {
  87. global $dsql,$cfg_cookie_encode;
  88. // 进行统计
  89. $pm = array('dduuid','ssid','browser','device','device_type','os','t','created_date','created_hour','url_type','typeid','aid','value','sign');
  90. $pmvalue = array();
  91. foreach ($pm as $v) {
  92. $pmvalue[$v] = $_GET[$v];
  93. }
  94. ksort($pmvalue);
  95. $sign = $pmvalue['sign'];
  96. unset($pmvalue['sign']);
  97. if (time() - $pmvalue['t'] > 5) {
  98. die("DedeBIZ:time out");
  99. }
  100. $cs = sha1(http_build_query($pmvalue).md5($cfg_cookie_encode));
  101. if ($sign !== $cs) {
  102. die("DedeBIZ:check sign failed");
  103. }
  104. $pmvalue['ip'] = GetIP();
  105. $kstr = $vstr = array();
  106. foreach ($pmvalue as $key => $value) {
  107. $kstr[] = "`{$key}`";
  108. $vstr[] = "'".addslashes($value)."'";
  109. }
  110. $insql = "INSERT INTO `#@__statistics_detail`(".implode(",",$kstr).") VALUES (".implode(",",$vstr).")";
  111. return $dsql->ExecuteNoneQuery($insql);
  112. }
  113. // 生成uuid
  114. function _uniqidReal($lenght = 13) {
  115. if (function_exists("random_bytes")) {
  116. $bytes = random_bytes(ceil($lenght / 2));
  117. } elseif (function_exists("openssl_random_pseudo_bytes")) {
  118. $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
  119. } else {
  120. throw new Exception("no cryptographically secure random function available");
  121. }
  122. return substr(bin2hex($bytes), 0, $lenght);
  123. }
  124. // 获取某天的统计信息
  125. function GetInfoByDate($d=0)
  126. {
  127. global $dsql;
  128. if ($d == -1) {
  129. $pv = $dsql->GetOne("SELECT SUM(pv) as total FROM `#@__statistics`");
  130. $uv = $dsql->GetOne("SELECT SUM(uv) as total FROM `#@__statistics`");
  131. $ip = $dsql->GetOne("SELECT SUM(ip) as total FROM `#@__statistics`");
  132. $vv = $dsql->GetOne("SELECT SUM(vv) as total FROM `#@__statistics`");
  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. $today = MyDate("Ymd",time());
  142. if ($d==0) {
  143. $d = $today;
  144. }
  145. $d = intval($d);
  146. // 如果统计数据中存在,则直接查询统计表
  147. $info = $dsql->GetOne("SELECT * FROM `#@__statistics` WHERE sdate = $d");
  148. if (is_array($info)) {
  149. return $info;
  150. }
  151. $pv = $dsql->GetOne("SELECT COUNT(*) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  152. $uv = $dsql->GetOne("SELECT COUNT(DISTINCT dduuid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  153. $ip = $dsql->GetOne("SELECT COUNT(DISTINCT ip) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  154. $vv = $dsql->GetOne("SELECT COUNT(DISTINCT ssid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  155. if ($d < intval($today)) {
  156. // 缓存数据
  157. $insql = "INSERT INTO `#@__statistics`(`sdate`,`pv`,`uv`,`ip`,`vv`) VALUES ('$d', '{$pv['total']}','{$uv['total']}','{$ip['total']}','{$vv['total']}')";
  158. $dsql->ExecuteNoneQuery($insql);
  159. }
  160. return array(
  161. "sdate" => $d,
  162. "pv" => $pv['total'],
  163. "uv" => $uv['total'],
  164. "ip" => $ip['total'],
  165. "vv" => $vv['total'],
  166. );
  167. }
  168. }