odstranene stare nepoterbne PHP subory

- nepouzivane api, po novom je v backend zostavenie api pomocou TPsoft/APIlite
- subor funkcii
- vobec nepouyzivany index
- uz nepouyzivane config, novy je v subadresary /backend/config
This commit is contained in:
2025-10-11 22:02:41 +02:00
parent 948fce896f
commit fbfe46542b
4 changed files with 0 additions and 587 deletions

201
api.php
View File

@ -1,201 +0,0 @@
<?php
include_once 'config.php';
$action = $_REQUEST['action'];
$result = null;
$error = null;
switch ($action) {
default:
case 'help':
$result = help();
break;
case 'add':
$report_id = reportAdd($_REQUEST['title'], $_REQUEST['description'], $_REQUEST['status'], $_REQUEST['group'], $_REQUEST['priority']);
if ($report_id === false) $error = 'Report add failed';
$result = array('report_id' => $report_id);
break;
case 'update':
$suc = reportUpdate($_REQUEST['report_id'], json_decode($_REQUEST['report_data'], true));
if ($suc === false) $error = 'Update failed';
$result = array('processed' => $suc);
break;
case 'delete':
$suc = reportDelete($_REQUEST['report_id']);
if ($suc === false) $error = 'Update failed';
$result = array('processed' => $suc);
break;
case 'get':
$result = reportGet($_REQUEST['report_id']);
break;
case 'getAll':
$result = reportGetAll($_REQUEST['status']);
break;
case 'getAllGrouped':
$result = reportGetAllGrouped(json_decode($_REQUEST['status'], true), $_REQUEST['page'] == 'null' ? null : $_REQUEST['page']);
break;
case 'getArchived':
$result = reportGetArchived($_REQUEST['page'] == 'null' ? null : $_REQUEST['page']);
break;
case 'updateOrdNum':
$suc = reportUpdateOrdnum($_REQUEST['ordnums']);
if ($suc === false) $error = 'Update Ordnum failed';
$result = array('processed' => $suc);
break;
case 'updateStatus':
$suc = reportUpdateStatus($_REQUEST['report_id'], $_REQUEST['status']);
if ($suc === false) $error = 'Update Status failed';
$result = array('processed' => $suc);
break;
case 'attachmentAdd':
$suc = attachmentAdd($_REQUEST['report_id'], $_REQUEST['attachment_type'], $_REQUEST['attachment_content']);
if ($suc === false) $error = 'Attachment add failed';
$result = array('processed' => $suc);
break;
case 'attachmentUpdate':
$suc = attachmentUpdate($_REQUEST['attachment_id'], $_REQUEST['attachment_content']);
if ($suc === false) $error = 'Attachment update failed';
$result = array('processed' => $suc);
break;
case 'attachmentGetAll':
$result = attachmentGetAll($_REQUEST['report_id']);
break;
case 'attachmentDelete':
$suc = attachmentDelete($_REQUEST['attachment_id']);
if ($suc === false) $error = 'Attachment delete failed';
$result = array('processed' => $suc);
break;
}
header('Content-Type: application/json');
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept');
echo json_encode(
is_null($error)
? array('status' => 'OK', 'data' => $result)
: array('status' => 'ERROR', 'msg' => $error),
);
exit;
function help()
{
return [
'actions' => [
'help' => [
'name' => 'help',
'description' => 'Show this help',
'params' => []
],
'add' => [
'name' => 'add',
'description' => 'Add report',
'params' => [
'title' => 'Report title',
'description' => 'Report description',
'status' => 'Report status',
'group' => 'Report group',
'priority' => 'Report priority',
]
],
'update' => [
'name' => 'update',
'description' => 'Update report',
'params' => [
'report_id' => 'Report id',
'report_data' => [
'title' => 'Report title',
'description' => 'Report description',
'status' => 'Report status',
'group' => 'Report group',
'priority' => 'Report priority',
]
]
],
'delete' => [
'name' => 'delete',
'description' => 'Delete report',
'params' => [
'report_id' => 'Report id',
]
],
'get' => [
'name' => 'get',
'description' => 'Get report',
'params' => [
'report_id' => 'Report id',
]
],
'getAll' => [
'name' => 'getAll',
'description' => 'Get all reports',
'params' => [
'status' => '(ptional) Report status, default: 0,1,2,3',
]
],
'getAllGrouped' => [
'name' => 'getAllGrouped',
'description' => 'Get all reports grouped by group',
'params' => [
'status' => '(ptional) Report status, default: 0,1,2,3',
'page' => '(ptional) Page number, default: null = vsetky',
]
],
'getArchived' => [
'name' => 'getArchived',
'description' => 'Get archived reports',
'params' => [
'page' => '(ptional) Page number, default: null = vsetky',
]
],
'updateOrdNum' => [
'name' => 'updateordnum',
'description' => 'Update report ordnum',
'params' => [
'ordnums' => 'Report ordnums in json format {report_id: ordnum, ...}',
]
],
'updateStatus' => [
'name' => 'updatestatus',
'description' => 'Update report status',
'params' => [
'report_id' => 'Report id',
'status' => 'Report status',
]
],
'attachmentAdd' => [
'name' => 'attachmentAdd',
'description' => 'Add attachment to report',
'params' => [
'report_id' => 'Report id',
'content_type' => 'Attachment content type',
'content' => 'Attachment content',
]
],
'attachmentUpdate' => [
'name' => 'attachmentUpdate',
'description' => 'Update attachment',
'params' => [
'attachment_id' => 'Attachment id',
'content' => 'Attachment content; if empty, attachment will be deleted',
]
],
'attachmentGetAll' => [
'name' => 'attachmentGetAll',
'description' => 'Get all attachments for report',
'params' => [
'report_id' => 'Report id',
]
],
'attachmentGet' => [
'name' => 'attachmentGet',
'description' => 'Get attachment',
'params' => [
'attachment_id' => 'Attachment id',
]
]
]
];
}

