Advertisement
zefie

PS3 Content Scanner

May 24th, 2018
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.62 KB | None | 0 0
  1. <?php
  2. $ps3_ip="192.168.11.206";
  3. $ps3_ftp_user="ps3";
  4. $ps3_ftp_pass="";
  5.  
  6. // Override PSN Packages into software category
  7. $override['software'] = array(
  8.     'NPEA90124',
  9.     'NPIA00025',
  10.     'NPUP00030',
  11.     'NPUP10028'
  12. );
  13.  
  14. // Override PSN Packages (Disc Games) into PS3 Games category
  15. $override['ps3'] = array(
  16.     'NPUA80472',
  17.     'NPUA80480',
  18.     'NPUA80638',
  19.     'NPUA80657',
  20.     'NPUA80662',
  21.     'NPUB30575',
  22.     'NPUB30606',
  23.     'NPUB30638',
  24.     'NPUB30700',
  25.     'NPUB30704',
  26.     'NPUB30715',
  27.     'NPUB30886',
  28.     'NPUB30923',
  29.     'NPUB30932',
  30.     'NPUB30985',
  31.     'NPUB31052',
  32.     'NPUB31083',
  33.     'NPUB31133',
  34.     'NPUB31136',
  35.     'NPUB31154',
  36.     'NPUB31241',
  37.     'NPUB31247',
  38.     'NPUB31257',
  39.     'NPUB31306'
  40. );
  41.  
  42. echo "\n";
  43. echo "Please note that mygames.txt output will be cleaner than\n";
  44. echo "what is shown on this console.\n\n";
  45. echo "Connecting to PS3... Please wait...\n";
  46. $ps3ftp = ftp_connect($ps3_ip);
  47. $login_result = ftp_login($ps3ftp, $ps3_ftp_user, $ps3_ftp_pass);
  48. echo "Connected, scanning games...\n";
  49. $content['ps1'] = scanISO($ps3ftp,'ps1');
  50. $content['ps2'] = scanISO($ps3ftp,'ps2');
  51. $content['ps3'] = scanISO($ps3ftp,'ps3');
  52. $content['dvd'] = scanISO($ps3ftp,'dvd');
  53. $content = scanFolder($ps3ftp,$content,'game');
  54. // scanFolder might work with /dev_hdd0/GAMES and GAMEZ but is not tested
  55. echo "Done, disconnecting from PS3...\n";
  56. ftp_close($ps3ftp);
  57.  
  58. echo "Generating mygames.txt... ";
  59. $out = "";
  60. if (@$content['ps1']) { $out .= processData($content['ps1'],"PLAYSTATION(R) 1 Games"); }
  61. if (@$content['ps2']) { $out .= processData($content['ps2'],"PLAYSTATION(R) 2 Games"); }
  62. if (@$content['ps3']) { $out .= processData($content['ps3'],"PLAYSTATION(R) 3 Games"); }
  63. if (@$content['psp']) { $out .= processData($content['psp'],"PLAYSTATION(R) Portable Games"); }
  64. if (@$content['psn']) { $out .= processData($content['psn'],"PLAYSTATION(R) Network Games"); }
  65. if (@$content['software']) { $out .= processData($content['software'],"PLAYSTATION(R) 3 Software"); }
  66. if (@$content['dvd']) { $out .= processData($content['dvd'],"DVD Video ISOs"); }
  67.  
  68. // Generate mygames.txt
  69. $out = preg_replace("/\n/","\r\n",$out);
  70. $fp = fopen("mygames.txt","w+");
  71. fwrite($fp,$out);
  72. fclose($fp);
  73. echo "Done.\n";
  74.  
  75. /* -------------- Functions -------------- */
  76.  
  77. function removeUnwantedWording($iso) {
  78.      $iso = preg_replace("/ \(Trial Version\)/","",$iso);
  79.      $iso = preg_replace("/ Trial Version/","",$iso);
  80.      $iso = preg_replace("/: \Trial/","",$iso);
  81.      $iso = preg_replace("/ Trial$/","",$iso);
  82.      $iso = preg_replace("/ Demo$/","",$iso);
  83.      $iso = preg_replace("/ \(Disc [0-9]\)/","",$iso);
  84.      $iso = preg_replace("/^GT Academy 2013$/","SEN Enabler",$iso);
  85.      $iso = preg_replace("/ Unknown CFW\!$/","",$iso); 
  86.      return $iso;
  87. }
  88. function checkOverride($s,$k) {
  89.     global $override;
  90.     if (@$override[$k]) {
  91.         return in_array($s,$override[$k]);
  92.     }
  93.     return false;
  94. }
  95. function isBlacklisted($id) {
  96.     global $blacklist;
  97.     if (@$blacklist) {
  98.         return in_array($id,$blacklist);
  99.     }
  100.     return false;
  101. }  
  102. function readParamSFO($data,$force_max = false) {
  103.     $keytable_offset = unpack('I',substr($data,8,4))[1];
  104.     $data_offset = unpack('I',substr($data,12,4))[1];
  105.     $table_entries = unpack('I',substr($data,16,4))[1];
  106.  
  107.     $keytable = array_filter(explode("\0",substr($data,$keytable_offset,($data_offset - $keytable_offset))));
  108.     $idxtable = substr($data,20,($keytable_offset-20));
  109.     $data = substr($data,$data_offset,-1);
  110.  
  111.     $pos = 0;
  112.     $i = 1;
  113.     while ($pos < strlen($idxtable)) {
  114.         $key[$i]['start'] = unpack('I',substr($idxtable,$pos,2)."\0\0")[1];
  115.         $pos = $pos + 2;
  116.         $key[$i]['fmt'] = unpack('I',substr($idxtable,$pos,2)."\0\0")[1];
  117.         $pos = $pos + 2;
  118.         $key[$i]['len'] = unpack('I',substr($idxtable,$pos,4))[1];
  119.         $pos = $pos + 4;
  120.         $key[$i]['max_len'] = unpack('I',substr($idxtable,$pos,4))[1];
  121.         $pos = $pos + 4;
  122.         $key[$i]['offset'] = unpack('I',substr($idxtable,$pos,4))[1];
  123.         $pos = $pos + 4;
  124.         $i++;
  125.     }
  126.     $i = 0;
  127.     foreach ($key as $k) {
  128.         if ($force_max) {
  129.             $pm = substr($data,$k['offset'],$k['max_len']);
  130.         } else {
  131.             $pm = substr($data,$k['offset'],$k['len']);
  132.         }
  133.         $param[$keytable[$i]] = trim($pm);
  134.         $i++;
  135.     }
  136.     return $param;
  137. }
  138. function getNameFromISO($iso) {
  139.     $p1 = strpos($iso,'[') + 1;
  140.     $p2 = strpos($iso,']',$p1);
  141.     return substr($iso,$p1,($p2 - strlen($iso)));
  142. }
  143. function scanISO($ftp,$dir) {
  144.     // Assumes filename is XXXX#####-[Game Name].iso (or .bin)
  145.     // Falls back to entire filename as title.
  146.     switch (strtolower($dir)) {
  147.         case "psx":
  148.             // no break to allow it to hit ps1 processing
  149.             $dir = "ps1";
  150.            
  151.         case "ps1":
  152.             $rdir = "PSXISO";
  153.             break;
  154.            
  155.         case "ps2":
  156.             $rdir = "PS2ISO";
  157.             break;
  158.  
  159.         case "ps3":
  160.             $rdir = "PS3ISO";
  161.             break;
  162.  
  163.         case "dvd":
  164.             $rdir = "DVDISO";
  165.             break;
  166.  
  167.         case "bd":
  168.             $rdir = "BDISO";
  169.             break;
  170.            
  171.         default:
  172.             return false;
  173.             break;
  174.     }
  175.     $res = [];
  176.     $ftptmp = ftp_mlsd($ftp,"/dev_hdd0/".$rdir);
  177.     foreach ($ftptmp as $d) {
  178.         if (strtolower(substr($d['name'],(strlen($d['name']) - 3),3)) == "bin" || strtolower(substr($d['name'],(strlen($d['name']) - 3),3)) == "iso") {
  179.             if (preg_match("/^[MSNUB][A-Z]{3}[0-9]{5}/",$d['name'])) {
  180.                 $v = getNameFromISO($d['name']);
  181.             } else {
  182.                 $v = substr($d['name'],0,(strlen($d['name']) - 4));
  183.             }
  184.             echo strtoupper($dir).": ".$v."\n";
  185.             $res[] = removeUnwantedWording($v)." (ISO)";
  186.         }
  187.     }
  188.     sort($res);
  189.     $res = array_unique($res);
  190.     return $res;
  191. }
  192. function scanFolder($ftp,$content,$dir,$filter = true) {
  193.     $ftptmp = ftp_mlsd($ftp,"/dev_hdd0/".$dir);
  194.     $res = [];
  195.     foreach ($ftptmp as $d) {
  196.         // is XXXX#####, starts with N or U, is a directory, isn't blacklisted (by id)
  197.         if ((preg_match("/^[NU][A-Z]{3}[0-9]{5}$/",$d['name']) && $d['type'] == 'dir' && !isBlacklisted($d['name'])) || !$filter) {
  198.             ob_start();
  199.             $result = ftp_get($ftp, "php://output", "/dev_hdd0/".$dir."/".$d['name']."/PARAM.SFO", FTP_BINARY);
  200.             $ps3sfo = ob_get_contents();
  201.             ob_end_clean();
  202.             if (strlen(@$ps3sfo) > 0) {
  203.                 $pdecode = readParamSFO($ps3sfo);
  204.                 if (@$pdecode['BOOTABLE']) {
  205.                     $v = preg_replace("/\n/"," ",$pdecode['TITLE']);
  206.                    
  207.                     switch (substr($pdecode['TITLE_ID'],0,2)) {
  208.                         case "UL":
  209.                         case "UC":
  210.                             $pdecode = readParamSFO($ps3sfo,1);
  211.                             $v = preg_replace("/\n/"," ",$pdecode['TITLE']);
  212.                             echo "PSP (Pkg): ".$v."\n";
  213.                             $content['psp'][] = $v." (Installed)";
  214.                             break;
  215.                            
  216.                         case "SC":
  217.                         case "SL":
  218.                             // PS1 & PS2 both use the same TITLE_ID scheme, but PS2 games start at 20000
  219.                             // so assume any ID less than 20000 is a PS1 game.
  220.                             $ser = substr($pdecode['TITLE_ID'],4,5);
  221.                             if (intval($ser) >= 20000) {
  222.                                 echo "PS2: ".removeUnwantedWording($v)."\n";
  223.                                 $content['ps2'][] = removeUnwantedWording($v)." (PSN Install)";
  224.                             } else {
  225.                                 echo "PS1: ".removeUnwantedWording($v)."\n";
  226.                                 $content['ps1'][] = removeUnwantedWording($v)." (PSN Install)";
  227.                             }
  228.                             break;
  229.  
  230.                         default:
  231.                             // Fell back to generic PSN Package, so check overrides
  232.                             if (checkOverride($pdecode['TITLE_ID'],'ps1')) {
  233.                                 echo "PS1: ".removeUnwantedWording($v)."\n";
  234.                                 $content['ps1'][] = removeUnwantedWording($v)." (PSN Install)";
  235.                             }
  236.                             elseif (checkOverride($pdecode['TITLE_ID'],'ps2')) {
  237.                                 echo "PS2: ".removeUnwantedWording($v)."\n";
  238.                                 $content['ps2'][] = removeUnwantedWording($v)." (PSN Install)";
  239.                             }                      
  240.                             elseif (checkOverride($pdecode['TITLE_ID'],'ps3')) {
  241.                                 echo "PS3: ".removeUnwantedWording($v)."\n";
  242.                                 $content['ps3'][] = removeUnwantedWording($v)." (PSN Install)";
  243.                             }      
  244.                             elseif (checkOverride($pdecode['TITLE_ID'],'software')) {
  245.                                 echo "Software: ".removeUnwantedWording($v)."\n";
  246.                                 $content['software'][] = removeUnwantedWording($v);
  247.                             }                                  
  248.                             else {
  249.                                 echo "PSN: ".removeUnwantedWording($v)." (".$pdecode['TITLE_ID'].")\n";
  250.                                 $content['psn'][] = removeUnwantedWording($v);
  251.                             }
  252.                             break;
  253.                     }
  254.                 }
  255.             }
  256.         } elseif ($dir == 'game' && preg_match("/^[A-Z0-9]{9}$/",$d['name']) && (!preg_match("/^B[A-Z0-9]{8}$/",$d['name']) || $d['name'] == "BLES80608")) {
  257.             ob_start();
  258.             $result = ftp_get($ftp, "php://output", "/dev_hdd0/".$dir."/".$d['name']."/PARAM.SFO", FTP_BINARY);
  259.             $ps3sfo = ob_get_contents();
  260.             ob_end_clean();
  261.             $pdecode = readParamSFO($ps3sfo,1);
  262.             $v = preg_replace("/\n/"," ",$pdecode['TITLE']);
  263.             echo "Software: ".removeUnwantedWording($v)." (".$pdecode['TITLE_ID'].")\n";
  264.             $content['software'][] = removeUnwantedWording($v);        
  265.         }
  266.     }
  267.     return $content;
  268. }
  269. function processData($d,$s) {
  270.     natcasesort($d);
  271.     $out = " *----* ".$s." *----* \n";
  272.     foreach ($d as $f) {
  273.         $out .= "  - ".$f."\n";
  274.     }
  275.     return $out."\n";
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement