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

181 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 <<<EOT
  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. EOT;
  86. }
  87. //统计
  88. function Record()
  89. {
  90. global $dsql,$cfg_cookie_encode;
  91. //进行统计
  92. $pm = array('dduuid','ssid','browser','device','device_type','os','t','created_date','created_hour','url_type','typeid','aid','value','sign');
  93. $pmvalue = array();
  94. foreach ($pm as $v) {
  95. $pmvalue[$v] = $_GET[$v];
  96. }
  97. ksort($pmvalue);
  98. $sign = $pmvalue['sign'];
  99. unset($pmvalue['sign']);
  100. if (time() - $pmvalue['t'] > 5) {
  101. die("DedeBIZ:time out");
  102. }
  103. $cs = sha1(http_build_query($pmvalue).md5($cfg_cookie_encode));
  104. if ($sign !== $cs) {
  105. die("DedeBIZ:check sign failed");
  106. }
  107. $pmvalue['ip'] = GetIP();
  108. $kstr = $vstr = array();
  109. foreach ($pmvalue as $key => $value) {
  110. $kstr[] = "`{$key}`";
  111. $vstr[] = "'".addslashes($value)."'";
  112. }
  113. $insql = "INSERT INTO `#@__statistics_detail`(".implode(",",$kstr).") VALUES (".implode(",",$vstr).")";
  114. return $dsql->ExecuteNoneQuery($insql);
  115. }
  116. //生成uuid
  117. function _uniqidReal($lenght = 13) {
  118. if (function_exists("random_bytes")) {
  119. $bytes = random_bytes(ceil($lenght / 2));
  120. } elseif (function_exists("openssl_random_pseudo_bytes")) {
  121. $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
  122. } else {
  123. throw new Exception("no cryptographically secure random function available");
  124. }
  125. return substr(bin2hex($bytes), 0, $lenght);
  126. }
  127. function GetInfoByDateMulti($ds = array())
  128. {
  129. $results = array();
  130. foreach ($ds as $d) {
  131. $vv = $this->GetInfoByDate($d);
  132. $result[] = $vv;
  133. }
  134. return $result;
  135. }
  136. //获取某天的统计信息
  137. function GetInfoByDate($d=0)
  138. {
  139. global $dsql;
  140. if ($d == -1) {
  141. $pv = $dsql->GetOne("SELECT SUM(pv) as total FROM `#@__statistics`");
  142. $uv = $dsql->GetOne("SELECT SUM(uv) as total FROM `#@__statistics`");
  143. $ip = $dsql->GetOne("SELECT SUM(ip) as total FROM `#@__statistics`");
  144. $vv = $dsql->GetOne("SELECT SUM(vv) as total FROM `#@__statistics`");
  145. return array(
  146. "sdate" => $d,
  147. "pv" => $pv['total'],
  148. "uv" => $uv['total'],
  149. "ip" => $ip['total'],
  150. "vv" => $vv['total'],
  151. );
  152. }
  153. $today = MyDate("Ymd",time());
  154. if ($d==0) {
  155. $d = $today;
  156. }
  157. $d = intval($d);
  158. //如果统计数据中存在,则直接查询统计表
  159. $info = $dsql->GetOne("SELECT * FROM `#@__statistics` WHERE sdate = $d");
  160. if (is_array($info)) {
  161. return $info;
  162. }
  163. $pv = $dsql->GetOne("SELECT COUNT(*) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  164. $uv = $dsql->GetOne("SELECT COUNT(DISTINCT dduuid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  165. $ip = $dsql->GetOne("SELECT COUNT(DISTINCT ip) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  166. $vv = $dsql->GetOne("SELECT COUNT(DISTINCT ssid) as total FROM `#@__statistics_detail` WHERE created_date = $d");
  167. if ($d < intval($today)) {
  168. //缓存数据
  169. $insql = "INSERT INTO `#@__statistics` (`sdate`,`pv`,`uv`,`ip`,`vv`) VALUES ('$d', '{$pv['total']}','{$uv['total']}','{$ip['total']}','{$vv['total']}')";
  170. $dsql->ExecuteNoneQuery($insql);
  171. }
  172. return array(
  173. "sdate" => $d,
  174. "pv" => $pv['total'],
  175. "uv" => $uv['total'],
  176. "ip" => $ip['total'],
  177. "vv" => $vv['total'],
  178. );
  179. }
  180. }
  181. ?>