-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.php
119 lines (104 loc) · 3.06 KB
/
images.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
<?php
error_reporting(E_ERROR);
require_once 'imgdata.php';
$karnc = new imgdata();
/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param array $files
* @return array
*/
function getfiles($path, $allowFiles, &$files = array())
{
if (!is_dir($path)) {
return null;
}
if (substr($path, strlen($path) - 1) != '/') {
$path .= '/';
}
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $path . $file;
if (is_dir($path2)) {
getfiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
$files[] = substr($path2, strlen($_SERVER['DOCUMENT_ROOT']));
}
}
}
}
return $files;
}
/**
* 域名白名单校验函数
* @param $domain_list
* @return true/false
*/
function checkReferer($domain_list = array(
'szx.life',
'www.szx.life',
))
{
$status = false;
$refer = $_SERVER['HTTP_REFERER']; //前一URL
if ($refer) {
$referhost = parse_url($refer);
/**来源地址主域名**/
$host = strtolower($referhost['host']);
if ($host == $_SERVER['HTTP_HOST'] || in_array($host, $domain_list)) {
$status = true;
}
}
return $status;
}
//列出指定目录下的图片
$CONFIG = array();
$CONFIG['imageManagerAllowFiles'] = array(".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp");
$base_Path = '/wp-content/random-image/picture/'; //默认主目录
$category = 'a'; //默认分类目录
if ($_GET['folder']) {
$folder = trim($_GET['folder']);
$CONFIG['imageManagerListPath'] = $base_Path . $folder . '/'; //有GET访问的分类目录
} else {
$CONFIG['imageManagerListPath'] = $base_Path . $category . '/'; //无GET访问的默认目录
}
$allowFiles = $CONFIG['imageManagerAllowFiles'];
$path = $CONFIG['imageManagerListPath'];
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
//获取文件列表
$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
$files = getfiles($path, $allowFiles);
if (!count(array($path))) {
return "抱歉,没有找到匹配的文件!";
}
//获取指定范围的列表
$len = count($files);
for ($i = 0, $list = array(); $i < $len; $i++) {
$list[] = $files[$i];
}
$rand = array_rand($list, 1);
$img = $list[$rand];
$imgFile = $_SERVER['DOCUMENT_ROOT'] . (substr($list[$rand], 0, 1) == "/" ? "" : "/") . $img;
$imgNot = $_SERVER['DOCUMENT_ROOT'] . '/' . 'nico.gif'; //无授权域名图片
$refer = $_SERVER['HTTP_REFERER']; //前一URL
//存在前一URL
if ($refer) {
if (!checkReferer()) {
$karnc->getdir($imgNot);
$karnc->img2data();
$karnc->data2img();
die;
} else {
$karnc->getdir($imgFile);
$karnc->img2data();
$karnc->data2img();
die;
}
} else {
//直接访问API地址
$imgWeb = file_get_contents('imgweb.html');
echo $imgWeb;
die;
}