PageRenderTime 89ms CodeModel.GetById 27ms app.highlight 38ms RepoModel.GetById 17ms app.codeStats 0ms

/classes/other.php

https://github.com/gartz/RapidLeetch
PHP | 527 lines | 462 code | 39 blank | 26 comment | 142 complexity | 39452eff03097f4faed2e31b82fb0822 MD5 | raw file
  1<?php
  2if (!defined('RAPIDLEECH')) {
  3	require('../deny.php');
  4	exit;
  5}
  6
  7function create_hosts_file($host_file = "hosts.php") {
  8	$fp = opendir ( HOST_DIR . 'download/' );
  9	while ( ($file = readdir ( $fp )) !== false ) {
 10		if (substr ( $file, - 4 ) == ".inc") {
 11			require_once (HOST_DIR . 'download/' . $file);
 12		}
 13	}
 14	if (! is_array ( $host )) {
 15		print lang(127);
 16	} else {
 17		$fs = fopen ( HOST_DIR . 'download/' . $host_file, "wb" );
 18		if (! $fs) {
 19			print lang(128);
 20		} else {
 21			fwrite ( $fs, "<?php\r\n\$host = array(\r\n" );
 22			$i = 0;
 23			foreach ( $host as $site => $file ) {
 24				if ($i != (count ( $host ) - 1)) {
 25					fwrite ( $fs, "'" . $site . "' => '" . $file . "',\r\n" );
 26				} else {
 27					fwrite ( $fs, "'" . $site . "' => '" . $file . "');\r\n?>" );
 28				}
 29				$i ++;
 30			}
 31			closedir ( $fp );
 32			fclose ( $fs );
 33		}
 34	}
 35}
 36
 37function login_check() {
 38	global $options;
 39	if ($options['login']) {
 40		function logged_user($ul) {
 41			foreach ($ul as $user => $pass) {
 42				if ($_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $pass) { return true; }
 43			}
 44			return false;
 45		}
 46		if ($options['login_cgi']) {
 47			list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = @explode(':', base64_decode(substr((isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : $_SERVER['REDIRECT_HTTP_AUTHORIZATION']), 6)), 2);      
 48		}
 49		if (empty($_SERVER['PHP_AUTH_USER']) || !logged_user($options['users'])) {
 50			header ( 'WWW-Authenticate: Basic realm="RAPIDLEECH PLUGMOD"' );
 51			header ( "HTTP/1.0 401 Unauthorized" );
 52			include('deny.php');
 53			exit;
 54		}
 55	}
 56}
 57
 58function is_present($lpage, $mystr, $strerror = "", $head = 0) {
 59	$strerror = $strerror ? $strerror : $mystr;
 60	if (stristr ( $lpage, $mystr )) {
 61		html_error ( $strerror, $head );
 62	}
 63}
 64
 65function is_notpresent($lpage, $mystr, $strerror, $head = 0) {
 66	if (! stristr ( $lpage, $mystr )) {
 67		html_error ( $strerror, $head );
 68	}
 69}
 70
 71function insert_location($newlocation) {
 72	if (isset ( $_GET ["GO"] ) && $_GET ["GO"] == "GO") {
 73		list ( $location, $list ) = explode ( "?", $newlocation );
 74		$list = explode ( "&", $list );
 75		foreach ( $list as $l ) {
 76			list ( $name, $value ) = explode ( "=", $l );
 77			$_GET [$name] = $value;
 78		}
 79	} else {
 80		global $nn;
 81		list ( $location, $list ) = explode ( "?", $newlocation );
 82		$list = explode ( "&", $list );
 83		print '<form action="'.$location.'" method="post">' . $nn;
 84		foreach ( $list as $l ) {
 85			list ( $name, $value ) = explode ( "=", $l );
 86			print '<input type="hidden" name="'.$name.'" value="'.$value.'" />' . $nn;
 87		}
 88		echo ('<script type="text/javascript">void(document.forms[0].submit());</script>');
 89		echo ('</form>');
 90		echo ('</body>');
 91		echo ('</html>');
 92		flush ();
 93	}
 94}
 95
 96function pause_download() {
 97	global $pathWithName, $PHP_SELF, $_GET, $nn, $bytesReceived, $fs, $fp;
 98	$status = connection_status ();
 99	if (($status == 2 || $status == 3) && $pathWithName && $bytesReceived > - 1) {
100		flock ( $fs, LOCK_UN );
101		fclose ( $fs );
102		fclose ( $fp );
103	}
104}
105
106function cut_str($str, $left, $right) {
107	$str = substr ( stristr ( $str, $left ), strlen ( $left ) );
108	$leftLen = strlen ( stristr ( $str, $right ) );
109	$leftLen = $leftLen ? - ($leftLen) : strlen ( $str );
110	$str = substr ( $str, 0, $leftLen );
111	return $str;
112}
113
114// tweaked cutstr with pluresearch functionality
115function cutter($str, $left, $right,$cont=1)
116	{
117    for($iii=1;$iii<=$cont;$iii++){
118	$str = substr ( stristr ( $str, $left ), strlen ( $left ) );
119	}
120    $leftLen = strlen ( stristr ( $str, $right ) );
121    $leftLen = $leftLen ? - ($leftLen) : strlen ( $str );
122    $str = substr ( $str, 0, $leftLen );
123    return $str;
124}
125
126function write_file($file_name, $data, $trunk = 1) {
127	if ($trunk == 1) {
128		$mode = "wb";
129	} elseif ($trunk == 0) {
130		$mode = "ab";
131	}
132	$fp = fopen ( $file_name, $mode );
133	if (! $fp) {
134		return FALSE;
135	} else {
136		if (! flock ( $fp, LOCK_EX )) {
137			return FALSE;
138		} else {
139			if (! fwrite ( $fp, $data )) {
140				return FALSE;
141			} else {
142				if (! flock ( $fp, LOCK_UN )) {
143					return FALSE;
144				} else {
145					if (! fclose ( $fp )) {
146						return FALSE;
147					}
148				}
149			}
150		}
151	}
152	return TRUE;
153}
154
155function read_file($file_name, $count = -1) {
156	if ($count == - 1) {
157		$count = filesize ( $file_name );
158	}
159	$fp = fopen ( $file_name, "rb" );
160	flock ( $fp, LOCK_SH );
161	$ret = fread ( $fp, $count );
162	flock ( $fp, LOCK_UN );
163	fclose ( $fp );
164	return $ret;
165}
166
167function pre($var) {
168	echo "<pre>";
169	print_r ( $var );
170	echo "</pre>";
171}
172
173function getmicrotime() {
174	list ( $usec, $sec ) = explode ( " ", microtime () );
175	return (( float ) $usec + ( float ) $sec);
176}
177
178function html_error($msg, $head = 1) {
179	global $PHP_SELF, $options;
180	//if ($head == 1)
181	if (! headers_sent ()) {
182		include(TEMPLATE_DIR.'header.php');
183	}
184	echo ('<div align="center">');
185	echo ('<span class="htmlerror"><b>' . $msg . '</b></span><br /><br />');
186	if ($options['new_window']) { echo '<a href="javascript:window.close();">'.lang(378).'</a>'; }
187	else { echo '<a href="'.$PHP_SELF.'">'.lang(13).'</a>'; }
188
189	echo ('</div>');
190	include(TEMPLATE_DIR.'footer.php');
191	exit ();
192}
193
194function sec2time($time) {
195	$hour = round ( $time / 3600, 2 );
196	if ($hour >= 1) {
197		$hour = floor ( $hour );
198		$time -= $hour * 3600;
199	}
200	$min = round ( $time / 60, 2 );
201	if ($min >= 1) {
202		$min = floor ( $min );
203		$time -= $min * 60;
204	}
205	$sec = $time;
206	$hour = ($hour > 1) ? $hour . " ".lang(129)." " : ($hour == 1) ? $hour . " ".lang(130)." " : "";
207	$min = ($min > 1) ? $min . " ".lang(131)." " : ($min == 1) ? $min . " ".lang(132)." " : "";
208	$sec = ($sec > 1) ? $sec . " ".lang(133) : ($sec == 1 || $sec == 0) ? $sec . " ".lang(134) : "";
209	return $hour . $min . $sec;
210}
211
212// Updated function to be able to format up to Yotabytes!
213function bytesToKbOrMbOrGb($bytes) {
214	if (is_numeric ( $bytes )) {
215		$s = array ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
216		$e = floor ( log ( $bytes ) / log ( 1024 ) );
217		
218		return sprintf ( '%.2f ' . $s [$e], @($bytes / pow ( 1024, floor ( $e ) )) );
219	} else {
220		$size = "Unknown";
221	}
222	return $size;
223}
224
225function updateListInFile($list) {
226	if (count ( $list ) > 0) {
227		foreach ( $list as $key => $value ) {
228			$list [$key] = serialize ( $value );
229		}
230		if (! @write_file ( CONFIG_DIR . "files.lst", implode ( "\r\n", $list ) . "\r\n" ) && count ( $list ) > 0) {
231			return FALSE;
232		} else {
233			return TRUE;
234		}
235	} elseif (@file_exists ( CONFIG_DIR . "files.lst" )) {
236		// Truncate files.lst instead of removing it since we don't have full
237		// read/write permission on the configs folder
238		$fh = fopen(CONFIG_DIR.'files.lst','w');
239		fclose($fh);
240		return true;
241	}
242}
243
244function _cmp_list_enums($a, $b) {
245	return strcmp ( $a ["name"], $b ["name"] );
246}
247
248function file_data_size_time($file) {
249	global $options;
250	$size = $time = false;
251	if (is_file($file)) {
252		$size = @filesize($file);
253		$time = @filemtime($file);
254	}
255	if ($size === false && $options['2gb_fix'] && file_exists($file) && !is_dir($file) && !is_link($file)) {
256		if (substr(PHP_OS, 0, 3) !== "WIN") {
257			@exec('stat'.(stristr(@php_uname('s'), 'bsd') !== false ? '-f %m ' : ' -c %Y ').escapeshellarg($file), $time, $tmp);
258			if ($tmp == 0) { $time = trim(implode($time)); }
259			@exec('stat'.(stristr(@php_uname('s'), 'bsd') !== false ? '-f %z ' : ' -c %s ').escapeshellarg($file), $size, $tmp);
260			if ($tmp == 0) { $size = trim(implode($size)); }
261		}
262	}
263	if ($size === false || $time === false) { return false; }
264	return array($size, $time);
265}
266
267function _create_list() {
268	global $list, $_COOKIE, $options;
269	$glist = array ();
270	if (($options['show_all'] === true) & ($_COOKIE ["showAll"] == 1)) {
271		$dir = dir ( DOWNLOAD_DIR );
272		while ( false !== ($file = $dir->read ()) ) {
273			if (($tmp = file_data_size_time(DOWNLOAD_DIR.$file)) === false) { continue; }; list($size, $time) = $tmp;
274			if ($file != "." && $file != ".." && (! is_array ( $options['forbidden_filetypes'] ) || ! in_array ( strtolower ( strrchr ( $file, "." ) ), $options['forbidden_filetypes'] ))) {
275				$file = DOWNLOAD_DIR . $file;
276				while (isset($glist[$time])) { $time ++; }
277				$glist [$time] = array ("name" => realpath($file), "size" => bytesToKbOrMbOrGb($size), "date" => $time );
278			}
279		}
280		$dir->close ();
281		@uasort ( $glist, "_cmp_list_enums" );
282	} else {
283		if (@file_exists ( CONFIG_DIR . "files.lst" )) {
284			$glist = file ( CONFIG_DIR . "files.lst" );
285			foreach ( $glist as $key => $record ) {
286				foreach ( unserialize ( $record ) as $field => $value ) {
287					$listReformat [$key] [$field] = $value;
288					if ($field == "date")
289						$date = $value;
290				}
291				$glist [$date] = $listReformat [$key];
292				unset ( $glist [$key], $glistReformat [$key] );
293			}
294		}
295	}
296	$list = $glist;
297}
298
299function checkmail($mail) {
300	if (strlen ( $mail ) == 0) {
301		return false;
302	}
303	if (! preg_match ( "/^[a-z0-9_\.-]{1,20}@(([a-z0-9-]+\.)+(com|net|org|mil|" . "edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-" . "9]{1,3}\.[0-9]{1,3})$/is", $mail )) {
304		return false;
305	}
306	return true;
307}
308/* Fixed Shell exploit by: icedog */
309function fixfilename($fname, $fpach = '') {
310	$f_name = basename ( $fname );
311	$f_dir = dirname ( eregi_replace ( "\.\./", "", $fname ) );
312	$f_dir = ($f_dir == '.') ? '' : $f_dir;
313	$f_dir = eregi_replace ( "\.\./", "", $f_dir );
314	$fpach = eregi_replace ( "\.\./", "", $fpach );
315	$f_name = eregi_replace ( "\.(php|hta|pl|cgi|sph)", ".xxx", $f_name );
316	$ret = ($fpach) ? $fpach . DIRECTORY_SEPARATOR . $f_name : ($f_dir ? $f_dir . DIRECTORY_SEPARATOR : '') . $f_name;
317	return $ret;
318}
319function getfilesize($f) {
320	global $is_windows;
321	$stat = stat ( $f );
322	
323	if ($is_windows)
324		return sprintf ( "%u", $stat [7] );
325	if (($stat [11] * $stat [12]) < 4 * 1024 * 1024 * 1024)
326		return sprintf ( "%u", $stat [7] );
327	
328	global $max_4gb;
329	if ($max_4gb === false) {
330		$tmp_ = trim ( @shell_exec ( " ls -Ll " . @escapeshellarg ( $f ) ) );
331		while ( strstr ( $tmp_, '  ' ) ) {
332			$tmp_ = @str_replace ( '  ', ' ', $tmp_ );
333		}
334		$r = @explode ( ' ', $tmp_ );
335		$size_ = $r [4];
336	} else {
337		$size_ = - 1;
338	}
339	
340	return $size_;
341}
342function bytesToKbOrMb($bytes) {
343	$size = ($bytes >= (1024 * 1024 * 1024 * 1024)) ? round ( $bytes / (1024 * 1024 * 1024 * 1024), 2 ) . " TB" : (($bytes >= (1024 * 1024 * 1024)) ? round ( $bytes / (1024 * 1024 * 1024), 2 ) . " GB" : (($bytes >= (1024 * 1024)) ? round ( $bytes / (1024 * 1024), 2 ) . " MB" : round ( $bytes / 1024, 2 ) . " KB"));
344	return $size;
345}
346function defport($urls) {
347	if ($urls ["port"] !== '' && isset ( $urls ["port"] ))
348		return $urls ["port"];
349	
350	switch (strtolower ( $urls ["scheme"] )) {
351		case "http" :
352			return '80';
353		case "https" :
354			return '443';
355		case "ftp" :
356			return '21';
357	}
358}
359function getSize($file) {
360	$size = filesize ( $file );
361	if ($size < 0)
362		if (! (strtoupper ( substr ( PHP_OS, 0, 3 ) ) == 'WIN'))
363			$size = trim ( `stat -c%s $file` );
364		else {
365			$fsobj = new COM ( "Scripting.FileSystemObject" );
366			$f = $fsobj->GetFile ( $file );
367			$size = $file->Size;
368		}
369	return $size;
370}
371function purge_files($delay) {
372	if (file_exists ( CONFIG_DIR . "files.lst" ) && is_numeric ( $delay ) && $delay > 0) {
373		$files_lst = file ( CONFIG_DIR . "files.lst" );
374		$files_new = "";
375		foreach ( $files_lst as $files_line ) {
376			$files_data = unserialize ( trim ( $files_line ) );
377			if (file_exists ( $files_data ["name"] ) && is_file ( $files_data ["name"] )) {
378				if (time () - $files_data ["date"] >= $delay) {
379					@unlink ( $files_data ["name"] );
380				} else {
381					$files_new .= $files_line;
382				}
383			} else {
384			}
385		}
386		file_put_contents ( CONFIG_DIR . "files.lst", $files_new );
387	}
388}
389// PHP4 compatibility
390if (! function_exists ( "file_put_contents" ) && ! defined ( "FILE_APPEND" )) {
391	define ( "FILE_APPEND", 1 );
392	function file_put_contents($n, $d, $flag = false) {
393		$mode = ($flag == FILE_APPEND || strtoupper ( $flag ) == "FILE_APPEND") ? "a" : "w";
394		$f = @fopen ( $n, $mode );
395		if ($f === false) {
396			return 0;
397		} else {
398			if (is_array ( $d )) {
399				$d = implode ( $d );
400			}
401			$bytes_written = fwrite ( $f, $d );
402			fclose ( $f );
403			return $bytes_written;
404		}
405	}
406}
407if (! function_exists ( "file_get_contents" )) {
408	function file_get_contents($filename, $incpath = false) {
409		if (false === $fh = fopen ( $filename, "rb", $incpath )) {
410			trigger_error ( "file_get_contents() failed to open stream: No such file or directory", E_USER_WARNING );
411			return false;
412		}
413		clearstatcache ();
414		if ($fsize = @filesize ( $filename )) {
415			$data = fread ( $fh, $fsize );
416		} else {
417			$data = "";
418			while ( ! feof ( $fh ) ) {
419				$data .= fread ( $fh, 8192 );
420			}
421		}
422		fclose ( $fh );
423		return $data;
424	}
425}
426
427// Using this function instead due to some compatibility problems
428function is__writable($path) {
429	//will work in despite of Windows ACLs bug
430	//NOTE: use a trailing slash for folders!!!
431	//see http://bugs.php.net/bug.php?id=27609
432	//see http://bugs.php.net/bug.php?id=30931
433	
434
435	if ($path {strlen ( $path ) - 1} == '/') // recursively return a temporary file path
436		return is__writable ( $path . uniqid ( mt_rand () ) . '.tmp' );
437	else if (is_dir ( $path ))
438		return is__writable ( $path . '/' . uniqid ( mt_rand () ) . '.tmp' );
439		// check tmp file for read/write capabilities
440	$rm = file_exists ( $path );
441	$f = @fopen ( $path, 'a' );
442	if ($f === false)
443		return false;
444	fclose ( $f );
445	if (! $rm)
446		unlink ( $path );
447	return true;
448}
449
450function link_for_file($filename, $only_link = FALSE, $style = '') {
451	$inCurrDir = strstr(dirname($filename), ROOT_DIR) ? TRUE : FALSE;
452	$PHP_SELF = !$PHP_SELF ? $_SERVER ["PHP_SELF"] : $PHP_SELF;
453	if ($inCurrDir) {
454		$Path = parse_url($PHP_SELF);
455		$Path = substr($Path["path"], 0, strlen($Path["path"]) - strlen(strrchr($Path["path"], "/")));
456		$Path = str_replace('\\', '/', $Path.substr(dirname($filename), strlen(ROOT_DIR)));
457	}
458	elseif (dirname($PHP_SELF.'safe') != '/') {
459		$in_webdir_path = dirname(str_replace('\\', '/', $PHP_SELF.'safe'));
460		$in_webdir_sub = substr_count($in_webdir_path, '/');
461		$in_webdir_root = ROOT_DIR.'/';
462		for ($i=1; $i <= $in_webdir_sub; $i++) {
463			$in_webdir_path = substr($in_webdir_path, 0, strrpos($in_webdir_path, '/'));
464			$in_webdir_root = realpath($in_webdir_root.'/../').'/';
465			$in_webdir = (strpos(str_replace('\\', '/', dirname($filename).'/'), str_replace('\\', '/', $in_webdir_root)) === 0) ? TRUE : FALSE;
466			if ($in_webdir) {
467				$Path = dirname($in_webdir_path.'/'.substr($filename, strlen($in_webdir_root)));
468				break;
469			}
470		}
471	}
472	else {
473		$Path = FALSE;
474		if ($only_link) { return ''; }
475	}
476	$basename = htmlentities(basename($filename));
477	$Path = htmlentities($Path).'/'.rawurlencode(basename($filename));
478	if ($only_link) { return 'http://'.urldecode($_SERVER['HTTP_HOST']).$Path; }
479	elseif ($Path === FALSE) { return '<span>'.$basename.'</span>'; }
480	else { return '<a href="'.$Path.'"'.($style !== '' ? ' '.$style : '').'>'.$basename.'</a>'; }
481}
482
483function lang($id) {
484	global $options;
485	include('languages/'.basename($options['default_language']).'.php');
486	return $lang[$id];
487}
488
489#need to keep premium account cookies safe!
490function encrypt($string)
491{
492	global $secretkey;
493	if (!$secretkey) return html_error('Value for $secretkey is empty, please create a random one (56 chars max) in accounts.php!');
494	require_once 'class.pcrypt.php';
495
496	/*
497	MODE: MODE_ECB or MODE_CBC
498	ALGO: BLOWFISH
499	KEY:  Your secret key :) (max lenght: 56)
500	*/
501	$crypt = new pcrypt(MODE_CBC, "BLOWFISH", "$secretkey");
502
503	// to encrypt
504	$ciphertext = $crypt->encrypt($string);
505
506	return $ciphertext;
507}
508
509function decrypt($string)
510{
511	global $secretkey;
512	if (!$secretkey) return html_error('Value for $secretkey is empty, please create a random one (56 chars max) in accounts.php!');
513	require_once 'class.pcrypt.php';
514	
515	/*
516	MODE: MODE_ECB or MODE_CBC
517	ALGO: BLOWFISH
518	KEY:  Your secret key :) (max lenght: 56)
519	*/
520	$crypt = new pcrypt(MODE_CBC, "BLOWFISH", "$secretkey");
521
522	// to decrypt
523	$decrypted  = $crypt->decrypt($string);
524
525	return $decrypted;
526}
527?>