Files
wevads-platform/app/controllers/FileStash.php
2026-02-26 03:06:17 +01:00

180 lines
4.7 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace IR\App\Controllers;
if (!defined('IR_START')) {
exit('<pre>No direct script access allowed</pre>');
}
/**
* @framework iResponse Framework
* @version 1.0
* @author Mariam bouzine
* @date 2019
* @name filestach.php
*/
# core
use Exception;
use IR\Core\Application as Application;
use IR\Logs\Logger;
# mvc
use IR\Mvc\Controller as Controller;
# http
use IR\Http\Request as Request;
# helpers
use IR\App\Helpers\Authentication as Authentication;
use IR\App\Helpers\Page as Page;
use IR\App\Helpers\Permissions as Permissions;
# exceptions
use IR\Exceptions\Types\PageException as PageException;
/**
* @name MtaServers
* @description MtaServers Controller
*/
class FileStash extends Controller
{
/**
* @app
* @readwrite
*/
protected $app;
/**
* @app
* @readwrite
*/
protected $authenticatedUser;
/**
* @name init
* @description initializing process before the action method executed
* @once
* @protected
*/
public function init()
{
# set the current application to a local variable
$this->app = Application::getCurrent();
# connect to the database
$this->app->database('system')->connect();
# check for authentication
if (!Authentication::isUserAuthenticated()) {
Page::redirect($this->app->http->request->getBaseURL() . RDS . 'auth' . RDS . 'login.' . DEFAULT_EXTENSION);
}
# check users roles
Authentication::checkUserRoles();
# get the authenticated user
$this->authenticatedUser = Authentication::getAuthenticatedUser();
}
/**
* @name file Stach
* @description the UploadFileStash action
* @before init
* @after closeConnections,checkForMessage
*/
public function main()
{
# Start or resume the session
session_start();
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser, __CLASS__, "main");
if ($access == false) {
throw new PageException('Access Denied !', 403);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$bucketName = $_POST['bucket-name'];
if(isset($_FILES['files']) && $_FILES['files']['error'] === UPLOAD_ERR_OK)
{
$tempFile = $_FILES['files']['tmp_name'];
$fileName = $_FILES['files']['name'];
$destinationDirectory = '/tmp/';
$destinationPath = $destinationDirectory . $fileName;
if(move_uploaded_file($tempFile, $destinationPath))
{
$cmd = "python3 ". BASE_PATH . "/app/api/filestash.py {$bucketName} {$fileName}";
$res = $this->app->utils->terminal->cmd($cmd);
$res = $res['output'];
Logger::getInstance()->error("res $res");
header("location: ?message={$res}");
$_SESSION['message'] = $res;
exit();
}
else
{
$_SESSION['message'] = "Error moving file to destination directory.";
header("location: ?message=Error moving file to destination directory.");
exit();
}
}
else
{
$_SESSION['message'] = "No file uploaded or error occurred.";
header("location: ?message=No file uploaded or error occurred.");
exit();
}
}
# Retrieve message from session
$message = isset($_SESSION['message']) ? $_SESSION['message'] : '';
Logger::getInstance()->error("message $message");
# Clear message from session
unset($_SESSION['message']);
# set menu status
$this->masterView->set([
'servers_management' => 'true',
'tools' => 'true',
'Upload_FileStash' => 'true',
]);
$this->pageView->set([
'message' => $message
]);
}
/**
* @name closeConnections
* @description close all connections
* @once
* @protected
*/
public function closeConnections()
{
# connect to the database
$this->app->database('system')->disconnect();
$this->app->database('clients')->disconnect();
}
/**
* @name checkForMessage
* @description checks for session messages
* @once
* @protected
*/
public function checkForMessage()
{
# check for message
Page::checkForMessage($this);
}
}