sebbu

repack extracted epubs

Sep 26th, 2020
872
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. if(PHP_OS==='Windows_NT' || PHP_OS==='WINNT') chdir('D:/htdocs/iBooks');
  3. else if(PHP_OS==='Linux') chdir('/z/ebooks/iBooks');
  4. chdir('Z:\ebooks\iBooks-metadata\unc');
  5.  
  6. function compare_name($str1, $str2) {
  7.     if(substr($str1,0,2)=='._') $str1=substr($str1, 2);
  8.     if(substr($str2,0,2)=='._') $str2=substr($str2, 2);
  9.     return strnatcasecmp($str1, $str2);
  10. }
  11. function check_xpath_result($res)
  12. {
  13.     if(!(is_array($res))) return false;
  14.     if(!(count($res)==1)) return false;
  15.     if(!(is_object($res[0]))) return false;
  16.     if(!(get_class($res[0])=='SimpleXMLElement')) return false;
  17.     if(!((int)$res[0]->count==0)) return false;
  18.     if(!(count($res[0]->attributes())==0)) return false;
  19.     if(!(count($res[0]->children())==0)) return false;
  20.     if(!(in_array($res[0]->getName(),array('string','integer')))) return false;
  21.     return true;
  22. }
  23. function check_xpath_result_details($res)
  24. {
  25.     var_dump(!(is_array($res)));
  26.     var_dump(count($res),1);
  27.     var_dump(!(is_object($res[0])));
  28.     var_dump(get_class($res[0]),'SimpleXMLElement');
  29.     var_dump((int)$res[0]->count,0);
  30.     var_dump(count($res[0]->attributes()),0);
  31.     var_dump(count($res[0]->children()),0);
  32.     var_dump($res[0]->getName(),'string|integer');
  33. }
  34. function zip_add_recur($zip, $glob, $exclude=array()) {
  35.     if(!is_object($zip)||get_class($zip)!=='ZipArchive') return false;
  36.     $source=realpath('.');
  37.     $sources[]=$source;
  38.     $source=str_replace('\\', '/', $source);
  39.     $sources[]=$source;
  40.     //var_dump($sources);die();
  41.     $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
  42.     foreach($files as $f)
  43.     {
  44.         if( in_array($f->getFilename(), array('.', '..')) ) continue;
  45.         $f2=$f->getPathname();
  46.         $f2=realpath($f2);
  47.         $f3=$f2;
  48.         $f3=str_replace($sources[0].'\\', '', $f3);
  49.         $f3=str_replace($sources[1].'/', '', $f3);
  50.         if( in_array($f3, $exclude)) continue;
  51.         $f3=str_replace('\\','/',$f3);
  52.         //var_dump($f3);//die();
  53.         //print_r($f3."\n");
  54.         if(!file_exists($f2)||!is_readable($f2)) die('unreadable file "'.$f3.'".');
  55.         if(is_dir($f2)) $res=$zip->addEmptyDir($f3);
  56.         else if(is_file($f2)) $res=$zip->addFile($f2, $f3);
  57.         if(!$res) return $res;
  58.     }
  59.     $res=$zip->close();
  60.     return $res;
  61. }
  62.  
  63. $ar = glob('*', GLOB_MARK);
  64. //$ar = array_merge ( $ar, glob('.*', GLOB_MARK) );
  65. usort($ar, 'compare_name');
  66. //var_dump($ar);
  67. foreach($ar as $fn) {
  68.     //if(is_dir($fn)) if(file_exists(substr($fn,0,-1).'.epub')) rename(substr($fn,0,-1).'.epub', '../iBooks-metadata/'.substr($fn,0,-1).'.epub');
  69.     //if(is_dir($fn)) if(file_exists(substr($fn,0,-1).'.ibooks')) rename(substr($fn,0,-1).'.ibooks', '../iBooks-metadata/'.substr($fn,0,-1).'.ibooks');
  70.     if(!is_dir($fn)) continue;
  71.     if(substr($fn,-1)!=='/') $fn.='/';
  72.     if(substr($fn,0,1)=='_') continue;
  73.     $mt=file_get_contents($fn.'mimetype');
  74.     $mt=rtrim($mt);
  75.     $mt_valid=array('application/epub+zip', 'application/x-ibooks+zip');
  76.     if(!in_array($mt, $mt_valid)) {
  77.         var_dump($fn, $mt);
  78.         die();
  79.     }
  80.     $data=file_get_contents($fn.'iTunesMetadata.plist');
  81.     $data_valid=array('<?xml ');
  82.     if(!in_array(substr($data, 0, 6), $data_valid)) {
  83.         var_dump($fn, substr($data, 0, 6));
  84.         die();
  85.     }
  86.     //var_dump($fn);
  87.     $itemId=0;
  88.     $publisher='';
  89.     $author='';
  90.     $name='';
  91.     $name2='';
  92.     $subtitle='';
  93.     $year='';
  94.     $ext='';
  95.     $mimetype='';
  96.     $xml = simplexml_load_string($data);
  97.     $res2 = $xml->xpath('//key[. = "itemId"]/following::node()[2]');
  98.     if(check_xpath_result($res2)) $itemId=strval($res2[0]);
  99.     $res2 = $xml->xpath('//key[. = "publisher"]/following::node()[2]');
  100.     if(check_xpath_result($res2)) $publisher=strval($res2[0]);
  101.     $res2 = $xml->xpath('//key[. = "artistName"]/following::node()[2]');
  102.     if(check_xpath_result($res2)) $author=strval($res2[0]);
  103.     $res2 = $xml->xpath('//key[. = "itemName"]/following::node()[2]');
  104.     if(check_xpath_result($res2)) $name=strval($res2[0]);
  105.     $res2 = $xml->xpath('//key[. = "sortName"]/following::node()[2]');
  106.     if(check_xpath_result($res2)) $name2=strval($res2[0]);
  107.     $res2 = $xml->xpath('//key[. = "subtitle"]/following::node()[2]');
  108.     if(check_xpath_result($res2)) $subtitle=strval($res2[0]);
  109.     $res2 = $xml->xpath('//key[. = "year"]/following::node()[2]');
  110.     if(check_xpath_result($res2)) $year=strval($res2[0]);
  111.     $res2 = $xml->xpath('//key[. = "fileExtension"]/following::node()[2]');
  112.     if(check_xpath_result($res2)) $ext=strval($res2[0]);
  113.     $res2 = $xml->xpath('//key[. = "mime-type"]/following::node()[2]');
  114.     if(check_xpath_result($res2)) $mimetype=trim(strval($res2[0]));
  115.     //var_dump($publisher, $author, $name, $name2, $subtitle, $year, $ext, $mimetype);
  116.     if($itemId=='630526236') $author='Various Authors';
  117.     if($itemId=='853028807') $name='Grimm_s Fairy Tales';
  118.     //$ext='epub';
  119.     $filename=$publisher.' - '.$author.' - '.$name.' ['.$itemId.'].'.$ext;
  120.     $filename=str_replace(array('\'\'','\''), '_', $filename);
  121.     $filename=str_replace(array(':','"','/','\\','&'),'-', $filename);
  122.     $filename=str_replace(array('<','>','|','?','*'),'', $filename);
  123.     $filename=str_replace('\'\'', '\'', $filename);
  124.     $filename=str_replace('  ',' ',$filename);
  125.     //var_dump($author, $name);
  126.     var_dump($filename);
  127.     /*$zip = new ZipArchive;
  128.     if(!is_object($zip)) {
  129.         var_dump('zip error : '.(int)$zip);
  130.         die();
  131.     }
  132.     if(PHP_VERSION>=80000) if(($res=$zip->getStatusString())!=='No error') var_dump($res);
  133.     $res = $zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  134.     if($res!==true) {
  135.         $det=array(ZipArchive::ER_EXISTS => 'ZipArchive::ER_EXISTS', ZipArchive::ER_INCONS => 'ZipArchive::ER_INCONS', ZipArchive::ER_INVAL => 'ZipArchive::ER_INVAL', ZipArchive::ER_MEMORY => 'ZipArchive::ER_MEMORY', ZipArchive::ER_NOENT => 'ZipArchive::ER_NOENT', ZipArchive::ER_NOZIP => 'ZipArchive::ER_NOZIP', ZipArchive::ER_OPEN => 'ZipArchive::ER_OPEN', ZipArchive::ER_READ => 'ZipArchive::ER_READ', ZipArchive::ER_SEEK => 'ZipArchive::ER_SEEK',);
  136.         var_dump('zip error : '.(int)$res);
  137.         if(array_key_exists($res, $det)) var_dump($det[$res]);
  138.         die();
  139.     }
  140.     if(($res=$zip->getStatusString())!=='No error') var_dump($res);
  141.     chdir($fn);
  142.     if(!file_exists('mimetype')||!is_readable('mimetype')) die('unreadable file.');
  143.     $res=$zip->addFile('mimetype');
  144.     //$res=$zip->addFile('../mimetype', 'mimetype');
  145.     if($res!==true) {
  146.         var_dump('zip error : '.(int)$res);
  147.         die();
  148.     }
  149.     if(($res=$zip->getStatusString())!=='No error') var_dump($res);
  150.     //$ar2=get_class_methods($zip);sort($ar2);var_dump($ar2);
  151.     $res=$zip->setCompressionIndex(0, ZipArchive::CM_STORE);
  152.     if($res!==true) {
  153.         var_dump('zip error : '.(int)$res);
  154.         if(($res=$zip->getStatusString())!=='No error') var_dump($res);
  155.         die();
  156.     }
  157.     $res=zip_add_recur($zip, '*', array('mimetype'));
  158.     chdir('..');//*/
  159.     while(in_array(substr($fn, -1),array('/','\\'))) $fn=substr($fn, 0, -1);
  160.     $res = rename($fn.'.'.$ext, $filename);
  161.     if($res)
  162.     {
  163.         rename($fn, '__delete/'.$fn);
  164.     }
  165.     else {
  166.         var_dump('error '.(int)$res.' with '.$fn.'.');
  167.         die();
  168.     }
  169.     //die();
  170. }
  171.  
RAW Paste Data