File size: 7,427 Bytes
52d9fb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | <?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Artisan;
use App\Lib\CurlRequest;
use App\Lib\FileManager;
use App\Models\UpdateLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Laramin\Utility\VugiChugi;
class SystemController extends Controller
{
public function systemInfo(){
$laravelVersion = app()->version();
$timeZone = config('app.timezone');
$pageTitle = 'Application Information';
return view('admin.system.info',compact('pageTitle', 'laravelVersion','timeZone'));
}
public function optimize(){
$pageTitle = 'Clear System Cache';
return view('admin.system.optimize',compact('pageTitle'));
}
public function optimizeClear(){
Artisan::call('optimize:clear');
$notify[] = ['success','Cache cleared successfully'];
return back()->withNotify($notify);
}
public function systemServerInfo(){
$currentPHP = phpversion();
$pageTitle = 'Server Information';
$serverDetails = $_SERVER;
return view('admin.system.server',compact('pageTitle', 'currentPHP', 'serverDetails'));
}
public function systemUpdate() {
$pageTitle = 'System Updates';
return view('admin.system.update',compact('pageTitle'));
}
public function systemUpdateProcess(){
if (gs('system_customized')) {
return response()->json([
'status'=>'error',
'message'=>[
'The system already customized. You can\'t update the project'
]
]);
}
if (version_compare(systemDetails()['version'],gs('available_version'),'==')) {
return response()->json([
'status'=>'info',
'message'=>[
'The system is currently up to date'
]
]);
}
if(!extension_loaded('zip')){
return response()->json([
'status'=>'error',
'message'=>[
'Zip Extension is required to update the system'
]
]);
}
$purchasecode = env('PURCHASECODE');
if (!$purchasecode) {
return response()->json([
'status'=>'error',
'message'=>[
'Invalid request. Please contact with support'
]
]);
}
$website = @$_SERVER['HTTP_HOST'] . @$_SERVER['REQUEST_URI'] . ' - ' . env("APP_URL");
$response = CurlRequest::curlPostContent(VugiChugi::upman(),[
'purchasecode'=>$purchasecode,
'product'=>systemDetails()['name'],
'version'=>systemDetails()['version'],
'website'=>$website,
]);
$response = json_decode($response);
if($response->status == 'error'){
return response()->json([
'status'=>'error',
'message'=>$response->message->error
]);
}
if($response->remark == 'already_updated'){
return response()->json([
'status'=>'info',
'message'=>$response->message->success
]);
}
$directory = 'core/temp/';
$files = [];
foreach($response->data->files as $key => $fileUrl){
$opts = [
"http" => [
"method" => "GET",
"header" => "Purchase-Code: $purchasecode"
]
];
$context = stream_context_create($opts);
$fileContent = file_get_contents($fileUrl,false,$context);
if(@json_decode($fileContent)->status == 'error'){
return response()->json([
'status'=>'error',
'message'=>@json_decode($fileContent)->message->error
]);
}
file_put_contents($directory.$key.'.zip',$fileContent);
$files[$key] = $fileContent;
}
$fileNames = array_keys($files);
foreach($fileNames as $fileName){
$rand = Str::random(10);
$dir = base_path('temp/' . $rand);
$extract = $this->extractZip(base_path('temp/' . $fileName.'.zip'), $dir);
if ($extract == false) {
$this->removeDir($dir);
return response()->json([
'status'=>'error',
'message'=>['Something went wrong while extracting the update']
]);
}
if (!file_exists($dir . '/config.json')) {
$this->removeDir($dir);
return response()->json([
'status'=>'error',
'message'=>['Config file not found']
]);
}
$getConfig = file_get_contents($dir . '/config.json');
$config = json_decode($getConfig);
$this->removeFile($directory . '/' . $fileName.'.zip');
$mainFile = $dir . '/update.zip';
if (!file_exists($mainFile)) {
$this->removeDir($dir);
return response()->json([
'status'=>'error',
'message'=>['Something went wrong while patching the update']
]);
}
//move file
$extract = $this->extractZip(base_path('temp/' . $rand . '/update.zip'), base_path('../'));
if ($extract == false) {
return response()->json([
'status'=>'error',
'message'=>['Something went wrong while extracting the update']
]);
}
//Execute database
if (file_exists($dir . '/update.sql')) {
$sql = file_get_contents($dir . '/update.sql');
DB::unprepared($sql);
}
$updateLog = new UpdateLog();
$updateLog->version = $config->version;
$updateLog->update_log = $config->changes;
$updateLog->save();
$this->removeDir($dir);
}
Artisan::call('optimize:clear');
return response()->json([
'status'=>'success',
'message'=>['System updated successfully']
]);
}
public function systemUpdateLog(){
$pageTitle = 'System Update Log';
$updates = UpdateLog::orderBy('id','desc')->paginate(getPaginate());
return view('admin.system.update_log',compact('pageTitle','updates'));
}
protected function extractZip($file, $extractTo)
{
$zip = new \ZipArchive;
$res = $zip->open($file);
if ($res != true) {
return false;
}
for( $i = 0 ; $i < $zip->numFiles ; $i++ ) {
if ( $zip->getNameIndex( $i ) != '/' && $zip->getNameIndex( $i ) != '__MACOSX/_' ) {
$zip->extractTo( $extractTo, array($zip->getNameIndex($i)) );
}
}
$zip->close();
return true;
}
protected function removeFile($path)
{
$fileManager = new FileManager();
$fileManager->removeFile($path);
}
protected function removeDir($location)
{
$fileManager = new FileManager();
$fileManager->removeDirectory($location);
}
}
|