Safety.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class Safety extends Base
  5. {
  6. var $_files;
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function index()
  12. {
  13. }
  14. protected function listDir($dir){
  15. if(is_dir($dir)){
  16. if ($dh = opendir($dir)) {
  17. while (($file= readdir($dh)) !== false){
  18. $tmp = str_replace('//','/',mac_convert_encoding($dir.$file, "UTF-8", "GB2312"));
  19. if((is_dir($dir."/".$file)) && $file!="." && $file!=".."){
  20. $this->listDir($dir."/".$file."/");
  21. } else{
  22. if($file!="." && $file!=".."){
  23. $this->_files[$tmp] = ['md5'=>md5_file($dir.$file)];
  24. }
  25. }
  26. }
  27. closedir($dh);
  28. }
  29. }
  30. }
  31. public function file()
  32. {
  33. $param = input();
  34. if($param['ck']){
  35. $ft = $param['ft'];
  36. if(empty($ft)){
  37. $ft = ['1','2'];
  38. }
  39. mac_echo('<style type="text/css">body{font-size:12px;color: #333333;line-height:21px;}span{font-weight:bold;color:#FF0000}</style>');
  40. $url = base64_decode("aHR0cDovL3VwZGF0ZS5tYWNjbXMubGEv") . "v10/mac_files_".config('version')['code'].'.html';
  41. $html = mac_curl_get($url);
  42. $json = json_decode($html,true);
  43. if(!$json){
  44. return $this->error(lang('admin/safety/file_msg1'));
  45. }
  46. $this->listDir('./');
  47. if(!is_array($this->_files)){
  48. return $this->error(lang('admin/safety/file_msg2'));
  49. }
  50. foreach($this->_files as $k=>$v){
  51. $color = '';
  52. $msg = 'ok';
  53. if(empty($json[$k]) && in_array('1',$ft)){
  54. $color = 'BlueViolet';
  55. $msg = lang('admin/safety/file_msg3');
  56. }
  57. elseif(!empty($json[$k]) && $v['md5'] != $json[$k]['md5'] && in_array('2',$ft)){
  58. $color = 'red';
  59. $msg = lang('admin/safety/file_msg4');
  60. }
  61. if($color!='') {
  62. //$this->_files[$k]['jc'] = $color;
  63. mac_echo($k . '---' . "<font color=$color>" . $msg . '</font>');
  64. }
  65. }
  66. exit;
  67. }
  68. return $this->fetch('admin@safety/file');
  69. }
  70. public function data()
  71. {
  72. $param = input();
  73. if ($param['ck']) {
  74. $pre = config('database.prefix');
  75. $schema = Db::query('select * from information_schema.columns where table_schema = ?', [config('database.database')]);
  76. $col_list = [];
  77. $sql = '';
  78. foreach ($schema as $k => $v) {
  79. $col_list[$v['TABLE_NAME']][$v['COLUMN_NAME']] = $v;
  80. }
  81. $tables = ['actor', 'art', 'gbook', 'link', 'topic', 'type', 'vod'];
  82. $param['tbi'] = intval($param['tbi']);
  83. if ($param['tbi'] >= count($tables)) {
  84. mac_echo(lang('admin/safety/data_clear_ok'));
  85. die;
  86. }
  87. $check_arr = ["<script","<iframe","{php}","{:"];
  88. $rel_val = [
  89. [
  90. "/<script[\s\S]*?<\/(.*)>/is",
  91. "/<script[\s\S]*?>/is",
  92. ],
  93. [
  94. "/<iframe[\s\S]*?<\/(.*)>/is",
  95. "/<iframe[\s\S]*?>/is",
  96. ],
  97. [
  98. "/{php}[\s\S]*?{\/php}/is",
  99. ],
  100. [
  101. "/{:[\s\S]*?}/is",
  102. ]
  103. ];
  104. mac_echo('<style type="text/css">body{font-size:12px;color: #333333;line-height:21px;}span{font-weight:bold;color:#FF0000}</style>');
  105. foreach ($col_list as $k1 => $v1) {
  106. $pre_tb = str_replace($pre, '', $k1);
  107. $si = array_search($pre_tb, $tables);
  108. if ($pre_tb !== $tables[$param['tbi']]) {
  109. continue;
  110. }
  111. mac_echo(lang('admin/safety/data_check_tip1',[$k1]));
  112. $where = [];
  113. foreach ($v1 as $k2 => $v2) {
  114. if (strpos($v2['DATA_TYPE'], 'int') === false) {
  115. $where[$k2] = ['like', mac_like_arr(join(',', $check_arr)), 'OR'];
  116. }
  117. }
  118. if (!empty($where)) {
  119. $field = array_keys($where);
  120. $field[] = $tables[$si] . '_id';
  121. $list = Db::name($pre_tb)->field($field)->whereOr($where)->fetchSql(false)->select();
  122. mac_echo(lang('admin/safety/data_check_tip2',[count($list)]));
  123. foreach ($list as $k3 => $v3) {
  124. $update = [];
  125. $col_id = $tables[$si] . '_id';
  126. $col_name = $tables[$si] . '_name';
  127. $val_id = $v3[$col_id];;
  128. $val_name = strip_tags($v3[$col_name]);
  129. $ck = false;
  130. $where2 = [];
  131. $where2[$col_id] = $val_id;
  132. foreach ($v3 as $k4 => $v4) {
  133. if ($k4 != $col_id) {
  134. $val = $v4;
  135. foreach ($check_arr as $kk => $vv) {
  136. foreach($rel_val[$kk] as $k5=>$v5){
  137. $val = preg_replace($v5, "", $val);
  138. }
  139. }
  140. if ($val !== $v4) {
  141. $update[$k4] = $val;
  142. $ck = true;
  143. }
  144. }
  145. }
  146. if ($ck) {
  147. $r = Db::name($pre_tb)->where($where2)->update($update);
  148. mac_echo($val_id . '、' . $val_name . ' ok');
  149. }
  150. }
  151. }
  152. }
  153. $param['tbi']++;
  154. $url = url('safety/data') . '?' . http_build_query($param);
  155. mac_jump($url, 3);
  156. exit;
  157. }
  158. return $this->fetch('admin@safety/data');
  159. }
  160. }