input('SERVICENAME'); $action = $request->input('ACTION'); $serverKeyClient = $request->input('SERVERKEYCLIENT'); // if ($serviceName !== 'SMARTUPDATE' || $action !== 'HISTORY_PATCH') { // return response()->json(['error' => 'Invalid request'], 400); // } if ($action === 'HISTORY_PATCH') { $this->historyPatch($serverKeyClient); } else if ($action === 'LIST_PATCH') { $this->listPatch($request); } else if ($action === 'UPDATE_PATCH') { $this->updatePatch($request); } } public function historyPatch($serverKeyClient) { $serverId = DB::table('conf_server_license') ->where('SNKEY', $serverKeyClient) ->value('ID'); if (!$serverId) { return response()->json(['error' => 'Invalid SERVERKEYCLIENT'], 404); } $patchHistory = $this->queryToSimpleTable(" SELECT TaskFinish as 'Update Date', TaskRunner as 'Update By', PDATE as 'Patch Date', PATCHNAME as 'Title', PLEVEL as 'Level', PDESC as 'Description' FROM conf_server_pendding AS t0 INNER JOIN conf_smartupdate AS t1 ON t0.PatchID = t1.PID WHERE ServerId = '$serverId' AND TaskStatus = 999 AND PAPPROVEDATE < NOW() AND PAPPROVEDATE <> '0000-00-00 00:00:00' AND TaskDate < NOW() AND TaskDate <> '0000-00-00 00:00:00' "); return $patchHistory; } public function listPatch($request) { $serverKeyClient = $request->input('SERVERKEYCLIENT'); $clientVersion = $request->input('CLIENTVERSION'); $serverId = $this->getServerId($serverKeyClient); if (!$serverId) { return response()->json(['error' => 'Invalid SERVERKEYCLIENT'], 404); } $pendingUpdates = ConfServerPendding::select( 't0.PatchID as PatchID', 't1.PDATE as PatchDate', 't1.PATCHNAME as Title', 't1.PLEVEL as Level', 't1.PDESC as Description', DB::raw("IF('{$clientVersion}' <= t1.major_version, 'Update', 'Version incompatible') AS Action") ) ->from('conf_server_pendding as t0') ->join('conf_smartupdate as t1', 't0.PatchID', '=', 't1.PID') ->where('t0.ServerId', $serverId) ->where('t0.TaskStatus', '<', 999) ->where('t1.PAPPROVEDATE', '<', now()) ->where('t1.PAPPROVEDATE', '<>', '0000-00-00 00:00:00') ->where('t0.TaskDate', '<', now()) ->where('t0.TaskDate', '<>', '0000-00-00 00:00:00') ->orderBy('t0.PatchID', 'ASC') ->get(); $cannotUpdateAll = false; foreach ($pendingUpdates as $index => $update) { if ($index > 0 && $update->Action == 'Update') { $update->Action = 'Require previous update'; } if ($update->Action == 'Version incompatible') { $cannotUpdateAll = true; } } DB::table('log_history')->insert([ 'HDATE' => now(), 'HSID' => $serverId, 'HACTION' => 'LIST_PATCH', ]); ConfServerPendding::where('ServerID', $serverId) ->where('TaskStatus', '<', 999) ->where('TaskDate', '<', now()) ->update(['TaskStatus' => 1]); echo view('smartupdate.patch_update', [ 'serverKeyClient' => $serverKeyClient, 'pendingUpdates' => $pendingUpdates, 'clientVersion' => $clientVersion, 'cannotUpdateAll' => $cannotUpdateAll, 'uId' => $request->input('MUID'), 'uCode' => $request->input('MUCODE'), ]); } public function queryToSimpleTable($query, $numberSortOpt = "YES", $columnSortOpt = "YES", $colorPack = ['#DDDDDD', '#F2F2F2', '#FFFFFF', '#F2F2FF'], $noCodeIfEmpty = "NO") { $result = DB::select($query); if (empty($result)) { return $noCodeIfEmpty === "YES" ? '' : '