View File

@ -1,31 +0,0 @@
<?php
if (file_exists('c:/php/includes/igor.php')) {
require_once 'c:/php/includes/igor.php';
}
require_once __DIR__.'/lib/functions.inc.php';
require_once __DIR__.'/lib/Medoo/src/Medoo.php';
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI']; // obsahuje aj query string
define('URL_PREFIX', $protocol.$host.str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']));
define('UPLOAD_DIR_ATTACHMENTS', __DIR__.'/data/attachments/');
if (!file_exists(UPLOAD_DIR_ATTACHMENTS)) {
mkdir(UPLOAD_DIR_ATTACHMENTS, 0777, true);
}
define('UPLOAD_URL_ATTACHMENTS', URL_PREFIX.'data/attachments/');
global $db;
$db = new Medoo\Medoo([
'type' => 'sqlite',
'database' => __DIR__ . '/data/database.db'
]);
dbCheck();
?>

View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BugReport</title>
</head>
<body>
</body>
</html>

View File

@ -1,344 +0,0 @@
<?php
/**
* String functions
*/
function allowedChars($str, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-')
{
return preg_match('/^[' . $chars . ']+$/', $str);
}
function sanitizeFilename($filename, $allowedExtensions = [])
{
// Rozdelenie názvu a prípony
$pathInfo = pathinfo($filename);
$name = $pathInfo['filename'] ?? 'file';
$extension = strtolower($pathInfo['extension'] ?? '');
// Odstránenie nebezpečných znakov z názvu
$name = preg_replace('/[^a-zA-Z0-9_-]/', '_', $name);
$name = substr($name, 0, 100); // voliteľné obmedzenie dĺžky
// Validácia prípony, ak je zoznam povolený
if (
$allowedExtensions
&& count($allowedExtensions) > 0
&& !in_array($extension, $allowedExtensions)
) {
$extension = 'bin'; // fallback ak prípona nie je povolená
}
return $name . '.' . $extension;
}
/**
* Check database
*/
function dbCheck()
{
global $db;
$db_version = option('version');
if ($db_version === null) {
$db->create('options', [
'key' => [
'VARCHAR(64)',
'NOT NULL',
'UNIQUE'
],
'value' => [
'TEXT',
'NOT NULL'
],
'created_at' => [
'DATETIME',
'DEFAULT CURRENT_TIMESTAMP'
]
]);
option('version', '0');
$db_version = '0';
}
if ($db_version === '0') {
$db->create('reports', [
'report_id' => [
'INTEGER',
'PRIMARY KEY',
'AUTOINCREMENT'
],
'report_title' => [
'VARCHAR(255)',
'DEFAULT NULL'
],
'report_description' => [
'TEXT',
'DEFAULT NULL'
],
'report_status' => [
'INTEGER',
'DEFAULT 0'
],
'report_group' => [
'VARCHAR(255)',
'DEFAULT NULL'
],
'report_priority' => [
'INTEGER',
'DEFAULT 0'
],
'created_dt' => [
'DATETIME',
'DEFAULT NULL'
],
]);
option('version', '1');
$db_version = '1';
}
if ($db_version === '1') {
$db->create('attachments', [
'attachment_id' => [
'INTEGER',
'PRIMARY KEY',
'AUTOINCREMENT'
],
'report_id' => [
'INTEGER',
'NOT NULL'
],
'attachment_type' => [
'VARCHAR(255)',
'DEFAULT NULL'
],
'attachment_content' => [
'TEXT',
'DEFAULT NULL'
],
'created_dt' => [
'DATETIME',
'DEFAULT NULL'
],
'updated_dt' => [
'DATETIME',
'DEFAULT NULL'
],
]);
option('version', '2');
$db_version = '2';
}
if ($db_version === '2') {
$db->query("ALTER TABLE reports ADD COLUMN ordnum INTEGER DEFAULT 0");
option('version', '3');
$db_version = '3';
}
}
function option($key, $value = null)
{
global $db;
if (tableExits('options') === null) {
return null;
}
if ($value === null) {
return $db->get('options', 'value', [
'key' => $key
]);
}
$exits = $db->get('options', 'value', [
'key' => $key
]);
if ($exits !== null) {
return $db->update('options', [
'value' => $value
], [
'key' => $key
]);
}
return $db->insert('options', [
'key' => $key,
'value' => $value
]);
}
function tableExits($table)
{
global $db;
return $db->get('sqlite_master', 'name', [
'type' => 'table',
'name' => $table
]);
}
/**
* Reports
*/
function reportAdd($title, $description, $status = 0, $group = null, $priority = 0)
{
global $db;
$status = intval($status);
$priority = intval($priority);
$db->insert('reports', [
'report_title' => $title,
'report_description' => $description,
'report_status' => $status,
'report_group' => $group,
'report_priority' => $priority,
'created_dt' => date('Y-m-d H:i:s')
]);
return $db->id();
}
function reportUpdate($report_id, $report_data)
{
global $db;
$stm = $db->update('reports', $report_data, [
'report_id' => $report_id
]);
return ($stm->rowCount() > 0);
}
function reportUpdateStatus($report_id, $status)
{
global $db;
$stm = $db->update('reports', [
'report_status' => $status
], [
'report_id' => $report_id
]);
return ($stm->rowCount() > 0);
}
function reportUpdateOrdnum($ordnums)
{
global $db;
$ordnums = json_decode($ordnums, true);
$suc = true;
foreach ($ordnums as $report_id => $ordnum) {
$stm = $db->update('reports', [
'ordnum' => $ordnum
], [
'report_id' => $report_id
]);
$suc &= ($stm->rowCount() > 0);
}
return $suc;
}
function reportDelete($report_id)
{
global $db;
$stm = $db->delete('reports', [
'report_id' => $report_id
]);
return ($stm->rowCount() > 0);
}
function reportGet($report_id)
{
global $db;
return $db->get('reports', '*', [
'report_id' => $report_id
]);
}
function reportGetAll($status = null, $page = null)
{
global $db;
if ($status === null) $status = array(0, 1, 2, 3);
$params = [
'ORDER' => ['report_priority' => 'DESC', 'ordnum' => 'ASC'],
'report_status' => $status
];
if ($page !== null) $params['LIMIT'] = [$page * 10, 10];
return $db->select('reports', '*', $params);
}
function reportGetAllGrouped($status = null, $page = null)
{
$all = reportGetAll($status, $page);
$groups = [];
foreach ($all as $report) {
$groups[$report['report_status']][] = $report;
}
return $groups;
}
function reportGetArchived($page = null)
{
global $db;
$params = [
'ORDER' => ['created_dt' => 'DESC'],
'report_status' => '4'
];
if ($page !== null) $params['LIMIT'] = [$page * 10, 10];
return $db->select('reports', '*', $params);
}
/**
* Attachments
*/
function attachmentGet($attachment_id)
{
global $db;
return $db->get('attachments', '*', [
'attachment_id' => $attachment_id
]);
}
function attachmentAdd($report_id, $attachment_type, $attachment_content)
{
global $db;
if ($attachment_type == 'file') {
$data = json_decode($attachment_content, true);
if (!is_array($data)) return false;
$base64 = preg_replace('/^data:.*?;base64,/', '', $data['base64']);
$base64_data = base64_decode($base64);
$filename = 'report_' . $report_id . '_' . time() . '_' . sanitizeFilename($data['filename']);
file_put_contents(UPLOAD_DIR_ATTACHMENTS . $filename, $base64_data);
$attachment_content = $filename;
}
$stm = $db->insert('attachments', [
'report_id' => $report_id,
'attachment_type' => $attachment_type,
'attachment_content' => $attachment_content,
'created_dt' => date('Y-m-d H:i:s')
]);
return ($stm->rowCount() > 0);
}
function attachmentUpdate($attachment_id, $attachment_content)
{
global $db;
if (strlen(trim($attachment_content)) <= 0) return attachmentDelete($attachment_id);
$stm = $db->update('attachments', [
'attachment_content' => $attachment_content,
'updated_dt' => date('Y-m-d H:i:s')
], [
'attachment_id' => $attachment_id
]);
return ($stm->rowCount() > 0);
}
function attachmentDelete($attachment_id)
{
global $db;
$attachment = attachmentGet($attachment_id);
if ($attachment['attachment_type'] == 'file'
&& file_exists(UPLOAD_DIR_ATTACHMENTS . $attachment['attachment_content']))
{
unlink(UPLOAD_DIR_ATTACHMENTS . $attachment['attachment_content']);
}
$stm = $db->delete('attachments', [
'attachment_id' => $attachment_id
]);
return ($stm->rowCount() > 0);
}
function attachmentGetAll($report_id)
{
global $db;
$all = $db->select('attachments', '*', [
'ORDER' => ['created_dt' => 'ASC'],
'report_id' => $report_id
]);
if (is_array($all)) foreach ($all as $key => $row) {
if ($all[$key]['attachment_type'] == 'file') {
$all[$key]['attachment_content'] = UPLOAD_URL_ATTACHMENTS . $all[$key]['attachment_content'];
}
}
return $all;
}