From 9cc2ba2efeece5e77c51890a5c2a0f227f633537 Mon Sep 17 00:00:00 2001 From: Abyss Morgan <14877700+AbyssMorgan@users.noreply.github.com> Date: Sat, 13 May 2023 11:07:13 +0200 Subject: [PATCH] v1.5.3 --- _get_package.cmd | 1 + includes/AVE.php | 294 ++-------------------------- includes/main.php | 1 + includes/services/AveCore.php | 308 ++++++++++++++++++++++++++++++ includes/services/CommandLine.php | 91 ++++++--- includes/tools/MediaSorter.php | 92 +++++++++ includes/tools/MediaTools.php | 144 ++++++++++++++ includes/tools/MySQLTools.php | 12 +- includes/tools/NamesGenerator.php | 4 +- version | 2 +- 10 files changed, 633 insertions(+), 316 deletions(-) create mode 100644 includes/services/AveCore.php diff --git a/_get_package.cmd b/_get_package.cmd index cd10b00..4879bea 100644 --- a/_get_package.cmd +++ b/_get_package.cmd @@ -5,4 +5,5 @@ SET PATH=%PATH%;%CD%\bin\main;%CD%\bin\php;%CD%\bin\imagick "%CD%\bin\php\php.exe" "%CD%\includes\main.php" --put-version FOR /F "tokens=*" %%s IN ('TYPE "%CD%\version"') DO SET _VERSION=%%s "%CD%\bin\main\7z.exe" a -mx9 -t7z "%CD%\AVE-PHP_v%_VERSION%.7z" "bin" "includes" "commands" "AVE-PHP.cmd" "README.md" "LICENSE" +"%CD%\bin\main\7z.exe" a -mx9 -t7z "%CD%\AVE-PHP_v%_VERSION%_UPDATE.7z" "includes" "commands" "AVE-PHP.cmd" "README.md" "LICENSE" PAUSE diff --git a/includes/AVE.php b/includes/AVE.php index 1812fb6..91644be 100644 --- a/includes/AVE.php +++ b/includes/AVE.php @@ -3,8 +3,7 @@ declare(strict_types=1); use App\Services\IniFile; -use App\Services\Logs; -use App\Services\CommandLine; +use App\Services\AveCore; use App\Tools\NamesGenerator; use App\Tools\FileFunctions; @@ -14,30 +13,15 @@ use App\Tools\CheckFileIntegrity; use App\Tools\MySQLTools; -class AVE extends CommandLine { +class AVE extends AveCore { - public IniFile $config; public IniFile $mkvmerge; - public Logs $log_event; - public Logs $log_error; - public Logs $log_data; - - public string $path; public string $app_data; public bool $abort = false; - public bool $open_log = false; public string $app_name = "AVE"; - public string $version = "1.5.2"; - - private ?string $command; - private array $arguments; - private string $logo; - private string $tool_name; - private string $subtool_name; - private array $folders_state = []; - private $tool; + public string $version = "1.5.3"; private array $folders_to_scan = [ 'bin', @@ -46,13 +30,7 @@ class AVE extends CommandLine { ]; public function __construct(array $arguments){ - parent::__construct(); - date_default_timezone_set(IntlTimeZone::createDefault()->getID()); - $this->path = $this->get_file_path(__DIR__."/.."); - unset($arguments[0]); - $this->command = $arguments[1] ?? null; - if(isset($arguments[1])) unset($arguments[1]); - $this->arguments = array_values($arguments); + parent::__construct($arguments); $this->logo = "\r\n AVE-PHP Toolkit v$this->version by Abyss Morgan\r\n"; $changed = false; @@ -148,11 +126,6 @@ public function __construct(array $arguments){ } } - public function get_version_number(string $version) : int { - $ver = explode(".", $version); - return 10000 * intval($ver[0]) + 100*intval($ver[1]) + intval($ver[2]); - } - 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); @@ -202,15 +175,6 @@ public function tool_update(bool $response = false) : void { } } - public function clear() : void { - $this->cls(); - if($this->config->get('AVE_SHOW_LOGO')){ - echo "$this->logo\r\n"; - } else { - echo "\r\n"; - } - } - public function execute() : void { switch(strtolower($this->command ?? '')){ case '--make-backup': { @@ -235,7 +199,7 @@ public function execute() : void { while(!$this->abort){ $this->abort = $this->select_tool(); } - $this->exit(10, $this->open_log); + $this->exit(10, false); break; } default: { @@ -245,31 +209,6 @@ public function execute() : void { } } - public function set_tool(string $name) : void { - $this->tool_name = $name; - $this->subtool_name = ''; - $this->title("$this->app_name v$this->version > $this->tool_name"); - $this->write_log("Set Tool: $this->tool_name"); - } - - public function set_subtool(string $name) : void { - $this->subtool_name = $name; - $this->title("$this->app_name v$this->version > $this->tool_name > $this->subtool_name"); - $this->write_log("Set Tool: $this->tool_name > $this->subtool_name"); - } - - public function set_progress(int $progress, int $errors) : void { - $title = "$this->app_name v$this->version > $this->tool_name"; - if(!empty($this->subtool_name)) $title .= " > $this->subtool_name"; - $this->title("$title > Files: $progress Errors: $errors"); - } - - public function set_progress_ex(string $label, int $progress, int $total) : void { - $title = "$this->app_name v$this->version > $this->tool_name"; - if(!empty($this->subtool_name)) $title .= " > $this->subtool_name"; - $this->title("$title > $label: $progress / $total"); - } - public function select_tool() : bool { $this->write_log("Select Tool"); $this->clear(); @@ -285,7 +224,9 @@ public function select_tool() : bool { ' 4 - Media Tools', ' 5 - Check File Integrity', ' 6 - MySQL Tools', + ' ', ' C - Open config folder', + ' H - Show documentation', ' U - Check for updates', ]); @@ -324,12 +265,16 @@ public function select_tool() : bool { $this->open_file($this->app_data, ""); break; } + case 'H': { + $this->clear(); + $this->open_url("https://github.com/AbyssMorgan/AVE-PHP/wiki"); + break; + } case 'U': { $this->clear(); $this->title("$this->app_name v$this->version > CheckForUpdates"); $this->tool_update(true); return true; - break; } } if(!$this->abort && !is_null($this->tool)){ @@ -338,221 +283,6 @@ public function select_tool() : bool { return false; } - public function select_action() : bool { - do { - $this->clear(); - $this->title("$this->app_name v$this->version > $this->tool_name"); - $this->tool->help(); - echo " Action: "; - $line = $this->get_input(); - if($line == '#') return false; - $response = $this->tool->action($line); - } - while(!$response); - return true; - } - - public function setup_folders(array $folders) : void { - $this->folders_state = []; - foreach($folders as $folder){ - $this->folders_state[$folder] = file_exists($folder) ? '' : '[NOT EXISTS]'; - $this->write_log("Scan: $folder"); - } - $this->print_folders_state(); - } - - public function set_folder_done(string $folder) : void { - $this->folders_state[$folder] = '[DONE]'; - $this->print_folders_state(); - } - - public function print_folders_state() : void { - $this->clear(); - foreach($this->folders_state as $folder_name => $state){ - echo " Scan: \"$folder_name\" $state\r\n"; - } - } - - public function rmdir(string $path, bool $log = true) : bool { - if(!file_exists($path) || !is_dir($path)) return false; - if(rmdir($path)){ - if($log) $this->write_log("DELETE \"$path\""); - return true; - } else { - if($log) $this->write_error("FAILED DELETE \"$path\""); - return false; - } - } - - public function unlink(string $path, bool $log = true) : bool { - if(!file_exists($path) || is_dir($path)) return false; - if(unlink($path)){ - if($log) $this->write_log("DELETE \"$path\""); - return true; - } else { - if($log) $this->write_error("FAILED DELETE \"$path\""); - return false; - } - } - - public function mkdir(string $path, bool $log = true) : bool { - if(file_exists($path) && is_dir($path)) return true; - if(mkdir($path, 0777, true)){ - if($log) $this->write_log("MKDIR \"$path\""); - return true; - } else { - if($log) $this->write_error("FAILED MKDIR \"$path\""); - return false; - } - } - - public function rename(string $from, string $to, bool $log = true) : bool { - if($from == $to) return true; - if(file_exists($to) && pathinfo($from, PATHINFO_DIRNAME) != pathinfo($to, PATHINFO_DIRNAME)){ - if($log) $this->write_error("FAILED RENAME \"$from\" \"$to\" FILE EXIST"); - return false; - } - if(rename($from, $to)){ - if($log) $this->write_log("RENAME \"$from\" \"$to\""); - return true; - } else { - if($log) $this->write_error("FAILED RENAME \"$from\" \"$to\""); - return false; - } - } - - public function copy(string $from, string $to, bool $log = true) : bool { - if($from == $to) return true; - if(file_exists($to) && pathinfo($from, PATHINFO_DIRNAME) != pathinfo($to, PATHINFO_DIRNAME)){ - if($log) $this->write_error("FAILED COPY \"$from\" \"$to\" FILE EXIST"); - return false; - } - if(copy($from, $to)){ - if($log) $this->write_log("COPY \"$from\" \"$to\""); - return true; - } else { - if($log) $this->write_error("FAILED COPY \"$from\" \"$to\""); - return false; - } - } - - public function print_help(array $help) : void { - echo implode("\r\n", $help)."\r\n\r\n"; - } - - public function progress(int|float $count, int|float $total) : void { - if($total > 0){ - $percent = sprintf("%.02f", ($count / $total) * 100.0); - echo " Progress: $percent % \r"; - } - } - - public function getHashFromIDX(string $path, array &$keys, bool $progress) : int { - if(!file_exists($path)) return 0; - $cnt = 0; - $size = filesize($path); - $fp = @fopen($path, "r"); - if($fp){ - while(($line = fgets($fp)) !== false){ - $line = trim($line); - $keys[pathinfo($line, PATHINFO_FILENAME)] = $line; - $cnt++; - if($progress) $this->progress(ftell($fp), $size); - } - fclose($fp); - } - return $cnt; - } - - public function formatBytes(int $bytes, int $precision = 2) : string { - if($bytes <= 0) return '0.00 B'; - $i = floor(log($bytes)/log(1024)); - $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - return sprintf('%.'.$precision.'f', $bytes/pow(1024, $i)).' '.$sizes[$i]; - } - - public function getFiles(string $path, array|null $extensions = null, array|null $except = null) : array { - $data = []; - $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)); - foreach($files as $file){ - if($file->isDir() || $file->isLink()) continue; - if(!is_null($extensions) && !in_array(strtolower($file->getExtension()), $extensions)) continue; - if(!is_null($except) && in_array(strtolower($file->getExtension()), $except)) continue; - $fp = $file->getRealPath(); - if(!$fp) continue; - array_push($data, $fp); - } - return $data; - } - - public function getFolders(string $path) : array { - $data = []; - $files = new DirectoryIterator($path); - array_push($data, $path); - foreach($files as $file){ - if($file->isDir() && !$file->isDot()){ - $data = array_merge($data, $this->getFolders($file->getRealPath())); - } - } - return $data; - } - - public function write_log(string|array $data) : void { - if($this->config->get('AVE_LOG_EVENT')){ - $this->log_event->write($data); - } - } - - public function write_error(string|array $data) : void { - if($this->config->get('AVE_LOG_ERROR')){ - $this->log_error->write($data); - } - } - - public function write_data(string|array $data) : void { - $this->log_data->write($data); - } - - public function exit(int $seconds = 10, bool $open_log = false) : void { - $this->write_log("Exit"); - $this->open_logs($open_log, false); - $this->timeout($seconds); - } - - public function init_logs(){ - $timestamp = date("Y-m-d His"); - $this->log_event = new Logs($this->get_file_path($this->config->get('AVE_LOG_FOLDER')."/$timestamp-Event.txt"), true, true); - $this->log_error = new Logs($this->get_file_path($this->config->get('AVE_LOG_FOLDER')."/$timestamp-Error.txt"), true, true); - $this->log_data = new Logs($this->get_file_path($this->config->get('AVE_DATA_FOLDER')."/$timestamp.txt"), false, true); - } - - public function open_logs(bool $open_event = false, bool $init = true) : void { - $this->log_event->close(); - $this->log_error->close(); - $this->log_data->close(); - if($this->config->get('AVE_OPEN_LOG_EVENT') && $open_event && file_exists($this->log_event->getPath())){ - $this->open_file($this->log_event->getPath()); - } - if(file_exists($this->log_data->getPath())){ - $this->open_file($this->log_data->getPath()); - } - if(file_exists($this->log_error->getPath())){ - $this->open_file($this->log_error->getPath()); - } - if($init) $this->init_logs(); - } - - private function timeout(int $seconds) : void { - $this->title("$this->app_name v$this->version > Exit $seconds seconds"); - if($seconds > 0){ - sleep(1); - $seconds--; - $this->timeout($seconds); - } else { - exit(0); - } - } - } ?> diff --git a/includes/main.php b/includes/main.php index 8a3110c..42bb38b 100644 --- a/includes/main.php +++ b/includes/main.php @@ -15,6 +15,7 @@ require_once("$includes_path/services/Logs.php"); require_once("$includes_path/services/IniFile.php"); require_once("$includes_path/services/CommandLine.php"); + require_once("$includes_path/services/AveCore.php"); require_once("$includes_path/services/GuardPattern.php"); require_once("$includes_path/services/GuardDriver.php"); require_once("$includes_path/services/FaceDetector.php"); diff --git a/includes/services/AveCore.php b/includes/services/AveCore.php new file mode 100644 index 0000000..048ce89 --- /dev/null +++ b/includes/services/AveCore.php @@ -0,0 +1,308 @@ +getID()); + $this->path = $this->get_file_path(__DIR__."/../.."); + $this->logo = ''; + } + + public function select_action() : bool { + do { + $this->clear(); + $this->title("$this->app_name v$this->version > $this->tool_name"); + $this->tool->help(); + echo " Action: "; + $line = $this->get_input(); + if($line == '#') return false; + $response = $this->tool->action($line); + } + while(!$response); + return true; + } + + public function setup_folders(array $folders) : void { + $this->folders_state = []; + foreach($folders as $folder){ + $this->folders_state[$folder] = file_exists($folder) ? '' : '[NOT EXISTS]'; + $this->write_log("Scan: $folder"); + } + $this->print_folders_state(); + } + + public function set_folder_done(string $folder) : void { + $this->folders_state[$folder] = '[DONE]'; + $this->print_folders_state(); + } + + public function print_folders_state() : void { + $this->clear(); + foreach($this->folders_state as $folder_name => $state){ + echo " Scan: \"$folder_name\" $state\r\n"; + } + } + + public function set_tool(string $name) : void { + $this->tool_name = $name; + $this->subtool_name = ''; + $this->title("$this->app_name v$this->version > $this->tool_name"); + $this->write_log("Set Tool: $this->tool_name"); + } + + public function set_subtool(string $name) : void { + $this->subtool_name = $name; + $this->title("$this->app_name v$this->version > $this->tool_name > $this->subtool_name"); + $this->write_log("Set Tool: $this->tool_name > $this->subtool_name"); + } + + public function set_progress(int $progress, int $errors) : void { + $title = "$this->app_name v$this->version > $this->tool_name"; + if(!empty($this->subtool_name)) $title .= " > $this->subtool_name"; + $this->title("$title > Files: $progress Errors: $errors"); + } + + public function set_progress_ex(string $label, int $progress, int $total) : void { + $title = "$this->app_name v$this->version > $this->tool_name"; + if(!empty($this->subtool_name)) $title .= " > $this->subtool_name"; + $this->title("$title > $label: $progress / $total"); + } + + public function clear() : void { + $this->cls(); + if($this->config->get('AVE_SHOW_LOGO', false)){ + echo "$this->logo\r\n"; + } else { + echo "\r\n"; + } + } + + public function get_version_number(string $version) : int { + $ver = explode(".", $version); + return 10000 * intval($ver[0]) + 100*intval($ver[1]) + intval($ver[2]); + } + + public function print_help(array $help) : void { + echo implode("\r\n", $help)."\r\n\r\n"; + } + + public function progress(int|float $count, int|float $total) : void { + if($total > 0){ + $percent = sprintf("%.02f", ($count / $total) * 100.0); + echo " Progress: $percent % \r"; + } + } + + public function is_valid_label(string $label) : bool { + return preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $label) == 1; + } + + public function progress_ex(string $label, int|float $count, int|float $total) : void { + if($total > 0){ + $percent = sprintf("%.02f", ($count / $total) * 100.0); + echo " $label Progress: $percent % \r"; + } + } + + public function getHashFromIDX(string $path, array &$keys, bool $progress) : int { + if(!file_exists($path)) return 0; + $cnt = 0; + $size = filesize($path); + $fp = @fopen($path, "r"); + if($fp){ + while(($line = fgets($fp)) !== false){ + $line = trim($line); + $keys[pathinfo($line, PATHINFO_FILENAME)] = $line; + $cnt++; + if($progress) $this->progress(ftell($fp), $size); + } + fclose($fp); + } + return $cnt; + } + + public function formatBytes(int $bytes, int $precision = 2) : string { + if($bytes <= 0) return '0.00 B'; + $i = floor(log($bytes)/log(1024)); + $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + return sprintf('%.'.$precision.'f', $bytes/pow(1024, $i)).' '.$sizes[$i]; + } + + public function getFiles(string $path, array|null $extensions = null, array|null $except = null) : array { + $data = []; + $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)); + foreach($files as $file){ + if($file->isDir() || $file->isLink()) continue; + if(!is_null($extensions) && !in_array(strtolower($file->getExtension()), $extensions)) continue; + if(!is_null($except) && in_array(strtolower($file->getExtension()), $except)) continue; + $fp = $file->getRealPath(); + if(!$fp) continue; + array_push($data, $fp); + } + return $data; + } + + public function getFolders(string $path) : array { + $data = []; + $files = new DirectoryIterator($path); + array_push($data, $path); + foreach($files as $file){ + if($file->isDir() && !$file->isDot()){ + $data = array_merge($data, $this->getFolders($file->getRealPath())); + } + } + return $data; + } + + public function exit(int $seconds = 10, bool $open_log = false) : void { + $this->write_log("Exit"); + $this->open_logs($open_log, false); + $this->timeout($seconds); + } + + public function init_logs(){ + $timestamp = date("Y-m-d His"); + $this->log_event = new Logs($this->get_file_path($this->config->get('AVE_LOG_FOLDER')."/$timestamp-Event.txt"), true, true); + $this->log_error = new Logs($this->get_file_path($this->config->get('AVE_LOG_FOLDER')."/$timestamp-Error.txt"), true, true); + $this->log_data = new Logs($this->get_file_path($this->config->get('AVE_DATA_FOLDER')."/$timestamp.txt"), false, true); + } + + public function open_logs(bool $open_event = false, bool $init = true) : void { + $this->log_event->close(); + $this->log_error->close(); + $this->log_data->close(); + if($this->config->get('AVE_OPEN_LOG_EVENT', true) && $open_event && file_exists($this->log_event->getPath())){ + $this->open_file($this->log_event->getPath()); + } + if(file_exists($this->log_data->getPath())){ + $this->open_file($this->log_data->getPath()); + } + if(file_exists($this->log_error->getPath())){ + $this->open_file($this->log_error->getPath()); + } + if($init) $this->init_logs(); + } + + public function timeout(int $seconds) : void { + $this->title("$this->app_name v$this->version > Exit $seconds seconds"); + if($seconds > 0){ + sleep(1); + $seconds--; + $this->timeout($seconds); + } else { + exit(0); + } + } + + public function write_log(string|array $data) : void { + if($this->config->get('AVE_LOG_EVENT', true)){ + $this->log_event->write($data); + } + } + + public function write_error(string|array $data) : void { + if($this->config->get('AVE_LOG_ERROR', true)){ + $this->log_error->write($data); + } + } + + public function write_data(string|array $data) : void { + $this->log_data->write($data); + } + + public function rmdir(string $path, bool $log = true) : bool { + if(!file_exists($path) || !is_dir($path)) return false; + if(rmdir($path)){ + if($log) $this->write_log("DELETE \"$path\""); + return true; + } else { + if($log) $this->write_error("FAILED DELETE \"$path\""); + return false; + } + } + + public function unlink(string $path, bool $log = true) : bool { + if(!file_exists($path) || is_dir($path)) return false; + if(unlink($path)){ + if($log) $this->write_log("DELETE \"$path\""); + return true; + } else { + if($log) $this->write_error("FAILED DELETE \"$path\""); + return false; + } + } + + public function mkdir(string $path, bool $log = true) : bool { + if(file_exists($path) && is_dir($path)) return true; + if(mkdir($path, 0755, true)){ + if($log) $this->write_log("MKDIR \"$path\""); + return true; + } else { + if($log) $this->write_error("FAILED MKDIR \"$path\""); + return false; + } + } + + public function rename(string $from, string $to, bool $log = true) : bool { + if($from == $to) return true; + if(file_exists($to) && pathinfo($from, PATHINFO_DIRNAME) != pathinfo($to, PATHINFO_DIRNAME)){ + if($log) $this->write_error("FAILED RENAME \"$from\" \"$to\" FILE EXIST"); + return false; + } + $dir = pathinfo($to, PATHINFO_DIRNAME); + if(!file_exists($dir)) $this->mkdir($dir); + if(rename($from, $to)){ + if($log) $this->write_log("RENAME \"$from\" \"$to\""); + return true; + } else { + if($log) $this->write_error("FAILED RENAME \"$from\" \"$to\""); + return false; + } + } + + public function copy(string $from, string $to, bool $log = true) : bool { + if($from == $to) return true; + if(file_exists($to) && pathinfo($from, PATHINFO_DIRNAME) != pathinfo($to, PATHINFO_DIRNAME)){ + if($log) $this->write_error("FAILED COPY \"$from\" \"$to\" FILE EXIST"); + return false; + } + $dir = pathinfo($to, PATHINFO_DIRNAME); + if(!file_exists($dir)) $this->mkdir($dir); + if(copy($from, $to)){ + if($log) $this->write_log("COPY \"$from\" \"$to\""); + return true; + } else { + if($log) $this->write_error("FAILED COPY \"$from\" \"$to\""); + return false; + } + } + +} + +?> diff --git a/includes/services/CommandLine.php b/includes/services/CommandLine.php index 425ed35..e6da023 100644 --- a/includes/services/CommandLine.php +++ b/includes/services/CommandLine.php @@ -6,8 +6,16 @@ class CommandLine { - public function __construct(){ - + public ?string $command; + public array $arguments; + public bool $windows; + + public function __construct(array $arguments){ + unset($arguments[0]); + $this->command = $arguments[1] ?? null; + if(isset($arguments[1])) unset($arguments[1]); + $this->arguments = array_values($arguments); + $this->windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; } public function cmd_escape(string $text) : string { @@ -15,11 +23,15 @@ public function cmd_escape(string $text) : string { } public function title(string $title) : void { - system("TITLE ".$this->cmd_escape($title)); + if($this->windows){ + system("TITLE ".$this->cmd_escape($title)); + } } public function cls() : void { - popen('cls', 'w'); + if($this->windows){ + popen('cls', 'w'); + } } public function get_input() : string { @@ -32,7 +44,9 @@ public function get_input_no_trim() : string { public function pause(?string $message = null) : void { if(!is_null($message)) echo $message; - system("PAUSE > nul"); + if($this->windows){ + system("PAUSE > nul"); + } } public function get_folders(string $string) : array { @@ -45,17 +59,17 @@ public function get_folders(string $string) : array { while($offset < $length){ if(substr($string, $offset, 1) == '"'){ $end = strpos($string, '"', $offset+1); - array_push($folders, substr($string, $offset+1, $end - $offset-1)); + array_push($folders, $this->get_file_path(substr($string, $offset+1, $end - $offset-1))); $offset = $end + 1; } else if(substr($string, $offset, 1) == ' '){ $offset++; } else { $end = strpos($string, ' ', $offset); if($end !== false){ - array_push($folders, substr($string, $offset, $end - $offset)); + array_push($folders, $this->get_file_path(substr($string, $offset, $end - $offset))); $offset = $end + 1; } else { - array_push($folders, substr($string, $offset)); + array_push($folders, $this->get_file_path(substr($string, $offset))); $offset = $length; } } @@ -70,34 +84,53 @@ public function get_variable(string $string) : string { } public function open_file(string $path, string $params = '/MIN') : void { - if(file_exists($path)){ + if($this->windows && file_exists($path)){ exec("START $params \"\" \"$path\""); } } + public function open_url(string $url) : void { + if(strpos($url, "https://") !== false || strpos($url, "http://") !== false){ + exec("START \"\" \"$url\""); + } + } + public function get_file_attributes(string $path) : array { - exec("ATTRIB \"$path\"", $var); - $attributes = substr($var[0],0,21); - return [ - 'R' => (strpos($attributes, "R") !== false), - 'A' => (strpos($attributes, "A") !== false), - 'S' => (strpos($attributes, "S") !== false), - 'H' => (strpos($attributes, "H") !== false), - 'I' => (strpos($attributes, "I") !== false), - ]; + if($this->windows){ + exec("ATTRIB \"$path\"", $var); + $attributes = str_replace($path, '', $var[0]); + return [ + 'R' => (strpos($attributes, "R") !== false), + 'A' => (strpos($attributes, "A") !== false), + 'S' => (strpos($attributes, "S") !== false), + 'H' => (strpos($attributes, "H") !== false), + 'I' => (strpos($attributes, "I") !== false), + ]; + } else { + return [ + 'R' => false, + 'A' => false, + 'S' => false, + 'H' => false, + 'I' => false, + ]; + } } public function set_file_attributes(string $path, bool|null $r = null, bool|null $a = null, bool|null $s = null, bool|null $h = null, bool|null $i = null) : void { - $attributes = ''; - if(!is_null($r)) $attributes .= ($r ? '+' : '-').'R '; - if(!is_null($a)) $attributes .= ($a ? '+' : '-').'A '; - if(!is_null($s)) $attributes .= ($s ? '+' : '-').'S '; - if(!is_null($h)) $attributes .= ($h ? '+' : '-').'H '; - if(!is_null($i)) $attributes .= ($i ? '+' : '-').'I '; - exec("ATTRIB $attributes \"$path\""); + if($this->windows){ + $attributes = ''; + if(!is_null($r)) $attributes .= ($r ? '+' : '-').'R '; + if(!is_null($a)) $attributes .= ($a ? '+' : '-').'A '; + if(!is_null($s)) $attributes .= ($s ? '+' : '-').'S '; + if(!is_null($h)) $attributes .= ($h ? '+' : '-').'H '; + if(!is_null($i)) $attributes .= ($i ? '+' : '-').'I '; + exec("ATTRIB $attributes \"$path\""); + } } public function is_valid_device(string $path) : bool { + if(!$this->windows) return true; if(substr($path, 1, 1) == ':'){ return file_exists(substr($path, 0, 3)); } else if(substr($path, 0, 2) == "\\\\"){ @@ -115,6 +148,14 @@ public function get_file_path(string $path) : string { return str_replace(["/", "\\"], DIRECTORY_SEPARATOR, $path); } + public function getComputerName() : string { + if($this->windows){ + return $this->get_variable("%COMPUTERNAME%"); + } else { + return shell_exec('hostname'); + } + } + } ?> diff --git a/includes/tools/MediaSorter.php b/includes/tools/MediaSorter.php index 3f34938..5b69ce8 100644 --- a/includes/tools/MediaSorter.php +++ b/includes/tools/MediaSorter.php @@ -29,6 +29,7 @@ public function help() : void { ' 2 - Sort Gif: Animated', ' 3 - Sort Media: Quality', ' 4 - Sort Images: Colors count', + ' 5 - Sort Videos: Auto detect series name', ]); } @@ -41,6 +42,7 @@ public function action(string $action) : bool { case '2': return $this->ToolSortGifAnimated(); case '3': return $this->ToolSortMedia(); case '4': return $this->ToolSortImagesColor(); + case '5': return $this->ToolSortVideosAutoDetectSeriesName(); } return false; } @@ -413,6 +415,96 @@ public function ToolSortImagesColor() : bool { return false; } + public function ToolSortVideosAutoDetectSeriesName() : bool { + $this->ave->clear(); + $this->ave->set_subtool("SortVideosAutoDetectSeriesName"); + + set_input: + echo " Input: "; + $line = $this->ave->get_input(); + if($line == '#') return false; + $folders = $this->ave->get_folders($line); + if(!isset($folders[0])) goto set_input; + $input = $folders[0]; + + if(!file_exists($input) || !is_dir($input)){ + echo " Invalid input folder\r\n"; + goto set_input; + } + + $media = new MediaFunctions(); + + $progress = 0; + $errors = 0; + $this->ave->set_progress($progress, $errors); + $video_extensions = explode(" ", $this->ave->config->get('AVE_EXTENSIONS_VIDEO')); + $files = $this->ave->getFiles($input, $video_extensions); + $items = 0; + $total = count($files); + foreach($files as $file){ + $items++; + if(!file_exists($file)) continue; + + $file_name = str_replace(['SEASON','EPISODE',' '], ['S','E',''], strtoupper(pathinfo($file, PATHINFO_FILENAME))); + if(preg_match("/S[0-9]{1,2}E[0-9]{1,3}(.*)E[0-9]{1,3}/", $file_name, $mathes) == 1){ + $escaped_name = preg_replace("/[^SE0-9]/i", "", $mathes[0]); + $marker = $mathes[0]; + } else if(preg_match("/S[0-9]{1,2}E[0-9]{1,3}/", $file_name, $mathes) == 1){ + $escaped_name = preg_replace("/[^SE0-9]/i", "", $mathes[0]); + $marker = $mathes[0]; + } else if(preg_match("/\[S[0-9]{2}\.E[0-9]{1,3}\]/", $file_name, $mathes) == 1){ + $escaped_name = preg_replace("/[^SE0-9]/i", "", $mathes[0]); + $marker = $mathes[0]; + } else if(preg_match("/(\[S0\.)(E[0-9]{1,3})\]/", $file_name, $mathes) == 1){ + $escaped_name = "S01".preg_replace("/[^E0-9]/i", "", $mathes[2]); + $marker = $mathes[2]; + } else { + $escaped_name = ''; + $this->ave->write_error("FAILED GET SERIES ID \"$file\""); + $errors++; + } + if(!empty($escaped_name)){ + $end = strpos($file_name, $marker); + if($end === false){ + $this->ave->write_error("FAILED GET MARKER \"$file\""); + $errors++; + } else { + $folder_name = str_replace(['_', '.', "\u{00A0}"], ' ', substr(pathinfo($file, PATHINFO_FILENAME), 0, $end)); + $folder_name = str_replace([';', '@', '#', '~', '!', '$', '%', '^', '&'], '', $folder_name); + while(strpos($folder_name, ' ') !== false){ + $folder_name = str_replace(' ', ' ', $folder_name); + } + $folder_name = trim($folder_name, ' '); + if(empty($folder_name)){ + $this->ave->write_error("ESCAPED FOLDER NAME IS EMPTY \"$file\""); + $errors++; + } else { + $new_name = $this->ave->get_file_path("$input/$folder_name/".pathinfo($file, PATHINFO_BASENAME)); + $new_path = $this->ave->get_file_path("$input/$folder_name"); + if(!file_exists($new_path)) $this->ave->mkdir($new_path); + if(file_exists($new_name) && strtoupper($new_name) != strtoupper($file)){ + $this->ave->write_error("DUPLICATE \"$file\" AS \"$new_name\""); + $errors++; + } else { + if($this->ave->rename($file, $new_name)){ + $progress++; + } else { + $errors++; + } + } + } + } + } + $this->ave->progress($items, $total); + $this->ave->set_progress($progress, $errors); + } + $this->ave->progress($items, $total); + + $this->ave->open_logs(true); + $this->ave->pause(" Operation done, press enter to back to menu"); + return false; + } + } ?> diff --git a/includes/tools/MediaTools.php b/includes/tools/MediaTools.php index 5f7c1e1..69ba0ae 100644 --- a/includes/tools/MediaTools.php +++ b/includes/tools/MediaTools.php @@ -5,6 +5,7 @@ namespace App\Tools; use AVE; +use Imagick; use App\Dictionaries\MediaOrientation; use App\Services\MediaFunctions; @@ -30,6 +31,7 @@ public function help() : void { ' 1 - Merge: Video + SRT', ' 2 - Avatar generator', ' 3 - Video: Fetch media info', + ' 4 - Image converter' ]); } @@ -41,6 +43,7 @@ public function action(string $action) : bool { case '1': return $this->ToolMergeVideoSubtitles(); case '2': return $this->ToolAvatarGenerator(); case '3': return $this->ToolVideoFetchMediaInfo(); + case '4': return $this->ToolImageConverter(); } return false; } @@ -398,6 +401,147 @@ public function ToolVideoFetchMediaInfo() : bool { return false; } + public function ToolImageConverter() : bool { + $this->ave->set_subtool("ImageConverter"); + + set_mode: + $this->ave->clear(); + $this->ave->print_help([ + ' Modes:', + ' 0 - Image > WEBP', + ' 1 - Image > JPG', + ' 2 - Image > PNG', + ' 3 - Image > GIF', + ]); + + echo " Mode: "; + $line = $this->ave->get_input(); + if($line == '#') return false; + + $this->params = [ + 'mode' => strtolower($line[0] ?? '?'), + ]; + + if(!in_array($this->params['mode'],['0','1','2','3'])) goto set_mode; + $this->ave->clear(); + + set_input: + echo " Input: "; + $line = $this->ave->get_input(); + if($line == '#') return false; + $folders = $this->ave->get_folders($line); + if(!isset($folders[0])) goto set_input; + $input = $folders[0]; + + if(!file_exists($input) || !is_dir($input)){ + echo " Invalid input folder\r\n"; + goto set_input; + } + + set_output: + echo " Output: "; + $line = $this->ave->get_input(); + if($line == '#') return false; + $folders = $this->ave->get_folders($line); + if(!isset($folders[0])) goto set_output; + $output = $folders[0]; + + if($input == $output){ + echo " Output folder must be different than input folder\r\n"; + goto set_output; + } + + if((file_exists($output) && !is_dir($output)) || !$this->ave->mkdir($output)){ + echo " Invalid output folder\r\n"; + goto set_output; + } + + $progress = 0; + $errors = 0; + + $extensions = explode(" ", $this->ave->config->get('AVE_EXTENSIONS_PHOTO')); + $files = $this->ave->getFiles($input); + $items = 0; + $total = count($files); + foreach($files as $file){ + $items++; + $this->ave->set_progress($progress, $errors); + if(!file_exists($file)) continue; + if(!in_array(pathinfo($file, PATHINFO_EXTENSION), $extensions)){ + $this->ave->write_error("FILE FORMAT NOT SUPORTED \"$file\""); + $errors++; + continue; + } + $folder = pathinfo($file, PATHINFO_DIRNAME); + $directory = str_replace($input, $output, $folder); + if(!file_exists($directory)){ + if($this->ave->mkdir($directory)){ + $progress++; + } else { + $errors++; + continue; + } + } + $new_name = $this->ave->get_file_path("$directory/".pathinfo($file, PATHINFO_FILENAME)); + if(file_exists($new_name)){ + $this->ave->write_error("FILE ALREADY EXISTS \"$new_name\""); + $errors++; + continue; + } + $image = new Imagick($file); + if(!$image->valid()){ + $this->ave->write_error("FAILED READ IMAGE \"$file\" BY IMAGICK"); + $errors++; + continue; + } + switch(intval($this->params['mode'])){ + case 0: { + $image->setImageFormat('webp'); + $image->setOption('webp:lossless', 'true'); + $new_name .= ".webp"; + break; + } + case 1: { + $image->setImageFormat('jpeg'); + $new_name .= ".jpg"; + break; + } + case 2: { + $image->setImageFormat('png'); + $new_name .= ".png"; + break; + } + case 3: { + $image->setImageFormat('gif'); + $new_name .= ".gif"; + break; + } + } + $image->setImageCompressionQuality(100); + try { + $image->writeImage($new_name); + } + catch(\Exception $e){ + $this->ave->write_error($e->getMessage()); + } + $image->destroy(); + if(!file_exists($new_name)){ + $this->ave->write_error("FAILED SAVE FILE \"$new_name\""); + $errors++; + continue; + } else { + $this->ave->write_log("CONVERT \"$file\" TO \"$new_name\""); + $progress++; + } + $this->ave->progress($items, $total); + $this->ave->set_progress($progress, $errors); + } + + $this->ave->open_logs(true); + $this->ave->pause(" Operation done, press enter to back to menu"); + return false; + } + } ?> diff --git a/includes/tools/MySQLTools.php b/includes/tools/MySQLTools.php index 651c049..4f8bfae 100644 --- a/includes/tools/MySQLTools.php +++ b/includes/tools/MySQLTools.php @@ -72,7 +72,7 @@ public function ToolConfigureConnection() : bool { echo " Label: "; $label = $this->ave->get_input(); if($label == '#') return false; - if(!preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $label)){ + if(!$this->ave->is_valid_label($label)){ echo " Invalid label\r\n"; goto set_label; } @@ -187,7 +187,7 @@ public function ToolRemoveConnection() : bool { echo " Label: "; $label = $this->ave->get_input(); if($label == '#') return false; - if(!preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $label)){ + if(!$this->ave->is_valid_label($label)){ echo " Invalid label\r\n"; goto set_label; } @@ -242,7 +242,7 @@ public function ToolMakeBackup() : bool { echo " Label: "; $label = $this->ave->get_input(); if($label == '#') return false; - if(!preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $label)){ + if(!$this->ave->is_valid_label($label)){ echo " Invalid label\r\n"; goto set_label; } @@ -322,7 +322,7 @@ public function ToolMakeClone() : bool { echo " Source label: "; $source = $this->ave->get_input(); if($source == '#') return false; - if(!preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $source)){ + if(!$this->ave->is_valid_label($source)){ echo " Invalid label\r\n"; goto set_label_source; } @@ -346,7 +346,7 @@ public function ToolMakeClone() : bool { echo " Destination label: "; $destination = $this->ave->get_input(); if($destination == '#') return false; - if(!preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $destination)){ + if(!$this->ave->is_valid_label($destination)){ echo " Invalid label\r\n"; goto set_label_destination; } @@ -412,7 +412,7 @@ public function ToolMakeClone() : bool { } public function ToolMakeBackupCMD(string $label) : bool { - if(!preg_match('/(?=[a-zA-Z0-9_\-]{3,20}$)/i', $label)){ + if(!$this->ave->is_valid_label($label)){ echo " Invalid label \"$label\"\r\n"; return false; } diff --git a/includes/tools/NamesGenerator.php b/includes/tools/NamesGenerator.php index b1730c0..a2f4c47 100644 --- a/includes/tools/NamesGenerator.php +++ b/includes/tools/NamesGenerator.php @@ -350,8 +350,8 @@ public function ToolVideoGenerator() : bool { ' 0 - CheckSum', ' 1 - Resolution', ' 2 - Thumbnail', - ' 3 - Full: CheckSum + Resolution + Thumbnail', - ' 4 - Lite: CheckSum + Resolution', + ' 3 - CheckSum + Resolution + Thumbnail', + ' 4 - CheckSum + Resolution', ' ?0 - md5 (default)', ' ?1 - sha256', ' ?2 - crc32', diff --git a/version b/version index a73b432..1d5e9e0 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.5.2 \ No newline at end of file +1.5.3 \ No newline at end of file