Upload 2 images with torrent

Started by rickandmary, July 22, 2012, 05:28:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rickandmary

Files to be edited:
upload.php
takeupload.php
edit.php
takeedit.php
delete.php
details.php

Files to be added:
thumbnail.php
viewimage.php

run SQL
ALTER TABLE `torrents` ADD `image1` TEXT NOT NULL AFTER `ori_descr` ,
ADD `image2` TEXT NOT NULL AFTER `image1`;


in upload.php find
tr("Torrent file", "<input type=file name=file size=80>\n", 1);

replace with
tr("Torrent file", "<input type=file name=tfile size=80>\n", 1);

find
tr("Torrent name", "<input type=\"text\" name=\"name\" size=\"80\" />
(Taken from filename if not specified. Please use descriptive names.)\n", 1);


add after
tr("Images", "Max File Size: 500kb
Accepted Formats: .gif .jpg
Image 1:&nbsp&nbsp<input type=file name=image0 size=80>
Image 2:&nbsp&nbsp<input type=file name=image1 size=80>\n", 1);


find
<tr><td align="center" colspan="2"><input type="submit" class=btn value="Do it!" /></td></tr>

replace with
<tr><td align="center" colspan="2"><input type="submit" class=btn value="Do it!" />
Click once only! - Uploading an image will require more loading time</td></tr>


in takeupload.php find
if (!isset($_FILES["file"]))

replace with
if (!isset($_FILES["tfile"]))

find
$f = $_FILES["file"];

replace with
$f = $_FILES["tfile"];

find
// Replace punctuation characters with spaces

add above
//////////////////////////////////////////////
//////////////Take Image Uploads//////////////

$maxfilesize = 512000; // 500kb

$allowed_types = array(
"image/gif" => "gif",
"image/pjpeg" => "jpg",
"image/jpeg" => "jpg",
"image/jpg" => "jpg"
// Add more types here if you like
);

for ($x=0; $x < 2; $x++) {

if (!($_FILES[image.$x]['name'] == "")) {
$y = $x + 1;

// Is valid filetype?
if (!array_key_exists($_FILES[image.$x]['type'], $allowed_types))
bark("Invalid file type! Image $y");

// Is within allowed filesize?
if ($_FILES[image.$x]['size'] > $maxfilesize)
bark("Invalid file size! Image $y - Must be less than 500kb");

// Where to upload?
// Update for your own server. Make sure the folder has chmod write permissions. Remember this director
$uploaddir = "/home/user/public_html/trackerdir/torrents/images/";

// What is the temporary file name?
$ifile = $_FILES[image.$x]['tmp_name'];

// Calculate what the next torrent id will be
$ret = mysql_query("SHOW TABLE STATUS LIKE 'torrents'");
$row = mysql_fetch_array($ret);
$next_id = $row['Auto_increment'];

// By what filename should the tracker associate the image with?
$ifilename = $next_id . $x . substr($_FILES[image.$x]['name'], strlen($_FILES[image.$x]['name'])-4, 4);

// Upload the file
$copy = copy($ifile, $uploaddir.$ifilename);

if (!$copy)
bark("Error occured uploading image! - Image $y");

$inames[] = $ifilename;

}

}

//////////////////////////////////////////////


find
$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) VALUES (" . implode(",", array_map("sqlesc", array(searchfield("$shortfname $dname $torrent"), $fname, $CURUSER["id"], "no", $infohash, $torrent, $totallen, count($filelist), $type, $descr, $descr, 0 + $_POST["type"], $dname))) . ", '" . get_date_time() . "', '" . get_date_time() . "', $nfo)");

replace with
$ret = mysql_query("INSERT INTO torrents (search_text, filename, owner, visible, info_hash, name, size, numfiles, type, descr, ori_descr, image1, image2, category, save_as, added, last_action, nfo) VALUES (" . implode(",", array_map("sqlesc", array(searchfield("$shortfname $dname $torrent"), $fname, $CURUSER["id"], "no", $infohash, $torrent, $totallen, count($filelist), $type, $descr, $descr, $inames[0], $inames[1], 0 + $_POST["type"], $dname))) . ", '" . get_date_time() . "', '" . get_date_time() . "', $nfo)");

in edit.php find
tr("Torrent name", "<input type=\"text\" name=\"name\" value=\"" . htmlspecialchars($row["name"]) . "\" size=\"80\" />", 1);

add after
tr("Images", "<input type=radio name=img1action value='keep' checked>Keep Image 1&nbsp&nbsp"."<input type=radio name=img1action value='delete'>Delete Image 1&nbsp&nbsp"."<input type=radio name=img1action value='update'>Update Image 1
Image 1:&nbsp&nbsp<input type=file name=image0 size=80>

<input type=radio name=img2action value='keep' checked>Keep Image 2&nbsp&nbsp"."<input type=radio name=img2action value='delete'>Delete Image 2&nbsp&nbsp"."<input type=radio name=img2action value='update'>Update Image 2
Image 2:&nbsp&nbsp<input type=file name=image1 size=80>", 1);


in takeedit.php find
function bark($msg) {
    genbark($msg, "Edit failed!");
}


add after
////////////////////////////////////////////////
function uploadimage($x, $imgname, $tid) {

$maxfilesize = 512000; // 500kb

$allowed_types = array(
"image/gif" => "gif",
"image/pjpeg" => "jpg",
"image/jpeg" => "jpg",
"image/jpg" => "jpg"
// Add more types here if you like
);

if (!($_FILES[image.$x]['name'] == "")) {

if ($imgname != "") {
// Make sure is same as in takeedit.php (except for the $imgname bit)
$img = "/home/user/public_html/trackerdir/torrents/images/$imgname";
$del = unlink($img);
}

$y = $x + 1;

// Is valid filetype?
if (!array_key_exists($_FILES[image.$x]['type'], $allowed_types))
bark("Invalid file type! Image $y");

// Is within allowed filesize?
if ($_FILES[image.$x]['size'] > $maxfilesize)
bark("Invalid file size! Image $y - Must be less than 500kb");

// Where to upload?
// Make sure is same as on takeupload.php
$uploaddir = "/home/user/public_html/trackerdir/torrents/images/";

// What is the temporary file name?
$ifile = $_FILES[image.$x]['tmp_name'];

// By what filename should the tracker associate the image with?
$ifilename = $tid . $x . substr($_FILES[image.$x]['name'], strlen($_FILES[image.$x]['name'])-4, 4);

// Upload the file
$copy = copy($ifile, $uploaddir.$ifilename);

if (!$copy)
bark("Error occured uploading image! - Image $y");

return $ifilename;

}

}
////////////////////////////////////////////////


find
$res = mysql_query("SELECT owner, filename, save_as FROM torrents WHERE id = $id")

replace with
$res = mysql_query("SELECT owner, filename, save_as, image1, image2 FROM torrents WHERE id = $id");

find
$dname = $row["save_as"];

add after
$img1action = $_POST['img1action'];
if ($img1action == "update")
$updateset[] = "image1 = " .sqlesc(uploadimage(0, $row[image1], $id));
if ($img1action == "delete") {
if ($row[image1]) {
$del = unlink("/home/user/public_html/trackerdir/torrents/images/$row[image1]");
$updateset[] = "image1 = ''";
}
}

$img2action = $_POST['img2action'];
if ($img2action == "update")
$updateset[] = "image2 = " .sqlesc(uploadimage(1, $row[image2], $id));
if ($img2action == "delete") {
if ($row[image2]) {
$del = unlink("/home/user/public_html/trackerdir/torrents/images/$row[image2]");
$updateset[] = "image2 = ''";
}
}


in delete.php find
$res = mysql_query("SELECT name,owner,seeders FROM torrents WHERE id = $id");

replace with
$res = mysql_query("SELECT name,owner,seeders,image1,image2 FROM torrents WHERE id = $id");

find
deletetorrent($id)

add after
if ($row["image1"]) {
$img1 = "/home/vixbit/public_html/trader/torrents/images/$row[image1]";
$del = unlink($img1);
}
if ($row["image2"]) {
$img2 = "/home/vixbit/public_html/trader/torrents/images/$row[image2]";
$del = unlink($img2);
}


in details.php find
$res = mysql_query("SELECT torrents.seeders, torrents.banned, torrents.leechers, torrents.info_hash, torrents.filename, LENGTH(torrents.nfo) AS nfosz, UNIX_TIMESTAMP() - UNIX_TIMESTAMP(torrents.last_action) AS lastseed, torrents.numratings, torrents.name, IF(torrents.numratings < $minvotes, NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating, torrents.owner, torrents.save_as, torrents.descr, torrents.visible, torrents.size, torrents.added, torrents.views, torrents.hits, torrents.times_completed, torrents.id, torrents.type, torrents.numfiles, torrents.nfo, torrents.category, categories.name AS cat_name, users.username FROM torrents LEFT JOIN categories ON torrents.category = categories.id LEFT JOIN users ON torrents.owner = users.id WHERE torrents.id = $id") or sqlerr();

replace with
$res = mysql_query("SELECT torrents.seeders, torrents.banned, torrents.leechers, torrents.info_hash, torrents.filename, LENGTH(torrents.nfo) AS nfosz, UNIX_TIMESTAMP() - UNIX_TIMESTAMP(torrents.last_action) AS lastseed, torrents.numratings, torrents.name, IF(torrents.numratings < $minvotes, NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating, torrents.owner, torrents.save_as, torrents.descr, torrents.visible, torrents.size, torrents.added, torrents.views, torrents.hits, torrents.times_completed, torrents.id, torrents.type, torrents.numfiles, torrents.nfo, torrents.image1, torrents.image2, torrents.category, categories.name AS cat_name, users.username FROM torrents LEFT JOIN categories ON torrents.category = categories.id LEFT JOIN users ON torrents.owner = users.id WHERE torrents.id = $id") or sqlerr()

find
if (!empty($row["descr"]))
tr("Description", str_replace(array("\n", "  "), array("
\n", " "), format_urls(htmlspecialchars($row["descr"]))), 1);


add after
if ($row["image1"] != "" OR $row["image2"] != "") {
     if ($row["image1"] != "")
    $img1 = "[img]thumbnail.php?$row[image1][/img]";
     if ($row["image2"] != "")
    $img2 = "[img]thumbnail.php?$row[image2][/img]";
     tr("Images", $img1 . "&nbsp&nbsp" . $img2, 1);
}


save and upload to root as thumbnail.php
<?php
# Constants
define(IMAGE_BASE'/home/vixbit/public_html/trader/torrents/images/');
define(MAX_WIDTH150);
define(MAX_HEIGHT150);

# Get image location
$image_file str_replace('..'''$_SERVER['QUERY_STRING']);
$image_path IMAGE_BASE "/$image_file";

# Load image
$img null;
$ext strtolower(end(explode('.'$image_path)));
if (
$ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if (
$ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefromgif($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

# Get image size and scale ratio
$width imagesx($img);
$height imagesy($img);
$scale min(MAX_WIDTH/$widthMAX_HEIGHT/$height);

# If the image is larger than the max shrink it
if ($scale 1) {
$new_width floor($scale*$width);
$new_height floor($scale*$height);

# Create a new temporary image
$tmp_img imagecreatetruecolor($new_width$new_height);

# Copy and resize old image into new image
imagecopyresized($tmp_img$img0000,
$new_width$new_height$width$height);
imagedestroy($img);
$img $tmp_img;
}
}

# Create error image if necessary
if (!$img) {
$img imagecreate(MAX_WIDTHMAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>


save and upload to root as viewimage.php
<?
require "include/bittorrent.php";
dbconn(false);

loggedinorreturn();

$pic = $_GET["pic"];

stdhead("View Image");
print("<h1>$pic</h1>\n");
print("<p align=center>[img]torrents/images/$pic[/img]</p>\n");
?>

<h3 align=center>Back</h3>

<?
stdfoot();
?>


credit to author