YGGo/public/file.php

138 lines
4.0 KiB
PHP
Raw Normal View History

2023-05-15 06:18:18 +00:00
<?php
2023-06-30 11:38:29 +00:00
require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/icon.php');
require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/ftp.php');
2023-05-15 06:18:18 +00:00
$type = !empty($_GET['type']) ? $_GET['type'] : false;
switch ($type) {
case 'identicon':
$query = md5($_GET['query']);
$width = isset($_GET['width']) ? (int) $_GET['width'] : 16;
$height = isset($_GET['height']) ? (int) $_GET['height'] : 16;
$radius = isset($_GET['radius']) ? (int) $_GET['radius'] : 0;
header('Content-Type: image/webp');
if (WEBSITE_IDENTICON_IMAGE_CACHE) {
2023-06-30 11:38:29 +00:00
$filename = __DIR__ . '/../storage/cache/' . $query . '.webp';
2023-05-15 06:18:18 +00:00
if (!file_exists($filename)) {
$icon = new Icon();
file_put_contents($filename, $icon->generateImageResource($query, $width, $height, false, $radius));
}
echo file_get_contents($filename);
} else {
$icon = new Icon();
echo $icon->generateImageResource($query, $width, $height, false, $radius);
}
break;
case 'snap':
// Connect database
2023-08-05 16:39:49 +00:00
try {
2023-05-15 06:18:18 +00:00
2023-08-05 16:39:49 +00:00
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
} catch(Exception $e) {
var_dump($e);
exit;
}
// Init request
$crc32ip = crc32(!empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
2023-05-15 06:18:18 +00:00
// Get snap details from DB
if ($hostPageSnap = $db->getHostPageSnap(!empty($_GET['hps']) ? (int) $_GET['hps'] : 0)) {
2023-07-31 10:33:30 +00:00
// Prepare filenames
$hostPageSnapPath = 'hps/' . substr(trim(chunk_split($hostPageSnap->hostPageSnapId, 1, '/'), '/'), 0, -1);
$hostPageSnapFile = $hostPageSnapPath . substr($hostPageSnap->hostPageSnapId, -1) . '.zip';
2023-07-30 11:46:07 +00:00
// Get snap file
2023-07-31 10:33:30 +00:00
foreach (json_decode(SNAP_STORAGE) as $node => $storages) {
2023-05-15 06:18:18 +00:00
2023-07-31 10:33:30 +00:00
foreach ($storages as $location => $storage) {
2023-05-15 06:18:18 +00:00
// Generate storage id
2023-07-31 10:33:30 +00:00
$crc32name = crc32(sprintf('%s.%s', $node, $location));
2023-05-15 06:18:18 +00:00
2023-07-30 20:32:02 +00:00
if ($hostPageSnapStorage = $db->findHostPageSnapStorageByCRC32Name($hostPageSnap->hostPageSnapId, $crc32name)) {
2023-05-15 06:18:18 +00:00
2023-07-31 10:33:30 +00:00
switch ($node) {
2023-05-15 06:18:18 +00:00
2023-07-30 11:39:21 +00:00
case 'localhost':
2023-05-15 06:18:18 +00:00
// Download local snap in higher priority if possible
2023-07-31 10:33:30 +00:00
if (file_exists($storage->directory . $hostPageSnapFile) &&
is_readable($storage->directory . $hostPageSnapFile)) {
2023-05-15 06:18:18 +00:00
// Register snap download
$db->addHostPageSnapDownload($hostPageSnapStorage->hostPageSnapStorageId, $crc32ip, time());
2023-05-15 06:18:18 +00:00
// Return snap file
header('Content-Type: application/zip');
2023-07-31 10:33:30 +00:00
header(sprintf('Content-Length: %s', filesize($storage->directory . $hostPageSnapFile)));
header(sprintf('Content-Disposition: filename="snap.%s.zip"', $hostPageSnap->hostPageSnapId));
readfile($storage->directory . $hostPageSnapFile);
2023-05-15 06:18:18 +00:00
exit;
}
2023-05-15 06:18:18 +00:00
2023-07-30 11:39:21 +00:00
break;
case 'ftp':
2023-05-15 06:18:18 +00:00
$ftp = new Ftp();
2023-05-15 06:18:18 +00:00
if ($ftp->connect($storage->host, $storage->port, $storage->username, $storage->password, $storage->directory, $storage->timeout, $storage->passive)) {
2023-05-15 06:18:18 +00:00
// Register snap download
$db->addHostPageSnapDownload($hostPageSnapStorage->hostPageSnapStorageId, $crc32ip, time());
2023-05-15 06:18:18 +00:00
// Return snap file
header('Content-Type: application/zip');
2023-07-31 10:33:30 +00:00
header(sprintf('Content-Length: %s', $ftp->size($hostPageSnapFile)));
header(sprintf('Content-Disposition: filename="snap.%s.zip"', $hostPageSnap->hostPageSnapId));
2023-05-15 06:18:18 +00:00
2023-07-31 10:33:30 +00:00
$ftp->get($hostPageSnapFile, 'php://output');
2023-05-15 06:18:18 +00:00
2023-07-29 13:00:04 +00:00
$ftp->close();
exit;
}
2023-05-15 06:18:18 +00:00
2023-07-30 11:39:21 +00:00
break;
}
}
2023-05-15 06:18:18 +00:00
}
}
}
2023-05-15 06:18:18 +00:00
header('HTTP/1.0 404 Not Found');
2023-05-15 06:18:18 +00:00
echo _('404 Snap not found');
2023-05-15 06:18:18 +00:00
break;
2023-05-15 06:18:18 +00:00
default:
header('HTTP/1.0 404 Not Found');
echo _('404');
}