ElixirVault/www/system-brs-core/sys/sysUpdater/inc/sysUpdaterController.php

270 lines
10 KiB
PHP

<?php
namespace BRSystems\Core\Sys\sysUpdater\inc;
use BRSystems\Core\Library\cDb\cDb;
use BRSystems\Core\Library\cDefuseCrypto\cDefuseCrypto;
use BRSystems\Core\Library\cINI\cINI;
use BRSystems\Core\Library\cJSON\cJSON;
use BRSystems\Core\Library\cLog\cLog;
use BRSystems\Core\Library\cTimer\cTimer;
use Exception;
/**
* sysUpdater Controller
*
* @category sysUpdater Controller
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @since Version 1.0
*/
class sysUpdaterController
{
public $sDir;
public $sName;
public $oModule;
const LOGGER_KEY = '_project_updater_log';
// SAME AS PROJECT EXPORT MODULE CONSTANTS
CONST SERVER_V_UPDATE = 8000;
CONST EXPORT_SETUP = 17059;
CONST EXPORT_ERROR = 1;
CONST EXPORT_SVN_REQ = 4000; // Export active & updated for export
CONST EXPORT_SVN_REQ_L = 4001; // SVN Export LOCKED
CONST EXPORT_PREPARED = 5003; // SVN Export ready for download
CONST EXPORT_ACCEPT = 4081;
CONST EXPORT_BACKUP = 5;
CONST EXPORT_DOWNLOAD = 6;
CONST EXPORT_DOWNLOAD_OK = 601;
CONST EXPORT_UPDATED = 7;
CONST EXPORT_DB_UPDATED = 8;
CONST EXPORT_CANCELED = 9;
CONST EXPORT_2FA_CHECK = 10;
CONST REQUEST_UPDATE = 'update-requests';
CONST REQUEST_SERVER = 'server-requests';
CONST REQUEST_ACTION_CONST = '%REQ_ACTION%';
CONST REQUEST_VALIDATION_CONST = '%REQ_VALIDATION%';
CONST PROJECT_EXPORT_TIMEOUT = 600; // Timeout after 10 min
STATIC $EXPORT2WORDS = array(
self::EXPORT_SETUP => 'Requested!',
self::EXPORT_ERROR => 'Error!',
self::EXPORT_SVN_REQ => 'SVN export!',
self::EXPORT_SVN_REQ_L => 'SVN export lock!',
self::EXPORT_PREPARED => 'Prepared!',
self::EXPORT_ACCEPT => 'Accepted!',
self::EXPORT_BACKUP => 'Backuping!',
self::EXPORT_DOWNLOAD => 'Downloading!',
self::EXPORT_UPDATED => 'Updated!',
self::EXPORT_DB_UPDATED => 'DB Updated!',
self::EXPORT_CANCELED => 'Canceled!',
);
/**
* Class constructor
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysUpdater Controller
*/
public function __construct() {
$this->oModule = new sysUpdaterModule();
}
/**
* SYSTEM USERS MODULE - Function to show main form in the module
*
* @category sysUpdater Controller
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @returns bool true
*/
public function run(){
$this->cTimer = new cTimer();
//$this->oView->setAsJSON('baseUrl', $this->gL());
//$this->oView->setAsJSON('gM', $this->getTranslations());
//$this->oView->setAsJSON('vcfg', $this->getUserValCfg());
pr("READY!\n\n");
$this->oView->display();
}
/**
* Function to init update for specific project
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysUpdater Controller
* @param int $iExportId - server svn export table ID - encrypted by default key
* @param string|bool $sVFile - project version file location for version increase - encrypted by default key
* @param string|bool $sReqUrl - request link for data / statuses / ect.
* @param string|bool $sToken - request validation token
* @returns bool true or redirect to error
*/
public function initProjectUpdate($iExportId, $sVFile = false, $sReqUrl = false, $sToken = false){
$oDb = new cDb();
$oDefuse = new cDefuseCrypto();
$sErrorDescription = 'EXPORT/UPDATE ACCESS ERROR!!!';
try{
// Decode sent data
$iExportId = $oDefuse->decrypt($iExportId);
$sVFile = $oDefuse->decrypt($sVFile);
$sToken = $oDefuse->decrypt($sToken);
cLog::logLine(self::LOGGER_KEY, '-----------------------------------------------------------------');
cLog::logLine(self::LOGGER_KEY, "exp_id >> {$iExportId} | vers_file >> {$sVFile} | token >> {$sToken}");
if($iExportId && !!$sVFile && !!$sReqUrl && !!$sToken){
// Get version data
$this->getThisProjectVersionData();
// Check if export exists on main server & request has been submitted within last 10min
$sRequest = str_replace(self::REQUEST_ACTION_CONST, self::EXPORT_SVN_REQ, $sReqUrl);
$sRequest = str_replace(self::REQUEST_VALIDATION_CONST, $oDefuse->encrypt($sToken), $sRequest);
$sResponseJson = file_get_contents($sRequest);
$vStatus = cJSON::decode($sResponseJson, true);
cLog::logLine(self::LOGGER_KEY, "req >> {$sRequest} | resp >> {$sResponseJson}");
if(!empty($vStatus['jsonok']) && $vStatus['jsonok']){
if($iExportId && strlen($sVFile) && file_exists(__CFG__ . $sVFile)){
// Load site configuration file
$oCfg = new cINI(__CFG__ . $sVFile);
// Cancel all older exports
// Disable duplicates if exists
$this->oModule->oDb->_queryUpdate(
'site_update',
array('u_status' => sysUpdaterController::EXPORT_CANCELED),
array(
'u_status' => array(
self::EXPORT_SETUP,
self::EXPORT_SVN_REQ,
self::EXPORT_PREPARED,
self::EXPORT_ACCEPT,
self::EXPORT_BACKUP,
self::EXPORT_DOWNLOAD,
self::EXPORT_UPDATED,
self::EXPORT_DB_UPDATED,
)
));
$this->oModule->updRequestStatus(false, array(
'u_export_id' => $iExportId,
'u_date' => time(),
'u_remote_file' => '',
'u_status_req_url' => $sReqUrl,
'u_version' => '',
'u_old_version' => $oCfg->getV('version_control', 'CMS_VERSION'),
'u_response' => '',
'u_status' => self::EXPORT_ACCEPT,
'u_token' => $sToken,
));
cLog::logLine(self::LOGGER_KEY, 'Project update accepted & version file request initiated | current v' . $oCfg->getV('version_control', 'CMS_VERSION'));
cJSON::sendOk(array('old_v' => $oCfg->getV('version_control', 'CMS_VERSION')));
}
}else $sErrorDescription = 'EXPORT/UPDATE EXPIRED!!!';
}
}catch (Exception $e){
pr($e);
}
//sleep(5);
cJSON::sendError(array('error_msg' => $sErrorDescription));
}
/**
* Function to get THIS - CURRENT project data from coonfig
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysUpdater Controller
* @returns array - version data
*/
public function getThisProjectVersionData(){
}
/**
* Function to check if this project has been marker for update & status is ok fro update
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysUpdater Controller
* @returns void
*/
public function chkProjectUpdateStatus(){
// Chk project update status
$oDefuse = new cDefuseCrypto();
$vUpdateStatus = $this->oModule->getProjectUpdateStatus();
if(!!$vUpdateStatus && is_array($vUpdateStatus)){
// Request & check export status on update server
$sRequest = str_replace(self::REQUEST_ACTION_CONST, self::EXPORT_PREPARED, $vUpdateStatus['u_status_req_url']);
$sRequest = str_replace(self::REQUEST_VALIDATION_CONST, $oDefuse->encrypt($vUpdateStatus['u_token']), $sRequest);
$sResponseJson = file_get_contents($sRequest);
$vStatus = cJSON::decode($sResponseJson, true);
cLog::logLine(self::LOGGER_KEY, "req >> {$sRequest} | resp >> {$sResponseJson}");
if(isset($vStatus['jsonok']) && $vStatus['jsonok'] && isset($vStatus['token']) && isset($vStatus['data']) && isset($vStatus['version'])){
$sToken = $oDefuse->decrypt($vStatus['token']);
$sFile = $oDefuse->decrypt($vStatus['data']);
if(!!$sFile && !!$sToken && $sToken == $vUpdateStatus['u_token']){
$this->oModule->updRequestStatus($vUpdateStatus['id'], array(
'u_remote_file' => $sFile,
'u_version' => $vStatus['version'],
'u_status' => self::EXPORT_DOWNLOAD,
'u_response' => $sResponseJson,
));
// Validate version
if(version_compare($vUpdateStatus['u_old_version'], $vStatus['version'], '<')){
cLog::logLine(self::LOGGER_KEY, "Update package version OK!");
// Init script project update via .sh file
// cLog::logLine(self::LOGGER_KEY, "req >> {$sRequest} | resp >> {$sResponseJson}");
// Version is lower - cancel update
}else{
cLog::logLine(self::LOGGER_KEY, "Update canceled due to version control -> server version is higher than update package");
$this->oModule->updRequestStatus($vUpdateStatus['id'], array(
'u_status' => self::EXPORT_CANCELED,
'u_notes' => "Canceled due to Version incompatibility issues! Current v{$vUpdateStatus['u_old_version']} < Update v{$vStatus['version']}",
));
// Cancel project update
$sRequest = str_replace(self::REQUEST_ACTION_CONST, self::EXPORT_CANCELED, $vUpdateStatus['u_status_req_url']);
$sRequest = str_replace(self::REQUEST_VALIDATION_CONST, $oDefuse->encrypt($vUpdateStatus['u_token']), $sRequest);
$sRequest = "{$sRequest}/msg:" . urlencode("Version incompatibility issue!") . "/";
cLog::logLine(self::LOGGER_KEY, "Cancellation req >> {$sRequest}");
$sResponseJson = file_get_contents($sRequest);
}
}
}
}
}
}