PHP CURL Question
Posted: Fri Jan 29, 2010 2:37 am
I created this about 3 years ago for vBulletin i am trying to update it for a client so that it will also search rapidshare.com and hot file. anybody have any ideas
Current Output:
http://rapidshare.de/files/29195350/11.zip
http://rapidshare.de/files/29195350/11.zip
http://depositfiles.com/de/files/294810
http://depositfiles.com/de/files/294810
http://rapidshare.com/files/352064/WSS5071608.rar
http://rapidshare.com/files/352064/WSS5071608.rar
http://hotfile.com/dl/23262989/8de535f/
http://hotfile.com/dl/23262989/8de535f/
Code: Select all
<?php
session_start();
/*
Raproductions Link Checker
Credits :
Raproductions - Code.
Thanks to testers.
*/
require_once ('includes/config.php');
require_once('global.php');
set_time_limit(0);
define('vno', '11');
// Start of user defined variables
$trash = $vbulletin->options['bot_trash'];
$botid = $vbulletin->options['bot_id'];
$botname = $vbulletin->options['bot_name'];
$check = $vbulletin->options['bot_ftc'];
$check = ($vbulletin->options['bot_ftcs'] == 1) ? ("`forumid` IN ($check)") : ("`forumid` NOT IN ($check)");
$pa = '
Links checked on ' . date('d/m/y'). ' by link checker bot';
$ppa = '<!--Botstart--><br /> Links checked on ' . date('d/m/y'). ' by link checker bot<!--Botend-->';
$perd = $vbulletin->options['bot_pdv'];
$tr = $vbulletin->options['bot_tr'];
$pmt = $vbulletin->options['bot_pmt'];
$pms = $vbulletin->options['bot_pmr'];
$pus = $vbulletin->options['bot_pus'];
$reportforumid = $vbulletin->options['bot_rf'];
$showurl = $vbulletin->options['bot_su'];
//End of user defined variables. Don't edit below this line. No really don't.
$y = 0;
$count = 0;
$sleep = 0;
$topic = 0;
$force = 0;
$prefix = $config['Database']['tableprefix'];
function curl($link, $postfeilds = '')
{
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($postfeilds)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfeilds);
}
$page = curl_exec($ch);
return($page);
curl_close($ch);
}
function check($link, $y, $regex, $pattern='', $replace='')
{
global $y;
if(!empty($pattern))
{
$link = preg_replace($pattern, $replace, $link);
}
$page = curl($link);
if(eregi($regex , $page))
{
$y++;
flush();
ob_flush();
echo " Active : $link<br />";
}
elseif(!eregi($regex , $page))
{
flush();
ob_flush();
echo "Dead : $link<br />";
}
else
{
exit('Some error');
}
unset($page);
}
function cc($var, $query)
{
global $connect, $config, $prefix, $check, $force;
if(!mysql_ping($connect) || $force == 1)
{
mysql_close($connect);
$connect = mysql_connect($config['MasterServer']['servername'], $config['MasterServer']['username'] , $config['MasterServer']['password']) or die('Unable to connect to mysql server.');
mysql_select_db($config['Database']['dbname'], $connect) or die('Unable to connect to the db');
echo 'Connection re-established to mysql<br />';
}
if($force != 1)
{
$var = mysql_query($query) or die('Error : ' . mysql_error() . 'on query :' . $query);
return($var);
}
}
$sites = array(
array("megaupload\.com/([a-z]{2}\/)?\?d=", "(Filename:)|(All download slots assigned to your country)", "@\/([a-z]{2}\/)@i", "/en/"),
array("rapidshare\.de\/files\/", "You want to download"),
array("filefactory\.com\/file\/", "(download link)|(Please try again later)"),
array("mediafire\.com\/?", "You requested", "@com\?@i", "com/download.php?"),
array("mediafire\.com\/\?sharekey", "Folder Name:"),
array("depositfiles\.com\/([a-z]{2}\/)?files\/", "File Name", "@(com\/files\/)|(com\/[a-z]{2}\/files\/)@i", "com/en/files/"),
array("sendspace\.com\/file\/", "The download link is located below.")
);
$connect = mysql_connect($config['MasterServer']['servername'], $config['MasterServer']['username'] , $config['MasterServer']['password']) or die('Unable to connect to mysql server.');
mysql_select_db($config['Database']['dbname'], $connect) or die('Unable to connect to the db');
if(isset($_REQUEST['debug']))
{
$mode = $_GET['debug'];
echo "<a href = '?debug=checkatopic'>Check a topic</a> | <a href = '?debug=gettopicid'>Get Current Topic ID</a> | <a href = '?debug=ignoretopic'>Ignore Topic</a> | <a href = '?debug=unignoretopic'>Unignore Topic</a> | <a href = '?debug=phpinfo'>PHPInfo</a> | <a href = '?debug=general'>General Debugging</a> | <a href = '?debug=curlcheck'>cURL Check</a> | <a href = '?debug=vcheck'>Version Check</a><br />Current Mode : $mode<br />";
switch($mode)
{
case 'checkatopic' :
if(isset($_REQUEST['topic']))
{
$_SESSION['checkatopic'] = $_GET['topic'];
echo 'Please restart the bot not in debug mode';
}
else
{
echo '<form action = "" method = "get"><input type = "text" name = "topic"><br /><input type = "submit" value = "Check Topic"><input type = "hidden" name = "debug" value = "checkatopic"></form>';
}
exit();
case 'gettopicid' :
$_SESSION['gettopicid'] = 1;
echo 'Please restart the bot not in the debug mode<br />';
exit();
case 'ignoretopic' :
if(isset($_REQUEST['topic']))
{
$tid = $_GET['topic'];
mysql_query("UPDATE `".$prefix."thread` SET `ignore_check` = 1 WHERE `threadid` = '$tid'") or die(mysql_error());
echo "Topic $tid put to ignore list";
}
else
{
echo '<form action = "" method = "get"><input type = "text" name = "topic"><br /><input type = "submit" value = "Ignore Topic"><input type = "hidden" name = "debug" value = "ignoretopic"></form>';
}
exit();
case 'unignoretopic' :
if(isset($_REQUEST['topic']))
{
$tid = $_GET['topic'];
mysql_query("UPDATE `".$prefix."thread` SET `ignore_check` = 0 WHERE `threadid` = '$tid'") or die(mysql_error());
echo "Topic $tid removed from ignore list";
}
else
{
echo '<form action = "" method = "get"><input type = "text" name = "topic"><br /><input type = "submit" value = "Unignore Topic"><input type = "hidden" name = "debug" value = "unignoretopic"></form>';
}
exit();
case 'phpinfo' :
phpinfo();
exit();
case 'general' :
echo (!extension_loaded("curl")) ? 'Warning : cURL is not enabled. This will cause the Bot to work unexceptedly.<br />Please load activate cURL extension (http://www.php.net/cURL)<br />' : 'Success : cURL is enabled<br/>';
echo (PHP_VERSION < 5) ? 'Warning : You are running an older version of PHP,it is recommended to update to version 5<br/>' : 'Success : PHP Version is atleast 5<br />';
echo (ini_get('safe_mode') == 1) ? 'Warning : Safe mode is on, please disable it to prevent unexpected errors<br />' : 'Success : Safe mode is disabled<br />';
exit();
case 'curlcheck' :
$page = curl('http://maxwcoders.com/bots/vcheck.html');
echo (!$page) ? 'Warning : cURL is not working as expected' : 'Success : cURL is working fine';
exit();
case 'vcheck' :
$vpage = file_get_contents('http://maxwcoders.com/bots/vcheck.html');
echo ($vpage > vno) ? 'Warning : You are running an older version of the bot<br />' : 'Success : You have the latest version of bot<br />';
exit();
}
exit();
}
if($_SESSION['checkatopic'])
{
$qu1 = cc($qu1, "SELECT `threadid` ,`firstpostid`, `forumid`, `replycount`, `title` FROM `".$prefix."thread` WHERE `threadid` = '$_SESSION[checkatopic]'");
$med = 'Debug Mode : Check a topic';
}
elseif(!$_REQUEST['method'])
{
$qu1 = cc($qu1, "SELECT `threadid` ,`firstpostid`, `forumid`, `replycount`, `title` FROM `".$prefix."thread` WHERE `time_check` = '0' AND `ignore_check` = '0' AND ($check)");
$med = 'Checking unchecked topics';
}
elseif($_REQUEST['method'] == 'ignored')
{
$qu1 = cc($qu1, "SELECT `threadid` , `firstpostid`, `forumid`, `replycount`, `title` FROM `".$prefix."thread` WHERE `ignore_check` = '1' AND ($check)");
$med = 'Checking ignored topics';
}
elseif($_REQUEST['method'] == '1day')
{
$qu1 = cc($qu1, "SELECT `threadid` ,`firstpostid`, `forumid`, `replycount`, `title` FROM `".$prefix."thread` WHERE ".time()." - `time_check` >= '86400' AND `ignore_check` = '0' AND ($check)");
$med = 'Checking topics where 24 hours have passed after checking them';
}
elseif($_REQUEST['method'] == 'allei')
{
$qu1 = cc($qu1, "SELECT `threadid` ,`firstpostid`, `forumid`, `replycount`, `title` FROM `".$prefix."thread` WHERE `ignore_check` = '0' AND ($check)");
$med = 'Checking all topics, expect ignored topics';
}
elseif($_REQUEST['method'] == 'all')
{
$qu1 = cc($qu1, "SELECT `threadid` ,`firstpostid`, `forumid`, `replycount`, `title` FROM `".$prefix."thread` WHERE ($check)");
$med = 'Checking all topics';
}
else
{
exit('No method specified');
}
echo "<a href = '?method'>Unchecked</a> | <a href = '?method=ignored'>Ignored</a> | <a href = '?method=1day'>1 Day</a> | <a href = '?method=allei'>All except ignored</a> | <a href = '?method=all'>All</a> | <a href= '?debug'>Debug Mode</a><br />Current Method : $med<br />";
while($re1 = mysql_fetch_assoc($qu1))
{
$pid = $re1['firstpostid'];
$tid = $re1['threadid'];
if($_SESSION['gettopicid'] == 1)
{
echo "Current topic id is $tid<br />";
session_destroy();
exit();
}
$forum = $re1['forumid'];
$replies = $re1['replycount'];
$title = $re1['title'];
$qu2 = cc($qu2, "SELECT `pagetext`, `userid` , `username` FROM `".$prefix."post` WHERE `postid` = $pid");
$re2 = mysql_fetch_assoc($qu2);
$alllinks = $re2['pagetext'];
$uid = $re2['userid'];
$uname = $re2['username'];
$topic++;
$post = $alllinks;
$alllinks = explode(" ", $alllinks);
$alllinks = implode("\n", $alllinks);
$alllinks = explode("\n", $alllinks);
foreach($alllinks as $link)
{
if(eregi("(http|www)" , $link))
{
//Remove the BBCode, to get the real link.
$link = preg_replace("/(\[URL\]|\[url\]|\[CODE\]|\[code\]|\[QUOTE\]|\[quote\]|\[hide\]|\[HIDE\])/" , "" , $link);
$link = preg_replace("/\[\/(.+)\]/" , "" , $link);
$link = preg_replace("/(\[url=\")|(\[URL=\")/" , "" , $link);
$link = preg_replace("/\"\](.+)/" , "" , $link);
$link = str_replace(":", ":", $link);
if(substr($link, 0, 4) != 'http')
{
$link = preg_replace("/(.*)http:\/\//" , "" , $link);
$link = 'http://' . $link;
}
if(eregi('rapidshare\.com\/files\/', $link))
{
$file[] = $link;
$count++;
}
foreach($sites as $site)
{
if(eregi($site[0], $link))
{
check(trim($link), $y, $site[1],$site[2],$site[3]);
$count++;
}
}
}
}
if($file[0])
{
$rslinks = array_chunk($file, 50);
foreach($rslinks as $newlink)
{
$postfeilds = array();
$newlink = implode("\n" , $newlink);
$postfeilds['urls'] = $newlink;
$page = curl('http://rapidshare.com/cgi-bin/checkfiles.cgi', $postfeilds);
preg_match_all('/File is on server number/' , $page , $match);
$y = $y + count($match[0]);
unset($page);
}
}
if($_SESSION['checkatopic'])
{
echo "Topic ID : $_SESSION[checkatopic]<br />";
if($file[0])
{
$rslinks = array_chunk($file, 1);
foreach($rslinks as $rslink)
{
$postfeilds['urls'] = $rslink[0];
$page = curl('http://rapidshare.com/cgi-bin/checkfiles.cgi', $postfeilds);
preg_match('/File is on server number/' , $page , $match);
echo ($match[0] == 'File is on server number') ? "Active : $rslink[0]<br />" : "Dead : $rslink[0]<br />";
unset($page);
}
}
echo "Total link count is $count, and active links are $y<br />";
if($count == $y && $count != 0)
{
echo "All links are alive according to the bot<br />";
}
elseif($count > $y && $count != 0)
{
echo ($per < $perd) ? "Less than $perd links are dead, bot would report the topic<br />" : "$perd, or more than $perd links are dead, bot would bin this topic<br />";
}
elseif($count == 0)
{
echo "No relevant links found, bot would just ignore this topic<br />";
}
session_destroy();
exit('Exiting check a topic debug mode');
}
unset($rslinks);
unset($newlink);
unset($file);
unset($alllinks);
unset($link);
$realurl = $showurl . $tid;
if($count == $y && $count != 0 )
{
$post = preg_replace("/[\r?\n]+Links checked on(.*)by link checker bot/" , "" , $post); //Replace if it is already checked by LC bot
$post = mysql_real_escape_string($post . $pa); //Append the statement to the post
$qu3 = cc($qu3, "UPDATE `".$prefix."post` SET `pagetext` = '$post' WHERE `postid` = $pid");
$qu4 = cc($qu4, "SELECT `pagetext_html` FROM `".$prefix."postparsed` WHERE `postid` = '$pid'");
if(mysql_num_rows($qu4) > 0)
{
$postparsed = mysql_result($qu4, 'pagetext_html');
$postparsed = preg_replace("/\<\!--Botstart--\>(.*)\<\!--Botend--\>/" , "" , $postparsed);
$postparsed = mysql_real_escape_string($postparsed . $ppa);
$qu5 = cc($qu5, "UPDATE `".$prefix."postparsed` SET `pagetext_html` = '$postparsed' WHERE `postid` = '$pid'");
}
$qu6 = cc($qu6, "UPDATE `".$prefix."thread` SET `time_check` = ".time().", `ignore_check` = '0' WHERE `threadid` = '$tid'");
flush();
ob_flush();
echo "<a href=$realurl>Checked Topic ID : $tid</a><br />";
}
elseif($count > $y && $count != 0)
{
$per = (($count - $y) * 100)/$count;
if($per < $perd && $vbulletin->options['bot_ers'] == 1)
{
$uniq = md5(uniqid(rand() . time()));
$qu4 = cc($qu4, "INSERT INTO `".$prefix."thread` (`title`, `forumid`, `open`, `postusername`, `postuserid`, `dateline`, `notes`, `visible`, `lastpost`, `lastposter`) VALUES ('Post Reported by Bot' , '$reportforumid' , '1' , '$botname' , '$botid' , ".time().", '$uniq' , '1', ".time().", '$botname')");
$qu5 = cc($qu5, "SELECT `threadid` FROM `".$prefix."thread` WHERE `notes` = '$uniq'");
$rtid = mysql_result($qu5, 'threadid');
$newmessage = "This thread has some dead links and some alive links [URL=$realurl]".$realurl.'[/url]';
$newmessage = mysql_real_escape_string($newmessage);
$qu6 = cc($qu6, "INSERT INTO `".$prefix."post` (`threadid`, `username`, `userid`, `dateline`, `pagetext`) VALUES('$rtid' , '$botname', '$botid', ".time().", '$newmessage')");
$qu7 = cc($qu7, "SELECT `postid` FROM `".$prefix."post` WHERE `threadid` = '$rtid'");
$npid = mysql_result($qu7, 'postid');
$qu8 = cc($qu8, "UPDATE `".$prefix."thread` SET `time_check` = ".time()." WHERE `threadid` = '$tid'");
$qu9 = cc($qu9, "UPDATE `".$prefix."thread` SET `firstpostid` = '$npid', `lastpostid` = '$npid' WHERE `threadid` = '$rtid'");
$qu10 = cc($qu10, "UPDATE `".$prefix."forum` SET `threadcount` = `threadcount` + 1, `replycount` = `replycount` + 1 , `lastpost` = ".time()." ,`lastposter` = '$botname', `lastthreadid` = '$rtid', `lastthread` = 'Post Reported By Bot' WHERE `forumid` = '$reportforumid'");
flush();
ob_flush();
echo "<a href=$realurl>Post Reported : $tid</a><br />";
}
if($per >= $perd)
{
$pmtu = "[url=$realurl]$realurl" . '[/url]';
$pms = str_replace('{TOPICURL}', "$pmtu", $pms);
//Move the topic
$qu4 = cc($qu4, "UPDATE `".$prefix."thread` SET `forumid` = '$trash' WHERE `threadid` = '$tid'");
//Check if user exists
$qu5 = cc($qu5, "SELECT `username` FROM `".$prefix."user` WHERE `username` = '$uname'");
//If PM is disabled, keep it disabled else take the value of 1 if user exists.
$psf = ($pus == 0) ? 0 : ((mysql_num_rows($qu5) == 0) ? 0 : 1);
if($psf == 1)
{
//If PM is enabled, send it.
$force = 1;
cc(FALSE, FALSE);
$botpermissions['adminpermissions'] = 2;
$pmdm =& datamanager_init('PM', $vbulletin, ERRTYPE_ARRAY);
$pmdm->set('fromuserid', $botid);
$pmdm->set('fromusername', $botname);
$pmdm->set('title', $pmt);
$pmdm->set('message', $pms);
$pmdm->set_recipients($uname, $botpermissions);
$pmdm->set_info('receipt', false);
$pmdm->set_info('savecopy', false);
$pmdm->overridequota = true;
$pmdm->set('dateline', TIMENOW);
$pmdm->save();
$force = 0;
}
//Reply to the topic
$qu9 = cc($qu9, "INSERT INTO `".$prefix."post` (`threadid` , `parentid` , `username` , `userid` , `dateline` , `pagetext`, `ipaddress` , `visible`) VALUES ('$tid' , '$tid' , '$botname' , '$botid' , ".time()." , '$tr' , '127.0.0.1' , '1')");
$qu10 = cc($qu10, "UPDATE `".$prefix."thread` SET `replycount` = `replycount` + 1, `lastposter` = '$botname', `lastpost` = ".time()." WHERE `threadid` = '$tid'");
$qu11 = cc($qu11, "UPDATE `".$prefix."forum` SET `threadcount` = `threadcount` + 1, `replycount` = `replycount` + 1 , `lastpost` = ".time()." ,`lastposter` = '$botname', `lastthreadid` = '$tid', `lastthread` = '".mysql_real_escape_string($title)."' WHERE `forumid` = '$trash'");
$qu12 = cc($qu12, "UPDATE `".$prefix."user` SET `posts` = `posts` + 1, `lastpost` = ".time()." WHERE `userid` = '$botid'");
$qu13 = cc($qu13, "UPDATE `".$prefix."forum` SET `replycount` = `replycount` - $replies, `threadcount` = `threadcount` - 1 WHERE `forumid` = '$forum'");
flush();
ob_flush();
echo "<a href=$realurl>Moved to Trash Id : $tid</a><br />";
}
}
elseif($count == 0)
{
//If there are no matching links just add to ignore list.
$qu5 = cc($qu5, "UPDATE `".$prefix."thread` SET `ignore_check` = '1' WHERE `threadid` = '$tid'");
flush();
ob_flush();
echo "<a href=$realurl>No Relevant Links Found : $tid</a><br />";
}
unset($count);
unset($y);
}
?>This is the post it scanned and moved:Current Method : Checking unchecked topics
Dead : http://rapidshare.de/files/29195350/11.zip
Dead : http://rapidshare.de/files/29195350/11.zip
Dead : http://rapidshare.de/files/29195350/11.zip
Dead : http://depositfiles.com/en/files/294810
Dead : http://depositfiles.com/en/files/294810
Dead : http://depositfiles.com/en/files/294810
Connection re-established to mysql
http://rapidshare.de/files/29195350/11.zip
http://rapidshare.de/files/29195350/11.zip
Code: Select all
http://rapidshare.de/files/29195350/11.ziphttp://depositfiles.com/de/files/294810
http://depositfiles.com/de/files/294810
Code: Select all
http://depositfiles.com/de/files/294810http://rapidshare.com/files/352064/WSS5071608.rar
http://rapidshare.com/files/352064/WSS5071608.rar
Code: Select all
http://rapidshare.com/files/352064/WSS5071608.rarhttp://hotfile.com/dl/23262989/8de535f/
http://hotfile.com/dl/23262989/8de535f/
Code: Select all
http://hotfile.com/dl/23262989/8de535f/