Code cleanup, refactoring and bug fixes:
- Fixed first time running warnings about missing packages.json / desc files - Fixed bug preventing Select All from being able to be clicked - Fixed bug in select all and dependent checkboxes / race condition - Eliminated vertical space gap between page header and table
This commit is contained in:
@ -1,38 +1,36 @@
|
||||
<?php
|
||||
|
||||
// Progress bar for curl download
|
||||
function progress_bar($download_size, $downloaded_size, $upload_size = null, $uploaded_size = null)
|
||||
{
|
||||
function progress_bar($download_size, $downloaded_size, $upload_size = null, $uploaded_size = null) {
|
||||
ob_start();
|
||||
static $previousProgress = 0;
|
||||
|
||||
|
||||
if ($download_size == 0)
|
||||
$progress = 0;
|
||||
else
|
||||
$progress = round($downloaded_size * 100 / $download_size);
|
||||
|
||||
if ( $progress > $previousProgress){
|
||||
$previousProgress = $progress;
|
||||
$pct=(double)($progress/100);
|
||||
$bar=round($pct * 30);
|
||||
$pct_disp=number_format($pct * 100, 0);
|
||||
$status_bar="\r[";
|
||||
$status_bar.=str_repeat("|", $bar);
|
||||
|
||||
if($bar < 30){
|
||||
$status_bar.=">";
|
||||
$status_bar.=str_repeat("-", 30 - $bar);
|
||||
if ($progress > $previousProgress) {
|
||||
$previousProgress = $progress;
|
||||
$pct = (double)($progress / 100);
|
||||
$bar = round($pct * 30);
|
||||
$pct_disp = number_format($pct * 100, 0);
|
||||
$status_bar = "\r[";
|
||||
$status_bar .= str_repeat("|", $bar);
|
||||
|
||||
if ($bar < 30) {
|
||||
$status_bar .= ">";
|
||||
$status_bar .= str_repeat("-", 30 - $bar);
|
||||
} else {
|
||||
$status_bar.="|";
|
||||
$status_bar .= "|";
|
||||
}
|
||||
|
||||
$status_bar.="] $pct_disp%";
|
||||
$status_bar .= "] $pct_disp%";
|
||||
|
||||
echo $status_bar;
|
||||
ob_flush();
|
||||
|
||||
|
||||
if($progress == 100) {
|
||||
if ($progress == 100) {
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
@ -48,42 +46,39 @@ function get_file_from_url($file, $url) {
|
||||
//curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
|
||||
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/' . $ch_vers['version'] );
|
||||
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress_bar' );
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/'.$ch_vers['version']);
|
||||
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress_bar');
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_FILE, $chfile );
|
||||
curl_setopt($ch, CURLOPT_FILE, $chfile);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
fclose($chfile);
|
||||
}
|
||||
|
||||
// get a json array of the contents of gihub repo
|
||||
function get_content_from_github($repo, $file){
|
||||
function get_content_from_github($repo, $file) {
|
||||
$ch = curl_init();
|
||||
$ch_vers = curl_version();
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/' . $ch_vers['version'] );
|
||||
curl_setopt($ch, CURLOPT_URL, $repo);
|
||||
$content = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
if ((!empty($content)) && ($content != file_get_contents($file)))
|
||||
file_put_contents($file, $content);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json', 'Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/'.$ch_vers['version']);
|
||||
curl_setopt($ch, CURLOPT_URL, $repo);
|
||||
$content = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
if (!empty($content) && (!is_file($file) || $content != file_get_contents($file)))
|
||||
file_put_contents($file, $content);
|
||||
}
|
||||
|
||||
// Compare the github sha1 value of a file
|
||||
function file_check_sha1($file, $sha1){
|
||||
function file_check_sha1($file, $sha1) {
|
||||
$size = filesize($file);
|
||||
$handle = fopen($file, "rb");
|
||||
$contents = fread($handle, $size);
|
||||
fclose($handle);
|
||||
|
||||
$contents = file_get_contents($file);
|
||||
|
||||
// create a sha1 like github does
|
||||
$str = "blob ".$size."\0".$contents;
|
||||
$sha1_file = sha1($str);
|
||||
|
||||
$return = ($sha1_file == $sha1) ? true : false;
|
||||
return $return;
|
||||
|
||||
return ($sha1_file == $sha1);
|
||||
}
|
||||
?>
|
@ -1,5 +1,39 @@
|
||||
<?php
|
||||
function format_size($value, $decimals, $unit='?') {
|
||||
$plg_path = '/boot/config/plugins/NerdPack/'; // plugin path
|
||||
$pkg_path = $plg_path.'packages/'; // package path
|
||||
|
||||
$pkg_desc = 'https://raw.githubusercontent.com/dmacias72/unRAID-plugins/master/plugins/packages-desc';
|
||||
$pkg_repo = 'https://api.github.com/repos/eschultz/unraid6-nerdpack/contents/packages';
|
||||
|
||||
$desc_file = $pkg_path.'packages-desc';
|
||||
$repo_file = $pkg_path.'packages.json';
|
||||
$config_file = $plg_path.'NerdPack.cfg';
|
||||
|
||||
// get package configs
|
||||
$pkg_cfg = (is_file($config_file)) ? parse_ini_file($config_file) : [];
|
||||
|
||||
// get array of downloaded packages
|
||||
$pkgs_downloaded = file_exists($pkg_path) ? array_diff(scandir($pkg_path, 1), ['.', '..']) : [];
|
||||
|
||||
// get array of all installed packages
|
||||
$pkgs_installed = array_diff(scandir("/var/log/packages", 1), ['.', '..']);
|
||||
|
||||
$pkgs_desc_file = (is_file($desc_file)) ? file_get_contents($desc_file) : '[]';
|
||||
$pkgs_desc_array = json_decode($pkgs_desc_file, true);
|
||||
|
||||
$pkgs_github_file = (is_file($repo_file)) ? file_get_contents($repo_file) : '[]';
|
||||
$pkgs_github_array = json_decode($pkgs_github_file, true);
|
||||
|
||||
|
||||
function logger($output, $quiet = false) {
|
||||
exec('echo '.escapeshellarg($output).' 2>&1 | logger -tnerdpack');
|
||||
if (!$quiet) {
|
||||
echo "\n".$output." \n";
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
|
||||
function format_size($value, $decimals, $unit = '?') {
|
||||
if ($value == '')
|
||||
return 'unknown';
|
||||
|
||||
@ -26,7 +60,7 @@ function format_size($value, $decimals, $unit='?') {
|
||||
case 'T': return number_format($value / (float)1099511627776, $decimals).' TB';
|
||||
case 'G': return number_format($value / (float)(1 << 30), $decimals).' GB';
|
||||
case 'M': return number_format($value / (float)(1 << 20), $decimals).' MB';
|
||||
case 'K': return number_format($value / (float)(1 << 10), $decimals).' kB';
|
||||
case 'K': return number_format($value / (float)(1 << 10), $decimals).' KB';
|
||||
case 'B': return $value.' B';
|
||||
}
|
||||
|
||||
|
@ -1,71 +1,40 @@
|
||||
<?php
|
||||
require_once("/usr/local/emhttp/plugins/NerdPack/include/NerdPackHelpers.php");
|
||||
require_once '/usr/local/emhttp/plugins/NerdPack/include/NerdPackHelpers.php';
|
||||
|
||||
$plg_path ="/boot/config/plugins/NerdPack";// plugin path
|
||||
$pkg_path = "$plg_path/packages/"; // package path
|
||||
$config_file = "$plg_path/NerdPack.cfg";// config file
|
||||
$pkgs_array = [];
|
||||
|
||||
$pkg_cfg = (is_file($config_file)) ? parse_ini_file($config_file) : array(); // get package configs
|
||||
|
||||
$pkgs_downloaded = array_diff( scandir($pkg_path, 1), array(".", "..") ); // get array of downloaded packages
|
||||
|
||||
$pkgs_installed = array_diff( scandir("/var/log/packages", 1), array(".", "..") ); // get array of all installed packages
|
||||
|
||||
$pkgs_desc_file = file_get_contents($pkg_path."packages-desc");// get package descriptions
|
||||
$pkgs_desc_array = json_decode($pkgs_desc_file, true);
|
||||
|
||||
$pkgs_github_file = file_get_contents($pkg_path."packages.json");// get packages
|
||||
$pkgs_github_array = json_decode($pkgs_github_file, true);
|
||||
|
||||
$pkgs_array = array();
|
||||
|
||||
for ($i = 0; $i < sizeof($pkgs_github_array); $i++) {
|
||||
|
||||
$pkg_nameArray = explode("-", $pkgs_github_array[$i]["name"]); // split package name into array
|
||||
foreach ($pkgs_github_array as $pkg_github) {
|
||||
$pkg_nameArray = explode('-', $pkg_github['name']); // split package name into array
|
||||
|
||||
$pkg_name = $pkg_nameArray[0];
|
||||
if (sizeof($pkg_nameArray) > 4){ //if package name has a subset get it
|
||||
if (sizeof($pkg_nameArray) > 4) { //if package name has a subset get it
|
||||
for ($ii = 1; $ii < sizeof($pkg_nameArray)-3; $ii++) {
|
||||
$pkg_name = $pkg_name."-".$pkg_nameArray[$ii];
|
||||
}
|
||||
$pkg_name .= '-'.$pkg_nameArray[$ii];
|
||||
}
|
||||
}
|
||||
|
||||
$pkg_version = $pkg_nameArray[sizeof($pkg_nameArray)-3]; // get package version
|
||||
$pkg_version = $pkg_nameArray[sizeof($pkg_nameArray) - 3]; // get package version
|
||||
|
||||
$pkg_nver = $pkg_name."-".str_replace(".", "_", $pkg_version); // add underscored version to package name
|
||||
|
||||
$pkg_pattern = "/^".$pkg_name.".*/"; // search patter for packages
|
||||
$pkg_nver = $pkg_name.'-'.str_replace('.', '_', $pkg_version); // add underscored version to package name
|
||||
|
||||
$pkg = array();
|
||||
$pkg_pattern = '/^'.$pkg_name.'.*/'; // search patter for packages
|
||||
|
||||
$pkg["name"] = $pkgs_github_array[$i]["name"]; // add full package name
|
||||
|
||||
$pkg["pkgname"] = $pkg_name; // add package name only
|
||||
|
||||
$pkg["pkgnver"] = $pkg_nver; // add package name with underscored version
|
||||
|
||||
$pkg["pkgversion"] = $pkg_version; // add package name with underscored version
|
||||
|
||||
$pkg["size"] = format_size($pkgs_github_array[$i]["size"], 1, '?'); // add package size
|
||||
|
||||
// checks if package name is installed
|
||||
$pkg["installed"] = preg_grep($pkg_pattern , $pkgs_installed) ? "yes" : "no";
|
||||
|
||||
// checks if package is installed equals github exactly
|
||||
$pkg["installeq"] = in_array( pathinfo($pkgs_github_array[$i]["name"], PATHINFO_FILENAME), $pkgs_installed ) ? "yes" : "no";
|
||||
|
||||
// checks if package name is downloaded
|
||||
$pkg["downloaded"] = preg_grep($pkg_pattern , $pkgs_downloaded) ? "yes" : "no";
|
||||
|
||||
// checks if package is downloaded equals github exactly
|
||||
$pkg["downloadeq"] = in_array( $pkgs_github_array[$i]["name"], $pkgs_downloaded ) ? "yes" : "no";
|
||||
|
||||
$pkg["config"] = isset($pkg_cfg["$pkg_nver"]) ? $pkg_cfg["$pkg_nver"] : "no"; // checks config for install preference
|
||||
|
||||
$pkg["desc"] = $pkgs_desc_array["$pkg_name"];
|
||||
$pkg = [
|
||||
'name' => $pkg_github['name'], // add full package name
|
||||
'pkgname' => $pkg_name, // add package name only
|
||||
'pkgnver' => $pkg_nver, // add package name with underscored version
|
||||
'pkgversion' => $pkg_version, // add package name with raw version
|
||||
'size' => format_size($pkg_github['size'], 1, '?'), // add package size
|
||||
'installed' => preg_grep($pkg_pattern, $pkgs_installed) ? 'yes' : 'no', // checks if package name is installed
|
||||
'installeq' => in_array(pathinfo($pkg_github['name'], PATHINFO_FILENAME), $pkgs_installed) ? 'yes' : 'no', // checks if package is installed equals github exactly
|
||||
'downloaded' => preg_grep($pkg_pattern, $pkgs_downloaded) ? 'yes' : 'no', // checks if package name is downloaded
|
||||
'downloadeq' => in_array($pkg_github['name'], $pkgs_downloaded) ? 'yes' : 'no', // checks if package is downloaded equals github exactly
|
||||
'config' => isset($pkg_cfg[$pkg_nver]) ? $pkg_cfg[$pkg_nver] : 'no', // checks config for install preference
|
||||
'desc' => $pkgs_desc_array[$pkg_name]
|
||||
];
|
||||
|
||||
$pkgs_array[] = $pkg;
|
||||
}
|
||||
|
||||
echo json_encode($pkgs_array);
|
||||
?>
|
||||
?>
|
Reference in New Issue
Block a user