Jump to content

Stops working


TheUnknown

Recommended Posts

this import files into a folder and then puts the info on the files into a database

 

Sometimes i can import 1000 files without it stopping and other times ill be lucky to import 3 files.

 

it will start the import then the page will say done

 

Do you see anything in this code that may cause this issue?

many thanks for looking over it!!!!!

 

 

<?php
dbconn(false);
loggedinorreturn();
adminonly();
stdhead();
ob_start();

ini_set("upload_max_filesize",$max_torrent_size);
$MAX_FILE_SIZE = $max_torrent_size;

// DO FILE COUNT

$dir = "import";
$dh  = opendir($dir);

while (false !== ($filename = readdir($dh)))
  $files[] = $filename;

// END FILE COUNT


// BDEC FUNCTION
function dict_get($d, $k, $t)
{
if ($d["type"] != "dictionary") {
  $message = "This isn't a dictionary.";
  }
  $dd = $d["value"];
  if (!isset($dd[$k])) {
  return;
  }
  $v = $dd[$k];
  if ($v["type"] != $t) {
  $message = "invalid dictionary entry type";
  }
  return $v["value"];
}
// END BDEC FUNCTION




// BEGIN FILE LISTING
begin_frame("import", center);

// END FILE LISTING

if ($action == 'import') {
set_time_limit(1200);
  // CONFIGURE DETAILS
  // $catid = 1;
  $descr = 'no details';
  //END CONFIGURE DETAILS
  echo "<b>Cat ID</b>: ". $catid."<BR>";
  $x=0;
  $num = count($files);
  while ($x < $num) {
  $message = null;
  $pattern = '.torrent';
  if (strpos($files[$x], $pattern)) {
	  $tmpname = './import/' . $files[$x];
	  $fname = $files[$x];
	  echo '<b>tmpname</b>: ' . $tmpname .'<br><b>fname</b>: '.$fname.'<br>';

	  if (!preg_match('/^(.+)\.torrent$/si', $fname, $matches)) {
		  echo 'Invalid filename (not a .torrent).';
	  }
	  $shortfname = $torrent = $matches[1];
	  echo '<b>torrent shortname</b>: '.$shortfname.'<br>';
	  $dict = bdec_file("./import/".$files[$x], $max_torrent_size);

	  list($ann, $info) = dict_check($dict, "announce(string):info");
	  echo "<b>announce url</b>: ". $ann . "<br>";

	  $url_ann = array();
	  $a=0;
	  $announcelist = dict_get($dict, "announce-list","list");

	  if (isset($announcelist) && count($announcelist)) {
		  foreach($announcelist as $fn) {
			  $hi = $fn["value"];

			  for ($i=0; $i < count($hi); $i++ ) {
				  $url_ann[] = $hi[$i]["value"];

			  }
		  }
		  for ($j=0; $j<count($announce_urls); $j++) {
			  for ($k=0; $k<count($url_ann); $k++) {
				  // echo "announce url: ".$url_ann[$k] . "<br>";
				  // debug purposes only
				  if ($announce_urls[$j]==$url_ann[$k]) {
					  $a+=1;
				  }
			  }
		  }
		  if ($a<1) {
			  $external = 'yes';
		  }
	  } else {
		  if (!in_array($ann, $announce_urls, 1)) {
			  $external='yes';
		  }
	  }

	  list($dname, $plen, $pieces) = dict_check($info, "name(string):piece length(integer):pieces(string)");
	  echo '<b>dname</b>: ' .$dname.'<br>';
	  if (strlen($pieces) % 20 != 0) {
		  $message = "Invalid pieces!";
	  }

	  $filelist = array();
	  $totallen = dict_get($info, "length", "integer");
	  if (isset($totallen)) {
		  $filelist[] = array($dname, $totallen);
		  $type = "single";
	  } else {
		  $flist = dict_get($info, "files", "list");
		  if (!isset($flist)) {
			  $message = "Missing both length and files";
		  }
		  if (!count($flist)) {
			  $message = "No files";
		  }
		  $totallen = 0;
		  foreach($flist as $fn) {
			  list($ll, $ff) = dict_check($fn, "length(integer):path(list)");
			  $totallen += $ll;
			  $ffa = array();
			  foreach($ff as $ffe) {
				  if ($ffe["type"] != "string") {
					  $message = "Filename error";
				  }
				  $ffa[] = $ffe["value"];
			  }
			  if (!count($ffa)) {
				  $message = "Filename error";
			  }
			  $ffe = implode("/", $ffa);
			  $filelist[] = array($ffe, $ll);
		  }
		  $type = "multi";
	  }

	  //  $infohash = pack("H*", sha1($info["string"]));
	  $tmphex = sha1($info["string"]);
	  $hexhash = strtolower($tmphex);
	  echo '<b>hash</b>: ' .$hexhash. '<br>';
	  if (strlen($hexhash) != 40) {
		  $message = "Error: Info hash must be exactly 40 hex bytes. Contact an admin to fix this";
	  }

	  // Replace punctuation characters with spaces
	  echo 'message: '.$message.'<br>';
	  if (!$message) {
		  $torrent = str_replace(".torrent","",$torrent);
		  $torrent = str_replace("_", " ", $torrent);
		  $torrent = str_replace(".", " ", $torrent);
		  $torrent = str_replace("+", " ", $torrent);
		  $torrent = str_replace("-", " ", $torrent);
		  $torrent = htmlspecialchars(urldecode($torrent));
		  
		  echo '<b>torrent name</b>: ' . $torrent . '<br>';
		  $nfo = sqlesc(str_replace("\x0d\x0d\x0a", "\x0d\x0a", @file_get_contents($nfofilename)));
		  $ret = mysql_query("INSERT INTO torrents (search_text, filename, owner, visible, info_hash, name, size, numfiles, type, descr, ori_descr, category, save_as, added, last_action, nfo,announce,external) VALUES (" .
		  implode(",", array_map("sqlesc", array(searchfield("$shortfname $dname $torrent"), $fname, ".$CURUSER[id].", "no", $hexhash, $torrent, $totallen, count($filelist), $type, $descr, $descr, 0 + $catid, $dname))) .
		  ", '" . get_date_time() . "', '" . get_date_time() . "', $nfo,'".$ann."','".$external."')");
		  if (!$ret) {
			  $message = "Mysql Error: ".mysql_error();
			  if (mysql_errno() == 1062) {
				  $message = "Torrent already uploaded!";
			  }
		  }
	  echo 'message: '.$message.'<br>';

		  $id = mysql_insert_id();
	  echo '<b>id</b>: '.$id.'<br>';
		  copy($tmpname, "$torrent_dir/$id.torrent");
		  $dict = bdec_file("$torrent_dir/$id.torrent", filesize("$torrent_dir/$id.torrent"));
			@unlink($tmpname);

		  $dict["value"]["comment"]["type"] = "string";
		  $dict["value"]["comment"]["value"] = "[ Visit ". $SITEURL." ] ".$dict["value"]["comment"]["value"];

		  $fn = benc($dict);

		  $save = @fopen("$torrent_dir/$id.torrent", 'w+');
		  @flock($save, LOCK_EX);
		  @ftruncate($save, 0);
		  if (@fwrite($save, $fn)) {
			  @flock($save, LOCK_UN);
			  @fclose($save);
		  }
		  @mysql_query("DELETE FROM files WHERE torrent = $id");
		  foreach($filelist as $file) {
			  @mysql_query("INSERT INTO files (torrent, filename, size) VALUES ($id, ".sqlesc($file[0]).",".$file[1].")");
		  }

		  if ($external=='yes') { 
				require_once("quickscrape-func.php");
				quickscrape($id);

				  $status = "online";

			  $ret = mysql_query("INSERT INTO announce (`announce_url`, `force`, `status`) VALUES ('".$ann."','no','".$status."')");
			  if (!$ret) {
				  // we can't insert, because it already exists ! then we update !
				  // echo "<br>2)Mysql Error: ".mysql_errno(). "-".mysql_error();
				  if (mysql_errno() == 1062) {
					  mysql_query("UPDATE announce SET status='".$status."' WHERE announce_url=".$ann." AND force='no' ");
				  } else {

					  $id = mysql_insert_id();
					  $msg.="This is the first time a torrent is coming from this tracker. Please allow some time for the site to scrape this tracker</div><br>";
				  }

			  }


			  if ($status=="online") {
				  echo "<br><center><u>Tracker Status:</u> <font color=green>ONLINE</font></center><br>";
			  } else {
				  echo "<br><center><u>Tracker Status:</u> <font color=red>OFFLINE</font></center><br>";
			  }
		  }
		  ob_flush();flush();
		  $user = $CURUSER["username"];
		  write_log("Torrent $id ($torrent) was uploaded by ".$user);

if ($IRCANNOUNCE) {
$msg_bt = chr(3)."9".chr(2)." $SITENAME ".chr(2).chr(3)."10- ".chr(3)."13New Torrent: ".chr(3)."10(".chr(3)."4 $torrent ".chr(3)."10) ".chr(3)."13Size: ".chr(3)."10( ".chr(3)."9".mksize($totallen)."".chr(3)."10 ) ".chr(3)."13Uploader: (".chr(3)."4 $user ".chr(3)."10) ".chr(3)."13Link: ".chr(3)."10(".chr(3)."15 $SITEURL/torrents-details.php?id=$id&hit=1".chr(3)."10 )\r\n";
   $fs = fsockopen("$ANNOUNCEIP", "$ANNOUNCEPORT", $errno, $errstr);
  if($fs) {
	 fwrite($fs, $msg_bt);
	 fclose($fs);
  }
}
				   
	  }
  }
  $x++;
  }

} else {
  $i=0;
  $num = count($files);
  while ($i < $num) {
  $pattern = '.torrent';
  if (strpos($files[$i], $pattern)) {
	  print htmlspecialchars(urldecode($files[$i]))."<br />";
  }
  $i++;
  }
}
?>
<br>
<form action="import.php" method="post">
<input type="hidden" name="action" value="import">
<select name="catid">
<option value="0" selected>-- NONE SELECTED --</option>
<?
$cats = genrelist();

$catdropdown = "";
foreach ($cats as $cat) {
$catdropdown .= "<option value=\"".$cat["id"]."\"";
$catdropdown .= ">" . htmlspecialchars($cat["name"]) . "</option>\n";
}
echo $catdropdown;
?>
</select>
<input type="submit" value="submit">
</form>

<?
end_frame();
stdfoot();
ob_end_flush();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.