SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM
information_schema.SCHEMATA S
WHERE schema_name = 'your_database_name'
AND
(DEFAULT_CHARACTER_SET_NAME != 'utf8'
OR
DEFAULT_COLLATION_NAME not like 'utf8%');
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
SELECT CONCAT(
'ALTER TABLE ', table_name, ' CHARACTER SET utf8 COLLATE utf8_general_ci; ',
'ALTER TABLE ', table_name, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ')
FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C
WHERE C.collation_name = T.table_collation
AND T.table_schema = 'your_database_name'
AND
(C.CHARACTER_SET_NAME != 'utf8'
OR
C.COLLATION_NAME not like 'utf8%')
ALTER TABLE rma CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_history CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_history CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_products CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_products CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_report_period CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_report_period CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_reservation CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_reservation CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_supplier_return CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_supplier_return CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_supplier_return_history CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_supplier_return_history CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE rma_supplier_return_product CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_supplier_return_product CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
<script type="text/javascript" language="javascript" src="<?php echo $site_config["SITEURL"];?>/js/rating.js"></script>
<script type="text/javascript" language="javascript" src="<?php echo $site_config["SITEURL"];?>/js/behavior.js"></script>
/*Star Rating css*/
.ratingblock {
display:block;
border-bottom:1px solid #999;
padding-bottom:8px;
margin-bottom:8px;
}
.loading {
height: 30px;
background: url('images/working.gif') 50% 50% no-repeat;
}
.unit-rating { /* the UL */
list-style:none;
margin: 0px;
padding:0px;
height: 30px;
position: relative;
background: url('images/starrating.gif') top left repeat-x;
}
.unit-rating li{
text-indent: -90000px;
padding:0px;
margin:0px;
/**/
float: left;
/* */
}
.unit-rating li a {
outline: none;
display:block;
width:30px;
height: 30px;
text-decoration: none;
text-indent: -9000px;
z-index: 20;
position: absolute;
padding: 0px;
}
.unit-rating li a:hover{
background: url('images/starrating.gif') left center;
z-index: 2;
left: 0px;
}
.unit-rating a.r1-unit{left: 0px;}
.unit-rating a.r1-unit:hover{width:30px;}
.unit-rating a.r2-unit{left:30px;}
.unit-rating a.r2-unit:hover{width: 60px;}
.unit-rating a.r3-unit{left: 60px;}
.unit-rating a.r3-unit:hover{width: 90px;}
.unit-rating a.r4-unit{left: 90px;}
.unit-rating a.r4-unit:hover{width: 120px;}
.unit-rating a.r5-unit{left: 120px;}
.unit-rating a.r5-unit:hover{width: 150px;}
.unit-rating a.r6-unit{left: 150px;}
.unit-rating a.r6-unit:hover{width: 180px;}
.unit-rating a.r7-unit{left: 180px;}
.unit-rating a.r7-unit:hover{width: 210px;}
.unit-rating a.r8-unit{left: 210px;}
.unit-rating a.r8-unit:hover{width: 240px;}
.unit-rating a.r9-unit{left: 240px;}
.unit-rating a.r9-unit:hover{width: 270px;}
.unit-rating a.r10-unit{left: 270px;}
.unit-rating a.r10-unit:hover{width: 300px;}
.unit-rating li.current-rating {
background: url('images/starrating.gif') left bottom;
position: absolute;
height: 30px;
display: block;
text-indent: -9000px;
z-index: 1;
}
.voted {color:#999;}
.thanks {color:#36AA3D;}
.static {color:#5D3126;}
}
//take rating
if ($_GET["takerating"] == 'yes'){
$rating = (int)$_POST['rating'];
if ($rating <= 0 || $rating > 5)
show_error_msg(T_("RATING_ERROR"), T_("INVAILD_RATING"), 1);
$res = SQL_Query_exec("INSERT INTO ratings (torrent, user, rating, added) VALUES ($id, " . $CURUSER["id"] . ", $rating, '".get_date_time()."')");
if (!$res) {
if (mysqli_errno($GLOBALS["DBconnector"]) == 1062)
show_error_msg(T_("RATING_ERROR"), T_("YOU_ALREADY_RATED_TORRENT"), 1);
else
show_error_msg(T_("RATING_ERROR"), T_("A_UNKNOWN_ERROR_CONTACT_STAFF"), 1);
}
SQL_Query_exec("UPDATE torrents SET numratings = numratings + 1, ratingsum = ratingsum + $rating WHERE id = $id");
show_error_msg(T_("RATING_SUCCESS"), T_("RATING_THANK")."<br /><br /><a href='torrents-details.php?id=$id'>" .T_("BACK_TO_TORRENT"). "</a>");
}
//take rating
if ($_GET["takerating"] == 'yes'){
$rating = (int)$_GET['j'];
if ($rating <= 0 || $rating > 5)
show_error_msg(T_("RATING_ERROR"), T_("INVAILD_RATING"), 1);
$res = SQL_Query_exec("INSERT INTO ratings (torrent, user, rating, added) VALUES ($id, " . $CURUSER["id"] . ", $rating, '".get_date_time()."')");
if (!$res) {
if (mysqli_errno($GLOBALS["DBconnector"]) == 1062)
show_error_msg(T_("RATING_ERROR"), T_("YOU_ALREADY_RATED_TORRENT"), 1);
else
show_error_msg(T_("RATING_ERROR"), T_("A_UNKNOWN_ERROR_CONTACT_STAFF"), 1);
}
SQL_Query_exec("UPDATE torrents SET numratings = numratings + 1, ratingsum = ratingsum + $rating WHERE id = $id");
show_error_msg(T_("RATING_ERROR"), T_("RATING_THANK")."<br /><br /><a href='torrents-details.php?id=$id'>" .T_("BACK_TO_TORRENT"). "</a>");
}
// $srating IS RATING VARIABLE
$srating = "";
$srating .= "<table class='f-border' cellspacing=\"1\" cellpadding=\"4\" width='100%'><tr><td class='f-title' width='60'><b>".T_("RATINGS").":</b></td><td class='f-title' valign='middle'>";
if (!isset($row["rating"])) {
$srating .= "Not Yet Rated";
}else{
$rpic = ratingpic($row["rating"]);
if (!isset($rpic))
$srating .= "invalid?";
else
$srating .= "$rpic (" . $row["rating"] . " ".T_("OUT_OF")." 5) " . $row["numratings"] . " ".T_("USERS_HAVE_RATED");
}
$srating .= "\n";
if (!isset($CURUSER))
$srating .= "(<a href=\"account-login.php?returnto=" . urlencode($_SERVER["REQUEST_URI"]) . "&nowarn=1\">Log in</a> to rate it)";
else {
$ratings = array(
5 => T_("COOL"),
4 => T_("PRETTY_GOOD"),
3 => T_("DECENT"),
2 => T_("PRETTY_BAD"),
1 => T_("SUCKS")
);
//if (!$owned || $moderator) {
$xres = SQL_Query_exec("SELECT rating, added FROM ratings WHERE torrent = $id AND user = " . $CURUSER["id"]);
$xrow = mysqli_fetch_assoc($xres);
if ($xrow)
$srating .= "<br /><i>(".T_("YOU_RATED")." \"" . $xrow["rating"] . " - " . $ratings[$xrow["rating"]] . "\")</i>";
else {
$srating .= "<form style=\"display:inline;\" method=\"post\" action=\"torrents-details.php?id=$id&takerating=yes\"><input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
$srating .= "<select name=\"rating\">\n";
$srating .= "<option value=\"0\">(".T_("ADD_RATING").")</option>\n";
foreach ($ratings as $k => $v) {
$srating .= "<option value=\"$k\">$k - $v</option>\n";
}
$srating .= "</select>\n";
$srating .= "<input type=\"submit\" value=\"".T_("VOTE")."\" />";
$srating .= "</form>\n";
}
//}
}
$srating .= "</td></tr></table>";
print("<center>". $srating . "</center>");// rating
//END DEFINE RATING VARIABLE
//end download box
// $srating IS RATING VARIABLE
$total_votes = 0;
$total_value = 0;
$query = SQL_Query_exec("SELECT COUNT(torrent) as total_votes, SUM(rating) as total_value FROM `ratings` WHERE `torrent` = '$id'");
while ($rating = mysqli_fetch_array($query)) {
$total_votes = $rating['total_votes'];
$total_value = $rating['total_value'];
}
$voted = get_row_count("ratings", "WHERE `torrent` = '" . $id . "' AND `user` = '" . $CURUSER["id"] . "'");
if ($total_votes < 1) {
$count = 0;
} else {
$count = $total_votes;
}
$current_rating = $total_value;
$tense = ($count == 1) ? "vote" : "votes";
if (is_numeric($current_rating) && is_numeric($count)) {
$rating_width = @number_format($current_rating / $count, 2) * 30;
//$rating_width = @number_format((floatval).$current_rating / $count, 2) * 30;
$rating_1 = @number_format($current_rating / $count, 1);
$rating_2 = @number_format($current_rating / $count, 2);
}
if (!$voted) {
$rater .= '<div id="unit_long' . $id . '">';
$rater .= '<ul id="unit_ul' . $id . '" class="unit-rating" style="width: ' . 30 * 5 . 'px;">';
$rater .= '<li class="current_rating" style="width: ' . $rating_width . '"px;"> Currently ' . $rating_2 . '/' . 5 . '</li>';
for ($ncount = 1; $ncount <= 5; $ncount++) {
if (!$voted) {
if ($CURUSER)
$rater .='<li><a href="torrents-details.php?id=' . $id . '&takerating=yes&j=' . $ncount . '" title="'.$ncount.' out of 5" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';
else
$rater .='<li><a href="account-login.php?returnto="' . urlencode($_SERVER["REQUEST_URI"]) . '&nowarn=1" title="'.$ncount.' out of '.$units.'" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';
}
}
$ncount = 0;
}
$rater .= '</ul>';
$rater .= '<p>';
if ($voted) {
$static_rater = array();
$static_rater[] .= '<div id="unit_long'.$id.'">';
$static_rater[] .= '<ul id="unit_ul'.$id.'" class="unit-rating" style="width:'. 30*5 .'px;">';
$static_rater[] .= '<li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';
$static_rater[] .= '</ul>';
$rater .= join("\n", $static_rater);
}
$rater .= '<strong>' . $rating_1 . '</strong>/5 (' . $count . ' ' . $tense . ' cast)';
$rater .= '</p>';
//END DEFINE RATING VARIABLE
print("<tr><td align='left'><b>".T_("HITS").":</b></td><td>" . number_format($row["hits"]) . "</td></tr>\n");
print("<tr><td align='left'><b>".T_("Rating").":</b></td><td>" . $rater . "</td></tr>\n");
$mcat = implode(',', array(2, 2));
$tcat = implode(',', array(48, 48));
CREATE TABLE IF NOT EXISTS `announcements` (
`id` int(10) unsigned NOT NULL auto_increment,
`subject` varchar(64) NOT NULL,
`message` text NOT NULL,
`by` varchar(16) NOT NULL default 'Admin',
`added` datetime NOT NULL default '0000-00-00 00:00:00',
`minclassread` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `subject` (`subject`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `users` ADD `announce_read` ENUM( 'yes', 'no' ) NOT NULL DEFAULT 'no';
ALTER TABLE `users` ADD INDEX ( `announce_read` ) ;
echo "<br /><b>".T_("NO_NEWS")."</b>";
}
end_frame();
}
//Announcment popup
if ($CURUSER && $CURUSER["announce_read"] == "no" && $arr = announcement()) {
?>
<script type="text/javascript" src="js/announcement.js"></script>
<script type="text/javascript" src="js/preview.js"></script>
<!-- annoucement start #<?=$arr[id];?> -->
<div id="dropin" style="position:absolute;visibility:hidden;left:300px;top:100px;width:500px;height:100px">
<table border="0" cellpadding="0" cellspacing="0" width="650" class="error">
<tbody><tr><td class="none" style="padding: 2px 0 0 10px; background: red">
<font color=black><b>Title:</b> <?=$arr["subject"]?> -- <b>Created on"</b> <?=$arr["added"]?> -- <b>By:</b> <?=$arr["by"]?></b></font></td>
<td width="50" align="right" class="none" style="padding: 2px; background: red"><a href="#" onClick="dismissbox();return false"><img src="images/close.jpg" border="0"></a></td></tr>
<tr><td colspan="2" class=none width="650" style="padding: 0 0 0 10px;" bgcolor="#ffffff">
<p>
<?=format_comment($arr["message"])?>
</p><br />
<img id="loading" style=" visibility: hidden" src="images/load.gif">
<span style="color:red" name="preview" id="previewr" align="left">
<a href="#" onclick="javascript:clearannouncement(this.parentNode,'clear_ann.php')">Click here to mark announcement read.</a>
</span><br><br></form>
</td></tr></tbody></table>
</div>
<!-- annoucement end #<?=$arr[id];?>-->
<span id="new_ann" style="visibility: visible"><table border=0 cellspacing=0 cellpadding=10 bgcolor=red width=100% align=center><tr><td style='padding: 10px; background: red' align='center'>
<b><a href="javascript:show_announcement()"><font color=white>New announcement! Please click here to read!</font></a></b></td></tr></table></span><br />
<?php
}
function announcement () {
GLOBAL $CURUSER;
if ($CURUSER && $CURUSER['announce_read'] == "no") {
$res = SQL_Query_exec("SELECT * FROM announcements WHERE minclassread <= $CURUSER[class] ORDER by added DESC LIMIT 1");
$ret = mysqli_fetch_array($res);
}
return $ret;
}
<td align="center"><a href="announcements.php"><img src="images/admin/announce.jpg" border="0" width="32" height="32" alt="" /><br /><?php echo T_("ANNOUNCEMENT"); ?></a><br /></td
</a></td>
deletetorrent($_GET['delete']);
if ($_SERVER['HTTP_REFERER'] != $_SERVER['PHP_SELF'])
{
echo "<meta http-equiv='refresh' content='0;URL=". $_SERVER['PHP_SELF']."'>";
}
<?php
/**
* Scrapeer, a tiny PHP library that lets you scrape
* HTTP(S) and UDP trackers for torrent information.
*
* This file is extensively based on Johannes Zinnau's
* work, which can be found at https://goo.gl/7hyjde
*
* Licensed under a Creative Commons
* Attribution-ShareAlike 3.0 Unported License
* http://creativecommons.org/licenses/by-sa/3.0
*
* @package Scrapeer
*/
namespace Scrapeer;
/**
* The one and only class you'll ever need.
*/
class Scraper {
/**
* Current version of Scrapeer
*
* @var string
*/
const VERSION = '0.5.4';
/**
* Array of errors
*
* @var array
*/
private $errors = array();
/**
* Array of infohashes to scrape
*
* @var array
*/
private $infohashes = array();
/**
* Timeout for a single tracker
*
* @var int
*/
private $timeout;
/**
* Initiates the scraper
*
* @throws \RangeException In case of invalid amount of info-hashes.
*
* @param array|string $hashes List (>1) or string of infohash(es).
* @param array|string $trackers List (>1) or string of tracker(s).
* @param int|null $max_trackers Optional. Maximum number of trackers to be scraped, Default all.
* @param int $timeout Optional. Maximum time for each tracker scrape in seconds, Default 2.
* @param bool $announce Optional. Use announce instead of scrape, Default false.
* @return array List of results.
*/
public function scrape( $hashes, $trackers, $max_trackers = null, $timeout = 2, $announce = false ) {
$final_result = array();
if ( empty( $trackers ) ) {
$this->errors[] = 'No tracker specified, aborting.';
return $final_result;
} else if ( ! is_array( $trackers ) ) {
$trackers = array( $trackers );
}
if ( is_int( $timeout ) ) {
$this->timeout = $timeout;
} else {
$this->timeout = 20;
$this->errors[] = 'Timeout must be an integer. Using default value.';
}
try {
$this->infohashes = $this->normalize_infohashes( $hashes );
} catch ( \RangeException $e ) {
$this->errors[] = $e->getMessage();
return $final_result;
}
$max_iterations = is_int( $max_trackers ) ? $max_trackers : count( $trackers );
foreach ( $trackers as $index => $tracker ) {
if ( ! empty( $this->infohashes ) && $index < $max_iterations ) {
$info = parse_url( $tracker );
$protocol = $info['scheme'];
$host = $info['host'];
if ( empty( $protocol ) || empty( $host ) ) {
$this->errors[] = 'Skipping invalid tracker (' . $tracker . ').';
continue;
}
$port = isset( $info['port'] ) ? $info['port'] : null;
$path = isset( $info['path'] ) ? $info['path'] : null;
$passkey = $this->get_passkey( $path );
$result = $this->try_scrape( $protocol, $host, $port, $passkey, $announce );
$final_result = array_merge( $final_result, $result );
continue;
}
break;
}
return $final_result;
}
/**
* Normalizes the given hashes
*
* @throws \RangeException If amount of valid infohashes > 64 or < 1.
*
* @param array $infohashes List of infohash(es).
* @return array Normalized infohash(es).
*/
private function normalize_infohashes( $infohashes ) {
if ( ! is_array( $infohashes ) ) {
$infohashes = array( $infohashes );
}
foreach ( $infohashes as $index => $infohash ) {
if ( ! preg_match( '/^[a-f0-9]{40}$/i', $infohash ) ) {
$this->errors[] = 'Invalid infohash skipped (' . $infohash . ').';
unset( $infohashes[ $index ] );
}
}
$total_infohashes = count( $infohashes );
if ( $total_infohashes > 64 || $total_infohashes < 1 ) {
throw new \RangeException( 'Invalid amount of valid infohashes (' . $total_infohashes . ').' );
}
$infohashes = array_values( $infohashes );
return $infohashes;
}
/**
* Returns the passkey found in the scrape request.
*
* @param string $path Path from the scrape request.
* @return string Passkey or empty string.
*/
private function get_passkey( $path ) {
if ( ! is_null( $path ) && preg_match( '/[a-z0-9]{32}/i', $path, $matches ) ) {
return '/' . $matches[0];
}
return '';
}
/**
* Tries to scrape with a single tracker.
*
* @throws \Exception In case of unsupported protocol.
*
* @param string $protocol Protocol of the tracker.
* @param string $host Domain or address of the tracker.
* @param int $port Optional. Port number of the tracker.
* @param string $passkey Optional. Passkey provided in the scrape request.
* @param bool $announce Optional. Use announce instead of scrape, Default false.
* @return array List of results.
*/
private function try_scrape( $protocol, $host, $port, $passkey, $announce ) {
$infohashes = $this->infohashes;
$this->infohashes = array();
$results = array();
try {
switch ( $protocol ) {
case 'udp':
$port = isset( $port ) ? $port : 80;
$results = $this->scrape_udp( $infohashes, $host, $port, $announce );
break;
case 'http':
$port = isset( $port ) ? $port : 80;
$results = $this->scrape_http( $infohashes, $protocol, $host, $port, $passkey, $announce );
break;
case 'https':
$port = isset( $port ) ? $port : 443;
$results = $this->scrape_http( $infohashes, $protocol, $host, $port, $passkey, $announce );
break;
default:
throw new \Exception( 'Unsupported protocol (' . $protocol . '://' . $host . ').' );
}
} catch ( \Exception $e ) {
$this->infohashes = $infohashes;
$this->errors[] = $e->getMessage();
}
return $results;
}
/**
* Initiates the HTTP(S) scraping
*
* @param array|string $infohashes List (>1) or string of infohash(es).
* @param string $protocol Protocol to use for the scraping.
* @param string $host Domain or IP address of the tracker.
* @param int $port Optional. Port number of the tracker, Default 80 (HTTP) or 443 (HTTPS).
* @param string $passkey Optional. Passkey provided in the scrape request.
* @param bool $announce Optional. Use announce instead of scrape, Default false.
* @return array List of results.
*/
private function scrape_http( $infohashes, $protocol, $host, $port, $passkey, $announce ) {
if ( true === $announce ) {
$response = $this->http_announce( $infohashes, $protocol, $host, $port, $passkey );
} else {
$query = $this->http_query( $infohashes, $protocol, $host, $port, $passkey );
$response = $this->http_request( $query, $host, $port );
}
$results = $this->http_data( $response, $infohashes, $host );
return $results;
}
/**
* Builds the HTTP(S) query
*
* @param array|string $infohashes List (>1) or string of infohash(es).
* @param string $protocol Protocol to use for the scraping.
* @param string $host Domain or IP address of the tracker.
* @param int $port Port number of the tracker, Default 80 (HTTP) or 443 (HTTPS).
* @param string $passkey Optional. Passkey provided in the scrape request.
* @return string Request query.
*/
private function http_query( $infohashes, $protocol, $host, $port, $passkey ) {
$tracker_url = $protocol . '://' . $host . ':' . $port . $passkey;
$scrape_query = '';
foreach ( $infohashes as $index => $infohash ) {
if ( $index > 0 ) {
$scrape_query .= '&info_hash=' . urlencode( pack( 'H*', $infohash ) );
} else {
$scrape_query .= '/scrape?info_hash=' . urlencode( pack( 'H*', $infohash ) );
}
}
$request_query = $tracker_url . $scrape_query;
return $request_query;
}
/**
* Executes the query and returns the result
*
* @throws \Exception If the connection can't be established.
* @throws \Exception If the response isn't valid.
*
* @param string $query The query that will be executed.
* @param string $host Domain or IP address of the tracker.
* @param int $port Port number of the tracker, Default 80 (HTTP) or 443 (HTTPS).
* @return string Request response.
*/
private function http_request( $query, $host, $port ) {
$context = stream_context_create( array(
'http' => array(
'timeout' => $this->timeout,
),
));
if ( false === ( $response = @file_get_contents( $query, false, $context ) ) ) {
throw new \Exception( 'Invalid scrape connection (' . $host . ':' . $port . ').' );
}
if ( substr( $response, 0, 12 ) !== 'd5:filesd20:' ) {
throw new \Exception( 'Invalid scrape response (' . $host . ':' . $port . ').' );
}
return $response;
}
/**
* Builds the query, sends the announce request and returns the data
*
* @throws \Exception If the connection can't be established.
*
* @param array|string $infohashes List (>1) or string of infohash(es).
* @param string $protocol Protocol to use for the scraping.
* @param string $host Domain or IP address of the tracker.
* @param int $port Port number of the tracker, Default 80 (HTTP) or 443 (HTTPS).
* @param string $passkey Optional. Passkey provided in the scrape request.
* @return string Request response.
*/
private function http_announce( $infohashes, $protocol, $host, $port, $passkey ) {
$tracker_url = $protocol . '://' . $host . ':' . $port . $passkey;
$context = stream_context_create( array(
'http' => array(
'timeout' => $this->timeout,
),
));
$response_data = '';
foreach ( $infohashes as $infohash ) {
$query = $tracker_url . '/announce?info_hash=' . urlencode( pack( 'H*', $infohash ) );
if ( false === ( $response = @file_get_contents( $query, false, $context ) ) ) {
throw new \Exception( 'Invalid announce connection (' . $host . ':' . $port . ').' );
}
if ( substr( $response, 0, 12 ) !== 'd8:completei' ||
substr( $response, 0, 46 ) === 'd8:completei0e10:downloadedi0e10:incompletei1e' ) {
continue;
}
$ben_hash = '20:' . pack( 'H*', $infohash ) . 'd';
$response_data .= $ben_hash . $response;
}
return $response_data;
}
/**
* Parses the response and returns the data
*
* @param string $response The response that will be parsed.
* @param array $infohashes List of infohash(es).
* @param string $host Domain or IP address of the tracker.
* @return array Parsed data.
*/
private function http_data( $response, $infohashes, $host ) {
$torrents_data = array();
foreach ( $infohashes as $infohash ) {
$ben_hash = '20:' . pack( 'H*', $infohash ) . 'd';
$start_pos = strpos( $response, $ben_hash );
if ( false !== $start_pos ) {
$start = $start_pos + 24;
$head = substr( $response, $start );
$end = strpos( $head, 'ee' ) + 1;
$data = substr( $response, $start, $end );
$seeders = '8:completei';
$torrent_info['seeders'] = $this->get_information( $data, $seeders, 'e' );
$completed = '10:downloadedi';
$torrent_info['completed'] = $this->get_information( $data, $completed, 'e' );
$leechers = '10:incompletei';
$torrent_info['leechers'] = $this->get_information( $data, $leechers, 'e' );
$torrents_data[ $infohash ] = $torrent_info;
} else {
$this->collect_infohash( $infohash );
$this->errors[] = 'Invalid infohash (' . $infohash . ') for tracker: ' . $host . '.';
}
}
return $torrents_data;
}
/**
* Parses a string and returns the data between $start and $end.
*
* @param string $data The data that will be parsed.
* @param string $start Beginning part of the data.
* @param string $end Ending part of the data.
* @return int Parsed information or 0.
*/
private function get_information( $data, $start, $end ) {
$start_pos = strpos( $data, $start );
if ( false !== $start_pos ) {
$start = $start_pos + strlen( $start );
$head = substr( $data, $start );
$end = strpos( $head, $end );
$information = substr( $data, $start, $end );
return (int) $information;
}
return 0;
}
/**
* Initiates the UDP scraping
*
* @param array|string $infohashes List (>1) or string of infohash(es).
* @param string $host Domain or IP address of the tracker.
* @param int $port Optional. Port number of the tracker, Default 80.
* @param bool $announce Optional. Use announce instead of scrape, Default false.
* @return array List of results.
*/
private function scrape_udp( $infohashes, $host, $port, $announce ) {
list( $socket, $transaction_id, $connection_id ) = $this->prepare_udp( $host, $port );
if ( true === $announce ) {
$response = $this->udp_announce( $socket, $infohashes, $connection_id );
$keys = 'Nleechers/Nseeders';
$start = 12;
$end = 16;
$offset = 20;
} else {
$response = $this->udp_scrape( $socket, $infohashes, $connection_id, $transaction_id, $host, $port );
$keys = 'Nseeders/Ncompleted/Nleechers';
$start = 8;
$end = $offset = 12;
}
$results = $this->udp_scrape_data( $response, $infohashes, $host, $keys, $start, $end, $offset );
return $results;
}
/**
* Prepares the UDP connection
*
* @param string $host Domain or IP address of the tracker.
* @param int $port Optional. Port number of the tracker, Default 80.
* @return array Created socket, transaction ID and connection ID.
*/
private function prepare_udp( $host, $port ) {
$socket = $this->udp_create_connection( $host, $port );
$transaction_id = $this->udp_connection_request( $socket );
$connection_id = $this->udp_connection_response( $socket, $transaction_id, $host, $port );
return array( $socket, $transaction_id, $connection_id );
}
/**
* Creates the UDP socket and establishes the connection
*
* @throws \Exception If the socket couldn't be created or connected to.
*
* @param string $host Domain or IP address of the tracker.
* @param int $port Port number of the tracker, Default 80.
* @return resource $socket Created and connected socket.
*/
private function udp_create_connection( $host, $port ) {
if ( false === ( $socket = @socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ) ) ) {
throw new \Exception( "Couldn't create socket." );
}
$timeout = $this->timeout;
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array( 'sec' => $timeout, 'usec' => 0 ) );
socket_set_option( $socket, SOL_SOCKET, SO_SNDTIMEO, array( 'sec' => $timeout, 'usec' => 0 ) );
if ( false === @socket_connect( $socket, $host, $port ) ) {
throw new \Exception( "Couldn't connect to socket." );
}
return $socket;
}
/**
* Writes to the connected socket and returns the transaction ID
*
* @throws \Exception If the socket couldn't be written to.
*
* @param resource $socket The socket resource.
* @return int The transaction ID.
*/
private function udp_connection_request( $socket ) {
$connection_id = "\x00\x00\x04\x17\x27\x10\x19\x80";
$action = pack( 'N', 0 );
$transaction_id = mt_rand( 0, 2147483647 );
$buffer = $connection_id . $action . pack( 'N', $transaction_id );
if ( false === @socket_write( $socket, $buffer, strlen( $buffer ) ) ) {
socket_close( $socket );
throw new \Exception( "Couldn't write to socket." );
}
return $transaction_id;
}
/**
* Reads the connection response and returns the connection ID
*
* @throws \Exception If anything fails with the scraping.
*
* @param resource $socket The socket resource.
* @param int $transaction_id The transaction ID.
* @param string $host Domain or IP address of the tracker.
* @param int $port Port number of the tracker, Default 80.
* @return string The connection ID.
*/
private function udp_connection_response( $socket, $transaction_id, $host, $port ) {
if ( false === ( $response = @socket_read( $socket, 16 ) ) ) {
socket_close( $socket );
throw new \Exception( 'Invalid scrape connection! (' . $host . ':' . $port . ').' );
}
if ( strlen( $response ) < 16 ) {
socket_close( $socket );
throw new \Exception( 'Invalid scrape response (' . $host . ':' . $port . ').' );
}
$result = unpack( 'Naction/Ntransaction_id', $response );
if ( 0 !== $result['action'] || $result['transaction_id'] !== $transaction_id ) {
socket_close( $socket );
throw new \Exception( 'Invalid scrape result (' . $host . ':' . $port . ').' );
}
$connection_id = substr( $response, 8, 8 );
return $connection_id;
}
/**
* Reads the socket response and returns the torrent data
*
* @throws \Exception If anything fails while reading the response.
*
* @param resource $socket The socket resource.
* @param array $hashes List (>1) or string of infohash(es).
* @param string $connection_id The connection ID.
* @param int $transaction_id The transaction ID.
* @param string $host Domain or IP address of the tracker.
* @param int $port Port number of the tracker, Default 80.
* @return string Response data.
*/
private function udp_scrape( $socket, $hashes, $connection_id, $transaction_id, $host, $port ) {
$this->udp_scrape_request( $socket, $hashes, $connection_id, $transaction_id );
$read_length = 8 + ( 12 * count( $hashes ) );
if ( false === ( $response = @socket_read( $socket, $read_length ) ) ) {
socket_close( $socket );
throw new \Exception( 'Invalid scrape connection (' . $host . ':' . $port . ').' );
}
socket_close( $socket );
if ( strlen( $response ) < $read_length ) {
throw new \Exception( 'Invalid scrape response (' . $host . ':' . $port . ').' );
}
$result = unpack( 'Naction/Ntransaction_id', $response );
if ( 2 !== $result['action'] || $result['transaction_id'] !== $transaction_id ) {
throw new \Exception( 'Invalid scrape result (' . $host . ':' . $port . ').' );
}
return $response;
}
/**
* Writes to the connected socket
*
* @throws \Exception If the socket couldn't be written to.
*
* @param resource $socket The socket resource.
* @param array $hashes List (>1) or string of infohash(es).
* @param string $connection_id The connection ID.
* @param int $transaction_id The transaction ID.
*/
private function udp_scrape_request( $socket, $hashes, $connection_id, $transaction_id ) {
$action = pack( 'N', 2 );
$infohashes = '';
foreach ( $hashes as $infohash ) {
$infohashes .= pack( 'H*', $infohash );
}
$buffer = $connection_id . $action . pack( 'N', $transaction_id ) . $infohashes;
if ( false === @socket_write( $socket, $buffer, strlen( $buffer ) ) ) {
socket_close( $socket );
throw new \Exception( "Couldn't write to socket." );
}
}
/**
* Writes the announce to the connected socket
*
* @throws \Exception If the socket couldn't be written to.
*
* @param resource $socket The socket resource.
* @param array $hashes List (>1) or string of infohash(es).
* @param string $connection_id The connection ID.
* @return string Torrent(s) data.
*/
private function udp_announce( $socket, $hashes, $connection_id ) {
$action = pack( 'N', 1 );
$downloaded = $left = $uploaded = "\x30\x30\x30\x30\x30\x30\x30\x30";
$peer_id = $this->random_peer_id();
$event = pack( 'N', 3 );
$ip_addr = pack( 'N', 0 );
$key = pack( 'N', mt_rand( 0, 2147483647 ) );
$num_want = -1;
$ann_port = pack( 'N', mt_rand( 0, 255 ) );
$response_data = '';
foreach ( $hashes as $infohash ) {
$transaction_id = mt_rand( 0, 2147483647 );
$buffer = $connection_id . $action . pack( 'N', $transaction_id ) . pack( 'H*', $infohash ) .
$peer_id . $downloaded . $left . $uploaded . $event . $ip_addr . $key . $num_want . $ann_port;
if ( false === @socket_write( $socket, $buffer, strlen( $buffer ) ) ) {
socket_close( $socket );
throw new \Exception( "Couldn't write announce to socket." );
}
$response = $this->udp_verify_announce( $socket, $transaction_id );
if ( false === $response ) {
continue;
}
$response_data .= $response;
}
socket_close( $socket );
return $response_data;
}
/**
* Generates a random peer ID
*
* @return string Generated peer ID.
*/
private function random_peer_id() {
$identifier = '-SP0054-';
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$peer_id = $identifier . substr( str_shuffle( $chars ), 0, 12 );
return $peer_id;
}
/**
* Verifies the correctness of the announce response
*
* @param resource $socket The socket resource.
* @param int $transaction_id The transaction ID.
* @return string Response data.
*/
private function udp_verify_announce( $socket, $transaction_id ) {
if ( false === ( $response = @socket_read( $socket, 20 ) ) ) {
return false;
}
if ( strlen( $response ) < 20 ) {
return false;
}
$result = unpack( 'Naction/Ntransaction_id', $response );
if ( 1 !== $result['action'] || $result['transaction_id'] !== $transaction_id ) {
return false;
}
return $response;
}
/**
* Reads the socket response and returns the torrent data
*
* @param string $response Data from the request response.
* @param array $hashes List (>1) or string of infohash(es).
* @param string $host Domain or IP address of the tracker.
* @param string $keys Keys for the unpacked information.
* @param int $start Start of the content we want to unpack.
* @param int $end End of the content we want to unpack.
* @param int $offset Offset to the next content part.
* @return array Scraped torrent data.
*/
private function udp_scrape_data( $response, $hashes, $host, $keys, $start, $end, $offset ) {
$torrents_data = array();
foreach ( $hashes as $infohash ) {
$byte_string = substr( $response, $start, $end );
$data = unpack( 'N', $byte_string );
$content = $data[1];
if ( ! empty( $content ) ) {
$results = unpack( $keys, $byte_string );
$torrents_data[ $infohash ] = $results;
} else {
$this->collect_infohash( $infohash );
$this->errors[] = 'Invalid infohash (' . $infohash . ') for tracker: ' . $host . '.';
}
$start += $offset;
}
return $torrents_data;
}
/**
* Collects info-hashes that couldn't be scraped.
*
* @param string $infohash Infohash that wasn't scraped.
*/
private function collect_infohash( $infohash ) {
$this->infohashes[] = $infohash;
}
/**
* Checks if there are any errors
*
* @return bool True or false, depending if errors are present or not.
*/
public function has_errors() {
return ! empty( $this->errors );
}
/**
* Returns all the errors that were logged
*
* @return array All the logged errors.
*/
public function get_errors() {
return $this->errors;
}
}
?>
[code]
torrent-details
[code]
require_once("backend/scraper.php");
//===| UDP Scraper
$tress = SQL_Query_exec("SELECT * FROM `announce` WHERE `torrent` = $id");
if (mysqli_num_rows($tress) > 1){
$resu = SQL_Query_exec("SELECT url FROM announce WHERE torrent=$id");
}
else
{
$resu = SQL_Query_exec("SELECT id, info_hash, announce FROM torrents WHERE external = 'yes' AND id = $id");
}
while ($rowu = mysqli_fetch_array($resu)){
if (mysqli_num_rows($tress) > 1){
$ann = $rowu["url"];
}else{
$ann = $rowu["announce"];
}
$annlist = array($ann);
$seeders = $leechers = $downloaded = NULL;
foreach ($annlist as $ann)
{
$tracker = explode("/", $ann);
$path = array_pop($tracker);
$oldpath = $path;
$tracker = implode("/", $tracker)."/".$path;
if ($oldpath == $path){
continue;
}
}
if(preg_match('/udp:\/\//', $tracker)){
$udp=true;
try
{
$info = $row['info_hash'];
$udp = new scrapeer\scraper();
$stats=$udp->scrape($info, $tracker);
print_r($stats);
foreach ($stats as $idu=>$scrape)
{
$seeders += $scrape['seeders'];
$leechers += $scrape['leechers'];
$downloaded += $scrape['completed'];
}
}
catch(ScraperException $e)
{
$e->isConnectionError();
}
}
elseif (preg_match('/http:\/\//', $tracker)) {
try {
$http = new scrapeer\scraper($timeout,$maxread );
$stats = $http->scrape($row['info_hash'],$tracker);
print_r($stats);
foreach ($stats as $idu => $scrape) {
$seeders= $scrape['seeders'];
$leechers= $scrape['leechers'];
$downloaded= $scrape['completed'];
}
}
catch(ScraperException $e) {
$e->isConnectionError();
}
}
if ($seeders != -1)
{
$seeders = $scrape[ 'seeders' ];
$leechers = $scrape[ 'leechers' ];
$downloaded = $scrape[ 'completed' ];
SQL_Query_exec("UPDATE `announce` SET `online` = 'yes', `seeders` = '" .$seeders."', `leechers` = '" .$leechers."', `times_completed` = '".$downloaded."' WHERE `url` = ".sqlesc($ann)." AND `torrent` = $id");
} else {
SQL_Query_exec("UPDATE `announce` SET `online` = 'no' WHERE `url` = ".sqlesc($ann)." AND `torrent` = $id");
}
}
if ($seeders !== NULL)
{
SQL_Query_exec("UPDATE torrents SET leechers = '".$leechers."', seeders = '".$seeders."', times_completed = '".$downloaded."', last_action = '".get_date_time()."', visible = 'yes' WHERE id = '".$row['id']."'");
}
else
{
SQL_Query_exec("UPDATE torrents SET last_action = '".get_date_time()."' WHERE id ='".$row['id']."'");
}
//===| End UDP Scraper
//===| UDP Scraper
$tress = SQL_Query_exec("SELECT * FROM `announce` WHERE `torrent` = $id");
if (mysqli_num_rows($tress) > 1){
$resu = SQL_Query_exec("SELECT url FROM announce WHERE torrent=$id");
}
else
{
$resu = SQL_Query_exec("SELECT id, info_hash, announce FROM torrents WHERE external = 'yes' AND id = $id");
}
while ($rowu = mysqli_fetch_array($resu)){
if (mysqli_num_rows($tress) > 1){
$ann = $rowu["url"];
}else{
$ann = $rowu["announce"];
}
$annlist = array($ann);
$seeders = $leechers = $downloaded = NULL;
foreach ($annlist as $ann)
{
$tracker = explode("/", $ann);
$path = array_pop($tracker);
$oldpath = $path;
$path = preg_replace("/^announce/", "scrape", $path);
$tracker = implode("/", $tracker)."/".$path;
if ($oldpath == $path){
continue;
}
}
if(preg_match('/udp:\/\//', $tracker)){
$udp=true;
try
{
$timeout = 2;
$udp= new udptscraper($timeout);
$stats=$udp->scrape($tracker,$row["info_hash"]);
//print_r($stats); exit();
foreach ($stats as $idu=>$scrape)
{
$seeders1 += $scrape['seeders'];
$leechers1 += $scrape['leechers'];
$downloaded1 += $scrape['completed'];
}
}
catch(ScraperException $e)
{
$e->isConnectionError();
}
}
else
{
$stats = torrent_scrape_url($tracker, $row["info_hash"]);
}
if ($scrape['seeds'] != -1)
{
$seeders1 += ( isset( $stats[ 'seeds' ] ) ) ? $stats[ 'seeds' ] : $scrape[ 'seeders' ];
$leechers1 += ( isset( $stats[ 'peers' ] ) ) ? $stats[ 'peers' ] : $scrape[ 'leechers' ];
$downloaded1 += ( isset( $stats[ 'downloaded' ] ) ) ? $stats[ 'downloaded' ] : $scrape[ 'completed' ];
SQL_Query_exec("UPDATE `announce` SET `online` = 'yes', `seeders` = '" .$seeders1."', `leechers` = '" .$leechers1."', `times_completed` = '".$downloaded1."' WHERE `url` = ".sqlesc($ann)." AND `torrent` = $id");
} else {
SQL_Query_exec("UPDATE `announce` SET `online` = 'no' WHERE `url` = ".sqlesc($ann)." AND `torrent` = $id");
}
}
if ($seeders1 !== NULL)
{
SQL_Query_exec("UPDATE torrents SET leechers = '".$leechers1."', seeders = '".$seeders1."', times_completed = '".$downloaded1."', last_action = '".get_date_time()."', visible = 'yes' WHERE id = '".$row['id']."'");
}
else
{
SQL_Query_exec("UPDATE torrents SET last_action = '".get_date_time()."' WHERE id ='".$row['id']."'");
}
//===| End UDP Scraper
if ($seeders1 !== NULL)
{
print ("<div style='margin-bottom:5px; margin-left:20px'><b>".T_("LIVE_STATS")."</b>: </div>");
print ("<div style='margin-left:20px'>".T_("SEEDERS").": <font color='limegreen'>".$seeders1."</font></div>");
print ("<div style='margin-left:20px'>".T_("LEECHERS").": <font color='#FF2000'>".$leechers1."</font></div>");
print ("<div style='margin-left:20px'>".T_("COMPLETED").": <font color='#0080FF'>".$downloaded1."</font></div>");
}
else
{
print ("<div style='margin-top:10px; margin-bottom:5px' align='center'><b>".T_("LIVE_STATS")."</b>: </div>");
print ("<div style='font:italic 14px Times' align='center'><font color='#FF2000'>".T_("TRACKERS_OFFLINE")."!</font></div>");
}
print ("<div style='margin-top:10px; margin-bottom:10px' align='center'>
<a href='torrents-details.php?id=".$id."'><input type='submit' name='submit' value=".T_("SUBMIT")."></a>
</div>");
print ("</td>");
}
else
{
print ("<td class='table_col1' width='150' border='0' valign='middle' align='center'>
<div style='margin-top:5px; margin-bottom:10px; font:bold 16px Times' align='center'><font color='#FF2000'>".T_("EXTERNAL_TORRENT")."</font></div>
<div style='font:normal 12px Times' align='center'>".T_("UPDATE_STATS")."</div></div>
<form action='torrents-details.php?id=$id&scrape=1' method='post'>
<div style='margin-top:5px; margin-bottom:10px' align='center'><input type='submit' name='submit' value=".T_("UPDATE")." /></div>
</form>
</td>");
}
print ("</tr></table></td>");
}
//===| End Scrape External Torrents