Commit 1b502c77 authored by Sarun Mungthanya's avatar Sarun Mungthanya
Browse files

add update patch

parent d9ee275c
# ใช้ภาพพื้นฐานของ PHP เวอร์ชัน 8.2 พร้อมกับ FPM
FROM php:8.2-fpm
# ติดตั้งส่วนขยายและโปรแกรมที่จำเป็น
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
zip \
unzip \
libzip-dev \
pkg-config \
&& docker-php-ext-configure zip \
&& docker-php-ext-install pdo_mysql gd zip
# ติดตั้ง Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# กำหนด Working Directory
WORKDIR /var/www/html
# ลบ node_modules หากมีอยู่แล้ว
RUN rm -rf node_modules
# คัดลอกไฟล์ทั้งหมดจากโฟลเดอร์ปัจจุบันไปยัง Container
COPY . .
# กำหนดสิทธิ์ให้ถูกต้อง
RUN chown -R www-data:www-data /var/www/html
# ติดตั้ง Dependencies ของ PHP
RUN composer install --no-dev --optimize-autoloader
# ติดตั้ง Node.js และ Vite
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs \
&& npm ci
# กำหนดสิทธิ์ให้กับโฟลเดอร์ .npm
RUN mkdir -p /var/www/.npm && chown -R www-data:www-data /var/www/.npm
# รัน build assets
USER www-data
RUN npm run build
# เปิดพอร์ต 9000 เพื่อใช้งาน PHP-FPM
EXPOSE 9000
CMD ["php-fpm"]
...@@ -2,9 +2,14 @@ ...@@ -2,9 +2,14 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use App\Models\ConfServerPendding; use App\Models\ConfServerPendding;
use App\Models\TabPatchFile;
use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Throwable;
class SmartUpdateController extends Controller class SmartUpdateController extends Controller
{ {
...@@ -23,6 +28,8 @@ class SmartUpdateController extends Controller ...@@ -23,6 +28,8 @@ class SmartUpdateController extends Controller
$this->historyPatch($serverKeyClient); $this->historyPatch($serverKeyClient);
} else if ($action === 'LIST_PATCH') { } else if ($action === 'LIST_PATCH') {
$this->listPatch($request); $this->listPatch($request);
} else if ($action === 'UPDATE_PATCH') {
$this->updatePatch($request);
} }
} }
...@@ -62,13 +69,13 @@ class SmartUpdateController extends Controller ...@@ -62,13 +69,13 @@ class SmartUpdateController extends Controller
$serverKeyClient = $request->input('SERVERKEYCLIENT'); $serverKeyClient = $request->input('SERVERKEYCLIENT');
$clientVersion = $request->input('CLIENTVERSION'); $clientVersion = $request->input('CLIENTVERSION');
// ดึง Server ID จากฐานข้อมูล
$serverId = $this->getServerId($serverKeyClient); $serverId = $this->getServerId($serverKeyClient);
if (!$serverId) { if (!$serverId) {
return response()->json(['error' => 'Invalid SERVERKEYCLIENT'], 404); return response()->json(['error' => 'Invalid SERVERKEYCLIENT'], 404);
} }
$pendingUpdates = ConfServerPendding::select( $pendingUpdates = ConfServerPendding::select(
't0.PatchID as PatchID',
't1.PDATE as PatchDate', 't1.PDATE as PatchDate',
't1.PATCHNAME as Title', 't1.PATCHNAME as Title',
't1.PLEVEL as Level', 't1.PLEVEL as Level',
...@@ -108,12 +115,13 @@ class SmartUpdateController extends Controller ...@@ -108,12 +115,13 @@ class SmartUpdateController extends Controller
->where('TaskDate', '<', now()) ->where('TaskDate', '<', now())
->update(['TaskStatus' => 1]); ->update(['TaskStatus' => 1]);
echo view('smartupdate.patch_update', [ echo view('smartupdate.patch_update', [
'serverKeyClient' => $serverKeyClient, 'serverKeyClient' => $serverKeyClient,
'pendingUpdates' => $pendingUpdates, 'pendingUpdates' => $pendingUpdates,
'clientVersion' => $clientVersion, 'clientVersion' => $clientVersion,
'cannotUpdateAll' => $cannotUpdateAll, '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") public function queryToSimpleTable($query, $numberSortOpt = "YES", $columnSortOpt = "YES", $colorPack = ['#DDDDDD', '#F2F2F2', '#FFFFFF', '#F2F2FF'], $noCodeIfEmpty = "NO")
...@@ -156,10 +164,230 @@ class SmartUpdateController extends Controller ...@@ -156,10 +164,230 @@ class SmartUpdateController extends Controller
return $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=<<<EOF2' . chr(13) . $patchCode . chr(13) . 'EOF2;' . chr(13));
$patchCode = str_replace('#', '$', $patchCode);
if (isset($mode) && $mode === 'AUTO') {
$pid = 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');
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=<<<EOF2' . chr(13) . $patchCode . chr(13) . 'EOF2;' . chr(13));
$patchCode = str_replace('#', '$', $patchCode);
return $patchCode;
}
public static function getServerId($serverKeyClient) public static function getServerId($serverKeyClient)
{ {
return DB::table('conf_server_license') return DB::table('conf_server_license')
->where('SNKEY', $serverKeyClient) ->where('SNKEY', $serverKeyClient)
->value('ID'); ->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;
}
} }
...@@ -63,6 +63,7 @@ class Kernel extends HttpKernel ...@@ -63,6 +63,7 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'disableDebugbar' => \App\Http\Middleware\DisableDebugbar::class,
]; ];
protected $commands = [ protected $commands = [
......
<?php
namespace App\Http\Middleware;
use Barryvdh\Debugbar\Facades\Debugbar;
use Closure;
class DisableDebugbar
{
public function handle($request, Closure $next)
{
if (app()->bound('debugbar')) {
Debugbar::disable();
}
return $next($request);
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Providers; namespace App\Providers;
use Barryvdh\Debugbar\Facades\Debugbar;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
...@@ -23,6 +24,10 @@ class AppServiceProvider extends ServiceProvider ...@@ -23,6 +24,10 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
// if (request()->isJson() || request()->ajax()) {
if (app()->bound('debugbar')) {
Debugbar::disable();
}
}
} }
} }
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html
- ./vendor:/var/www/html/vendor
- ./node_modules:/var/www/html/node_modules
ports:
- "9000:9000"
depends_on:
- db
environment:
- APP_NAME=${APP_NAME}
- APP_ENV=local
- APP_KEY=${APP_KEY}
- APP_DEBUG=true
- APP_URL=${APP_URL}
- DB_HOST=${DB_HOST}
- DB_PORT=${DB_PORT}
- DB_DATABASE=${DB_DATABASE}
- DB_USERNAME=${DB_USERNAME}
- DB_PASSWORD=${DB_PASSWORD}
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel
volumes:
- db_data:/var/lib/mysql
ports:
- "3307:3306"
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- .:/var/www/html
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- app
volumes:
db_data:
# Nginx configuration file
# Global settings
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# Server block
server {
listen 80;
server_name localhost;
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
}
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
"prettier": "^2.8.4", "prettier": "^2.8.4",
"prettier-plugin-tailwindcss": "^0.2.2", "prettier-plugin-tailwindcss": "^0.2.2",
"tailwindcss": "^3.2.6", "tailwindcss": "^3.2.6",
"vite": "^4.1.1" "vite": "^4.5.3"
}, },
"dependencies": { "dependencies": {
"@alpinejs/collapse": "^3.11.1", "@alpinejs/collapse": "^3.11.1",
......
...@@ -19,7 +19,9 @@ ...@@ -19,7 +19,9 @@
text-decoration: none; text-decoration: none;
margin: 5px; margin: 5px;
} }
.update-btn:hover , .update-btn:active {
.update-btn:hover,
.update-btn:active {
background-color: #1a3855; background-color: #1a3855;
} }
</style> </style>
...@@ -53,7 +55,11 @@ ...@@ -53,7 +55,11 @@
<td>{{ $update->Description }}</td> <td>{{ $update->Description }}</td>
<td style="text-align: center;"> <td style="text-align: center;">
@if ($update->Action === 'Update') @if ($update->Action === 'Update')
<a href="{{ route('smart_update', ['patchId' => $update->PatchID]) }}" @php
$data = "$serverKeyClient|$update->PatchID|$clientVersion";
$encryptedData = Crypt::encrypt($data);
@endphp
<a href="data_defaulttemplate.php?UID={{ $uId }}&UCODE={{ $uCode }}&SERVICENAME=IMCORESERVICE&ACTION=SMART_UPDATE&PATCHID={{ $update->PatchID }}"
class="btn update-btn">Update</a> class="btn update-btn">Update</a>
@else @else
<span class="text-muted">{{ $update->Action }}</span> <span class="text-muted">{{ $update->Action }}</span>
......
...@@ -18,5 +18,5 @@ use Illuminate\Support\Facades\Route; ...@@ -18,5 +18,5 @@ use Illuminate\Support\Facades\Route;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user(); return $request->user();
}); });
Route::post('/smartupdate', [SmartUpdateController::class, 'checkAction'])->name('smart_update'); Route::post('/smartupdate', [SmartUpdateController::class, 'checkAction'])->name('smart_update')->middleware('disableDebugbar');
Route::get('/smartupdate', [SmartUpdateController::class, 'checkAction']); Route::get('/smartupdate', [SmartUpdateController::class, 'checkAction'])->middleware('disableDebugbar');;
\ No newline at end of file
...@@ -5,7 +5,7 @@ export default defineConfig({ ...@@ -5,7 +5,7 @@ export default defineConfig({
plugins: [ plugins: [
laravel({ laravel({
input: ['resources/css/app.css', 'resources/js/app.js', 'resources/js/sweetalert.js'], input: ['resources/css/app.css', 'resources/js/app.js', 'resources/js/sweetalert.js'],
refresh: false, refresh: false ,
jquery: 'jQuery' jquery: 'jQuery'
}), }),
], ],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment