visit.mod.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function visit_update_today($type, $module_name = '') {
  8. global $_W;
  9. $module_name = trim($module_name);
  10. $type = trim($type);
  11. if (empty($type) || !in_array($type, array('app', 'web', 'api'))) {
  12. return false;
  13. }
  14. if ($type == 'app' && empty($module_name)) {
  15. return false;
  16. }
  17. $today = date('Ymd');
  18. $stat_visit_teble = table('stat_visit');
  19. $stat_visit_teble->searchWithDate($today);
  20. $stat_visit_teble->searchWithModule($module_name);
  21. $stat_visit_teble->searchWithUnacid($_W['uniacid']);
  22. $stat_visit_teble->searchWithType($type);
  23. $today_exist = $stat_visit_teble->get();
  24. if (empty($today_exist)) {
  25. $insert_data = array(
  26. 'uniacid' => $_W['uniacid'],
  27. 'module' => $module_name,
  28. 'type' => $type,
  29. 'date' => $today,
  30. 'count' => 1,
  31. 'ip_count' => 0
  32. );
  33. pdo_insert('stat_visit', $insert_data);
  34. $today_exist = $insert_data;
  35. $today_exist['id'] = pdo_insertid();
  36. } else {
  37. $data = array('count' => $today_exist['count'] + 1);
  38. pdo_update('stat_visit', $data, array('id' => $today_exist['id']));
  39. }
  40. if (!empty($today_exist['id'])) {
  41. $ip = ip2long(getip());
  42. $stat_ip_visit_table = table('stat_visit_ip');
  43. $stat_ip_visit_table->searchWithIp($ip);
  44. $stat_ip_visit_table->searchWithDate($today);
  45. $ip_today_exist = $stat_ip_visit_table->get();
  46. if (empty($ip_today_exist)) {
  47. $ip_insert_data = array(
  48. 'ip' => $ip,
  49. 'uniacid' => $_W['uniacid'],
  50. 'module' => $module_name,
  51. 'type' => $type,
  52. 'date' => $today,
  53. );
  54. pdo_insert('stat_visit_ip', $ip_insert_data);
  55. pdo_update('stat_visit', array('ip_count' => $today_exist['ip_count'] + 1), array('id' => $today_exist['id']));
  56. }
  57. }
  58. return true;
  59. }
  60. function visit_app_update_today_visit($module_name) {
  61. global $_W;
  62. $module_name = trim($module_name);
  63. if (empty($module_name) || !in_array($_W['account']['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH, ACCOUNT_TYPE_WEBAPP_NORMAL))) {
  64. return false;
  65. }
  66. $today = date('Ymd');
  67. $stat_visit_teble = table('stat_visit');
  68. $stat_visit_teble->searchWithDate($today);
  69. $stat_visit_teble->searchWithModule($module_name);
  70. $stat_visit_teble->searchWithType('app');
  71. $stat_visit_teble->searchWithUnacid($_W['uniacid']);
  72. $today_exist = $stat_visit_teble->get();
  73. if (empty($today_exist)) {
  74. $insert_data = array(
  75. 'uniacid' => $_W['uniacid'],
  76. 'module' => $module_name,
  77. 'type' => 'app',
  78. 'date' => $today,
  79. 'count' => 1,
  80. 'ip_count' => 0
  81. );
  82. pdo_insert('stat_visit', $insert_data);
  83. $today_exist = $insert_data;
  84. $today_exist['id'] = pdo_insertid();
  85. } else {
  86. $data = array('count' => $today_exist['count'] + 1);
  87. pdo_update('stat_visit', $data, array('id' => $today_exist['id']));
  88. }
  89. $yestoday = date('Ymd', strtotime('-1 day'));
  90. $cache_key = cache_system_key('delete_visit_ip', array('date' => $yestoday));
  91. $is_delete_visit_ip = cache_load($cache_key);
  92. if (empty($is_delete_visit_ip)) {
  93. pdo_delete('stat_visit_ip', $yestoday);
  94. cache_write($cache_key, true);
  95. }
  96. if (!empty($today_exist['id'])) {
  97. $ip = ip2long(getip());
  98. $stat_ip_visit_table = table('stat_visit_ip');
  99. $stat_ip_visit_table->searchWithIp($ip);
  100. $stat_ip_visit_table->searchWithDate($today);
  101. $ip_today_exist = $stat_ip_visit_table->get();
  102. if (empty($ip_today_exist)) {
  103. $ip_insert_data = array(
  104. 'ip' => $ip,
  105. 'uniacid' => $_W['uniacid'],
  106. 'module' => $module_name,
  107. 'type' => 'app',
  108. 'date' => $today,
  109. );
  110. pdo_insert('stat_visit_ip', $ip_insert_data);
  111. pdo_update('stat_visit', array('ip_count' => $today_exist['ip_count'] + 1), array('id' => $today_exist['id']));
  112. }
  113. }
  114. return true;
  115. }
  116. function visit_app_pass_visit_limit($uniacid = 0) {
  117. global $_W;
  118. if (strpos($_W['siteurl'], 'api.php?') === false && ($_W['isajax'] || $_W['ispost'] || strpos($_W['siteurl'], 'c=utility&a=visit') !== false)) {
  119. return false;
  120. }
  121. $uniacid = intval($uniacid) > 0 ? intval($uniacid) : $_W['uniacid'];
  122. $limit = uni_setting_load('statistics', $uniacid);
  123. $limit = $limit['statistics'];
  124. if (empty($limit)) {
  125. return false;
  126. }
  127. $cachekey = cache_system_key('statistics', array('uniacid' => $uniacid));
  128. $cache = cache_load($cachekey);
  129. if (!empty($cache) && ($cache['time'] + $limit['interval'] > TIMESTAMP)) {
  130. return $cache['limit'];
  131. }
  132. $data = array('time' => TIMESTAMP, 'limit' => false);
  133. $today_num = visit_app_today_visit($uniacid);
  134. if (!empty($limit['founder'])) {
  135. $before_num = visit_app_month_visit_till_today($uniacid);
  136. $sum_num = intval($limit['founder']) - intval($limit['use']);
  137. if ($sum_num <= 0 || $limit['founder'] <= $before_num + $today_num) {
  138. $data['limit'] = true;
  139. cache_write($cachekey, $data);
  140. return true;
  141. }
  142. }
  143. if (!empty($limit['owner']) && $today_num > $limit['owner']) {
  144. $data['limit'] = true;
  145. cache_write($cachekey, $data);
  146. return true;
  147. }
  148. if (!empty($limit['founder']) && ($before_num + $today_num) > $limit['founder']) {
  149. $limit['use'] = !empty($limit['use']) ? (intval($limit['use']) + 1) : 1;
  150. uni_setting_save('statistics', $limit);
  151. }
  152. cache_write($cachekey, $data);
  153. return false;
  154. }
  155. function visit_app_month_visit_till_today($uniacid = 0) {
  156. global $_W;
  157. $result = 0;
  158. $uniacid = intval($uniacid) > 0 ? intval($uniacid) : $_W['uniacid'];
  159. $today = date('Ymd');
  160. $cachekey = cache_system_key('uniacid_visit', array('uniacid' => $uniacid, 'today' => $today));
  161. $cache = cache_load($cachekey);
  162. if (!empty($cache)) {
  163. return $cache;
  164. }
  165. $start = date('Ym01', strtotime(date("Ymd")));
  166. $end = date('Ymd', strtotime('-1 day'));
  167. $stat_visit_teble = table('stat_visit');
  168. $stat_visit_teble->searchWithGreaterThenDate($start);
  169. $stat_visit_teble->searchWithLessThenDate($end);
  170. $stat_visit_teble->searchWithType('app');
  171. $stat_visit_teble->searchWithUnacid($uniacid);
  172. $visit = $stat_visit_teble->getall();
  173. if (!empty($visit)) {
  174. foreach ($visit as $val) {
  175. $result += $val['count'];
  176. }
  177. }
  178. cache_write($cachekey, $result);
  179. return $result;
  180. }
  181. function visit_app_today_visit($uniacid = 0) {
  182. global $_W;
  183. $result = 0;
  184. $uniacid = intval($uniacid) > 0 ? intval($uniacid) : $_W['uniacid'];
  185. $stat_visit_teble = table('stat_visit');
  186. $stat_visit_teble->searchWithDate(date('Ymd'));
  187. $stat_visit_teble->searchWithType('app');
  188. $stat_visit_teble->searchWithUnacid($uniacid);
  189. $today = $stat_visit_teble->getall();
  190. if (!empty($today)) {
  191. foreach ($today as $val) {
  192. $result += $val['count'];
  193. }
  194. }
  195. return $result;
  196. }