73,6 → 73,7 |
$a = strtolower($_GET['a']); |
|
$p = ''; |
$v = ''; |
if ($a != 'checkup') { |
if (empty($_GET['p'])) { |
http_response_code(404); |
79,56 → 80,78 |
echo "ERROR: no package specified\r\n"; |
exit(0); |
} |
$p = strtolower($_GET['p']); |
$pv = explode('-', strtolower($_GET['p'])); |
$p = $pv[0]; |
if (!empty($pv[1])) $v = $pv[1]; |
} |
|
$v = ''; |
if (!empty($_GET['v'])) $v = $_GET['v']; |
|
// pull action is easy (does not require looking into pkg list), do it now |
// is action valid? |
|
if ($a === 'pull') { |
if (file_exists($p . '.svp')) { |
header('Content-Disposition: attachment; filename="' . $p . '.svp"'); |
header('Content-Type: application/octet-stream'); |
readfile($p . '.svp'); |
} else { |
http_response_code(404); |
echo "ERROR: package not found on server\r\n"; |
} |
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) { |
http_response_code(404); |
echo "ERROR: invalid action\r\n"; |
exit(0); |
} |
|
// is action valid? |
|
if (($a !== 'search') && ($a !== 'checkup')) { |
// load pkg db |
|
$db = json_decode(file_get_contents('_index.json'), true); |
if (empty($db)) { |
http_response_code(404); |
echo "ERROR: invalid action\r\n"; |
echo "ERROR: server error, database not found\n"; |
exit(0); |
} |
|
// iterate over packages now |
|
$handle = fopen("index.tsv", "rb"); |
if ($handle === FALSE) { |
http_response_code(404); |
echo "ERROR: Server-side internal error\r\n"; |
// pull action |
|
if ($a === 'pull') { |
$fname = false; |
if (empty($v)) { // take first version (that's the preferred one) |
$fname = array_key_first($db[$p]['versions']); |
} else { |
// look for a specific version string |
foreach ($db[$p]['versions'] as $f => $e) { |
if (strcasecmp($e['ver'], $v) == 0) { |
$fname = $f; |
break; |
} |
} |
} |
if (file_exists($fname)) { |
header('Content-Disposition: attachment; filename="' . $fname); |
header('Content-Type: application/octet-stream'); |
readfile($fname); |
} else { |
http_response_code(404); |
echo "ERROR: package not found on server\r\n"; |
} |
exit(0); |
} |
|
|
// search action |
|
if ($a === 'search') { |
$matches = 0; |
while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) { |
if ((stristr($pkg[0], $p)) || (stristr($pkg[2], $p))) { |
echo str_pad(strtoupper($pkg[0]), 12) . str_pad("ver: {$pkg[1]} ", 16) . str_pad("size: " . nicesize(filesize($pkg[0] . '.svp')), 16) . "BSUM: " . sprintf("%04X", $pkg[3]) . "\r\n"; |
echo wordwrap($pkg[2], 79, "\r\n", true); |
foreach ($db as $pkg => $meta) { |
if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) { |
// fetch first (preferred) version |
$prefver_fname = array_key_first($meta['versions']); |
$prefver = array_shift($meta['versions']); |
echo str_pad(strtoupper($pkg), 12) . str_pad("ver: {$prefver['ver']} ", 16) . str_pad("size: " . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n"; |
echo wordwrap($meta['desc'], 79, "\r\n", true); |
echo "\r\n"; |
// do I have any alt versions? |
$altvers = glob("{$pkg[0]}-*.svp"); |
if (!empty($altvers)) { |
$alts = str_replace('.svp', '', $altvers); |
echo wordwrap("[alt versions: " . implode(', ', $alts), 79, "\r\n", true) . "]\r\n"; |
$altlist = array(); |
foreach ($meta['versions'] as $altver) { |
$altlist[] = $pkg . '-' . $altver['ver']; |
} |
if (!empty($altlist)) { |
echo wordwrap("[alt versions: " . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n"; |
} |
echo "\r\n"; |
$matches++; |
} |
136,32 → 159,36 |
if ($matches == 0) echo "No matching package found.\r\n"; |
} |
|
|
// checkup action |
|
if ($a === 'checkup') { |
$found = 0; |
$remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version |
while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) { |
// is $pkg part of remote packages? |
foreach ($remote_pkgs as $rpkg) { |
if (strcasecmp($pkg[0], $rpkg[0]) != 0) continue; |
if ($pkg[1] === $rpkg[1]) continue; // skip same version |
if ($found == 0) { |
echo str_pad('', 58, '-') . "\r\n"; |
tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20)); |
tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false); |
} |
$found++; |
tabulprint(array('' . $pkg[0], $rpkg[1], $pkg[1]), array(8, 20, 20)); |
break; |
|
foreach ($remote_pkgs as $rpkg) { |
$rpkg[0] = strtolower($rpkg[0]); |
if (empty($db[$rpkg[0]])) continue; |
|
$dbpkg = $db[$rpkg[0]]; |
// compare user's version with preferred version in repo |
$prefver = array_shift($dbpkg['versions']); |
if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue; |
// found version mismatch |
if ($found == 0) { |
echo str_pad('', 58, '-') . "\r\n"; |
tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20)); |
tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false); |
} |
$found++; |
tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 20, 20)); |
} |
if ($found == 0) { |
echo "no available updates\r\n"; |
} else { |
echo str_pad('', 58, '-') . "\r\n"; |
echo "found {$found} differing packages\r\n"; |
echo "found {$found} differing package(s)\r\n"; |
} |
} |
fclose($handle); |
|
|
?> |