国内流行的内容管理系统(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.

180 lines
6.7KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. require_once(DEDEINC."/libraries/agent.class.php");
  4. /**
  5. * 轻量级流量统计
  6. *
  7. * @version $id:statistics.class.php 11:42 2022年03月26日 tianya $
  8. * @package DedeBIZ.Libraries
  9. * @copyright Copyright (c) 2022 DedeBIZ.COM
  10. * @license https://www.dedebiz.com/license
  11. * @link https://www.dedebiz.com
  12. */
  13. class DedeStatistics {
  14. function __construct()
  15. {
  16. }
  17. //获取统计js
  18. function GetStat()
  19. {
  20. global $envs,$cfg_cookie_encode;
  21. $agent = new Agent();
  22. //不记录爬虫
  23. if ($agent->isRobot()) {
  24. return "";
  25. }
  26. $pm = array();
  27. $pm['dduuid'] = GetCookie("DedeStUUID");
  28. if (empty($pm['dduuid'])) {
  29. $pm['dduuid'] = $this->_uniqidReal();
  30. PutCookie('DedeStUUID', $pm['dduuid'], 60 * 24 * 365);
  31. }
  32. $pm['ssid'] = session_id();
  33. if (empty($pm['ssid'])) {
  34. session_start();
  35. $pm['ssid'] = session_id();
  36. }
  37. $url_type = isset($_GET['url_type'])? $_GET['url_type'] : 0;
  38. $typeid = isset($_GET['typeid'])? $_GET['typeid'] : 0;
  39. $aid = isset($_GET['aid'])? $_GET['aid'] : 0;
  40. $value = isset($_GET['value'])? $_GET['value'] : '';
  41. $pm['browser'] = $agent->browser();
  42. $pm['device'] = $agent->device();
  43. $pm['device_type'] = $agent->deviceType();
  44. $pm['os'] = $agent->platform();
  45. $pm['t'] = time();
  46. $pm['created_date'] = MyDate("Ymd",$pm['t']);
  47. $pm['created_hour'] = MyDate("H",$pm['t']);
  48. $pm['url_type'] = isset($envs['url_type'])? $envs['url_type'] : $url_type;
  49. $pm['typeid'] = isset($envs['typeid'])? $envs['typeid'] : $typeid;
  50. $pm['aid'] = isset($envs['aid'])? $envs['aid'] : $aid;
  51. $pm['value'] = isset($envs['value'])? $envs['value'] : $value;
  52. ksort($pm);
  53. $pm['sign'] = sha1(http_build_query($pm).md5($cfg_cookie_encode));
  54. $pm['dopost'] = "stat";
  55. $url = $GLOBALS['cfg_cmspath'].'/apps/statistics.php?'.http_build_query($pm);
  56. return "
  57. (function() {
  58. let u = '{$url}';
  59. var ms_ie = false;
  60. var ua = window.navigator.userAgent;
  61. if ((ua.indexOf('MSIE ') > -1) || (ua.indexOf('Trident/') > -1)) {
  62. ms_ie = true;
  63. }
  64. if (ms_ie) {
  65. var xhr;
  66. if (window.XMLHttpRequest) {
  67. xhr = new XMLHttpRequest();
  68. } else if (window.ActiveXObject) { //IE
  69. try {
  70. xhr = new ActiveXObject('Msxml2.XMLHTTP');
  71. } catch (e) {
  72. try {
  73. xhr = new ActiveXObject('Microsoft.XMLHTTP');
  74. } catch (e) {}
  75. }
  76. }
  77. if (xhr) {
  78. xhr.open('GET', u, true);
  79. xhr.send(null);
  80. }
  81. } else {
  82. fetch(u);
  83. }
  84. })();";
  85. }
  86. //统计
  87. function Record()
  88. {
  89. global $dsql,$cfg_cookie_encode;
  90. //进行统计
  91. $pm = array('dduuid','ssid','browser','device','device_type','os','t','created_date','created_hour','url_type','typeid','aid','value','sign');
  92. $pmvalue = array();
  93. foreach ($pm as $v) {
  94. $pmvalue[$v] = $_GET[$v];
  95. }
  96. ksort($pmvalue);
  97. $sign = $pmvalue['sign'];
  98. unset($pmvalue['sign']);
  99. if (time() - $pmvalue['t'] > 5) {
  100. die("DedeBIZ:time out");
  101. }
  102. $cs = sha1(http_build_query($pmvalue).md5($cfg_cookie_encode));
  103. if ($sign !== $cs) {
  104. die("DedeBIZ:check sign failed");
  105. }
  106. $pmvalue['ip'] = GetIP();
  107. $kstr = $vstr = array();
  108. foreach ($pmvalue as $key => $value) {
  109. $kstr[] = "`{$key}`";
  110. $vstr[] = "'".addslashes($value)."'";
  111. }
  112. $insql = "INSERT INTO `#@__statistics_detail`(".implode(",",$kstr).") VALUES (".implode(",",$vstr).")";
  113. return $dsql->ExecuteNoneQuery($insql);
  114. }
  115. //生成uuid
  116. function _uniqidReal($lenght = 13) {
  117. if (function_exists("random_bytes")) {
  118. $bytes = random_bytes(ceil($lenght / 2));
  119. } elseif (function_exists("openssl_random_pseudo_bytes")) {
  120. $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
  121. } else {
  122. throw new Exception("no cryptographically secure random function available");
  123. }
  124. return substr(bin2hex($bytes), 0, $lenght);
  125. }
  126. function GetInfoByDateMulti($ds = array())
  127. {
  128. $results = array();
  129. foreach ($ds as $d) {
  130. $vv = $this->GetInfoByDate($d);
  131. $result[] = $vv;
  132. }
  133. return $result;
  134. }
  135. //获取某天的统计信息
  136. function GetInfoByDate($d=0)
  137. {
  138. global $dsql;
  139. if ($d == -1) {
  140. $pv = $dsql->GetOne("SELECT SUM(pv) as total FROM `#@__statistics`");
  141. $uv = $dsql->GetOne("SELECT SUM(uv) as total FROM `#@__statistics`");
  142. $ip = $dsql->GetOne("SELECT SUM(ip) as total FROM `#@__statistics`");
  143. $vv = $dsql->GetOne("SELECT SUM(vv) as total FROM `#@__statistics`");
  144. return array(
  145. "sdate" => $d,
  146. "pv" => $pv['total'],
  147. "uv" => $uv['total'],
  148. "ip" => $ip['total'],
  149. "vv" => $vv['total'],
  150. );
  151. }
  152. $today = MyDate("Ymd",time());
  153. if ($d==0) {
  154. $d = $today;
  155. }
  156. $d = intval($d);
  157. //如果统计数据中存在,则直接查询统计表
  158. $info = $dsql->GetOne("SELECT * FROM `#@__statistics` WHERE sdate = $d");
  159. if (is_array($info)) {
  160. return $info;
  161. }
  162. $pv = $dsql->GetOne("SELECT COUNT(*) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  163. $uv = $dsql->GetOne("SELECT COUNT(DISTINCT dduuid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  164. $ip = $dsql->GetOne("SELECT COUNT(DISTINCT ip) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  165. $vv = $dsql->GetOne("SELECT COUNT(DISTINCT ssid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  166. if ($d < intval($today)) {
  167. //缓存数据
  168. $insql = "INSERT INTO `#@__statistics` (`sdate`,`pv`,`uv`,`ip`,`vv`) VALUES ('$d', '{$pv['total']}','{$uv['total']}','{$ip['total']}','{$vv['total']}')";
  169. $dsql->ExecuteNoneQuery($insql);
  170. }
  171. return array(
  172. "sdate" => $d,
  173. "pv" => $pv['total'],
  174. "uv" => $uv['total'],
  175. "ip" => $ip['total'],
  176. "vv" => $vv['total'],
  177. );
  178. }
  179. }
  180. ?>