mirror of
https://github.com/nokonoko/Uguu.git
synced 2024-01-06 13:35:15 +00:00
major code cleanup and more error checking added.
This commit is contained in:
parent
8220242883
commit
4c21cfa0dc
@ -24,6 +24,7 @@ namespace Core {
|
|||||||
|
|
||||||
require_once 'Upload.class.php';
|
require_once 'Upload.class.php';
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use PDO;
|
use PDO;
|
||||||
use Upload as Upload;
|
use Upload as Upload;
|
||||||
|
|
||||||
@ -55,10 +56,13 @@ namespace Core {
|
|||||||
public static array $BLOCKED_MIME;
|
public static array $BLOCKED_MIME;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public static function loadConfig()
|
public static function loadConfig()
|
||||||
{
|
{
|
||||||
if (!file_exists('/Users/go.johansson/PERSONAL_REPOS/Uguu/dist.json')) {
|
if (!file_exists('/Users/go.johansson/PERSONAL_REPOS/Uguu/dist.json')) {
|
||||||
throw new \Exception('Cant read settings file.', 500);
|
throw new Exception('Cant read settings file.', 500);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$settings_array = json_decode(
|
$settings_array = json_decode(
|
||||||
@ -82,8 +86,8 @@ namespace Core {
|
|||||||
self::$BLOCKED_EXTENSIONS = $settings_array['BLOCKED_EXTENSIONS'];
|
self::$BLOCKED_EXTENSIONS = $settings_array['BLOCKED_EXTENSIONS'];
|
||||||
self::$BLOCKED_MIME = $settings_array['BLOCKED_MIME'];
|
self::$BLOCKED_MIME = $settings_array['BLOCKED_MIME'];
|
||||||
self::$DOUBLE_DOTS = $settings_array['DOUBLE_DOTS'];
|
self::$DOUBLE_DOTS = $settings_array['DOUBLE_DOTS'];
|
||||||
} catch (\Exception $e) {
|
} catch (Exception) {
|
||||||
throw new \Exception('Cant populate settings.', 500);
|
throw new Exception('Cant populate settings.', 500);
|
||||||
}
|
}
|
||||||
(new Database())->assemblePDO();
|
(new Database())->assemblePDO();
|
||||||
}
|
}
|
||||||
@ -112,30 +116,10 @@ namespace Core {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The Response class is a do-it-all for getting responses out in different
|
|
||||||
* formats.
|
|
||||||
*
|
|
||||||
* @todo Create sub-classes to split and extend this god object.
|
|
||||||
*/
|
|
||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
/**
|
private mixed $type;
|
||||||
* Indicates response type used for routing.
|
|
||||||
*
|
|
||||||
* Valid strings are 'csv', 'html', 'json' and 'text'.
|
|
||||||
*
|
|
||||||
* @var string Response type
|
|
||||||
*/
|
|
||||||
private $type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates requested response type.
|
|
||||||
*
|
|
||||||
* Valid strings are 'csv', 'html', 'json', 'gyazo' and 'text'.
|
|
||||||
*
|
|
||||||
* @param string|null $response_type Response type
|
|
||||||
*/
|
|
||||||
public function __construct($response_type = null)
|
public function __construct($response_type = null)
|
||||||
{
|
{
|
||||||
switch ($response_type) {
|
switch ($response_type) {
|
||||||
@ -167,14 +151,6 @@ namespace Core {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Routes error messages depending on response type.
|
|
||||||
*
|
|
||||||
* @param int $code HTTP status code number
|
|
||||||
* @param int $desc descriptive error message
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function error($code, $desc)
|
public function error($code, $desc)
|
||||||
{
|
{
|
||||||
$response = null;
|
$response = null;
|
||||||
@ -197,46 +173,17 @@ namespace Core {
|
|||||||
echo $response;
|
echo $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function csvError($description): string
|
||||||
* Indicates with CSV body the request was invalid.
|
|
||||||
*
|
|
||||||
* @param int $description descriptive error message
|
|
||||||
*
|
|
||||||
* @return string error message in CSV format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function csvError($description)
|
|
||||||
{
|
{
|
||||||
return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
|
return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function htmlError($code, $description): string
|
||||||
* Indicates with HTML body the request was invalid.
|
|
||||||
*
|
|
||||||
* @param int $code HTTP status code number
|
|
||||||
* @param int $description descriptive error message
|
|
||||||
*
|
|
||||||
* @return string error message in HTML format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function htmlError($code, $description)
|
|
||||||
{
|
{
|
||||||
return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
|
return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function jsonError($code, $description): bool|string
|
||||||
* Indicates with JSON body the request was invalid.
|
|
||||||
*
|
|
||||||
* @param int $code HTTP status code number
|
|
||||||
* @param int $description descriptive error message
|
|
||||||
*
|
|
||||||
* @return string error message in pretty-printed JSON format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function jsonError($code, $description)
|
|
||||||
{
|
{
|
||||||
return json_encode([
|
return json_encode([
|
||||||
'success' => false,
|
'success' => false,
|
||||||
@ -245,28 +192,12 @@ namespace Core {
|
|||||||
], JSON_PRETTY_PRINT);
|
], JSON_PRETTY_PRINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates with plain text body the request was invalid.
|
private static function textError($code, $description): string
|
||||||
*
|
|
||||||
* @param int $code HTTP status code number
|
|
||||||
* @param int $description descriptive error message
|
|
||||||
*
|
|
||||||
* @return string error message in plain text format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function textError($code, $description)
|
|
||||||
{
|
{
|
||||||
return 'ERROR: (' . $code . ') ' . $description;
|
return 'ERROR: (' . $code . ') ' . $description;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Routes success messages depending on response type.
|
|
||||||
*
|
|
||||||
* @param mixed[] $files
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function send($files)
|
public function send($files)
|
||||||
{
|
{
|
||||||
$response = null;
|
$response = null;
|
||||||
@ -290,16 +221,7 @@ namespace Core {
|
|||||||
echo $response;
|
echo $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function csvSuccess($files): string
|
||||||
* Indicates with CSV body the request was successful.
|
|
||||||
*
|
|
||||||
* @param mixed[] $files
|
|
||||||
*
|
|
||||||
* @return string success message in CSV format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function csvSuccess($files)
|
|
||||||
{
|
{
|
||||||
$result = '"name","url","hash","size"' . "\r\n";
|
$result = '"name","url","hash","size"' . "\r\n";
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
@ -312,16 +234,7 @@ namespace Core {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function htmlSuccess($files): string
|
||||||
* Indicates with HTML body the request was successful.
|
|
||||||
*
|
|
||||||
* @param mixed[] $files
|
|
||||||
*
|
|
||||||
* @return string success message in HTML format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function htmlSuccess($files)
|
|
||||||
{
|
{
|
||||||
$result = '';
|
$result = '';
|
||||||
|
|
||||||
@ -332,16 +245,7 @@ namespace Core {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function jsonSuccess($files): bool|string
|
||||||
* Indicates with JSON body the request was successful.
|
|
||||||
*
|
|
||||||
* @param mixed[] $files
|
|
||||||
*
|
|
||||||
* @return string success message in pretty-printed JSON format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function jsonSuccess($files)
|
|
||||||
{
|
{
|
||||||
return json_encode([
|
return json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
@ -349,16 +253,7 @@ namespace Core {
|
|||||||
], JSON_PRETTY_PRINT);
|
], JSON_PRETTY_PRINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static function textSuccess($files): string
|
||||||
* Indicates with plain text body the request was successful.
|
|
||||||
*
|
|
||||||
* @param mixed[] $files
|
|
||||||
*
|
|
||||||
* @return string success message in plain text format
|
|
||||||
* @deprecated 2.1.0 Will be renamed to camelCase format.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static function textSuccess($files)
|
|
||||||
{
|
{
|
||||||
$result = '';
|
$result = '';
|
||||||
|
|
||||||
@ -370,9 +265,11 @@ namespace Core {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Database
|
class Database
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public static function assemblePDO()
|
public static function assemblePDO()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -380,11 +277,14 @@ namespace Core {
|
|||||||
Settings::$DB_MODE . ':' . Settings::$DB_PATH, Settings::$DB_USER,
|
Settings::$DB_MODE . ':' . Settings::$DB_PATH, Settings::$DB_USER,
|
||||||
Settings::$DB_PASS
|
Settings::$DB_PASS
|
||||||
);
|
);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception) {
|
||||||
throw new \Exception('Cant connect to DB.', 500);
|
throw new Exception('Cant connect to DB.', 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function dbCheckNameExists()
|
public function dbCheckNameExists()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -392,11 +292,14 @@ namespace Core {
|
|||||||
$q->bindValue(':name', Upload::$NEW_NAME_FULL);
|
$q->bindValue(':name', Upload::$NEW_NAME_FULL);
|
||||||
$q->execute();
|
$q->execute();
|
||||||
return $q->fetchColumn();
|
return $q->fetchColumn();
|
||||||
} catch (\Exception $e) {
|
} catch (Exception) {
|
||||||
throw new \Exception('Cant check if name exists in DB.', 500);
|
throw new Exception('Cant check if name exists in DB.', 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function checkFileBlacklist()
|
public function checkFileBlacklist()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -405,13 +308,16 @@ namespace Core {
|
|||||||
$q->execute();
|
$q->execute();
|
||||||
$result = $q->fetch();
|
$result = $q->fetch();
|
||||||
if ($result['count'] > 0) {
|
if ($result['count'] > 0) {
|
||||||
throw new \Exception('File blacklisted!', 415);
|
throw new Exception('File blacklisted!', 415);
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (Exception) {
|
||||||
throw new \Exception('Cant check blacklist DB.', 500);
|
throw new Exception('Cant check blacklist DB.', 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function antiDupe()
|
public function antiDupe()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -425,11 +331,14 @@ namespace Core {
|
|||||||
if ($result['count'] > 0) {
|
if ($result['count'] > 0) {
|
||||||
Upload::$NEW_NAME_FULL = $result['filename'];
|
Upload::$NEW_NAME_FULL = $result['filename'];
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (Exception) {
|
||||||
throw new \Exception('Cant check for dupes in DB.', 500);
|
throw new Exception('Cant check for dupes in DB.', 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function newIntoDB()
|
public function newIntoDB()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -444,8 +353,8 @@ namespace Core {
|
|||||||
$q->bindValue(':date', time(), PDO::PARAM_STR);
|
$q->bindValue(':date', time(), PDO::PARAM_STR);
|
||||||
$q->bindValue(':ip', Upload::$IP, PDO::PARAM_STR);
|
$q->bindValue(':ip', Upload::$IP, PDO::PARAM_STR);
|
||||||
$q->execute();
|
$q->execute();
|
||||||
} catch (\Exception $e) {
|
} catch (Exception) {
|
||||||
throw new \Exception('Cant insert into DB.', 500);
|
throw new Exception('Cant insert into DB.', 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,10 @@ class Upload
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function uploadFile($file): array
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function uploadFile(): array
|
||||||
{
|
{
|
||||||
(new Settings())->loadConfig();
|
(new Settings())->loadConfig();
|
||||||
|
|
||||||
@ -73,7 +76,7 @@ class Upload
|
|||||||
(new Database())->antiDupe();
|
(new Database())->antiDupe();
|
||||||
}
|
}
|
||||||
|
|
||||||
(new Upload())->generateName($file);
|
(new Upload())->generateName();
|
||||||
|
|
||||||
|
|
||||||
if (!is_dir(Settings::$FILES_ROOT)) {
|
if (!is_dir(Settings::$FILES_ROOT)) {
|
||||||
@ -104,9 +107,12 @@ class Upload
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateName($file): string
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function generateName(): string
|
||||||
{
|
{
|
||||||
(new Upload())->fileInfo($file);
|
(new Upload())->fileInfo();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (Settings::$FILES_RETRIES === 0) {
|
if (Settings::$FILES_RETRIES === 0) {
|
||||||
@ -135,7 +141,7 @@ class Upload
|
|||||||
return self::$NEW_NAME_FULL;
|
return self::$NEW_NAME_FULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fileInfo($file)
|
public function fileInfo()
|
||||||
{
|
{
|
||||||
if (isset($_FILES['files'])) {
|
if (isset($_FILES['files'])) {
|
||||||
self::$SHA1 = sha1_file(self::$TEMP_FILE);
|
self::$SHA1 = sha1_file(self::$TEMP_FILE);
|
||||||
@ -159,6 +165,9 @@ class Upload
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function checkMimeBlacklist()
|
public function checkMimeBlacklist()
|
||||||
{
|
{
|
||||||
if (in_array(self::$FILE_MIME, Settings::$BLOCKED_MIME)) {
|
if (in_array(self::$FILE_MIME, Settings::$BLOCKED_MIME)) {
|
||||||
@ -166,6 +175,9 @@ class Upload
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function checkExtensionBlacklist()
|
public function checkExtensionBlacklist()
|
||||||
{
|
{
|
||||||
if (in_array(self::$FILE_EXTENSION, Settings::$BLOCKED_EXTENSIONS)) {
|
if (in_array(self::$FILE_EXTENSION, Settings::$BLOCKED_EXTENSIONS)) {
|
||||||
|
@ -28,9 +28,11 @@ if (isset($_FILES['files'])) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
foreach ($uploads as $upload) {
|
foreach ($uploads as $upload) {
|
||||||
$res[] = (new Upload())->uploadFile($upload);
|
$res[] = (new Upload())->uploadFile();
|
||||||
|
}
|
||||||
|
if (isset($res)) {
|
||||||
|
$response->send($res);
|
||||||
}
|
}
|
||||||
$response->send($res);
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$response->error($e->getCode(), $e->getMessage());
|
$response->error($e->getCode(), $e->getMessage());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user