File size: 13,146 Bytes
a4a352b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace app\admin\controller;

use think\Db;

class Annex extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function data()
    {
        $param = input();
        $param['page'] = intval($param['page']) < 1 ? 1 : $param['page'];
        $param['limit'] = intval($param['limit']) < 1 ? $this->_pagesize : $param['limit'];

        $where = [];
        if (!empty($param['type'])) {
            $where['annex_type'] = ['eq', $param['type']];
        }
        if (!empty($param['mid'])) {
            $where['annex_mid'] = ['eq', $param['mid']];
        }
        if(!empty($param['wd'])){
            $param['wd'] = htmlspecialchars(urldecode($param['wd']));
            $where['annex_file'] = ['like','%'.$param['wd'].'%'];
        }

        $order='annex_time desc';
        $res = model('Annex')->listData($where,$order,$param['page'],$param['limit']);

        $this->assign('list', $res['list']);
        $this->assign('total', $res['total']);
        $this->assign('page', $res['page']);
        $this->assign('limit', $res['limit']);

        $param['page'] = '{page}';
        $param['limit'] = '{limit}';
        $this->assign('param', $param);

        $this->assign('title', lang('admin/annex/title'));
        return $this->fetch('admin@annex/index');
    }

    public function file()
    {
        $path = input('path');
        $path = str_replace('\\','',$path);
        $path = str_replace('/','',$path);

        if(empty($path)){
            $path = '@upload';
        }

        if(substr($path,0,7) != "@upload") { $path = "@upload"; }
        if(count( explode("..@",$path) ) > 1) {
            $this->error(lang('illegal_request'));
            return;
        }


        $uppath = substr($path,0,strrpos($path,"@"));

        $ischild = 0;
        if ($path !="@upload"){
            $ischild = 1;
        }
        $this->assign('uppath',$uppath);
        $this->assign('ischild',$ischild);


        $num_path = 0;
        $num_file = 0;
        $sum_size = 0;
        $filters = ",,cache,break,artcollect,downdata,playdata,export,vodcollect,";
        $files = [];

        $pp = str_replace('@','/',$path);

        if(is_dir('.'.$pp)){

            $farr = glob('.'.$pp.'/*');
            if($farr){
                foreach($farr as $f){

                    if ( is_dir($f) ){

                        if(strpos($filters,",".$f.",")<=0){
                            $num_path++;
                            $tmp_path = str_replace('./upload/','@upload/',$f);
                            $tmp_path = str_replace('/','@',$tmp_path);

                            $tmp_name = str_replace($path.'@','',$tmp_path);


                            $files[] = ['isfile'=>0,'name'=>$tmp_name,'path'=>$tmp_path];
                        }
                    }
                    elseif(is_file($f)){
                        if (strpos($f,".html") <=0 && strpos($f,".htm") <=0){
                            $num_file++;
                            $fsize = filesize($f);
                            $sum_size += $fsize;
                            $fsize = mac_format_size($fsize);
                            $ftime = filemtime($f);
                            $tmp_path = mac_convert_encoding($f,"UTF-8","GB2312");

                            $tmp_path = str_replace('./upload/','@upload/',$f);
                            $tmp_path = str_replace('/','@',$tmp_path);

                            $tmp_name = str_replace($path.'@',"",$tmp_path);
                            $tmp_path = str_replace('@','/',$tmp_path);

                            $files[] = ['isfile'=>1,'name'=>$tmp_name,'path'=>$tmp_path, 'size'=>$fsize, 'time'=>$ftime];
                        }
                    }

                }
            }
        }
        $this->assign('sum_size',mac_format_size($sum_size));
        $this->assign('num_file',$num_file);
        $this->assign('num_path',$num_path);

        $this->assign('files',$files);

        $this->assign('title',lang('admin/annex/title'));
        return $this->fetch('admin@annex/file');
    }

    public function info()
    {
        if (Request()->isPost()) {
            $param = input('post.');
            $res = model('Annex')->saveData($param);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }

        $id = input('id');
        $where=[];
        $where['annex_id'] = ['eq',$id];
        $res = model('Annex')->infoData($where);
        $info = $res['info'];
        $this->assign('info',$info);

        $this->assign('title',lang('admin/annex/title'));
        return $this->fetch('admin@annex/info');
    }

    public function del()
    {
        $param = input();
        $ids = $param['ids'];

        if(!empty($ids)){
            if(is_array($ids)){
                foreach($ids as $k=>$v){
                    $ids[$k] = str_replace('./','',$v);
                }
            }
            $where=[];
            $where['annex_id|annex_file'] = ['in',$ids];
            $res = model('Annex')->delData($where);
            if($res['code']>1){
                return $this->error($res['msg']);
            }
            return $this->success($res['msg']);
        }
        return $this->error(lang('param_err'));
    }

    public function check()
    {
        mac_echo('<style type="text/css">body{font-size:12px;color: #333333;line-height:21px;}span{font-weight:bold;color:#FF0000}</style>');

        $param = input();
        $num = intval($param['num']);
        $start = intval($param['start']);
        $page_count = intval($param['page_count']);
        $data_count = intval($param['data_count']);
        if($start<1){
            $start=1;
        }
        if($page_count<1){
            $page_count=1;
        }
        $page_size = 500;
        if(empty($data_count)){
            $where=[];
            $data_count = model('Annex')->countData($where);
            $page_count = ceil($data_count / $page_size);

            $param['data_count'] = $data_count;
            $param['page_count'] = $page_count;
            $param['page_size'] = $page_size;
        }

        if($start > $page_count){
            mac_echo(lang('admin/annex/check_complete'));
            exit;
        }

        mac_echo(lang('admin/annex/info_tip',[$param['data_count'],$param['page_count'],$param['page_size'],$start]));
        $limit_str = ($page_size * ($page_count-$start)) .",".$page_size;

        $list = Db::name('Annex')->field('*')->where($where)->limit($limit_str)->fetchSql(false)->orderRaw('annex_time desc')->select();
        foreach ($list as $k3 => $v3) {
            $tmp = $v3['annex_file'];
            if(!file_exists('./'.$tmp)){
                $where=[];
                $where['annex_file'] = ['eq',$tmp];
                $r = Db::name('Annex')->where($where)->delete();
                mac_echo($tmp . '...del');
            }
        }
        $param['start'] = ++$start;
        $url = url('annex/check') .'?'. http_build_query($param);
        mac_jump( $url ,3);
    }

    public function init()
    {
        $param = input();

        if($param['ck']){
            mac_echo('<style type="text/css">body{font-size:12px;color: #333333;line-height:21px;}span{font-weight:bold;color:#FF0000}</style>');

            $start = intval($param['start']);
            if($start<1){
                $start=1;
            }

            $pre = config('database.prefix');
            $schema = Db::query('select * from information_schema.columns where table_schema = ?', [config('database.database')]);
            $col_list = [];
            foreach ($schema as $k => $v) {
                $col_list[$v['TABLE_NAME']][$v['COLUMN_NAME']] = $v;
            }
            $tables = ['actor', 'art', 'topic', 'type', 'vod', 'website' ,'actor', 'role'];
            $param['tbi'] = intval($param['tbi']);
            if ($param['tbi'] >= count($tables)) {
                mac_echo(lang('admin/annex/check_ok'));
                die;
            }
            $tab = $tables[$param['tbi']];

            $where=[];
            $page_size = 500;
            $data_count = model($tab)->countData($where);
            $page_count = ceil($data_count / $page_size);

            if($start > $page_count){
                mac_echo(lang('admin/annex/check_jump',[$tab]));
                $param['tbi']++;
                $param['start'] = 1;
                $url = url('annex/init') . '?' . http_build_query($param);
                mac_jump($url, 3);
                exit;
            }

            mac_echo(lang('admin/annex/check_tip1',[$tab,$data_count,$page_count,$page_size,$start]));

            foreach ($col_list as $k1 => $v1) {
                $pre_tb = str_replace($pre, '', $k1);
                $si = array_search($pre_tb, $tables);
                if ($pre_tb !== $tab) {
                    continue;
                }
                $limit_str = ($page_size * ($page_count-$start)) .",".$page_size;
                $list = Db::name($pre_tb)->field('*')->limit($limit_str)->fetchSql(false)->select();

                $adds = [];
                foreach ($list as $k3 => $v3) {
                    $col_id = $tables[$si] . '_id';
                    $col_name = $tables[$si] . '_name';
                    $val_id = $v3[$col_id];;
                    $val_name = strip_tags($v3[$col_name]);
                    $ck = false;
                    $where2 = [];
                    $where2[$col_id] = $val_id;
                    $imgs = [];
                    $add = [];
                    $add['id'] = $val_id;
                    $add['name'] = $val_name;
                    $add['col_id'] = $col_id;

                    $col = $tables[$si] . '_pic';
                    $val = $v3[$col];
                    if (substr($val, 0, 6) == 'upload' && file_exists('./' . $val)) {
                        $imgs[] = ['annex_file' => $val, 'annex_time' => time(), 'annex_size' => filesize('./' . $val), 'annex_type' => 'image'];
                        $ck = true;
                    }
                    $col = $tables[$si] . '_pic_thumb';
                    $val = $v3[$col];
                    if (substr($val, 0, 6) == 'upload' && file_exists('./' . $val)) {
                        $imgs[] = ['annex_file' => $val, 'annex_time' => time(), 'annex_size' => filesize('./' . $val), 'annex_type' => 'image'];
                        $ck = true;
                    }
                    $col = $tables[$si] . '_pic_slide';
                    $val = $v3[$col];
                    if (substr($val, 0, 6) == 'upload' && file_exists('./' . $val)) {
                        $imgs[] = ['annex_file' => $val, 'annex_time' => time(), 'annex_size' => filesize('./' . $val), 'annex_type' => 'image'];
                        $ck = true;
                    }

                    $col = $tables[$si] . '_content';
                    $val = $v3[$col];
                    if (!empty($val)) {
                        $rule = mac_buildregx("<img[^>]*src\s*=\s*['" . chr(34) . "]?([\w/\-\:.]*)['" . chr(34) . "]?[^>]*>", "is");
                        preg_match_all($rule, $val, $matches);

                        $matchfieldarr = $matches[1];
                        foreach ($matchfieldarr as $f => $matchfieldstr) {
                            $img_src = trim(preg_replace("/[ \r\n\t\f]{1,}/", " ", $matchfieldstr));
                            if (substr($img_src, 0, 7) == '/upload' && file_exists('.' . $img_src)) {
                                $imgs[] = ['annex_file' => substr($img_src, 1), 'annex_time' => time(), 'annex_size' => filesize('.' . $img_src), 'annex_type' => 'image'];
                                $ck = true;
                            }
                        }
                    }
                    $add['imgs'] = $imgs;
                    $adds[] = $add;
                }
                if (!empty($adds)) {
                    $insert = [];
                    foreach ($adds as $k => $v) {
                        $des = '<font color=red>'.lang('skip').'</font>';
                        if (!empty($v['imgs'])) {
                            foreach($v['imgs'] as $k2 => $v2){
                                $where = [];
                                $where['annex_file'] = $v2['annex_file'];
                                $r = model('Annex')->infoData($where);
                                if ($r['code'] !== 1) {
                                    $insert[] = $v2;
                                    $des = '<font color=green>ok</font>';
                                }
                            }
                        }
                        mac_echo($v['name'] . '...' . $des);
                        model('Annex')->insertAll($insert);
                    }
                }
            }

            $param['start']++;
            $url = url('annex/init') . '?' . http_build_query($param);
            mac_jump($url, 3);
            exit;
        }
        return $this->fetch('admin@annex/init');
    }


}