No direct script access allowed'); /** * @framework iResponse Framework * @version 1.0 * @author Amine Idrissi * @date 2019 * @name GapiUsers.php */ # core use IR\Core\Application as Application; # mvc use IR\Mvc\Controller as Controller; # models use IR\App\Models\Admin\GapiAdmin as GapiAdmin; use IR\App\Models\Admin\GapiUser as GapiUser; use IR\App\Models\Admin\Proxy as Proxy; use IR\App\Models\Admin\GapiUsersBounce as GapiUsersBounce; # 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 GapiUsers * @description GapiUsers Controller */ class GapiUsers 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', 'email', 'admin_name', 'message', 'status', 'created_date' ]; # creating the html part of the list $columns = Page::createTableHeader($columnsArray); $filters = Page::createTableFilters($columnsArray); # set menu status $this->masterView->set([ 'gapi_management' => 'true', 'gapi_servers' => 'true', 'gapi_servers_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', 'email', 'admin_name', 'message', 'status', 'created_date' ]; # fetching the results to create the ajax list die(json_encode(DataTable::init2($data,'admin.gapi_users s',$columns,new GapiUser(),'gapi-users','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([ 'gapi_management' => 'true', 'gapi_servers' => 'true', 'gapi_servers_add' => 'true' ]); # set data to the page view $this->pageView->set([ 'GapiAdmin' => GapiAdmin::all(GapiAdmin::FETCH_ARRAY,['status = ?','Activated'],['id','name'],'id','ASC') ]); } /** * @name replace * @description the replace action * @before init * @after closeConnections,checkForMessage */ public function replace() { # 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([ 'gapi_management' => 'true', //'gapi_servers' => 'true', 'gapi_domain_replace' => 'true' ]); # set data to the page view //$this->pageView->set(); } /** * @name multiEdit * @description the multiEdit action * @before init * @after closeConnections,checkForMessage */ public function multiEdit() { # 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([ 'gapi_management' => 'true', 'gapi_servers' => 'true', 'gapi_servers_multiEdit' => 'true' ]); # set data to the page view } /** * @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); $files = $this->app->http->request->retrieve(Request::ALL,Request::FILES); $message = 'Internal server error !'; $flag = 'error'; if(count($data)) { $username = $this->authenticatedUser->getEmail(); # update case if($this->app->utils->arrays->get($data,'id') > 0) { //print_r($data);exit; //edit gapi user # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit'); if($access == false) { throw new PageException('Access Denied !',403); } $message = 'Record updated succesfully !'; $GapiUser = new GapiUser(); $GapiUser->setId(intval($this->app->utils->arrays->get($data,'id'))); $GapiUser->load(); $GapiUser->setLastUpdatedBy($username); $GapiUser->setLastUpdatedDate(date('Y-m-d')); $GapiAdmin = GapiAdmin::first(GapiAdmin::FETCH_ARRAY,['id = ?',intval($this->app->utils->arrays->get($data,'admin-id'))]); $result = -1; if(count($GapiAdmin) == 0) { $message = 'Gapi Admin not found !'; } else { $GapiUser->setStatus($this->app->utils->arrays->get($data,'user-status','Activated')); $GapiUser->setAdminId(intval($this->app->utils->arrays->get($GapiAdmin,'id'))); $GapiUser->setAdminName($this->app->utils->arrays->get($GapiAdmin,'name')); $GapiUser->setEmail($this->app->utils->arrays->get($data,'email')); $GapiUser->setEmailId(str_replace(['.','"',"'"], '', strtolower($this->app->utils->arrays->get($data,'email')))); $GapiUser->setPassword(str_replace([",",'"'], '',$this->app->utils->arrays->get($data,'password'))); $GapiUser->setRecovry(str_replace([",",'"'], '',$this->app->utils->arrays->get($data,'recovry'))); if($this->app->utils->arrays->get($data,'proxy-id') > 0) { $GapiUser->setProxyId(intval($this->app->utils->arrays->get($data,'proxy-id'))); } $result = $GapiUser->update(); if($result > -1) { $flag = 'success'; } } } else if($this->app->utils->arrays->get($data,'admin-id') > 0) { // add gapi 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,'gapi-emails')))); $GapiAdmin = GapiAdmin::first(GapiAdmin::FETCH_ARRAY,['id = ?',intval($this->app->utils->arrays->get($data,'admin-id'))]); if(!is_array($Emails) || count($Emails) == 0 || count($GapiAdmin) == 0) { $message = 'Data not found !'; }else{ $result = -1; foreach ($Emails as $email) { $email=str_replace(["\n","\r"], '',$email); $result = -1; $message = 'Record stored succesfully !'; $GapiUser = new GapiUser(); $GapiUser->setCreatedBy($username); $GapiUser->setCreatedDate(date('Y-m-d')); $GapiUser->setLastUpdatedBy($username); $GapiUser->setLastUpdatedDate(date('Y-m-d')); $infos=explode(";", $email); $GapiUser->setEmail(preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0])); $GapiUser->setEmailId(str_replace(['.','"',"'"], '', strtolower(preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0])))); if(strpos($email, ";") !== false && count(explode(";", $email))==4){ $infos=explode(";", $email); //$GapiUser->setEmail(preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0])); //$GapiUser->setEmailId(str_replace(['.','"',"'"], '', strtolower(preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0])))); $GapiUser->setPassword(str_replace([",",'"'], '',$infos[1])); $GapiUser->setRecovry(str_replace([",",'"'], '',$infos[2])); $proxy = Proxy::first(Proxy::FETCH_ARRAY,['host = ?',str_replace([",",'"'], '',$infos[3])]); if(count($proxy) >0) { $GapiUser->setProxyId(str_replace([",",'"'], '',$proxy["id"])); }else{ $message = 'Proxy not exist !'; $flag = 'error'; break; } }elseif (strpos($email, ";") !== false) { $message = 'Email format incorect !'; $flag = 'error'; break; }else{ $GapiUser->setEmail(preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$email)); } $GapiUser->setMessage('check'); $GapiUser->setStatus($this->app->utils->arrays->get($data,'user-status','Activated')); $GapiUser->setAdminId(intval($this->app->utils->arrays->get($GapiAdmin,'id'))); $GapiUser->setAdminName($this->app->utils->arrays->get($GapiAdmin,'name')); $result = $GapiUser->insert(); } if($result > -1) { $flag = 'success'; } } }else{ // edit multi gapi user,or add password,recovry and proxy # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'edit'); if($access == false) { throw new PageException('Access Denied !',403); } $Emails = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'gapi-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 Updated succesfully !'; if(strpos($email, ";") !== false && count(explode(";", $email))==4){ $infos=explode(";", $email); $GapiUserInfos = GapiUser::first(GapiUser::FETCH_ARRAY,['email = ?',preg_replace('/[^a-zA-Z0-9_\-.@]/i', '',$infos[0])]); if(!$GapiUserInfos)continue; $GapiUser = new GapiUser(); $GapiUser->setId(intval($GapiUserInfos['id'])); $GapiUser->load(); $GapiUser->setLastUpdatedBy($username); $GapiUser->setLastUpdatedDate(date('Y-m-d')); $GapiUser->setPassword(str_replace([",",'"'], '',$infos[1])); $GapiUser->setRecovry(str_replace([",",'"'], '',$infos[2])); $proxy = Proxy::first(Proxy::FETCH_ARRAY,['host = ?',str_replace([",",'"'], '',$infos[3])]); if(count($proxy) >0) { $GapiUser->setProxyId(str_replace([",",'"'], '',$proxy["id"])); $result = $GapiUser->update(); }else{ $message = 'Proxy not exist !'; $flag = 'error'; break; } }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([ 'gapi_management' => 'true', 'gapi_servers' => 'true', 'gapi_servers_show' => 'true' ]); if(!isset($id) || !is_numeric($id) || intval($id) == 0) { $valid = false; } $gapiUser = GapiUser::first(GapiUser::FETCH_ARRAY,['id = ?',$id]); $proxys = Proxy::all(Proxy::FETCH_ARRAY,['status = ?','Activated'],['id','host'],'id','ASC'); if(count($gapiUser) == 0) { $valid = false; } if($valid == true) { # set data to the page view $this->pageView->set([ 'gapiUser' => $gapiUser, 'proxys' => $proxys, 'gapiAdmin' => GapiAdmin::all(GapiAdmin::FETCH_ARRAY,['status = ?','Activated'],['id','name'],'id','ASC') ]); } else { # stores the message in the session Page::registerMessage('error','Invalid gapi user id !'); # redirect to lists page Page::redirect(); } } /** * @name multiDelete * @description the multiDelete action * @before init * @after closeConnections,checkForMessage */ public function multiDelete() { # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'delete'); if($access == false) { throw new PageException('Access Denied !',403); } $data = $this->app->http->request->retrieve(Request::ALL,Request::POST); $message = 'Internal server error !'; $flag = 'error'; if(count($data)){ $username = $this->authenticatedUser->getEmail(); $users = array_filter(array_unique(explode(PHP_EOL,$this->app->utils->arrays->get($data,'users')))); //exit; if(!is_array($users) || count($users) == 0) { $message = 'Users not found !'; } else { $result = -1; foreach ($users as $user) { $user=str_replace(["\n","\r"," ",'"',","], "",$user); if (strpos($user, "@") !== false) { $result +=GapiUser::deleteWhere("LOWER(email) = ?",[ strtolower($user)]); }else{ $result +=GapiUser::deleteWhere('LOWER(email) like ?',["%@".strtolower($user)]); } } if($result > -1) { $message = 'Records delete succesfully !'; $flag = 'success'; } } # stores the message in the session Page::registerMessage($flag, $message); # redirect to lists page Page::redirect(); } # set menu status $this->masterView->set([ 'gapi_management' => 'true', 'gapi_users_multi_delete' => 'true' ]); } /** * @name replaceDomain * @description the replaceDomain action * @before init * @after closeConnections,checkForMessage */ public function replaceDomain() { # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'replace'); if($access == false) { throw new PageException('Access Denied !',403); } $data = $this->app->http->request->retrieve(Request::ALL,Request::POST); $message = 'Internal server error !'; $flag = 'error'; if(count($data)){ $username = $this->authenticatedUser->getEmail(); $old_domain = $this->app->utils->arrays->get($data,'old_domain'); $new_domain = $this->app->utils->arrays->get($data,'new_domain'); $old_domain=str_replace(["\n","\r"," ","'",'"',","], "",$old_domain); $new_domain=str_replace(["\n","\r"," ","'",'"',","], "",$new_domain); if($old_domain=="" || $new_domain=="" ) { $message = 'Domain not found !'; } else { $result = -1; $results = $this->app->database('system')->execute("UPDATE admin.gapi_admin SET email = replace(email, '".$old_domain."', '".$new_domain."') where email like '%".$old_domain."'"); $results = $this->app->database('system')->execute("UPDATE admin.gapi_users SET email = replace(email, '".$old_domain."', '".$new_domain."') where email like '%".$old_domain."'"); $results = $this->app->database('system')->execute("UPDATE production.teams_authorisations SET gapi_users_ids = replace(gapi_users_ids, '".$old_domain."', '".$new_domain."')"); if(count($result) > -1) { $message = 'Records update succesfully !'; $flag = 'success'; } } # stores the message in the session Page::registerMessage($flag, $message); # redirect to lists page Page::redirect(); } # set menu status $this->masterView->set([ 'gapi_management' => 'true', 'gapi_domain_replace' => 'true' ]); } /** * @name main * @description the main action * @before init * @after closeConnections,checkForMessage */ public function bounce() { # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'delete'); if($access == false) { throw new PageException('Access Denied !',403); } # preparing the columns array to create the list $columnsArray = [ 'id', 'user_id', 'user_email', 'bounce', 'message', 'created_date' ]; $activeUsers = $this->app->database('system')->query()->from('admin.gapi_users')->where('message = ?','ok')->count(); $bounce = $this->app->database('system')->execute("select sum(bounce) from admin.gapi_users_bounce "); # creating the html part of the list $columns = Page::createTableHeader($columnsArray); $filters = Page::createTableFilters($columnsArray); # set menu status $this->masterView->set([ 'gapi_management' => 'true', //'oapi_servers' => 'true', 'gapi_users_bounce' => 'true' ]); # set data to the page view $this->pageView->set([ 'columns' => $columns, 'filters' => $filters, 'users' => $activeUsers, 'bounce' => $bounce[0]["sum"] ]); } /** * @name getAdmin * @description the getAdmin action * @before init * @after closeConnections */ public function getUsersBounce() { # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser,__CLASS__,'delete'); 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', 'user_id', 'user_email', 'bounce', 'message', 'created_date' ]; # fetching the results to create the ajax list die(json_encode(DataTable::init2($data,'admin.gapi_users_bounce s',$columns,new GapiUsersBounce(),'gapi-users','DESC',null,false))); } } /** * @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); } }