PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/options/split.php

http://rapidleech.googlecode.com/
PHP | 168 lines | 167 code | 1 blank | 0 comment | 11 complexity | 1448800c0f4150728eea9d713a0439db MD5 | raw file
  1. <?php
  2. function rl_split() {
  3. global $PHP_SELF, $list, $options;
  4. ?>
  5. <form method="post" action="<?php echo $PHP_SELF; ?>">
  6. <input type="hidden" name="act" value="split_go" />
  7. <table align="center">
  8. <tr>
  9. <td>
  10. <table>
  11. <?php
  12. for($i = 0; $i < count ( $_GET ["files"] ); $i ++) {
  13. $file = $list [$_GET ["files"] [$i]];
  14. ?>
  15. <tr>
  16. <td align="center">
  17. <input type="hidden" name="files[]" value="<?php echo $_GET ["files"] [$i]; ?>" /> <b><?php echo basename ( $file ["name"] ); ?></b>
  18. </td>
  19. </tr>
  20. <tr>
  21. <td><?php echo lang(143); ?>:&nbsp;<input type="text" name="partSize[]"
  22. size="2"
  23. value="<?php echo ($_COOKIE ["partSize"] ? $_COOKIE ["partSize"] : 10); ?>" />&nbsp;MB
  24. </td>
  25. </tr>
  26. <?php
  27. if ($options['download_dir_is_changeable']) {
  28. ?>
  29. <tr>
  30. <td><?php echo lang(40); ?>:&nbsp;<input type="text" name="saveTo[]" size="40"
  31. value="<?php echo addslashes ( $options['download_dir'] ); ?>" /></td>
  32. </tr>
  33. <?php
  34. }
  35. ?>
  36. <tr>
  37. <td><input type="checkbox" name="del_ok"
  38. <?php echo $options['disable_deleting'] ? 'disabled="disabled"' : 'checked="checked"'; ?> />&nbsp;<?php echo lang(203); ?></td>
  39. </tr>
  40. <tr>
  41. <td align="left">CRC32 generation mode:<br />
  42. <?php
  43. if (function_exists ( 'hash_file' )) {
  44. ?><input type="radio" name="crc_mode[<?php echo $i; ?>]"
  45. value="hash_file" checked="checked" />&nbsp;Use hash_file (Recommended)<br />
  46. <?php
  47. }
  48. ?>
  49. <input type="radio"
  50. name="crc_mode[<?php echo $i; ?>]" value="file_read" />&nbsp;Read file to memory<br />
  51. <input type="radio" name="crc_mode[<?php echo $i; ?>]"
  52. value="fake"
  53. <?php
  54. if (! function_exists ( 'hash_file' )) {
  55. echo ' checked="checked"';
  56. }
  57. ?> />&nbsp;Fake
  58. crc</td>
  59. </tr>
  60. <tr>
  61. <td></td>
  62. </tr>
  63. <?php
  64. }
  65. ?>
  66. </table>
  67. </td>
  68. <td><input type="submit" value="<?php echo lang(290); ?>" /></td>
  69. </tr>
  70. <tr>
  71. <td></td>
  72. </tr>
  73. </table>
  74. </form>
  75. <?php
  76. }
  77. function split_go() {
  78. global $list, $options;
  79. for($i = 0; $i < count ( $_POST ["files"] ); $i ++) {
  80. $split_ok = true;
  81. $file = $list [$_POST ["files"] [$i]];
  82. $partSize = round ( ($_POST ["partSize"] [$i]) * 1024 * 1024 );
  83. $saveTo = ($options['download_dir_is_changeable'] ? stripslashes ( $_POST ["saveTo"] [$i] ) : realpath ( $options['download_dir'] )) . '/';
  84. $dest_name = basename ( $file ["name"] );
  85. $fileSize = filesize ( $file ["name"] );
  86. $totalParts = ceil ( $fileSize / $partSize );
  87. $crc = ($_POST ['crc_mode'] [$i] == 'file_read') ? dechex ( crc32 ( read_file ( $file ["name"] ) ) ) : (($_POST ['crc_mode'] [$i] == 'hash_file' && function_exists ( 'hash_file' )) ? hash_file ( 'crc32b', $file ["name"] ) : '111111');
  88. $crc = str_repeat ( "0", 8 - strlen ( $crc ) ) . strtoupper ( $crc );
  89. echo "Started to split file <b>" . basename ( $file ["name"] ) . "</b> parts of " . bytesToKbOrMbOrGb ( $partSize ) . ", Using Method - Total Commander...<br />";
  90. echo "Total Parts: <b>" . $totalParts . "</b><br /><br />";
  91. for($j = 1; $j <= $totalParts; $j ++) {
  92. if (file_exists ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) )) {
  93. echo "It is not possible to split the file. A piece already exists<b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
  94. continue 2;
  95. }
  96. }
  97. if (file_exists ( $saveTo . $dest_name . '.crc' )) {
  98. echo "It is not possible to split the file. CRC file already exists<b>" . $dest_name . '.crc' . "</b> !<br /><br />";
  99. } elseif (! is_file ( $file ["name"] )) {
  100. echo "It is not possible to split the file. Source file not found<b>" . $file ["name"] . "</b> !<br /><br />";
  101. } elseif (! is_dir ( $saveTo )) {
  102. echo "It is not possible to split the file. Directory doesn't exist<b>" . $saveTo . "</b> !<br /><br />";
  103. } elseif (! @write_file ( $saveTo . $dest_name . ".crc", "filename=" . $dest_name . "\r\n" . "size=" . $fileSize . "\r\n" . "crc32=" . $crc . "\r\n" )) {
  104. echo "It is not possible to split the file. CRC Error<b>" . $dest_name . ".crc" . "</b> !<br /><br />";
  105. } else {
  106. $time = filemtime ( $saveTo . $dest_name . '.crc' );
  107. while ( isset ( $list [$time] ) ) {
  108. $time ++;
  109. }
  110. $list [$time] = array ("name" => $saveTo . $dest_name . '.crc', "size" => bytesToKbOrMbOrGb ( filesize ( $saveTo . $dest_name . '.crc' ) ), "date" => $time );
  111. $split_buffer_size = 2 * 1024 * 1024;
  112. $split_source = @fopen ( $file ["name"], "rb" );
  113. if (! $split_source) {
  114. echo "It is not possible to open source file <b>" . $file ["name"] . "</b> !<br /><br />";
  115. continue;
  116. }
  117. for($j = 1; $j <= $totalParts; $j ++) {
  118. $split_dest = @fopen ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ), "wb" );
  119. if (! $split_dest) {
  120. echo "Error openning file <b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
  121. $split_ok = false;
  122. break;
  123. }
  124. $split_write_times = floor ( $partSize / $split_buffer_size );
  125. for($k = 0; $k < $split_write_times; $k ++) {
  126. $split_buffer = fread ( $split_source, $split_buffer_size );
  127. if (fwrite ( $split_dest, $split_buffer ) === false) {
  128. echo "Error writing the file <b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
  129. $split_ok = false;
  130. break;
  131. }
  132. }
  133. $split_rest = $partSize - ($split_write_times * $split_buffer_size);
  134. if ($split_ok && $split_rest > 0) {
  135. $split_buffer = fread ( $split_source, $split_rest );
  136. if (fwrite ( $split_dest, $split_buffer ) === false) {
  137. echo "Error writing the file <b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
  138. $split_ok = false;
  139. }
  140. }
  141. fclose ( $split_dest );
  142. if ($split_ok) {
  143. $time = filemtime ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) );
  144. while ( isset ( $list [$time] ) ) {
  145. $time ++;
  146. }
  147. $list [$time] = array ("name" => $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ), "size" => bytesToKbOrMbOrGb ( filesize ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) ) ), "date" => $time );
  148. }
  149. }
  150. fclose ( $split_source );
  151. if ($split_ok) {
  152. if ($_POST["del_ok"] && !$options['disable_deleting']) {
  153. if (@unlink ( $file ["name"] )) {
  154. unset ( $list [$_POST ["files"] [$i]] );
  155. echo "Source file deleted.<br /><br />";
  156. } else {
  157. echo "Source file is<b>not deleted!</b><br /><br />";
  158. }
  159. }
  160. }
  161. if (! updateListInFile ( $list )) {
  162. echo "Couldn't update file list. Problem writing to file!<br /><br />";
  163. }
  164. }
  165. }
  166. }
  167. ?>