forked from huangqiheng/html-sensor
/
funcs.php
1488 lines (1247 loc) · 37 KB
/
funcs.php
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'modules/memcached_namespace.php';
require_once 'modules/geoipcity.php';
require_once 'modules/device_name.php';
require_once 'dbase.php';
//缓存目录路径
define('DEVICE_SAVED_PATH', dirname(__FILE__).'/cache/device');
define('ASYNC_MSG_PATH', dirname(__FILE__).'/cache/asyncmsg');
define('BROWSER_CACHE_PATH', dirname(__FILE__).'/cache/browser');
define('LOCALE_CACHE_PATH', dirname(__FILE__).'/cache/locale');
define('TITLE_CACHE_PATH', dirname(__FILE__).'/cache/title');
define('LOCAL_LOG_FILE', dirname(__FILE__).'/cache/log/debug.log');
//memcached需要用到的
define('MEMC_HOST', '127.0.0.1');
define('MEMC_PORT', 11211);
define('API_MEMC_POOL', 'api_memcached_pool');//没必要改
define('GET_BROWSER', 'GET_BROWSER');//没必要改
define('GET_BROWSER_EXPIRE', 3600*8);
define('GET_DEVICE', 'GET_DEVICE');//没必要改
define('GET_DEVICE_EXPIRE', 3600*8);
define('GET_LOCALE', 'GET_LOCALE');//没必要改
define('GET_LOCALE_EXPIRE', 3600*8);
define('GET_CONF_EXPIRE', 5);
define('FORCE_CONF_UPDATE', false);
//jsondb界面字段映射
$field_mapper_data = [
'名称' => 'name',
'分类标签' => 'categorys',
//弹窗任务
'目标人群' => 'targets',
'消息集合' => 'messages',
'消息样式' => 'style', //苹果风格?
'运行状态' => 'status',
'开始时间' => 'start_time',
'结束时间' => 'finish_time',
'执行次数' => 'times',
'每次间隔' => 'time_interval',
'前距时间' => 'time_interval_pre',
'间隔模式' => 'time_interval_mode',
'消息顺序' => 'msg_sequence',
'任务互斥' => 'repel',
//替换任务
'单页个数' => 'max_perpage',
'间隔PV' => 'pv_interval',
'前距PV' => 'pv_interval_pre',
//目标用户
'便携设备' => 'ismobiledevice',
'浏览器' => 'browser',
'操作系统' => 'platform',
'设备名' => 'device_name',
'地区' => 'region',
'语言' => 'language',
'浏览器特征(正则)' => 'UserAgent',
'首访用户' => 'is_first_session',
'首日用户' => 'is_first_day',
'首访第一页' => 'is_first_pageview',
'已注册' => 'is_registed',
'访问网址(正则)' => 'Visiting',
'来访停留秒数区间' => 'staytime',
'来访次数区间' => 'session_count',
'页面浏览数区间' => 'session_pv',
'总页面浏览数区间' => 'pageview_count',
'账户名' => 'kword_account',
'网页标题关键字' => 'kword_title',
'提交框关键字' => 'kword_submit',
'购物车关键字' => 'kword_cart',
'收藏夹关键字' => 'kword_favorite',
'包含关键字' => 'include_kword',
'不包含关键字' => 'exclude_kword',
//消息的翻译
'消息标题' => 'title',
'消息内容' => 'text',
'消息类型' => 'msgmod', //异步还是实时
'消息形式' => 'msgform', //弹出还是替换
'显示位置' => 'position',
'固定显示' => 'sticky',
'显示时长' => 'time',
'弹窗警示' => 'before_open',
];
function get_confs_cached($db_name=null, $table_name=null, $force_update=false)
{
FORCE_CONF_UPDATE && ($force_update = true);
$key = $db_name.'|config|'.$table_name;
$key_valid = $db_name.'|config.valid|'.$table_name;
$mem = api_open_mmc();
if ($force_update === false) {
//如果还有效,直接获取返回
if ($is_valid = $mem->get($key_valid)) {
if ($result = $mem->get($key)) {
return $result;
}
//如果已经失效
} else {
//看看旧的在否?有则先设置标记,让其他人先获取
if ($result = $mem->get($key)) {
$mem->set($key_valid, 'true', GET_CONF_EXPIRE);
sync_job('get_confs', [$db_name, $table_name]);
return $result;
}
}
}
$result = get_confs($db_name, $table_name, '名称');
$config = $result['push-settings'];
$popup_confs = make_popup_list($config);
$replace_confs = make_replace_list($config);
$confs = ['popup' => $popup_confs, 'replace' => $replace_confs];
$confs = fields_mapper($confs);
$confs['system'] = get_system_confs();
$mem->set($key, $confs);
$mem->set($key_valid, 'true', GET_CONF_EXPIRE);
return $confs;
}
function get_system_confs()
{
$result = [];
$configs = get_confs('push-settings', 'system');
$configs = @$configs['push-settings']['system'];
if (empty($configs)) {
return [];
}
$type_descs = [];
foreach($configs as $conf) {
$key = $conf['key'];
$value = $conf['value'];
if ($conf['type'] === 'integer') {
$value = intval($value);
}
elseif($conf['type'] === 'date') {
$value = format_time($value);
}
elseif($conf['type'] === 'json') {
$value = json_decode($value);
}
if ($conf['array'] === 'yes') {
if (!array_key_exists($key, $result)) {
$result[$key] = [];
}
$items = &$result[$key];
if (!in_array($value, $items)) {
$items[] = $value;
}
} else {
$result[$key] = $value;
}
$type_descs[$key] = $conf['type'];
}
$result['type_desc'] = $type_descs;
return $result;
}
function fields_mapper($confs)
{
global $field_mapper_data;
$confs = replace_keys($confs, function($key)use($field_mapper_data){
$res = @$field_mapper_data[$key];
return empty($res)? $key : $res;
});
return $confs;
}
function replace_keys(array $input, $replace_func)
{
$return = array();
foreach ($input as $key => $value) {
$key = call_user_func($replace_func, $key);
if (is_array($value)) {
$value = replace_keys($value, $replace_func);
}
$return[$key] = $value;
}
return $return;
}
function make_replace_list($result)
{
$conf_replace_task = $result['replace-task'];
$conf_users = $result['users'];
$conf_messages = $result['messages'];
$conf_keywords = $result['keywords'];
$conf_positions = $result['positions'];
$result = array();
foreach ($conf_replace_task as $name=>$task) {
$target_users = @$task['目标人群'];
$target_messages = @$task['消息集合'];
if (empty($target_messages)) {continue;}
$new_target = get_user_selected($conf_users, $target_users);
$pick_message = get_user_selected($conf_messages, $target_messages);
foreach ($pick_message as $name=>$msg) {
if ($msg['消息形式'] === '替换显示') {
continue;
}
unset($pick_message[$name]);
}
$task['目标人群'] = format_targets($new_target, $conf_keywords);
$task['消息集合'] = format_message($pick_message, $conf_positions);
$task['运行状态'] = task_status($task['运行状态']);
$task['开始时间'] = trans_time($task['开始时间']);
$task['结束时间'] = trans_time($task['结束时间']);
$task['消息顺序'] = pop_sequence($task['消息顺序']);
$task['执行次数'] = intval($task['执行次数']);
$task['单页个数'] = intval($task['单页个数']);
$task['间隔PV'] = intval($task['间隔PV']);
$task['前距PV'] = intval($task['前距PV']);
$result[$name] = $task;
}
return $result;
}
function make_popup_list($result)
{
$conf_popup_task = $result['popup-task'];
$conf_users = $result['users'];
$conf_messages = $result['messages'];
$conf_keywords = $result['keywords'];
$conf_positions = $result['positions'];
$result = array();
foreach ($conf_popup_task as $name=>$task) {
$target_users = @$task['目标人群'];
$target_messages = @$task['消息集合'];
if (empty($target_messages)) {continue;}
$new_target = get_user_selected($conf_users, $target_users);
$pick_message = get_user_selected($conf_messages, $target_messages);
foreach ($pick_message as $name=>$msg) {
if ($msg['消息形式'] === '弹出显示') {
continue;
}
unset($pick_message[$name]);
}
$task['目标人群'] = format_targets($new_target, $conf_keywords);
$task['消息集合'] = format_message($pick_message, $conf_positions);
$task['运行状态'] = task_status($task['运行状态']);
$task['消息样式'] = pop_style($task['消息样式']);
$task['开始时间'] = trans_time($task['开始时间']);
$task['结束时间'] = trans_time($task['结束时间']);
$task['执行次数'] = intval($task['执行次数']);
$task['间隔模式'] = internal_mode($task['间隔模式']);
$task['每次间隔'] = intval($task['每次间隔']);
$task['前距时间'] = intval($task['前距时间']);
$task['消息顺序'] = pop_sequence($task['消息顺序']);
$task['任务互斥'] = pop_repel($task['任务互斥']);
$result[$name] = $task;
}
return $result;
}
function pop_style($style)
{
switch ($style) {
case '苹果风格' : return 'mac';
}
return 'normal';
}
function task_status($status)
{
if ($status === '活动') {
return 'active';
}
return 'inactive';
}
function pop_repel($repel)
{
if ($repel === '单独显示') {
return true;
}
return false;
}
function pop_sequence($sequence)
{
switch ($sequence) {
case '顺序消息' : return 'sequence';
case '随机消息' : return 'random';
case '乱序消息' : return 'confusion';
}
return 'sequence';
}
function internal_mode($mode)
{
if ($mode === '绝对间隔') {
return 'absolute';
}
return 'relative';
}
function check_input($input, $max_length = 32)
{
$input = trim(strip_tags($input));
return substr($input, 0, $max_length);
}
function check_sticky($sticky)
{
if ($sticky === '固定显示') {
return true;
}
return false;
}
function check_msgmod($msgmod)
{
switch ($msgmod) {
case '实时消息' : return 'realtime';
case '异步消息' : return 'heartbeat';
}
return 'heartbeat';
}
function check_msgform($msgform)
{
switch ($msgform) {
case '弹出显示' : return 'popup';
case '替换显示' : return 'replace';
}
return 'popup';
}
function check_before_open($bo)
{
switch ($bo) {
case '没有警示' : return false;
case '强制警示' : return true;
}
return false;
}
function check_position($position, $conf_positions)
{
switch ($position) {
case '页面左上方' : return 'top-left';
case '页面左下方' : return 'bottom-left';
case '页面右上方' : return 'top-right';
case '页面右下方' : return 'bottom-right';
}
//对于替换来说,这里得到的是“标签”的名字,所以实际上,可能包含多个位置
$posi_list = [];
foreach($conf_positions as $posi_item) {
$cats = $posi_item['分类标签'];
if (in_array($position, $cats)) {
$posi_list[] = md5($posi_item['名称']);
}
}
return $posi_list;
}
function format_message($messages, $conf_positions)
{
$new_message = [];
foreach ($messages as &$item) {
$item['消息形式'] = check_msgform($item['消息形式']);
$item['显示时长'] = intval($item['显示时长']);
$item['固定显示'] = check_sticky($item['固定显示']);
$item['消息类型'] = check_msgmod($item['消息类型']);
$item['弹窗警示'] = check_before_open($item['弹窗警示']);
$item['显示位置'] = check_position($item['显示位置'], $conf_positions);
$new_message[] = $item;
}
return $new_message;
}
function format_targets($targets, $conf_keywords)
{
if (empty($targets)) {
return [];
}
foreach ($targets as &$item) {
$item['账户名'] = get_user_selected($conf_keywords, $item['账户名']);
$item['网页标题关键字'] = get_user_selected($conf_keywords, $item['网页标题关键字']);
$item['提交框关键字'] = get_user_selected($conf_keywords, $item['提交框关键字']);
$item['购物车关键字'] = get_user_selected($conf_keywords, $item['购物车关键字']);
$item['收藏夹关键字'] = get_user_selected($conf_keywords, $item['收藏夹关键字']);
$item['来访停留秒数区间'] = get_ranges($item['来访停留秒数区间']);
$item['来访次数区间'] = get_ranges($item['来访次数区间']);
$item['总页面浏览数区间'] = get_ranges($item['总页面浏览数区间']);
$item['页面浏览数区间'] = get_ranges($item['页面浏览数区间']);
$item['便携设备'] = get_bool(@$item['便携设备'], '移动设备');
$item['首访用户'] = get_bool(@$item['首访用户'], '首次访问');
$item['首日用户'] = get_bool(@$item['首日用户'], '首日来访');
$item['首访第一页'] = get_bool(@$item['首访第一页'], '第一页');
$item['已注册'] = get_bool(@$item['已注册'], '已注册');
}
return $targets;
}
function get_bool($item_str, $true_val)
{
if (empty($item_str)) {
return null;
}
return ($item_str === $true_val);
}
function get_ranges($range_arr)
{
$result = [];
if (is_array($range_arr)) {
foreach($range_arr as $range_str) {
$result[] = get_range($range_str);
}
} else {
}
return $result;
}
function get_range($range_str)
{
$res = explode('-', $range_str);
if (empty($res)) {
return [];
}
return [intval($res[0]), intval($res[1])];
}
function trans_time($time_str)
{
return strtotime(preg_replace('/\(.+\)/', '', $time_str));
}
function make_capview($username, $nickname, $caption)
{
empty($nickname) && (!empty($username)) && ($cap_view=$username.'@'.$caption);
empty($username) && (!empty($nickname)) && ($cap_view=$nickname.'@'.$caption);
empty($cap_view) && ($cap_view=$nickname.'('.$username.')@'.$caption);
return $cap_view;
}
function is_nomsg()
{
return (isset($PARAMS['nomsg']))? ($PARAMS['nomsg'] === 'true') : false;
}
function is_md5_keys($key)
{
return (strlen($key) === 32);
}
function gen_events($browser_save)
{
$events = array();
$events['debug'] = is_debug_client();
$events['im'] = intval($browser_save['ismobiledevice']);
$events['ii'] = intval($browser_save['ip_changed']);
$events['ir'] = intval($browser_save['region_changed']);
$events['pf'] = $browser_save['first_pageview'];
$events['pv'] = $browser_save['pageview_count'];
$events['se'] = $browser_save['session_count'];
$events['ss'] = $browser_save['start_session'];
$events['sp'] = $browser_save['session_pv'];
$events['tp'] = $browser_save['popup_times'];
$events['tr'] = $browser_save['replace_times'];
$events['cb'] = $browser_save['browser'];
$events['cp'] = $browser_save['platform'];
$events['cd'] = $browser_save['device_name'];
$events['cr'] = $browser_save['region'];
return $events;
}
function get_user_selected($configs, $user_selected, $tag_field ='分类标签')
{
if (empty($user_selected)) {
return [];
}
$result = array();
foreach($configs as $name=>$values) {
$matchs = array_intersect($values[$tag_field], $user_selected);
if (empty($matchs)) {continue; }
$result[$name] = $values;
}
return $result;
}
function general_match_tasks($browser_save, $task_id, &$task_info, $task)
{
$result = array(false, false);
$task_info['pageviews'] += 1;
$task_print = $task_info['name'].'('.$task_info['run_times'].'/'.$task['times'].')';
//忽略不是自己的任务
if ($task_info['bypass'] === true) {
omp_trace($task_print.' not mine');
return $result;
}
//判读是否过期
$now = time();
$run_status = ($now<$task['start_time'])? 'waiting' : (($now>$task['finish_time'])? 'timeout' : 'running');
$result[1] = ($run_status === 'timeout');
//任务是否被用户停止
$status = $task['status'];
if ($status === 'inactive') {
omp_trace($task_print.' inactive');
return $result;
}
//记录超时
if ($run_status === 'timeout') {
$task['status'] = 'inactive';
omp_trace($task_print.' expired');
return $result;
}
//检查是否在时间区间内,否则忽略
if ($run_status !== 'running') {
omp_trace($task_print.' not time rigion ');
return $result;
}
//检查执行次数是否已经达到
if ($task['times'] !== -1) {
if ($task_info['run_times'] >= $task['times']) {
omp_trace($task_print.' times exceed ('.$task['times'].')');
return $result;
}
}
//详细再查条件
$matched_target = targets_matched($task['targets'], $browser_save, true);
if (!$matched_target) {
omp_trace($task_print.' target not match');
return $result;
}
$result[0] = true;
return $result;
}
function targets_matched($targets, $browser_save, $is_detail=false)
{
$matched = false;
if (empty($targets)) {
omp_trace('targets empty.');
return true;
}
foreach($targets as $target) {
do {
$key_names = ['ismobiledevice', 'browser', 'platform', 'device_name', 'region', 'language'];
if (!match_normal($target, $browser_save, $key_names)) {
omp_trace('browser matched failure');
break;
}
//正则表达式匹配UA,如果不匹配则这条任务略过
$UA = $browser_save['UserAgent'].@$browser_save['XRequestWith'];
if (!match_regex($UA, $target['UserAgent'])) {
omp_trace('UserAgent substr: '.$UA.'!='.$target['UserAgent']);
break;
}
//如果是详细匹配,要包括实时的Visiting和Account
if ($is_detail) {
$key_names = ['is_first_day', 'is_first_session','is_first_pageview','is_registed'];
if (!match_normal($target, $browser_save, $key_names)) {
omp_trace('user matched failure');
break;
}
//正访问网址的正则匹配
if (!match_regex($browser_save['Visiting'],$target['Visiting'])){
omp_trace('target : '.$browser_save['Visiting'].'!='.$target['Visiting']);
break;
}
//检查关键字匹配
$key_names = ['kword_account', 'kword_title','kword_submit','kword_cart', 'kword_favorite'];
if (!match_keyword($target, $browser_save, $key_names)) {
//todo
}
//访问停留时间, 访问页面次数, 总访问页面次数, 来访次数
$key_names = ['staytime','session_pv','pageview_count', 'session_count'];
if (!match_range($browser_save, $target, $key_names)) {
omp_trace('match range: failure');
break;
}
}
//过关斩将,最后匹配成功了
$matched = $target;
omp_trace('match: '.$target['name']);
break 2;
} while(false);
}
return $matched;
}
function match_normal($target, $browser_save, $keys)
{
foreach ($keys as $key_name) {
$from_device = @$browser_save[$key_name];
$from_config = @$target[$key_name];
if (empty($from_config)) {
continue;
}
//执行bool类型的命令匹配
if (is_bool($from_device)) {
if (!match_bool($from_device, $from_config)) {
omp_trace("$key_name not match: ".$from_device.'!='. $from_config);
return false;
} else {
}
//执行子字符串的命令匹配
} elseif (is_string($from_device)) {
if (is_string($from_config)) {
if (!match_substr($from_device, $from_config)) {
omp_trace("$key_name not substr: ".$from_device.'!='. $from_config);
return false;
} else {
}
//如果配置是数据,则逐个匹配
} elseif (is_array($from_config)) {
$found = false;
foreach($from_config as $conf) {
if (match_substr($from_device, $conf)) {
$found = true;
break;
}
}
if (!$found) {
omp_trace("$key_name not substr: ".$from_device.'!='. implode(',', $from_config));
return false;
}
} else {
}
} elseif (is_null($from_device)) {
if ($from_config !== '--') {
return false;
}
} else {
}
}
return true;
}
function match_keyword($from_device, $from_config)
{
}
function match_regex($from_device, $from_config)
{
if ($from_config === '--') {
return true;
}
if (empty($from_config)) {
return true;
}
if (preg_match($from_config, $from_device)) {
return true;
}
return false;
}
function match_substr($from_device, $from_config)
{
if ($from_config === '--') {
return true;
}
if (empty($from_device)) {
return false;
}
$from_device = strtolower($from_device);
$from_config = strtolower($from_config);
$sub_found = false;
$subitems = explode(' ', $from_config);
foreach($subitems as $subitem) {
if (false !== strpos($from_device, $subitem)) {
$sub_found = true;
break;
}
}
return $sub_found;
}
function match_bool($from_device, $from_config)
{
if (empty($from_config)) {return true;}
if ($from_config === '--') {return true;}
if (($from_config === 'true') xor $from_device) {return false;}
return true;
}
function match_range($target, $browser_save, $keys)
{
foreach ($keys as $key_name) {
$from_device = @$browser_save[$key_name];
$from_config = @$target[$key_name];
if (empty($from_config)) {
continue;
}
if (!match_range_val($from_device, $from_config)) {
omp_trace('match range: '.$from_device.'!='.implode(',', $from_config));
return false;
}
}
return true;
}
function match_range_val($current_val, $range_input)
{
$current_val = intval($current_val);
if (is_string($range_input)) {
if ($range_input === '--') {
return true;
}
$ranges = explode(',', $range_input);
foreach($ranges as $range_item) {
$range_pair = explode('-', $range_item);
if (count($range_pair)!==2) {
continue;
}
if (($current_val>=$range_pair[0]) && ($current_val<=$range_pair[1])) {
return true;
}
}
} elseif (is_array($range_input)) {
if (($current_val>=$range_input[0]) && ($current_val<=$range_input[1])) {
return true;
}
}
return false;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
function remake_msgque(&$task_info, &$task)
{
$make_step = count($task['messages']);
$make_count = $task['times'];
$make_mode = $task['msg_sequence'];
$task_info['msg_queue'] = make_msgque($make_step, $make_count, $make_mode);
$task_info['ori_seq'] = $make_mode;
$task_info['ori_times'] = $make_count;
$task_info['ori_msglen'] = $make_step;
}
function make_simple_queue($msg_count, $mode)
{
if ($msg_count === 0) {return [];}
if ($msg_count === 1) {return [0];}
$template = range(0, $msg_count-1);
switch($mode) {
case 'confusion': ;
shuffle($template);
$result = [];
$count = count($template);
while($msg_count) {
$result[] = $template[rand(0, $count-1)];
$msg_count -= 1;
}
return $result;
case 'random': shuffle($template);
case 'sequence':return $template;
default: return $template;
}
}
function fill_replace_queue($seq_mode, $used_queue, $task_msg_count)
{
$sample_queue =[];
$iter_queue = $used_queue;
do {
if (!empty($iter_queue)) {
foreach ($iter_queue as $index) {
if (!in_array($index, $sample_queue)) {
$sample_queue[] = $index;
}
}
}
if (count($sample_queue) >= $task_msg_count) {
break;
}
$iter_queue = make_simple_queue($task_msg_count, $seq_mode);
foreach ($iter_queue as $index) {
$used_queue[] = $index;
}
} while (true);
return $used_queue;
}
function make_msgque($step, $count, $mode)
{
$source_sequence = ['sequence', 'random', 'confusion'];
if (!in_array($mode, $source_sequence)) {
$mode= 'sequence';
}
$template = range(0, $count-1);
$result = [];
foreach ($template as $num) {
$result[] = $num % $step;
}
if ($mode === 'sequence') {
return $result;
}
elseif ($mode === 'random') {
$output = [];
do {
$sample = range(0, $step-1);
shuffle($sample);
$output = array_merge($output, $sample);
}while(count($output)<$count);
$output = array_slice($output, 0, $count);
return $output;
}
elseif ($mode === 'confusion') {
shuffle($result);
return $result;
}
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
function api_open_mmc()
{
$mem = new NSMemcached(API_MEMC_POOL);
$ss = $mem->getServerList();
if (empty($ss)) {
$mem->addServer(MEMC_HOST, MEMC_PORT);
}
return $mem;
}
function get_browser_cached($useragent)
{
$digest = md5($useragent);
$browser_file= browser_file($digest);
if (file_exists($browser_file)) {
$saved = unserialize(file_get_contents($browser_file));
} else {
$browser_o = get_browser($useragent);
$device_name = get_device_name($useragent);
$saved = [
'get_browser' => $browser_o,
'get_device_name' => $device_name,
];
file_put_contents($browser_file, serialize($saved));
}
return [$saved['get_browser'], $saved['get_device_name']];
}
function get_locale_mem($ip)
{
$mem = api_open_mmc();
if ($city_name = $mem->ns_get(GET_DEVICE, $ip)) {
return $city_name;
}
$city_name = get_city_name($ip);
if (empty($city_name)) {
$city_name = $ip;
}
$mem->ns_set(GET_LOCALE, $ip, $city_name, GET_LOCALE_EXPIRE);
return $city_name;
}
function get_accept_language()
{
$ori = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (empty($ori)) {
return '';
}
$arr = explode(',', $ori);
if (count($arr)>0) {
return $arr[0];
}
return '';
}
function gen_uuid() {
return sprintf( '%04x%04x%04x%04x%04x%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
function get_device()
{
$device = isset($_COOKIE[COOKIE_DEVICE_ID]) ? $_COOKIE[COOKIE_DEVICE_ID] : null;
$device_get = isset($_GET['device']) ? $_GET['device'] : null;
return ($device_get)? $device_get : $device ;
}
function browser_file($digest)
{
return BROWSER_CACHE_PATH.'/'.$digest.'.browser';
}
function device_file($device)
{
return DEVICE_SAVED_PATH.'/'.$device.'.device';
}
function asyncmsg_file($device)
{
return ASYNC_MSG_PATH.'/'.$device.'.msgs';
}
function title_file($md5)
{
return TITLE_CACHE_PATH.'/'.$md5.'.title';
}
function get_ua_infos()