Files
2026-02-26 03:06:17 +01:00

402 lines
11 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 Amine Idrissi <contact@iresponse.tech>
* @date 2019
* @name Proxys.php
*/
# core
use IR\Core\Application as Application;
# mvc
use IR\Mvc\Controller as Controller;
# models
use IR\App\Models\Admin\Proxy as Proxy;
# 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\DataTable as DataTable;
use IR\App\Helpers\Permissions as Permissions;
# exceptions
use IR\Exceptions\Types\PageException as PageException;
/**
* @name Proxys
* @description Proxys Controller
*/
class Proxys 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 main
* @description the main action
* @before init
* @after closeConnections,checkForMessage
*/
public function main()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,__FUNCTION__);
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# preparing the columns array to create the list
$columnsArray = [
'id',
'name',
'host',
'port',
'user_name' =>'user',
'password',
'status',
'created_date'
];
# creating the html part of the list
$columns = Page::createTableHeader($columnsArray);
$filters = Page::createTableFilters($columnsArray);
# set menu status
$this->masterView->set([
'proxy_management' => 'true',
//'gapi_servers' => 'true',
'proxys_show' => 'true'
]);
# set data to the page view
$this->pageView->set([
'columns' => $columns,
'filters' => $filters
]);
}
/**
* @name getAdmin
* @description the getAdmin action
* @before init
* @after closeConnections
*/
public function get()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'main');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
//print_r($data);exit;
if(count($data))
{
$url = $this->app->http->request->getBaseURL();
# preparing the columns array to create the list
$columns = [
'id',
'name',
'host',
'port',
'user_name' =>'user',
'password',
'status',
'created_date'
];
# fetching the results to create the ajax list
die(json_encode(DataTable::init($data,'admin.proxy s',$columns,new Proxy(),'proxys','DESC',null)));
}
}
/**
* @name add
* @description the add action
* @before init
* @after closeConnections,checkForMessage
*/
public function add()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,__FUNCTION__);
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# set menu status
$this->masterView->set([
'proxy_management' => 'true',
'proxys_add' => 'true'
]);
}
/**
* @name save
* @description the save action
* @before init
* @after closeConnections
*/
public function save()
{
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
$message = 'Internal server error !';
$flag = 'error';
if(count($data))
{
$update = false;
$Proxy = new Proxy();
$username = $this->authenticatedUser->getEmail();
# update case
if($this->app->utils->arrays->get($data,'id') > 0)
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$update = true;
$message = 'Record updated succesfully !';
$Proxy->setId(intval($this->app->utils->arrays->get($data,'id')));
$Proxy->load();
$Proxy->setLastUpdatedBy($username);
$Proxy->setLastUpdatedDate(date('Y-m-d'));
$Proxy->setStatus($this->app->utils->arrays->get($data,'proxy-status','Activated'));
$Proxy->setName($this->app->utils->arrays->get($data,'proxy-name'));
$Proxy->setHost($this->app->utils->arrays->get($data,'proxy-host'));
$Proxy->setPort($this->app->utils->arrays->get($data,'proxy-port'));
$Proxy->setUserName($this->app->utils->arrays->get($data,'proxy-user-name'));
$Proxy->setPassword(str_replace([' ',"\n","\r","\n\r","\t"], '',$this->app->utils->arrays->get($data,'proxy-password')));
$result = $Proxy->update();
if($result > -1)
{
$flag = 'success';
}
}
else
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'add');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$Proxys = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'proxys'))));
if(!is_array($Proxys) || count($Proxys) == 0 )
{
$message = 'Data not found !';
}else{
$result = -1;
foreach ($Proxys as $proxy)
{
$tmp=explode(",",str_replace([' ',"\n","\r","\n\r","\t"], '',$proxy) );
if(count($tmp)!=5) continue;
$result = -1;
$message = 'Record stored succesfully !';
$Proxy = new Proxy();
$Proxy->setCreatedBy($username);
$Proxy->setCreatedDate(date('Y-m-d'));
$Proxy->setLastUpdatedBy($username);
$Proxy->setLastUpdatedDate(date('Y-m-d'));
$Proxy->setStatus('Activated');
$Proxy->setName(str_replace(['"',"'"], "", $tmp[0]));
$Proxy->setHost(str_replace(['"',"'"], "", $tmp[1]));
$Proxy->setPort(str_replace(['"',"'"], "", $tmp[2]));
$Proxy->setUserName(str_replace(['"',"'"], "", $tmp[3]));
$Proxy->setPassword(str_replace(['"',"'"], "", $tmp[4]));
$result = $Proxy->insert();
}
if($result > -1)
{
$flag = 'success';
}
}
}
}
# stores the message in the session
Page::registerMessage($flag, $message);
# redirect to lists page
Page::redirect();
}
/**
* @name edit
* @description the edit action
* @before init
* @after closeConnections,checkForMessage
*/
public function edit()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,__FUNCTION__);
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$arguments = func_get_args();
$id = isset($arguments) && count($arguments) > 0 ? $arguments[0] : null;
$valid = true;
# set menu status
$this->masterView->set([
'proxy_management' => 'true',
'proxys_show' => 'true'
]);
if(!isset($id) || !is_numeric($id) || intval($id) == 0)
{
$valid = false;
}
$proxy = Proxy::first(Proxy::FETCH_ARRAY,['id = ?',$id]);
if(count($proxy) == 0)
{
$valid = false;
}
if($valid == true)
{
# set data to the page view
$this->pageView->set([
'proxy' => $proxy
]);
}
else
{
# stores the message in the session
Page::registerMessage('error','Invalid Proxy id !');
# redirect to lists page
Page::redirect();
}
}
/**
* @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);
}
}