packages-to-index.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, $folder, $filename) {
  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. $path = "{$folder}/{$filename}";
  39. $size = filesize($path);
  40. $hash = hash_file('sha256', $path);
  41. $manifiest[] = "Filename: {$filename}";
  42. $manifiest[] = "Size: {$size}";
  43. $manifiest[] = "SHA256sum: {$hash}";
  44. $manifiest[] = '';
  45. return implode("\n", $manifiest);
  46. }
  47. // blah
  48. echo "[*] Iterate folder content\n";
  49. $packagesIndex = [];
  50. $files = array_slice(scandir($folder), 2);
  51. foreach ($files as $item) {
  52. if (strpos($item, '.ipk') === false) {
  53. continue;
  54. }
  55. echo " [+] {$item}\n";
  56. @delTree('unpack');
  57. @mkdir('unpack');
  58. try {
  59. $phar = new PharData("{$folder}/{$item}");
  60. $phar->extractTo('unpack');
  61. unset($phar);
  62. // hack for "Cannot extract ".", internal error" fix
  63. /*
  64. $phar = new PharData('unpack/control.tar.gz', RecursiveDirectoryIterator::SKIP_DOTS);
  65. $phar->convertToData(Phar::ZIP);
  66. unset($phar);
  67. $zip = new ZipArchive;
  68. $zip->open('unpack/control.zip');
  69. $zip->extractTo('unpack');
  70. $zip->close();
  71. */
  72. shell_exec('tar -xzf unpack/control.tar.gz -C unpack --warning=no-timestamp');
  73. } catch (Exception $e) {
  74. echo " [!!!] Error in process\n";
  75. var_dump($e);
  76. exit();
  77. continue;
  78. }
  79. $packagesIndex[] = cleanManifiest(file_get_contents('unpack/control'), $folder, $item);
  80. }
  81. @delTree('unpack');
  82. echo "[*] Generating file: Packages\n";
  83. file_put_contents('Packages', implode("\n", $packagesIndex));