packages-to-index.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. # by DSR! from https://github.com/xchwarze/wifi-pineapple-cloner
  3. error_reporting(E_ALL);
  4. if (!isset($_SERVER['argv']) && !isset($argv)) {
  5. echo "Please enable the register_argc_argv directive in your php.ini\n";
  6. exit(1);
  7. } elseif (!isset($argv)) {
  8. $argv = $_SERVER['argv'];
  9. }
  10. if (!isset($argv[1])) {
  11. echo "Run with \"php packages-to-index.php [FOLDER]\"\n";
  12. echo " FOLDER -> packages folder\n";
  13. exit(1);
  14. }
  15. // config
  16. $folder = $argv[1];
  17. function delTree($dir) {
  18. $files = array_slice(scandir($dir), 2);
  19. foreach ($files as $file) {
  20. (is_dir("{$dir}/{$file}")) ? delTree("{$dir}/{$file}") : unlink("{$dir}/{$file}");
  21. }
  22. return rmdir($dir);
  23. }
  24. function cleanManifiest($string, $path) {
  25. $blacklist = [
  26. 'Source',
  27. 'SourceName',
  28. 'LicenseFiles',
  29. 'Maintainer',
  30. ];
  31. $manifiest = [];
  32. foreach (explode("\n", $string) as $value) {
  33. $key = (explode(':', $value))[0];
  34. if (!empty($key) && !in_array($key, $blacklist)) {
  35. $manifiest[] = $value;
  36. }
  37. }
  38. $hash = hash_file('sha256', $path);
  39. $manifiest[] = "SHA256sum: {$hash}";
  40. $manifiest[] = '';
  41. return implode("\n", $manifiest);
  42. }
  43. // blah
  44. echo "[*] Iterate folder content\n";
  45. $packagesIndex = [];
  46. $files = array_slice(scandir($folder), 2);
  47. foreach ($files as $item) {
  48. if (strpos($item, '.ipk') === false) {
  49. continue;
  50. }
  51. echo " [+] {$item}\n";
  52. @delTree('unpack');
  53. @mkdir('unpack');
  54. try {
  55. $phar = new PharData("{$folder}/{$item}");
  56. $phar->extractTo('unpack');
  57. unset($phar);
  58. // hack for "Cannot extract ".", internal error" fix
  59. /*
  60. $phar = new PharData('unpack/control.tar.gz', RecursiveDirectoryIterator::SKIP_DOTS);
  61. $phar->convertToData(Phar::ZIP);
  62. unset($phar);
  63. $zip = new ZipArchive;
  64. $zip->open('unpack/control.zip');
  65. $zip->extractTo('unpack');
  66. $zip->close();
  67. */
  68. shell_exec('tar -xzf unpack/control.tar.gz -C unpack --warning=no-timestamp');
  69. } catch (Exception $e) {
  70. echo " [!!!] Error in process\n";
  71. var_dump($e);
  72. exit();
  73. continue;
  74. }
  75. $packagesIndex[] = cleanManifiest(file_get_contents('unpack/control'), "{$folder}/{$item}");
  76. }
  77. @delTree('unpack');
  78. echo "[*] Generating file: Packages\n";
  79. file_put_contents('Packages', implode("\n", $packagesIndex));