opkg-parser.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 opkg-parser.php [PATH] [DIFF_SEPARATOR]\"\n";
  12. echo " PATH -> path to opkg status file\n";
  13. echo " DIFF_SEPARATOR -> use /n separator\n";
  14. exit(1);
  15. }
  16. function processFile($filePath, $showEssentials, $showDependencies)
  17. {
  18. $block = [];
  19. $packagesData = [];
  20. foreach (file($filePath) as $line) {
  21. $clean = trim($line);
  22. if (empty($clean)) {
  23. if (count($block) > 0) {
  24. $packagesData[] = $block;
  25. $block = [];
  26. }
  27. } else {
  28. $parts = explode(': ', $clean);
  29. if (count($parts) == 2) {
  30. $block[ trim($parts[0]) ] = trim($parts[1]);
  31. }
  32. }
  33. }
  34. if (count($block) > 0) {
  35. $packagesData[] = $block;
  36. }
  37. return cleanInstallData($packagesData, $showEssentials, $showDependencies);
  38. }
  39. function cleanInstallData($output, $showEssentials, $showDependencies)
  40. {
  41. $packages = [];
  42. $depends = [];
  43. // generate packages and depends array
  44. foreach ($output as $data) {
  45. if (
  46. !isset($data['Auto-Installed']) &&
  47. isValidPackage($data['Package'])
  48. ) {
  49. if (
  50. !isset($data['Essential']) ||
  51. (
  52. isset($data['Essential']) && $showEssentials
  53. )
  54. ) {
  55. $packages[] = $data['Package'];
  56. if (isset($data['Depends'])) {
  57. foreach (explode(',', $data['Depends']) as $dependency) {
  58. $dependency = trim($dependency);
  59. if (!in_array($dependency, $depends)) {
  60. $depends[] = $dependency;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. // show all installed packages
  68. if ($showDependencies) {
  69. sort($packages);
  70. return $packages;
  71. }
  72. // show only target packages
  73. $targetPackages = [];
  74. foreach ($packages as $package) {
  75. if (!in_array($package, $depends)) {
  76. $targetPackages[] = $package;
  77. }
  78. }
  79. //var_dump($depends);
  80. sort($targetPackages);
  81. return $targetPackages;
  82. }
  83. function isValidPackage($name)
  84. {
  85. $packageBlacklist = [
  86. // hak5 packages (based on mk6)
  87. 'pineap',
  88. 'aircrack-ng-hak5',
  89. 'cc-client',
  90. 'libwifi',
  91. 'resetssids',
  92. 'http_sniffer',
  93. 'log_daemon',
  94. // based on hardware
  95. 'kmod-ath',
  96. 'kmod-ath9k',
  97. 'kmod-ath9k-htc',
  98. 'mt7601u-firmware',
  99. 'uboot-envtools',
  100. 'ubi-utils',
  101. ];
  102. // only kmod
  103. //return !in_array($name, $packageBlacklist) && strpos($name, 'kmod-') !== false;
  104. // not show kmod
  105. //return !in_array($name, $packageBlacklist) && strpos($name, 'kmod-') === false;
  106. // all
  107. return !in_array($name, $packageBlacklist);
  108. }
  109. echo "\nopkg status parser - by DSR!";
  110. echo "\n---------------------------------------\n\n";
  111. $printSep = (isset($argv[2]) && filter_var($argv[2], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) ? "\n" : ' ';
  112. $statusFile = $argv[1];
  113. if (!file_exists($statusFile)) {
  114. echo "[!!!] File not found: \"($statusFile)\"\n";
  115. return 0;
  116. }
  117. $statusData = processFile($statusFile, false, false);
  118. echo "======== Packages (" . count($statusData) . ") ========\n";
  119. echo implode($printSep, $statusData);
  120. echo "\n\n\n";
  121. $statusDataEssentials = processFile($statusFile, true, false);
  122. $essentialPackages = [];
  123. foreach ($statusDataEssentials as $key) {
  124. if (!in_array($key, $statusData)) {
  125. $essentialPackages[] = $key;
  126. }
  127. }
  128. echo "======== Essentials Packages (" . count($essentialPackages) . ") ========\n";
  129. echo implode($printSep, $essentialPackages);
  130. echo "\n";