PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/options/merge.php

http://rapidleech.googlecode.com/
PHP | 177 lines | 176 code | 1 blank | 0 comment | 59 complexity | c5eb92c5859b5d8a90cd8d82f550a7c5 MD5 | raw file
  1. <?php
  2. function merge() {
  3. global $options, $list, $PHP_SELF;
  4. if (count($_GET["files"]) !== 1) {
  5. echo lang(167)."<br /><br />";
  6. }
  7. else {
  8. $file = $list [$_GET ["files"] [0]];
  9. if (substr ( $file ["name"], - 4 ) == '.001' && is_file ( substr ( $file ["name"], 0, - 4 ) . '.crc' )) {
  10. echo lang(168)."<br /><br />";
  11. } elseif (substr ( $file ["name"], - 4 ) !== '.crc' && substr ( $file ["name"], - 4 ) !== '.001') {
  12. echo lang(169)."<br /><br />";
  13. } else {
  14. echo lang(306)." <b>".basename(substr($file["name"], 0, -4))."</b><br /><br />";
  15. $usingcrcfile = (substr ( $file ["name"], - 4 ) === '.001') ? false : true;
  16. ?>
  17. <form method="post" action="<?php echo $PHP_SELF; ?>"><input type="hidden" name="files[0]" value="<?php echo $_GET ["files"] [0]; ?>" />
  18. <table>
  19. <?php
  20. if ($usingcrcfile) {
  21. ?>
  22. <tr>
  23. <td align="left"><input type="checkbox" name="crc_check" value="1" checked="checked" onclick="javascript:var displ=this.checked?'inline':'none';document.getElementById('crc_check_mode').style.display=displ;" />&nbsp;<?php echo lang(170); ?><br />
  24. <span id="crc_check_mode"><?php echo lang(171); ?>:<br />
  25. <?php
  26. if (function_exists ( 'hash_file' )) {
  27. ?><input type="radio" name="crc_mode" value="hash_file" checked="checked" />&nbsp;<?php echo lang(172); ?><br />
  28. <?php } ?>
  29. <input type="radio" name="crc_mode" value="file_read" />&nbsp;<?php echo lang(173); ?><br />
  30. <input type="radio" name="crc_mode" value="fake"<?php if (! function_exists ( 'hash_file' )) { echo 'checked="checked"'; }?> />&nbsp;<?php echo lang(174); ?></span></td>
  31. </tr>
  32. <tr>
  33. <td><input type="checkbox" name="del_ok" <?php echo $options['disable_deleting'] ? 'disabled="disabled"' : 'checked="checked"'; ?> />&nbsp;<?php echo lang(175); ?></td>
  34. </tr>
  35. <?php
  36. } else {
  37. ?>
  38. <tr>
  39. <td align="center"><?php echo lang(176); ?>: <b><?php echo lang(177); ?></b></td>
  40. </tr>
  41. <?php
  42. }
  43. ?>
  44. <tr>
  45. <td align="center"><input type="hidden" name="act" value="merge_go" /> <input type="submit" value="<?php echo lang(291); ?>" /></td>
  46. </tr>
  47. </table>
  48. </form>
  49. <?php
  50. }
  51. }
  52. }
  53. function merge_go() {
  54. global $list, $options;
  55. if (count($_POST["files"]) !== 1) {
  56. echo lang(167)."<br /><br />";
  57. } else {
  58. $file = $list [$_POST ["files"] [0]];
  59. if (substr ( $file ["name"], - 4 ) == '.001' && is_file ( substr ( $file ["name"], 0, - 4 ) . '.crc' )) {
  60. echo lang(168)."<br /><br />";
  61. } elseif (substr ( $file ["name"], - 4 ) !== '.crc' && substr ( $file ["name"], - 4 ) !== '.001') {
  62. echo lang(169)."<br /><br />";
  63. } else {
  64. $usingcrcfile = (substr ( $file ["name"], - 4 ) === '.001') ? false : true;
  65. if (! $usingcrcfile) {
  66. $data = array ('filename' => basename ( substr ( $file ["name"], 0, - 4 ) ), 'size' => - 1, 'crc32' => '00111111' );
  67. } else {
  68. $fs = @fopen ( $file ["name"], "rb" );
  69. }
  70. if ($usingcrcfile && ! $fs) {
  71. echo lang(178)."<br /><br />";
  72. } else {
  73. if ($usingcrcfile) {
  74. $data = array ();
  75. while ( ! feof ( $fs ) ) {
  76. $data_ = explode ( '=', trim ( fgets ( $fs ) ), 2 );
  77. $data [$data_ [0]] = $data_ [1];
  78. }
  79. fclose ( $fs );
  80. }
  81. $path = realpath ( DOWNLOAD_DIR ) . '/';
  82. $filename = basename ( $data ['filename'] );
  83. $partfiles = array ();
  84. $partsSize = 0;
  85. for($j = 1; $j < 10000; $j ++) {
  86. if (! is_file ( $path . $filename . '.' . sprintf ( "%03d", $j ) )) {
  87. if ($j == 1) {
  88. $partsSize = - 1;
  89. }
  90. break;
  91. }
  92. $partfiles [] = $path . $filename . '.' . sprintf ( "%03d", $j );
  93. $partsSize += filesize ( $path . $filename . '.' . sprintf ( "%03d", $j ) );
  94. }
  95. if (file_exists ( $path . $filename )) {
  96. printf(lang(179),$path . $filename);
  97. echo "<br /><br />";
  98. } elseif ($usingcrcfile && $partsSize != $data ['size']) {
  99. echo lang(180)."<br /><br />";
  100. } elseif ($options['check_these_before_unzipping'] && is_array ( $options['forbidden_filetypes'] ) && in_array ( strtolower ( strrchr ( $filename, "." ) ), $options['forbidden_filetypes'] )) {
  101. printf(lang(181),strrchr ( $filename, "." ));
  102. echo "<br /><br />";
  103. } else {
  104. $merge_buffer_size = 2 * 1024 * 1024;
  105. $merge_dest = @fopen ( $path . $filename, "wb" );
  106. if (! $merge_dest) {
  107. printf(lang(182),$path . $filename);
  108. echo "<br /><br />";
  109. } else {
  110. $merge_ok = true;
  111. foreach ( $partfiles as $part ) {
  112. $merge_source = @fopen ( $part, "rb" );
  113. while ( ! feof ( $merge_source ) ) {
  114. $merge_buffer = fread ( $merge_source, $merge_buffer_size );
  115. if ($merge_buffer === false) {
  116. printf(lang(65),$part);
  117. echo "<br /><br />";
  118. $merge_ok = false;
  119. break;
  120. }
  121. if (fwrite ( $merge_dest, $merge_buffer ) === false) {
  122. printf(lang(183),$path . $filename);
  123. echo "<br /><br />";
  124. $merge_ok = false;
  125. break;
  126. }
  127. }
  128. fclose ( $merge_source );
  129. if (! $merge_ok) {
  130. break;
  131. }
  132. }
  133. fclose ( $merge_dest );
  134. if ($merge_ok) {
  135. $fc = ($_POST ['crc_mode'] == 'file_read') ? dechex ( crc32 ( read_file ( $path . $filename ) ) ) : (($_POST ['crc_mode'] == 'hash_file' && function_exists ( 'hash_file' )) ? hash_file ( 'crc32b', $path . $filename ) : '111111');
  136. $fc = str_repeat ( "0", 8 - strlen ( $fc ) ) . strtoupper ( $fc );
  137. if ($fc != strtoupper ( $data ["crc32"] )) {
  138. echo lang(184)."<br /><br />";
  139. } else {
  140. printf(lang(185),$filename);
  141. echo '!<br /><br />';
  142. if ($usingcrcfile && $fc != '00111111' && $_POST["del_ok"] && !$options['disable_deleting']) {
  143. if ($usingcrcfile) {
  144. $partfiles [] = $file ["name"];
  145. }
  146. foreach ( $partfiles as $part ) {
  147. if (@unlink ( $part )) {
  148. foreach ( $list as $list_key => $list_file ) {
  149. if ($list_file ["name"] === $part) {
  150. unset ( $list [$list_key] );
  151. }
  152. }
  153. echo "<b>" . basename ( $part ) . "</b> ".lang(186).".<br />";
  154. } else {
  155. echo "<b>" . basename ( $part ) . "</b> ".lang(187).".<br />";
  156. }
  157. }
  158. echo "<br />";
  159. }
  160. $time = filemtime($path.$filename);
  161. while ( isset ( $list [$time] ) ) {
  162. $time ++;
  163. }
  164. $list [$time] = array ("name" => $path . $filename, "size" => bytesToKbOrMbOrGb ( $partsSize ), "date" => $time );
  165. if (! updateListInFile ( $list )) {
  166. echo lang(146)."<br /><br />";
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. ?>