Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 4th, 2010 | Syntax: PHP | Size: 8.09 KB | Hits: 469 | Expires: Never
Copy text to clipboard
  1. <?php
  2. if(!defined('IN_PHPBB')){exit;}
  3. /**
  4.         *
  5.         *       Triniwarez link checker bot!
  6.         *       Developed by Litewarez
  7.         *       Version 1.0
  8.         *       Runs in co-operation with the PHPBB Framework!
  9. */
  10.  
  11. class TriniWarezBot
  12. {
  13.         //Varaibles
  14.         var $bot_uid = false;
  15.         var $trash_fid = 0;
  16.        
  17.         var $bot_logged_in = false;
  18.         var $percent_level = 40; //If the percentage of working links is less than this then we will post a reply!
  19.        
  20.         /*Hosts - do not match schema or subdomains unless subdomains are needed!*/
  21.         //rapidshare, megaupload, hotfile
  22.         var $_HOSTS = array(
  23.                 'rapidshare'    => array(
  24.                         'regex'         => array('exp' => '#(rapidshare.com\/files/[0-9]+\/.+?)[\r\n|\r|\n|\t|\<]+#i','offset' => 1),
  25.                         'callback'      => array('TriniWarezBot','check_rapidshare')
  26.                 ),
  27.                 'megaupload'    => array(
  28.                         'regex'         => array('exp' => '#(megaupload.com/\?d=.*)[\r\n|\r|\n|\t|\<]+#i', 'offset' => 1),
  29.                         'callback'      => array('TriniWarezBot','check_megaupload')
  30.                 )
  31.         );
  32.        
  33.         public function __construct($buid)//Bot UID
  34.         {
  35.                 $this->bot_uid = $buid;
  36.         }
  37.        
  38.         public function initSession()
  39.         {
  40.                 global $user,$auth;
  41.                 $user->session_begin();
  42.                 $auth->acl($user->data);
  43.         }
  44.        
  45.         public function LogBotOnline()
  46.         {
  47.                 global $user;
  48.                 if($user->session_create($this->bot_uid,false,false,false))
  49.                 {
  50.                         $this->bot_logged_in = true;
  51.                 }
  52.                 return $this->bot_logged_in;
  53.         }
  54.        
  55.         public function moveDeadTo($fid)
  56.         {
  57.                 $this->trash_fid = $fid;
  58.         }
  59.        
  60.         public function startChecking()
  61.         {
  62.                 global $db,$auth,$user;
  63.                 $sql_array = array(
  64.                         'SELECT'                        => 't.topic_id,t.forum_id,t.topic_first_post_id,p.post_text,p.post_subject,p.bbcode_uid',
  65.                         'FROM'                          => array(
  66.                                 TOPICS_TABLE    => 't',
  67.                                 POSTS_TABLE     => 'p'
  68.                         ),
  69.                         '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
  70.                 );
  71.                 $sql = $db->sql_build_query('SELECT',$sql_array);
  72.                 $result = $db->sql_query_limit($sql,200);
  73.                
  74.                 $topics_to_move = array();
  75.                 $topics_checked = array();
  76.                 $reply_data = array();
  77.                 while($post = $db->sql_fetchrow($result))
  78.                 {
  79.                         //Check to see if the bot has permissions to moderate the forum!
  80.                         if(!$auth->acl_gets(array('m_move','m_reply'),$post['topic_id'])) //Make sure we can reply AND Move the tipic
  81.                         {
  82.                                 continue;
  83.                         }
  84.                        
  85.                         $topics_checked[] = $post['topic_id'];
  86.                         //phpBB always encodes there post data for storage!
  87.                         $post_text = $this->bbcode_specialchars($post['post_text']);
  88.                         $code_blocks = $this->getBBCodeContents($post_text,$post['bbcode_uid']);
  89.                         //Set Defaults
  90.                         $alive = 1;$dead = $total_links = 0;
  91.                         foreach($code_blocks as $link_set)
  92.                         {
  93.                                 //Ok so here were looping threw the possible code blocks and we need to extract the links from the hosts variable
  94.                                 foreach($this->_HOSTS as $host_id => $meta)
  95.                                 {
  96.                                         preg_match_all($meta['regex']['exp'],$link_set,$links);
  97.                                         $links = $links[$meta['regex']['offset']];
  98.                                         $total_links = ($total_links + count($links));
  99.                                         if(count($links) > 0)
  100.                                         {
  101.                                                 if(is_callable($meta['callback'])) //is_callable due to cross object calling
  102.                                                 {
  103.                                                         $data = call_user_func($meta['callback'],$links);
  104.                                                         if(isset($data['alive']) && isset($data['dead']))
  105.                                                         {
  106.                                                                 $alive  = ($alive + $data['alive']);
  107.                                                                 $dead   = ($dead + $data['dead']);
  108.                                                         }
  109.                                                 }
  110.                                         }
  111.                                 }
  112.                         }//foreach
  113.                         if($dead > 0) //Do we have dead links and also remove division of 0
  114.                         {
  115.                                 $percentage_of_topics = number_format( (($alive / $total_links) * 100),0);
  116.                                 if($percentage_of_topics < $this->percent_level)
  117.                                 {
  118.                                         $topics_to_move[] = $post['topic_id'];
  119.                                         $reply_data[] = $post; //set the post data here so we can reply to this entitiy!
  120.                                 }
  121.                         }
  122.                 }
  123.                 //BULK MOVE TOPICS AND BULK UPDATE SCAN TIMES!
  124.                 if(count($topics_to_move) > 0 && $this->trash_fid > 0)
  125.                 {
  126.                         //Add reply to that topic! - Also we may not have perms for trash bin so we add a reply before we move the topic!
  127.                         foreach($reply_data as $topic_data)
  128.                         {
  129.                                 $subject = $user->data['username'] . ' - Status report!';
  130.                                 $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]";
  131.                                
  132.                                 $bitfield = $uid = $flags = false;
  133.                                 generate_text_for_storage($message,$uid,$bitfield,$flags);
  134.                                
  135.                                 $data = array(
  136.                                         // General Posting Settings
  137.                                         'forum_id'              => $topic_data['forum_id'],
  138.                                         'topic_id'              => $topic_data['topic_id'],
  139.                                         'icon_id'               => false,
  140.                                         'enable_bbcode' => true,
  141.                                         'enable_smilies'=> true,
  142.                                         'enable_urls'   => true,
  143.                                         'enable_sig'    => true,
  144.                                         'message'               => $message,
  145.                                         'message_md5'   => md5($message),
  146.                                         'bbcode_bitfield'=> $bitfield,'bbcode_uid'=> $uid,
  147.                                         'post_edit_locked'=> 0,
  148.                                         'topic_title'   => $subject,
  149.                                         'notify_set'    => false,
  150.                                         'notify'                => false,
  151.                                         'post_time'             => 0,
  152.                                         'forum_name'    => '',
  153.                                         'enable_indexing'=> false
  154.                                 );
  155.                                 //Submit a reply!
  156.                                 //submit_post('reply',$subject,false,POST_NORMAL,$poll,$data,true,true);
  157.                                 echo $topic_data['topic_subject'] . ' Would have been moved!';
  158.                         }
  159.                         //Ad finaly move the topics!
  160.                         //move_topics($topics_to_move,$this->trash_fid);
  161.                         echo 'and moved to trash!<br />';
  162.                 }
  163.                 //Onced moved we can add the BOT LAST CHECK TIME!
  164.                 if(count($topics_checked) > 0)
  165.                 {
  166.                         $sql = "UPDATE ".POSTS_TABLE." SET post_bot_last_scan = ".time()." WHERE " . $db->sql_in_set('topic_id',$topics_checked);
  167.                         $db->sql_query($sql);
  168.                 }
  169.         }//func
  170.        
  171.         //Checkers
  172.         private function check_megaupload($links)
  173.         {
  174.                 $alive = $dead = 0;
  175.                 $checker_script = 'http://www.megaupload.com/mgr_linkcheck.php';
  176.                 foreach($links as $link)
  177.                 {
  178.                         $link = str_replace(array('http://www.','http://','www.'),'',trim($link));
  179.                 }
  180.         }
  181.        
  182.         private function check_rapidshare($links)
  183.         {
  184.                 //Links should always be treated as an array!
  185.                 //With RS we need to split the links to get the ID and the filenames to work with there API
  186.                 $meta = array();
  187.                 $alive = 0;
  188.                 $dead = 0;
  189.                 foreach($links as $id => $link)
  190.                 {
  191.                         $tmp    = explode('/',str_replace(array('http://www.','http://','www.'),'',$link));
  192.                         $meta['id_numbers'][] = $tmp[2];
  193.                         $meta['names'][] = $tmp[3];
  194.                 }
  195.                 $host = 'http://api.rapidshare.com/cgi-bin/rsapi.cgi';
  196.                 $query = '?sub=checkfiles_v1&files=' . @implode(',',$meta['id_numbers']) . '&filenames=' . @implode(',',$meta['names']);
  197.                
  198.                 //File get contents WILL be replaced
  199.                 $return = $this->getHTTPData($host.$query);
  200.                 //Need to explode the data!
  201.                 if($return)
  202.                 {
  203.                         $each_link_info = explode("\n",trim($return));
  204.                         foreach($each_link_info as $link_info)
  205.                         {
  206.                                 $meta = explode(',',$link_info);
  207.                                 $file_status = (int)$meta[4];
  208.                                 switch($file_status)
  209.                                 {
  210.                                         case 1:case 2:case 6:
  211.                                                 $alive++;
  212.                                         break;
  213.                                         case 3:case 4:case 5:case 0:
  214.                                                 $dead++;
  215.                                         break;
  216.                                 }
  217.                         }
  218.                 }
  219.                 return array('alive' => $alive,'dead' => $dead);
  220.         }
  221.        
  222.         //Private Functions
  223.         private function getHTTPData($url,$postData = false)
  224.         {
  225.                 $handle = curl_init();
  226.                 curl_setopt ($handle, CURLOPT_URL, $url);
  227.                 curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 3);
  228.                 curl_setopt ($handle, CURLOPT_VERBOSE, 1);
  229.                 curl_setopt ($handle, CURLOPT_HEADER ,true);
  230.                 curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
  231.                 curl_setopt ($handle, CURLOPT_HTTPHEADER, array('Expect:'));
  232.                 if ($postData !== false)
  233.                 {
  234.                         curl_setopt($handle, CURLOPT_POSTFIELDS,http_build_query($postData));
  235.                 }
  236.                 $buffer = curl_exec($handle);
  237.                 curl_close($handle);
  238.                 return $buffer;
  239.         }
  240.        
  241.         private function getBBCodeContents($post,$bbcode_id = false)
  242.         {
  243.                 preg_match_all('#\[code'.($bbcode_id ? ':' . $bbcode_id : '').'\](.+?)\[\/code'.($bbcode_id ? ':' . $bbcode_id : '').'\]#ise',$post,$data,PREG_SET_ORDER);
  244.                 $sets = array();
  245.                 foreach($data as $item => $sub_item)
  246.                 {
  247.                         if(isset($sub_item[1])){$sets[] = $sub_item[1];}
  248.                 }
  249.                 return $sets;
  250.         }
  251.        
  252.         private function bbcode_specialchars($text)
  253.         {
  254.                 $str_from = array('<', '>', '[', ']', '.', ':');
  255.                 $str_to = array('&lt;', '&gt;', '&#91;', '&#93;', '&#46;', '&#58;');
  256.                 return str_replace($str_to, $str_from, $text);
  257.         }
  258. }
  259. ?>