sugoi/static/php/includes/Upload.class.php

207 lines
5.8 KiB
PHP
Raw Normal View History

2022-01-22 02:09:47 +00:00
<?php
/*
* Uguu
*
2022-01-22 16:24:09 +00:00
* @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
2022-01-22 02:09:47 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
require_once 'Core.namespace.php';
use Core\Database as Database;
use Core\Settings as Settings;
class Upload
{
2022-01-22 02:09:47 +00:00
public static string $FILE_NAME;
2022-01-29 20:11:12 +00:00
public static mixed $FILE_EXTENSION;
2022-01-22 02:09:47 +00:00
public static string $FILE_MIME;
public static string $SHA1;
public static string $NEW_NAME;
public static string $NEW_NAME_FULL;
2022-01-29 20:11:12 +00:00
public static mixed $IP;
2022-01-22 02:09:47 +00:00
public static string $FILE_SIZE;
public static string $TEMP_FILE;
2022-01-23 11:33:37 +00:00
public static function reFiles($files): array
{
$result = [];
$files = self::diverseArray($files);
foreach ($files as $file) {
self::$FILE_NAME = $file['name'];
self::$FILE_SIZE = $file['size'];
self::$TEMP_FILE = $file['tmp_name'];
2022-01-22 21:04:09 +00:00
self::$SHA1 = sha1_file(self::$TEMP_FILE);
$result[] = [self::$FILE_NAME, self::$FILE_SIZE, self::$TEMP_FILE, self::$SHA1];
}
return $result;
}
2022-01-23 11:33:37 +00:00
public static function diverseArray($files): array
{
$result = [];
foreach ($files as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
$result[$key2][$key1] = $value2;
}
}
return $result;
}
/**
* @throws Exception
*/
2022-01-23 11:33:37 +00:00
public static function uploadFile(): array
2022-01-22 02:09:47 +00:00
{
2022-01-23 11:33:37 +00:00
Settings::loadConfig();
self::fileInfo();
2022-01-22 21:38:39 +00:00
if (Settings::$BLACKLIST_DB) {
2022-01-23 11:33:37 +00:00
Database::checkFileBlacklist();
2022-01-22 02:09:47 +00:00
}
2022-01-22 21:38:39 +00:00
if (Settings::$FILTER_MODE) {
self::checkMimeBlacklist();
2022-01-29 20:00:53 +00:00
if(!is_null(self::$FILE_EXTENSION)){
self::checkExtensionBlacklist();
}
2022-01-22 21:38:39 +00:00
}
2022-01-22 21:38:39 +00:00
if (Settings::$ANTI_DUPE) {
2022-01-23 11:33:37 +00:00
Database::antiDupe();
2022-01-22 21:38:39 +00:00
}
if (!Settings::$ANTI_DUPE) {
2022-01-23 11:33:37 +00:00
self::generateName();
2022-01-22 21:38:39 +00:00
}
2022-01-22 02:09:47 +00:00
if (!is_dir(Settings::$FILES_ROOT)) {
throw new Exception('File storage path not accessible.', 500);
}
if (!move_uploaded_file(self::$TEMP_FILE, Settings::$FILES_ROOT . self::$NEW_NAME_FULL)) {
throw new Exception('Failed to move file to destination', 500);
2022-01-22 02:09:47 +00:00
}
if (!chmod(Settings::$FILES_ROOT . self::$NEW_NAME_FULL, 0644)) {
throw new Exception('Failed to change file permissions', 500);
2022-01-22 02:09:47 +00:00
}
2022-01-23 11:33:37 +00:00
Database::newIntoDB();
2022-01-22 02:09:47 +00:00
if (Settings::$SSL) {
$preURL = 'https://';
} else {
$preURL = 'http://';
}
return [
2022-01-22 02:09:47 +00:00
'hash' => self::$SHA1,
'name' => self::$FILE_NAME,
'url' => $preURL . Settings::$URL . '/' . rawurlencode(self::$NEW_NAME_FULL),
2022-01-22 02:09:47 +00:00
'size' => self::$FILE_SIZE
];
2022-01-22 02:09:47 +00:00
}
2022-01-22 21:38:39 +00:00
2022-01-29 19:03:35 +00:00
public static function getIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
self::$IP = $_SERVER['HTTP_CLIENT_IP'];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
self::$IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
if (!isset(self::$IP)) {
self::$IP = $_SERVER['REMOTE_ADDR'];
}
}
2022-01-23 11:33:37 +00:00
public static function fileInfo()
2022-01-22 18:50:06 +00:00
{
if (isset($_FILES['files'])) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
self::$FILE_MIME = finfo_file($finfo, self::$TEMP_FILE);
finfo_close($finfo);
2022-01-22 02:09:47 +00:00
2022-01-29 19:03:35 +00:00
$extension = explode('.', self::$FILE_NAME);
2022-01-29 20:00:53 +00:00
if(substr_count(self::$FILE_NAME, '.') === 0) {
self::$FILE_EXTENSION = null;
} elseif(substr_count(self::$FILE_NAME, '.') > 1) {
self::$FILE_EXTENSION = $extension[count($extension)-2].'.'.$extension[count($extension)-1];
} else {
self::$FILE_EXTENSION = $extension[count($extension)-1];
}
2022-01-29 19:03:35 +00:00
2022-01-22 18:50:06 +00:00
if (Settings::$LOG_IP) {
2022-01-29 19:03:35 +00:00
self::getIP();
2022-01-22 18:50:06 +00:00
} else {
2022-01-29 19:03:35 +00:00
self::$IP = null;
2022-01-22 18:50:06 +00:00
}
}
}
2022-01-22 21:38:39 +00:00
/**
* @throws Exception
*/
2022-01-23 11:33:37 +00:00
public static function checkMimeBlacklist()
2022-01-22 02:09:47 +00:00
{
2022-01-22 21:38:39 +00:00
if (in_array(self::$FILE_MIME, Settings::$BLOCKED_MIME)) {
throw new Exception('Filetype not allowed.', 415);
}
}
/**
2022-01-22 23:38:00 +00:00
* Check if file extension is blacklisted
* if it does throw an exception.
*
2022-01-22 21:38:39 +00:00
* @throws Exception
*/
2022-01-23 11:33:37 +00:00
public static function checkExtensionBlacklist()
2022-01-22 21:38:39 +00:00
{
if (in_array(self::$FILE_EXTENSION, Settings::$BLOCKED_EXTENSIONS)) {
throw new Exception('Filetype not allowed.', 415);
}
}
2022-01-22 02:09:47 +00:00
2022-01-22 21:38:39 +00:00
/**
* @throws Exception
*/
2022-01-23 11:33:37 +00:00
public static function generateName()
2022-01-22 21:38:39 +00:00
{
2022-01-22 02:09:47 +00:00
do {
if (Settings::$FILES_RETRIES === 0) {
throw new Exception('Gave up trying to find an unused name!', 500);
2022-01-22 02:09:47 +00:00
}
self::$NEW_NAME = '';
2022-01-22 02:09:47 +00:00
for ($i = 0; $i < Settings::$NAME_LENGTH; ++$i) {
self::$NEW_NAME .= Settings::$ID_CHARSET[mt_rand(0, strlen(Settings::$ID_CHARSET))];
}
2022-01-29 20:00:53 +00:00
self::$NEW_NAME_FULL = self::$NEW_NAME;
if (!is_null(self::$FILE_EXTENSION)) {
2022-01-22 21:38:39 +00:00
self::$NEW_NAME_FULL .= '.' . self::$FILE_EXTENSION;
2022-01-22 02:09:47 +00:00
}
2022-01-29 20:00:53 +00:00
2022-01-23 11:33:37 +00:00
} while (Database::dbCheckNameExists() > 0);
2022-01-22 02:09:47 +00:00
}
2022-01-22 21:38:39 +00:00
}