uncompress.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env php
  2. <?php
  3. /* $Id: uncompress.php 2939 2007-04-23 16:44:45Z b4rt $ */
  4. /*******************************************************************************
  5. LICENSE
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License (GPL)
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  15. *******************************************************************************/
  16. // prevent invocation from web
  17. if (empty($argv[0])) die();
  18. if (isset($_REQUEST['argv'])) die();
  19. // dummy
  20. $_SESSION = array('cache' => false);
  21. /******************************************************************************/
  22. // change to docroot if needed
  23. if (!is_file(realpath(getcwd().'/inc/functions/functions.core.php')))
  24. chdir(realpath(dirname(__FILE__)."/.."));
  25. // check for home
  26. if (!is_file('inc/functions/functions.core.php'))
  27. exit("Error: this script can only be used in its default-path (DOCROOT/bin/)\n");
  28. // core functions
  29. require_once('inc/functions/functions.core.php');
  30. /**
  31. * @author R.D. Damron
  32. * @name rar/zip uncompression
  33. * @usage ./uncompress.php "pathtofile" "extractdir" "typeofcompression" "uncompressor-bin" "password"
  34. */
  35. $logfile = 'error.log';
  36. //convert and set variables
  37. $arg1 = urldecode($argv[1]);
  38. $arg2 = urldecode($argv[2]);
  39. $arg3 = $argv[3];
  40. $arg4 = $argv[4];
  41. $arg5 = $argv[5];
  42. // unrar file
  43. if (strcasecmp('rar', $arg3) == 0){
  44. if (file_exists($arg2.$logfile))
  45. @unlink($arg2.$logfile);
  46. $Command = tfb_shellencode($arg4)." x -o+ -p". tfb_shellencode($arg5) ." ". tfb_shellencode($arg1) . " " . tfb_shellencode($arg2);
  47. $unrarpid = trim(shell_exec("nohup ".$Command." > " . tfb_shellencode($arg2.$logfile) . " 2>&1 & echo $!"));
  48. echo 'Uncompressing file...<BR>PID is: ' . $unrarpid . '<BR>';
  49. usleep(250000); // wait for 0.25 seconds
  50. while (is_running($unrarpid)) {
  51. if (file_exists($arg2.$logfile)) {
  52. $lines = file($arg2.$logfile);
  53. foreach($lines as $chkline) {
  54. if (strpos($chkline, 'already exists. Overwrite it ?') !== FALSE){
  55. kill($unrarpid);
  56. echo 'File has already been extracted, please delete extracted file if re-extraction is necessary.';
  57. break 2;
  58. }
  59. if (strpos($chkline, 'Cannot find volume') !== FALSE){
  60. kill($unrarpid);
  61. echo 'File has a missing volume and can not been extracted.';
  62. break 2;
  63. }
  64. if (strpos($chkline, 'ERROR: Bad archive') !== FALSE){
  65. kill($unrarpid);
  66. echo 'File has a bad volume and can not been extracted.';
  67. break 2;
  68. }
  69. if (strpos($chkline, 'CRC failed') !== FALSE){
  70. kill($unrarpid);
  71. echo 'File extraction has failed with a CRC error and was not been extracted.';
  72. break 2;
  73. }
  74. }
  75. }
  76. usleep(250000); // wait for 0.25 seconds
  77. }
  78. if (file_exists($arg2.$logfile)) {
  79. $lines = file($arg2.$logfile);
  80. foreach($lines as $chkline) {
  81. if (strpos($chkline, 'All OK') !== FALSE){
  82. echo 'File has successfully been extracted!';
  83. @unlink($arg2.$logfile);
  84. // exit
  85. exit();
  86. }
  87. }
  88. }
  89. // exit
  90. exit();
  91. }
  92. // unzip
  93. if (strcasecmp('zip', $arg3) == 0) {
  94. if (file_exists($arg2.$logfile))
  95. @unlink($arg2.$logfile);
  96. $Command = tfb_shellencode($arg4).' -o ' . tfb_shellencode($arg1) . ' -d ' . tfb_shellencode($arg2);
  97. $unzippid = trim(shell_exec("nohup ".$Command." > " . tfb_shellencode($arg2.$logfile) . " 2>&1 & echo $!"));
  98. echo 'Uncompressing file...<BR>PID is: ' . $unzippid . '<BR>';
  99. usleep(250000); // wait for 0.25 seconds
  100. while (is_running($unzippid)) {
  101. usleep(250000); // wait for 0.25 seconds
  102. /* occupy time to cause popup window load bar to load in conjunction with unzip progress */
  103. }
  104. // exit
  105. exit();
  106. }
  107. /**
  108. * is_running
  109. *
  110. * @param $PID
  111. * @return
  112. */
  113. function is_running($PID){
  114. $ProcessState = exec("ps ".tfb_shellencode($PID));
  115. return (count($ProcessState) >= 2);
  116. }
  117. /**
  118. * kill
  119. *
  120. * @param $PID
  121. * @return
  122. */
  123. function kill($PID){
  124. exec("kill -KILL ".tfb_shellencode($PID));
  125. return true;
  126. }
  127. /**
  128. * del
  129. *
  130. * @param $file
  131. * @return
  132. */
  133. function del($file){
  134. exec("rm -rf ".tfb_shellencode($file));
  135. return true;
  136. }
  137. ?>