From 54fb50fcca13390520f29751b8961a90c0027071 Mon Sep 17 00:00:00 2001 From: Abyss Morgan <14877700+AbyssMorgan@users.noreply.github.com> Date: Tue, 1 Nov 2022 22:05:54 +0100 Subject: [PATCH] Update v1.2.0 - Update mtn and ffmpeg - Add check for updates and download updates function --- .gitignore | 1 + AVE-PHP.cmd | 4 +- _get_package.cmd | 6 ++- config/default.ini | 2 + includes/AVE.php | 127 ++++++++++++++++++++++++++++++++++++++++++++- includes/main.php | 2 +- version | 2 +- 7 files changed, 136 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 9ea4fa7..980f8d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin/* config/user.ini AVE-PHP.7z +AVE.ave-guard diff --git a/AVE-PHP.cmd b/AVE-PHP.cmd index 57593bf..f286060 100644 --- a/AVE-PHP.cmd +++ b/AVE-PHP.cmd @@ -1,7 +1,7 @@ @ECHO OFF chcp 65001 CLS -SET PATH=%PATH%;%CD%\bin;%CD%\bin\php;%CD%\bin\imagick;%CD%\bin\ffmpeg;%CD%\bin\mtn -FOR /F "tokens=*" %%s IN ('php "%CD%\includes\main.php" GET_COLOR') DO COLOR %%s +SET PATH=%PATH%;%CD%\bin\main;%CD%\bin\php;%CD%\bin\imagick +FOR /F "tokens=*" %%s IN ('php "%CD%\includes\main.php" --get-color') DO COLOR %%s php "%CD%\includes\main.php" GOTO :eof \ No newline at end of file diff --git a/_get_package.cmd b/_get_package.cmd index 8948fab..5e502c9 100644 --- a/_get_package.cmd +++ b/_get_package.cmd @@ -1,5 +1,7 @@ @ECHO OFF -SET PATH=%PATH%;%CD%\bin +SET PATH=%PATH%;%CD%\bin\main;%CD%\bin\php;%CD%\bin\imagick +php "%CD%\includes\main.php" --put-version +php "%CD%\includes\main.php" --guard-generate IF EXIST "%CD%\AVE-PHP.7z" DEL /Q /A "%CD%\AVE-PHP.7z" -7z a -mx9 -t7z "%CD%\AVE-PHP.7z" "bin" "includes" "meta" "config" "AVE-PHP.cmd" -x!"config\user.ini" "LICENSE" +7z a -mx9 -t7z "%CD%\AVE-PHP.7z" "bin" "includes" "meta" "config" "AVE-PHP.cmd" "AVE.ave-guard" -x!"config\user.ini" "LICENSE" PAUSE \ No newline at end of file diff --git a/config/default.ini b/config/default.ini index a1c9b31..5fce829 100644 --- a/config/default.ini +++ b/config/default.ini @@ -21,3 +21,5 @@ AVE_THUMBNAIL_ROWS=5 AVE_THUMBNAIL_WIDTH=2048 AVE_IGNORE_SCAN="desktop.ini" AVE_SUBTITLES_LANGUAGE="PL" +AVE_CHECK_FOR_UPDATES=true +AVE_CHECK_FOR_UPDATES_DAYS=7 diff --git a/includes/AVE.php b/includes/AVE.php index 163e110..a872b2f 100644 --- a/includes/AVE.php +++ b/includes/AVE.php @@ -5,6 +5,7 @@ use App\Services\IniFile; use App\Services\Logs; use App\Services\CommandLine; +use App\Services\GuardDriver; use App\Tools\NamesGenerator; use App\Tools\FileFunctions; @@ -25,17 +26,29 @@ class AVE extends CommandLine { public Logs $log_data; public string $path; + public bool $abort = false; private string $app_name = "AVE"; - private string $version = "1.1"; + private string $version = "1.2.0"; private ?string $command; private array $arguments; private string $logo; private string $tool_name; private string $subtool_name; private array $folders_state = []; + private string $guard_file; private $tool; + private array $folders_to_scan = [ + 'bin', + 'includes', + ]; + + private array $files_to_scan = [ + 'config/default.ini', + 'config/mkvmerge.ini', + ]; + public function __construct(array $arguments){ parent::__construct(); $this->path = __DIR__.DIRECTORY_SEPARATOR.".."; @@ -48,12 +61,31 @@ public function __construct(array $arguments){ $config_default = new IniFile("$this->path/config/default.ini", true); $this->config = new IniFile("$this->path/config/user.ini", true); $this->mkvmerge = new IniFile("$this->path/config/mkvmerge.ini", true); + $this->guard_file = "$this->path/AVE.ave-guard"; foreach($config_default->getAll() as $key => $value){ if(!$this->config->isSet($key)){ $this->config->set($key,$value); $changed = true; } } + if($this->version != $this->config->get('APP_VERSION')){ + $this->config->set('APP_VERSION', $this->version); + $changed = true; + $version_changed = true; + } else { + $version_changed = false; + } + + $check_for_updates = false; + if($this->config->get('AVE_CHECK_FOR_UPDATES') && $this->command != '--get-color'){ + $next_check_update = $this->config->get('APP_NEXT_CHECK_FOR_UPDATE', date("U") - 3600); + if(date("U") >= $next_check_update){ + $this->config->set('APP_NEXT_CHECK_FOR_UPDATE', date("U") + 86400 * $this->config->get('AVE_CHECK_FOR_UPDATES_DAYS')); + $changed = true; + $check_for_updates = true; + } + } + $config_default->close(); if($changed){ $this->config->save(); @@ -72,6 +104,68 @@ public function __construct(array $arguments){ $this->log_error = new Logs($this->config->get('AVE_LOG_FOLDER').DIRECTORY_SEPARATOR."$timestamp-Error.txt", true, true); $this->log_data = new Logs($this->config->get('AVE_DATA_FOLDER').DIRECTORY_SEPARATOR."$timestamp.txt", false, true); ini_set('memory_limit', $this->config->get('AVE_MAX_MEMORY_LIMIT')); + + $dev = file_exists($this->path.DIRECTORY_SEPARATOR."_get_package.cmd"); + + if($version_changed && !$dev){ + echo " Check for remove unused files...\r\n"; + $validation = $this->validate(['damaged' => false, 'unknown' => true, 'missing' => false]); + foreach($validation as $error){ + if($error['type'] == 'unknown'){ + $this->unlink($this->path.DIRECTORY_SEPARATOR.$error['file']); + } + } + } + + if($check_for_updates && !$dev){ + $this->tool_update(); + } + } + + public function check_for_updates(string &$version) : bool { + $ch = curl_init("https://raw.githubusercontent.com/AbyssMorgan/AVE-PHP/master/version"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + $response = curl_exec($ch); + if(!curl_errno($ch)){ + $version = $response; + return ($this->version != $response); + } else { + $error = curl_error($ch); + echo " Failed check for updates: $error\r\n"; + return false; + } + } + + public function download_update($version){ + echo " Download update...\r\n"; + $file = $this->path.DIRECTORY_SEPARATOR."AVE-PHP.7z"; + if(file_exists($file)) unlink($file); + $fh = fopen($file,"wb"); + $ch = curl_init("https://github.com/AbyssMorgan/AVE-PHP/releases/download/v$version/AVE-PHP.7z"); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_FILE, $fh); + $response = curl_exec($ch); + if(!curl_errno($ch)){ + exec("START \"\" \"\""); + $this->abort = true; + } else { + echo " Failed download updates: $error\r\n"; + } + } + + public function tool_update(){ + echo " Check for updates ...\r\n"; + $version = ''; + if($this->check_for_updates($version)){ + echo " Update available AVE-PHP v$version current v$this->version\r\n"; + echo " Download now (Y/N): "; + $line = $this->get_input(); + if(strtoupper($line[0] ?? 'N') == 'Y'){ + $this->download_update($version); + } + } } public function clear() : void { @@ -88,7 +182,25 @@ public function execute() : void { $this->select_tool(); } else { switch(strtolower($this->command)){ - case 'get_color': { + case '--guard-generate': { + $guard = new GuardDriver($this->guard_file); + $cwd = getcwd(); + chdir($this->path); + $guard->setFolders($this->folders_to_scan); + $guard->setFiles($this->files_to_scan); + $guard->generate(); + chdir($cwd); + break; + } + case '--put-version': { + file_put_contents("$this->path/version", $this->version); + break; + } + case '--guard-validate': { + echo print_r($this->validate(), true); + break; + } + case '--get-color': { echo $this->config->get('AVE_COLOR') ?? 'AF'; break; } @@ -100,6 +212,17 @@ public function execute() : void { } } + public function validate(array $flags = ['damaged' => true, 'unknown' => true, 'missing' => true]) : array { + $guard = new GuardDriver($this->guard_file); + $cwd = getcwd(); + chdir($this->path); + $guard->setFolders($this->folders_to_scan); + $guard->setFiles($this->files_to_scan); + $validation = $guard->validate($flags); + chdir($cwd); + return $validation; + } + public function set_tool(string $name) : void { $this->tool_name = $name; $this->subtool_name = ''; diff --git a/includes/main.php b/includes/main.php index e64e4e1..337282a 100644 --- a/includes/main.php +++ b/includes/main.php @@ -20,7 +20,7 @@ try { $ave = new AVE($argv); - $ave->execute(); + if(!$ave->abort) $ave->execute(); } catch(\Exception $e){ echo $e->getMessage()."\r\n"; diff --git a/version b/version index b123147..867e524 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.1 \ No newline at end of file +1.2.0 \ No newline at end of file