国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

statistics.class.php 6.7KB

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