<?php
if(!defined('IN_PHPBB')){exit;}
/**
*
* Triniwarez link checker bot!
* Developed by Litewarez
* Version 1.0
* Runs in co-operation with the PHPBB Framework!
*/
class TriniWarezBot
{
//Varaibles
var $bot_uid = false;
var $trash_fid = 0;
var $bot_logged_in = false;
var $percent_level = 40; //If the percentage of working links is less than this then we will post a reply!
/*Hosts - do not match schema or subdomains unless subdomains are needed!*/
//rapidshare, megaupload, hotfile
var $_HOSTS = array(
'rapidshare' => array(
'regex' => array('exp' => '#(rapidshare.com\/files/[0-9]+\/.+?)[\r\n|\r|\n|\t|\<]+#i','offset' => 1),
'callback' => array('TriniWarezBot','check_rapidshare')
),
'megaupload' => array(
'regex' => array('exp' => '#(megaupload.com/\?d=.*)[\r\n|\r|\n|\t|\<]+#i', 'offset' => 1),
'callback' => array('TriniWarezBot','check_megaupload')
)
);
public function __construct($buid)//Bot UID
{
$this->bot_uid = $buid;
}
public function initSession()
{
global $user,$auth;
$user->session_begin();
$auth->acl($user->data);
}
public function LogBotOnline()
{
global $user;
if($user->session_create($this->bot_uid,false,false,false))
{
$this->bot_logged_in = true;
}
return $this->bot_logged_in;
}
public function moveDeadTo($fid)
{
$this->trash_fid = $fid;
}
public function startChecking()
{
global $db,$auth,$user;
$sql_array = array(
'SELECT' => 't.topic_id,t.forum_id,t.topic_first_post_id,p.post_text,p.post_subject,p.bbcode_uid',
'FROM' => array(
TOPICS_TABLE => 't',
POSTS_TABLE => 'p'
),
'WHERE' => 't.topic_id = p.topic_id AND t.topic_first_post_id = p.post_id AND t.forum_id != ' . $this->trash_fid, //p.post_bot_last_scan = 0
);
$sql = $db->sql_build_query('SELECT',$sql_array);
$result = $db->sql_query_limit($sql,200);
$topics_to_move = array();
$topics_checked = array();
$reply_data = array();
while($post = $db->sql_fetchrow($result))
{
//Check to see if the bot has permissions to moderate the forum!
if(!$auth->acl_gets(array('m_move','m_reply'),$post['topic_id'])) //Make sure we can reply AND Move the tipic
{
continue;
}
$topics_checked[] = $post['topic_id'];
//phpBB always encodes there post data for storage!
$post_text = $this->bbcode_specialchars($post['post_text']);
$code_blocks = $this->getBBCodeContents($post_text,$post['bbcode_uid']);
//Set Defaults
$alive = 1;$dead = $total_links = 0;
foreach($code_blocks as $link_set)
{
//Ok so here were looping threw the possible code blocks and we need to extract the links from the hosts variable
foreach($this->_HOSTS as $host_id => $meta)
{
preg_match_all($meta['regex']['exp'],$link_set,$links);
$links = $links[$meta['regex']['offset']];
$total_links = ($total_links + count($links));
if(count($links) > 0)
{
if(is_callable($meta['callback'])) //is_callable due to cross object calling
{
$data = call_user_func($meta['callback'],$links);
if(isset($data['alive']) && isset($data['dead']))
{
$alive = ($alive + $data['alive']);
$dead = ($dead + $data['dead']);
}
}
}
}
}//foreach
if($dead > 0) //Do we have dead links and also remove division of 0
{
$percentage_of_topics = number_format( (($alive / $total_links) * 100),0);
if($percentage_of_topics < $this->percent_level)
{
$topics_to_move[] = $post['topic_id'];
$reply_data[] = $post; //set the post data here so we can reply to this entitiy!
}
}
}
//BULK MOVE TOPICS AND BULK UPDATE SCAN TIMES!
if(count($topics_to_move) > 0 && $this->trash_fid > 0)
{
//Add reply to that topic! - Also we may not have perms for trash bin so we add a reply before we move the topic!
foreach($reply_data as $topic_data)
{
$subject = $user->data['username'] . ' - Status report!';
$message = "[mod=".$user->data['username']."]I have detected that this topic contains ".$percentage_of_topics."% out of ".$total_links." links to be dead!\r\nif i am wrong then please as a moderator to review this![/mod]";
$bitfield = $uid = $flags = false;
generate_text_for_storage($message,$uid,$bitfield,$flags);
$data = array(
// General Posting Settings
'forum_id' => $topic_data['forum_id'],
'topic_id' => $topic_data['topic_id'],
'icon_id' => false,
'enable_bbcode' => true,
'enable_smilies'=> true,
'enable_urls' => true,
'enable_sig' => true,
'message' => $message,
'message_md5' => md5($message),
'bbcode_bitfield'=> $bitfield,'bbcode_uid'=> $uid,
'post_edit_locked'=> 0,
'topic_title' => $subject,
'notify_set' => false,
'notify' => false,
'post_time' => 0,
'forum_name' => '',
'enable_indexing'=> false
);
//Submit a reply!
//submit_post('reply',$subject,false,POST_NORMAL,$poll,$data,true,true);
echo $topic_data['topic_subject'] . ' Would have been moved!';
}
//Ad finaly move the topics!
//move_topics($topics_to_move,$this->trash_fid);
echo 'and moved to trash!<br />';
}
//Onced moved we can add the BOT LAST CHECK TIME!
if(count($topics_checked) > 0)
{
$sql = "UPDATE ".POSTS_TABLE." SET post_bot_last_scan = ".time()." WHERE " . $db->sql_in_set('topic_id',$topics_checked);
$db->sql_query($sql);
}
}//func
//Checkers
private function check_megaupload($links)
{
$alive = $dead = 0;
$checker_script = 'http://www.megaupload.com/mgr_linkcheck.php';
foreach($links as $link)
{
$link = str_replace(array('http://www.','http://','www.'),'',trim($link));
}
}
private function check_rapidshare($links)
{
//Links should always be treated as an array!
//With RS we need to split the links to get the ID and the filenames to work with there API
$meta = array();
$alive = 0;
$dead = 0;
foreach($links as $id => $link)
{
$tmp = explode('/',str_replace(array('http://www.','http://','www.'),'',$link));
$meta['id_numbers'][] = $tmp[2];
$meta['names'][] = $tmp[3];
}
$host = 'http://api.rapidshare.com/cgi-bin/rsapi.cgi';
$query = '?sub=checkfiles_v1&files=' . @implode(',',$meta['id_numbers']) . '&filenames=' . @implode(',',$meta['names']);
//File get contents WILL be replaced
$return = $this->getHTTPData($host.$query);
//Need to explode the data!
if($return)
{
$each_link_info = explode("\n",trim($return));
foreach($each_link_info as $link_info)
{
$meta = explode(',',$link_info);
$file_status = (int)$meta[4];
switch($file_status)
{
case 1:case 2:case 6:
$alive++;
break;
case 3:case 4:case 5:case 0:
$dead++;
break;
}
}
}
return array('alive' => $alive,'dead' => $dead);
}
//Private Functions
private function getHTTPData($url,$postData = false)
{
$handle = curl_init();
curl_setopt ($handle, CURLOPT_URL, $url);
curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt ($handle, CURLOPT_VERBOSE, 1);
curl_setopt ($handle, CURLOPT_HEADER ,true);
curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($handle, CURLOPT_HTTPHEADER, array('Expect:'));
if ($postData !== false)
{
curl_setopt($handle, CURLOPT_POSTFIELDS,http_build_query($postData));
}
$buffer = curl_exec($handle);
curl_close($handle);
return $buffer;
}
private function getBBCodeContents($post,$bbcode_id = false)
{
preg_match_all('#\[code'.($bbcode_id ? ':' . $bbcode_id : '').'\](.+?)\[\/code'.($bbcode_id ? ':' . $bbcode_id : '').'\]#ise',$post,$data,PREG_SET_ORDER);
$sets = array();
foreach($data as $item => $sub_item)
{
if(isset($sub_item[1])){$sets[] = $sub_item[1];}
}
return $sets;
}
private function bbcode_specialchars($text)
{
$str_from = array('<', '>', '[', ']', '.', ':');
$str_to = array('<', '>', '[', ']', '.', ':');
return str_replace($str_to, $str_from, $text);
}
}
?>