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