pixel-icon-web/php/download.php

89 lines
2.5 KiB
PHP

<?php
if (empty($_GET['icon']) || !preg_match("/^[\w\-]+(\.svg)?$/", $_GET['icon'])) exit;
$request = $_GET['icon'];
$icons_path = __DIR__."/icons";
function get_ip() {
//whether ip is from the share internet
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from the proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from the remote address
else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$time = date("Y-m-d H:i:s");
$ip = get_ip();
if ($request == 'all') {
file_put_contents('download_log', "$time - $ip - download all".PHP_EOL , FILE_APPEND | LOCK_EX);
$mtime = filemtime(__DIR__.'/assets/index.js');
$zip_filename = "pixel-icons-$mtime.zip";
$zip_path = __DIR__."/zipped/$zip_filename";
if (!is_file($zip_path)) {
$zip = new ZipArchive();
if ($zip->open($zip_path, ZipArchive::CREATE) === TRUE) {
$files = scandir($icons_path);
foreach ($files as $file) {
$file_path = $icons_path."/$file";
if (substr($file, -4, 4) == ".svg") {
touch($file_path);
$zip->addFile($file_path, $file);
}
}
$zip->close();
} else {
echo "Oops";
exit;
}
}
if (is_file($zip_path)) {
header('Content-Type: application/zip');
$quoted = sprintf('"%s"', addcslashes(basename($zip_path), '"\\'));
$size = filesize($zip_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile($zip_path);
}
} else {
file_put_contents('download_log', "$time - $ip - $request".PHP_EOL , FILE_APPEND | LOCK_EX);
$file = $icons_path . "/$request";
if (!is_file($file)) exit;
header('Content-Type: image/svg+xml');
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile($file);
}