krom

DAZ Content Installer

Oct 19th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 31.61 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4. date_default_timezone_set("Europe/Berlin");
  5.  
  6. /**
  7.  * Krom 2014
  8.  *
  9.  * License: Do whatever the fuck you want to do with it. Credits would be nice.
  10.  *
  11.  * TODO:
  12.  * - log installed files, so re-installation can be prevented
  13.  * - EXE installer: poll for supported flags and create custom options
  14.  *  
  15.  * v1.1 - 19.10.2015
  16.  *    + added support for installing "people" and "data" folders.
  17.  *    ~ forced target folder names to be uppercase for "Runtime" and "People"
  18.  *    ~ forced target folder name to be lowercase for "data"
  19.  */
  20.  
  21.  
  22. class DAZ3D_Installer {
  23.     /* private, do not change */
  24.     private $_found_installs;
  25.     private $_internal_ignore;
  26.     private $_logfile;
  27.  
  28.     /* you can/have to change these, after creating an instance */
  29.     public $source_dir;                // where to search for installables?
  30.     public $install_dir;            // where to install to?
  31.     public $unzip_dir;                // if I need to unzip, where can I do so? (only 1 zip at a time)
  32.     public $log_dir;                // where can I write my logfile to?
  33.     public $ignore_dirs;            // are there dirs I should ignore (see constructor)
  34.     public $installed_file;            // not working yet. log installed stuff here
  35.     public $create_target_subdirs;    // should I install everything to it's own directory? (Note: sometimes name guessing does not work that good, depends on folder structure)
  36.     public $skip_existing_dirs;        // if I should install to own dirs, should I skip those that already exist?
  37.     public $bInstallOne;            // for testing: should I install 1 package per category only?
  38.  
  39.     // --------------------------------------------------------------------- //
  40.     public function __construct()  {
  41.         $this->_internal_ignore = array();
  42.         $this->_found_installs = array();
  43.         $this->_logfile = strftime("%Y%m%d%H%M%S_install.log");
  44.  
  45.         $this->source_dir        = '';
  46.         $this->install_dir         = '';
  47.         $this->unzip_dir           = '';
  48.         $this->log_dir             = '';
  49.         $this->installed_file     = '';
  50.         $this->ignore_dirs        = array();
  51.         $this->create_target_subdirs = true;
  52.         $this->skip_existing_dirs      = false;
  53.  
  54.         // install only one package per category (for testing)
  55.         $this->bInstallOne = true;
  56.        
  57.         // these are pretty much static.
  58.         // we don't want documents, and readmes
  59.         // we do not process copying to program files dir.
  60.         // we need to ignore MAC installers
  61.         $this->ignore_dirs = array(
  62.             'documents',
  63.             'program files',
  64.             'readme',
  65.             'mac'
  66.         );
  67.     }
  68.     // --------------------------------------------------------------------- //
  69.     public function install()  {
  70.  
  71.         // check settings
  72.         if (!is_dir($this->source_dir))
  73.             $this->_log2file("Source dir is not a dir?! >>".$this->source_dir."<<\n", true, true);
  74.         if (!is_dir($this->install_dir))
  75.             $this->_log2file("Install dir is not a dir?! >>".$this->install_dir."<<\n", true, true);
  76.         if (!is_dir($this->unzip_dir))
  77.             $this->_log2file("Unzip dir is not a dir?! >>".$this->unzip_dir."<<\n", true, true);
  78.         if (!is_dir($this->log_dir))
  79.             $this->_log2file("Log dir is not a dir?! >>".$this->log_dir."<<\n", true, true);
  80.  
  81.  
  82.         $this->_log2file("Starting installation");
  83.         $this->_log2file("  1. Scanning for installables");
  84.  
  85.         $this->_scanDir($this->source_dir, $this->_found_installs);
  86.  
  87.         $stats = $this->getStats($this->_found_installs);
  88.         $this->_log2file("    EXE: ".$stats['exe']);
  89.         $this->_log2file("    ZIP: ".$stats['zip']);
  90.         $this->_log2file("    Copy Content: ".$stats['content']);
  91.         $this->_log2file("    Copy Runtime: ".$stats['runtime']);
  92.         $this->_log2file("    Copy Other: ".$stats['other']);
  93.         $this->_log2file("    Total: ".$stats['total']);
  94.         $this->_log2file("  --------------------------");
  95.  
  96.         if (empty($this->_found_installs))  {
  97.             $this->_log2file("Nothing to install found. Aborting.");
  98.             return true;
  99.         }
  100.  
  101.         echo "--------------------------\n";
  102.         echo "Found these:\n";
  103.         print_r($stats);
  104.         echo "--------------------------\n";
  105.  
  106.         do {
  107.             if (PHP_OS == 'WINNT')  {
  108.                 echo "Do you wish to continue? (y/n): ";
  109.                 $k = stream_get_line(STDIN, 1024, PHP_EOL);
  110.             } else $k = readline("Do you wish to continue? (y/n): ");
  111.         } while ($k != 'y' && $k != 'j' && $k != 'n');
  112.  
  113.         if ($k == 'n')  {
  114.             $this->_log2file("User abort.");
  115.             return true;
  116.         }
  117.  
  118.         $this->_log2file("  2. Installing...");
  119.  
  120.         // EXE
  121.         if ($stats['exe'] > 0)  {
  122.             do {
  123.                 if (PHP_OS == 'WINNT')  {
  124.                     echo "Install ".$stats['exe']." exe files? (y/n): ";
  125.                     $k = stream_get_line(STDIN, 1024, PHP_EOL);
  126.                 } else $k = readline("Install ".$stats['exe']." exe files? (y/n): ");
  127.             } while ($k != 'y' && $k != 'j' && $k != 'n');
  128.  
  129.             if ($k == 'y' || $k == 'j')
  130.                  $this->_install_EXE($this->_found_installs);
  131.             else $this->_log2file("    Skipping EXE files\n    ------------------------");
  132.         }
  133.         // ZIP
  134.         if ($stats['zip'] > 0)  {
  135.             do {
  136.                 if (PHP_OS == 'WINNT')  {
  137.                     echo "Install ".$stats['zip']." zip files? (y/n): ";
  138.                     $k = stream_get_line(STDIN, 1024, PHP_EOL);
  139.                 } else $k = readline("Install ".$stats['zip']." zip files? (y/n): ");
  140.             } while ($k != 'y' && $k != 'j' && $k != 'n');
  141.  
  142.             if ($k == 'y' || $k == 'j')
  143.                  $this->_install_ZIP($this->_found_installs);
  144.             else $this->_log2file("    Skipping ZIP files\n    ------------------------");
  145.         }
  146.         // CONTENT
  147.         // considering installing this and runtime first, as there might be dependencies
  148.         // on the exe files - had that with genesis 2, where the genesis2 wasn't an installable
  149.         // but a dependency for installables - which in turn did not install.
  150.         if ($stats['content'] > 0)  {
  151.             do {
  152.                 if (PHP_OS == 'WINNT')  {
  153.                     echo "Copy ".$stats['content']." Content folders? (y/n): ";
  154.                     $k = stream_get_line(STDIN, 1024, PHP_EOL);
  155.                 } else $k = readline("Copy ".$stats['content']." Content folders? (y/n): ");
  156.             } while ($k != 'y' && $k != 'j' && $k != 'n');
  157.  
  158.             if ($k == 'y' || $k == 'j')
  159.                  $this->_install_CONTENT($this->_found_installs);
  160.             else $this->_log2file("    Skipping CONTENT folders\n    ------------------------");
  161.         }
  162.         // RUNTIME
  163.         if ($stats['runtime'] > 0)  {
  164.             do {
  165.                 if (PHP_OS == 'WINNT')  {
  166.                     echo "Copy ".$stats['runtime']." Runtime folders? (y/n): ";
  167.                     $k = stream_get_line(STDIN, 1024, PHP_EOL);
  168.                 } else $k = readline("Copy ".$stats['runtime']." Runtime folders? (y/n): ");
  169.             } while ($k != 'y' && $k != 'j' && $k != 'n');
  170.  
  171.             if ($k == 'y' || $k == 'j')
  172.                  $this->_install_RUNTIME($this->_found_installs);
  173.             else $this->_log2file("    Skipping RUNTIME folders\n    ------------------------");
  174.         }
  175.         // OTHER
  176.         if ($stats['other'] > 0)  {
  177.             do {
  178.                 if (PHP_OS == 'WINNT')  {
  179.                     echo "Copy ".$stats['runtime']." other folders? (y/n): ";
  180.                     $k = stream_get_line(STDIN, 1024, PHP_EOL);
  181.                 } else $k = readline("Copy ".$stats['runtime']." other folders? (y/n): ");
  182.             } while ($k != 'y' && $k != 'j' && $k != 'n');
  183.  
  184.             if ($k == 'y' || $k == 'j')
  185.                  $this->_install_OTHER($this->_found_installs);
  186.             else $this->_log2file("    Skipping other folders\n    ------------------------");
  187.         }
  188.  
  189.         echo "\n *** Installation completed ***\n";
  190.         echo "Have a nice day! :)\n";
  191.         $this->_log2file("Installation completed!");
  192.         return true;
  193.     }
  194.     // --------------------------------------------------------------------- //
  195.     private function _install_EXE($data)  {
  196.         $stats = $this->getStats($data);
  197.  
  198.         $this->_log2file("    Installing ".$stats['exe']." EXE files");
  199.  
  200.         // ------------------------------------------------------------------
  201.         $cnt = 1;
  202.         foreach($data as $fk => $fv)  {
  203.             if ($fv['type'] != 'exe') continue;
  204.  
  205.             $name = str_ireplace(".exe", "", basename($fv['path']));
  206.  
  207.             echo "  Installing [".($cnt)."/".$stats['exe']."]: ".$name."\n";
  208.             $this->_log2file("      [".($cnt)."/".$stats['exe']."] installing: ".$name);
  209.             $cnt++;
  210.            
  211.         //    if ($cnt < 77) continue;
  212.  
  213.             if ($this->create_target_subdirs)  {
  214.                 if (is_dir($this->install_dir . $name.DIRECTORY_SEPARATOR))  {
  215.                     if ($this->skip_existing_dirs == true)  {
  216.                         $this->_log2file("        target dir exists. Skipping.");
  217.                         continue;
  218.                     }
  219.                 } else if (!mkdir($this->install_dir . $name.DIRECTORY_SEPARATOR))
  220.                     die("Failed to create subdir: ".($this->install_dir . $name.DIRECTORY_SEPARATOR)."\n");
  221.                 chmod($this->install_dir . $name.DIRECTORY_SEPARATOR, 0777);
  222.                 $dst_dir = $this->install_dir . $name.DIRECTORY_SEPARATOR;
  223.             } else $dst_dir = $this->install_dir . DIRECTORY_SEPARATOR;
  224.  
  225.            
  226.             // 4 ways
  227.             // could make that smarter by polling the exe for its options
  228.             // with --help option.
  229.             // still strange problems with "creating folder" on option3+4 "unattended" instead of "win32"
  230.             //   - as for the same problem with #1 or #2, adding a trailing slash fixed this. have to test #3+#4.
  231.             // at least option #1 is not really unattended - it is, but only for very new installers (those "wide" ones)
  232.            
  233.             // 1.
  234.             $options_1 = ' --mode unattended --unattendedmodeui minimal --accept_license yes --create_uninstaller no --create_startmenu_shortcuts no --installdir_content "'.$dst_dir.'"';
  235.             $options_2 = ' --mode unattended --unattendedmodeui minimal --Uninstaller 0 --installpath "'.$dst_dir.'"';
  236.             $options_3 = ' --mode win32 --uninstaller 0  --chosenpath "'.$dst_dir.'" --pathselection specify --prefix "'.$dst_dir.'"';
  237.             $options_4 = ' --mode win32 --Uninstaller 0  --installpath "'.$dst_dir.'" --prefix "'.$dst_dir.'"';
  238.  
  239.             $error = false;
  240.  
  241.             echo "  ".basename($fv['path'])."\n";
  242.             $this->_log2file("        ".$fv['path']." $options_1");
  243.            
  244.             echo "   trying method #1\n";
  245.            
  246.             // yeah not nice, I know. call me a lazy ass. :)
  247.             $bar = popen('"'.$fv['path'].'"' . $options_1 . ' 2>&1', 'r');
  248.             $read = fread($bar, 2048);
  249.             pclose($bar);
  250.             if (stripos($read, "error") !== false)  {
  251.                 echo "   trying method #2\n";                
  252.                 $bar = popen('"'.$fv['path'].'"' . $options_2 . ' 2>&1', 'r');
  253.                 $read = fread($bar, 2048);
  254.                 pclose($bar);                
  255.                 if (stripos($read, "error") !== false)  {
  256.                     echo "   trying method #3\n";
  257.                     $bar = popen('"'.$fv['path'].'"' . $options_3 . ' 2>&1', 'r');
  258.                     $read = fread($bar, 2048);
  259.                     pclose($bar);
  260.                     if (stripos($read, "error") !== false)  {                
  261.                         echo "   trying method #4\n";
  262.                         $bar = popen('"'.$fv['path'].'"' . $options_4 . ' 2>&1', 'r');
  263.                         $read = fread($bar, 2048);
  264.                         pclose($bar);
  265.                         if (stripos($read, "error") !== false)  {
  266.                             echo "  Failed to install\n";
  267.                             $this->_log2file("        failed to install");
  268.                             $error = true;
  269.                             $bar = popen('"'.$fv['path'].'" 2>&1', 'r');
  270.                             $read = fread($bar, 2048);
  271.                             pclose($bar);
  272.                        
  273.                         } else echo "   success method #4: $read\n";
  274.                     } else echo "   success method #3: $read\n";
  275.                 } else echo "   success method #2: $read\n";
  276.             } else echo "   success method #1: $read\n";
  277.                
  278.             if ($this->bInstallOne) break;
  279.         }
  280.         // ------------------------------------------------------------------
  281.         $this->_log2file("    done installing exe files");
  282.         $this->_log2file("    ------------------------");
  283.     }
  284.     // --------------------------------------------------------------------- //
  285.     private function _install_ZIP()  {
  286.         $stats = $this->getStats($this->_found_installs);
  287.  
  288.         $this->_log2file("    Installing ".$stats['zip']." ZIP files");
  289.         // ------------------------------------------------------------------
  290.         $cnt = 1;
  291.         foreach($this->_found_installs as $fk => $fv)  {
  292.             if ($fv['type'] != 'zip') continue;
  293.             $foo = explode(DIRECTORY_SEPARATOR, $fv['path']);
  294.  
  295.             echo "  Processing ZIP [".($cnt)."/".$stats['zip']."]: ".$foo[sizeof($foo)-2]." - ".basename($fv['path'])."\n";
  296.             $this->_log2file("      [".($cnt)."/".$stats['zip']."] process ZIP: ".$foo[sizeof($foo)-2]." - ".basename($fv['path']));
  297.  
  298.             echo "    unzipping\n";
  299.             $this->_log2file("        unzipping");
  300.            
  301.             // unzip to temp dir
  302.             $tmpdir = $this->unzip_dir . DIRECTORY_SEPARATOR . str_ireplace(".zip", "", basename($fv['path'])) . DIRECTORY_SEPARATOR;
  303.             if (!mkdir($tmpdir)) return false;
  304.  
  305.             if (!$this->_unzip($fv['path'], $tmpdir))  {
  306.                 self::_s_delTree($tmpdir);
  307.                 return false;
  308.             }
  309.  
  310.             // scan temp dir
  311.             echo "    scanning\n";
  312.             $this->_log2file("        scanning");
  313.  
  314.             $found_installs = array();
  315.  
  316.             $this->_scanDir($tmpdir, $found_installs);
  317.  
  318.             $stats_zip = $this->getStats($found_installs);
  319.             $this->_log2file("          EXE: ".$stats_zip['exe']);
  320.             $this->_log2file("          ZIP: ".$stats_zip['zip']);
  321.             $this->_log2file("          Copy Content: ".$stats_zip['content']);
  322.             $this->_log2file("          Copy Runtime: ".$stats_zip['runtime']);
  323.             $this->_log2file("          Copy Other: ".$stats_zip['other']);
  324.             $this->_log2file("          Total: ".$stats_zip['total']);
  325.             $this->_log2file("          --------------------------");
  326.  
  327.             if (empty($found_installs))  {
  328.                 $this->_log2file("Nothing to install found.");
  329.                 self::_s_delTree($tmpdir);
  330.                 return false;
  331.             }
  332.  
  333.             // install using scanned method
  334.             echo "    installing\n";
  335.             $this->_log2file("        installing");
  336.  
  337.             if ($stats_zip['exe'] > 0)  {
  338.                 $this->_install_EXE($found_installs);
  339.             }
  340.             /*
  341.             // allow recursion?
  342.             if ($stats_zip['zip'] > 0)  {
  343.                 $this->_install_ZIP($found_installs);
  344.             }
  345.             */
  346.             if ($stats_zip['content'] > 0)  {
  347.                 $this->_install_CONTENT($found_installs);
  348.             }
  349.             if ($stats_zip['runtime'] > 0)  {
  350.                 $this->_install_RUNTIME($found_installs);
  351.             }
  352.             if ($stats_zip['other'] > 0)  {
  353.                 $this->_install_OTHER($found_installs);
  354.             }
  355.            
  356.                 // proceed as usual
  357.  
  358.             // delete temp dir
  359.             self::_s_delTree($tmpdir);
  360.  
  361.             $cnt++;
  362.             if ($this->bInstallOne) break;
  363.         }
  364.         // ------------------------------------------------------------------
  365.         $this->_log2file("    done installing zip files");
  366.         $this->_log2file("    ------------------------");
  367.     }
  368.     // --------------------------------------------------------------------- //
  369.     private function _install_CONTENT($data)  {
  370.         $stats = $this->getStats($data);
  371.  
  372.         $this->_log2file("    Copying ".$stats['content']." Content folders");
  373.         // ------------------------------------------------------------------
  374.         $cnt = 1;
  375.         foreach($data as $fk => $fv)  {
  376.             if ($fv['type'] != 'copy_content') continue;
  377.             $foo = explode(DIRECTORY_SEPARATOR, $fv['path']);
  378.  
  379.             echo "  Copying [".($cnt)."/".$stats['content']."]: ".$foo[sizeof($foo)-2]."\n";
  380.             $this->_log2file("      [".($cnt)."/".$stats['content']."] copying: ".$foo[sizeof($foo)-2]);
  381.             $cnt++;
  382.  
  383.             if ($this->create_target_subdirs)  {
  384.                 if (is_dir($this->install_dir . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR))  {
  385.                     if ($this->skip_existing_dirs == true)  {
  386.                         $this->_log2file("        target dir exists. Skipping.");
  387.                         continue;
  388.                     }
  389.                 } else if (!mkdir($this->install_dir . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR))
  390.                     die("Failed to create subdir: ".($this->install_dir . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR)."\n");
  391.                 chmod($this->install_dir . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR, 0777);
  392.                 $dst_dir = $this->install_dir . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR;
  393.             } else $dst_dir = $this->install_dir;
  394.  
  395.             $this->_recurse_copy($fv['path'], $dst_dir);
  396.  
  397.             if ($this->bInstallOne) break;
  398.         }
  399.         // ------------------------------------------------------------------
  400.         $this->_log2file("    done copying content folders");
  401.         $this->_log2file("    ------------------------");        
  402.     }
  403.     // --------------------------------------------------------------------- //
  404.     private function _install_OTHER($data)  {
  405.         $stats = $this->getStats($data);
  406.  
  407.         $this->_log2file("    Copying ".$stats['other']." other folders");
  408.         // ------------------------------------------------------------------
  409.         $cnt = 1;
  410.         foreach($data as $fk => $fv)  {
  411.             if ($fv['type'] != 'copy_other') continue;
  412.             $foo = explode(DIRECTORY_SEPARATOR, $fv['path']);
  413.  
  414.             $path_add =  ((isset($fv['dir'])) ? $fv['dir'] . DIRECTORY_SEPARATOR : '');
  415.  
  416.             echo "  Copying [".($cnt)."/".$stats['other']."]: ".$foo[sizeof($foo)-2]."\n";
  417.             $this->_log2file("      [".($cnt)."/".$stats['other']."] copying: ".$foo[sizeof($foo)-2]);
  418.             $cnt++;
  419.  
  420.             if ($this->create_target_subdirs)  {
  421.                 if (is_dir($this->install_dir . $path_add . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR))  {
  422.                     if ($this->skip_existing_dirs == true)  {
  423.                         $this->_log2file("        target dir exists. Skipping.");
  424.                         continue;
  425.                     }
  426.                 } else if (!mkdir($this->install_dir . $path_add . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR))
  427.                     die("Failed to create subdir: ".($this->install_dir . $path_add . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR)."\n");
  428.                 chmod($this->install_dir . $path_add . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR, 0777);
  429.                 $dst_dir = $this->install_dir . $path_add . $foo[sizeof($foo)-2].DIRECTORY_SEPARATOR;
  430.             } else $dst_dir = $this->install_dir . $path_add;
  431.  
  432.             $this->_recurse_copy($fv['path'], $dst_dir);
  433.  
  434.             if ($this->bInstallOne) break;
  435.         }
  436.         // ------------------------------------------------------------------
  437.         $this->_log2file("    done copying content folders");
  438.         $this->_log2file("    ------------------------");        
  439.     }
  440.     // --------------------------------------------------------------------- //
  441.     private function _install_RUNTIME($data)  {
  442.         $stats = $this->getStats($data);
  443.  
  444.         $this->_log2file("    Copying ".$stats['runtime']." Runtime folders");
  445.         // ------------------------------------------------------------------
  446.         $cnt = 1;
  447.         foreach($data as $fk => $fv)  {
  448.             if ($fv['type'] != 'copy_runtime') continue;
  449.             $foo = explode(DIRECTORY_SEPARATOR, $fv['path']);
  450.  
  451.             echo "  Copying [".($cnt)."/".$stats['runtime']."]: ".$foo[sizeof($foo)-3]."\n";
  452.             $this->_log2file("      [".($cnt)."/".$stats['runtime']."] copying: ".$foo[sizeof($foo)-3]."\n");
  453.             $cnt++;
  454.  
  455.             if ($this->create_target_subdirs)  {
  456.                 if (is_dir($this->install_dir . $foo[sizeof($foo)-3].DIRECTORY_SEPARATOR))  {
  457.                     if ($this->skip_existing_dirs == true)  {
  458.                         $this->_log2file("        target dir exists. Skipping.");
  459.                         continue;
  460.                     }
  461.                 } else if (!mkdir($this->install_dir . $foo[sizeof($foo)-3].DIRECTORY_SEPARATOR))
  462.                     die("Failed to create subdir: ".($this->install_dir . $foo[sizeof($foo)-3].DIRECTORY_SEPARATOR)."\n");
  463.                 chmod($this->install_dir . $foo[sizeof($foo)-3].DIRECTORY_SEPARATOR, 0777);
  464.                 $dst_dir = $this->install_dir . $foo[sizeof($foo)-3].DIRECTORY_SEPARATOR;
  465.             } else $dst_dir = $this->install_dir;
  466.  
  467.             $this->_recurse_copy($fv['path'], $dst_dir . 'runtime' .  DIRECTORY_SEPARATOR );
  468.  
  469.             if ($this->bInstallOne) break;
  470.         }
  471.         // ------------------------------------------------------------------
  472.         $this->_log2file("    done copying runtime folders");
  473.         $this->_log2file("    ------------------------");
  474.     }
  475.     // --------------------------------------------------------------------- //
  476.     private function _scanDir($_dir, &$found)  {
  477.         if (!is_dir($_dir)) return false;
  478.         $files = scandir($_dir);
  479.         if (empty($files)) return false;
  480.         foreach($files as $k => $v)  {
  481.             if ($v[0] == '.') continue;
  482.             $ext = substr($v, strrpos($v, '.')+1);
  483.             if ($ext == 'exe')  {
  484.                 $found[] = array('type' => 'exe', 'path' => $_dir . $v);
  485.             } else if ($ext == 'zip')  {
  486.                 // moah complicated, as we don't know: a) what's in the zip, b) if it has been unpacked already
  487.                 // check for b)
  488.                 if (is_dir($_dir . substr($v, 0, -4) . DIRECTORY_SEPARATOR))
  489.                     continue;
  490.                 // check for a)
  491.                 if ($this->_scanZip($_dir . $v))  {
  492.                     $found[] = array('type' => 'zip', 'path' => $_dir . $v);
  493.                 } else {
  494.                     echo "__unusable ZIP: ". $_dir . $v . "\n";
  495.                  }
  496.             } else if (is_dir($_dir . $v . '/'))  {
  497.                 // Content has higher priority, as it can contain a runtime folder
  498.                 if (strtolower($v) == 'content')  {
  499.                     $found[] = array('type' => 'copy_content', 'path' => $_dir . $v . DIRECTORY_SEPARATOR);
  500.                 } else if (strtolower($v) == 'runtime')  {
  501.                     $found[] = array('type' => 'copy_runtime', 'path' => $_dir . $v . DIRECTORY_SEPARATOR);
  502.                 } else if (strtolower($v) == 'people')  {
  503.                     $found[] = array('type' => 'copy_other', 'path' => $_dir . $v . DIRECTORY_SEPARATOR, 'dir' => 'People');
  504.                 } else if (strtolower($v) == 'data')  {
  505.                     $found[] = array('type' => 'copy_other', 'path' => $_dir . $v . DIRECTORY_SEPARATOR, 'dir' => 'data');
  506.                 } else {
  507.                     if (!in_array(strtolower($v), $this->ignore_dirs))
  508.                         $this->_scanDir($_dir . $v . DIRECTORY_SEPARATOR, $found);
  509.                 }
  510.             }
  511.         }
  512.     }
  513.     // --------------------------------------------------------------------- //
  514.     private function _scanZip($zipfile)  {
  515.         if (!is_file($zipfile)) return false;
  516.  
  517.         $_internal_ignore = array();
  518.         $_found_something = false;
  519.  
  520.         $zip = new ZipArchive();            
  521.  
  522.         if ($zip->open($zipfile))  {
  523.             for ($i=0; $i<$zip->numFiles;$i++) {
  524.                 $filename = $zip->statIndex($i);
  525.                 $filename = $filename['name'];
  526.  
  527.                 if (stripos(strtolower($filename), '.exe') !== false)  {
  528.                     #echo "______ZIP: found exe: ".$filename."\n";
  529.                    $_found_something = true;
  530.                     continue;
  531.                 }
  532.                 if (!empty($_internal_ignore))  {
  533.                     foreach($_internal_ignore as $ik => $iv)  {
  534.                         if (strpos($filename, $iv) !== false)  {
  535.                             #echo "______ZIP: ignore ".$filename."\n";
  536.                            continue(2);
  537.                         }
  538.                     }
  539.                 }
  540.                 if (stripos(strtolower($filename), 'content') !== false)  {
  541.                     #echo "______ZIP: found content: ".$filename." - ".dirname($filename.'/').'/'."\n";
  542.                    $add = dirname($filename);
  543.                     if ($add == '.') $add = (($filename[0] === 'C') ? 'C' : 'c') . 'ontent'.DIRECTORY_SEPARATOR;
  544.                     $_internal_ignore[] = $add;
  545.                     $_found_something = true;
  546.                 } else if (stripos(strtolower($filename), 'runtime') !== false)  {
  547.                     #echo "______ZIP: found runtime: ".$filename."\n";
  548.                    $add = dirname($filename);
  549.                     if ($add == '.') $add = (($filename[0] === 'R') ? 'R' : 'R') . 'untime'.DIRECTORY_SEPARATOR; // force uppercase
  550.                     $_internal_ignore[] = $add;
  551.                     $_found_something = true;
  552.                 } else if (stripos(strtolower($filename), 'people') !== false)  {
  553.                     #echo "______ZIP: found people: ".$filename."\n";
  554.                    $add = dirname($filename);
  555.                     if ($add == '.') $add = (($filename[0] === 'P') ? 'P' : 'P') . 'eople'.DIRECTORY_SEPARATOR; // force uppercase
  556.                     $_internal_ignore[] = $add;
  557.                     $_found_something = true;
  558.                 } else if (stripos(strtolower($filename), 'data') !== false)  {
  559.                     #echo "______ZIP: found data: ".$filename."\n";
  560.                    $add = dirname($filename);
  561.                     if ($add == '.') $add = (($filename[0] === 'D') ? 'd' : 'd') . 'ata'.DIRECTORY_SEPARATOR; // force lowercase
  562.                     $_internal_ignore[] = $add;
  563.                     $_found_something = true;
  564.                 }  else {
  565.                     #echo "__found unknown folder: ".dirname($filename)."\n";
  566.                }
  567.             }
  568.             $zip->close();
  569.         }
  570.         return $_found_something;
  571.     }
  572.     // --------------------------------------------------------------------- //
  573.     private function _unzip($zipfile, $target)  {
  574.         if (!is_file($zipfile)) return false;
  575.         if (!is_dir($target))   return false;
  576.         $zip = new ZipArchive();
  577.         if ($zip->open($zipfile))  {
  578.             $zip->extractTo($target);
  579.             $zip->close();
  580.         }
  581.         return true;
  582.     }
  583.     // --------------------------------------------------------------------- //
  584.     public function getStats($data)  {
  585.         $stats = array(
  586.             'exe' => 0,
  587.             'zip' => 0,
  588.             'runtime' => 0,
  589.             'content' => 0,
  590.             'other'   => 0,
  591.             'total' => 0
  592.         );
  593.         if (empty($data) || !is_array($data)) return $stats;
  594.            foreach($data as $k => $v)  {
  595.             switch($v['type'])  {
  596.                 case 'exe' : $stats['exe']++; break;
  597.                 case 'zip' : $stats['zip']++; break;
  598.                 case 'copy_content' : $stats['content']++; break;
  599.                 case 'copy_runtime' : $stats['runtime']++; break;
  600.                 case 'copy_other'   : $stats['other']++; break;
  601.                 default: break;
  602.             }
  603.         }
  604.         $stats['total'] = array_sum($stats);
  605.         return $stats;
  606.     }
  607.     // --------------------------------------------------------------------- //
  608.     private function _log2file($str, $timestamp = true, $fatal = false)  {
  609.         if (($fh = fopen($this->log_dir . DIRECTORY_SEPARATOR . $this->_logfile, 'ab+')))  {
  610.             fwrite($fh, (($timestamp) ? '['.strftime("%d.%m.%Y-%H:%M:%S").']' : '') . $str . "\n");
  611.             if ($fatal) fwrite($fh, (($timestamp) ? '['.strftime("%d.%m.%Y-%H:%M:%S").']' : '') ."Exiting due to fatal error.\n");
  612.             fclose($fh);
  613.         if ($fatal) die($str."\n");
  614.         } else die("Failed to write to log ".$this->log_dir . $this->_logfile."\n");
  615.         if ($fatal) exit;
  616.     }
  617.     // --------------------------------------------------------------------- //
  618.     private function _recurse_copy($src,$dst) {
  619.         $dir = opendir($src);
  620.         @mkdir($dst);
  621.         while(false !== ( $file = readdir($dir)) ) {
  622.             if (( $file != '.' ) && ( $file != '..' )) {
  623.                 if ( is_dir($src . DIRECTORY_SEPARATOR . $file) ) {
  624.                     $this->_recurse_copy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file);
  625.                 }
  626.                 else {
  627.                     copy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file);
  628.                 }
  629.             }
  630.         }
  631.         closedir($dir);
  632.     }
  633.     // --------------------------------------------------------------------- //
  634.     private static function _s_delTree($dir) {
  635.         if ($dir == '/' || !strlen($dir)) die("FATAL. delTree called without dir or with root: ".$dir."\n");
  636.         $files = array_diff(scandir($dir), array('.','..'));
  637.         foreach ($files as $file) {
  638.             (is_dir("$dir/$file")) ? self::_s_delTree("$dir/$file") : unlink("$dir/$file");
  639.         }
  640.         return rmdir($dir);
  641.     }
  642.     // --------------------------------------------------------------------- //
  643. }
  644.  
  645.  
  646.  
  647. ///////////////////////////////////////////////////////////////////////////////
  648. ///////////////////////////////////////////////////////////////////////////////
  649. $daz_installer = new DAZ3D_Installer();
  650.  
  651. // Windows:
  652. /* remove "#" in front of the next three lines. add "#" in front of the three linux lines */
  653. #$daz_installer->source_dir     = "C:\\content_4_install\\";
  654. #$daz_installer->install_dir    = "C:\\extracted\\";
  655. #$daz_installer->unzip_dir  = 'C:\\temp\\';
  656.  
  657. // Linux
  658. $daz_installer->source_dir  = "/media/MyDrive/content_4_install/";
  659. $daz_installer->install_dir     = "/media/MyDrive/extracted/";
  660. $daz_installer->unzip_dir   = '/media/MyDrive/_temp/';
  661.  
  662.  
  663. $daz_installer->log_dir     = dirname(__FILE__). DIRECTORY_SEPARATOR;
  664. $daz_installer->installed_file  = dirname(__FILE__). DIRECTORY_SEPARATOR . 'installed.txt';
  665.  
  666. ///////////////////////////////////////////////////////////////////////////////
  667. $daz_installer->create_target_subdirs = false;
  668. $daz_installer->skip_existing_dirs    = false;
  669. $daz_installer->bInstallOne       = false;
  670. ///////////////////////////////////////////////////////////////////////////////
  671. $daz_installer->install();
  672. ///////////////////////////////////////////////////////////////////////////////
  673. ///////////////////////////////////////////////////////////////////////////////
  674. ?>
Advertisement
Add Comment
Please, Sign In to add comment