Files
fmgapp/app/controllers/FapiAdmins.php

540 lines
17 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 FapiAdmins.php
*/
# core
use IR\Core\Application as Application;
# mvc
use IR\Mvc\Controller as Controller;
# models
use IR\App\Models\Admin\FapiAdmin as FapiAdmin;
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 FapiAdmins
* @description FapiAdmins Controller
*/
class FapiAdmins 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',
'status',
'email',
'users',
'created_date'
];
# creating the html part of the list
$columns = Page::createTableHeader($columnsArray);
$filters = Page::createTableFilters($columnsArray);
# set menu status
$this->masterView->set([
'fapi_management' => 'true',
//'gapi_servers' => 'true',
'fapi_admins_show' => 'true'
]);
# set data to the page view
$this->pageView->set([
'columns' => $columns,
'filters' => $filters
]);
}
/**
* @name get
* @description the get 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',
'status',
'email',
'users_count' => 'users',
'created_date'
];
# fetching the results to create the ajax list
die(json_encode(DataTable::init($data,'admin.fapi_admin s',$columns,new FapiAdmin(),'fapi-admins','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([
'fapi_management' => 'true',
'fapi_servers' => 'true',
'fapi_servers_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))
{
$username = $this->authenticatedUser->getEmail();
# update case
if($this->app->utils->arrays->get($data,'id') > 0)
{
//edit fapi user
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$message = 'Record updated succesfully !';
$FapiAdmin = new FapiAdmin();
$FapiAdmin->setId(intval($this->app->utils->arrays->get($data,'id')));
$FapiAdmin->load();
$FapiAdmin->setLastUpdatedBy($username);
$FapiAdmin->setLastUpdatedDate(date('Y-m-d'));
$result = -1;
$old_Name=$FapiAdmin->getName();
$FapiAdmin->setStatus($this->app->utils->arrays->get($data,'fapi-status','Activated'));
$FapiAdmin->setName($this->app->utils->arrays->get($data,'fapi-name'));
$FapiAdmin->setEmail($this->app->utils->arrays->get($data,'fapi-email'));
$FapiAdmin->setPassword(str_replace([",",'"'], '',$this->app->utils->arrays->get($data,'password')));
$FapiAdmin->setRecovry(str_replace([",",'"'], '',$this->app->utils->arrays->get($data,'recovry')));
if($this->app->utils->arrays->get($data,'proxy-id') > 0)
{
$FapiAdmin->setProxyId(intval($this->app->utils->arrays->get($data,'proxy-id')));
}
$result = $FapiAdmin->update();
if($result > -1)
{
if(trim($this->app->utils->arrays->get($data,'fapi-name'))!=$old_Name){
$this->app->database('system')->execute("UPDATE admin.fapi_accounts SET admin_name = '".trim($this->app->utils->arrays->get($data,'fapi-name'))."' where admin_id = ".$FapiAdmin->getId().";");
}
$flag = 'success';
}
}
else
{
// add fapi user
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'add');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
$Emails = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'fapi-emails'))));
if(!is_array($Emails) || count($Emails) == 0 )
{
$message = 'Data not found !';
}else{
$result = -1;
foreach ($Emails as $email)
{
$email=str_replace(["\n","\r"], '',$email);
$result = -1;
$message = 'Record stored succesfully !';
if(strpos($email, ";") !== false && count(explode(";", $email))==4){
$FapiAdmin = new FapiAdmin();
$FapiAdmin->setCreatedBy($username);
$FapiAdmin->setCreatedDate(date('Y-m-d'));
$FapiAdmin->setLastUpdatedBy($username);
$FapiAdmin->setLastUpdatedDate(date('Y-m-d'));
$FapiAdmin->setStatus('Activated');
$infos=explode(";", $email);
$name=explode("@", preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0]))[0];
$FapiAdmin->setName($name);
$FapiAdmin->setEmail(preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0]));
$FapiAdmin->setPassword(str_replace([",",'"'], '',$infos[1]));
$FapiAdmin->setRecovry(str_replace([",",'"'], '',$infos[2]));
$proxy = Proxy::first(Proxy::FETCH_ARRAY,['host = ?',str_replace([",",'"'], '',$infos[3])]);
if(count($proxy) >0)
{
$FapiAdmin->setProxyId(str_replace([",",'"'], '',$proxy["id"]));
}else{
$message = 'Proxy not exist !';
$flag = 'error';
break;
}
$result = $FapiAdmin->insert();
}elseif (strpos($email, ";") !== false) {
$message = 'Email format incorect !';
$flag = 'error';
break;
}
}
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([
'fapi_management' => 'true',
'fapi_servers' => 'true',
'fapi_servers_show' => 'true'
]);
if(!isset($id) || !is_numeric($id) || intval($id) == 0)
{
$valid = false;
}
$Account = FapiAdmin::first(FapiAdmin::FETCH_ARRAY,['id = ?',$id]);
$proxys = Proxy::all(Proxy::FETCH_ARRAY,['status = ?','Activated'],['id','host'],'id','ASC');
if(count($Account) == 0)
{
$valid = false;
}
if($valid == true)
{
# preparing the columns array to create the list
$columnsArray = [
'id',
'name',
'status',
'message',
'created_date'
];
# creating the html part of the list
$columns = '';
$filters = '';
foreach ($columnsArray as $column)
{
if($column != 'id')
{
$columns .= '<th>' . ucwords(str_replace('_',' ',strtolower($column))) . '</th>' . PHP_EOL;
if(strpos($column,'_date') > -1 || strpos($column,'_time') > -1)
{
$filters .= '<td> <div id="' . $column . '_range" class="input-group date-range-picker"> <input type="text" class="form-control form-filter" name="' . $column . '_range"> <span class="input-group-btn"> <button class="btn default date-range-toggle" type="button"> <i class="fa fa-calendar"></i> </button> </span> </div> </td>' . PHP_EOL;
}
else
{
if($column == 'status')
{
$filters .= '<td> <select name="status" class="form-control form-filter input-sm"> <option value="" selected>All</option> <option value="Activated">Activated</option> <option value="Inactivated">Inactivated</option> </select> </td>' . PHP_EOL;
}
else
{
$filters .= '<td><input type="text" class="form-control form-filter" name="' . $column . '"></td>' . PHP_EOL;
}
}
}
}
# set data to the page view
$this->pageView->set([
'Account' => $Account,
'columns' => $columns,
'proxys' => $proxys,
'filters' => $filters
]);
}
else
{
# stores the message in the session
Page::registerMessage('error','Invalid FApi Account id !');
# redirect to lists page
Page::redirect();
}
}
/**
* @name accounts
* @description the accounts action
* @before init
* @after closeConnections
*/
public function accounts()
{
# check for permissions
$access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit');
if($access == false)
{
throw new PageException('Access Denied !',403);
}
# set menu status
$this->masterView->set([
'fapi_management' => 'true',
'fapi_servers' => 'true',
'fapi_servers_show' => 'true'
]);
$arguments = func_get_args();
$page = isset($arguments) && count($arguments) ? $arguments[0] : '';
if(isset($page) && $page != '')
{
switch ($page)
{
case 'get' :
{
# get post data
$data = $this->app->http->request->retrieve(Request::ALL,Request::POST);
if(count($data))
{
$accountId = isset($arguments) && count($arguments) ? intval($arguments[1]) : 0;
# preparing the columns array to create the list
$columns = [
'id',
'name',
'status',
'message',
'created_date'
];
# fetching the results to create the ajax list
$query = $this->app->database('system')->query()->from('admin.fapi_accounts',$columns)->where('admin_id = ?',$accountId);
die(json_encode(DataTable::init($data,'admin.fapi_accounts s',$columns,new FapiAdmin(),'fapi-accounts','DESC',$query)));
}
break;
}
}
}
}
/**
* @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);
}
}