rapidleech /classes/options/split.php

Language PHP Lines 168
MD5 Hash 1448800c0f4150728eea9d713a0439db Estimated Cost $2,763 (why?)
Repository http://rapidleech.googlecode.com/svn/trunk/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
function rl_split() {
	global $PHP_SELF, $list, $options;
?>
<form method="post" action="<?php echo $PHP_SELF; ?>">
<input type="hidden" name="act" value="split_go" />
		<table align="center">
			<tr>
				<td>
				<table>
<?php
	for($i = 0; $i < count ( $_GET ["files"] ); $i ++) {
		$file = $list [$_GET ["files"] [$i]];
?>
<tr>
<td align="center">
<input type="hidden" name="files[]" value="<?php echo $_GET ["files"] [$i]; ?>" /> <b><?php echo basename ( $file ["name"] ); ?></b>
						</td>
					</tr>
					<tr>
						<td><?php echo lang(143); ?>:&nbsp;<input type="text" name="partSize[]"
							size="2"
							value="<?php echo ($_COOKIE ["partSize"] ? $_COOKIE ["partSize"] : 10); ?>" />&nbsp;MB
						</td>
					</tr>
<?php
		if ($options['download_dir_is_changeable']) {
?>
<tr>
						<td><?php echo lang(40); ?>:&nbsp;<input type="text" name="saveTo[]" size="40"
							value="<?php echo addslashes ( $options['download_dir'] ); ?>" /></td>
					</tr>
<?php
		}
?>
					<tr>
						<td><input type="checkbox" name="del_ok"
							<?php echo $options['disable_deleting'] ? 'disabled="disabled"' : 'checked="checked"'; ?> />&nbsp;<?php echo lang(203); ?></td>
					</tr>
					<tr>
						<td align="left">CRC32 generation mode:<br />
<?php
		if (function_exists ( 'hash_file' )) {
?><input type="radio" name="crc_mode[<?php echo $i; ?>]"
							value="hash_file" checked="checked" />&nbsp;Use hash_file (Recommended)<br />
<?php
		}
?>
						<input type="radio"
							name="crc_mode[<?php echo $i; ?>]" value="file_read" />&nbsp;Read file to memory<br />
						<input type="radio" name="crc_mode[<?php echo $i; ?>]"
							value="fake"
<?php
			if (! function_exists ( 'hash_file' )) {
				echo ' checked="checked"';
			}
?> />&nbsp;Fake
						crc</td>
					</tr>
					<tr>
						<td></td>
					</tr>
<?php
	}
?>
                                  </table>
				</td>
				<td><input type="submit" value="<?php echo lang(290); ?>" /></td>
			</tr>
			<tr>
				<td></td>
			</tr>
		</table>
		</form>
<?php
}

function split_go() {
	global $list, $options;
	for($i = 0; $i < count ( $_POST ["files"] ); $i ++) {
		$split_ok = true;
		$file = $list [$_POST ["files"] [$i]];
		$partSize = round ( ($_POST ["partSize"] [$i]) * 1024 * 1024 );
		$saveTo = ($options['download_dir_is_changeable'] ? stripslashes ( $_POST ["saveTo"] [$i] ) : realpath ( $options['download_dir'] )) . '/';
		$dest_name = basename ( $file ["name"] );
		$fileSize = filesize ( $file ["name"] );
		$totalParts = ceil ( $fileSize / $partSize );
		$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');
		$crc = str_repeat ( "0", 8 - strlen ( $crc ) ) . strtoupper ( $crc );
		echo "Started to split file <b>" . basename ( $file ["name"] ) . "</b> parts of " . bytesToKbOrMbOrGb ( $partSize ) . ", Using Method - Total Commander...<br />";
		echo "Total Parts: <b>" . $totalParts . "</b><br /><br />";
		for($j = 1; $j <= $totalParts; $j ++) {
			if (file_exists ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) )) {
				echo "It is not possible to split the file. A piece already exists<b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
				continue 2;
			}
		}
		if (file_exists ( $saveTo . $dest_name . '.crc' )) {
			echo "It is not possible to split the file. CRC file already exists<b>" . $dest_name . '.crc' . "</b> !<br /><br />";
		} elseif (! is_file ( $file ["name"] )) {
			echo "It is not possible to split the file. Source file not found<b>" . $file ["name"] . "</b> !<br /><br />";
		} elseif (! is_dir ( $saveTo )) {
			echo "It is not possible to split the file. Directory doesn't exist<b>" . $saveTo . "</b> !<br /><br />";
		} elseif (! @write_file ( $saveTo . $dest_name . ".crc", "filename=" . $dest_name . "\r\n" . "size=" . $fileSize . "\r\n" . "crc32=" . $crc . "\r\n" )) {
			echo "It is not possible to split the file. CRC Error<b>" . $dest_name . ".crc" . "</b> !<br /><br />";
		} else {
			$time = filemtime ( $saveTo . $dest_name . '.crc' );
			while ( isset ( $list [$time] ) ) {
				$time ++;
			}
			$list [$time] = array ("name" => $saveTo . $dest_name . '.crc', "size" => bytesToKbOrMbOrGb ( filesize ( $saveTo . $dest_name . '.crc' ) ), "date" => $time );
			$split_buffer_size = 2 * 1024 * 1024;
			$split_source = @fopen ( $file ["name"], "rb" );
			if (! $split_source) {
				echo "It is not possible to open source file <b>" . $file ["name"] . "</b> !<br /><br />";
				continue;
			}
			for($j = 1; $j <= $totalParts; $j ++) {
				$split_dest = @fopen ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ), "wb" );
				if (! $split_dest) {
					echo "Error openning file <b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
					$split_ok = false;
					break;
				}
				$split_write_times = floor ( $partSize / $split_buffer_size );
				for($k = 0; $k < $split_write_times; $k ++) {
					$split_buffer = fread ( $split_source, $split_buffer_size );
					if (fwrite ( $split_dest, $split_buffer ) === false) {
						echo "Error writing the file <b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
						$split_ok = false;
						break;
					}
				}
				$split_rest = $partSize - ($split_write_times * $split_buffer_size);
				if ($split_ok && $split_rest > 0) {
					$split_buffer = fread ( $split_source, $split_rest );
					if (fwrite ( $split_dest, $split_buffer ) === false) {
						echo "Error writing the file <b>" . $dest_name . '.' . sprintf ( "%03d", $j ) . "</b> !<br /><br />";
						$split_ok = false;
					}
				}
				fclose ( $split_dest );
				if ($split_ok) {
					$time = filemtime ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) );
					while ( isset ( $list [$time] ) ) {
						$time ++;
					}
					$list [$time] = array ("name" => $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ), "size" => bytesToKbOrMbOrGb ( filesize ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) ) ), "date" => $time );
				}
			}
			fclose ( $split_source );
			if ($split_ok) {
				if ($_POST["del_ok"] && !$options['disable_deleting']) {
					if (@unlink ( $file ["name"] )) {
						unset ( $list [$_POST ["files"] [$i]] );
						echo "Source file deleted.<br /><br />";
					} else {
						echo "Source file is<b>not deleted!</b><br /><br />";
					}
				}
			}
			if (! updateListInFile ( $list )) {
				echo "Couldn't update file list. Problem writing to file!<br /><br />";
			}
		}
	}
}
?>
Back to Top