forked from YieldRay/Random-Picture
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YieldRay
committed
Mar 25, 2022
1 parent
8e5fdd4
commit 1ea0613
Showing
6 changed files
with
220 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,54 @@ | ||
<?php | ||
const ALLOW_OUTPUT = false; // 修改以开启 | ||
const ERROR_IMG = [ | ||
404 => 'https://http.cat/404', | ||
503 => 'https://http.cat/503' | ||
]; | ||
|
||
const ALLOW_RAW_OUTPUT = true; | ||
// 是否开启 ?raw 选项,可能会消耗服务器较多流量 | ||
|
||
function has_query($query) | ||
{ | ||
return isset($_GET[$query]); | ||
} | ||
if (file_exists('../url.csv')) { | ||
$url = file('../url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
$imgs_array = file('../url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
} else { | ||
$url = file('http://' . $_SERVER['HTTP_HOST'] . '/url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
$imgs_array = file('http://' . $_SERVER['HTTP_HOST'] . '/url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
} | ||
|
||
if (empty($url[0])) { | ||
$code = 503; | ||
$target_url = ERROR_IMG[503]; | ||
if (count($imgs_array) == 0) { | ||
$imgs_array = ['https://http.cat/503']; | ||
} | ||
|
||
|
||
$id = $_REQUEST['id']; | ||
$type = $_REQUEST['type']; | ||
$length = count($url); | ||
$final_id = array_rand($url); | ||
$is_idValid = false; | ||
|
||
if (isset($id)) { | ||
$id = 0; | ||
if (is_numeric($_GET['id'])) { | ||
header('Cache-Control: public, max-age=86400'); | ||
if (is_numeric($id)) { | ||
settype($id, 'integer'); | ||
if ($is_idValid = is_int($id)) { | ||
$final_id = $id; // id是整数 | ||
} | ||
global $id; | ||
$id = $_GET['id']; | ||
settype($id, 'int'); | ||
$len = count($imgs_array); | ||
if ($id >= $len || $id < 0) { | ||
$id = array_rand($imgs_array); | ||
} | ||
} else { | ||
header('Cache-Control: no-cache'); | ||
$id = array_rand($imgs_array); | ||
} | ||
|
||
|
||
|
||
if (!$code && $is_idValid && $final_id > $length) { | ||
// 超过最大数 | ||
$code = 404; | ||
$target_url = ERROR_IMG[404]; | ||
} else { | ||
$code = 200; | ||
$target_url = $url[$final_id]; | ||
if (has_query('json')) { | ||
header('Access-Control-Allow-Origin: *'); | ||
header('Content-Type: application/json'); | ||
echo json_encode(['id' => $id, 'url' => $imgs_array[$id]]); | ||
exit(); | ||
} | ||
|
||
/** | ||
* Variables for OUTPUT | ||
* $code | ||
* $target_url final url | ||
* $length amount of images | ||
*/ | ||
header('Access-Control-Max-Age: 86400'); // 1day | ||
header('Access-Control-Allow-Origin: *'); | ||
|
||
switch ($type) { | ||
case 'length': | ||
echo $length; | ||
break; | ||
case 'json': | ||
$result = [ | ||
'code' => $code, | ||
'url' => $target_url | ||
]; | ||
header('Content-Type: text/json'); | ||
echo json_encode($result); | ||
break; | ||
case 'JSON': | ||
$result = [ | ||
'code' => $code, | ||
'url' => $target_url | ||
]; | ||
$imageInfo = getimagesize($target_url); | ||
$imageSize = get_headers($target_url, 1)['Content-Length']; | ||
$result['width'] = $imageInfo[0]; | ||
$result['height'] = $imageInfo[1]; | ||
$result['mime'] = $imageInfo['mime']; | ||
$result['size'] = $imageSize; | ||
header('Content-Type: text/json'); | ||
echo json_encode($result); | ||
break; | ||
case 'output': | ||
header($header); | ||
if (ALLOW_OUTPUT) { | ||
header('Content-Type: image/png'); | ||
echo file_get_contents($target_url); | ||
} else { | ||
die('disabled'); | ||
} | ||
break; | ||
default: | ||
header('Location: ' . $target_url); | ||
if (has_query('raw')) { | ||
if (!ALLOW_RAW_OUTPUT) { | ||
header('HTTP/1.1 403 Forbidden'); | ||
exit(); | ||
} | ||
header('Content-Type: image/png'); | ||
echo file_get_contents($imgs_array[$id]); | ||
exit(); | ||
} else { | ||
header('Location: ' . $imgs_array[$id]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.