No data found.

'; } $columns = array_keys((array) $result[0]); $tabID = 'table' . rand() + 50; $res = $columnSortOpt === "YES" ? "" : ""; $res .= "
"; foreach ($columns as $column) { $res .= ""; } $res .= ""; foreach ($result as $index => $row) { $bgColor = $colorPack[($index % 2) + 2]; $res .= ""; foreach ($columns as $column) { // $cellValue = iconv('UTF-8','TIS-620', $row->$column); $align = is_numeric(str_replace(',', '', $row->$column)) && $numberSortOpt === "YES" ? " align='right' " : ''; $res .= ""; } $res .= ""; } $res .= "
" . ($column) . "
" . ($row->$column) . "
"; if ($columnSortOpt === "YES") { $res .= ""; } return $res; } public static function updatePatch($request) { $serverKeyClient = $request->input('SERVERKEYCLIENT'); $clientVersion = $request->input('CLIENTVERSION'); $patchId = $request->input('PATCHID'); $serverId = DB::table('conf_server_license') ->where('SNKEY', $serverKeyClient) ->value('ID'); if (!empty($clientVersion)) { DB::table('conf_server_license') ->where('ID', $serverId) ->update(['cur_version' => $clientVersion]); } if ($patchId === 'AUTO') { $patchId = DB::table('conf_server_pendding as t0') ->join('conf_smartupdate as t1', 't0.PatchID', '=', 't1.PID') ->where('t1.PLEVEL', 'AUTO') ->where('t0.ServerId', $serverId) ->where('t0.TaskStatus', '<', 999) ->where('t1.PAPPROVEDATE', '<', now()) ->where('t1.PAPPROVEDATE', '<>', '0000-00-00 00:00:00') ->where('t0.TaskDate', '<', now()) ->where('t0.TaskDate', '<>', '0000-00-00 00:00:00') ->value('PID'); $mode = 'AUTO'; } $patchDetails = DB::table('conf_server_pendding as t0') ->join('conf_smartupdate as t1', 't0.PatchID', '=', 't1.PID') ->where('t1.PID', $patchId) ->where('t0.ServerId', $serverId) ->where('t0.TaskStatus', '<', 999) ->where('t1.PAPPROVEDATE', '<', now()) ->where('t1.PAPPROVEDATE', '<>', '0000-00-00 00:00:00') ->where('t0.TaskDate', '<', now()) ->where('t0.TaskDate', '<>', '0000-00-00 00:00:00') ->first(['PATCHCODE_SERVER', 'PATCHCODE', 'MAJOR_VERSION']); if ($patchDetails && $clientVersion > $patchDetails->MAJOR_VERSION) { return response()->json([ 'status' => 'FAILED', 'message' => 'Version Incompatible.', ]); } if (!empty($patchDetails->PATCHCODE_SERVER)) { $ptid = static::extractPtid($patchDetails->PATCHCODE_SERVER); $pacthFiles = TabPatchFile::where("ptid", $ptid)->get(); foreach ($pacthFiles as $index => $row) { ${'file_name_' . $index} = $row->file_name; ${'file_data_' . $index} = $row->file_data; } $max = count($pacthFiles); $checkDeployemnt = strpos($patchDetails->PATCHCODE_SERVER, 'file_exists("deployment/".$VERSION'); $folder = static::extractVersion($patchDetails->PATCHCODE_SERVER); if ($checkDeployemnt !== false) { $DOUPGRADE = "N"; if (!empty($folder)) { if (Storage::exists("deployment/" . $folder)) { $DOUPGRADE = "Y"; // if (Storage::exists("deployment/" . $folder . ".cachefiles")) { // $DATA = Storage::get("deployment/" . $folder . ".cachefiles"); // } else { $DATA = base64_encode(gzcompress(var_export(static::getFileVersion($folder), true))); // Storage::put("deployment/" . $folder . ".cachefiles", $DATA); // } // if (Storage::exists("deployment/" . $folder . ".cachedirs")) { // $DATADIR = Storage::get("deployment/" . $folder . ".cachedirs"); // } else { $DATADIR = base64_encode(gzcompress(var_export(static::getDirVersion($folder), true))); // Storage::put("deployment/" . $folder . ".cachedirs", $DATADIR); // } } else { $DOUPGRADE = "N"; } } $file_name_0 = ''; $file_data_0 = ''; } $patchCode = $patchDetails->PATCHCODE; $patchCode = str_replace('updatePatchFile("$$file_name_0", "$$file_data_0");', '', $patchCode); $patchCode = str_replace('$', '#', $patchCode); $patchCode = str_replace('##', '$', $patchCode); eval('$patchCode=<<join('conf_smartupdate as t1', 't0.PatchID', '=', 't1.PID') ->where('t1.PLEVEL', 'AUTO') ->where('t0.ServerId', $serverId) ->where('t0.TaskStatus', '<', 999) ->where('t1.PAPPROVEDATE', '<', now()) ->where('t1.PAPPROVEDATE', '<>', '0000-00-00 00:00:00') ->where('t0.TaskDate', '<', now()) ->where('t0.TaskDate', '<>', '0000-00-00 00:00:00') ->value('PID'); if ($pid > 0) { $patchCode .= "\$AUTO='#NEED_MORE_PATCH#';"; $patchCode .= "\$PATCHID=$patchId;"; } else { $patchCode .= "\$AUTO='#NO_MORE_PATCH#';"; $patchCode .= "\$PATCHID=$patchId;"; } } if ($serverKeyClient == '3101023619000000120120228') { $patchCode = encrypt($patchCode, $serverKeyClient); } DB::table('log_history')->insert([ 'HDATE' => now(), 'HSID' => $serverId, 'HACTION' => "PATCH $patchId", ]); DB::table('conf_server_pendding') ->where('ServerId', $serverId) ->where('PatchID', $patchId) ->update(['TaskStatus' => 2]); echo base64_encode($patchCode); } else { echo base64_encode('$PATCH_STATUS="DONE";'); } } public static function processPatchCode($patchCode) { $patchCode = str_replace('$', '#', $patchCode); $patchCode = str_replace('##', '$', $patchCode); eval('$patchCode=<<where('SNKEY', $serverKeyClient) ->value('ID'); } function encrypt($pure_string, $encryption_key) { $cipher = "aes-256-cbc"; $iv_size = openssl_cipher_iv_length($cipher); $iv = openssl_random_pseudo_bytes($iv_size); $encrypted_string = openssl_encrypt($pure_string, $cipher, $encryption_key, 0, $iv); return base64_encode($encrypted_string . '::' . $iv); } // function encrypt($pure_string, $encryption_key) { // $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); // $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); // $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv); // return $encrypted_string; // } public static function getFileVersion($version) { $DATA = []; $directoryPath = storage_path("app/deployment/" . $version); if (File::exists($directoryPath)) { $files = File::allFiles($directoryPath); foreach ($files as $file) { $contents = File::get($file); $relativeFilePath = substr($file->getPathname(), strlen($directoryPath) + 1); $DATA[$relativeFilePath] = base64_encode($contents); } } return $DATA; } public static function getDirVersion($version) { $DATA = array(); $directoryPath = storage_path("app/deployment/" . $version); if (File::exists($directoryPath)) { $files = File::allFiles($directoryPath); foreach ($files as $file) { $path_parts = pathinfo($file); $dir = substr($path_parts['dirname'], strlen($directoryPath) + 1); $DATA[$dir] = $dir; } } return $DATA; } public static function extractPtid($string) { $pattern = "/ptid\s*=\s*'(\d+)'/"; if (preg_match($pattern, $string, $matches)) { return $matches[1]; } return null; } public static function extractVersion($string) { $pattern = '/\$VERSION\s*=\s*"([^"]+)"/'; if (preg_match($pattern, $string, $matches)) { return $matches[1]; } return null; } }